diff --git a/Ladybird/AppKit/main.mm b/Ladybird/AppKit/main.mm index 0f6a42b71fd..030ebae8193 100644 --- a/Ladybird/AppKit/main.mm +++ b/Ladybird/AppKit/main.mm @@ -4,6 +4,7 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include @@ -12,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +29,45 @@ # error "This project requires ARC" #endif +static Vector sanitize_urls(Vector const& raw_urls) +{ + Vector 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 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 serenity_main(Main::Arguments arguments) { AK::set_rich_debug_enabled(true); @@ -42,12 +83,13 @@ ErrorOr serenity_main(Main::Arguments arguments) Gfx::FontDatabase::set_default_font_query("Katica 10 400 0"); Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0"); - Vector raw_urls; + Vector raw_urls; Vector certificates; StringView webdriver_content_ipc_path; bool use_gpu_painting = false; bool debug_web_content = false; bool log_all_js_exceptions = false; + bool new_window = false; Core::ArgsParser args_parser; args_parser.set_general_help("The Ladybird web browser"); @@ -57,8 +99,23 @@ ErrorOr 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(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(new_window, "Force opening in a new window", "new-window", 'n'); 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(); auto mach_port_server = make(); @@ -74,17 +131,6 @@ ErrorOr 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 TRY([application launchRequestServer:certificates]); - URL::URL new_tab_page_url = Browser::default_new_tab_url; - Vector 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; command_line_builder.join(' ', arguments.strings); Ladybird::WebContentOptions web_content_options { @@ -95,8 +141,8 @@ ErrorOr serenity_main(Main::Arguments arguments) .log_all_js_exceptions = log_all_js_exceptions ? Ladybird::LogAllJSExceptions::Yes : Ladybird::LogAllJSExceptions::No, }; - auto* delegate = [[ApplicationDelegate alloc] init:move(initial_urls) - newTabPageURL:move(new_tab_page_url) + auto* delegate = [[ApplicationDelegate alloc] init:sanitize_urls(raw_urls) + newTabPageURL:URL::URL { Browser::default_new_tab_url } withCookieJar:move(cookie_jar) webContentOptions:web_content_options webdriverContentIPCPath:webdriver_content_ipc_path];