Pārlūkot izejas kodu

Ladybird+headless-browser: Make RequestServer single instance on Lagom

Co-Authored-By: Timothy Flynn <trflynn89@pm.me>
Andrew Kaster 1 gadu atpakaļ
vecāks
revīzija
c87e32154a

+ 15 - 0
Ladybird/AppKit/Application/Application.h

@@ -6,8 +6,23 @@
 
 #pragma once
 
+#include <AK/Error.h>
+#include <AK/Vector.h>
+#include <LibWebView/Forward.h>
+#include <LibWebView/SocketPair.h>
+
 #import <System/Cocoa.h>
 
+namespace Ladybird {
+class WebViewBridge;
+}
+
 @interface Application : NSApplication
 
+- (instancetype)init;
+
+- (ErrorOr<void>)launchRequestServer:(Vector<ByteString> const&)certificates;
+- (ErrorOr<NonnullRefPtr<WebView::WebContentClient>>)launchWebContent:(Ladybird::WebViewBridge&)web_view_bridge;
+- (ErrorOr<WebView::SocketPair>)launchWebWorker;
+
 @end

+ 35 - 0
Ladybird/AppKit/Application/Application.mm

@@ -4,7 +4,10 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
+#include <AK/ByteString.h>
+#include <Application/ApplicationBridge.h>
 #include <LibCore/EventLoop.h>
+#include <LibWebView/WebContentClient.h>
 
 #import <Application/Application.h>
 
@@ -13,10 +16,42 @@
 #endif
 
 @interface Application ()
+{
+    OwnPtr<Ladybird::ApplicationBridge> m_application_bridge;
+}
+
 @end
 
 @implementation Application
 
