Application.cpp 9.6 KB

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