main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright (c) 2020-2024, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/LexicalPath.h>
  7. #include <LibCore/ArgsParser.h>
  8. #include <LibCore/EventLoop.h>
  9. #include <LibCore/LocalServer.h>
  10. #include <LibCore/Process.h>
  11. #include <LibCore/Resource.h>
  12. #include <LibCore/SystemServerTakeover.h>
  13. #include <LibGfx/Font/FontDatabase.h>
  14. #include <LibGfx/Font/PathFontProvider.h>
  15. #include <LibIPC/ConnectionFromClient.h>
  16. #include <LibJS/Bytecode/Interpreter.h>
  17. #include <LibMain/Main.h>
  18. #include <LibMedia/Audio/Loader.h>
  19. #include <LibRequests/RequestClient.h>
  20. #include <LibWeb/Bindings/MainThreadVM.h>
  21. #include <LibWeb/HTML/Window.h>
  22. #include <LibWeb/Loader/ContentFilter.h>
  23. #include <LibWeb/Loader/GeneratedPagesLoader.h>
  24. #include <LibWeb/Loader/ResourceLoader.h>
  25. #include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
  26. #include <LibWeb/Platform/AudioCodecPluginAgnostic.h>
  27. #include <LibWeb/Platform/EventLoopPluginSerenity.h>
  28. #include <UI/FontPlugin.h>
  29. #include <UI/ImageCodecPlugin.h>
  30. #include <UI/Utilities.h>
  31. #include <WebContent/ConnectionFromClient.h>
  32. #include <WebContent/PageClient.h>
  33. #include <WebContent/WebDriverConnection.h>
  34. #if defined(HAVE_QT)
  35. # include <QCoreApplication>
  36. # include <UI/Qt/EventLoopImplementationQt.h>
  37. # if defined(HAVE_QT_MULTIMEDIA)
  38. # include <UI/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(StringView config_path);
  45. static ErrorOr<void> load_autoplay_allowlist(StringView config_path);
  46. static ErrorOr<void> initialize_resource_loader(JS::Heap&, 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. auto config_path = ByteString::formatted("{}/ladybird/default-config", s_ladybird_resource_root);
  81. StringView mach_server_name {};
  82. Vector<ByteString> certificates;
  83. int request_server_socket { -1 };
  84. int image_decoder_socket { -1 };
  85. bool is_layout_test_mode = false;
  86. bool expose_internals_object = false;
  87. bool wait_for_debugger = false;
  88. bool log_all_js_exceptions = false;
  89. bool enable_idl_tracing = false;
  90. bool enable_http_cache = false;
  91. bool force_cpu_painting = false;
  92. bool force_fontconfig = false;
  93. bool collect_garbage_on_every_allocation = 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(config_path, "Ladybird configuration path", "config-path", 0, "config_path");
  98. args_parser.add_option(request_server_socket, "File descriptor of the socket for the RequestServer connection", "request-server-socket", 'r', "request_server_socket");
  99. args_parser.add_option(image_decoder_socket, "File descriptor of the socket for the ImageDecoder connection", "image-decoder-socket", 'i', "image_decoder_socket");
  100. args_parser.add_option(is_layout_test_mode, "Is layout test mode", "layout-test-mode");
  101. args_parser.add_option(expose_internals_object, "Expose internals object", "expose-internals-object");
  102. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  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.add_option(force_cpu_painting, "Force CPU painting", "force-cpu-painting");
  109. args_parser.add_option(force_fontconfig, "Force using fontconfig for font loading", "force-fontconfig");
  110. args_parser.add_option(collect_garbage_on_every_allocation, "Collect garbage after every JS heap allocation", "collect-garbage-on-every-allocation");
  111. args_parser.parse(arguments);
  112. if (wait_for_debugger) {
  113. Core::Process::wait_for_debugger_and_break();
  114. }
  115. auto& font_provider = static_cast<Gfx::PathFontProvider&>(Gfx::FontDatabase::the().install_system_font_provider(make<Gfx::PathFontProvider>()));
  116. if (force_fontconfig) {
  117. font_provider.set_name_but_fixme_should_create_custom_system_font_provider("FontConfig"_string);
  118. }
  119. font_provider.load_all_fonts_from_uri("resource://fonts"sv);
  120. // Layout test mode implies internals object is exposed and the Skia CPU backend is used
  121. if (is_layout_test_mode) {
  122. expose_internals_object = true;
  123. force_cpu_painting = true;
  124. }
  125. Web::set_chrome_process_command_line(command_line);
  126. Web::set_chrome_process_executable_path(executable_path);
  127. // Always use the CPU backend for layout tests, as the GPU backend is not deterministic
  128. WebContent::PageClient::set_use_skia_painter(force_cpu_painting ? WebContent::PageClient::UseSkiaPainter::CPUBackend : WebContent::PageClient::UseSkiaPainter::GPUBackendIfAvailable);
  129. if (enable_http_cache) {
  130. Web::Fetch::Fetching::g_http_cache_enabled = true;
  131. }
  132. #if defined(AK_OS_MACOS)
  133. if (!mach_server_name.is_empty()) {
  134. [[maybe_unused]] auto server_port = Core::Platform::register_with_mach_server(mach_server_name);
  135. // FIXME: For some reason, our implementation of IOSurface does not work on Intel macOS. Remove this conditional
  136. // compilation when that is resolved.
  137. # if ARCH(AARCH64)
  138. WebContent::BackingStoreManager::set_browser_mach_port(move(server_port));
  139. # endif
  140. }
  141. #endif
  142. TRY(initialize_image_decoder(image_decoder_socket));
  143. Web::HTML::Window::set_internals_object_exposed(expose_internals_object);
  144. Web::Platform::FontPlugin::install(*new Ladybird::FontPlugin(is_layout_test_mode, &font_provider));
  145. TRY(Web::Bindings::initialize_main_thread_vm(Web::HTML::EventLoop::Type::Window));
  146. if (collect_garbage_on_every_allocation)
  147. Web::Bindings::main_thread_vm().heap().set_should_collect_on_every_allocation(true);
  148. TRY(initialize_resource_loader(Web::Bindings::main_thread_vm().heap(), request_server_socket));
  149. if (log_all_js_exceptions) {
  150. JS::g_log_all_js_exceptions = true;
  151. }
  152. if (enable_idl_tracing) {
  153. Web::WebIDL::g_enable_idl_tracing = true;
  154. }
  155. auto maybe_content_filter_error = load_content_filters(config_path);
  156. if (maybe_content_filter_error.is_error())
  157. dbgln("Failed to load content filters: {}", maybe_content_filter_error.error());
  158. auto maybe_autoplay_allowlist_error = load_autoplay_allowlist(config_path);
  159. if (maybe_autoplay_allowlist_error.is_error())
  160. dbgln("Failed to load autoplay allowlist: {}", maybe_autoplay_allowlist_error.error());
  161. static_assert(IsSame<IPC::Transport, IPC::TransportSocket>, "Need to handle other IPC transports here");
  162. auto webcontent_socket = TRY(Core::take_over_socket_from_system_server("WebContent"sv));
  163. auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(Web::Bindings::main_thread_vm().heap(), IPC::Transport(move(webcontent_socket))));
  164. webcontent_client->on_image_decoder_connection = [&](auto& socket_file) {
  165. auto maybe_error = reinitialize_image_decoder(socket_file);
  166. if (maybe_error.is_error())
  167. dbgln("Failed to reinitialize image decoder: {}", maybe_error.error());
  168. };
  169. return event_loop.exec();
  170. }
  171. static ErrorOr<void> load_content_filters(StringView config_path)
  172. {
  173. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  174. auto file = TRY(Core::File::open(ByteString::formatted("{}/BrowserContentFilters.txt", config_path), Core::File::OpenMode::Read));
  175. auto ad_filter_list = TRY(Core::InputBufferedFile::create(move(file)));
  176. Vector<String> patterns;
  177. while (TRY(ad_filter_list->can_read_line())) {
  178. auto line = TRY(ad_filter_list->read_line(buffer));
  179. if (line.is_empty())
  180. continue;
  181. auto pattern = TRY(String::from_utf8(line));
  182. TRY(patterns.try_append(move(pattern)));
  183. }
  184. auto& content_filter = Web::ContentFilter::the();
  185. TRY(content_filter.set_patterns(patterns));
  186. return {};
  187. }
  188. static ErrorOr<void> load_autoplay_allowlist(StringView config_path)
  189. {
  190. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  191. auto file = TRY(Core::File::open(ByteString::formatted("{}/BrowserAutoplayAllowlist.txt", config_path), Core::File::OpenMode::Read));
  192. auto allowlist = TRY(Core::InputBufferedFile::create(move(file)));
  193. Vector<String> origins;
  194. while (TRY(allowlist->can_read_line())) {
  195. auto line = TRY(allowlist->read_line(buffer));
  196. if (line.is_empty())
  197. continue;
  198. auto domain = TRY(String::from_utf8(line));
  199. TRY(origins.try_append(move(domain)));
  200. }
  201. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  202. TRY(autoplay_allowlist.enable_for_origins(origins));
  203. return {};
  204. }
  205. ErrorOr<void> initialize_resource_loader(JS::Heap& heap, int request_server_socket)
  206. {
  207. static_assert(IsSame<IPC::Transport, IPC::TransportSocket>, "Need to handle other IPC transports here");
  208. auto socket = TRY(Core::LocalSocket::adopt_fd(request_server_socket));
  209. TRY(socket->set_blocking(true));
  210. auto request_client = TRY(try_make_ref_counted<Requests::RequestClient>(IPC::Transport(move(socket))));
  211. Web::ResourceLoader::initialize(heap, move(request_client));
  212. return {};
  213. }
  214. ErrorOr<void> initialize_image_decoder(int image_decoder_socket)
  215. {
  216. static_assert(IsSame<IPC::Transport, IPC::TransportSocket>, "Need to handle other IPC transports here");
  217. auto socket = TRY(Core::LocalSocket::adopt_fd(image_decoder_socket));
  218. TRY(socket->set_blocking(true));
  219. auto new_client = TRY(try_make_ref_counted<ImageDecoderClient::Client>(IPC::Transport(move(socket))));
  220. Web::Platform::ImageCodecPlugin::install(*new Ladybird::ImageCodecPlugin(move(new_client)));
  221. return {};
  222. }
  223. ErrorOr<void> reinitialize_image_decoder(IPC::File const& image_decoder_socket)
  224. {
  225. static_assert(IsSame<IPC::Transport, IPC::TransportSocket>, "Need to handle other IPC transports here");
  226. auto socket = TRY(Core::LocalSocket::adopt_fd(image_decoder_socket.take_fd()));
  227. TRY(socket->set_blocking(true));
  228. auto new_client = TRY(try_make_ref_counted<ImageDecoderClient::Client>(IPC::Transport(move(socket))));
  229. static_cast<Ladybird::ImageCodecPlugin&>(Web::Platform::ImageCodecPlugin::the()).set_client(move(new_client));
  230. return {};
  231. }