main.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "BrowserWindow.h"
  7. #include "EventLoopImplementationQt.h"
  8. #include "Settings.h"
  9. #include "WebContentView.h"
  10. #include <Ladybird/HelperProcess.h>
  11. #include <Ladybird/Utilities.h>
  12. #include <LibCore/ArgsParser.h>
  13. #include <LibCore/EventLoop.h>
  14. #include <LibCore/Process.h>
  15. #include <LibCore/System.h>
  16. #include <LibGfx/Font/FontDatabase.h>
  17. #include <LibMain/Main.h>
  18. #include <LibWebView/CookieJar.h>
  19. #include <LibWebView/Database.h>
  20. #include <LibWebView/ProcessManager.h>
  21. #include <LibWebView/URL.h>
  22. #include <QApplication>
  23. #include <QFileOpenEvent>
  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. class LadybirdApplication : public QApplication {
  54. public:
  55. LadybirdApplication(int& argc, char** argv)
  56. : QApplication(argc, argv)
  57. {
  58. }
  59. Function<void(URL::URL)> on_open_file;
  60. bool event(QEvent* event) override
  61. {
  62. switch (event->type()) {
  63. case QEvent::FileOpen: {
  64. if (!on_open_file)
  65. break;
  66. auto const& open_event = *static_cast<QFileOpenEvent const*>(event);
  67. auto file = ak_string_from_qstring(open_event.file());
  68. if (auto file_url = WebView::sanitize_url(file); file_url.has_value())
  69. on_open_file(file_url.release_value());
  70. }
  71. default:
  72. break;
  73. }
  74. return QApplication::event(event);
  75. }
  76. };
  77. ErrorOr<int> serenity_main(Main::Arguments arguments)
  78. {
  79. AK::set_rich_debug_enabled(true);
  80. LadybirdApplication app(arguments.argc, arguments.argv);
  81. Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
  82. Core::EventLoop event_loop;
  83. static_cast<Ladybird::EventLoopImplementationQt&>(event_loop.impl()).set_main_loop();
  84. TRY(handle_attached_debugger());
  85. platform_init();
  86. // NOTE: We only instantiate this to ensure that Gfx::FontDatabase has its default queries initialized.
  87. Gfx::FontDatabase::set_default_font_query("Katica 10 400 0");
  88. Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0");
  89. Vector<StringView> raw_urls;
  90. StringView webdriver_content_ipc_path;
  91. Vector<ByteString> certificates;
  92. bool enable_callgrind_profiling = false;
  93. bool disable_sql_database = false;
  94. bool enable_qt_networking = false;
  95. bool use_gpu_painting = false;
  96. bool debug_web_content = false;
  97. bool log_all_js_exceptions = false;
  98. bool enable_idl_tracing = false;
  99. Core::ArgsParser args_parser;
  100. args_parser.set_general_help("The Ladybird web browser :^)");
  101. args_parser.add_positional_argument(raw_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
  102. args_parser.add_option(webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path", Core::ArgsParser::OptionHideMode::CommandLineAndMarkdown);
  103. args_parser.add_option(enable_callgrind_profiling, "Enable Callgrind profiling", "enable-callgrind-profiling", 'P');
  104. args_parser.add_option(disable_sql_database, "Disable SQL database", "disable-sql-database", 0);
  105. args_parser.add_option(enable_qt_networking, "Enable Qt as the backend networking service", "enable-qt-networking", 0);
  106. args_parser.add_option(use_gpu_painting, "Enable GPU painting", "enable-gpu-painting", 0);
  107. args_parser.add_option(debug_web_content, "Wait for debugger to attach to WebContent", "debug-web-content", 0);
  108. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  109. args_parser.add_option(log_all_js_exceptions, "Log all JavaScript exceptions", "log-all-js-exceptions", 0);
  110. args_parser.add_option(enable_idl_tracing, "Enable IDL tracing", "enable-idl-tracing", 0);
  111. args_parser.parse(arguments);
  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. database = TRY(WebView::Database::create(move(sql_server_paths)));
  124. }
  125. auto cookie_jar = database ? TRY(WebView::CookieJar::create(*database)) : WebView::CookieJar::create();
  126. Vector<URL::URL> initial_urls;
  127. for (auto const& raw_url : raw_urls) {
  128. if (auto url = WebView::sanitize_url(raw_url); url.has_value())
  129. initial_urls.append(url.release_value());
  130. }
  131. if (initial_urls.is_empty()) {
  132. auto new_tab_page = Ladybird::Settings::the()->new_tab_page();
  133. initial_urls.append(ak_string_from_qstring(new_tab_page));
  134. }
  135. StringBuilder command_line_builder;
  136. command_line_builder.join(' ', arguments.strings);
  137. Ladybird::WebContentOptions web_content_options {
  138. .command_line = MUST(command_line_builder.to_string()),
  139. .executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))),
  140. .certificates = move(certificates),
  141. .enable_callgrind_profiling = enable_callgrind_profiling ? Ladybird::EnableCallgrindProfiling::Yes : Ladybird::EnableCallgrindProfiling::No,
  142. .enable_gpu_painting = use_gpu_painting ? Ladybird::EnableGPUPainting::Yes : Ladybird::EnableGPUPainting::No,
  143. .use_lagom_networking = enable_qt_networking ? Ladybird::UseLagomNetworking::No : Ladybird::UseLagomNetworking::Yes,
  144. .wait_for_debugger = debug_web_content ? Ladybird::WaitForDebugger::Yes : Ladybird::WaitForDebugger::No,
  145. .log_all_js_exceptions = log_all_js_exceptions ? Ladybird::LogAllJSExceptions::Yes : Ladybird::LogAllJSExceptions::No,
  146. .enable_idl_tracing = enable_idl_tracing ? Ladybird::EnableIDLTracing::Yes : Ladybird::EnableIDLTracing::No,
  147. };
  148. Ladybird::BrowserWindow window(initial_urls, cookie_jar, web_content_options, webdriver_content_ipc_path);
  149. window.setWindowTitle("Ladybird");
  150. app.on_open_file = [&](auto file_url) {
  151. window.view().load(file_url);
  152. };
  153. if (Ladybird::Settings::the()->is_maximized()) {
  154. window.showMaximized();
  155. } else {
  156. auto last_position = Ladybird::Settings::the()->last_position();
  157. if (last_position.has_value())
  158. window.move(last_position.value());
  159. window.resize(Ladybird::Settings::the()->last_size());
  160. }
  161. window.show();
  162. return event_loop.exec();
  163. }