LibWeb: Add initial CookieStore support

This commit is contained in:
Idan Horowitz 2025-08-04 22:17:20 +03:00 committed by Tim Flynn
commit d6c2893663
Notes: github-actions[bot] 2025-08-08 17:12:40 +00:00
19 changed files with 134 additions and 2 deletions

View file

@ -80,6 +80,7 @@ set(SOURCES
ContentSecurityPolicy/Violation.cpp
Cookie/Cookie.cpp
Cookie/ParsedCookie.cpp
CookieStore/CookieStore.cpp
CredentialManagement/Credential.cpp
CredentialManagement/CredentialsContainer.cpp
CredentialManagement/FederatedCredential.cpp

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2025, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/CookieStorePrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CookieStore/CookieStore.h>
namespace Web::CookieStore {
GC_DEFINE_ALLOCATOR(CookieStore);
CookieStore::CookieStore(JS::Realm& realm)
: DOM::EventTarget(realm)
{
}
void CookieStore::initialize(JS::Realm& realm)
{
WEB_SET_PROTOTYPE_FOR_INTERFACE(CookieStore);
Base::initialize(realm);
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2025, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/DOM/EventTarget.h>
namespace Web::CookieStore {
// https://cookiestore.spec.whatwg.org/#cookiestore
class CookieStore final : public DOM::EventTarget {
WEB_PLATFORM_OBJECT(CookieStore, DOM::EventTarget);
GC_DECLARE_ALLOCATOR(CookieStore);
private:
CookieStore(JS::Realm&);
virtual void initialize(JS::Realm&) override;
};
}

View file

@ -0,0 +1,21 @@
#import <DOM/EventTarget.idl>
#import <DOM/EventHandler.idl>
#import <HTML/Window.idl>
#import <ServiceWorker/ServiceWorkerGlobalScope.idl>
// https://cookiestore.spec.whatwg.org/#cookiestore
[Exposed=(ServiceWorker,Window), SecureContext]
interface CookieStore : EventTarget {
};
// https://cookiestore.spec.whatwg.org/#Window
[SecureContext]
partial interface Window {
[SameObject] readonly attribute CookieStore cookieStore;
};
// https://cookiestore.spec.whatwg.org/#ServiceWorkerGlobalScope
partial interface ServiceWorkerGlobalScope {
[SameObject] readonly attribute CookieStore cookieStore;
};

View file

@ -173,6 +173,12 @@ enum class Source;
}
namespace Web::CookieStore {
class CookieStore;
}
namespace Web::CredentialManagement {
class Credential;

View file

@ -24,6 +24,7 @@
#include <LibWeb/CSS/MediaQueryList.h>
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/CSS/Screen.h>
#include <LibWeb/CookieStore/CookieStore.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/Event.h>
@ -130,6 +131,7 @@ void Window::visit_edges(JS::Cell::Visitor& visitor)
visitor.visit(m_pdf_viewer_plugin_objects);
visitor.visit(m_pdf_viewer_mime_type_objects);
visitor.visit(m_close_watcher_manager);
visitor.visit(m_cookie_store);
visitor.visit(m_locationbar);
visitor.visit(m_menubar);
visitor.visit(m_personalbar);
@ -1121,6 +1123,17 @@ GC::Ref<CloseWatcherManager> Window::close_watcher_manager()
return GC::Ref { *m_close_watcher_manager };
}
// https://cookiestore.spec.whatwg.org/#Window
GC::Ref<CookieStore::CookieStore> Window::cookie_store()
{
auto& realm = this->realm();
// The cookieStore getter steps are to return thiss associated CookieStore.
if (!m_cookie_store)
m_cookie_store = realm.create<CookieStore::CookieStore>(realm);
return *m_cookie_store;
}
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-alert
void Window::alert(String const& message)
{

View file

@ -194,6 +194,7 @@ public:
[[nodiscard]] GC::Ref<Navigator> navigator();
[[nodiscard]] GC::Ref<CloseWatcherManager> close_watcher_manager();
[[nodiscard]] GC::Ref<CookieStore::CookieStore> cookie_store();
void alert(String const& message = {});
bool confirm(Optional<String> const& message);
@ -310,6 +311,7 @@ private:
GC::Ptr<Navigator> m_navigator;
GC::Ptr<Location> m_location;
GC::Ptr<CloseWatcherManager> m_close_watcher_manager;
GC::Ptr<CookieStore::CookieStore> m_cookie_store;
// https://html.spec.whatwg.org/multipage/nav-history-apis.html#window-navigation-api
GC::Ptr<Navigation> m_navigation;

View file

@ -1,3 +1,4 @@
#import <CookieStore/CookieStore.idl>
#import <CSS/MediaQueryList.idl>
#import <CSS/Screen.idl>
#import <CSS/VisualViewport.idl>

View file

@ -109,6 +109,8 @@ public:
protected:
explicit WorkerGlobalScope(JS::Realm&, GC::Ref<Web::Page>);
virtual void visit_edges(Cell::Visitor&) override;
virtual void initialize_web_interfaces_impl();
void close_a_worker();
@ -120,8 +122,6 @@ protected:
private:
virtual bool is_window_or_worker_global_scope_mixin() const final { return true; }
virtual void visit_edges(Cell::Visitor&) override;
GC::Ptr<WorkerLocation> m_location;
GC::Ptr<WorkerNavigator> m_navigator;

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CookieStore/CookieStore.h>
#include <LibWeb/ServiceWorker/EventNames.h>
#include <LibWeb/ServiceWorker/ServiceWorkerGlobalScope.h>
@ -18,6 +19,13 @@ ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(JS::Realm& realm, GC::Ref<Web
{
}
void ServiceWorkerGlobalScope::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_cookie_store);
}
// https://w3c.github.io/ServiceWorker/#dom-serviceworkerglobalscope-oninstall
void ServiceWorkerGlobalScope::set_oninstall(GC::Ptr<WebIDL::CallbackType> value)
{
@ -78,4 +86,15 @@ GC::Ptr<WebIDL::CallbackType> ServiceWorkerGlobalScope::onmessageerror()
return event_handler_attribute(EventNames::messageerror);
}
// https://cookiestore.spec.whatwg.org/#ServiceWorkerGlobalScope
GC::Ref<CookieStore::CookieStore> ServiceWorkerGlobalScope::cookie_store()
{
auto& realm = this->realm();
// The cookieStore getter steps are to return thiss associated CookieStore.
if (!m_cookie_store)
m_cookie_store = realm.create<CookieStore::CookieStore>(realm);
return *m_cookie_store;
}
}

View file

@ -33,8 +33,15 @@ public:
void set_onmessageerror(GC::Ptr<WebIDL::CallbackType>);
GC::Ptr<WebIDL::CallbackType> onmessageerror();
[[nodiscard]] GC::Ref<CookieStore::CookieStore> cookie_store();
protected:
explicit ServiceWorkerGlobalScope(JS::Realm&, GC::Ref<Web::Page>);
private:
virtual void visit_edges(Cell::Visitor&) override;
GC::Ptr<CookieStore::CookieStore> m_cookie_store;
};
}

