main.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/ImageCodecPlugin.h>
  9. #include <Ladybird/Utilities.h>
  10. #include <LibAudio/Loader.h>
  11. #include <LibCore/ArgsParser.h>
  12. #include <LibCore/EventLoop.h>
  13. #include <LibCore/LocalServer.h>
  14. #include <LibCore/Process.h>
  15. #include <LibCore/Resource.h>
  16. #include <LibCore/System.h>
  17. #include <LibCore/SystemServerTakeover.h>
  18. #include <LibIPC/ConnectionFromClient.h>
  19. #include <LibJS/Bytecode/Interpreter.h>
  20. #include <LibMain/Main.h>
  21. #include <LibProtocol/RequestClient.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 <LibCore/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(int request_server_socket);
  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. namespace Web::Fetch::Fetching {
  57. extern bool g_http_cache_enabled;
  58. }
  59. ErrorOr<int> serenity_main(Main::Arguments arguments)
  60. {
  61. AK::set_rich_debug_enabled(true);
  62. #if defined(HAVE_QT)
  63. QCoreApplication app(arguments.argc, arguments.argv);
  64. Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
  65. #endif
  66. Core::EventLoop event_loop;
  67. platform_init();
  68. Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity);
  69. Web::Platform::ImageCodecPlugin::install(*new Ladybird::ImageCodecPlugin);
  70. Web::Platform::AudioCodecPlugin::install_creation_hook([](auto loader) {
  71. #if defined(HAVE_QT_MULTIMEDIA)
  72. return Ladybird::AudioCodecPluginQt::create(move(loader));
  73. #elif defined(AK_OS_MACOS) || defined(HAVE_PULSEAUDIO)
  74. return Web::Platform::AudioCodecPluginAgnostic::create(move(loader));
  75. #else
  76. (void)loader;
  77. return Error::from_string_literal("Don't know how to initialize audio in this configuration!");
  78. #endif
  79. });
  80. StringView command_line {};
  81. StringView executable_path {};
  82. StringView mach_server_name {};
  83. Vector<ByteString> certificates;
  84. int request_server_socket { -1 };
  85. bool is_layout_test_mode = false;
  86. bool expose_internals_object = false;
  87. bool use_lagom_networking = false;
  88. bool use_gpu_painting = false;
  89. bool use_skia_painter = false;
  90. bool wait_for_debugger = false;
  91. bool log_all_js_exceptions = false;
  92. bool enable_idl_tracing = false;
  93. bool enable_http_cache = false;
  94. Core::ArgsParser args_parser;
  95. args_parser.add_option(command_line, "Chrome process command line", "command-line", 0, "command_line");
  96. args_parser.add_option(executable_path, "Chrome process executable path", "executable-path", 0, "executable_path");
  97. args_parser.add_option(request_server_socket, "File descriptor of the socket for the RequestServer connection", "request-server-socket", 'r', "request_server_socket");
  98. args_parser.add_option(is_layout_test_mode, "Is layout test mode", "layout-test-mode");
  99. args_parser.add_option(expose_internals_object, "Expose internals object", "expose-internals-object");
  100. args_parser.add_option(use_lagom_networking, "Enable Lagom servers for networking", "use-lagom-networking");
  101. args_parser.add_option(use_gpu_painting, "Enable GPU painting", "use-gpu-painting");
  102. args_parser.add_option(use_skia_painter, "Enable Skia painter", "use-skia-painting");
  103. args_parser.add_option(wait_for_debugger, "Wait for debugger", "wait-for-debugger");
  104. args_parser.add_option(mach_server_name, "Mach server name", "mach-server-name", 0, "mach_server_name");
  105. args_parser.add_option(log_all_js_exceptions, "Log all JavaScript exceptions", "log-all-js-exceptions");
  106. args_parser.add_option(enable_idl_tracing, "Enable IDL tracing", "enable-idl-tracing");
  107. args_parser.add_option(enable_http_cache, "Enable HTTP cache", "enable-http-cache");
  108. args_parser.parse(arguments);
  109. if (wait_for_debugger) {
  110. Core::Process::wait_for_debugger_and_break();
  111. }
  112. // Layout test mode implies internals object is exposed
  113. if (is_layout_test_mode)
  114. expose_internals_object = true;
  115. Web::set_chrome_process_command_line(command_line);
  116. Web::set_chrome_process_executable_path(executable_path);
  117. if (use_gpu_painting) {
  118. WebContent::PageClient::set_use_gpu_painter();
  119. }
  120. if (use_skia_painter) {
  121. WebContent::PageClient::set_use_skia_painter();
  122. }
  123. if (enable_http_cache) {
  124. Web::Fetch::Fetching::g_http_cache_enabled = true;
  125. }
  126. #if defined(AK_OS_MACOS)
  127. if (!mach_server_name.is_empty()) {
  128. auto server_port = Core::Platform::register_with_mach_server(mach_server_name);
  129. WebContent::BackingStoreManager::set_browser_mach_port(move(server_port));
  130. }
  131. #endif
  132. #if defined(HAVE_QT)
  133. if (!use_lagom_networking)
  134. Web::ResourceLoader::initialize(Ladybird::RequestManagerQt::create());
  135. else
  136. #endif
  137. TRY(initialize_lagom_networking(request_server_socket));
  138. Web::HTML::Window::set_internals_object_exposed(expose_internals_object);
  139. Web::Platform::FontPlugin::install(*new Ladybird::FontPlugin(is_layout_test_mode));
  140. TRY(Web::Bindings::initialize_main_thread_vm());
  141. if (log_all_js_exceptions) {
  142. JS::g_log_all_js_exceptions = true;
  143. }
  144. if (enable_idl_tracing) {
  145. Web::WebIDL::g_enable_idl_tracing = true;
  146. }
  147. auto maybe_content_filter_error = load_content_filters();
  148. if (maybe_content_filter_error.is_error())
  149. dbgln("Failed to load content filters: {}", maybe_content_filter_error.error());
  150. auto maybe_autoplay_allowlist_error = load_autoplay_allowlist();
  151. if (maybe_autoplay_allowlist_error.is_error())
  152. dbgln("Failed to load autoplay allowlist: {}", maybe_autoplay_allowlist_error.error());
  153. auto webcontent_socket = TRY(Core::take_over_socket_from_system_server("WebContent"sv));
  154. auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(move(webcontent_socket)));
  155. return event_loop.exec();
  156. }
  157. static ErrorOr<void> load_content_filters()
  158. {
  159. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  160. auto resource = TRY(Core::Resource::load_from_uri("resource://ladybird/BrowserContentFilters.txt"sv));
  161. auto ad_filter_list = TRY(InputBufferedSeekable<FixedMemoryStream>::create(make<FixedMemoryStream>(resource->data())));
  162. Vector<String> patterns;
  163. while (TRY(ad_filter_list->can_read_line())) {
  164. auto line = TRY(ad_filter_list->read_line(buffer));
  165. if (line.is_empty())
  166. continue;
  167. auto pattern = TRY(String::from_utf8(line));
  168. TRY(patterns.try_append(move(pattern)));
  169. }
  170. auto& content_filter = Web::ContentFilter::the();
  171. TRY(content_filter.set_patterns(patterns));
  172. return {};
  173. }
  174. static ErrorOr<void> load_autoplay_allowlist()
  175. {
  176. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  177. auto resource = TRY(Core::Resource::load_from_uri("resource://ladybird/BrowserAutoplayAllowlist.txt"sv));
  178. auto allowlist = TRY(InputBufferedSeekable<FixedMemoryStream>::create(make<FixedMemoryStream>(resource->data())));
  179. Vector<String> origins;
  180. while (TRY(allowlist->can_read_line())) {
  181. auto line = TRY(allowlist->read_line(buffer));
  182. if (line.is_empty())
  183. continue;
  184. auto domain = TRY(String::from_utf8(line));
  185. TRY(origins.try_append(move(domain)));
  186. }
  187. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  188. TRY(autoplay_allowlist.enable_for_origins(origins));
  189. return {};
  190. }
  191. ErrorOr<void> initialize_lagom_networking(int request_server_socket)
  192. {
  193. auto socket = TRY(Core::LocalSocket::adopt_fd(request_server_socket));
  194. TRY(socket->set_blocking(true));
  195. auto new_client = TRY(try_make_ref_counted<Protocol::RequestClient>(move(socket)));
  196. Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create(move(new_client))));
  197. return {};
  198. }