summaryrefslogtreecommitdiffstats
path: root/dom/cache/CacheStorage.cpp
blob: 75ffc803e0decf838c3c4d2d2b647c370d6f5d95 (plain)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
/* -*- 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"

#include "mozilla/Preferences.h"
#include "mozilla/Unused.h"
#include "mozilla/dom/CacheBinding.h"
#include "mozilla/dom/CacheStorageBinding.h"
#include "mozilla/dom/InternalRequest.h"
#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/CacheCommon.h"
#include "mozilla/dom/cache/CacheStorageChild.h"
#include "mozilla/dom/cache/CacheWorkerRef.h"
#include "mozilla/dom/cache/PCacheChild.h"
#include "mozilla/dom/cache/ReadStream.h"
#include "mozilla/dom/cache/TypeUtils.h"
#include "mozilla/dom/quota/QuotaManager.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/ipc/BackgroundChild.h"
#include "mozilla/ipc/BackgroundUtils.h"
#include "mozilla/ipc/PBackgroundChild.h"
#include "mozilla/ipc/PBackgroundSharedTypes.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/StaticPrefs_extensions.h"
#include "nsContentUtils.h"
#include "mozilla/dom/Document.h"
#include "nsIGlobalObject.h"
#include "nsMixedContentBlocker.h"
#include "nsURLParsers.h"
#include "js/Object.h"  // JS::GetClass

namespace mozilla::dom::cache {

using mozilla::ErrorResult;
using mozilla::Unused;
using mozilla::dom::quota::QuotaManager;
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);
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(mozilla::dom::cache::CacheStorage,
                                      mGlobal);

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CacheStorage)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

// We cannot reference IPC types in a webidl binding implementation header.  So
// define this in the .cpp.
struct CacheStorage::Entry final {
  RefPtr<Promise> mPromise;
  CacheOpArgs mArgs;
  // We cannot add the requests until after the actor is present.  So store
  // the request data separately for now.
  SafeRefPtr<InternalRequest> mRequest;
};

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.
  CACHE_TRY(OkIf(aPrincipalInfo.type() == PrincipalInfo::TContentPrincipalInfo),
            false);

  // If we're in testing mode, then don't do any more work to determine 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* const url = flatURL.get();

  // off the main thread URL parsing using nsStdURLParser.
  const nsCOMPtr<nsIURLParser> urlParser = new nsStdURLParser();

  uint32_t schemePos;
  int32_t schemeLen;
  uint32_t authPos;
  int32_t authLen;
  CACHE_TRY(
      urlParser->ParseURL(url, flatURL.Length(), &schemePos, &schemeLen,
                          &authPos, &authLen, nullptr, nullptr),  // ignore path
      false);

  const nsAutoCString scheme(Substring(flatURL, schemePos, schemeLen));
  if (scheme.LowerCaseEqualsLiteral("https") ||
      scheme.LowerCaseEqualsLiteral("file")) {
    return true;
  }

  if (StaticPrefs::extensions_backgroundServiceWorker_enabled_AtStartup() &&
      scheme.LowerCaseEqualsLiteral("moz-extension")) {
    return true;
  }

  uint32_t hostPos;
  int32_t hostLen;
  CACHE_TRY(urlParser->ParseAuthority(url + authPos, authLen, nullptr,
                                      nullptr,           // ignore username
                                      nullptr, nullptr,  // ignore password
                                      &hostPos, &hostLen,
                                      nullptr),  // ignore port
            false);

  return nsMixedContentBlocker::IsPotentiallyTrustworthyLoopbackHost(
      nsDependentCSubstring(url + authPos + hostPos, hostLen));
}

}  // namespace

// static
already_AddRefed<CacheStorage> CacheStorage::CreateOnMainThread(
    Namespace aNamespace, nsIGlobalObject* aGlobal, nsIPrincipal* aPrincipal,
    bool aForceTrustedOrigin, ErrorResult& aRv) {
  MOZ_DIAGNOSTIC_ASSERT(aGlobal);
  MOZ_DIAGNOSTIC_ASSERT(aPrincipal);
  MOZ_ASSERT(NS_IsMainThread());

  PrincipalInfo principalInfo;
  CACHE_TRY(PrincipalToPrincipalInfo(aPrincipal, &principalInfo), nullptr,
            [&aRv](const nsresult rv) { aRv.Throw(rv); });

  CACHE_TRY(OkIf(QuotaManager::IsPrincipalInfoValid(principalInfo)),
            RefPtr{new CacheStorage(NS_ERROR_DOM_SECURITY_ERR)}.forget(),
            [](const auto) {
              NS_WARNING("CacheStorage not supported on invalid origins.");
            });

  const bool testingEnabled =
      aForceTrustedOrigin ||
      Preferences::GetBool("dom.caches.testing.enabled", false) ||
      StaticPrefs::dom_serviceWorkers_testing_enabled();

  if (!IsTrusted(principalInfo, testingEnabled)) {
    NS_WARNING("CacheStorage not supported on untrusted origins.");
    RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
    return ref.forget();
  }

  RefPtr<CacheStorage> ref =
      new CacheStorage(aNamespace, aGlobal, principalInfo, nullptr);
  return ref.forget();
}

// static
already_AddRefed<CacheStorage> CacheStorage::CreateOnWorker(
    Namespace aNamespace, nsIGlobalObject* aGlobal,
    WorkerPrivate* aWorkerPrivate, ErrorResult& aRv) {
  MOZ_DIAGNOSTIC_ASSERT(aGlobal);
  MOZ_DIAGNOSTIC_ASSERT(aWorkerPrivate);
  aWorkerPrivate->AssertIsOnWorkerThread();

  if (aWorkerPrivate->GetOriginAttributes().mPrivateBrowsingId > 0) {
    NS_WARNING("CacheStorage not supported during private browsing.");
    RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
    return ref.forget();
  }

  SafeRefPtr<CacheWorkerRef> workerRef =
      CacheWorkerRef::Create(aWorkerPrivate, CacheWorkerRef::eIPCWorkerRef);
  if (!workerRef) {
    NS_WARNING("Worker thread is shutting down.");
    aRv.Throw(NS_ERROR_FAILURE);
    return nullptr;
  }

  const PrincipalInfo& principalInfo =
      aWorkerPrivate->GetEffectiveStoragePrincipalInfo();

  CACHE_TRY(OkIf(QuotaManager::IsPrincipalInfoValid(principalInfo)), nullptr,
            [&aRv](const auto) { aRv.Throw(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("CacheStorage not supported on untrusted origins.");
    RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
    return ref.forget();
  }

  RefPtr<CacheStorage> ref = new CacheStorage(
      aNamespace, aGlobal, principalInfo, std::move(workerRef));
  return ref.forget();
}

// static
bool CacheStorage::DefineCaches(JSContext* aCx, JS::Handle<JSObject*> aGlobal) {
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_DIAGNOSTIC_ASSERT(JS::GetClass(aGlobal)->flags & JSCLASS_DOM_GLOBAL,
                        "Passed object is not a global object!");
  js::AssertSameCompartment(aCx, aGlobal);

  if (NS_WARN_IF(!CacheStorage_Binding::GetConstructorObject(aCx) ||
                 !Cache_Binding::GetConstructorObject(aCx))) {
    return false;
  }

  nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aGlobal);
  MOZ_DIAGNOSTIC_ASSERT(principal);

  ErrorResult rv;
  RefPtr<CacheStorage> storage =
      CreateOnMainThread(DEFAULT_NAMESPACE, xpc::NativeGlobal(aGlobal),
                         principal, true, /* force trusted */
                         rv);
  if (NS_WARN_IF(rv.MaybeSetPendingException(aCx))) {
    return false;
  }

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

