1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
/* -*- 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 "BoundStorageKey.h"
#include "BoundStorageKeyCache.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/cache/AutoUtils.h"
#include "mozilla/dom/cache/Cache.h"
#include "mozilla/dom/cache/CacheStorageChild.h"
#include "mozilla/dom/cache/CacheWorkerRef.h"
#include "mozilla/dom/quota/PrincipalUtils.h"
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/PBackgroundChild.h"
#include "mozilla/ipc/PBackgroundSharedTypes.h"
namespace mozilla::dom::cache {
using mozilla::ipc::BackgroundChild;
using mozilla::ipc::PrincipalInfo;
NS_IMPL_ISUPPORTS(BoundStorageKey, nsISupports)
template <typename PromiseType>
struct BoundStorageKeyCacheStorage::Entry final {
RefPtr<PromiseType> mPromise;
CacheOpArgs mArgs;
};
nsresult BoundStorageKey::Init(Namespace aNamespace,
const PrincipalInfo& aPrincipalInfo,
nsISerialEventTarget* aTarget) {
MOZ_DIAGNOSTIC_ASSERT(aTarget);
// setup child and parent actors and retarget to aTarget
auto* actor = new BoundStorageKeyChild(this);
MOZ_ASSERT(actor);
{
::mozilla::ipc::Endpoint<PBoundStorageKeyChild> childEP;
::mozilla::ipc::Endpoint<PBoundStorageKeyParent> parentEP;
auto rv = PBoundStorageKey::CreateEndpoints(&parentEP, &childEP);
Unused << NS_WARN_IF(NS_FAILED(rv));
PBackgroundChild* bgActor = BackgroundChild::GetOrCreateForCurrentThread();
if (NS_WARN_IF(!bgActor)) {
NS_WARNING("BoundStorageKey failed to obtain bgActor");
return NS_ERROR_UNEXPECTED;
}
bgActor->SendCreateBoundStorageKeyParent(std::move(parentEP), aNamespace,
aPrincipalInfo);
if (NS_WARN_IF(!(childEP.Bind(actor, aTarget)))) {
NS_WARNING("BoundStorageKeyChildActor failed to bind to target.");
return NS_ERROR_UNEXPECTED;
}
}
mActor = actor;
return NS_OK;
}
void BoundStorageKey::OnActorDestroy(BoundStorageKeyChild* aActor) {
MOZ_DIAGNOSTIC_ASSERT(mActor);
MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
MOZ_DIAGNOSTIC_ASSERT(!NS_FAILED(mStatus));
// Note that we will never get an actor again in case another request is
// made before this object is destructed.
mActor->ClearListener();
mActor = nullptr;
mStatus = NS_ERROR_UNEXPECTED;
}
BoundStorageKey::~BoundStorageKey() {
if (mActor) {
mActor->StartDestroyFromListener();
}
MOZ_DIAGNOSTIC_ASSERT(!mActor);
}
// static
already_AddRefed<BoundStorageKeyCacheStorage>
BoundStorageKeyCacheStorage::Create(Namespace aNamespace,
nsIGlobalObject* aGlobal,
WorkerPrivate* aWorkerPrivate,
nsISerialEventTarget* aActorTarget,
ErrorResult& aRv) {
MOZ_DIAGNOSTIC_ASSERT(aWorkerPrivate);
if (aWorkerPrivate->GetOriginAttributes().IsPrivateBrowsing() &&
!StaticPrefs::dom_cache_privateBrowsing_enabled()) {
NS_WARNING("BoundStorageKey not supported during private browsing.");
aRv = NS_ERROR_DOM_SECURITY_ERR;
return nullptr;
}
const PrincipalInfo& principalInfo =
aWorkerPrivate->GetEffectiveStoragePrincipalInfo();
QM_TRY(OkIf(quota::IsPrincipalInfoValid(principalInfo)), nullptr,
[&aRv](const auto) { aRv = NS_ERROR_FAILURE; });
// 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.
bool testingEnabled = StaticPrefs::dom_caches_testing_enabled() ||
StaticPrefs::dom_serviceWorkers_testing_enabled() ||
aWorkerPrivate->ServiceWorkersTestingInWindow() ||
aWorkerPrivate->IsServiceWorker();
if (!IsTrusted(principalInfo, testingEnabled)) {
NS_WARNING("BoundStorageKey not supported on untrusted origins.");
aRv = NS_ERROR_UNEXPECTED;
return nullptr;
}
RefPtr<BoundStorageKeyCacheStorage> ref =
new BoundStorageKeyCacheStorage(aNamespace, aGlobal, principalInfo);
auto rv = ref->Init(aWorkerPrivate, aNamespace, principalInfo, aActorTarget);
if (NS_WARN_IF(NS_FAILED(rv))) {
aRv = rv;
return nullptr;
}
MOZ_ASSERT(ref->mActor);
return ref.forget();
}
nsresult BoundStorageKeyCacheStorage::Init(WorkerPrivate* aWorkerPrivate,
Namespace aNamespace,
const PrincipalInfo& aPrincipalInfo,
nsISerialEventTarget* aTarget) {
auto rc = BoundStorageKey::Init(aNamespace, aPrincipalInfo, aTarget);
if (NS_FAILED(rc)) {
return rc;
}
mCacheStorageChild = CreateCacheStorageChild(aWorkerPrivate);
MOZ_ASSERT(mCacheStorageChild);
return mCacheStorageChild ? NS_OK : NS_ERROR_FAILURE;
}
BoundStorageKeyCacheStorage::BoundStorageKeyCacheStorage(
Namespace aNamespace, nsIGlobalObject* aGlobal,
const mozilla::ipc::PrincipalInfo& aPrincipalInfo)
: mGlobal(aGlobal),
mPrincipalInfo(MakeUnique<PrincipalInfo>(aPrincipalInfo)),
mNamespace(aNamespace) {}
void BoundStorageKeyCacheStorage::OnActorDestroy(CacheStorageChild* aActor) {
AssertOwningThread();
MOZ_DIAGNOSTIC_ASSERT(mActor);
MOZ_DIAGNOSTIC_ASSERT(mCacheStorageChild.get() == aActor);
mCacheStorageChild->ClearListener();
mCacheStorageChild = nullptr;
}
BoundStorageKeyCacheStorage::~BoundStorageKeyCacheStorage() {
AssertOwningThread();
if (mCacheStorageChild) {
mCacheStorageChild->StartDestroyFromListener();
}
}
already_AddRefed<CacheStorageChild>
BoundStorageKeyCacheStorage::CreateCacheStorageChild(
WorkerPrivate* aWorkerPrivate) {
SafeRefPtr<CacheWorkerRef> workerRef;
if (aWorkerPrivate) {
aWorkerPrivate->AssertIsOnWorkerThread();
workerRef =
CacheWorkerRef::Create(aWorkerPrivate, CacheWorkerRef::eIPCWorkerRef);
if (NS_WARN_IF(!workerRef)) {
return nullptr;
}
}
RefPtr<CacheStorageChild> newActor =
new CacheStorageChild(this, std::move(workerRef), mActor.get());
auto* constructedActor = mActor->SendPCacheStorageConstructor(
newActor, mNamespace, *mPrincipalInfo);
if (NS_WARN_IF(!constructedActor)) {
mStatus = NS_ERROR_UNEXPECTED;
return nullptr;
}
MOZ_DIAGNOSTIC_ASSERT(newActor == constructedActor);
return newActor.forget();
}
template <typename EntryType>
void BoundStorageKeyCacheStorage::RunRequest(EntryType&& aEntry) {
MOZ_ASSERT(mActor);
MOZ_ASSERT(mCacheStorageChild);
AutoChildOpArgs args(this, aEntry.mArgs, 1);
RefPtr<CacheStoragePromise> p{aEntry.mPromise.forget()};
mCacheStorageChild->ExecuteOp(mGlobal, p, this, args.SendAsOpArgs());
}
auto BoundStorageKeyCacheStorage::Open(const nsAString& aKey, ErrorResult& aRv)
-> already_AddRefed<CacheStoragePromise> {
AssertOwningThread();
if (NS_WARN_IF(NS_FAILED(mStatus))) {
aRv = mStatus;
return nullptr;
}
RefPtr<OpenResultPromise::Private> promise{
new OpenResultPromise::Private(__func__)};
RunRequest(Entry<OpenResultPromise::Private>{
promise, StorageOpenArgs{nsString(aKey)}});
return promise.forget();
}
auto BoundStorageKeyCacheStorage::Has(const nsAString& aKey, ErrorResult& aRv)
-> already_AddRefed<CacheStoragePromise> {
AssertOwningThread();
if (NS_WARN_IF(NS_FAILED(mStatus))) {
aRv = mStatus;
return nullptr;
}
RefPtr<HasResultPromise::Private> promise{
new HasResultPromise::Private(__func__)};
RunRequest(Entry<HasResultPromise::Private>{promise,
StorageHasArgs(nsString(aKey))});
return promise.forget();
}
auto BoundStorageKeyCacheStorage::Delete(const nsAString& aKey,
ErrorResult& aRv)
-> already_AddRefed<CacheStoragePromise> {
AssertOwningThread();
if (NS_WARN_IF(NS_FAILED(mStatus))) {
aRv = mStatus;
return nullptr;
}
RefPtr<DeleteResultPromise::Private> promise{
new DeleteResultPromise::Private(__func__)};
RunRequest(Entry<DeleteResultPromise::Private>{
promise, StorageDeleteArgs{nsString(aKey)}});
return promise.forget();
}
auto BoundStorageKeyCacheStorage::Keys(ErrorResult& aRv)
-> already_AddRefed<CacheStoragePromise> {
AssertOwningThread();
if (NS_WARN_IF(NS_FAILED(mStatus))) {
aRv = mStatus;
return nullptr;
}
RefPtr<KeysResultPromise::Private> promise{
new KeysResultPromise::Private(__func__)};
RunRequest(Entry<KeysResultPromise::Private>{promise, StorageKeysArgs()});
return promise.forget();
}
} // namespace mozilla::dom::cache
|