From 2ffa8b7ffc766fdd2fab237c52f8135ae5bb65cd Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Mon, 31 Aug 2015 14:26:30 -0700 Subject: [PATCH] Bug 1184607 P7.6 Expose CacheStorage .caches property on xpcshell global. r=smaug * * * 7.6 interdiff --- dom/cache/CacheStorage.cpp | 35 +++++++++++++++++++++++++++++++++++ dom/cache/CacheStorage.h | 3 +++ js/xpconnect/src/Sandbox.cpp | 6 ++++++ js/xpconnect/src/xpcprivate.h | 1 + 4 files changed, 45 insertions(+) diff --git a/dom/cache/CacheStorage.cpp b/dom/cache/CacheStorage.cpp index a6fb8a51e75..ae828d7b50d 100644 --- a/dom/cache/CacheStorage.cpp +++ b/dom/cache/CacheStorage.cpp @@ -239,6 +239,41 @@ CacheStorage::CreateOnWorker(Namespace aNamespace, nsIGlobalObject* aGlobal, return ref.forget(); } +// static +bool +CacheStorage::DefineCaches(JSContext* aCx, JS::Handle aGlobal) +{ + MOZ_ASSERT(NS_IsMainThread()); + MOZ_ASSERT(js::GetObjectClass(aGlobal)->flags & JSCLASS_DOM_GLOBAL, + "Passed object is not a global object!"); + + if (NS_WARN_IF(!CacheStorageBinding::GetConstructorObject(aCx, aGlobal) || + !CacheBinding::GetConstructorObject(aCx, aGlobal))) { + return false; + } + + nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aGlobal); + MOZ_ASSERT(principal); + + ErrorResult rv; + nsRefPtr storage = + CreateOnMainThread(DEFAULT_NAMESPACE, xpc::NativeGlobal(aGlobal), principal, + false, /* private browsing */ + true, /* force trusted */ + rv); + if (NS_WARN_IF(rv.Failed())) { + return ThrowMethodFailed(aCx, rv); + } + + JS::Rooted caches(aCx); + js::AssertSameCompartment(aCx, aGlobal); + if (NS_WARN_IF(!ToJSValue(aCx, storage, &caches))) { + return false; + } + + return JS_DefineProperty(aCx, aGlobal, "caches", caches, JSPROP_ENUMERATE); +} + CacheStorage::CacheStorage(Namespace aNamespace, nsIGlobalObject* aGlobal, const PrincipalInfo& aPrincipalInfo, Feature* aFeature) : mNamespace(aNamespace) diff --git a/dom/cache/CacheStorage.h b/dom/cache/CacheStorage.h index 98a9338173f..19061ab3416 100644 --- a/dom/cache/CacheStorage.h +++ b/dom/cache/CacheStorage.h @@ -56,6 +56,9 @@ public: CreateOnWorker(Namespace aNamespace, nsIGlobalObject* aGlobal, workers::WorkerPrivate* aWorkerPrivate, ErrorResult& aRv); + static bool + DefineCaches(JSContext* aCx, JS::Handle aGlobal); + // webidl interface methods already_AddRefed Match(const RequestOrUSVString& aRequest, const CacheQueryOptions& aOptions, diff --git a/js/xpconnect/src/Sandbox.cpp b/js/xpconnect/src/Sandbox.cpp index 1a08862176c..8a44da5a2af 100644 --- a/js/xpconnect/src/Sandbox.cpp +++ b/js/xpconnect/src/Sandbox.cpp @@ -29,6 +29,7 @@ #include "Crypto.h" #include "mozilla/dom/BindingUtils.h" #include "mozilla/dom/BlobBinding.h" +#include "mozilla/dom/cache/CacheStorage.h" #include "mozilla/dom/CSSBinding.h" #include "mozilla/dom/indexedDB/IndexedDatabaseManager.h" #include "mozilla/dom/Fetch.h" @@ -907,6 +908,8 @@ xpc::GlobalProperties::Parse(JSContext* cx, JS::HandleObject obj) #endif } else if (!strcmp(name.ptr(), "fetch")) { fetch = true; + } else if (!strcmp(name.ptr(), "caches")) { + caches = true; } else { JS_ReportError(cx, "Unknown property name: %s", name.ptr()); return false; @@ -972,6 +975,9 @@ xpc::GlobalProperties::Define(JSContext* cx, JS::HandleObject obj) if (fetch && !SandboxCreateFetch(cx, obj)) return false; + if (caches && !dom::cache::CacheStorage::DefineCaches(cx, obj)) + return false; + return true; } diff --git a/js/xpconnect/src/xpcprivate.h b/js/xpconnect/src/xpcprivate.h index 9f09a8fa449..ed61224d665 100644 --- a/js/xpconnect/src/xpcprivate.h +++ b/js/xpconnect/src/xpcprivate.h @@ -3418,6 +3418,7 @@ struct GlobalProperties { bool crypto : 1; bool rtcIdentityProvider : 1; bool fetch : 1; + bool caches : 1; }; // Infallible.