CacheStorage::CacheStorage(Namespace aNamespace, nsIGlobalObject* aGlobal,
                           const PrincipalInfo& aPrincipalInfo,
                           SafeRefPtr<CacheWorkerRef> aWorkerRef)
    : mNamespace(aNamespace),
      mGlobal(aGlobal),
      mPrincipalInfo(MakeUnique<PrincipalInfo>(aPrincipalInfo)),
      mActor(nullptr),
      mStatus(NS_OK) {
  MOZ_DIAGNOSTIC_ASSERT(mGlobal);

  // If the PBackground actor is already initialized then we can
  // immediately use it
  PBackgroundChild* actor = BackgroundChild::GetOrCreateForCurrentThread();
  if (NS_WARN_IF(!actor)) {
    mStatus = NS_ERROR_UNEXPECTED;
    return;
  }

  // WorkerRef ownership is passed to the CacheStorageChild actor and any
  // actors it may create.  The WorkerRef will keep the worker thread alive
  // until the actors can gracefully shutdown.
  CacheStorageChild* newActor =
      new CacheStorageChild(this, std::move(aWorkerRef));
  PCacheStorageChild* constructedActor = actor->SendPCacheStorageConstructor(
      newActor, mNamespace, *mPrincipalInfo);

  if (NS_WARN_IF(!constructedActor)) {
    mStatus = NS_ERROR_UNEXPECTED;
    return;
  }

  MOZ_DIAGNOSTIC_ASSERT(constructedActor == newActor);
  mActor = newActor;
}