+- (instancetype)init
+{
+    if (self = [super init]) {
+        m_application_bridge = make<Ladybird::ApplicationBridge>();
+    }
+
+    return self;
+}
+
+#pragma mark - Public methods
+
+- (ErrorOr<void>)launchRequestServer:(Vector<ByteString> const&)certificates
+{
+    return m_application_bridge->launch_request_server(certificates);
+}
+
+- (ErrorOr<NonnullRefPtr<WebView::WebContentClient>>)launchWebContent:(Ladybird::WebViewBridge&)web_view_bridge
+{
+    return m_application_bridge->launch_web_content(web_view_bridge);
+}
+
+- (ErrorOr<WebView::SocketPair>)launchWebWorker
+{
+    return m_application_bridge->launch_web_worker();
+}
+
+#pragma mark - NSApplication
+
 - (void)terminate:(id)sender
 {
     Core::EventLoop::current().quit(0);

+ 59 - 0
Ladybird/AppKit/Application/ApplicationBridge.cpp

@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/ByteString.h>
+#include <Application/ApplicationBridge.h>
+#include <Ladybird/AppKit/UI/LadybirdWebViewBridge.h>
+#include <Ladybird/HelperProcess.h>
+#include <Ladybird/Utilities.h>
+#include <LibProtocol/RequestClient.h>
+#include <LibWebView/WebContentClient.h>
+
+namespace Ladybird {
+
+// Unfortunately, the Protocol namespace conflicts hard with a @Protocol interface defined by Objective-C. And the #define
+// trick we use for e.g. Duration does not work for Protocol. So here, we make sure that any use of the Protocol namespace
+// is limited to .cpp files (i.e. not .h files that an Objective-C file can include).
+struct ApplicationBridgeImpl {
+    RefPtr<Protocol::RequestClient> request_server_client;
+};
+
+ApplicationBridge::ApplicationBridge()
+    : m_impl(make<ApplicationBridgeImpl>())
+{
+}
+
+ApplicationBridge::~ApplicationBridge() = default;
+
+ErrorOr<void> ApplicationBridge::launch_request_server(Vector<ByteString> const& certificates)
+{
+    auto request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
+    auto protocol_client = TRY(launch_request_server_process(request_server_paths, s_serenity_resource_root, certificates));
+
+    m_impl->request_server_client = move(protocol_client);
+    return {};
+}
+
+ErrorOr<NonnullRefPtr<WebView::WebContentClient>> ApplicationBridge::launch_web_content(WebViewBridge& web_view_bridge)
+{
+    // FIXME: Fail to open the tab, rather than crashing the whole application if this fails
+    auto request_server_sockets = TRY(connect_new_request_server_client(*m_impl->request_server_client));
+
+    auto web_content_paths = TRY(get_paths_for_helper_process("WebContent"sv));
+    auto web_content = TRY(launch_web_content_process(web_view_bridge, web_content_paths, web_view_bridge.web_content_options(), move(request_server_sockets)));
+
+    return web_content;
+}
+
+ErrorOr<WebView::SocketPair> ApplicationBridge::launch_web_worker()
+{
+    auto web_worker_paths = TRY(get_paths_for_helper_process("WebWorker"sv));
+    auto worker_client = TRY(launch_web_worker_process(web_worker_paths, *m_impl->request_server_client));
+
+    return worker_client->dup_sockets();
+}
+
+}

+ 32 - 0
Ladybird/AppKit/Application/ApplicationBridge.h

@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/NonnullOwnPtr.h>
+#include <AK/Vector.h>
+#include <LibWebView/Forward.h>
+#include <LibWebView/SocketPair.h>
+
+namespace Ladybird {
+
+struct ApplicationBridgeImpl;
+class WebViewBridge;
+
+class ApplicationBridge {
+public:
+    ApplicationBridge();
+    ~ApplicationBridge();
+
+    ErrorOr<void> launch_request_server(Vector<ByteString> const& certificates);
+    ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content(WebViewBridge&);
+    ErrorOr<WebView::SocketPair> launch_web_worker();
+
+private:
+    NonnullOwnPtr<ApplicationBridgeImpl> m_impl;
+};
+
+}

+ 13 - 0
Ladybird/AppKit/UI/LadybirdWebView.mm

@@ -15,6 +15,7 @@
 #include <LibWebView/URL.h>
 #include <UI/LadybirdWebViewBridge.h>
 
+#import <Application/Application.h>
 #import <Application/ApplicationDelegate.h>
 #import <UI/Event.h>
 #import <UI/LadybirdWebView.h>
@@ -106,6 +107,8 @@ struct HideCursor {
         m_web_view_bridge = MUST(Ladybird::WebViewBridge::create(move(screen_rects), device_pixel_ratio, [delegate webContentOptions], [delegate webdriverContentIPCPath], [delegate preferredColorScheme]));
         [self setWebViewCallbacks];
 
+        m_web_view_bridge->initialize_client();
+
         auto* area = [[NSTrackingArea alloc] initWithRect:[self bounds]
                                                   options:NSTrackingActiveInKeyWindow | NSTrackingInVisibleRect | NSTrackingMouseMoved
                                                     owner:self
@@ -277,6 +280,16 @@ static void copy_data_to_clipboard(StringView data, NSPasteboardType pasteboard_
         return [self.observer onCreateNewTab:"about:blank"sv activateTab:activate_tab];
     };
 
+    m_web_view_bridge->on_request_web_content = [self]() {
+        Application* application = NSApp;
+        return [application launchWebContent:*m_web_view_bridge].release_value_but_fixme_should_propagate_errors();
+    };
+
+    m_web_view_bridge->on_request_worker_agent = []() {
+        Application* application = NSApp;
+        return [application launchWebWorker].release_value_but_fixme_should_propagate_errors();
+    };
+
     m_web_view_bridge->on_activate_tab = [self]() {
         [[self window] orderFront:nil];
     };

+ 3 - 11
Ladybird/AppKit/UI/LadybirdWebViewBridge.cpp

@@ -36,8 +36,6 @@ WebViewBridge::WebViewBridge(Vector<Web::DevicePixelRect> screen_rects, float de
 {
     m_device_pixel_ratio = device_pixel_ratio;
 
-    initialize_client(CreateNewClient::Yes);
-
     on_scroll_by_delta = [this](auto x_delta, auto y_delta) {
         auto position = m_viewport_rect.location();
         position.set_x(position.x() + x_delta);
@@ -51,11 +49,6 @@ WebViewBridge::WebViewBridge(Vector<Web::DevicePixelRect> screen_rects, float de
         if (on_scroll)
             on_scroll(to_widget_position(position));
     };
-
-    on_request_worker_agent = [this]() {
-        auto worker_client = MUST(launch_web_worker_process(MUST(get_paths_for_helper_process("WebWorker"sv)), m_web_content_options.certificates));
-        return worker_client->dup_sockets();
-    };
 }
 
 WebViewBridge::~WebViewBridge() = default;
@@ -150,14 +143,13 @@ Gfx::IntPoint WebViewBridge::to_widget_position(Gfx::IntPoint content_position)
 
 void WebViewBridge::initialize_client(CreateNewClient)
 {
+    VERIFY(on_request_web_content);
+
     // FIXME: Don't create a new process when CreateNewClient is false
     //        We should create a new tab/window in the UI instead, and re-use the existing WebContentClient object.
     m_client_state = {};
 
-    auto candidate_web_content_paths = MUST(get_paths_for_helper_process("WebContent"sv));
-    auto new_client = MUST(launch_web_content_process(*this, candidate_web_content_paths, m_web_content_options));
-
-    m_client_state.client = new_client;
+    m_client_state.client = on_request_web_content();
     m_client_state.client->on_web_content_process_crash = [this] {
         Core::deferred_invoke([this] {
             handle_web_content_process_crash();

+ 5 - 2
Ladybird/AppKit/UI/LadybirdWebViewBridge.h

@@ -23,6 +23,10 @@ public:
     static ErrorOr<NonnullOwnPtr<WebViewBridge>> create(Vector<Web::DevicePixelRect> screen_rects, float device_pixel_ratio, WebContentOptions const&, Optional<StringView> webdriver_content_ipc_path, Web::CSS::PreferredColorScheme);
     virtual ~WebViewBridge() override;
 
+    virtual void initialize_client(CreateNewClient = CreateNewClient::Yes) override;
+
+    WebContentOptions const& web_content_options() const { return m_web_content_options; }
+
     float device_pixel_ratio() const { return m_device_pixel_ratio; }
     void set_device_pixel_ratio(float device_pixel_ratio);
     float inverse_device_pixel_ratio() const { return 1.0f / m_device_pixel_ratio; }
@@ -47,6 +51,7 @@ public:
     };
     Optional<Paintable> paintable();
 
+    Function<NonnullRefPtr<WebView::WebContentClient>()> on_request_web_content;
     Function<void()> on_zoom_level_changed;
     Function<void(Gfx::IntPoint)> on_scroll;
 
@@ -58,8 +63,6 @@ private:
     virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const override;
     virtual Gfx::IntPoint to_widget_position(Gfx::IntPoint content_position) const override;
 
-    virtual void initialize_client(CreateNewClient) override;
-
     Vector<Web::DevicePixelRect> m_screen_rects;
     Gfx::IntRect m_viewport_rect;
 

+ 4 - 2
Ladybird/AppKit/main.mm

@@ -31,7 +31,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
 {
     AK::set_rich_debug_enabled(true);
 
-    [Application sharedApplication];
+    Application* application = [Application sharedApplication];
 
     Core::EventLoopManager::install(*new Ladybird::CFEventLoopManager);
     Core::EventLoop event_loop;
@@ -71,6 +71,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
     auto database = TRY(WebView::Database::create(move(sql_server_paths)));
     auto cookie_jar = TRY(WebView::CookieJar::create(*database));
 
+    // 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<URL::URL> initial_urls;
 
@@ -87,7 +90,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
     Ladybird::WebContentOptions web_content_options {
         .command_line = MUST(command_line_builder.to_string()),
         .executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))),
-        .certificates = move(certificates),
         .enable_gpu_painting = use_gpu_painting ? Ladybird::EnableGPUPainting::Yes : Ladybird::EnableGPUPainting::No,
         .use_lagom_networking = Ladybird::UseLagomNetworking::Yes,
         .wait_for_debugger = debug_web_content ? Ladybird::WaitForDebugger::Yes : Ladybird::WaitForDebugger::No,

+ 1 - 0
Ladybird/CMakeLists.txt

@@ -139,6 +139,7 @@ elseif (APPLE)
         ${SOURCES}
         AppKit/main.mm
         AppKit/Application/Application.mm
+        AppKit/Application/ApplicationBridge.cpp
         AppKit/Application/ApplicationDelegate.mm
         AppKit/Application/EventLoopImplementation.mm
         AppKit/UI/Event.mm

+ 63 - 20
Ladybird/HelperProcess.cpp

@@ -12,7 +12,8 @@
 ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
     WebView::ViewImplementation& view,
     ReadonlySpan<ByteString> candidate_web_content_paths,
-    Ladybird::WebContentOptions const& web_content_options)
+    Ladybird::WebContentOptions const& web_content_options,
+    Optional<WebView::SocketPair> request_server_sockets)
 {
     int socket_fds[2] {};
     TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
@@ -73,10 +74,14 @@ ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
                 arguments.append("--mach-server-name"sv);
                 arguments.append(server.value());
             }
-            Vector<ByteString> certificate_args;
-            for (auto const& certificate : web_content_options.certificates) {
-                certificate_args.append(ByteString::formatted("--certificate={}", certificate));
-                arguments.append(certificate_args.last().view());
+            Vector<String> fd_strings;
+            if (request_server_sockets.has_value()) {
+                arguments.append("--request-server-socket"sv);
+                fd_strings.append(MUST(String::number(request_server_sockets->socket.fd())));
+                arguments.append(fd_strings.last());
+                arguments.append("--request-server-fd-passing-socket"sv);
+                fd_strings.append(MUST(String::number(request_server_sockets->fd_passing_socket.fd())));
+                arguments.append(fd_strings.last());
             }
 
             result = Core::System::exec(arguments[0], arguments.span(), Core::System::SearchInPath::Yes);
@@ -109,7 +114,7 @@ ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
 }
 
 template<typename Client>
-ErrorOr<NonnullRefPtr<Client>> launch_generic_server_process(ReadonlySpan<ByteString> candidate_server_paths, StringView serenity_resource_root, Vector<ByteString> const& certificates, StringView server_name)
+ErrorOr<NonnullRefPtr<Client>> launch_generic_server_process(ReadonlySpan<ByteString> candidate_server_paths, StringView server_name, Vector<StringView> extra_arguments = {})
 {
     int socket_fds[2] {};
     TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
@@ -139,20 +144,14 @@ ErrorOr<NonnullRefPtr<Client>> launch_generic_server_process(ReadonlySpan<ByteSt
             if (Core::System::access(path, X_OK).is_error())
                 continue;
 
-            auto arguments = Vector<StringView, 5> {
+            auto arguments = Vector<StringView> {
                 path.view(),
                 "--fd-passing-socket"sv,
                 fd_passing_socket_string,
             };
-            if (!serenity_resource_root.is_empty()) {
-                arguments.append("--serenity-resource-root"sv);
-                arguments.append(serenity_resource_root);
-            }
-            Vector<ByteString> certificate_args;
-            for (auto const& certificate : certificates) {
-                certificate_args.append(ByteString::formatted("--certificate={}", certificate));
-                arguments.append(certificate_args.last().view());
-            }
+
+            if (!extra_arguments.is_empty())
+                arguments.extend(extra_arguments);
 
             result = Core::System::exec(arguments[0], arguments.span(), Core::System::SearchInPath::Yes);
             if (!result.is_error())
@@ -180,15 +179,59 @@ ErrorOr<NonnullRefPtr<Client>> launch_generic_server_process(ReadonlySpan<ByteSt
 
 ErrorOr<NonnullRefPtr<ImageDecoderClient::Client>> launch_image_decoder_process(ReadonlySpan<ByteString> candidate_image_decoder_paths)
 {
-    return launch_generic_server_process<ImageDecoderClient::Client>(candidate_image_decoder_paths, ""sv, {}, "ImageDecoder"sv);
+    return launch_generic_server_process<ImageDecoderClient::Client>(candidate_image_decoder_paths, "ImageDecoder"sv);
 }
 
-ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, Vector<ByteString> const& certificates)
+ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, NonnullRefPtr<Protocol::RequestClient> request_client)
 {
-    return launch_generic_server_process<Web::HTML::WebWorkerClient>(candidate_web_worker_paths, ""sv, certificates, "WebWorker"sv);
+    auto request_server_sockets = TRY(connect_new_request_server_client(move(request_client)));
+
+    Vector<StringView> arguments;
+    Vector<String> fd_strings;
+
+    arguments.append("--request-server-socket"sv);
+    fd_strings.append(MUST(String::number(request_server_sockets.socket.fd())));
+    arguments.append(fd_strings.last());
+    arguments.append("--request-server-fd-passing-socket"sv);
+    fd_strings.append(MUST(String::number(request_server_sockets.fd_passing_socket.fd())));
+    arguments.append(fd_strings.last());
+
+    return launch_generic_server_process<Web::HTML::WebWorkerClient>(candidate_web_worker_paths, "WebWorker"sv, move(arguments));
 }
 
 ErrorOr<NonnullRefPtr<Protocol::RequestClient>> launch_request_server_process(ReadonlySpan<ByteString> candidate_request_server_paths, StringView serenity_resource_root, Vector<ByteString> const& certificates)
 {
-    return launch_generic_server_process<Protocol::RequestClient>(candidate_request_server_paths, serenity_resource_root, certificates, "RequestServer"sv);
+    Vector<StringView> arguments;
+    if (!serenity_resource_root.is_empty()) {
+        arguments.append("--serenity-resource-root"sv);
+        arguments.append(serenity_resource_root);
+    }
+    Vector<ByteString> certificate_args;
+    for (auto const& certificate : certificates) {
+        certificate_args.append(ByteString::formatted("--certificate={}", certificate));
+        arguments.append(certificate_args.last().view());
+    }
+
+    return launch_generic_server_process<Protocol::RequestClient>(candidate_request_server_paths, "RequestServer"sv, move(arguments));
+}
+
+ErrorOr<WebView::SocketPair> connect_new_request_server_client(Protocol::RequestClient& client)
+{
+    auto new_sockets = client.send_sync_but_allow_failure<Messages::RequestServer::ConnectNewClient>();
+    if (!new_sockets)
+        return Error::from_string_literal("Failed to connect to RequestServer");
+
+    auto socket = new_sockets->take_client_socket();
+    auto fd_passing_socket = new_sockets->take_client_fd_passing_socket();
+
+    // FIXME: IPC::Files transferred over the wire are always set O_CLOEXEC during decoding.
+    //        Perhaps we should add an option to IPC::File to allow the receiver to decide whether to
+    //        make it O_CLOEXEC or not. Or an attribute in the .ipc file?
+    for (auto fd : { socket.fd(), fd_passing_socket.fd() }) {
+        auto fd_flags = MUST(Core::System::fcntl(fd, F_GETFD));
+        fd_flags &= ~FD_CLOEXEC;
+        MUST(Core::System::fcntl(fd, F_SETFD, fd_flags));
+    }
+
+    return WebView::SocketPair { move(socket), move(fd_passing_socket) };
 }

+ 7 - 2
Ladybird/HelperProcess.h

@@ -8,19 +8,24 @@
 
 #include "Types.h"
 #include <AK/Error.h>
+#include <AK/Optional.h>
 #include <AK/Span.h>
 #include <AK/StringView.h>
 #include <LibImageDecoderClient/Client.h>
 #include <LibProtocol/RequestClient.h>
 #include <LibWeb/Worker/WebWorkerClient.h>
+#include <LibWebView/SocketPair.h>
 #include <LibWebView/ViewImplementation.h>
 #include <LibWebView/WebContentClient.h>
 
 ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(
     WebView::ViewImplementation& view,
     ReadonlySpan<ByteString> candidate_web_content_paths,
-    Ladybird::WebContentOptions const&);
+    Ladybird::WebContentOptions const&,
+    Optional<WebView::SocketPair> request_server_sockets = {});
 
 ErrorOr<NonnullRefPtr<ImageDecoderClient::Client>> launch_image_decoder_process(ReadonlySpan<ByteString> candidate_image_decoder_paths);
-ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, Vector<ByteString> const& certificates);
+ErrorOr<NonnullRefPtr<Web::HTML::WebWorkerClient>> launch_web_worker_process(ReadonlySpan<ByteString> candidate_web_worker_paths, NonnullRefPtr<Protocol::RequestClient>);
 ErrorOr<NonnullRefPtr<Protocol::RequestClient>> launch_request_server_process(ReadonlySpan<ByteString> candidate_request_server_paths, StringView serenity_resource_root, Vector<ByteString> const& certificates);
+
+ErrorOr<WebView::SocketPair> connect_new_request_server_client(Protocol::RequestClient&);

+ 1 - 1
Ladybird/Qt/Application.cpp

@@ -28,8 +28,8 @@ bool Application::event(QEvent* event)
 
         if (auto file_url = WebView::sanitize_url(file); file_url.has_value())
             on_open_file(file_url.release_value());
+        break;
     }
-
     default:
         break;
     }

+ 2 - 0
Ladybird/Qt/Application.h

@@ -7,6 +7,7 @@
 #pragma once
 
 #include <AK/Function.h>
+#include <LibProtocol/RequestClient.h>
 #include <LibURL/URL.h>
 #include <QApplication>
 
@@ -21,6 +22,7 @@ public:
     virtual bool event(QEvent* event) override;
 
     Function<void(URL::URL)> on_open_file;
+    RefPtr<Protocol::RequestClient> request_server_client;
 };
 
 }

+ 15 - 9
Ladybird/Qt/WebContentView.cpp

@@ -6,22 +6,19 @@
  */
 
 #include "WebContentView.h"
+#include "Application.h"
 #include "StringUtils.h"
 #include <AK/Assertions.h>
 #include <AK/ByteBuffer.h>
 #include <AK/Format.h>
-#include <AK/HashTable.h>
 #include <AK/LexicalPath.h>
 #include <AK/NonnullOwnPtr.h>
-#include <AK/StringBuilder.h>
 #include <AK/Types.h>
 #include <Kernel/API/KeyCode.h>
 #include <Ladybird/HelperProcess.h>
 #include <Ladybird/Utilities.h>
-#include <LibCore/ArgsParser.h>
 #include <LibCore/EventLoop.h>
 #include <LibCore/Resource.h>
-#include <LibCore/System.h>
 #include <LibCore/Timer.h>
 #include <LibGfx/Bitmap.h>
 #include <LibGfx/Font/FontDatabase.h>
@@ -30,10 +27,9 @@
 #include <LibGfx/Palette.h>
 #include <LibGfx/Rect.h>
 #include <LibGfx/SystemTheme.h>
-#include <LibMain/Main.h>
 #include <LibWeb/Crypto/Crypto.h>
-#include <LibWeb/Loader/ContentFilter.h>
 #include <LibWeb/Worker/WebWorkerClient.h>
+#include <LibWebView/SocketPair.h>
 #include <LibWebView/WebContentClient.h>
 #include <QApplication>
 #include <QCursor>
@@ -126,8 +122,9 @@ WebContentView::WebContentView(QWidget* window, WebContentOptions const& web_con
         finish_handling_key_event(event);
     };
 
-    on_request_worker_agent = [this]() {
-        auto worker_client = MUST(launch_web_worker_process(MUST(get_paths_for_helper_process("WebWorker"sv)), m_web_content_options.certificates));
+    on_request_worker_agent = []() {
+        auto& request_server_client = static_cast<Ladybird::Application*>(QApplication::instance())->request_server_client;
+        auto worker_client = MUST(launch_web_worker_process(MUST(get_paths_for_helper_process("WebWorker"sv)), *request_server_client));
         return worker_client->dup_sockets();
     };
 }
@@ -537,8 +534,17 @@ void WebContentView::initialize_client(WebView::ViewImplementation::CreateNewCli
     if (create_new_client == CreateNewClient::Yes) {
         m_client_state = {};
 
+        Optional<WebView::SocketPair> request_server_sockets;
+        if (m_web_content_options.use_lagom_networking == UseLagomNetworking::Yes) {
+            auto& protocol = static_cast<Ladybird::Application*>(QApplication::instance())->request_server_client;
+
+            // FIXME: Fail to open the tab, rather than crashing the whole application if this fails
+            auto sockets = connect_new_request_server_client(*protocol).release_value_but_fixme_should_propagate_errors();
+            request_server_sockets = AK::move(sockets);
+        }
+
         auto candidate_web_content_paths = get_paths_for_helper_process("WebContent"sv).release_value_but_fixme_should_propagate_errors();
-        auto new_client = launch_web_content_process(*this, candidate_web_content_paths, m_web_content_options).release_value_but_fixme_should_propagate_errors();
+        auto new_client = launch_web_content_process(*this, candidate_web_content_paths, m_web_content_options, AK::move(request_server_sockets)).release_value_but_fixme_should_propagate_errors();
 
         m_client_state.client = new_client;
     } else {

+ 6 - 1
Ladybird/Qt/main.cpp

@@ -132,12 +132,17 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
         initial_urls.append(ak_string_from_qstring(new_tab_page));
     }
 
+    // NOTE: WebWorker *always* needs a request server connection, even if WebContent uses Qt Networking
+    // FIXME: Create an abstraction to re-spawn the RequestServer and re-hook up its client hooks to each tab on crash
+    auto request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
+    auto protocol_client = TRY(launch_request_server_process(request_server_paths, s_serenity_resource_root, certificates));
+    app.request_server_client = protocol_client;
+
     StringBuilder command_line_builder;
     command_line_builder.join(' ', arguments.strings);
     Ladybird::WebContentOptions web_content_options {
         .command_line = MUST(command_line_builder.to_string()),
         .executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))),
