/* * Copyright (c) 2020-2023, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(HAVE_QT) # include # include # include # if defined(HAVE_QT_MULTIMEDIA) # include # endif #endif static ErrorOr load_content_filters(); static ErrorOr load_autoplay_allowlist(); static ErrorOr initialize_lagom_networking(Vector const& certificates); ErrorOr serenity_main(Main::Arguments arguments) { AK::set_rich_debug_enabled(true); #if defined(HAVE_QT) QCoreApplication app(arguments.argc, arguments.argv); Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt); #endif Core::EventLoop event_loop; platform_init(); Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity); Web::Platform::ImageCodecPlugin::install(*new Ladybird::ImageCodecPlugin); Web::Platform::AudioCodecPlugin::install_creation_hook([](auto loader) { #if defined(HAVE_QT_MULTIMEDIA) return Ladybird::AudioCodecPluginQt::create(move(loader)); #elif defined(AK_OS_MACOS) || defined(HAVE_PULSEAUDIO) return Web::Platform::AudioCodecPluginAgnostic::create(move(loader)); #else (void)loader; return Error::from_string_literal("Don't know how to initialize audio in this configuration!"); #endif }); StringView command_line {}; StringView executable_path {}; Vector certificates; int webcontent_fd_passing_socket { -1 }; bool is_layout_test_mode = false; bool use_lagom_networking = false; bool use_gpu_painting = false; bool wait_for_debugger = false; Core::ArgsParser args_parser; args_parser.add_option(command_line, "Chrome process command line", "command-line", 0, "command_line"); args_parser.add_option(executable_path, "Chrome process executable path", "executable-path", 0, "executable_path"); args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate"); args_parser.add_option(webcontent_fd_passing_socket, "File descriptor of the passing socket for the WebContent connection", "webcontent-fd-passing-socket", 'c', "webcontent_fd_passing_socket"); args_parser.add_option(is_layout_test_mode, "Is layout test mode", "layout-test-mode", 0); args_parser.add_option(use_lagom_networking, "Enable Lagom servers for networking", "use-lagom-networking", 0); args_parser.add_option(use_gpu_painting, "Enable GPU painting", "use-gpu-painting", 0); args_parser.add_option(wait_for_debugger, "Wait for debugger", "wait-for-debugger", 0); args_parser.parse(arguments); if (wait_for_debugger) { Core::Process::wait_for_debugger_and_break(); } Web::set_chrome_process_command_line(command_line); Web::set_chrome_process_executable_path(executable_path); if (use_gpu_painting) { WebContent::PageClient::set_use_gpu_painter(); } #if defined(HAVE_QT) if (!use_lagom_networking) Web::ResourceLoader::initialize(Ladybird::RequestManagerQt::create()); else #endif TRY(initialize_lagom_networking(certificates)); Web::HTML::Window::set_internals_object_exposed(is_layout_test_mode); VERIFY(webcontent_fd_passing_socket >= 0); Web::Platform::FontPlugin::install(*new Ladybird::FontPlugin(is_layout_test_mode)); 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::take_over_socket_from_system_server("WebContent"sv)); auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(move(webcontent_socket))); webcontent_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(webcontent_fd_passing_socket))); return event_loop.exec(); } static ErrorOr load_content_filters() { auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); auto resource = TRY(Core::Resource::load_from_uri("resource://ladybird/BrowserContentFilters.txt"sv)); auto ad_filter_list = TRY(InputBufferedSeekable::create(make(resource->data()))); Vector 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 load_autoplay_allowlist() { auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); auto resource = TRY(Core::Resource::load_from_uri("resource://ladybird/BrowserAutoplayAllowlist.txt"sv)); auto allowlist = TRY(InputBufferedSeekable::create(make(resource->data()))); Vector 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 {}; } static ErrorOr initialize_lagom_networking(Vector const& certificates) { auto candidate_request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv)); auto request_server_client = TRY(launch_request_server_process(candidate_request_server_paths, s_serenity_resource_root, certificates)); Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create(move(request_server_client)))); return {}; }