CacheStorage::CacheStorage(nsresult aFailureResult)
    : mNamespace(INVALID_NAMESPACE), mActor(nullptr), mStatus(aFailureResult) {
  MOZ_DIAGNOSTIC_ASSERT(NS_FAILED(mStatus));
}

already_AddRefed<Promise> CacheStorage::Match(
    JSContext* aCx, const RequestOrUSVString& aRequest,
    const MultiCacheQueryOptions& aOptions, ErrorResult& aRv) {
  NS_ASSERT_OWNINGTHREAD(CacheStorage);

  if (!HasStorageAccess()) {
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
    return nullptr;
  }

  if (NS_WARN_IF(NS_FAILED(mStatus))) {
    aRv.Throw(mStatus);
    return nullptr;
  }

  SafeRefPtr<InternalRequest> request =
      ToInternalRequest(aCx, aRequest, IgnoreBody, aRv);
  if (NS_WARN_IF(aRv.Failed())) {
    return nullptr;
  }

  RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
  if (NS_WARN_IF(!promise)) {
    return nullptr;
  }

  CacheQueryParams params;
  ToCacheQueryParams(params, aOptions);

  auto entry = MakeUnique<Entry>();
  entry->mPromise = promise;
  entry->mArgs = StorageMatchArgs(CacheRequest(), params, GetOpenMode());
  entry->mRequest = std::move(request);

  RunRequest(std::move(entry));

  return promise.forget();
}

already_AddRefed<Promise> CacheStorage::Has(const nsAString& aKey,
                                            ErrorResult& aRv) {
  NS_ASSERT_OWNINGTHREAD(CacheStorage);

  if (!HasStorageAccess()) {
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
    return nullptr;
  }

  if (NS_WARN_IF(NS_FAILED(mStatus))) {
    aRv.Throw(mStatus);
    return nullptr;
  }

  RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
  if (NS_WARN_IF(!promise)) {
    return nullptr;
  }

  auto entry = MakeUnique<Entry>();
  entry->mPromise = promise;
  entry->mArgs = StorageHasArgs(nsString(aKey));

  RunRequest(std::move(entry));

  return promise.forget();
}

already_AddRefed<Promise> CacheStorage::Open(const nsAString& aKey,
                                             ErrorResult& aRv) {
  NS_ASSERT_OWNINGTHREAD(CacheStorage);

  if (!HasStorageAccess()) {
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
    return nullptr;
  }

  if (NS_WARN_IF(NS_FAILED(mStatus))) {
    aRv.Throw(mStatus);
    return nullptr;
  }

  RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
  if (NS_WARN_IF(!promise)) {
    return nullptr;
  }

  auto entry = MakeUnique<Entry>();
  entry->mPromise = promise;
  entry->mArgs = StorageOpenArgs(nsString(aKey));

  RunRequest(std::move(entry));

  return promise.forget();
}

already_AddRefed<Promise> CacheStorage::Delete(const nsAString& aKey,
                                               ErrorResult& aRv) {
  NS_ASSERT_OWNINGTHREAD(CacheStorage);

  if (!HasStorageAccess()) {
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
    return nullptr;
  }

  if (NS_WARN_IF(NS_FAILED(mStatus))) {
    aRv.Throw(mStatus);
    return nullptr;
  }

  RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
  if (NS_WARN_IF(!promise)) {
    return nullptr;
  }

  auto entry = MakeUnique<Entry>();
  entry->mPromise = promise;
  entry->mArgs = StorageDeleteArgs(nsString(aKey));

  RunRequest(std::move(entry));

  return promise.forget();
}