-        .certificates = move(certificates),
         .enable_callgrind_profiling = enable_callgrind_profiling ? Ladybird::EnableCallgrindProfiling::Yes : Ladybird::EnableCallgrindProfiling::No,
         .enable_gpu_painting = use_gpu_painting ? Ladybird::EnableGPUPainting::Yes : Ladybird::EnableGPUPainting::No,
         .use_lagom_networking = enable_qt_networking ? Ladybird::UseLagomNetworking::No : Ladybird::UseLagomNetworking::Yes,

+ 0 - 1
Ladybird/Types.h

@@ -48,7 +48,6 @@ enum class EnableIDLTracing {
 struct WebContentOptions {
     String command_line;
     String executable_path;
-    Vector<ByteString> certificates;
     EnableCallgrindProfiling enable_callgrind_profiling { EnableCallgrindProfiling::No };
     EnableGPUPainting enable_gpu_painting { EnableGPUPainting::No };
     IsLayoutTestMode is_layout_test_mode { IsLayoutTestMode::No };

+ 14 - 8
Ladybird/WebContent/main.cpp

@@ -6,7 +6,6 @@
 
 #include <AK/LexicalPath.h>
 #include <Ladybird/FontPlugin.h>
-#include <Ladybird/HelperProcess.h>
 #include <Ladybird/ImageCodecPlugin.h>
 #include <Ladybird/Utilities.h>
 #include <LibAudio/Loader.h>
@@ -20,6 +19,7 @@
 #include <LibIPC/ConnectionFromClient.h>
 #include <LibJS/Bytecode/Interpreter.h>
 #include <LibMain/Main.h>
+#include <LibProtocol/RequestClient.h>
 #include <LibWeb/Bindings/MainThreadVM.h>
 #include <LibWeb/HTML/Window.h>
 #include <LibWeb/Loader/ContentFilter.h>
@@ -51,7 +51,7 @@
 
 static ErrorOr<void> load_content_filters();
 static ErrorOr<void> load_autoplay_allowlist();
-static ErrorOr<void> initialize_lagom_networking(Vector<ByteString> const& certificates);
+static ErrorOr<void> initialize_lagom_networking(int request_server_socket, int request_server_fd_passing_socket);
 
 namespace JS {
 extern bool g_log_all_js_exceptions;
@@ -93,6 +93,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
     StringView mach_server_name {};
     Vector<ByteString> certificates;
     int webcontent_fd_passing_socket { -1 };
+    int request_server_socket { -1 };
+    int request_server_fd_passing_socket { -1 };
     bool is_layout_test_mode = false;
     bool use_lagom_networking = false;
     bool use_gpu_painting = false;
@@ -103,8 +105,9 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
     Core::ArgsParser args_parser;
     args_parser.add_option(command_line, "Chrome process command line", "command-line", 0, "command_line");
     args_parser.add_option(executable_path, "Chrome process executable path", "executable-path", 0, "executable_path");
-    args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
     args_parser.add_option(webcontent_fd_passing_socket, "File descriptor of the passing socket for the WebContent connection", "webcontent-fd-passing-socket", 'c', "webcontent_fd_passing_socket");
+    args_parser.add_option(request_server_socket, "File descriptor of the socket for the RequestServer connection", "request-server-socket", 'r', "request_server_socket");
+    args_parser.add_option(request_server_fd_passing_socket, "File descriptor of the fd passing socket for the RequestServer connection", "request-server-fd-passing-socket", 'f', "request_server_fd_passing_socket");
     args_parser.add_option(is_layout_test_mode, "Is layout test mode", "layout-test-mode", 0);
     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);
@@ -136,7 +139,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
         Web::ResourceLoader::initialize(Ladybird::RequestManagerQt::create());
     else
 #endif
-        TRY(initialize_lagom_networking(certificates));
+        TRY(initialize_lagom_networking(request_server_socket, request_server_fd_passing_socket));
 
     Web::HTML::Window::set_internals_object_exposed(is_layout_test_mode);
 
@@ -217,11 +220,14 @@ static ErrorOr<void> load_autoplay_allowlist()
     return {};
 }
 
-static ErrorOr<void> initialize_lagom_networking(Vector<ByteString> const& certificates)
+ErrorOr<void> initialize_lagom_networking(int request_server_socket, int request_server_fd_passing_socket)
 {
-    auto candidate_request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
-    auto request_server_client = TRY(launch_request_server_process(candidate_request_server_paths, s_serenity_resource_root, certificates));
-    Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create(move(request_server_client))));
+    auto socket = TRY(Core::LocalSocket::adopt_fd(request_server_socket));
+    TRY(socket->set_blocking(true));
 
