2023-08-20 20:14:31 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-04-28 16:30:34 +00:00
|
|
|
#include <AK/Enumerate.h>
|
2024-05-31 08:54:11 +00:00
|
|
|
#include <Ladybird/DefaultSettings.h>
|
2024-04-04 20:13:14 +00:00
|
|
|
#include <Ladybird/MachPortServer.h>
|
2023-12-01 17:18:40 +00:00
|
|
|
#include <Ladybird/Types.h>
|
2023-08-20 20:14:31 +00:00
|
|
|
#include <Ladybird/Utilities.h>
|
|
|
|
#include <LibCore/ArgsParser.h>
|
|
|
|
#include <LibGfx/Font/FontDatabase.h>
|
|
|
|
#include <LibMain/Main.h>
|
2024-06-30 04:24:01 +00:00
|
|
|
#include <LibWebView/Application.h>
|
2024-04-28 16:30:34 +00:00
|
|
|
#include <LibWebView/ChromeProcess.h>
|
2023-08-31 11:07:07 +00:00
|
|
|
#include <LibWebView/CookieJar.h>
|
|
|
|
#include <LibWebView/Database.h>
|
2023-10-13 15:04:07 +00:00
|
|
|
#include <LibWebView/URL.h>
|
2024-06-20 18:34:51 +00:00
|
|
|
#include <LibWebView/ViewImplementation.h>
|
|
|
|
#include <LibWebView/WebContentClient.h>
|
2023-08-20 20:14:31 +00:00
|
|
|
|
|
|
|
#import <Application/Application.h>
|
|
|
|
#import <Application/ApplicationDelegate.h>
|
|
|
|
#import <Application/EventLoopImplementation.h>
|
|
|
|
#import <UI/Tab.h>
|
|
|
|
#import <UI/TabController.h>
|
|
|
|
|
|
|
|
#if !__has_feature(objc_arc)
|
|
|
|
# error "This project requires ARC"
|
|
|
|
#endif
|
|
|
|
|
2024-04-28 16:30:34 +00:00
|
|
|
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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 20:14:31 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|
|
|
{
|
2023-12-11 18:19:41 +00:00
|
|
|
AK::set_rich_debug_enabled(true);
|
|
|
|
|
2024-04-15 23:39:48 +00:00
|
|
|
Application* application = [Application sharedApplication];
|
2023-08-20 20:14:31 +00:00
|
|
|
|
|
|
|
Core::EventLoopManager::install(*new Ladybird::CFEventLoopManager);
|
2024-06-30 04:24:01 +00:00
|
|
|
WebView::Application web_view_app(arguments.argc, arguments.argv);
|
2023-08-20 20:14:31 +00:00
|
|
|
|
|
|
|
platform_init();
|
|
|
|
|
2024-04-28 16:30:34 +00:00
|
|
|
Vector<ByteString> raw_urls;
|
2024-02-06 15:25:22 +00:00
|
|
|
Vector<ByteString> certificates;
|
2023-08-20 20:14:31 +00:00
|
|
|
StringView webdriver_content_ipc_path;
|
2023-12-20 16:52:17 +00:00
|
|
|
bool debug_web_content = false;
|
2024-04-16 06:02:41 +00:00
|
|
|
bool log_all_js_exceptions = false;
|
2024-06-22 16:53:11 +00:00
|
|
|
bool enable_http_cache = false;
|
2024-04-28 16:30:34 +00:00
|
|
|
bool new_window = false;
|
2024-07-09 13:38:58 +00:00
|
|
|
bool force_new_process = false;
|
2024-07-09 13:38:21 +00:00
|
|
|
bool allow_popups = false;
|
2023-08-20 20:14:31 +00:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
args_parser.set_general_help("The Ladybird web browser");
|
2023-10-13 15:04:07 +00:00
|
|
|
args_parser.add_positional_argument(raw_urls, "URLs to open", "url", Core::ArgsParser::Required::No);
|
2023-09-03 13:14:56 +00:00
|
|
|
args_parser.add_option(webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path", Core::ArgsParser::OptionHideMode::CommandLineAndMarkdown);
|
2024-04-20 20:34:56 +00:00
|
|
|
args_parser.add_option(debug_web_content, "Wait for debugger to attach to WebContent", "debug-web-content");
|
2024-02-06 15:25:22 +00:00
|
|
|
args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
|
2024-04-20 20:34:56 +00:00
|
|
|
args_parser.add_option(log_all_js_exceptions, "Log all JavaScript exceptions", "log-all-js-exceptions");
|
2024-06-22 16:53:11 +00:00
|
|
|
args_parser.add_option(enable_http_cache, "Enable HTTP cache", "enable-http-cache");
|
2024-04-28 16:30:34 +00:00
|
|
|
args_parser.add_option(new_window, "Force opening in a new window", "new-window", 'n');
|
2024-07-09 13:38:58 +00:00
|
|
|
args_parser.add_option(force_new_process, "Force creation of new browser/chrome process", "force-new-process");
|
2024-07-09 13:38:21 +00:00
|
|
|
args_parser.add_option(allow_popups, "Disable popup blocking by default", "allow-popups");
|
2023-08-20 20:14:31 +00:00
|
|
|
args_parser.parse(arguments);
|
|
|
|
|
2024-07-22 14:17:44 +00:00
|
|
|
auto chrome_process = TRY(WebView::ChromeProcess::create());
|
2024-07-09 13:38:58 +00:00
|
|
|
if (!force_new_process && TRY(chrome_process.connect(raw_urls, new_window)) == WebView::ChromeProcess::ProcessDisposition::ExitProcess) {
|
2024-04-28 16:30:34 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2024-04-04 20:13:14 +00:00
|
|
|
auto mach_port_server = make<Ladybird::MachPortServer>();
|
|
|
|
set_mach_server_name(mach_port_server->server_port_name());
|
2024-06-30 04:24:01 +00:00
|
|
|
mach_port_server->on_receive_child_mach_port = [&web_view_app](auto pid, auto port) {
|
|
|
|
web_view_app.set_process_mach_port(pid, move(port));
|
2024-04-04 20:13:14 +00:00
|
|
|
};
|
2024-06-20 18:34:51 +00:00
|
|
|
mach_port_server->on_receive_backing_stores = [](Ladybird::MachPortServer::BackingStoresMessage message) {
|
2024-06-30 04:24:01 +00:00
|
|
|
if (auto view = WebView::WebContentClient::view_for_pid_and_page_id(message.pid, message.page_id); view.has_value())
|
|
|
|
view->did_allocate_iosurface_backing_stores(message.front_backing_store_id, move(message.front_backing_store_port), message.back_backing_store_id, move(message.back_backing_store_port));
|
2024-06-20 18:34:51 +00:00
|
|
|
};
|
2024-04-04 20:13:14 +00:00
|
|
|
|
2024-06-04 20:34:32 +00:00
|
|
|
auto database = TRY(WebView::Database::create());
|
2023-08-31 11:07:07 +00:00
|
|
|
auto cookie_jar = TRY(WebView::CookieJar::create(*database));
|
2023-08-20 20:14:31 +00:00
|
|
|
|
2024-04-15 23:39:48 +00:00
|
|
|
// 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]);
|
|
|
|
|
2024-06-26 19:44:42 +00:00
|
|
|
TRY([application launchImageDecoder]);
|
|
|
|
|
2024-01-16 17:55:40 +00:00
|
|
|
StringBuilder command_line_builder;
|
|
|
|
command_line_builder.join(' ', arguments.strings);
|
2023-12-01 17:18:40 +00:00
|
|
|
Ladybird::WebContentOptions web_content_options {
|
2024-01-16 17:55:40 +00:00
|
|
|
.command_line = MUST(command_line_builder.to_string()),
|
|
|
|
.executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))),
|
2023-12-22 03:56:47 +00:00
|
|
|
.wait_for_debugger = debug_web_content ? Ladybird::WaitForDebugger::Yes : Ladybird::WaitForDebugger::No,
|
2024-04-16 06:02:41 +00:00
|
|
|
.log_all_js_exceptions = log_all_js_exceptions ? Ladybird::LogAllJSExceptions::Yes : Ladybird::LogAllJSExceptions::No,
|
2024-06-22 16:53:11 +00:00
|
|
|
.enable_http_cache = enable_http_cache ? Ladybird::EnableHTTPCache::Yes : Ladybird::EnableHTTPCache::No,
|
2023-12-01 17:18:40 +00:00
|
|
|
};
|
|
|
|
|
2024-04-28 16:30:34 +00:00
|
|
|
auto* delegate = [[ApplicationDelegate alloc] init:sanitize_urls(raw_urls)
|
|
|
|
newTabPageURL:URL::URL { Browser::default_new_tab_url }
|
2023-08-20 20:14:31 +00:00
|
|
|
withCookieJar:move(cookie_jar)
|
2023-12-01 17:18:40 +00:00
|
|
|
webContentOptions:web_content_options
|
2024-07-09 13:38:21 +00:00
|
|
|
webdriverContentIPCPath:webdriver_content_ipc_path
|
|
|
|
allowPopups:allow_popups];
|
2023-08-20 20:14:31 +00:00
|
|
|
|
|
|
|
[NSApp setDelegate:delegate];
|
|
|
|
|
2024-06-30 04:24:01 +00:00
|
|
|
return web_view_app.exec();
|
2023-08-20 20:14:31 +00:00
|
|
|
}
|