already_AddRefed<Promise> CacheStorage::Keys(ErrorResult& aRv) {
  NS_ASSERT_OWNINGTHREAD(CacheStorage);

  if (!HasStorageAccess()) {
    aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
    return nullptr;
  }

  if (NS_WARN_IF(NS_FAILED(mStatus))) {
    aRv.Throw(mStatus);
    return nullptr;
  }

  RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
  if (NS_WARN_IF(!promise)) {
    return nullptr;
  }

  auto entry = MakeUnique<Entry>();
  entry->mPromise = promise;
  entry->mArgs = StorageKeysArgs();

  RunRequest(std::move(entry));

  return promise.forget();
}

// 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");
  static_assert(NUMBER_OF_NAMESPACES == CacheStorageNamespaceValues::Count,
                "Number of namespace should match webidl count");

  Namespace ns = static_cast<Namespace>(aNamespace);
  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());

  bool privateBrowsing = false;
  if (nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global)) {
    RefPtr<Document> doc = window->GetExtantDoc();
    if (doc) {
      nsCOMPtr<nsILoadContext> loadContext = doc->GetLoadContext();
      privateBrowsing = loadContext && loadContext->UsePrivateBrowsing();
    }
  }

  if (privateBrowsing) {
    RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
    return ref.forget();
  }

  // Create a CacheStorage object bypassing the trusted origin checks
  // since this is a chrome-only constructor.
  return CreateOnMainThread(ns, global, aPrincipal,
                            true /* force trusted origin */, aRv);
}

nsISupports* CacheStorage::GetParentObject() const { return mGlobal; }

JSObject* CacheStorage::WrapObject(JSContext* aContext,
                                   JS::Handle<JSObject*> aGivenProto) {
  return mozilla::dom::CacheStorage_Binding::Wrap(aContext, this, aGivenProto);
}

void CacheStorage::DestroyInternal(CacheStorageChild* aActor) {
  NS_ASSERT_OWNINGTHREAD(CacheStorage);
  MOZ_DIAGNOSTIC_ASSERT(mActor);
  MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
  MOZ_DIAGNOSTIC_ASSERT(!NS_FAILED(mStatus));
  mActor->ClearListener();
  mActor = nullptr;
  mStatus = NS_ERROR_UNEXPECTED;

  // 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

PBackgroundChild* CacheStorage::GetIPCManager() {
  // This is true because CacheStorage always uses IgnoreBody for requests.
  // So we should never need to get the IPC manager during Request or
  // Response serialization.
  MOZ_CRASH("CacheStorage does not implement TypeUtils::GetIPCManager()");
}

CacheStorage::~CacheStorage() {
  NS_ASSERT_OWNINGTHREAD(CacheStorage);
  if (mActor) {
    mActor->StartDestroyFromListener();
    // DestroyInternal() is called synchronously by StartDestroyFromListener().
    // So we should have already cleared the mActor.
    MOZ_DIAGNOSTIC_ASSERT(!mActor);
  }
}

void CacheStorage::RunRequest(UniquePtr<Entry> aEntry) {
  MOZ_ASSERT(mActor);

  AutoChildOpArgs args(this, aEntry->mArgs, 1);

  if (aEntry->mRequest) {
    ErrorResult rv;
    args.Add(*aEntry->mRequest, IgnoreBody, IgnoreInvalidScheme, rv);
    if (NS_WARN_IF(rv.Failed())) {
      aEntry->mPromise->MaybeReject(std::move(rv));
      return;
    }
  }

  mActor->ExecuteOp(mGlobal, aEntry->mPromise, this, args.SendAsOpArgs());
}

OpenMode CacheStorage::GetOpenMode() const {
  return mNamespace == CHROME_ONLY_NAMESPACE ? OpenMode::Eager : OpenMode::Lazy;
}

bool CacheStorage::HasStorageAccess() const {
  NS_ASSERT_OWNINGTHREAD(CacheStorage);

  StorageAccess access;

  if (NS_IsMainThread()) {
    nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mGlobal);
    if (NS_WARN_IF(!window)) {
      return true;
    }

    access = StorageAllowedForWindow(window);
  } else {
    WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
    MOZ_ASSERT(workerPrivate);

    access = workerPrivate->StorageAccess();
  }

  return access > StorageAccess::ePrivateBrowsing;
}

}  // namespace mozilla::dom::cache