Application.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <LibImageDecoderClient/Client.h>
  9. #include <LibWebView/Application.h>
  10. #include <LibWebView/URL.h>
  11. #include <LibWebView/WebContentClient.h>
  12. namespace WebView {
  13. Application* Application::s_the = nullptr;
  14. Application::Application()
  15. {
  16. VERIFY(!s_the);
  17. s_the = this;
  18. m_process_manager.on_process_exited = [this](Process&& process) {
  19. process_did_exit(move(process));
  20. };
  21. }
  22. Application::~Application()
  23. {
  24. s_the = nullptr;
  25. }
  26. void Application::initialize(Main::Arguments const& arguments, URL::URL new_tab_page_url)
  27. {
  28. Vector<ByteString> raw_urls;
  29. Vector<ByteString> certificates;
  30. bool new_window = false;
  31. bool force_new_process = false;
  32. bool allow_popups = false;
  33. bool disable_sql_database = false;
  34. Optional<StringView> webdriver_content_ipc_path;
  35. bool enable_callgrind_profiling = false;
  36. bool debug_web_content = false;
  37. bool log_all_js_exceptions = false;
  38. bool enable_idl_tracing = false;
  39. bool enable_http_cache = false;
  40. bool expose_internals_object = false;
  41. Core::ArgsParser args_parser;
  42. args_parser.set_general_help("The Ladybird web browser :^)");
  43. args_parser.add_positional_argument(raw_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
  44. args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
  45. args_parser.add_option(new_window, "Force opening in a new window", "new-window", 'n');
  46. args_parser.add_option(force_new_process, "Force creation of new browser/chrome process", "force-new-process");
  47. args_parser.add_option(allow_popups, "Disable popup blocking by default", "allow-popups");
  48. args_parser.add_option(disable_sql_database, "Disable SQL database", "disable-sql-database");
  49. args_parser.add_option(webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path", Core::ArgsParser::OptionHideMode::CommandLineAndMarkdown);
  50. args_parser.add_option(enable_callgrind_profiling, "Enable Callgrind profiling", "enable-callgrind-profiling", 'P');
  51. args_parser.add_option(debug_web_content, "Wait for debugger to attach to WebContent", "debug-web-content");
  52. args_parser.add_option(log_all_js_exceptions, "Log all JavaScript exceptions", "log-all-js-exceptions");
  53. args_parser.add_option(enable_idl_tracing, "Enable IDL tracing", "enable-idl-tracing");
  54. args_parser.add_option(enable_http_cache, "Enable HTTP cache", "enable-http-cache");
  55. args_parser.add_option(expose_internals_object, "Expose internals object", "expose-internals-object");
  56. create_platform_arguments(args_parser);
  57. args_parser.parse(arguments);
  58. m_chrome_options = {
  59. .urls = sanitize_urls(raw_urls, new_tab_page_url),
  60. .raw_urls = move(raw_urls),
  61. .new_tab_page_url = move(new_tab_page_url),
  62. .certificates = move(certificates),
  63. .new_window = new_window ? NewWindow::Yes : NewWindow::No,
  64. .force_new_process = force_new_process ? ForceNewProcess::Yes : ForceNewProcess::No,
  65. .allow_popups = allow_popups ? AllowPopups::Yes : AllowPopups::No,
  66. .disable_sql_database = disable_sql_database ? DisableSQLDatabase::Yes : DisableSQLDatabase::No,
  67. };
  68. if (webdriver_content_ipc_path.has_value())
  69. m_chrome_options.webdriver_content_ipc_path = *webdriver_content_ipc_path;
  70. m_web_content_options = {
  71. .command_line = MUST(String::join(' ', arguments.strings)),
  72. .executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))),
  73. .enable_callgrind_profiling = enable_callgrind_profiling ? EnableCallgrindProfiling::Yes : EnableCallgrindProfiling::No,
  74. .wait_for_debugger = debug_web_content ? WaitForDebugger::Yes : WaitForDebugger::No,
  75. .log_all_js_exceptions = log_all_js_exceptions ? LogAllJSExceptions::Yes : LogAllJSExceptions::No,
  76. .enable_idl_tracing = enable_idl_tracing ? EnableIDLTracing::Yes : EnableIDLTracing::No,
  77. .enable_http_cache = enable_http_cache ? EnableHTTPCache::Yes : EnableHTTPCache::No,
  78. .expose_internals_object = expose_internals_object ? ExposeInternalsObject::Yes : ExposeInternalsObject::No,
  79. };
  80. create_platform_options(m_chrome_options, m_web_content_options);
  81. }
  82. int Application::execute()
  83. {
  84. int ret = m_event_loop.exec();
  85. m_in_shutdown = true;
  86. return ret;
  87. }
  88. void Application::add_child_process(WebView::Process&& process)
  89. {
  90. m_process_manager.add_process(move(process));
  91. }
  92. #if defined(AK_OS_MACH)
  93. void Application::set_process_mach_port(pid_t pid, Core::MachPort&& port)
  94. {
  95. m_process_manager.set_process_mach_port(pid, move(port));
  96. }
  97. #endif
  98. Optional<Process&> Application::find_process(pid_t pid)
  99. {
  100. return m_process_manager.find_process(pid);
  101. }
  102. void Application::update_process_statistics()
  103. {
  104. m_process_manager.update_all_process_statistics();
  105. }
  106. String Application::generate_process_statistics_html()
  107. {
  108. return m_process_manager.generate_html();
  109. }
  110. void Application::process_did_exit(Process&& process)
  111. {
  112. if (m_in_shutdown)
  113. return;
  114. dbgln_if(WEBVIEW_PROCESS_DEBUG, "Process {} died, type: {}", process.pid(), process_name_from_type(process.type()));
  115. switch (process.type()) {
  116. case ProcessType::ImageDecoder:
  117. if (auto client = process.client<ImageDecoderClient::Client>(); client.has_value()) {
  118. dbgln_if(WEBVIEW_PROCESS_DEBUG, "Restart ImageDecoder process");
  119. if (auto on_death = move(client->on_death)) {
  120. on_death();
  121. }
  122. }
  123. break;
  124. case ProcessType::RequestServer:
  125. dbgln_if(WEBVIEW_PROCESS_DEBUG, "FIXME: Restart request server");
  126. break;
  127. case ProcessType::WebContent:
  128. if (auto client = process.client<WebContentClient>(); client.has_value()) {
  129. dbgln_if(WEBVIEW_PROCESS_DEBUG, "Restart WebContent process");
  130. if (auto on_web_content_process_crash = move(client->on_web_content_process_crash))
  131. on_web_content_process_crash();
  132. }
  133. break;
  134. case ProcessType::WebWorker:
  135. dbgln_if(WEBVIEW_PROCESS_DEBUG, "WebWorker {} died, not sure what to do.", process.pid());
  136. break;
  137. case ProcessType::Chrome:
  138. dbgln("Invalid process type to be dying: Chrome");
  139. VERIFY_NOT_REACHED();
  140. }
  141. }
  142. }