main.cpp 9.4 KB

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