View file

@ -1,3 +1,4 @@
#import <CookieStore/CookieStore.idl>
#import <DOM/EventHandler.idl>
// https://w3c.github.io/ServiceWorker/#serviceworkerglobalscope

View file

@ -13,6 +13,7 @@ libweb_js_bindings(Clipboard/ClipboardItem)
libweb_js_bindings(ContentSecurityPolicy/SecurityPolicyViolationEvent)
libweb_js_bindings(Compression/CompressionStream)
libweb_js_bindings(Compression/DecompressionStream)
libweb_js_bindings(CookieStore/CookieStore)
libweb_js_bindings(CredentialManagement/Credential)
libweb_js_bindings(CredentialManagement/CredentialsContainer)
libweb_js_bindings(CredentialManagement/FederatedCredential)

View file

@ -4816,6 +4816,7 @@ static void generate_using_namespace_definitions(SourceGenerator& generator)
using namespace Web::Animations;
using namespace Web::Clipboard;
using namespace Web::ContentSecurityPolicy;
using namespace Web::CookieStore;
using namespace Web::CredentialManagement;
using namespace Web::Crypto;
using namespace Web::CSS;

View file

@ -17,6 +17,7 @@ static constexpr Array libweb_interface_namespaces = {
"Clipboard"sv,
"Compression"sv,
"ContentSecurityPolicy"sv,
"CookieStore"sv,
"Crypto"sv,
"DOM"sv,
"DOMURL"sv,

View file

@ -333,6 +333,7 @@ shared_library("LibWeb") {
"CSS",
"Clipboard",
"Cookie",
"CookieStore",
"Crypto",
"DOM",
"DOMURL",

View file

@ -0,0 +1,5 @@
source_set("CookieStore") {
configs += [ "//Userland/Libraries/LibWeb:configs" ]
deps = [ "//Userland/Libraries/LibWeb:all_generated" ]
sources = [ "CookieStore.cpp" ]
}

View file

@ -30,6 +30,7 @@ standard_idl_files = [
"//Userland/Libraries/LibWeb/Animations/KeyframeEffect.idl",
"//Userland/Libraries/LibWeb/Clipboard/Clipboard.idl",
"//Userland/Libraries/LibWeb/Clipboard/ClipboardEvent.idl",
"//Userland/Libraries/LibWeb/CookieStore/CookieStore.idl",
"//Userland/Libraries/LibWeb/Crypto/Crypto.idl",
"//Userland/Libraries/LibWeb/Crypto/CryptoKey.idl",
"//Userland/Libraries/LibWeb/Crypto/SubtleCrypto.idl",

View file

@ -79,6 +79,7 @@ Comment
CompositionEvent
CompressionStream
ConstantSourceNode
CookieStore
CountQueuingStrategy
Credential
CredentialsContainer