main.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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/CookieJar.h>
  20. #include <LibWebView/Database.h>
  21. #include <LibWebView/ProcessManager.h>
  22. #include <LibWebView/URL.h>
  23. #if defined(AK_OS_MACOS)
  24. # include <Ladybird/MachPortServer.h>
  25. #endif
  26. namespace Ladybird {
  27. bool is_using_dark_system_theme(QWidget& widget)
  28. {
  29. // FIXME: Qt does not provide any method to query if the system is using a dark theme. We will have to implement
  30. // platform-specific methods if we wish to have better detection. For now, this inspects if Qt is using a
  31. // dark color for widget backgrounds using Rec. 709 luma coefficients.
  32. // https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients
  33. auto color = widget.palette().color(widget.backgroundRole());
  34. auto luma = 0.2126f * color.redF() + 0.7152f * color.greenF() + 0.0722f * color.blueF();
  35. return luma <= 0.5f;
  36. }
  37. }
  38. static ErrorOr<void> handle_attached_debugger()
  39. {
  40. #ifdef AK_OS_LINUX
  41. // Let's ignore SIGINT if we're being debugged because GDB
  42. // incorrectly forwards the signal to us even when it's set to
  43. // "nopass". See https://sourceware.org/bugzilla/show_bug.cgi?id=9425
  44. // for details.
  45. if (TRY(Core::Process::is_being_debugged())) {
  46. dbgln("Debugger is attached, ignoring SIGINT");
  47. TRY(Core::System::signal(SIGINT, SIG_IGN));
  48. }
  49. #endif
  50. return {};
  51. }
  52. ErrorOr<int> serenity_main(Main::Arguments arguments)
  53. {
  54. AK::set_rich_debug_enabled(true);
  55. Ladybird::Application app(arguments.argc, arguments.argv);
  56. Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
  57. Core::EventLoop event_loop;
  58. static_cast<Ladybird::EventLoopImplementationQt&>(event_loop.impl()).set_main_loop();
  59. TRY(handle_attached_debugger());
  60. platform_init();
  61. // NOTE: We only instantiate this to ensure that Gfx::FontDatabase has its default queries initialized.
  62. Gfx::FontDatabase::set_default_font_query("Katica 10 400 0");
  63. Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0");
  64. Vector<StringView> raw_urls;
  65. StringView webdriver_content_ipc_path;
  66. Vector<ByteString> certificates;
  67. bool enable_callgrind_profiling = false;
  68. bool disable_sql_database = false;
  69. bool enable_qt_networking = false;
  70. bool expose_internals_object = false;
  71. bool use_gpu_painting = false;
  72. bool debug_web_content = false;
  73. bool log_all_js_exceptions = false;
  74. bool enable_idl_tracing = false;
  75. Core::ArgsParser args_parser;
  76. args_parser.set_general_help("The Ladybird web browser :^)");
  77. args_parser.add_positional_argument(raw_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
  78. args_parser.add_option(webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path", Core::ArgsParser::OptionHideMode::CommandLineAndMarkdown);
  79. args_parser.add_option(enable_callgrind_profiling, "Enable Callgrind profiling", "enable-callgrind-profiling", 'P');
  80. args_parser.add_option(disable_sql_database, "Disable SQL database", "disable-sql-database");
  81. args_parser.add_option(enable_qt_networking, "Enable Qt as the backend networking service", "enable-qt-networking");
  82. args_parser.add_option(use_gpu_painting, "Enable GPU painting", "enable-gpu-painting");
  83. args_parser.add_option(debug_web_content, "Wait for debugger to attach to WebContent", "debug-web-content");
  84. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  85. args_parser.add_option(log_all_js_exceptions, "Log all JavaScript exceptions", "log-all-js-exceptions");
  86. args_parser.add_option(enable_idl_tracing, "Enable IDL tracing", "enable-idl-tracing");
  87. args_parser.add_option(expose_internals_object, "Expose internals object", "expose-internals-object");
  88. args_parser.parse(arguments);
  89. WebView::ProcessManager::initialize();
  90. #if defined(AK_OS_MACOS)
  91. auto mach_port_server = make<Ladybird::MachPortServer>();
  92. set_mach_server_name(mach_port_server->server_port_name());
  93. mach_port_server->on_receive_child_mach_port = [](auto pid, auto port) {
  94. WebView::ProcessManager::the().add_process(pid, move(port));
  95. };
  96. #endif
  97. RefPtr<WebView::Database> database;
  98. if (!disable_sql_database) {
  99. auto sql_server_paths = TRY(get_paths_for_helper_process("SQLServer"sv));
  100. auto sql_client = TRY(launch_sql_server_process(sql_server_paths));
  101. database = TRY(WebView::Database::create(sql_client));
  102. }
  103. auto cookie_jar = database ? TRY(WebView::CookieJar::create(*database)) : WebView::CookieJar::create();
  104. Vector<URL::URL> initial_urls;
  105. for (auto const& raw_url : raw_urls) {
  106. if (auto url = WebView::sanitize_url(raw_url); url.has_value())
  107. initial_urls.append(url.release_value());
  108. }
  109. if (initial_urls.is_empty()) {
  110. auto new_tab_page = Ladybird::Settings::the()->new_tab_page();
  111. initial_urls.append(ak_string_from_qstring(new_tab_page));
  112. }
  113. // NOTE: WebWorker *always* needs a request server connection, even if WebContent uses Qt Networking
  114. // FIXME: Create an abstraction to re-spawn the RequestServer and re-hook up its client hooks to each tab on crash
  115. auto request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
  116. auto protocol_client = TRY(launch_request_server_process(request_server_paths, s_serenity_resource_root, certificates));
  117. app.request_server_client = protocol_client;
  118. StringBuilder command_line_builder;
  119. command_line_builder.join(' ', arguments.strings);
  120. Ladybird::WebContentOptions web_content_options {
  121. .command_line = MUST(command_line_builder.to_string()),
  122. .executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))),
  123. .enable_callgrind_profiling = enable_callgrind_profiling ? Ladybird::EnableCallgrindProfiling::Yes : Ladybird::EnableCallgrindProfiling::No,
  124. .enable_gpu_painting = use_gpu_painting ? Ladybird::EnableGPUPainting::Yes : Ladybird::EnableGPUPainting::No,
  125. .use_lagom_networking = enable_qt_networking ? Ladybird::UseLagomNetworking::No : Ladybird::UseLagomNetworking::Yes,
  126. .wait_for_debugger = debug_web_content ? Ladybird::WaitForDebugger::Yes : Ladybird::WaitForDebugger::No,
  127. .log_all_js_exceptions = log_all_js_exceptions ? Ladybird::LogAllJSExceptions::Yes : Ladybird::LogAllJSExceptions::No,
  128. .enable_idl_tracing = enable_idl_tracing ? Ladybird::EnableIDLTracing::Yes : Ladybird::EnableIDLTracing::No,
  129. .expose_internals_object = expose_internals_object ? Ladybird::ExposeInternalsObject::Yes : Ladybird::ExposeInternalsObject::No,
  130. };
  131. Ladybird::BrowserWindow window(initial_urls, cookie_jar, web_content_options, webdriver_content_ipc_path);
  132. window.setWindowTitle("Ladybird");
  133. app.on_open_file = [&](auto file_url) {
  134. window.view().load(file_url);
  135. };
  136. if (Ladybird::Settings::the()->is_maximized()) {
  137. window.showMaximized();
  138. } else {
  139. auto last_position = Ladybird::Settings::the()->last_position();
  140. if (last_position.has_value())
  141. window.move(last_position.value());
  142. window.resize(Ladybird::Settings::the()->last_size());
  143. }
  144. window.show();
  145. return event_loop.exec();
  146. }