main.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2020-2024, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/LexicalPath.h>
  7. #include <Ladybird/FontPlugin.h>
  8. #include <Ladybird/HelperProcess.h>
  9. #include <Ladybird/ImageCodecPlugin.h>
  10. #include <Ladybird/Utilities.h>
  11. #include <LibAudio/Loader.h>
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibCore/EventLoop.h>
  14. #include <LibCore/LocalServer.h>
  15. #include <LibCore/Process.h>
  16. #include <LibCore/Resource.h>
  17. #include <LibCore/System.h>
  18. #include <LibCore/SystemServerTakeover.h>
  19. #include <LibIPC/ConnectionFromClient.h>
  20. #include <LibJS/Bytecode/Interpreter.h>
  21. #include <LibMain/Main.h>
  22. #include <LibWeb/Bindings/MainThreadVM.h>
  23. #include <LibWeb/HTML/Window.h>
  24. #include <LibWeb/Loader/ContentFilter.h>
  25. #include <LibWeb/Loader/GeneratedPagesLoader.h>
  26. #include <LibWeb/Loader/ResourceLoader.h>
  27. #include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
  28. #include <LibWeb/Platform/AudioCodecPluginAgnostic.h>
  29. #include <LibWeb/Platform/EventLoopPluginSerenity.h>
  30. #include <LibWeb/WebSockets/WebSocket.h>
  31. #include <LibWebView/RequestServerAdapter.h>
  32. #include <LibWebView/WebSocketClientAdapter.h>
  33. #include <WebContent/ConnectionFromClient.h>
  34. #include <WebContent/PageClient.h>
  35. #include <WebContent/WebDriverConnection.h>
  36. #if defined(HAVE_QT)
  37. # include <Ladybird/Qt/EventLoopImplementationQt.h>
  38. # include <Ladybird/Qt/RequestManagerQt.h>
  39. # include <QCoreApplication>
  40. # if defined(HAVE_QT_MULTIMEDIA)
  41. # include <Ladybird/Qt/AudioCodecPluginQt.h>
  42. # endif
  43. #endif
  44. #if defined(AK_OS_MACOS)
  45. # include <LibWebView/Platform/ProcessStatisticsMach.h>
  46. #endif
  47. static ErrorOr<void> load_content_filters();
  48. static ErrorOr<void> load_autoplay_allowlist();
  49. static ErrorOr<void> initialize_lagom_networking(Vector<ByteString> const& certificates);
  50. namespace JS {
  51. extern bool g_log_all_js_exceptions;
  52. }
  53. namespace Web::WebIDL {
  54. extern bool g_enable_idl_tracing;
  55. }
  56. ErrorOr<int> serenity_main(Main::Arguments arguments)
  57. {
  58. AK::set_rich_debug_enabled(true);
  59. #if defined(HAVE_QT)
  60. QCoreApplication app(arguments.argc, arguments.argv);
  61. Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
  62. #endif
  63. Core::EventLoop event_loop;
  64. platform_init();
  65. Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity);
  66. Web::Platform::ImageCodecPlugin::install(*new Ladybird::ImageCodecPlugin);
  67. Web::Platform::AudioCodecPlugin::install_creation_hook([](auto loader) {
  68. #if defined(HAVE_QT_MULTIMEDIA)
  69. return Ladybird::AudioCodecPluginQt::create(move(loader));
  70. #elif defined(AK_OS_MACOS) || defined(HAVE_PULSEAUDIO)
  71. return Web::Platform::AudioCodecPluginAgnostic::create(move(loader));
  72. #else
  73. (void)loader;
  74. return Error::from_string_literal("Don't know how to initialize audio in this configuration!");
  75. #endif
  76. });
  77. StringView command_line {};
  78. StringView executable_path {};
  79. StringView mach_server_name {};
  80. Vector<ByteString> certificates;
  81. int webcontent_fd_passing_socket { -1 };
  82. bool is_layout_test_mode = false;
  83. bool use_lagom_networking = false;
  84. bool use_gpu_painting = false;
  85. bool wait_for_debugger = false;
  86. bool log_all_js_exceptions = false;
  87. bool enable_idl_tracing = false;
  88. Core::ArgsParser args_parser;
  89. args_parser.add_option(command_line, "Chrome process command line", "command-line", 0, "command_line");
  90. args_parser.add_option(executable_path, "Chrome process executable path", "executable-path", 0, "executable_path");
  91. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  92. 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");
  93. args_parser.add_option(is_layout_test_mode, "Is layout test mode", "layout-test-mode", 0);
  94. args_parser.add_option(use_lagom_networking, "Enable Lagom servers for networking", "use-lagom-networking", 0);
  95. args_parser.add_option(use_gpu_painting, "Enable GPU painting", "use-gpu-painting", 0);
  96. args_parser.add_option(wait_for_debugger, "Wait for debugger", "wait-for-debugger", 0);
  97. args_parser.add_option(mach_server_name, "Mach server name", "mach-server-name", 0, "mach_server_name");
  98. args_parser.add_option(log_all_js_exceptions, "Log all JavaScript exceptions", "log-all-js-exceptions", 0);
  99. args_parser.add_option(enable_idl_tracing, "Enable IDL tracing", "enable-idl-tracing", 0);
  100. args_parser.parse(arguments);
  101. if (wait_for_debugger) {
  102. Core::Process::wait_for_debugger_and_break();
  103. }
  104. Web::set_chrome_process_command_line(command_line);
  105. Web::set_chrome_process_executable_path(executable_path);
  106. if (use_gpu_painting) {
  107. WebContent::PageClient::set_use_gpu_painter();
  108. }
  109. #if defined(AK_OS_MACOS)
  110. if (!mach_server_name.is_empty()) {
  111. WebView::register_with_mach_server(mach_server_name);
  112. }
  113. #endif
  114. #if defined(HAVE_QT)
  115. if (!use_lagom_networking)
  116. Web::ResourceLoader::initialize(Ladybird::RequestManagerQt::create());
  117. else
  118. #endif
  119. TRY(initialize_lagom_networking(certificates));
  120. Web::HTML::Window::set_internals_object_exposed(is_layout_test_mode);
  121. VERIFY(webcontent_fd_passing_socket >= 0);
  122. Web::Platform::FontPlugin::install(*new Ladybird::FontPlugin(is_layout_test_mode));
  123. TRY(Web::Bindings::initialize_main_thread_vm());
  124. if (log_all_js_exceptions) {
  125. JS::g_log_all_js_exceptions = true;
  126. }
  127. if (enable_idl_tracing) {
  128. Web::WebIDL::g_enable_idl_tracing = true;
  129. }
  130. auto maybe_content_filter_error = load_content_filters();
  131. if (maybe_content_filter_error.is_error())
  132. dbgln("Failed to load content filters: {}", maybe_content_filter_error.error());
  133. auto maybe_autoplay_allowlist_error = load_autoplay_allowlist();
  134. if (maybe_autoplay_allowlist_error.is_error())
  135. dbgln("Failed to load autoplay allowlist: {}", maybe_autoplay_allowlist_error.error());
  136. auto webcontent_socket = TRY(Core::take_over_socket_from_system_server("WebContent"sv));
  137. auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(move(webcontent_socket)));
  138. webcontent_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(webcontent_fd_passing_socket)));
  139. return event_loop.exec();
  140. }
  141. static ErrorOr<void> load_content_filters()
  142. {
  143. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  144. auto resource = TRY(Core::Resource::load_from_uri("resource://ladybird/BrowserContentFilters.txt"sv));
  145. auto ad_filter_list = TRY(InputBufferedSeekable<FixedMemoryStream>::create(make<FixedMemoryStream>(resource->data())));
  146. Vector<String> patterns;
  147. while (TRY(ad_filter_list->can_read_line())) {
  148. auto line = TRY(ad_filter_list->read_line(buffer));
  149. if (line.is_empty())
  150. continue;
  151. auto pattern = TRY(String::from_utf8(line));
  152. TRY(patterns.try_append(move(pattern)));
  153. }
  154. auto& content_filter = Web::ContentFilter::the();
  155. TRY(content_filter.set_patterns(patterns));
  156. return {};
  157. }
  158. static ErrorOr<void> load_autoplay_allowlist()
  159. {
  160. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  161. auto resource = TRY(Core::Resource::load_from_uri("resource://ladybird/BrowserAutoplayAllowlist.txt"sv));
  162. auto allowlist = TRY(InputBufferedSeekable<FixedMemoryStream>::create(make<FixedMemoryStream>(resource->data())));
  163. Vector<String> origins;
  164. while (TRY(allowlist->can_read_line())) {
  165. auto line = TRY(allowlist->read_line(buffer));
  166. if (line.is_empty())
  167. continue;
  168. auto domain = TRY(String::from_utf8(line));
  169. TRY(origins.try_append(move(domain)));
  170. }
  171. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  172. TRY(autoplay_allowlist.enable_for_origins(origins));
  173. return {};
  174. }
  175. static ErrorOr<void> initialize_lagom_networking(Vector<ByteString> const& certificates)
  176. {
  177. auto candidate_request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
  178. auto request_server_client = TRY(launch_request_server_process(candidate_request_server_paths, s_serenity_resource_root, certificates));
  179. Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create(move(request_server_client))));
  180. return {};
  181. }