2015-03-02 16:20:00 +03:00
|
|
|
/* -*- 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/cache/CacheStorage.h"
|
|
|
|
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2017-10-26 14:35:08 +03:00
|
|
|
#include "mozilla/dom/CacheBinding.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/CacheStorageBinding.h"
|
2017-10-26 14:35:08 +03:00
|
|
|
#include "mozilla/dom/InternalRequest.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
#include "mozilla/dom/Response.h"
|
|
|
|
#include "mozilla/dom/cache/AutoUtils.h"
|
|
|
|
#include "mozilla/dom/cache/Cache.h"
|
|
|
|
#include "mozilla/dom/cache/CacheChild.h"
|
|
|
|
#include "mozilla/dom/cache/CacheStorageChild.h"
|
2019-07-12 14:17:59 +03:00
|
|
|
#include "mozilla/dom/cache/CacheWorkerRef.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/dom/cache/PCacheChild.h"
|
|
|
|
#include "mozilla/dom/cache/ReadStream.h"
|
|
|
|
#include "mozilla/dom/cache/TypeUtils.h"
|
2019-02-23 19:03:40 +03:00
|
|
|
#include "mozilla/dom/quota/QuotaManager.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "mozilla/ipc/BackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundSharedTypes.h"
|
2019-07-26 04:10:23 +03:00
|
|
|
#include "mozilla/StaticPrefs_dom.h"
|
2020-07-03 13:14:24 +03:00
|
|
|
#include "mozilla/StaticPrefs_extensions.h"
|
2015-10-26 19:14:47 +03:00
|
|
|
#include "nsContentUtils.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "nsIGlobalObject.h"
|
2019-08-30 10:57:34 +03:00
|
|
|
#include "nsMixedContentBlocker.h"
|
2015-06-28 06:19:24 +03:00
|
|
|
#include "nsURLParsers.h"
|
2020-09-09 01:55:38 +03:00
|
|
|
#include "js/Object.h" // JS::GetClass
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom::cache {
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
using mozilla::ErrorResult;
|
2015-11-02 08:53:26 +03:00
|
|
|
using mozilla::Unused;
|
2019-02-23 19:03:40 +03:00
|
|
|
using mozilla::dom::quota::QuotaManager;
|
2015-03-02 16:20:00 +03:00
|
|
|
using mozilla::ipc::BackgroundChild;
|
|
|
|
using mozilla::ipc::IProtocol;
|
|
|
|
using mozilla::ipc::PBackgroundChild;
|
|
|
|
using mozilla::ipc::PrincipalInfo;
|
|
|
|
using mozilla::ipc::PrincipalToPrincipalInfo;
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(mozilla::dom::cache::CacheStorage);
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(mozilla::dom::cache::CacheStorage);
|
2015-04-16 22:00:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(mozilla::dom::cache::CacheStorage,
|
|
|
|
mGlobal);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CacheStorage)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2015-04-16 22:00:15 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2015-03-02 16:20:00 +03:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
// We cannot reference IPC types in a webidl binding implementation header. So
|
2017-10-24 13:02:40 +03:00
|
|
|
// define this in the .cpp.
|
2015-04-16 22:00:15 +03:00
|
|
|
struct CacheStorage::Entry final {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> mPromise;
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheOpArgs mArgs;
|
|
|
|
// We cannot add the requests until after the actor is present. So store
|
|
|
|
// the request data separately for now.
|
2020-05-11 15:10:53 +03:00
|
|
|
SafeRefPtr<InternalRequest> mRequest;
|
2015-04-16 22:00:15 +03:00
|
|
|
};
|
|
|
|
|
2015-06-28 06:19:24 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool IsTrusted(const PrincipalInfo& aPrincipalInfo, bool aTestingPrefEnabled) {
|
|
|
|
// Can happen on main thread or worker thread
|
|
|
|
|
|
|
|
if (aPrincipalInfo.type() == PrincipalInfo::TSystemPrincipalInfo) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Require a ContentPrincipal to avoid null principal, etc.
|
2016-11-20 19:57:22 +03:00
|
|
|
if (NS_WARN_IF(aPrincipalInfo.type() !=
|
|
|
|
PrincipalInfo::TContentPrincipalInfo)) {
|
2015-06-28 06:19:24 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're in testing mode, then don't do any more work to determing if
|
|
|
|
// the origin is trusted. We have to run some tests as http.
|
|
|
|
if (aTestingPrefEnabled) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now parse the scheme of the principal's origin. This is a short term
|
|
|
|
// method for determining "trust". In the long term we need to implement
|
|
|
|
// the full algorithm here:
|
|
|
|
//
|
|
|
|
// https://w3c.github.io/webappsec/specs/powerfulfeatures/#settings-secure
|
|
|
|
//
|
|
|
|
// TODO: Implement full secure setting algorithm. (bug 1177856)
|
|
|
|
|
|
|
|
const nsCString& flatURL = aPrincipalInfo.get_ContentPrincipalInfo().spec();
|
|
|
|
const char* url = flatURL.get();
|
|
|
|
|
|
|
|
// off the main thread URL parsing using nsStdURLParser.
|
|
|
|
nsCOMPtr<nsIURLParser> urlParser = new nsStdURLParser();
|
|
|
|
|
|
|
|
uint32_t schemePos;
|
|
|
|
int32_t schemeLen;
|
|
|
|
uint32_t authPos;
|
|
|
|
int32_t authLen;
|
|
|
|
nsresult rv =
|
|
|
|
urlParser->ParseURL(url, flatURL.Length(), &schemePos, &schemeLen,
|
|
|
|
&authPos, &authLen, nullptr, nullptr); // ignore path
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString scheme(Substring(flatURL, schemePos, schemeLen));
|
|
|
|
if (scheme.LowerCaseEqualsLiteral("https") ||
|
|
|
|
scheme.LowerCaseEqualsLiteral("file")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-03 13:14:24 +03:00
|
|
|
if (StaticPrefs::extensions_backgroundServiceWorker_enabled_AtStartup() &&
|
|
|
|
scheme.LowerCaseEqualsLiteral("moz-extension")) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-28 06:19:24 +03:00
|
|
|
uint32_t hostPos;
|
|
|
|
int32_t hostLen;
|
|
|
|
|
|
|
|
rv = urlParser->ParseAuthority(url + authPos, authLen, nullptr,
|
|
|
|
nullptr, // ignore username
|
|
|
|
nullptr, nullptr, // ignore password
|
|
|
|
&hostPos, &hostLen,
|
|
|
|
nullptr); // ignore port
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentCSubstring hostname(url + authPos + hostPos, hostLen);
|
|
|
|
|
2019-08-30 10:57:34 +03:00
|
|
|
return nsMixedContentBlocker::IsPotentiallyTrustworthyLoopbackHost(hostname);
|
2015-06-28 06:19:24 +03:00
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-06-28 06:19:24 +03:00
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<CacheStorage> CacheStorage::CreateOnMainThread(
|
|
|
|
Namespace aNamespace, nsIGlobalObject* aGlobal, nsIPrincipal* aPrincipal,
|
2015-06-28 06:19:24 +03:00
|
|
|
bool aForceTrustedOrigin, ErrorResult& aRv) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aGlobal);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aPrincipal);
|
2015-03-02 16:20:00 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-06-28 06:19:24 +03:00
|
|
|
PrincipalInfo principalInfo;
|
|
|
|
nsresult rv = PrincipalToPrincipalInfo(aPrincipal, &principalInfo);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-02-23 19:03:40 +03:00
|
|
|
if (NS_WARN_IF(!QuotaManager::IsPrincipalInfoValid(principalInfo))) {
|
|
|
|
NS_WARNING("CacheStorage not supported on invalid origins.");
|
|
|
|
RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2015-06-28 06:19:24 +03:00
|
|
|
bool testingEnabled =
|
|
|
|
aForceTrustedOrigin ||
|
|
|
|
Preferences::GetBool("dom.caches.testing.enabled", false) ||
|
2018-09-10 21:36:16 +03:00
|
|
|
StaticPrefs::dom_serviceWorkers_testing_enabled();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2015-06-28 06:19:24 +03:00
|
|
|
if (!IsTrusted(principalInfo, testingEnabled)) {
|
|
|
|
NS_WARNING("CacheStorage not supported on untrusted origins.");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
|
2015-06-27 05:36:39 +03:00
|
|
|
return ref.forget();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheStorage> ref =
|
|
|
|
new CacheStorage(aNamespace, aGlobal, principalInfo, nullptr);
|
2015-03-02 16:20:00 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<CacheStorage> CacheStorage::CreateOnWorker(
|
|
|
|
Namespace aNamespace, nsIGlobalObject* aGlobal,
|
|
|
|
WorkerPrivate* aWorkerPrivate, ErrorResult& aRv) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aGlobal);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aWorkerPrivate);
|
2015-03-02 16:20:00 +03:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2016-09-20 04:13:00 +03:00
|
|
|
if (aWorkerPrivate->GetOriginAttributes().mPrivateBrowsingId > 0) {
|
2015-06-27 05:36:40 +03:00
|
|
|
NS_WARNING("CacheStorage not supported during private browsing.");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
|
2015-06-27 05:36:40 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2020-05-11 15:12:22 +03:00
|
|
|
SafeRefPtr<CacheWorkerRef> workerRef =
|
2019-07-12 14:17:59 +03:00
|
|
|
CacheWorkerRef::Create(aWorkerPrivate, CacheWorkerRef::eIPCWorkerRef);
|
|
|
|
if (!workerRef) {
|
2015-03-02 16:20:00 +03:00
|
|
|
NS_WARNING("Worker thread is shutting down.");
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-05-29 18:42:43 +03:00
|
|
|
const PrincipalInfo& principalInfo =
|
|
|
|
aWorkerPrivate->GetEffectiveStoragePrincipalInfo();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2019-02-23 19:03:40 +03:00
|
|
|
if (NS_WARN_IF(!QuotaManager::IsPrincipalInfoValid(principalInfo))) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-02 21:47:10 +03:00
|
|
|
// We have a number of cases where we want to skip the https scheme
|
|
|
|
// validation:
|
|
|
|
//
|
|
|
|
// 1) Any worker when dom.caches.testing.enabled pref is true.
|
|
|
|
// 2) Any worker when dom.serviceWorkers.testing.enabled pref is true. This
|
|
|
|
// is mainly because most sites using SWs will expect Cache to work if
|
|
|
|
// SWs are enabled.
|
|
|
|
// 3) If the window that created this worker has the devtools SW testing
|
|
|
|
// option enabled. Same reasoning as (2).
|
|
|
|
// 4) If the worker itself is a ServiceWorker, then we always skip the
|
|
|
|
// origin checks. The ServiceWorker has its own trusted origin checks
|
|
|
|
// that are better than ours. In addition, we don't have information
|
|
|
|
// about the window any more, so we can't do our own checks.
|
2018-09-10 21:36:15 +03:00
|
|
|
bool testingEnabled = StaticPrefs::dom_caches_testing_enabled() ||
|
2018-09-10 21:36:16 +03:00
|
|
|
StaticPrefs::dom_serviceWorkers_testing_enabled() ||
|
2015-07-02 21:47:10 +03:00
|
|
|
aWorkerPrivate->ServiceWorkersTestingInWindow() ||
|
|
|
|
aWorkerPrivate->IsServiceWorker();
|
2015-06-28 06:19:24 +03:00
|
|
|
|
|
|
|
if (!IsTrusted(principalInfo, testingEnabled)) {
|
|
|
|
NS_WARNING("CacheStorage not supported on untrusted origins.");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
|
2015-06-27 05:36:39 +03:00
|
|
|
return ref.forget();
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:12:22 +03:00
|
|
|
RefPtr<CacheStorage> ref = new CacheStorage(
|
|
|
|
aNamespace, aGlobal, principalInfo, std::move(workerRef));
|
2015-03-02 16:20:00 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2015-09-01 00:26:30 +03:00
|
|
|
// static
|
|
|
|
bool CacheStorage::DefineCaches(JSContext* aCx, JS::Handle<JSObject*> aGlobal) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2020-09-09 01:55:38 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(JS::GetClass(aGlobal)->flags & JSCLASS_DOM_GLOBAL,
|
2017-01-06 23:41:15 +03:00
|
|
|
"Passed object is not a global object!");
|
2016-07-09 07:19:52 +03:00
|
|
|
js::AssertSameCompartment(aCx, aGlobal);
|
2015-09-01 00:26:30 +03:00
|
|
|
|
2018-06-26 00:20:54 +03:00
|
|
|
if (NS_WARN_IF(!CacheStorage_Binding::GetConstructorObject(aCx) ||
|
|
|
|
!Cache_Binding::GetConstructorObject(aCx))) {
|
2015-09-01 00:26:30 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aGlobal);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(principal);
|
2015-09-01 00:26:30 +03:00
|
|
|
|
|
|
|
ErrorResult rv;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheStorage> storage =
|
2015-09-01 00:26:30 +03:00
|
|
|
CreateOnMainThread(DEFAULT_NAMESPACE, xpc::NativeGlobal(aGlobal),
|
|
|
|
principal, true, /* force trusted */
|
|
|
|
rv);
|
2015-11-21 00:29:40 +03:00
|
|
|
if (NS_WARN_IF(rv.MaybeSetPendingException(aCx))) {
|
|
|
|
return false;
|
2015-09-01 00:26:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> caches(aCx);
|
|
|
|
if (NS_WARN_IF(!ToJSValue(aCx, storage, &caches))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS_DefineProperty(aCx, aGlobal, "caches", caches, JSPROP_ENUMERATE);
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
CacheStorage::CacheStorage(Namespace aNamespace, nsIGlobalObject* aGlobal,
|
2016-06-23 11:53:14 +03:00
|
|
|
const PrincipalInfo& aPrincipalInfo,
|
2020-05-11 15:12:22 +03:00
|
|
|
SafeRefPtr<CacheWorkerRef> aWorkerRef)
|
2015-03-02 16:20:00 +03:00
|
|
|
: mNamespace(aNamespace),
|
|
|
|
mGlobal(aGlobal),
|
|
|
|
mPrincipalInfo(MakeUnique<PrincipalInfo>(aPrincipalInfo)),
|
|
|
|
mActor(nullptr),
|
2015-06-27 05:36:39 +03:00
|
|
|
mStatus(NS_OK) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mGlobal);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// If the PBackground actor is already initialized then we can
|
|
|
|
// immediately use it
|
2017-10-24 13:02:40 +03:00
|
|
|
PBackgroundChild* actor = BackgroundChild::GetOrCreateForCurrentThread();
|
|
|
|
if (NS_WARN_IF(!actor)) {
|
|
|
|
mStatus = NS_ERROR_UNEXPECTED;
|
2015-03-02 16:20:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:17:59 +03:00
|
|
|
// WorkerRef ownership is passed to the CacheStorageChild actor and any
|
|
|
|
// actors it may create. The WorkerRef will keep the worker thread alive
|
2017-10-24 13:02:40 +03:00
|
|
|
// until the actors can gracefully shutdown.
|
2020-05-11 15:12:22 +03:00
|
|
|
CacheStorageChild* newActor =
|
|
|
|
new CacheStorageChild(this, std::move(aWorkerRef));
|
2017-10-24 13:02:40 +03:00
|
|
|
PCacheStorageChild* constructedActor = actor->SendPCacheStorageConstructor(
|
|
|
|
newActor, mNamespace, *mPrincipalInfo);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!constructedActor)) {
|
|
|
|
mStatus = NS_ERROR_UNEXPECTED;
|
|
|
|
return;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
2017-10-24 13:02:40 +03:00
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(constructedActor == newActor);
|
|
|
|
mActor = newActor;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-06-27 05:36:39 +03:00
|
|
|
CacheStorage::CacheStorage(nsresult aFailureResult)
|
|
|
|
: mNamespace(INVALID_NAMESPACE), mActor(nullptr), mStatus(aFailureResult) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(NS_FAILED(mStatus));
|
2015-06-27 05:36:39 +03:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
already_AddRefed<Promise> CacheStorage::Match(
|
2017-08-11 04:04:55 +03:00
|
|
|
JSContext* aCx, const RequestOrUSVString& aRequest,
|
2020-09-17 16:02:46 +03:00
|
|
|
const MultiCacheQueryOptions& aOptions, ErrorResult& aRv) {
|
2015-03-02 16:20:00 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStorage);
|
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
if (!HasStorageAccess()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-27 05:36:39 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(mStatus))) {
|
|
|
|
aRv.Throw(mStatus);
|
2015-03-02 16:20:00 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-11 15:10:53 +03:00
|
|
|
SafeRefPtr<InternalRequest> request =
|
2017-08-11 04:04:55 +03:00
|
|
|
ToInternalRequest(aCx, aRequest, IgnoreBody, aRv);
|
2015-04-16 22:00:15 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
|
2015-05-01 18:15:52 +03:00
|
|
|
if (NS_WARN_IF(!promise)) {
|
2015-03-02 16:20:00 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
CacheQueryParams params;
|
|
|
|
ToCacheQueryParams(params, aOptions);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2020-04-04 00:04:57 +03:00
|
|
|
auto entry = MakeUnique<Entry>();
|
2015-04-16 22:00:15 +03:00
|
|
|
entry->mPromise = promise;
|
2017-09-15 22:25:41 +03:00
|
|
|
entry->mArgs = StorageMatchArgs(CacheRequest(), params, GetOpenMode());
|
2020-05-11 15:10:53 +03:00
|
|
|
entry->mRequest = std::move(request);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
RunRequest(std::move(entry));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise> CacheStorage::Has(const nsAString& aKey,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStorage);
|
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
if (!HasStorageAccess()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-27 05:36:39 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(mStatus))) {
|
|
|
|
aRv.Throw(mStatus);
|
2015-04-15 03:11:19 +03:00
|
|
|
return nullptr;
|
2015-04-15 01:27:48 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
|
2015-05-01 18:15:52 +03:00
|
|
|
if (NS_WARN_IF(!promise)) {
|
2015-04-16 22:00:15 +03:00
|
|
|
return nullptr;
|
2015-04-15 10:55:53 +03:00
|
|
|
}
|
|
|
|
|
2020-04-04 00:04:57 +03:00
|
|
|
auto entry = MakeUnique<Entry>();
|
2015-04-16 22:00:15 +03:00
|
|
|
entry->mPromise = promise;
|
|
|
|
entry->mArgs = StorageHasArgs(nsString(aKey));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
RunRequest(std::move(entry));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise> CacheStorage::Open(const nsAString& aKey,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStorage);
|
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
if (!HasStorageAccess()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-27 05:36:39 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(mStatus))) {
|
|
|
|
aRv.Throw(mStatus);
|
2015-04-15 03:11:19 +03:00
|
|
|
return nullptr;
|
2015-04-15 01:27:48 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
|
2015-05-01 18:15:52 +03:00
|
|
|
if (NS_WARN_IF(!promise)) {
|
2015-04-16 22:00:15 +03:00
|
|
|
return nullptr;
|
2015-04-15 10:55:53 +03:00
|
|
|
}
|
|
|
|
|
2020-04-04 00:04:57 +03:00
|
|
|
auto entry = MakeUnique<Entry>();
|
2015-04-16 22:00:15 +03:00
|
|
|
entry->mPromise = promise;
|
|
|
|
entry->mArgs = StorageOpenArgs(nsString(aKey));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
RunRequest(std::move(entry));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise> CacheStorage::Delete(const nsAString& aKey,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStorage);
|
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
if (!HasStorageAccess()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-27 05:36:39 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(mStatus))) {
|
|
|
|
aRv.Throw(mStatus);
|
2015-04-15 03:11:19 +03:00
|
|
|
return nullptr;
|
2015-04-15 01:27:48 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
|
2015-05-01 18:15:52 +03:00
|
|
|
if (NS_WARN_IF(!promise)) {
|
2015-04-16 22:00:15 +03:00
|
|
|
return nullptr;
|
2015-04-15 10:55:53 +03:00
|
|
|
}
|
|
|
|
|
2020-04-04 00:04:57 +03:00
|
|
|
auto entry = MakeUnique<Entry>();
|
2015-04-16 22:00:15 +03:00
|
|
|
entry->mPromise = promise;
|
|
|
|
entry->mArgs = StorageDeleteArgs(nsString(aKey));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
RunRequest(std::move(entry));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise> CacheStorage::Keys(ErrorResult& aRv) {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStorage);
|
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
if (!HasStorageAccess()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-06-27 05:36:39 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(mStatus))) {
|
|
|
|
aRv.Throw(mStatus);
|
2015-04-15 03:11:19 +03:00
|
|
|
return nullptr;
|
2015-04-15 01:27:48 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
|
2015-05-01 18:15:52 +03:00
|
|
|
if (NS_WARN_IF(!promise)) {
|
2015-04-16 22:00:15 +03:00
|
|
|
return nullptr;
|
2015-04-15 10:55:53 +03:00
|
|
|
}
|
|
|
|
|
2020-04-04 00:04:57 +03:00
|
|
|
auto entry = MakeUnique<Entry>();
|
2015-04-16 22:00:15 +03:00
|
|
|
entry->mPromise = promise;
|
|
|
|
entry->mArgs = StorageKeysArgs();
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
RunRequest(std::move(entry));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2015-05-06 01:45:36 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<CacheStorage> CacheStorage::Constructor(
|
|
|
|
const GlobalObject& aGlobal, CacheStorageNamespace aNamespace,
|
|
|
|
nsIPrincipal* aPrincipal, ErrorResult& aRv) {
|
|
|
|
if (NS_WARN_IF(!NS_IsMainThread())) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: remove Namespace in favor of CacheStorageNamespace
|
|
|
|
static_assert(DEFAULT_NAMESPACE == (uint32_t)CacheStorageNamespace::Content,
|
|
|
|
"Default namespace should match webidl Content enum");
|
|
|
|
static_assert(
|
|
|
|
CHROME_ONLY_NAMESPACE == (uint32_t)CacheStorageNamespace::Chrome,
|
|
|
|
"Chrome namespace should match webidl Chrome enum");
|
2019-10-23 06:54:39 +03:00
|
|
|
static_assert(NUMBER_OF_NAMESPACES == CacheStorageNamespaceValues::Count,
|
|
|
|
"Number of namespace should match webidl count");
|
2015-05-06 01:45:36 +03:00
|
|
|
|
|
|
|
Namespace ns = static_cast<Namespace>(aNamespace);
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
|
2015-06-27 05:36:40 +03:00
|
|
|
|
|
|
|
bool privateBrowsing = false;
|
2016-01-30 20:05:36 +03:00
|
|
|
if (nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global)) {
|
2019-01-02 16:05:23 +03:00
|
|
|
RefPtr<Document> doc = window->GetExtantDoc();
|
2015-06-27 05:36:40 +03:00
|
|
|
if (doc) {
|
|
|
|
nsCOMPtr<nsILoadContext> loadContext = doc->GetLoadContext();
|
|
|
|
privateBrowsing = loadContext && loadContext->UsePrivateBrowsing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
if (privateBrowsing) {
|
|
|
|
RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2015-06-28 06:19:24 +03:00
|
|
|
// Create a CacheStorage object bypassing the trusted origin checks
|
|
|
|
// since this is a chrome-only constructor.
|
2018-07-10 11:09:59 +03:00
|
|
|
return CreateOnMainThread(ns, global, aPrincipal,
|
2015-06-28 06:19:24 +03:00
|
|
|
true /* force trusted origin */, aRv);
|
2015-05-06 01:45:36 +03:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
nsISupports* CacheStorage::GetParentObject() const { return mGlobal; }
|
|
|
|
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
JSObject* CacheStorage::WrapObject(JSContext* aContext,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return mozilla::dom::CacheStorage_Binding::Wrap(aContext, this, aGivenProto);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheStorage::DestroyInternal(CacheStorageChild* aActor) {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStorage);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mActor);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
|
2017-10-24 13:02:40 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!NS_FAILED(mStatus));
|
2015-03-02 16:20:00 +03:00
|
|
|
mActor->ClearListener();
|
|
|
|
mActor = nullptr;
|
2017-10-24 13:02:40 +03:00
|
|
|
mStatus = NS_ERROR_UNEXPECTED;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// Note that we will never get an actor again in case another request is
|
|
|
|
// made before this object is destructed.
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIGlobalObject* CacheStorage::GetGlobalObject() const { return mGlobal; }
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void CacheStorage::AssertOwningThread() const {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStorage);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
PBackgroundChild* CacheStorage::GetIPCManager() {
|
2015-03-22 09:52:12 +03:00
|
|
|
// This is true because CacheStorage always uses IgnoreBody for requests.
|
2016-05-15 20:32:09 +03:00
|
|
|
// So we should never need to get the IPC manager during Request or
|
|
|
|
// Response serialization.
|
|
|
|
MOZ_CRASH("CacheStorage does not implement TypeUtils::GetIPCManager()");
|
2015-03-22 09:52:12 +03:00
|
|
|
}
|
|
|
|
|
2015-03-02 16:20:00 +03:00
|
|
|
CacheStorage::~CacheStorage() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStorage);
|
|
|
|
if (mActor) {
|
2015-05-01 18:13:36 +03:00
|
|
|
mActor->StartDestroyFromListener();
|
|
|
|
// DestroyInternal() is called synchronously by StartDestroyFromListener().
|
|
|
|
// So we should have already cleared the mActor.
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mActor);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 00:04:57 +03:00
|
|
|
void CacheStorage::RunRequest(UniquePtr<Entry> aEntry) {
|
2017-10-24 13:02:40 +03:00
|
|
|
MOZ_ASSERT(mActor);
|
|
|
|
|
2020-04-04 00:04:57 +03:00
|
|
|
AutoChildOpArgs args(this, aEntry->mArgs, 1);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2020-04-04 00:04:57 +03:00
|
|
|
if (aEntry->mRequest) {
|
2015-04-16 22:00:15 +03:00
|
|
|
ErrorResult rv;
|
2020-05-11 15:10:53 +03:00
|
|
|
args.Add(*aEntry->mRequest, IgnoreBody, IgnoreInvalidScheme, rv);
|
2015-05-01 18:15:52 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2020-04-04 00:04:57 +03:00
|
|
|
aEntry->mPromise->MaybeReject(std::move(rv));
|
2017-10-24 13:02:40 +03:00
|
|
|
return;
|
2015-04-15 10:55:53 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 13:02:40 +03:00
|
|
|
|
2020-04-04 00:04:57 +03:00
|
|
|
mActor->ExecuteOp(mGlobal, aEntry->mPromise, this, args.SendAsOpArgs());
|
2015-04-15 10:55:53 +03:00
|
|
|
}
|
|
|
|
|
2017-09-15 22:25:41 +03:00
|
|
|
OpenMode CacheStorage::GetOpenMode() const {
|
|
|
|
return mNamespace == CHROME_ONLY_NAMESPACE ? OpenMode::Eager : OpenMode::Lazy;
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
bool CacheStorage::HasStorageAccess() const {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheStorage);
|
|
|
|
|
2019-05-27 17:06:49 +03:00
|
|
|
StorageAccess access;
|
2019-04-12 08:31:40 +03:00
|
|
|
|
2018-07-10 11:09:59 +03:00
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mGlobal);
|
|
|
|
if (NS_WARN_IF(!window)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-27 17:06:49 +03:00
|
|
|
access = StorageAllowedForWindow(window);
|
2019-04-12 08:31:40 +03:00
|
|
|
} else {
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
2018-07-10 11:09:59 +03:00
|
|
|
|
2019-04-12 08:31:40 +03:00
|
|
|
access = workerPrivate->StorageAccess();
|
|
|
|
}
|
2019-04-11 22:48:49 +03:00
|
|
|
|
2019-05-27 17:06:49 +03:00
|
|
|
return access > StorageAccess::ePrivateBrowsing;
|
2018-07-10 11:09:59 +03:00
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom::cache
|