main.cpp 10.0 KB

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