+    auto new_client = TRY(try_make_ref_counted<Protocol::RequestClient>(move(socket)));
+    new_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(request_server_fd_passing_socket)));
+
+    Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create(move(new_client))));
     return {};
 }

+ 14 - 8
Ladybird/WebWorker/main.cpp

@@ -26,20 +26,22 @@
 #include <LibWebView/WebSocketClientAdapter.h>
 #include <WebWorker/ConnectionFromClient.h>
 
-static ErrorOr<void> initialize_lagom_networking(Vector<ByteString> const& certificates);
+static ErrorOr<void> initialize_lagom_networking(int request_server_socket, int request_server_fd_passing_socket);
 
 ErrorOr<int> serenity_main(Main::Arguments arguments)
 {
     AK::set_rich_debug_enabled(true);
 
     int fd_passing_socket { -1 };
+    int request_server_socket { -1 };
+    int request_server_fd_passing_socket { -1 };
     StringView serenity_resource_root;
-    Vector<ByteString> certificates;
 
     Core::ArgsParser args_parser;
     args_parser.add_option(fd_passing_socket, "File descriptor of the fd passing socket", "fd-passing-socket", 'c', "fd-passing-socket");
+    args_parser.add_option(request_server_socket, "File descriptor of the request server socket", "request-server-socket", 's', "request-server-socket");
+    args_parser.add_option(request_server_fd_passing_socket, "File descriptor of the request server fd passing socket", "request-server-fd-passing-socket", 'f', "request-server-fd-passing-socket");
     args_parser.add_option(serenity_resource_root, "Absolute path to directory for serenity resources", "serenity-resource-root", 'r', "serenity-resource-root");
-    args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
     args_parser.parse(arguments);
 
     platform_init();
@@ -49,7 +51,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
 
     Web::Platform::FontPlugin::install(*new Web::Platform::FontPluginSerenity);
 
-    TRY(initialize_lagom_networking(certificates));
+    TRY(initialize_lagom_networking(request_server_socket, request_server_fd_passing_socket));
 
     VERIFY(fd_passing_socket >= 0);
 
@@ -61,11 +63,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
     return event_loop.exec();
 }
 
