Ladybird/AppKit: Add support for a singleton chrome process

This commit is contained in:
Timothy Flynn 2024-04-28 12:30:34 -04:00 committed by Andrew Kaster
parent bfb356fd6c
commit 1a1191cc6e
Notes: sideshowbarker 2024-07-17 06:40:35 +09:00

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/Enumerate.h>
#include <BrowserSettings/Defaults.h> #include <BrowserSettings/Defaults.h>
#include <Ladybird/MachPortServer.h> #include <Ladybird/MachPortServer.h>
#include <Ladybird/Types.h> #include <Ladybird/Types.h>
@ -12,6 +13,7 @@
#include <LibCore/EventLoop.h> #include <LibCore/EventLoop.h>
#include <LibGfx/Font/FontDatabase.h> #include <LibGfx/Font/FontDatabase.h>
#include <LibMain/Main.h> #include <LibMain/Main.h>
#include <LibWebView/ChromeProcess.h>
#include <LibWebView/CookieJar.h> #include <LibWebView/CookieJar.h>
#include <LibWebView/Database.h> #include <LibWebView/Database.h>
#include <LibWebView/ProcessManager.h> #include <LibWebView/ProcessManager.h>
@ -27,6 +29,45 @@
# error "This project requires ARC" # error "This project requires ARC"
#endif #endif
static Vector<URL::URL> sanitize_urls(Vector<ByteString> const& raw_urls)
{
Vector<URL::URL> sanitized_urls;
for (auto const& raw_url : raw_urls) {
if (auto url = WebView::sanitize_url(raw_url); url.has_value())
sanitized_urls.append(url.release_value());
}
if (sanitized_urls.is_empty()) {
URL::URL new_tab_page_url = Browser::default_new_tab_url;
sanitized_urls.append(move(new_tab_page_url));
}
return sanitized_urls;
}
enum class NewWindow {
No,
Yes,
};
static void open_urls_from_client(Vector<ByteString> const& raw_urls, NewWindow new_window)
{
ApplicationDelegate* delegate = [NSApp delegate];
Tab* tab = new_window == NewWindow::Yes ? nil : [delegate activeTab];
auto urls = sanitize_urls(raw_urls);
for (auto [i, url] : enumerate(urls)) {
auto activate_tab = i == 0 ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No;
auto* controller = [delegate createNewTab:url
fromTab:tab
activateTab:activate_tab];
tab = (Tab*)[controller window];
}
}
ErrorOr<int> serenity_main(Main::Arguments arguments) ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
AK::set_rich_debug_enabled(true); AK::set_rich_debug_enabled(true);
@ -42,12 +83,13 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Gfx::FontDatabase::set_default_font_query("Katica 10 400 0"); Gfx::FontDatabase::set_default_font_query("Katica 10 400 0");
Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0"); Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0");
Vector<StringView> raw_urls; Vector<ByteString> raw_urls;
Vector<ByteString> certificates; Vector<ByteString> certificates;
StringView webdriver_content_ipc_path; StringView webdriver_content_ipc_path;
bool use_gpu_painting = false; bool use_gpu_painting = false;
bool debug_web_content = false; bool debug_web_content = false;
bool log_all_js_exceptions = false; bool log_all_js_exceptions = false;
bool new_window = false;
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.set_general_help("The Ladybird web browser"); args_parser.set_general_help("The Ladybird web browser");
@ -57,8 +99,23 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.add_option(debug_web_content, "Wait for debugger to attach to WebContent", "debug-web-content"); args_parser.add_option(debug_web_content, "Wait for debugger to attach to WebContent", "debug-web-content");
args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate"); args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
args_parser.add_option(log_all_js_exceptions, "Log all JavaScript exceptions", "log-all-js-exceptions"); args_parser.add_option(log_all_js_exceptions, "Log all JavaScript exceptions", "log-all-js-exceptions");
args_parser.add_option(new_window, "Force opening in a new window", "new-window", 'n');
args_parser.parse(arguments); args_parser.parse(arguments);
WebView::ChromeProcess chrome_process;
if (TRY(chrome_process.connect(raw_urls, new_window)) == WebView::ChromeProcess::ProcessDisposition::ExitProcess) {
outln("Opening in existing process");
return 0;
}
chrome_process.on_new_tab = [&](auto const& raw_urls) {
open_urls_from_client(raw_urls, NewWindow::No);
};
chrome_process.on_new_window = [&](auto const& raw_urls) {
open_urls_from_client(raw_urls, NewWindow::Yes);
};
WebView::ProcessManager::initialize(); WebView::ProcessManager::initialize();
auto mach_port_server = make<Ladybird::MachPortServer>(); auto mach_port_server = make<Ladybird::MachPortServer>();
@ -74,17 +131,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
// FIXME: Create an abstraction to re-spawn the RequestServer and re-hook up its client hooks to each tab on crash // FIXME: Create an abstraction to re-spawn the RequestServer and re-hook up its client hooks to each tab on crash
TRY([application launchRequestServer:certificates]); TRY([application launchRequestServer:certificates]);
URL::URL new_tab_page_url = Browser::default_new_tab_url;
Vector<URL::URL> initial_urls;
for (auto const& raw_url : raw_urls) {
if (auto url = WebView::sanitize_url(raw_url); url.has_value())
initial_urls.append(url.release_value());
}
if (initial_urls.is_empty())
initial_urls.append(new_tab_page_url);
StringBuilder command_line_builder; StringBuilder command_line_builder;
command_line_builder.join(' ', arguments.strings); command_line_builder.join(' ', arguments.strings);
Ladybird::WebContentOptions web_content_options { Ladybird::WebContentOptions web_content_options {
@ -95,8 +141,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
.log_all_js_exceptions = log_all_js_exceptions ? Ladybird::LogAllJSExceptions::Yes : Ladybird::LogAllJSExceptions::No, .log_all_js_exceptions = log_all_js_exceptions ? Ladybird::LogAllJSExceptions::Yes : Ladybird::LogAllJSExceptions::No,
}; };
auto* delegate = [[ApplicationDelegate alloc] init:move(initial_urls) auto* delegate = [[ApplicationDelegate alloc] init:sanitize_urls(raw_urls)
newTabPageURL:move(new_tab_page_url) newTabPageURL:URL::URL { Browser::default_new_tab_url }
withCookieJar:move(cookie_jar) withCookieJar:move(cookie_jar)
webContentOptions:web_content_options webContentOptions:web_content_options
webdriverContentIPCPath:webdriver_content_ipc_path]; webdriverContentIPCPath:webdriver_content_ipc_path];