ladybird/Libraries/LibWeb/Worker/WebWorkerClient.h
Timothy Flynn 0482b6bb57 LibWeb+LibWebView+WebContent: Implement versioning for document cookies
This patch introduces a cookie cache in the WebContent process to reduce
blocking IPC calls when JS accesses document.cookie. The UI process now
maintains a cookie version counter per-domain in shared memory. When JS
reads document.cookie, we check whether we have a valid cached cookie by
comparing the current shared version to the last used version. If they
match, the cached cookie is returned without IPC.

This optimization is based on Chromium's shared versioning, in which it
was observed that 87% of document.cookie accesses were redundant. See:
https://blog.chromium.org/2024/06/introducing-shared-memory-versioning-to.html

Note that this cache only supports document.cookie, not HTTP Cookie
headers. HTTP cookies are attached to requests with varying URLs and
paths. The cookies that match the document URL might not match the
request URL, which we wouldn't know from WebContent. So attaching the
cached document cookie would be incorrect.

On https://twinings.co.uk, we see approximately 600 document.cookie
requests while the page loads. This patch reduces the time spent in
the document.cookie getter from ~45ms to 2-3ms.
2026-02-05 07:28:07 -05:00

39 lines
1.2 KiB
C++

/*
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibIPC/ConnectionToServer.h>
#include <LibWeb/Cookie/Cookie.h>
#include <LibWeb/Export.h>
#include <LibWeb/Worker/WebWorkerClientEndpoint.h>
#include <LibWeb/Worker/WebWorkerServerEndpoint.h>
namespace Web::HTML {
class WEB_API WebWorkerClient final
: public IPC::ConnectionToServer<WebWorkerClientEndpoint, WebWorkerServerEndpoint>
, public WebWorkerClientEndpoint {
C_OBJECT_ABSTRACT(WebWorkerClient);
public:
explicit WebWorkerClient(NonnullOwnPtr<IPC::Transport>);
virtual void did_close_worker() override;
virtual Messages::WebWorkerClient::DidRequestCookieResponse did_request_cookie(URL::URL, Cookie::Source) override;
virtual Messages::WebWorkerClient::RequestWorkerAgentResponse request_worker_agent(Web::Bindings::AgentType worker_type) override;
Function<void()> on_worker_close;
Function<Cookie::VersionedCookie(URL::URL const&, Cookie::Source)> on_request_cookie;
Function<IPC::File(Web::Bindings::AgentType)> on_request_worker_agent;
IPC::File clone_transport();
private:
virtual void die() override;
};
}