main.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. if (sanitized_urls.is_empty()) {
  61. auto new_tab_page = Ladybird::Settings::the()->new_tab_page();
  62. sanitized_urls.append(ak_string_from_qstring(new_tab_page));
  63. }
  64. return sanitized_urls;
  65. }
  66. ErrorOr<int> serenity_main(Main::Arguments arguments)
  67. {
  68. AK::set_rich_debug_enabled(true);
  69. Ladybird::Application app(arguments.argc, arguments.argv);
  70. Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
  71. Core::EventLoop event_loop;
  72. static_cast<Ladybird::EventLoopImplementationQt&>(event_loop.impl()).set_main_loop();
  73. TRY(handle_attached_debugger());
  74. platform_init();
  75. // NOTE: We only instantiate this to ensure that Gfx::FontDatabase has its default queries initialized.
  76. Gfx::FontDatabase::set_default_font_query("Katica 10 400 0");
  77. Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0");
  78. Vector<ByteString> raw_urls;
  79. StringView webdriver_content_ipc_path;
  80. Vector<ByteString> certificates;
  81. bool enable_callgrind_profiling = false;
  82. bool disable_sql_database = false;
  83. bool enable_qt_networking = false;
  84. bool expose_internals_object = false;
  85. bool use_gpu_painting = false;
  86. bool debug_web_content = false;
  87. bool log_all_js_exceptions = false;
  88. bool enable_idl_tracing = 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_gpu_painting, "Enable GPU painting", "enable-gpu-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(expose_internals_object, "Expose internals object", "expose-internals-object");
  102. args_parser.parse(arguments);
  103. WebView::ChromeProcess chrome_process;
  104. auto new_window = false;
  105. if (TRY(chrome_process.connect(raw_urls, new_window)) == WebView::ChromeProcess::ProcessDisposition::ExitProcess) {
  106. outln("Opening in existing process");
  107. return 0;
  108. }
  109. chrome_process.on_new_window = [](auto const& urls) {
  110. dbgln("asked to open new window with urls: {}", urls);
  111. };
  112. WebView::ProcessManager::initialize();
  113. #if defined(AK_OS_MACOS)
  114. auto mach_port_server = make<Ladybird::MachPortServer>();
  115. set_mach_server_name(mach_port_server->server_port_name());
  116. mach_port_server->on_receive_child_mach_port = [](auto pid, auto port) {
  117. WebView::ProcessManager::the().add_process(pid, move(port));
  118. };
  119. #endif
  120. RefPtr<WebView::Database> database;
  121. if (!disable_sql_database) {
  122. auto sql_server_paths = TRY(get_paths_for_helper_process("SQLServer"sv));
  123. auto sql_client = TRY(launch_sql_server_process(sql_server_paths));
  124. database = TRY(WebView::Database::create(sql_client));
  125. }
  126. auto cookie_jar = database ? TRY(WebView::CookieJar::create(*database)) : WebView::CookieJar::create();
  127. // NOTE: WebWorker *always* needs a request server connection, even if WebContent uses Qt Networking
  128. // FIXME: Create an abstraction to re-spawn the RequestServer and re-hook up its client hooks to each tab on crash
  129. auto request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
  130. auto protocol_client = TRY(launch_request_server_process(request_server_paths, s_serenity_resource_root, certificates));
  131. app.request_server_client = protocol_client;
  132. StringBuilder command_line_builder;
  133. command_line_builder.join(' ', arguments.strings);
  134. Ladybird::WebContentOptions web_content_options {
  135. .command_line = MUST(command_line_builder.to_string()),
  136. .executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))),
  137. .enable_callgrind_profiling = enable_callgrind_profiling ? Ladybird::EnableCallgrindProfiling::Yes : Ladybird::EnableCallgrindProfiling::No,
  138. .enable_gpu_painting = use_gpu_painting ? Ladybird::EnableGPUPainting::Yes : Ladybird::EnableGPUPainting::No,
  139. .use_lagom_networking = enable_qt_networking ? Ladybird::UseLagomNetworking::No : Ladybird::UseLagomNetworking::Yes,
  140. .wait_for_debugger = debug_web_content ? Ladybird::WaitForDebugger::Yes : Ladybird::WaitForDebugger::No,
  141. .log_all_js_exceptions = log_all_js_exceptions ? Ladybird::LogAllJSExceptions::Yes : Ladybird::LogAllJSExceptions::No,
  142. .enable_idl_tracing = enable_idl_tracing ? Ladybird::EnableIDLTracing::Yes : Ladybird::EnableIDLTracing::No,
  143. .expose_internals_object = expose_internals_object ? Ladybird::ExposeInternalsObject::Yes : Ladybird::ExposeInternalsObject::No,
  144. };
  145. Ladybird::BrowserWindow window(sanitize_urls(raw_urls), cookie_jar, web_content_options, webdriver_content_ipc_path);
  146. window.setWindowTitle("Ladybird");
  147. chrome_process.on_new_tab = [&](auto const& raw_urls) {
  148. auto urls = sanitize_urls(raw_urls);
  149. for (size_t i = 0; i < urls.size(); ++i) {
  150. window.new_tab_from_url(urls[i], (i == 0) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No);
  151. }
  152. window.show();
  153. window.activateWindow();
  154. window.raise();
  155. };
  156. app.on_open_file = [&](auto file_url) {
  157. window.view().load(file_url);
  158. };
  159. if (Ladybird::Settings::the()->is_maximized()) {
  160. window.showMaximized();
  161. } else {
  162. auto last_position = Ladybird::Settings::the()->last_position();
  163. if (last_position.has_value())
  164. window.move(last_position.value());
  165. window.resize(Ladybird::Settings::the()->last_size());
  166. }
  167. window.show();
  168. return event_loop.exec();
  169. }