Files
ladybird/Libraries/LibWeb/Cookie/Cookie.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

81 lines
1.9 KiB
C++

/*
* Copyright (c) 2021-2026, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/String.h>
#include <AK/Time.h>
#include <LibCore/SharedVersion.h>
#include <LibIPC/Forward.h>
#include <LibURL/URL.h>
#include <LibWeb/Export.h>
namespace Web::Cookie {
enum class SameSite {
Default,
None,
Strict,
Lax
};
enum class Source {
NonHttp,
Http,
};
struct WEB_API Cookie {
String creation_time_to_string() const;
String last_access_time_to_string() const;
String expiry_time_to_string() const;
String name;
String value;
SameSite same_site { SameSite::Default };
UnixDateTime creation_time {};
UnixDateTime last_access_time {};
UnixDateTime expiry_time {};
String domain {};
String path {};
bool secure { false };
bool http_only { false };
bool host_only { false };
bool persistent { false };
};
struct VersionedCookie {
Optional<Core::SharedVersion> cookie_version;
String cookie;
};
WEB_API StringView same_site_to_string(SameSite same_site_mode);
WEB_API SameSite same_site_from_string(StringView same_site_mode);
WEB_API Optional<String> canonicalize_domain(URL::URL const& url);
WEB_API bool domain_matches(StringView string, StringView domain_string);
WEB_API bool path_matches(StringView request_path, StringView cookie_path);
WEB_API String default_path(URL::URL const&);
WEB_API bool cookie_matches_url(Cookie const&, URL::URL const&, String const& retrieval_host_canonical, Optional<Source> = {});
}
namespace IPC {
template<>
WEB_API ErrorOr<void> encode(Encoder&, Web::Cookie::Cookie const&);
template<>
WEB_API ErrorOr<Web::Cookie::Cookie> decode(Decoder&);
template<>
WEB_API ErrorOr<void> encode(Encoder&, Web::Cookie::VersionedCookie const&);
template<>
WEB_API ErrorOr<Web::Cookie::VersionedCookie> decode(Decoder&);
}