-static ErrorOr<void> initialize_lagom_networking(Vector<ByteString> const& certificates)
+static ErrorOr<void> initialize_lagom_networking(int request_server_socket, int request_server_fd_passing_socket)
 {
-    auto candidate_request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
-    auto request_server_client = TRY(launch_request_server_process(candidate_request_server_paths, s_serenity_resource_root, certificates));
-    Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create(move(request_server_client))));
+    auto socket = TRY(Core::LocalSocket::adopt_fd(request_server_socket));
+    TRY(socket->set_blocking(true));
+
+    auto new_client = TRY(try_make_ref_counted<Protocol::RequestClient>(move(socket)));
+    new_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(request_server_fd_passing_socket)));
+
+    Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create(move(new_client))));
 
     return {};
 }

+ 1 - 0
Meta/gn/secondary/Ladybird/BUILD.gn

@@ -115,6 +115,7 @@ executable("ladybird_executable") {
   } else {
     sources += [
       "AppKit/Application/Application.mm",
+      "AppKit/Application/ApplicationBridge.cpp",
       "AppKit/Application/ApplicationDelegate.mm",
       "AppKit/Application/EventLoopImplementation.mm",
       "AppKit/UI/Event.mm",

+ 18 - 10
Userland/Utilities/headless-browser.cpp

@@ -41,6 +41,7 @@
 #include <LibGfx/StandardCursor.h>
 #include <LibGfx/SystemTheme.h>
 #include <LibIPC/File.h>
+#include <LibProtocol/RequestClient.h>
 #include <LibURL/URL.h>
 #include <LibWeb/Cookie/Cookie.h>
 #include <LibWeb/Cookie/ParsedCookie.h>
@@ -64,34 +65,41 @@ static StringView s_current_test_path;
 
 class HeadlessWebContentView final : public WebView::ViewImplementation {
 public:
-    static ErrorOr<NonnullOwnPtr<HeadlessWebContentView>> create(Core::AnonymousBuffer theme, Gfx::IntSize const& window_size, String const& command_line, StringView web_driver_ipc_path, Ladybird::IsLayoutTestMode is_layout_test_mode = Ladybird::IsLayoutTestMode::No, Vector<ByteString> const& certificates = {})
+    static ErrorOr<NonnullOwnPtr<HeadlessWebContentView>> create(Core::AnonymousBuffer theme, Gfx::IntSize const& window_size, String const& command_line, StringView web_driver_ipc_path, Ladybird::IsLayoutTestMode is_layout_test_mode = Ladybird::IsLayoutTestMode::No, Vector<ByteString> const& certificates = {}, StringView resources_folder = {})
     {
+        RefPtr<Protocol::RequestClient> request_client;
+
 #if defined(AK_OS_SERENITY)
         auto database = TRY(WebView::Database::create());
+        (void)resources_folder;
+        (void)certificates;
 #else
         auto sql_server_paths = TRY(get_paths_for_helper_process("SQLServer"sv));
         auto database = TRY(WebView::Database::create(move(sql_server_paths)));
+
+        auto request_server_paths = TRY(get_paths_for_helper_process("RequestServer"sv));
+        request_client = TRY(launch_request_server_process(request_server_paths, resources_folder, certificates));
 #endif
 
         auto cookie_jar = TRY(WebView::CookieJar::create(*database));
 
-        auto view = TRY(adopt_nonnull_own_or_enomem(new (nothrow) HeadlessWebContentView(move(database), move(cookie_jar), certificates)));
+        auto view = TRY(adopt_nonnull_own_or_enomem(new (nothrow) HeadlessWebContentView(move(database), move(cookie_jar), request_client)));
 
 #if defined(AK_OS_SERENITY)
         view->m_client_state.client = TRY(WebView::WebContentClient::try_create(*view));
         (void)command_line;
-        (void)certificates;
         (void)is_layout_test_mode;
 #else
         Ladybird::WebContentOptions web_content_options {
             .command_line = command_line,
             .executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))),
-            .certificates = certificates,
             .is_layout_test_mode = is_layout_test_mode,
         };
 
+        auto request_server_sockets = TRY(connect_new_request_server_client(*request_client));
+
         auto candidate_web_content_paths = TRY(get_paths_for_helper_process("WebContent"sv));
-        view->m_client_state.client = TRY(launch_web_content_process(*view, candidate_web_content_paths, web_content_options));
+        view->m_client_state.client = TRY(launch_web_content_process(*view, candidate_web_content_paths, web_content_options, move(request_server_sockets)));
 #endif
 
         view->client().async_update_system_theme(0, move(theme));
@@ -155,10 +163,10 @@ public:
     }
 
 private:
