Application.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibCore/ArgsParser.h>
  8. #include <LibCore/Environment.h>
  9. #include <LibCore/TimeZoneWatcher.h>
  10. #include <LibImageDecoderClient/Client.h>
  11. #include <LibWebView/Application.h>
  12. #include <LibWebView/URL.h>
  13. #include <LibWebView/UserAgent.h>
  14. #include <LibWebView/WebContentClient.h>
  15. namespace WebView {
  16. Application* Application::s_the = nullptr;
  17. Application::Application()
  18. {
  19. VERIFY(!s_the);
  20. s_the = this;
  21. // No need to monitor the system time zone if the TZ environment variable is set, as it overrides system preferences.
  22. if (!Core::Environment::has("TZ"sv)) {
  23. if (auto time_zone_watcher = Core::TimeZoneWatcher::create(); time_zone_watcher.is_error()) {
  24. warnln("Unable to monitor system time zone: {}", time_zone_watcher.error());
  25. } else {
  26. m_time_zone_watcher = time_zone_watcher.release_value();
  27. m_time_zone_watcher->on_time_zone_changed = []() {
  28. WebContentClient::for_each_client([&](WebView::WebContentClient& client) {
  29. client.async_system_time_zone_changed();
  30. return IterationDecision::Continue;
  31. });
  32. };
  33. }
  34. }
  35. m_process_manager.on_process_exited = [this](Process&& process) {
  36. process_did_exit(move(process));
  37. };
  38. }
  39. Application::~Application()
  40. {
  41. s_the = nullptr;
  42. }
  43. void Application::initialize(Main::Arguments const& arguments, URL::URL new_tab_page_url)
  44. {
  45. Vector<ByteString> raw_urls;
  46. Vector<ByteString> certificates;
  47. bool new_window = false;
  48. bool force_new_process = false;
  49. bool allow_popups = false;
  50. bool disable_sql_database = false;
  51. Optional<StringView> debug_process;
  52. Optional<StringView> profile_process;
  53. Optional<StringView> webdriver_content_ipc_path;
  54. Optional<StringView> user_agent_preset;
  55. bool log_all_js_exceptions = false;
  56. bool enable_idl_tracing = false;
  57. bool enable_http_cache = false;
  58. bool expose_internals_object = false;
  59. bool force_cpu_painting = false;
  60. bool force_fontconfig = false;
  61. Core::ArgsParser args_parser;
  62. args_parser.set_general_help("The Ladybird web browser :^)");
  63. args_parser.add_positional_argument(raw_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
  64. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  65. args_parser.add_option(new_window, "Force opening in a new window", "new-window", 'n');
  66. args_parser.add_option(force_new_process, "Force creation of new browser/chrome process", "force-new-process");
  67. args_parser.add_option(allow_popups, "Disable popup blocking by default", "allow-popups");
  68. args_parser.add_option(disable_sql_database, "Disable SQL database", "disable-sql-database");
  69. args_parser.add_option(debug_process, "Wait for a debugger to attach to the given process name (WebContent, RequestServer, etc.)", "debug-process", 0, "process-name");
  70. args_parser.add_option(profile_process, "Enable callgrind profiling of the given process name (WebContent, RequestServer, etc.)", "profile-process", 0, "process-name");
  71. args_parser.add_option(webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path", Core::ArgsParser::OptionHideMode::CommandLineAndMarkdown);
  72. args_parser.add_option(log_all_js_exceptions, "Log all JavaScript exceptions", "log-all-js-exceptions");
  73. args_parser.add_option(enable_idl_tracing, "Enable IDL tracing", "enable-idl-tracing");
  74. args_parser.add_option(enable_http_cache, "Enable HTTP cache", "enable-http-cache");
  75. args_parser.add_option(expose_internals_object, "Expose internals object", "expose-internals-object");
  76. args_parser.add_option(force_cpu_painting, "Force CPU painting", "force-cpu-painting");
  77. args_parser.add_option(force_fontconfig, "Force using fontconfig for font loading", "force-fontconfig");
  78. args_parser.add_option(Core::ArgsParser::Option {
  79. .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
  80. .help_string = "Name of the User-Agent preset to use in place of the default User-Agent",
  81. .long_name = "user-agent-preset",
  82. .value_name = "name",
  83. .accept_value = [&](StringView value) {
  84. user_agent_preset = normalize_user_agent_name(value);
  85. return user_agent_preset.has_value();
  86. },
  87. });
  88. create_platform_arguments(args_parser);
  89. args_parser.parse(arguments);
  90. Optional<ProcessType> debug_process_type;
  91. Optional<ProcessType> profile_process_type;
  92. if (debug_process.has_value())
  93. debug_process_type = process_type_from_name(*debug_process);
  94. if (profile_process.has_value())
  95. profile_process_type = process_type_from_name(*profile_process);
  96. m_chrome_options = {
  97. .urls = sanitize_urls(raw_urls, new_tab_page_url),
  98. .raw_urls = move(raw_urls),
  99. .new_tab_page_url = move(new_tab_page_url),
  100. .certificates = move(certificates),
  101. .new_window = new_window ? NewWindow::Yes : NewWindow::No,
  102. .force_new_process = force_new_process ? ForceNewProcess::Yes : ForceNewProcess::No,
  103. .allow_popups = allow_popups ? AllowPopups::Yes : AllowPopups::No,
  104. .disable_sql_database = disable_sql_database ? DisableSQLDatabase::Yes : DisableSQLDatabase::No,
  105. .debug_helper_process = move(debug_process_type),
  106. .profile_helper_process = move(profile_process_type),
  107. };
  108. if (webdriver_content_ipc_path.has_value())
  109. m_chrome_options.webdriver_content_ipc_path = *webdriver_content_ipc_path;
  110. m_web_content_options = {
  111. .command_line = MUST(String::join(' ', arguments.strings)),
  112. .executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))),
  113. .user_agent_preset = move(user_agent_preset),
  114. .log_all_js_exceptions = log_all_js_exceptions ? LogAllJSExceptions::Yes : LogAllJSExceptions::No,
  115. .enable_idl_tracing = enable_idl_tracing ? EnableIDLTracing::Yes : EnableIDLTracing::No,
  116. .enable_http_cache = enable_http_cache ? EnableHTTPCache::Yes : EnableHTTPCache::No,
  117. .expose_internals_object = expose_internals_object ? ExposeInternalsObject::Yes : ExposeInternalsObject::No,
  118. .force_cpu_painting = force_cpu_painting ? ForceCPUPainting::Yes : ForceCPUPainting::No,
  119. .force_fontconfig = force_fontconfig ? ForceFontconfig::Yes : ForceFontconfig::No,
  120. };
  121. create_platform_options(m_chrome_options, m_web_content_options);
  122. }
  123. int Application::execute()
  124. {
  125. int ret = m_event_loop.exec();
  126. m_in_shutdown = true;
  127. return ret;
  128. }
  129. void Application::add_child_process(WebView::Process&& process)
  130. {
  131. m_process_manager.add_process(move(process));
  132. }
  133. #if defined(AK_OS_MACH)
  134. void Application::set_process_mach_port(pid_t pid, Core::MachPort&& port)
  135. {
  136. m_process_manager.set_process_mach_port(pid, move(port));
  137. }
  138. #endif
  139. Optional<Process&> Application::find_process(pid_t pid)
  140. {
  141. return m_process_manager.find_process(pid);
  142. }
  143. void Application::update_process_statistics()
  144. {
  145. m_process_manager.update_all_process_statistics();
  146. }
  147. String Application::generate_process_statistics_html()
  148. {
  149. return m_process_manager.generate_html();
  150. }
  151. void Application::process_did_exit(Process&& process)
  152. {
  153. if (m_in_shutdown)
  154. return;
  155. dbgln_if(WEBVIEW_PROCESS_DEBUG, "Process {} died, type: {}", process.pid(), process_name_from_type(process.type()));
  156. switch (process.type()) {
  157. case ProcessType::ImageDecoder:
  158. if (auto client = process.client<ImageDecoderClient::Client>(); client.has_value()) {
  159. dbgln_if(WEBVIEW_PROCESS_DEBUG, "Restart ImageDecoder process");
  160. if (auto on_death = move(client->on_death)) {
  161. on_death();
  162. }
  163. }
  164. break;
  165. case ProcessType::RequestServer:
  166. dbgln_if(WEBVIEW_PROCESS_DEBUG, "FIXME: Restart request server");
  167. break;
  168. case ProcessType::WebContent:
  169. if (auto client = process.client<WebContentClient>(); client.has_value()) {
  170. dbgln_if(WEBVIEW_PROCESS_DEBUG, "Restart WebContent process");
  171. if (auto on_web_content_process_crash = move(client->on_web_content_process_crash))
  172. on_web_content_process_crash();
  173. }
  174. break;
  175. case ProcessType::WebWorker:
  176. dbgln_if(WEBVIEW_PROCESS_DEBUG, "WebWorker {} died, not sure what to do.", process.pid());
  177. break;
  178. case ProcessType::Chrome:
  179. dbgln("Invalid process type to be dying: Chrome");
  180. VERIFY_NOT_REACHED();
  181. }
  182. }
  183. }