Bug 1958405 - Move CachesEnabled out of nsGlobalWindowInner. r=webidl,smaug,asuth

Having this method here means nsGlobalWindowInner needs to be included in a number
of bindings files, and doesn't make a lot of sense. This patch moves it to Cache
and CacheStorage (the latter being a wrapper around the former) to avoid extra
includes.

Differential Revision: https://phabricator.services.mozilla.com/D244443
This commit is contained in:
Andrew McCreight
2025-04-03 22:26:13 +00:00
parent 9f78b36b73
commit ecb7c82162
9 changed files with 22 additions and 14 deletions

View File

@@ -3347,15 +3347,6 @@ bool nsGlobalWindowInner::DeviceSensorsEnabled(JSContext*, JSObject*) {
return Preferences::GetBool("device.sensors.enabled");
}
/* static */
bool nsGlobalWindowInner::CachesEnabled(JSContext* aCx, JSObject* aObj) {
if (!IsSecureContextOrObjectIsFromSecureContext(aCx, aObj)) {
return StaticPrefs::dom_caches_testing_enabled() ||
ServiceWorkersEnabled(aCx, aObj);
}
return true;
}
/* static */
bool nsGlobalWindowInner::IsGleanNeeded(JSContext* aCx, JSObject* aObj) {
// Glean is needed in ChromeOnly contexts and also in privileged about pages.

View File

@@ -425,8 +425,6 @@ class nsGlobalWindowInner final : public mozilla::dom::EventTarget,
static bool DeviceSensorsEnabled(JSContext*, JSObject*);
static bool CachesEnabled(JSContext* aCx, JSObject*);
// WebIDL permission Func for whether Glean APIs are permitted.
static bool IsGleanNeeded(JSContext*, JSObject*);

10
dom/cache/Cache.cpp vendored
View File

@@ -14,6 +14,7 @@
#include "mozilla/dom/PromiseNativeHandler.h"
#include "mozilla/dom/Response.h"
#include "mozilla/dom/RootedDictionary.h"
#include "mozilla/dom/ServiceWorkerUtils.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/CacheBinding.h"
#include "mozilla/dom/cache/AutoUtils.h"
@@ -223,6 +224,15 @@ Cache::Cache(nsIGlobalObject* aGlobal, CacheChild* aActor, Namespace aNamespace)
mActor->SetListener(this);
}
// static
bool Cache::CachesEnabled(JSContext* aCx, JSObject* aObj) {
if (!IsSecureContextOrObjectIsFromSecureContext(aCx, aObj)) {
return StaticPrefs::dom_caches_testing_enabled() ||
ServiceWorkersEnabled(aCx, aObj);
}
return true;
}
already_AddRefed<Promise> Cache::Match(JSContext* aCx,
const RequestOrUTF8String& aRequest,
const CacheQueryOptions& aOptions,

2
dom/cache/Cache.h vendored
View File

@@ -44,6 +44,8 @@ class Cache final : public nsISupports,
public:
Cache(nsIGlobalObject* aGlobal, CacheChild* aActor, Namespace aNamespace);
static bool CachesEnabled(JSContext* aCx, JSObject*);
// webidl interface methods
already_AddRefed<Promise> Match(JSContext* aCx,
const RequestOrUTF8String& aRequest,

View File

@@ -257,6 +257,11 @@ bool CacheStorage::DefineCachesForSandbox(JSContext* aCx,
return JS_DefineProperty(aCx, aGlobal, "caches", caches, JSPROP_ENUMERATE);
}
// static
bool CacheStorage::CachesEnabled(JSContext* aCx, JSObject* aObj) {
return cache::Cache::CachesEnabled(aCx, aObj);
}
CacheStorage::CacheStorage(Namespace aNamespace, nsIGlobalObject* aGlobal,
const PrincipalInfo& aPrincipalInfo,
SafeRefPtr<CacheWorkerRef> aWorkerRef)

View File

@@ -55,6 +55,8 @@ class CacheStorage final : public nsISupports,
static bool DefineCachesForSandbox(JSContext* aCx,
JS::Handle<JSObject*> aGlobal);
static bool CachesEnabled(JSContext* aCx, JSObject*);
// webidl interface methods
already_AddRefed<Promise> Match(JSContext* aCx,
const RequestOrUTF8String& aRequest,

View File

@@ -8,7 +8,7 @@
*/
[Exposed=(Window,Worker),
Func="nsGlobalWindowInner::CachesEnabled"]
Func="cache::Cache::CachesEnabled"]
interface Cache {
[NewObject]
Promise<Response> match(RequestInfo request, optional CacheQueryOptions options = {});

View File

@@ -10,7 +10,7 @@
interface Principal;
[Exposed=(Window,Worker),
Func="nsGlobalWindowInner::CachesEnabled"]
Func="cache::CacheStorage::CachesEnabled"]
interface CacheStorage {
[Throws, ChromeOnly]
constructor(CacheStorageNamespace namespace, Principal principal);

View File

@@ -72,7 +72,7 @@ partial interface mixin WindowOrWorkerGlobalScope {
// https://w3c.github.io/ServiceWorker/#self-caches
partial interface mixin WindowOrWorkerGlobalScope {
[Throws, Func="nsGlobalWindowInner::CachesEnabled", SameObject]
[Throws, Func="cache::CacheStorage::CachesEnabled", SameObject]
readonly attribute CacheStorage caches;
};