Bug 1184607 P7.6 Expose CacheStorage .caches property on xpcshell global. r=smaug

* * *
7.6 interdiff
This commit is contained in:
Ben Kelly 2015-08-31 14:26:30 -07:00
parent 40781e8295
commit 2ffa8b7ffc
4 changed files with 45 additions and 0 deletions

View File

@ -239,6 +239,41 @@ CacheStorage::CreateOnWorker(Namespace aNamespace, nsIGlobalObject* aGlobal,
return ref.forget();
}
// static
bool
CacheStorage::DefineCaches(JSContext* aCx, JS::Handle<JSObject*> 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<CacheStorage> 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<JS::Value> 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)

View File

@ -56,6 +56,9 @@ public:
CreateOnWorker(Namespace aNamespace, nsIGlobalObject* aGlobal,
workers::WorkerPrivate* aWorkerPrivate, ErrorResult& aRv);
static bool
DefineCaches(JSContext* aCx, JS::Handle<JSObject*> aGlobal);
// webidl interface methods
already_AddRefed<Promise> Match(const RequestOrUSVString& aRequest,
const CacheQueryOptions& aOptions,

View File

@ -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;
}

View File

@ -3418,6 +3418,7 @@ struct GlobalProperties {
bool crypto : 1;
bool rtcIdentityProvider : 1;
bool fetch : 1;
bool caches : 1;
};
// Infallible.