From c6b5fb82a26faede753a9494b4dbcf24eaafb660 Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Thu, 12 Sep 2024 16:48:18 +0000 Subject: [PATCH] Bug 1475599 - part 1 - CookieStore API - WebIDL, r=smaug,edgul Differential Revision: https://phabricator.services.mozilla.com/D215144 --- dom/base/nsGlobalWindowInner.cpp | 12 +++ dom/base/nsGlobalWindowInner.h | 4 + dom/cookiestore/CookieChangeEvent.cpp | 53 ++++++++++ dom/cookiestore/CookieChangeEvent.h | 41 +++++++ dom/cookiestore/CookieStore.cpp | 94 ++++++++++++++++ dom/cookiestore/CookieStore.h | 61 +++++++++++ dom/cookiestore/moz.build | 25 +++++ dom/moz.build | 1 + dom/webidl/CookieChangeEvent.webidl | 27 +++++ dom/webidl/CookieStore.webidl | 100 ++++++++++++++++++ dom/webidl/ServiceWorkerGlobalScope.webidl | 11 ++ dom/webidl/ServiceWorkerRegistration.webidl | 9 ++ dom/webidl/Window.webidl | 7 ++ dom/webidl/moz.build | 2 + dom/workers/WorkerScope.cpp | 12 ++- dom/workers/WorkerScope.h | 6 ++ modules/libpref/init/StaticPrefList.yaml | 6 ++ .../meta/cookie-store/__dir__.ini | 1 + xpcom/ds/StaticAtoms.py | 1 + 19 files changed, 472 insertions(+), 1 deletion(-) create mode 100644 dom/cookiestore/CookieChangeEvent.cpp create mode 100644 dom/cookiestore/CookieChangeEvent.h create mode 100644 dom/cookiestore/CookieStore.cpp create mode 100644 dom/cookiestore/CookieStore.h create mode 100644 dom/cookiestore/moz.build create mode 100644 dom/webidl/CookieChangeEvent.webidl create mode 100644 dom/webidl/CookieStore.webidl diff --git a/dom/base/nsGlobalWindowInner.cpp b/dom/base/nsGlobalWindowInner.cpp index 9448bdd07e31..25f53b64e8b1 100644 --- a/dom/base/nsGlobalWindowInner.cpp +++ b/dom/base/nsGlobalWindowInner.cpp @@ -108,6 +108,7 @@ #include "mozilla/dom/ClientState.h" #include "mozilla/dom/ClientsBinding.h" #include "mozilla/dom/Console.h" +#include "mozilla/dom/CookieStore.h" #include "mozilla/dom/ContentChild.h" #include "mozilla/dom/ContentFrameMessageManager.h" #include "mozilla/dom/ContentMediaController.h" @@ -1260,6 +1261,7 @@ void nsGlobalWindowInner::FreeInnerObjects() { mScrollbars = nullptr; mConsole = nullptr; + mCookieStore = nullptr; mPaintWorklet = nullptr; @@ -1450,6 +1452,7 @@ NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(nsGlobalWindowInner) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mScrollbars) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCrypto) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mConsole) + NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCookieStore) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPaintWorklet) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mExternal) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInstallTrigger) @@ -1562,6 +1565,7 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsGlobalWindowInner) NS_IMPL_CYCLE_COLLECTION_UNLINK(mScrollbars) NS_IMPL_CYCLE_COLLECTION_UNLINK(mCrypto) NS_IMPL_CYCLE_COLLECTION_UNLINK(mConsole) + NS_IMPL_CYCLE_COLLECTION_UNLINK(mCookieStore) NS_IMPL_CYCLE_COLLECTION_UNLINK(mPaintWorklet) NS_IMPL_CYCLE_COLLECTION_UNLINK(mExternal) NS_IMPL_CYCLE_COLLECTION_UNLINK(mInstallTrigger) @@ -7286,6 +7290,14 @@ already_AddRefed nsGlobalWindowInner::GetConsole(JSContext* aCx, return console.forget(); } +already_AddRefed nsGlobalWindowInner::CookieStore() { + if (!mCookieStore) { + mCookieStore = CookieStore::Create(this); + } + + return do_AddRef(mCookieStore); +} + bool nsGlobalWindowInner::IsSecureContext() const { JS::Realm* realm = js::GetNonCCWObjectRealm(GetWrapperPreserveColor()); return JS::GetIsSecureContext(realm); diff --git a/dom/base/nsGlobalWindowInner.h b/dom/base/nsGlobalWindowInner.h index 9dd9c8b6f5aa..4b6469d188f7 100644 --- a/dom/base/nsGlobalWindowInner.h +++ b/dom/base/nsGlobalWindowInner.h @@ -104,6 +104,7 @@ struct ChannelPixelLayout; class Credential; class ClientSource; class Console; +class CookieStore; class Crypto; class CustomElementRegistry; class DataTransfer; @@ -654,6 +655,8 @@ class nsGlobalWindowInner final : public mozilla::dom::EventTarget, already_AddRefed GetConsole(JSContext* aCx, mozilla::ErrorResult& aRv); + already_AddRefed CookieStore(); + // https://w3c.github.io/webappsec-secure-contexts/#dom-window-issecurecontext bool IsSecureContext() const; @@ -1374,6 +1377,7 @@ class nsGlobalWindowInner final : public mozilla::dom::EventTarget, RefPtr mCrypto; RefPtr mCacheStorage; RefPtr mConsole; + RefPtr mCookieStore; RefPtr mPaintWorklet; RefPtr mExternal; RefPtr mInstallTrigger; diff --git a/dom/cookiestore/CookieChangeEvent.cpp b/dom/cookiestore/CookieChangeEvent.cpp new file mode 100644 index 000000000000..f7b263f314d1 --- /dev/null +++ b/dom/cookiestore/CookieChangeEvent.cpp @@ -0,0 +1,53 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "mozilla/dom/CookieChangeEvent.h" +#include "mozilla/dom/CookieChangeEventBinding.h" + +namespace mozilla::dom { + +CookieChangeEvent::CookieChangeEvent(EventTarget* aOwner, + nsPresContext* aPresContext, + WidgetEvent* aEvent) + : Event(aOwner, aPresContext, aEvent) {} + +CookieChangeEvent::~CookieChangeEvent() = default; + +JSObject* CookieChangeEvent::WrapObjectInternal( + JSContext* aCx, JS::Handle aGivenProto) { + return mozilla::dom::CookieChangeEvent_Binding::Wrap(aCx, this, aGivenProto); +} + +void CookieChangeEvent::GetChanged(nsTArray& aList) const { + aList = mChanged.Clone(); +} + +void CookieChangeEvent::GetDeleted(nsTArray& aList) const { + aList = mDeleted.Clone(); +} + +/* static */ already_AddRefed CookieChangeEvent::Constructor( + const GlobalObject& aGlobal, const nsAString& aType, + const CookieChangeEventInit& aEventInit) { + nsCOMPtr t = do_QueryInterface(aGlobal.GetAsSupports()); + RefPtr event = new CookieChangeEvent(t, nullptr, nullptr); + bool trusted = event->Init(t); + + event->InitEvent(aType, aEventInit.mBubbles, aEventInit.mCancelable); + event->SetTrusted(trusted); + + if (aEventInit.mChanged.WasPassed()) { + event->mChanged = aEventInit.mChanged.Value(); + } + + if (aEventInit.mDeleted.WasPassed()) { + event->mDeleted = aEventInit.mDeleted.Value(); + } + + return event.forget(); +} + +} // namespace mozilla::dom diff --git a/dom/cookiestore/CookieChangeEvent.h b/dom/cookiestore/CookieChangeEvent.h new file mode 100644 index 000000000000..ba1a23721aa9 --- /dev/null +++ b/dom/cookiestore/CookieChangeEvent.h @@ -0,0 +1,41 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_CookieChangeEvent_h +#define mozilla_dom_CookieChangeEvent_h + +#include "mozilla/dom/Event.h" +#include "mozilla/dom/BindingUtils.h" +#include "mozilla/dom/CookieChangeEventBinding.h" + +namespace mozilla::dom { + +class CookieChangeEvent final : public Event { + public: + CookieChangeEvent(EventTarget* aOwner, nsPresContext* aPresContext, + WidgetEvent* aEvent); + + JSObject* WrapObjectInternal(JSContext* aCx, + JS::Handle aGivenProto) override; + + static already_AddRefed Constructor( + const GlobalObject& aGlobal, const nsAString& aType, + const CookieChangeEventInit& aEventInit); + + void GetChanged(nsTArray& aList) const; + + void GetDeleted(nsTArray& aList) const; + + private: + ~CookieChangeEvent(); + + nsTArray mChanged; + nsTArray mDeleted; +}; + +} // namespace mozilla::dom + +#endif /* mozilla_dom_CookieChangeEvent_h */ diff --git a/dom/cookiestore/CookieStore.cpp b/dom/cookiestore/CookieStore.cpp new file mode 100644 index 000000000000..09fc5556179f --- /dev/null +++ b/dom/cookiestore/CookieStore.cpp @@ -0,0 +1,94 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include "CookieStore.h" + +#include "mozilla/dom/Promise.h" +#include "nsIGlobalObject.h" + +namespace mozilla::dom { + +NS_IMPL_CYCLE_COLLECTION_CLASS(CookieStore) + +NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(CookieStore, + DOMEventTargetHelper) +NS_IMPL_CYCLE_COLLECTION_UNLINK_END + +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CookieStore, + DOMEventTargetHelper) +NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CookieStore) +NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper) + +NS_IMPL_ADDREF_INHERITED(CookieStore, DOMEventTargetHelper) +NS_IMPL_RELEASE_INHERITED(CookieStore, DOMEventTargetHelper) + +// static +already_AddRefed CookieStore::Create(nsIGlobalObject* aGlobal) { + return do_AddRef(new CookieStore(aGlobal)); +} + +CookieStore::CookieStore(nsIGlobalObject* aGlobal) + : DOMEventTargetHelper(aGlobal) {} + +CookieStore::~CookieStore() = default; + +JSObject* CookieStore::WrapObject(JSContext* aCx, + JS::Handle aGivenProto) { + return CookieStore_Binding::Wrap(aCx, this, aGivenProto); +} + +already_AddRefed CookieStore::Get(const nsAString& aName, + ErrorResult& aRv) { + aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); + return nullptr; +} + +already_AddRefed CookieStore::Get( + const CookieStoreGetOptions& aOptions, ErrorResult& aRv) { + aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); + return nullptr; +} + +already_AddRefed CookieStore::GetAll(const nsAString& aName, + ErrorResult& aRv) { + aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); + return nullptr; +} + +already_AddRefed CookieStore::GetAll( + const CookieStoreGetOptions& aOptions, ErrorResult& aRv) { + aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); + return nullptr; +} + +already_AddRefed CookieStore::Set(const nsAString& aName, + const nsAString& aValue, + ErrorResult& aRv) { + aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); + return nullptr; +} + +already_AddRefed CookieStore::Set(const CookieInit& aOptions, + ErrorResult& aRv) { + aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); + return nullptr; +} + +already_AddRefed CookieStore::Delete(const nsAString& aName, + ErrorResult& aRv) { + aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); + return nullptr; +} + +already_AddRefed CookieStore::Delete( + const CookieStoreDeleteOptions& aOptions, ErrorResult& aRv) { + aRv.Throw(NS_ERROR_NOT_IMPLEMENTED); + return nullptr; +} + +} // namespace mozilla::dom diff --git a/dom/cookiestore/CookieStore.h b/dom/cookiestore/CookieStore.h new file mode 100644 index 000000000000..b81222b5cc12 --- /dev/null +++ b/dom/cookiestore/CookieStore.h @@ -0,0 +1,61 @@ +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef mozilla_dom_CookieStore_h +#define mozilla_dom_CookieStore_h + +#include "mozilla/Attributes.h" +#include "mozilla/DOMEventTargetHelper.h" +#include "mozilla/dom/CookieStoreBinding.h" + +class nsIGlobalObject; + +namespace mozilla::dom { + +class Promise; + +class CookieStore final : public DOMEventTargetHelper { + friend class CookieStoreChild; + + public: + NS_DECL_ISUPPORTS_INHERITED + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(CookieStore, DOMEventTargetHelper) + + static already_AddRefed Create(nsIGlobalObject* aGlobalObject); + + JSObject* WrapObject(JSContext* aCx, + JS::Handle aGivenProto) override; + + already_AddRefed Get(const nsAString& aName, ErrorResult& aRv); + + already_AddRefed Get(const CookieStoreGetOptions& aOptions, + ErrorResult& aRv); + + already_AddRefed GetAll(const nsAString& aName, ErrorResult& aRv); + + already_AddRefed GetAll(const CookieStoreGetOptions& aOptions, + ErrorResult& aRv); + + already_AddRefed Set(const nsAString& aName, const nsAString& aValue, + ErrorResult& aRv); + + already_AddRefed Set(const CookieInit& aOptions, ErrorResult& aRv); + + already_AddRefed Delete(const nsAString& aName, ErrorResult& aRv); + + already_AddRefed Delete(const CookieStoreDeleteOptions& aOptions, + ErrorResult& aRv); + + IMPL_EVENT_HANDLER(change); + + private: + explicit CookieStore(nsIGlobalObject* aGlobal); + ~CookieStore(); +}; + +} // namespace mozilla::dom + +#endif /* mozilla_dom_CookieStore_h */ diff --git a/dom/cookiestore/moz.build b/dom/cookiestore/moz.build new file mode 100644 index 000000000000..bada9cf6d3be --- /dev/null +++ b/dom/cookiestore/moz.build @@ -0,0 +1,25 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +with Files("**"): + BUG_COMPONENT = ("Core", "Networking: Cookies") + +EXPORTS.mozilla.dom += [ + "CookieChangeEvent.h", + "CookieStore.h", +] + +UNIFIED_SOURCES += [ + "CookieChangeEvent.cpp", + "CookieStore.cpp", +] + +LOCAL_INCLUDES += [ + "../base", + "../events", +] + +include("/ipc/chromium/chromium-config.mozbuild") + +FINAL_LIBRARY = "xul" diff --git a/dom/moz.build b/dom/moz.build index df4467f46464..e76562d20224 100644 --- a/dom/moz.build +++ b/dom/moz.build @@ -37,6 +37,7 @@ DIRS += [ "chrome-webidl", "clients", "commandhandler", + "cookiestore", "credentialmanagement", "crypto", "debugger", diff --git a/dom/webidl/CookieChangeEvent.webidl b/dom/webidl/CookieChangeEvent.webidl new file mode 100644 index 000000000000..f272b5dbb709 --- /dev/null +++ b/dom/webidl/CookieChangeEvent.webidl @@ -0,0 +1,27 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + * + * The origin of this IDL file is: + * https://wicg.github.io/cookie-store/#CookieChangeEvent + */ + +[Exposed=Window, + SecureContext, + Pref="dom.cookieStore.enabled"] +interface CookieChangeEvent : Event { + constructor(DOMString type, optional CookieChangeEventInit eventInitDict = {}); + // TODO: Use FrozenArray once available. (Bug 1236777) + // [SameObject] readonly attribute FrozenArray changed; + // [SameObject] readonly attribute FrozenArray deleted; + [Frozen, Cached, Constant] + readonly attribute sequence changed; + [Frozen, Cached, Constant] + readonly attribute sequence deleted; +}; + +dictionary CookieChangeEventInit : EventInit { + CookieList changed; + CookieList deleted; +}; diff --git a/dom/webidl/CookieStore.webidl b/dom/webidl/CookieStore.webidl new file mode 100644 index 000000000000..c2b6b7eaeba6 --- /dev/null +++ b/dom/webidl/CookieStore.webidl @@ -0,0 +1,100 @@ +/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + * + * The origin of this IDL file is + * https://wicg.github.io/cookie-store/#idl-index + * + * Copyright © 2024 the Contributors to the Cookie Store API Specification, + * published by the Web Platform Incubator Community Group under the W3C + * Community Contributor License Agreement (CLA). A human-readable summary is + * available. + */ + +[Exposed=(ServiceWorker,Window), + SecureContext, + Pref="dom.cookieStore.enabled"] +interface CookieStore : EventTarget { + [Throws] + Promise get(USVString name); + [Throws] + Promise get(optional CookieStoreGetOptions options = {}); + + [Throws] + Promise getAll(USVString name); + [Throws] + Promise getAll(optional CookieStoreGetOptions options = {}); + + [Throws] + Promise set(USVString name, USVString value); + [Throws] + Promise set(CookieInit options); + + [Throws] + Promise delete(USVString name); + [Throws] + Promise delete(CookieStoreDeleteOptions options); + + /* Bug 1475599 - By spec, `onchange` should be available only on `Window`, + * but because we do not want to implement the ExtendableCookieChangeEvent + * for ServiceWorker, we have decided to expose this EventHandler everywhere. + */ + //[Exposed=Window] + attribute EventHandler onchange; +}; + +dictionary CookieStoreGetOptions { + USVString name; + USVString url; +}; + +enum CookieSameSite { + "strict", + "lax", + "none" +}; + +dictionary CookieInit { + required USVString name; + required USVString value; + DOMHighResTimeStamp? expires = null; + USVString? domain = null; + USVString path = "/"; + CookieSameSite sameSite = "strict"; + boolean partitioned = false; +}; + +dictionary CookieStoreDeleteOptions { + required USVString name; + USVString? domain = null; + USVString path = "/"; + boolean partitioned = false; +}; + +dictionary CookieListItem { + USVString name; + USVString value; + + /* Bug 1475599 - We decide to do not implement the entire cookie-store spec. + * Instead, we implement only the subset that is compatible with document.cookie */ + // USVString? domain; + // USVString path; + // DOMHighResTimeStamp? expires; + // boolean secure; + // CookieSameSite sameSite; + // boolean partitioned; +}; + +typedef sequence CookieList; + +/* Bug 1475599 - We decide to do not implement the entire cookie-store spec. + * Instead, we implement only the subset that is compatible with document.cookie +[Exposed=(ServiceWorker,Window), + SecureContext] +interface CookieStoreManager { + Promise subscribe(sequence subscriptions); + Promise> getSubscriptions(); + Promise unsubscribe(sequence subscriptions); +}; +*/ diff --git a/dom/webidl/ServiceWorkerGlobalScope.webidl b/dom/webidl/ServiceWorkerGlobalScope.webidl index 914fc37c8bee..59226a16be4d 100644 --- a/dom/webidl/ServiceWorkerGlobalScope.webidl +++ b/dom/webidl/ServiceWorkerGlobalScope.webidl @@ -7,6 +7,7 @@ * http://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html * http://w3c.github.io/push-api/ * https://notifications.spec.whatwg.org/ + * https://wicg.github.io/cookie-store/#ServiceWorkerGlobalScope * * You are granted a license to use, reproduce and create derivative works of * this document. @@ -47,3 +48,13 @@ partial interface ServiceWorkerGlobalScope { // Mixin the WebExtensions API globals (the actual properties are only available to // extension service workers, locked behind a Func="extensions::ExtensionAPIAllowed" annotation). ServiceWorkerGlobalScope includes ExtensionGlobalsMixin; + +// https://wicg.github.io/cookie-store/#ServiceWorkerGlobalScope +partial interface ServiceWorkerGlobalScope { + [SameObject, Pref="dom.cookieStore.enabled"] readonly attribute CookieStore cookieStore; + +/* Bug 1475599 - We decide to do not implement the entire cookie-store spec. + * Instead, we implement only the subset that is compatible with document.cookie + attribute EventHandler oncookiechange; + */ +}; diff --git a/dom/webidl/ServiceWorkerRegistration.webidl b/dom/webidl/ServiceWorkerRegistration.webidl index 6acb887cd616..38575c9498cf 100644 --- a/dom/webidl/ServiceWorkerRegistration.webidl +++ b/dom/webidl/ServiceWorkerRegistration.webidl @@ -7,6 +7,7 @@ * http://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html * https://w3c.github.io/push-api/ * https://notifications.spec.whatwg.org/ + * https://wicg.github.io/cookie-store/#idl-index */ [Func="ServiceWorkerVisible", @@ -52,3 +53,11 @@ partial interface ServiceWorkerRegistration { [NewObject, Func="mozilla::dom::Notification::PrefEnabled"] Promise> getNotifications(optional GetNotificationOptions filter = {}); }; + +/* Bug 1475599 - We decide to do not implement the entire cookie-store spec. + * Instead, we implement only the subset that is compatible with document.cookie +[Exposed=(ServiceWorker,Window)] +partial interface ServiceWorkerRegistration { + [SameObject] readonly attribute CookieStoreManager cookies; +}; +*/ diff --git a/dom/webidl/Window.webidl b/dom/webidl/Window.webidl index f7fe74094a66..2994df771282 100644 --- a/dom/webidl/Window.webidl +++ b/dom/webidl/Window.webidl @@ -17,6 +17,7 @@ * https://w3c.github.io/requestidlecallback/ * https://drafts.css-houdini.org/css-paint-api-1/#dom-window-paintworklet * https://wicg.github.io/visual-viewport/#the-visualviewport-interface + * https://wicg.github.io/cookie-store/#Window */ interface Principal; @@ -811,3 +812,9 @@ partial interface Window { dictionary WindowPostMessageOptions : StructuredSerializeOptions { USVString targetOrigin = "/"; }; + +// https://wicg.github.io/cookie-store/#Window +[SecureContext] +partial interface Window { + [SameObject, Pref="dom.cookieStore.enabled"] readonly attribute CookieStore cookieStore; +}; diff --git a/dom/webidl/moz.build b/dom/webidl/moz.build index 0d576f318c95..e9c83112410a 100644 --- a/dom/webidl/moz.build +++ b/dom/webidl/moz.build @@ -480,6 +480,8 @@ WEBIDL_FILES = [ "Console.webidl", "ConstantSourceNode.webidl", "ConvolverNode.webidl", + "CookieChangeEvent.webidl", + "CookieStore.webidl", "CreateOfferRequest.webidl", "CredentialManagement.webidl", "Crypto.webidl", diff --git a/dom/workers/WorkerScope.cpp b/dom/workers/WorkerScope.cpp index 22b6a5a63377..bf4e8683f622 100644 --- a/dom/workers/WorkerScope.cpp +++ b/dom/workers/WorkerScope.cpp @@ -48,6 +48,7 @@ #include "mozilla/dom/ClientSource.h" #include "mozilla/dom/Clients.h" #include "mozilla/dom/Console.h" +#include "mozilla/dom/CookieStore.h" #include "mozilla/dom/DOMMozPromiseRequestHolder.h" #include "mozilla/dom/DebuggerNotification.h" #include "mozilla/dom/DebuggerNotificationBinding.h" @@ -1099,7 +1100,8 @@ void SharedWorkerGlobalScope::Close() { } NS_IMPL_CYCLE_COLLECTION_INHERITED(ServiceWorkerGlobalScope, WorkerGlobalScope, - mClients, mExtensionBrowser, mRegistration) + mClients, mExtensionBrowser, mRegistration, + mCookieStore) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ServiceWorkerGlobalScope) NS_INTERFACE_MAP_END_INHERITING(WorkerGlobalScope) @@ -1246,6 +1248,14 @@ ServiceWorkerGlobalScope::AcquireExtensionBrowser() { return mExtensionBrowser.clonePtr(); } +already_AddRefed ServiceWorkerGlobalScope::CookieStore() { + if (!mCookieStore) { + mCookieStore = CookieStore::Create(this); + } + + return do_AddRef(mCookieStore); +} + bool WorkerDebuggerGlobalScope::WrapGlobalObject( JSContext* aCx, JS::MutableHandle aReflector) { AssertIsOnWorkerThread(); diff --git a/dom/workers/WorkerScope.h b/dom/workers/WorkerScope.h index c1fba611643e..391e283310a1 100644 --- a/dom/workers/WorkerScope.h +++ b/dom/workers/WorkerScope.h @@ -55,6 +55,7 @@ class ClientInfo; class ClientSource; class Clients; class Console; +class CookieStore; class Crypto; class DOMString; class DebuggerNotificationManager; @@ -482,6 +483,8 @@ class ServiceWorkerGlobalScope final : public WorkerGlobalScope { void EventListenerAdded(nsAtom* aType) override; + already_AddRefed CookieStore(); + IMPL_EVENT_HANDLER(message) IMPL_EVENT_HANDLER(messageerror) @@ -491,6 +494,8 @@ class ServiceWorkerGlobalScope final : public WorkerGlobalScope { IMPL_EVENT_HANDLER(push) IMPL_EVENT_HANDLER(pushsubscriptionchange) + IMPL_EVENT_HANDLER(cookiechange) + private: ~ServiceWorkerGlobalScope(); @@ -500,6 +505,7 @@ class ServiceWorkerGlobalScope final : public WorkerGlobalScope { const nsString mScope; RefPtr mRegistration; SafeRefPtr mExtensionBrowser; + RefPtr mCookieStore; }; class WorkerDebuggerGlobalScope final : public WorkerGlobalScopeBase { diff --git a/modules/libpref/init/StaticPrefList.yaml b/modules/libpref/init/StaticPrefList.yaml index 2b71e9e8f25b..2ea96f3abbeb 100644 --- a/modules/libpref/init/StaticPrefList.yaml +++ b/modules/libpref/init/StaticPrefList.yaml @@ -2348,6 +2348,12 @@ value: true mirror: always +# Disable cookie-store API +- name: dom.cookieStore.enabled + type: RelaxedAtomicBool + value: false + mirror: always + # Is support for CSSPseudoElement enabled? - name: dom.css_pseudo_element.enabled type: bool diff --git a/testing/web-platform/meta/cookie-store/__dir__.ini b/testing/web-platform/meta/cookie-store/__dir__.ini index 50828dcc79f0..fbbbe2ca3e67 100644 --- a/testing/web-platform/meta/cookie-store/__dir__.ini +++ b/testing/web-platform/meta/cookie-store/__dir__.ini @@ -1,3 +1,4 @@ # https://bugzilla.mozilla.org/show_bug.cgi?id=1475599 +prefs: [dom.cookieStore.enabled:true] implementation-status: backlog lsan-allowed: [Alloc, Malloc, Then, mozilla::BasePrincipal::CreateContentPrincipal, mozilla::dom::DocGroup::Create, mozilla::dom::ServiceWorkerJobQueue::RunJob, mozilla::dom::ServiceWorkerManager::Unregister, mozilla::dom::ServiceWorkerRegistrationMainThread::Unregister, mozilla::dom::UnregisterCallback::UnregisterCallback, mozilla::net::nsStandardURL::TemplatedMutator, operator] diff --git a/xpcom/ds/StaticAtoms.py b/xpcom/ds/StaticAtoms.py index d6d43f3dbd75..3402b91d50fa 100644 --- a/xpcom/ds/StaticAtoms.py +++ b/xpcom/ds/StaticAtoms.py @@ -800,6 +800,7 @@ STATIC_ATOMS = [ Atom("oncontextmenu", "oncontextmenu"), Atom("oncontextlost", "oncontextlost"), Atom("oncontextrestored", "oncontextrestored"), + Atom("oncookiechange", "oncookiechange"), Atom("oncopy", "oncopy"), Atom("oncut", "oncut"), Atom("oncurrententrychange", "oncurrententrychange"),