2023-09-06 19:42:16 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2023-09-15 21:01:23 -03:00
|
|
|
#include "WebContentService.h"
|
2023-09-15 18:09:01 -03:00
|
|
|
#include "LadybirdServiceBase.h"
|
2023-09-14 02:22:44 -03:00
|
|
|
#include <AK/LexicalPath.h>
|
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
|
#include <LibCore/EventLoop.h>
|
|
|
|
|
#include <LibCore/LocalServer.h>
|
|
|
|
|
#include <LibCore/System.h>
|
|
|
|
|
#include <LibIPC/ConnectionFromClient.h>
|
2023-10-31 18:27:23 -03:00
|
|
|
#include <LibImageDecoderClient/Client.h>
|
2023-09-14 02:22:44 -03:00
|
|
|
#include <LibJS/Bytecode/Interpreter.h>
|
2024-08-28 07:08:38 -03:00
|
|
|
#include <LibMedia/Audio/Loader.h>
|
2024-08-07 00:51:20 -03:00
|
|
|
#include <LibRequests/RequestClient.h>
|
2023-09-14 02:22:44 -03:00
|
|
|
#include <LibWeb/Bindings/MainThreadVM.h>
|
|
|
|
|
#include <LibWeb/HTML/Window.h>
|
2023-09-17 16:17:53 -03:00
|
|
|
#include <LibWeb/Loader/ContentFilter.h>
|
2023-09-21 12:32:31 -03:00
|
|
|
#include <LibWeb/Loader/GeneratedPagesLoader.h>
|
2023-09-14 02:22:44 -03:00
|
|
|
#include <LibWeb/Loader/ResourceLoader.h>
|
2023-09-17 16:17:53 -03:00
|
|
|
#include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
|
2023-09-14 02:22:44 -03:00
|
|
|
#include <LibWeb/Platform/EventLoopPluginSerenity.h>
|
2025-06-10 12:49:36 -03:00
|
|
|
#include <LibWebView/HelperProcess.h>
|
|
|
|
|
#include <LibWebView/Plugins/FontPlugin.h>
|
|
|
|
|
#include <LibWebView/Plugins/ImageCodecPlugin.h>
|
|
|
|
|
#include <LibWebView/SiteIsolation.h>
|
|
|
|
|
#include <LibWebView/Utilities.h>
|
2023-09-14 02:22:44 -03:00
|
|
|
#include <WebContent/ConnectionFromClient.h>
|
|
|
|
|
#include <WebContent/PageHost.h>
|
2023-09-06 19:42:16 -03:00
|
|
|
|
2024-08-07 00:51:20 -03:00
|
|
|
static ErrorOr<NonnullRefPtr<Requests::RequestClient>> bind_request_server_service()
|
2023-09-15 21:27:46 -03:00
|
|
|
{
|
2024-08-07 00:51:20 -03:00
|
|
|
return bind_service<Requests::RequestClient>(&bind_request_server_java);
|
2023-09-15 21:27:46 -03:00
|
|
|
}
|
|
|
|
|
|
2024-07-03 16:59:58 -03:00
|
|
|
static ErrorOr<NonnullRefPtr<ImageDecoderClient::Client>> bind_image_decoder_service()
|
|
|
|
|
{
|
|
|
|
|
return bind_service<ImageDecoderClient::Client>(&bind_image_decoder_java);
|
|
|
|
|
}
|
2023-10-31 18:27:23 -03:00
|
|
|
|
2023-09-17 16:17:53 -03:00
|
|
|
static ErrorOr<void> load_content_filters();
|
2023-10-31 18:27:23 -03:00
|
|
|
|
2023-09-17 16:17:53 -03:00
|
|
|
static ErrorOr<void> load_autoplay_allowlist();
|
|
|
|
|
|
2024-05-04 09:08:53 -03:00
|
|
|
ErrorOr<int> service_main(int ipc_socket)
|
2023-09-14 02:22:44 -03:00
|
|
|
{
|
|
|
|
|
Core::EventLoop event_loop;
|
|
|
|
|
|
|
|
|
|
Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity);
|
2024-07-03 16:59:58 -03:00
|
|
|
|
|
|
|
|
auto image_decoder_client = TRY(bind_image_decoder_service());
|
2025-06-10 12:49:36 -03:00
|
|
|
Web::Platform::ImageCodecPlugin::install(*new WebView::ImageCodecPlugin(move(image_decoder_client)));
|
2023-09-14 02:22:44 -03:00
|
|
|
|
2025-06-10 12:49:36 -03:00
|
|
|
Web::Bindings::initialize_main_thread_vm(Web::Bindings::AgentType::SimilarOriginWindow);
|
|
|
|
|
|
2023-09-15 21:01:23 -03:00
|
|
|
auto request_server_client = TRY(bind_request_server_service());
|
2025-06-10 12:49:36 -03:00
|
|
|
Web::ResourceLoader::initialize(Web::Bindings::main_thread_vm().heap(), move(request_server_client));
|
2023-09-14 02:22:44 -03:00
|
|
|
|
|
|
|
|
bool is_layout_test_mode = false;
|
|
|
|
|
|
|
|
|
|
Web::HTML::Window::set_internals_object_exposed(is_layout_test_mode);
|
2025-06-10 12:49:36 -03:00
|
|
|
Web::Platform::FontPlugin::install(*new WebView::FontPlugin(is_layout_test_mode));
|
2023-09-14 02:22:44 -03:00
|
|
|
|
2025-06-10 12:49:36 -03:00
|
|
|
// Currently site isolation doesn't work on Android since everything is running
|
|
|
|
|
// in the same process. It would require an entire redesign of this port
|
|
|
|
|
// in order to make it work. For now, it's better to just disable it.
|
|
|
|
|
WebView::disable_site_isolation();
|
2023-09-14 02:22:44 -03:00
|
|
|
|
2023-09-17 16:17:53 -03:00
|
|
|
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());
|
|
|
|
|
|
2023-09-14 02:22:44 -03:00
|
|
|
auto webcontent_socket = TRY(Core::LocalSocket::adopt_fd(ipc_socket));
|
2025-06-10 12:49:36 -03:00
|
|
|
auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(make<IPC::Transport>(move(webcontent_socket))));
|
2023-09-14 02:22:44 -03:00
|
|
|
|
|
|
|
|
return event_loop.exec();
|
|
|
|
|
}
|
2023-09-15 21:01:23 -03:00
|
|
|
|
2023-09-15 21:27:46 -03:00
|
|
|
template<typename Client>
|
2024-05-04 09:08:53 -03:00
|
|
|
ErrorOr<NonnullRefPtr<Client>> bind_service(void (*bind_method)(int))
|
2023-09-15 21:01:23 -03:00
|
|
|
{
|
|
|
|
|
int socket_fds[2] {};
|
|
|
|
|
TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
|
|
|
|
|
|
|
|
|
|
int ui_fd = socket_fds[0];
|
|
|
|
|
int server_fd = socket_fds[1];
|
|
|
|
|
|
|
|
|
|
// NOTE: The java object takes ownership of the socket fds
|
2024-05-04 09:08:53 -03:00
|
|
|
(*bind_method)(server_fd);
|
2023-09-15 21:01:23 -03:00
|
|
|
|
|
|
|
|
auto socket = TRY(Core::LocalSocket::adopt_fd(ui_fd));
|
|
|
|
|
TRY(socket->set_blocking(true));
|
|
|
|
|
|
2025-06-10 12:49:36 -03:00
|
|
|
auto new_client = TRY(try_make_ref_counted<Client>(make<IPC::Transport>(move(socket))));
|
2023-09-15 21:01:23 -03:00
|
|
|
|
|
|
|
|
return new_client;
|
|
|
|
|
}
|
2023-09-17 16:17:53 -03:00
|
|
|
|
|
|
|
|
static ErrorOr<void> load_content_filters()
|
|
|
|
|
{
|
2025-06-10 12:49:36 -03:00
|
|
|
auto file_or_error = Core::File::open(ByteString::formatted("{}/res/ladybird/default-config/BrowserContentFilters.txt", WebView::s_ladybird_resource_root), Core::File::OpenMode::Read);
|
2023-09-17 16:17:53 -03:00
|
|
|
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()
|
|
|
|
|
{
|
2025-06-10 12:49:36 -03:00
|
|
|
auto file_or_error = Core::File::open(TRY(String::formatted("{}/res/ladybird/default-config/BrowserAutoplayAllowlist.txt", WebView::s_ladybird_resource_root)), Core::File::OpenMode::Read);
|
2023-09-17 16:17:53 -03:00
|
|
|
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();
|
2025-06-10 12:49:36 -03:00
|
|
|
autoplay_allowlist.enable_for_origins(origins);
|
2023-09-17 16:17:53 -03:00
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|