mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 07:30:19 +00:00
Ladybird/Android: Enable content filters and Autoplay allowlist
Copy these over from the standard main.cpp for WebContent. The ideas are already starting to come together on how to unify these main files.
This commit is contained in:
parent
091a0e0b78
commit
de1bcf3597
Notes:
sideshowbarker
2024-07-16 23:34:44 +09:00
Author: https://github.com/ADKaster Commit: https://github.com/SerenityOS/serenity/commit/de1bcf3597 Pull-request: https://github.com/SerenityOS/serenity/pull/21100
1 changed files with 71 additions and 0 deletions
|
@ -20,8 +20,10 @@
|
||||||
#include <LibJS/Bytecode/Interpreter.h>
|
#include <LibJS/Bytecode/Interpreter.h>
|
||||||
#include <LibWeb/Bindings/MainThreadVM.h>
|
#include <LibWeb/Bindings/MainThreadVM.h>
|
||||||
#include <LibWeb/HTML/Window.h>
|
#include <LibWeb/HTML/Window.h>
|
||||||
|
#include <LibWeb/Loader/ContentFilter.h>
|
||||||
#include <LibWeb/Loader/FrameLoader.h>
|
#include <LibWeb/Loader/FrameLoader.h>
|
||||||
#include <LibWeb/Loader/ResourceLoader.h>
|
#include <LibWeb/Loader/ResourceLoader.h>
|
||||||
|
#include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
|
||||||
#include <LibWeb/Platform/AudioCodecPluginAgnostic.h>
|
#include <LibWeb/Platform/AudioCodecPluginAgnostic.h>
|
||||||
#include <LibWeb/Platform/EventLoopPluginSerenity.h>
|
#include <LibWeb/Platform/EventLoopPluginSerenity.h>
|
||||||
#include <LibWebView/RequestServerAdapter.h>
|
#include <LibWebView/RequestServerAdapter.h>
|
||||||
|
@ -42,6 +44,9 @@ static ErrorOr<NonnullRefPtr<Protocol::WebSocketClient>> bind_web_socket_service
|
||||||
return bind_service<Protocol::WebSocketClient>(&bind_web_socket_java);
|
return bind_service<Protocol::WebSocketClient>(&bind_web_socket_java);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ErrorOr<void> load_content_filters();
|
||||||
|
static ErrorOr<void> load_autoplay_allowlist();
|
||||||
|
|
||||||
ErrorOr<int> service_main(int ipc_socket, int fd_passing_socket)
|
ErrorOr<int> service_main(int ipc_socket, int fd_passing_socket)
|
||||||
{
|
{
|
||||||
Core::EventLoop event_loop;
|
Core::EventLoop event_loop;
|
||||||
|
@ -73,6 +78,14 @@ ErrorOr<int> service_main(int ipc_socket, int fd_passing_socket)
|
||||||
|
|
||||||
TRY(Web::Bindings::initialize_main_thread_vm());
|
TRY(Web::Bindings::initialize_main_thread_vm());
|
||||||
|
|
||||||
|
auto maybe_content_filter_error = load_content_filters();
|
||||||
|
if (maybe_content_filter_error.is_error())
|
||||||
|
dbgln("Failed to load content filters: {}", maybe_content_filter_error.error());
|
||||||
|
|
||||||
|
auto maybe_autoplay_allowlist_error = load_autoplay_allowlist();
|
||||||
|
if (maybe_autoplay_allowlist_error.is_error())
|
||||||
|
dbgln("Failed to load autoplay allowlist: {}", maybe_autoplay_allowlist_error.error());
|
||||||
|
|
||||||
auto webcontent_socket = TRY(Core::LocalSocket::adopt_fd(ipc_socket));
|
auto webcontent_socket = TRY(Core::LocalSocket::adopt_fd(ipc_socket));
|
||||||
auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(move(webcontent_socket)));
|
auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(move(webcontent_socket)));
|
||||||
webcontent_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(fd_passing_socket)));
|
webcontent_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(fd_passing_socket)));
|
||||||
|
@ -106,3 +119,61 @@ static ErrorOr<NonnullRefPtr<Client>> bind_service(void (*bind_method)(int, int)
|
||||||
|
|
||||||
return new_client;
|
return new_client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ErrorOr<void> load_content_filters()
|
||||||
|
{
|
||||||
|
auto file_or_error = Core::File::open(DeprecatedString::formatted("{}/home/anon/.config/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
|
||||||
|
if (file_or_error.is_error())
|
||||||
|
file_or_error = Core::File::open(DeprecatedString::formatted("{}/res/ladybird/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
|
||||||
|
if (file_or_error.is_error())
|
||||||
|
return file_or_error.release_error();
|
||||||
|
|
||||||
|
auto file = file_or_error.release_value();
|
||||||
|
auto ad_filter_list = TRY(Core::InputBufferedFile::create(move(file)));
|
||||||
|
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
|
||||||
|
|
||||||
|
Vector<String> patterns;
|
||||||
|
|
||||||
|
while (TRY(ad_filter_list->can_read_line())) {
|
||||||
|
auto line = TRY(ad_filter_list->read_line(buffer));
|
||||||
|
if (line.is_empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
auto pattern = TRY(String::from_utf8(line));
|
||||||
|
TRY(patterns.try_append(move(pattern)));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& content_filter = Web::ContentFilter::the();
|
||||||
|
TRY(content_filter.set_patterns(patterns));
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
static ErrorOr<void> load_autoplay_allowlist()
|
||||||
|
{
|
||||||
|
auto file_or_error = Core::File::open(TRY(String::formatted("{}/home/anon/.config/BrowserAutoplayAllowlist.txt", s_serenity_resource_root)), Core::File::OpenMode::Read);
|
||||||
|
if (file_or_error.is_error())
|
||||||
|
file_or_error = Core::File::open(TRY(String::formatted("{}/res/ladybird/BrowserAutoplayAllowlist.txt", s_serenity_resource_root)), Core::File::OpenMode::Read);
|
||||||
|
if (file_or_error.is_error())
|
||||||
|
return file_or_error.release_error();
|
||||||
|
|
||||||
|
auto file = file_or_error.release_value();
|
||||||
|
auto allowlist = TRY(Core::InputBufferedFile::create(move(file)));
|
||||||
|
auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
|
||||||
|
|
||||||
|
Vector<String> origins;
|
||||||
|
|
||||||
|
while (TRY(allowlist->can_read_line())) {
|
||||||
|
auto line = TRY(allowlist->read_line(buffer));
|
||||||
|
if (line.is_empty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
auto domain = TRY(String::from_utf8(line));
|
||||||
|
TRY(origins.try_append(move(domain)));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
|
||||||
|
TRY(autoplay_allowlist.enable_for_origins(origins));
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue