From 6d3fd2b54374ed6cf94bd663524accc4f021a6bb Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Sun, 7 Sep 2025 15:32:43 +0100 Subject: [PATCH] LibWeb: Implement cookie fetching for Workers Allows formulas to update on Google Sheets, which uses a Worker to update them and makes cookie authenticated requests, which was failing before this commit. This has the limitation that it has to proxy through the WebContent process, but that's how the current infrastructure is, which is outside the scope of this commit. --- Libraries/LibWeb/HTML/WorkerAgentParent.cpp | 10 ++++++++++ Libraries/LibWeb/HTML/WorkerAgentParent.h | 2 ++ Libraries/LibWeb/Worker/WebWorkerClient.cpp | 7 +++++++ Libraries/LibWeb/Worker/WebWorkerClient.h | 3 +++ Libraries/LibWeb/Worker/WebWorkerClient.ipc | 4 ++++ Services/WebWorker/PageHost.cpp | 5 +++++ Services/WebWorker/PageHost.h | 1 + 7 files changed, 32 insertions(+) diff --git a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp index 60a2a88e7bf..446c3ad1370 100644 --- a/Libraries/LibWeb/HTML/WorkerAgentParent.cpp +++ b/Libraries/LibWeb/HTML/WorkerAgentParent.cpp @@ -49,10 +49,20 @@ void WorkerAgentParent::initialize(JS::Realm& realm) auto transport = make(move(worker_socket)); m_worker_ipc = make_ref_counted(move(transport)); + setup_worker_ipc_callbacks(realm); m_worker_ipc->async_start_worker(m_url, m_worker_options.type, m_worker_options.credentials, m_worker_options.name, move(data_holder), m_outside_settings->serialize(), m_agent_type); } +void WorkerAgentParent::setup_worker_ipc_callbacks(JS::Realm& realm) +{ + // NOTE: As long as WorkerAgentParent is alive, realm and m_worker_ipc will be alive. + m_worker_ipc->on_request_cookie = [realm = GC::RawRef { realm }](URL::URL const& url, Cookie::Source source) { + auto& client = Bindings::principal_host_defined_page(realm).client(); + return client.page_did_request_cookie(url, source); + }; +} + void WorkerAgentParent::visit_edges(Cell::Visitor& visitor) { Base::visit_edges(visitor); diff --git a/Libraries/LibWeb/HTML/WorkerAgentParent.h b/Libraries/LibWeb/HTML/WorkerAgentParent.h index 693bc7e12a4..987200f5288 100644 --- a/Libraries/LibWeb/HTML/WorkerAgentParent.h +++ b/Libraries/LibWeb/HTML/WorkerAgentParent.h @@ -31,6 +31,8 @@ protected: virtual void visit_edges(Cell::Visitor&) override; private: + void setup_worker_ipc_callbacks(JS::Realm&); + WorkerOptions m_worker_options; Bindings::AgentType m_agent_type { Bindings::AgentType::DedicatedWorker }; URL::URL m_url; diff --git a/Libraries/LibWeb/Worker/WebWorkerClient.cpp b/Libraries/LibWeb/Worker/WebWorkerClient.cpp index 1778595ff39..1ac491624a8 100644 --- a/Libraries/LibWeb/Worker/WebWorkerClient.cpp +++ b/Libraries/LibWeb/Worker/WebWorkerClient.cpp @@ -20,6 +20,13 @@ void WebWorkerClient::did_close_worker() on_worker_close(); } +Messages::WebWorkerClient::DidRequestCookieResponse WebWorkerClient::did_request_cookie(URL::URL url, Cookie::Source source) +{ + if (on_request_cookie) + return on_request_cookie(url, source); + return String {}; +} + WebWorkerClient::WebWorkerClient(NonnullOwnPtr transport) : IPC::ConnectionToServer(*this, move(transport)) { diff --git a/Libraries/LibWeb/Worker/WebWorkerClient.h b/Libraries/LibWeb/Worker/WebWorkerClient.h index ecaffd5d1f2..884c0499463 100644 --- a/Libraries/LibWeb/Worker/WebWorkerClient.h +++ b/Libraries/LibWeb/Worker/WebWorkerClient.h @@ -7,6 +7,7 @@ #pragma once #include +#include #include #include #include @@ -22,8 +23,10 @@ public: explicit WebWorkerClient(NonnullOwnPtr); virtual void did_close_worker() override; + virtual Messages::WebWorkerClient::DidRequestCookieResponse did_request_cookie(URL::URL, Cookie::Source) override; Function on_worker_close; + Function on_request_cookie; IPC::File clone_transport(); diff --git a/Libraries/LibWeb/Worker/WebWorkerClient.ipc b/Libraries/LibWeb/Worker/WebWorkerClient.ipc index 4a6d849a14a..23db83cdd82 100644 --- a/Libraries/LibWeb/Worker/WebWorkerClient.ipc +++ b/Libraries/LibWeb/Worker/WebWorkerClient.ipc @@ -1,3 +1,7 @@ +#include +#include + endpoint WebWorkerClient { did_close_worker() =| + did_request_cookie(URL::URL url, Web::Cookie::Source source) => (String cookie) } diff --git a/Services/WebWorker/PageHost.cpp b/Services/WebWorker/PageHost.cpp index 048ca39aee1..c321186219a 100644 --- a/Services/WebWorker/PageHost.cpp +++ b/Services/WebWorker/PageHost.cpp @@ -77,6 +77,11 @@ Web::CSS::PreferredMotion PageHost::preferred_motion() const return Web::CSS::PreferredMotion::Auto; } +String PageHost::page_did_request_cookie(URL::URL const& url, Web::Cookie::Source source) +{ + return m_client.did_request_cookie(url, source); +} + void PageHost::request_file(Web::FileRequest request) { m_client.request_file(move(request)); diff --git a/Services/WebWorker/PageHost.h b/Services/WebWorker/PageHost.h index d4834802f9d..813895f0c9c 100644 --- a/Services/WebWorker/PageHost.h +++ b/Services/WebWorker/PageHost.h @@ -32,6 +32,7 @@ public: virtual Web::CSS::PreferredColorScheme preferred_color_scheme() const override; virtual Web::CSS::PreferredContrast preferred_contrast() const override; virtual Web::CSS::PreferredMotion preferred_motion() const override; + virtual String page_did_request_cookie(URL::URL const&, Web::Cookie::Source) override; virtual void request_file(Web::FileRequest) override; virtual Web::DisplayListPlayerType display_list_player_type() const override { VERIFY_NOT_REACHED(); } virtual bool is_headless() const override { VERIFY_NOT_REACHED(); }