-    HeadlessWebContentView(NonnullRefPtr<WebView::Database> database, WebView::CookieJar cookie_jar, Vector<ByteString> certificates)
+    HeadlessWebContentView(NonnullRefPtr<WebView::Database> database, WebView::CookieJar cookie_jar, RefPtr<Protocol::RequestClient> request_client = nullptr)
         : m_database(move(database))
         , m_cookie_jar(move(cookie_jar))
-        , m_certificates(move(certificates))
+        , m_request_client(move(request_client))
     {
         on_scroll_to_point = [this](auto position) {
             m_viewport_rect.set_location(position);
@@ -186,7 +194,7 @@ private:
             auto worker_client = MUST(Web::HTML::WebWorkerClient::try_create());
             (void)this;
 #else
-            auto worker_client = MUST(launch_web_worker_process(MUST(get_paths_for_helper_process("WebWorker"sv)), m_certificates));
+            auto worker_client = MUST(launch_web_worker_process(MUST(get_paths_for_helper_process("WebWorker"sv)), *m_request_client));
 #endif
             return worker_client->dup_sockets();
         };
@@ -205,7 +213,7 @@ private:
 
     NonnullRefPtr<WebView::Database> m_database;
     WebView::CookieJar m_cookie_jar;
-    Vector<ByteString> m_certificates;
+    RefPtr<Protocol::RequestClient> m_request_client;
 };
 
 static ErrorOr<NonnullRefPtr<Core::Timer>> load_page_for_screenshot_and_exit(Core::EventLoop& event_loop, HeadlessWebContentView& view, URL::URL url, int screenshot_timeout)
@@ -690,7 +698,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
 
     StringBuilder command_line_builder;
     command_line_builder.join(' ', arguments.strings);
-    auto view = TRY(HeadlessWebContentView::create(move(theme), window_size, MUST(command_line_builder.to_string()), web_driver_ipc_path, is_layout_test_mode ? Ladybird::IsLayoutTestMode::Yes : Ladybird::IsLayoutTestMode::No, certificates));
+    auto view = TRY(HeadlessWebContentView::create(move(theme), window_size, MUST(command_line_builder.to_string()), web_driver_ipc_path, is_layout_test_mode ? Ladybird::IsLayoutTestMode::Yes : Ladybird::IsLayoutTestMode::No, certificates, resources_folder));
 
     if (!test_root_path.is_empty()) {
         test_glob = ByteString::formatted("*{}*", test_glob);