From 3b5ac433ef22d971eaa4a37366a045c9cdc3a21d Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Thu, 4 Apr 2024 14:13:14 -0600 Subject: [PATCH] Ladybird: Use MachPortServer to get WebContent Mach ports on macOS --- Ladybird/AppKit/main.mm | 11 +++++++++-- Ladybird/HelperProcess.cpp | 5 +++++ Ladybird/Qt/main.cpp | 16 ++++++++++++++-- Ladybird/Utilities.cpp | 10 ++++++++++ Ladybird/Utilities.h | 2 ++ Ladybird/WebContent/main.cpp | 12 ++++++++++++ Userland/Libraries/LibWebView/ProcessManager.cpp | 6 ++++++ 7 files changed, 58 insertions(+), 4 deletions(-) diff --git a/Ladybird/AppKit/main.mm b/Ladybird/AppKit/main.mm index 4b14524c698..f816ea2855f 100644 --- a/Ladybird/AppKit/main.mm +++ b/Ladybird/AppKit/main.mm @@ -5,6 +5,7 @@ */ #include +#include #include #include #include @@ -56,6 +57,14 @@ ErrorOr serenity_main(Main::Arguments arguments) args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate"); args_parser.parse(arguments); + WebView::ProcessManager::initialize(); + + auto mach_port_server = make(); + set_mach_server_name(mach_port_server->server_port_name()); + mach_port_server->on_receive_child_mach_port = [](auto pid, auto port) { + WebView::ProcessManager::the().add_process(pid, move(port)); + }; + auto sql_server_paths = TRY(get_paths_for_helper_process("SQLServer"sv)); auto database = TRY(WebView::Database::create(move(sql_server_paths))); auto cookie_jar = TRY(WebView::CookieJar::create(*database)); @@ -82,8 +91,6 @@ ErrorOr serenity_main(Main::Arguments arguments) .wait_for_debugger = debug_web_content ? Ladybird::WaitForDebugger::Yes : Ladybird::WaitForDebugger::No, }; - WebView::ProcessManager::initialize(); - auto* delegate = [[ApplicationDelegate alloc] init:move(initial_urls) newTabPageURL:move(new_tab_page_url) withCookieJar:move(cookie_jar) diff --git a/Ladybird/HelperProcess.cpp b/Ladybird/HelperProcess.cpp index 2a37e0c3c05..7af81da77c9 100644 --- a/Ladybird/HelperProcess.cpp +++ b/Ladybird/HelperProcess.cpp @@ -5,6 +5,7 @@ */ #include "HelperProcess.h" +#include "Utilities.h" #include #include @@ -64,6 +65,10 @@ ErrorOr> launch_web_content_process( arguments.append("--use-gpu-painting"sv); if (web_content_options.wait_for_debugger == Ladybird::WaitForDebugger::Yes) arguments.append("--wait-for-debugger"sv); + if (auto server = mach_server_name(); server.has_value()) { + arguments.append("--mach-server-name"sv); + arguments.append(server.value()); + } Vector certificate_args; for (auto const& certificate : web_content_options.certificates) { certificate_args.append(ByteString::formatted("--certificate={}", certificate)); diff --git a/Ladybird/Qt/main.cpp b/Ladybird/Qt/main.cpp index c78230dbd0d..096b9c7e22d 100644 --- a/Ladybird/Qt/main.cpp +++ b/Ladybird/Qt/main.cpp @@ -23,6 +23,10 @@ #include #include +#if defined(AK_OS_MACOS) +# include +#endif + namespace Ladybird { bool is_using_dark_system_theme(QWidget& widget) @@ -125,6 +129,16 @@ ErrorOr serenity_main(Main::Arguments arguments) args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate"); args_parser.parse(arguments); + WebView::ProcessManager::initialize(); + +#if defined(AK_OS_MACOS) + auto mach_port_server = make(); + set_mach_server_name(mach_port_server->server_port_name()); + mach_port_server->on_receive_child_mach_port = [](auto pid, auto port) { + WebView::ProcessManager::the().add_process(pid, move(port)); + }; +#endif + RefPtr database; if (!disable_sql_database) { @@ -158,8 +172,6 @@ ErrorOr serenity_main(Main::Arguments arguments) .wait_for_debugger = debug_web_content ? Ladybird::WaitForDebugger::Yes : Ladybird::WaitForDebugger::No, }; - WebView::ProcessManager::initialize(); - Ladybird::BrowserWindow window(initial_urls, cookie_jar, web_content_options, webdriver_content_ipc_path); window.setWindowTitle("Ladybird"); diff --git a/Ladybird/Utilities.cpp b/Ladybird/Utilities.cpp index 96611fc2fca..fff19182eb8 100644 --- a/Ladybird/Utilities.cpp +++ b/Ladybird/Utilities.cpp @@ -25,6 +25,16 @@ constexpr auto libexec_path = "libexec"sv; ByteString s_serenity_resource_root; +Optional s_mach_server_name; +Optional mach_server_name() +{ + return s_mach_server_name; +} +void set_mach_server_name(ByteString name) +{ + s_mach_server_name = move(name); +} + ErrorOr application_directory() { auto current_executable_path = TRY(Core::System::current_executable_path()); diff --git a/Ladybird/Utilities.h b/Ladybird/Utilities.h index a033f7e8d2e..d3cbc935dfb 100644 --- a/Ladybird/Utilities.h +++ b/Ladybird/Utilities.h @@ -17,3 +17,5 @@ ErrorOr application_directory(); ErrorOr> get_paths_for_helper_process(StringView process_name); extern ByteString s_serenity_resource_root; +Optional mach_server_name(); +void set_mach_server_name(ByteString name); diff --git a/Ladybird/WebContent/main.cpp b/Ladybird/WebContent/main.cpp index 066bf9f3bd3..5a6ae7eee04 100644 --- a/Ladybird/WebContent/main.cpp +++ b/Ladybird/WebContent/main.cpp @@ -45,6 +45,10 @@ # endif #endif +#if defined(AK_OS_MACOS) +# include +#endif + static ErrorOr load_content_filters(); static ErrorOr load_autoplay_allowlist(); static ErrorOr initialize_lagom_networking(Vector const& certificates); @@ -78,6 +82,7 @@ ErrorOr serenity_main(Main::Arguments arguments) StringView command_line {}; StringView executable_path {}; + StringView mach_server_name {}; Vector certificates; int webcontent_fd_passing_socket { -1 }; bool is_layout_test_mode = false; @@ -94,6 +99,7 @@ ErrorOr serenity_main(Main::Arguments arguments) args_parser.add_option(use_lagom_networking, "Enable Lagom servers for networking", "use-lagom-networking", 0); args_parser.add_option(use_gpu_painting, "Enable GPU painting", "use-gpu-painting", 0); args_parser.add_option(wait_for_debugger, "Wait for debugger", "wait-for-debugger", 0); + args_parser.add_option(mach_server_name, "Mach server name", "mach-server-name", 0, "mach_server_name"); args_parser.parse(arguments); @@ -107,6 +113,12 @@ ErrorOr serenity_main(Main::Arguments arguments) WebContent::PageClient::set_use_gpu_painter(); } +#if defined(AK_OS_MACOS) + if (!mach_server_name.is_empty()) { + WebView::register_with_mach_server(mach_server_name); + } +#endif + #if defined(HAVE_QT) if (!use_lagom_networking) Web::ResourceLoader::initialize(Ladybird::RequestManagerQt::create()); diff --git a/Userland/Libraries/LibWebView/ProcessManager.cpp b/Userland/Libraries/LibWebView/ProcessManager.cpp index c93a1704451..acbe4759fb6 100644 --- a/Userland/Libraries/LibWebView/ProcessManager.cpp +++ b/Userland/Libraries/LibWebView/ProcessManager.cpp @@ -80,6 +80,12 @@ void ProcessManager::initialize() MUST(Core::System::sigaction(SIGCHLD, &action, nullptr)); the().add_process(WebView::ProcessType::Chrome, getpid()); +#ifdef AK_OS_MACH + auto self_send_port = mach_task_self(); + auto res = mach_port_mod_refs(mach_task_self(), self_send_port, MACH_PORT_RIGHT_SEND, +1); + VERIFY(res == KERN_SUCCESS); + the().add_process(getpid(), Core::MachPort::adopt_right(self_send_port, Core::MachPort::PortRight::Send)); +#endif } void ProcessManager::add_process(ProcessType type, pid_t pid)