main.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Application.h"
  7. #include "BrowserWindow.h"
  8. #include "EventLoopImplementationQt.h"
  9. #include "Settings.h"
  10. #include "WebContentView.h"
  11. #include <Ladybird/HelperProcess.h>
  12. #include <Ladybird/Utilities.h>
  13. #include <LibCore/ArgsParser.h>
  14. #include <LibCore/EventLoop.h>
  15. #include <LibCore/Process.h>
  16. #include <LibCore/System.h>
  17. #include <LibGfx/Font/FontDatabase.h>
  18. #include <LibMain/Main.h>
  19. #include <LibWebView/ChromeProcess.h>
  20. #include <LibWebView/CookieJar.h>
  21. #include <LibWebView/Database.h>
  22. #include <LibWebView/ProcessManager.h>
  23. #include <LibWebView/URL.h>
  24. #if defined(AK_OS_MACOS)
  25. # include <Ladybird/MachPortServer.h>
  26. #endif
  27. namespace Ladybird {
  28. bool is_using_dark_system_theme(QWidget& widget)
  29. {
  30. // FIXME: Qt does not provide any method to query if the system is using a dark theme. We will have to implement
  31. // platform-specific methods if we wish to have better detection. For now, this inspects if Qt is using a
  32. // dark color for widget backgrounds using Rec. 709 luma coefficients.
  33. // https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients
  34. auto color = widget.palette().color(widget.backgroundRole());
  35. auto luma = 0.2126f * color.redF() + 0.7152f * color.greenF() + 0.0722f * color.blueF();
  36. return luma <= 0.5f;
  37. }
  38. }
  39. static ErrorOr<void> handle_attached_debugger()
  40. {
  41. #ifdef AK_OS_LINUX
  42. // Let's ignore SIGINT if we're being debugged because GDB
  43. // incorrectly forwards the signal to us even when it's set to
  44. // "nopass". See https://sourceware.org/bugzilla/show_bug.cgi?id=9425
  45. // for details.
  46. if (TRY(Core::Process::is_being_debugged())) {
  47. dbgln("Debugger is attached, ignoring SIGINT");
  48. TRY(Core::System::signal(SIGINT, SIG_IGN));
  49. }
  50. #endif
  51. return {};
  52. }
  53. static Vector<URL::URL> sanitize_urls(Vector<ByteString> const& raw_urls)
  54. {
  55. Vector<URL::URL> sanitized_urls;
  56. for (auto const& raw_url : raw_urls) {
  57. if (auto url = WebView::sanitize_url(raw_url); url.has_value())
  58. sanitized_urls.append(url.release_value());
  59. }
  60. return sanitized_urls;
  61. }
  62. ErrorOr<int> serenity_main(Main::Arguments arguments)
  63. {
  64. AK::set_rich_debug_enabled(true);
  65. Ladybird::Application app(arguments.argc, arguments.argv);
  66. Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
  67. Core::EventLoop event_loop;
  68. static_cast<Ladybird::EventLoopImplementationQt&>(event_loop.impl()).set_main_loop();
  69. TRY(handle_attached_debugger());
  70. platform_init();
  71. Vector<ByteString> raw_urls;
  72. StringView webdriver_content_ipc_path;
  73. Vector<ByteString> certificates;
  74. bool enable_callgrind_profiling = false;
  75. bool disable_sql_database = false;
  76. bool enable_qt_networking = false;
  77. bool expose_internals_object = false;
  78. bool use_gpu_painting = false;
  79. bool use_skia_painting = false;
  80. bool debug_web_content = false;
  81. bool log_all_js_exceptions = false;
  82. bool enable_idl_tracing = false;
  83. bool enable_http_cache = false;
  84. bool new_window = false;
  85. bool force_new_process = false;
  86. bool allow_popups = false;
  87. Core::ArgsParser args_parser;
  88. args_parser.set_general_help("The Ladybird web browser :^)");
  89. args_parser.add_positional_argument(raw_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
  90. args_parser.add_option(webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path", Core::ArgsParser::OptionHideMode::CommandLineAndMarkdown);
  91. args_parser.add_option(enable_callgrind_profiling, "Enable Callgrind profiling", "enable-callgrind-profiling", 'P');
  92. args_parser.add_option(disable_sql_database, "Disable SQL database", "disable-sql-database");
  93. args_parser.add_option(enable_qt_networking, "Enable Qt as the backend networking service", "enable-qt-networking");
  94. args_parser.add_option(use_gpu_painting, "Enable GPU painting", "enable-gpu-painting");
  95. args_parser.add_option(use_skia_painting, "Enable Skia painting", "enable-skia-painting");
  96. args_parser.add_option(debug_web_content, "Wait for debugger to attach to WebContent", "debug-web-content");
  97. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  98. args_parser.add_option(log_all_js_exceptions, "Log all JavaScript exceptions", "log-all-js-exceptions");
  99. args_parser.add_option(enable_idl_tracing, "Enable IDL tracing", "enable-idl-tracing");
  100. args_parser.add_option(enable_http_cache, "Enable HTTP cache", "enable-http-cache");
  101. args_parser.add_option(expose_internals_object, "Expose internals object", "expose-internals-object");
  102. args_parser.add_option(new_window, "Force opening in a new window", "new-window", 'n');
  103. args_parser.add_option(force_new_process, "Force creation of new browser/chrome process", "force-new-process");
  104. args_parser.add_option(allow_popups, "Disable popup blocking by default", "allow-popups");
  105. args_parser.parse(arguments);
  106. WebView::ChromeProcess chrome_process;
  107. if (!force_new_process && TRY(chrome_process.connect(raw_urls, new_window)) == WebView::ChromeProcess::ProcessDisposition::ExitProcess) {
  108. outln("Opening in existing process");
  109. return 0;
  110. }
  111. chrome_process.on_new_tab = [&](auto const& raw_urls) {
  112. auto& window = app.active_window();
  113. auto urls = sanitize_urls(raw_urls);
  114. for (size_t i = 0; i < urls.size(); ++i) {
  115. window.new_tab_from_url(urls[i], (i == 0) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No);
  116. }
  117. window.show();
  118. window.activateWindow();
  119. window.raise();
  120. };
  121. app.on_open_file = [&](auto file_url) {
  122. auto& window = app.active_window();
  123. window.view().load(file_url);
  124. };
  125. WebView::ProcessManager::initialize();
  126. #if defined(AK_OS_MACOS)
  127. auto mach_port_server = make<Ladybird::MachPortServer>();
  128. set_mach_server_name(mach_port_server->server_port_name());
  129. mach_port_server->on_receive_child_mach_port = [](auto pid, auto port) {
  130. WebView::ProcessManager::the().add_process(pid, move(port));
  131. };
  132. mach_port_server->on_receive_backing_stores = [](Ladybird::MachPortServer::BackingStoresMessage message) {
  133. auto view = WebView::WebContentClient::view_for_pid_and_page_id(message.pid, message.page_id);
  134. view->did_allocate_iosurface_backing_stores(message.front_backing_store_id, move(message.front_backing_store_port), message.back_backing_store_id, move(message.back_backing_store_port));
  135. };
  136. #endif
  137. RefPtr<WebView::Database> database;
  138. if (!disable_sql_database)
  139. database = TRY(WebView::Database::create());
  140. auto cookie_jar = database ? TRY(WebView::CookieJar::create(*database)) : WebView::CookieJar::create();
  141. // NOTE: WebWorker *always* needs a request server connection, even if WebContent uses Qt Networking
  142. // FIXME: Create an abstraction to re-spawn the RequestServer and re-hook up its client hooks to each tab on crash
  143. auto request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
  144. auto protocol_client = TRY(launch_request_server_process(request_server_paths, s_serenity_resource_root, certificates));
  145. app.request_server_client = protocol_client;
  146. StringBuilder command_line_builder;
  147. command_line_builder.join(' ', arguments.strings);
  148. Ladybird::WebContentOptions web_content_options {
  149. .command_line = MUST(command_line_builder.to_string()),
  150. .executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))),
  151. .enable_callgrind_profiling = enable_callgrind_profiling ? Ladybird::EnableCallgrindProfiling::Yes : Ladybird::EnableCallgrindProfiling::No,
  152. .enable_gpu_painting = use_gpu_painting ? Ladybird::EnableGPUPainting::Yes : Ladybird::EnableGPUPainting::No,
  153. .enable_skia_painting = use_skia_painting ? Ladybird::EnableSkiaPainting::Yes : Ladybird::EnableSkiaPainting::No,
  154. .use_lagom_networking = enable_qt_networking ? Ladybird::UseLagomNetworking::No : Ladybird::UseLagomNetworking::Yes,
  155. .wait_for_debugger = debug_web_content ? Ladybird::WaitForDebugger::Yes : Ladybird::WaitForDebugger::No,
  156. .log_all_js_exceptions = log_all_js_exceptions ? Ladybird::LogAllJSExceptions::Yes : Ladybird::LogAllJSExceptions::No,
  157. .enable_idl_tracing = enable_idl_tracing ? Ladybird::EnableIDLTracing::Yes : Ladybird::EnableIDLTracing::No,
  158. .enable_http_cache = enable_http_cache ? Ladybird::EnableHTTPCache::Yes : Ladybird::EnableHTTPCache::No,
  159. .expose_internals_object = expose_internals_object ? Ladybird::ExposeInternalsObject::Yes : Ladybird::ExposeInternalsObject::No,
  160. };
  161. chrome_process.on_new_window = [&](auto const& urls) {
  162. app.new_window(sanitize_urls(urls), *cookie_jar, web_content_options, webdriver_content_ipc_path, allow_popups);
  163. };
  164. auto& window = app.new_window(sanitize_urls(raw_urls), *cookie_jar, web_content_options, webdriver_content_ipc_path, allow_popups);
  165. window.setWindowTitle("Ladybird");
  166. if (Ladybird::Settings::the()->is_maximized()) {
  167. window.showMaximized();
  168. } else {
  169. auto last_position = Ladybird::Settings::the()->last_position();
  170. if (last_position.has_value())
  171. window.move(last_position.value());
  172. window.resize(Ladybird::Settings::the()->last_size());
  173. }
  174. window.show();
  175. return event_loop.exec();
  176. }