1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "mozilla/dom/cache/CacheStorage.h"
8 
9 #include "mozilla/Preferences.h"
10 #include "mozilla/Unused.h"
11 #include "mozilla/dom/CacheBinding.h"
12 #include "mozilla/dom/CacheStorageBinding.h"
13 #include "mozilla/dom/InternalRequest.h"
14 #include "mozilla/dom/Promise.h"
15 #include "mozilla/dom/Response.h"
16 #include "mozilla/dom/cache/AutoUtils.h"
17 #include "mozilla/dom/cache/Cache.h"
18 #include "mozilla/dom/cache/CacheChild.h"
19 #include "mozilla/dom/cache/CacheCommon.h"
20 #include "mozilla/dom/cache/CacheStorageChild.h"
21 #include "mozilla/dom/cache/CacheWorkerRef.h"
22 #include "mozilla/dom/cache/PCacheChild.h"
23 #include "mozilla/dom/cache/ReadStream.h"
24 #include "mozilla/dom/cache/TypeUtils.h"
25 #include "mozilla/dom/quota/QuotaManager.h"
26 #include "mozilla/dom/WorkerPrivate.h"
27 #include "mozilla/ipc/BackgroundChild.h"
28 #include "mozilla/ipc/BackgroundUtils.h"
29 #include "mozilla/ipc/PBackgroundChild.h"
30 #include "mozilla/ipc/PBackgroundSharedTypes.h"
31 #include "mozilla/StaticPrefs_dom.h"
32 #include "mozilla/StaticPrefs_extensions.h"
33 #include "nsContentUtils.h"
34 #include "mozilla/dom/Document.h"
35 #include "nsIGlobalObject.h"
36 #include "nsMixedContentBlocker.h"
37 #include "nsURLParsers.h"
38 #include "js/Object.h"  // JS::GetClass
39 
40 namespace mozilla::dom::cache {
41 
42 using mozilla::ErrorResult;
43 using mozilla::Unused;
44 using mozilla::dom::quota::QuotaManager;
45 using mozilla::ipc::BackgroundChild;
46 using mozilla::ipc::IProtocol;
47 using mozilla::ipc::PBackgroundChild;
48 using mozilla::ipc::PrincipalInfo;
49 using mozilla::ipc::PrincipalToPrincipalInfo;
50 
51 NS_IMPL_CYCLE_COLLECTING_ADDREF(mozilla::dom::cache::CacheStorage);
52 NS_IMPL_CYCLE_COLLECTING_RELEASE(mozilla::dom::cache::CacheStorage);
53 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(mozilla::dom::cache::CacheStorage,
54                                       mGlobal);
55 
56 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CacheStorage)
57   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
58   NS_INTERFACE_MAP_ENTRY(nsISupports)
59 NS_INTERFACE_MAP_END
60 
61 // We cannot reference IPC types in a webidl binding implementation header.  So
62 // define this in the .cpp.
63 struct CacheStorage::Entry final {
64   RefPtr<Promise> mPromise;
65   CacheOpArgs mArgs;
66   // We cannot add the requests until after the actor is present.  So store
67   // the request data separately for now.
68   SafeRefPtr<InternalRequest> mRequest;
69 };
70 
71 namespace {
72 
IsTrusted(const PrincipalInfo & aPrincipalInfo,bool aTestingPrefEnabled)73 bool IsTrusted(const PrincipalInfo& aPrincipalInfo, bool aTestingPrefEnabled) {
74   // Can happen on main thread or worker thread
75 
76   if (aPrincipalInfo.type() == PrincipalInfo::TSystemPrincipalInfo) {
77     return true;
78   }
79 
80   // Require a ContentPrincipal to avoid null principal, etc.
81   QM_TRY(OkIf(aPrincipalInfo.type() == PrincipalInfo::TContentPrincipalInfo),
82          false);
83 
84   // If we're in testing mode, then don't do any more work to determine if
85   // the origin is trusted.  We have to run some tests as http.
86   if (aTestingPrefEnabled) {
87     return true;
88   }
89 
90   // Now parse the scheme of the principal's origin.  This is a short term
91   // method for determining "trust".  In the long term we need to implement
92   // the full algorithm here:
93   //
94   // https://w3c.github.io/webappsec/specs/powerfulfeatures/#settings-secure
95   //
96   // TODO: Implement full secure setting algorithm. (bug 1177856)
97 
98   const nsCString& flatURL = aPrincipalInfo.get_ContentPrincipalInfo().spec();
99   const char* const url = flatURL.get();
100 
101   // off the main thread URL parsing using nsStdURLParser.
102   const nsCOMPtr<nsIURLParser> urlParser = new nsStdURLParser();
103 
104   uint32_t schemePos;
105   int32_t schemeLen;
106   uint32_t authPos;
107   int32_t authLen;
108   QM_TRY(
109       urlParser->ParseURL(url, flatURL.Length(), &schemePos, &schemeLen,
110                           &authPos, &authLen, nullptr, nullptr),  // ignore path
111       false);
112 
113   const nsAutoCString scheme(Substring(flatURL, schemePos, schemeLen));
114   if (scheme.LowerCaseEqualsLiteral("https") ||
115       scheme.LowerCaseEqualsLiteral("file") ||
116       scheme.LowerCaseEqualsLiteral("moz-extension")) {
117     return true;
118   }
119 
120   uint32_t hostPos;
121   int32_t hostLen;
122   QM_TRY(urlParser->ParseAuthority(url + authPos, authLen, nullptr,
123                                    nullptr,           // ignore username
124                                    nullptr, nullptr,  // ignore password
125                                    &hostPos, &hostLen,
126                                    nullptr),  // ignore port
127          false);
128 
129   return nsMixedContentBlocker::IsPotentiallyTrustworthyLoopbackHost(
130       nsDependentCSubstring(url + authPos + hostPos, hostLen));
131 }
132 
133 }  // namespace
134 
135 // static
CreateOnMainThread(Namespace aNamespace,nsIGlobalObject * aGlobal,nsIPrincipal * aPrincipal,bool aForceTrustedOrigin,ErrorResult & aRv)136 already_AddRefed<CacheStorage> CacheStorage::CreateOnMainThread(
137     Namespace aNamespace, nsIGlobalObject* aGlobal, nsIPrincipal* aPrincipal,
138     bool aForceTrustedOrigin, ErrorResult& aRv) {
139   MOZ_DIAGNOSTIC_ASSERT(aGlobal);
140   MOZ_DIAGNOSTIC_ASSERT(aPrincipal);
141   MOZ_ASSERT(NS_IsMainThread());
142 
143   PrincipalInfo principalInfo;
144   QM_TRY(PrincipalToPrincipalInfo(aPrincipal, &principalInfo), nullptr,
145          [&aRv](const nsresult rv) { aRv.Throw(rv); });
146 
147   QM_TRY(OkIf(QuotaManager::IsPrincipalInfoValid(principalInfo)),
148          RefPtr{new CacheStorage(NS_ERROR_DOM_SECURITY_ERR)}.forget(),
149          [](const auto) {
150            NS_WARNING("CacheStorage not supported on invalid origins.");
151          });
152 
153   const bool testingEnabled =
154       aForceTrustedOrigin ||
155       Preferences::GetBool("dom.caches.testing.enabled", false) ||
156       StaticPrefs::dom_serviceWorkers_testing_enabled();
157 
158   if (!IsTrusted(principalInfo, testingEnabled)) {
159     NS_WARNING("CacheStorage not supported on untrusted origins.");
160     RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
161     return ref.forget();
162   }
163 
164   RefPtr<CacheStorage> ref =
165       new CacheStorage(aNamespace, aGlobal, principalInfo, nullptr);
166   return ref.forget();
167 }
168 
169 // static
CreateOnWorker(Namespace aNamespace,nsIGlobalObject * aGlobal,WorkerPrivate * aWorkerPrivate,ErrorResult & aRv)170 already_AddRefed<CacheStorage> CacheStorage::CreateOnWorker(
171     Namespace aNamespace, nsIGlobalObject* aGlobal,
172     WorkerPrivate* aWorkerPrivate, ErrorResult& aRv) {
173   MOZ_DIAGNOSTIC_ASSERT(aGlobal);
174   MOZ_DIAGNOSTIC_ASSERT(aWorkerPrivate);
175   aWorkerPrivate->AssertIsOnWorkerThread();
176 
177   if (aWorkerPrivate->GetOriginAttributes().mPrivateBrowsingId > 0) {
178     NS_WARNING("CacheStorage not supported during private browsing.");
179     RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
180     return ref.forget();
181   }
182 
183   SafeRefPtr<CacheWorkerRef> workerRef =
184       CacheWorkerRef::Create(aWorkerPrivate, CacheWorkerRef::eIPCWorkerRef);
185   if (!workerRef) {
186     NS_WARNING("Worker thread is shutting down.");
187     aRv.Throw(NS_ERROR_FAILURE);
188     return nullptr;
189   }
190 
191   const PrincipalInfo& principalInfo =
192       aWorkerPrivate->GetEffectiveStoragePrincipalInfo();
193 
194   QM_TRY(OkIf(QuotaManager::IsPrincipalInfoValid(principalInfo)), nullptr,
195          [&aRv](const auto) { aRv.Throw(NS_ERROR_FAILURE); });
196 
197   // We have a number of cases where we want to skip the https scheme
198   // validation:
199   //
200   // 1) Any worker when dom.caches.testing.enabled pref is true.
201   // 2) Any worker when dom.serviceWorkers.testing.enabled pref is true.  This
202   //    is mainly because most sites using SWs will expect Cache to work if
203   //    SWs are enabled.
204   // 3) If the window that created this worker has the devtools SW testing
205   //    option enabled.  Same reasoning as (2).
206   // 4) If the worker itself is a ServiceWorker, then we always skip the
207   //    origin checks.  The ServiceWorker has its own trusted origin checks
208   //    that are better than ours.  In addition, we don't have information
209   //    about the window any more, so we can't do our own checks.
210   bool testingEnabled = StaticPrefs::dom_caches_testing_enabled() ||
211                         StaticPrefs::dom_serviceWorkers_testing_enabled() ||
212                         aWorkerPrivate->ServiceWorkersTestingInWindow() ||
213                         aWorkerPrivate->IsServiceWorker();
214 
215   if (!IsTrusted(principalInfo, testingEnabled)) {
216     NS_WARNING("CacheStorage not supported on untrusted origins.");
217     RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
218     return ref.forget();
219   }
220 
221   RefPtr<CacheStorage> ref = new CacheStorage(
222       aNamespace, aGlobal, principalInfo, std::move(workerRef));
223   return ref.forget();
224 }
225 
226 // static
DefineCaches(JSContext * aCx,JS::Handle<JSObject * > aGlobal)227 bool CacheStorage::DefineCaches(JSContext* aCx, JS::Handle<JSObject*> aGlobal) {
228   MOZ_ASSERT(NS_IsMainThread());
229   MOZ_DIAGNOSTIC_ASSERT(JS::GetClass(aGlobal)->flags & JSCLASS_DOM_GLOBAL,
230                         "Passed object is not a global object!");
231   js::AssertSameCompartment(aCx, aGlobal);
232 
233   if (NS_WARN_IF(!CacheStorage_Binding::GetConstructorObject(aCx) ||
234                  !Cache_Binding::GetConstructorObject(aCx))) {
235     return false;
236   }
237 
238   nsIPrincipal* principal = nsContentUtils::ObjectPrincipal(aGlobal);
239   MOZ_DIAGNOSTIC_ASSERT(principal);
240 
241   ErrorResult rv;
242   RefPtr<CacheStorage> storage =
243       CreateOnMainThread(DEFAULT_NAMESPACE, xpc::NativeGlobal(aGlobal),
244                          principal, true, /* force trusted */
245                          rv);
246   if (NS_WARN_IF(rv.MaybeSetPendingException(aCx))) {
247     return false;
248   }
249 
250   JS::Rooted<JS::Value> caches(aCx);
251   if (NS_WARN_IF(!ToJSValue(aCx, storage, &caches))) {
252     return false;
253   }
254 
255   return JS_DefineProperty(aCx, aGlobal, "caches", caches, JSPROP_ENUMERATE);
256 }
257 
CacheStorage(Namespace aNamespace,nsIGlobalObject * aGlobal,const PrincipalInfo & aPrincipalInfo,SafeRefPtr<CacheWorkerRef> aWorkerRef)258 CacheStorage::CacheStorage(Namespace aNamespace, nsIGlobalObject* aGlobal,
259                            const PrincipalInfo& aPrincipalInfo,
260                            SafeRefPtr<CacheWorkerRef> aWorkerRef)
261     : mNamespace(aNamespace),
262       mGlobal(aGlobal),
263       mPrincipalInfo(MakeUnique<PrincipalInfo>(aPrincipalInfo)),
264       mActor(nullptr),
265       mStatus(NS_OK) {
266   MOZ_DIAGNOSTIC_ASSERT(mGlobal);
267 
268   // If the PBackground actor is already initialized then we can
269   // immediately use it
270   PBackgroundChild* actor = BackgroundChild::GetOrCreateForCurrentThread();
271   if (NS_WARN_IF(!actor)) {
272     mStatus = NS_ERROR_UNEXPECTED;
273     return;
274   }
275 
276   // WorkerRef ownership is passed to the CacheStorageChild actor and any
277   // actors it may create.  The WorkerRef will keep the worker thread alive
278   // until the actors can gracefully shutdown.
279   CacheStorageChild* newActor =
280       new CacheStorageChild(this, std::move(aWorkerRef));
281   PCacheStorageChild* constructedActor = actor->SendPCacheStorageConstructor(
282       newActor, mNamespace, *mPrincipalInfo);
283 
284   if (NS_WARN_IF(!constructedActor)) {
285     mStatus = NS_ERROR_UNEXPECTED;
286     return;
287   }
288 
289   MOZ_DIAGNOSTIC_ASSERT(constructedActor == newActor);
290   mActor = newActor;
291 }
292 
CacheStorage(nsresult aFailureResult)293 CacheStorage::CacheStorage(nsresult aFailureResult)
294     : mNamespace(INVALID_NAMESPACE), mActor(nullptr), mStatus(aFailureResult) {
295   MOZ_DIAGNOSTIC_ASSERT(NS_FAILED(mStatus));
296 }
297 
Match(JSContext * aCx,const RequestOrUSVString & aRequest,const MultiCacheQueryOptions & aOptions,ErrorResult & aRv)298 already_AddRefed<Promise> CacheStorage::Match(
299     JSContext* aCx, const RequestOrUSVString& aRequest,
300     const MultiCacheQueryOptions& aOptions, ErrorResult& aRv) {
301   NS_ASSERT_OWNINGTHREAD(CacheStorage);
302 
303   if (!HasStorageAccess()) {
304     aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
305     return nullptr;
306   }
307 
308   if (NS_WARN_IF(NS_FAILED(mStatus))) {
309     aRv.Throw(mStatus);
310     return nullptr;
311   }
312 
313   SafeRefPtr<InternalRequest> request =
314       ToInternalRequest(aCx, aRequest, IgnoreBody, aRv);
315   if (NS_WARN_IF(aRv.Failed())) {
316     return nullptr;
317   }
318 
319   RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
320   if (NS_WARN_IF(!promise)) {
321     return nullptr;
322   }
323 
324   CacheQueryParams params;
325   ToCacheQueryParams(params, aOptions);
326 
327   auto entry = MakeUnique<Entry>();
328   entry->mPromise = promise;
329   entry->mArgs = StorageMatchArgs(CacheRequest(), params, GetOpenMode());
330   entry->mRequest = std::move(request);
331 
332   RunRequest(std::move(entry));
333 
334   return promise.forget();
335 }
336 
Has(const nsAString & aKey,ErrorResult & aRv)337 already_AddRefed<Promise> CacheStorage::Has(const nsAString& aKey,
338                                             ErrorResult& aRv) {
339   NS_ASSERT_OWNINGTHREAD(CacheStorage);
340 
341   if (!HasStorageAccess()) {
342     aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
343     return nullptr;
344   }
345 
346   if (NS_WARN_IF(NS_FAILED(mStatus))) {
347     aRv.Throw(mStatus);
348     return nullptr;
349   }
350 
351   RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
352   if (NS_WARN_IF(!promise)) {
353     return nullptr;
354   }
355 
356   auto entry = MakeUnique<Entry>();
357   entry->mPromise = promise;
358   entry->mArgs = StorageHasArgs(nsString(aKey));
359 
360   RunRequest(std::move(entry));
361 
362   return promise.forget();
363 }
364 
Open(const nsAString & aKey,ErrorResult & aRv)365 already_AddRefed<Promise> CacheStorage::Open(const nsAString& aKey,
366                                              ErrorResult& aRv) {
367   NS_ASSERT_OWNINGTHREAD(CacheStorage);
368 
369   if (!HasStorageAccess()) {
370     aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
371     return nullptr;
372   }
373 
374   if (NS_WARN_IF(NS_FAILED(mStatus))) {
375     aRv.Throw(mStatus);
376     return nullptr;
377   }
378 
379   RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
380   if (NS_WARN_IF(!promise)) {
381     return nullptr;
382   }
383 
384   auto entry = MakeUnique<Entry>();
385   entry->mPromise = promise;
386   entry->mArgs = StorageOpenArgs(nsString(aKey));
387 
388   RunRequest(std::move(entry));
389 
390   return promise.forget();
391 }
392 
Delete(const nsAString & aKey,ErrorResult & aRv)393 already_AddRefed<Promise> CacheStorage::Delete(const nsAString& aKey,
394                                                ErrorResult& aRv) {
395   NS_ASSERT_OWNINGTHREAD(CacheStorage);
396 
397   if (!HasStorageAccess()) {
398     aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
399     return nullptr;
400   }
401 
402   if (NS_WARN_IF(NS_FAILED(mStatus))) {
403     aRv.Throw(mStatus);
404     return nullptr;
405   }
406 
407   RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
408   if (NS_WARN_IF(!promise)) {
409     return nullptr;
410   }
411 
412   auto entry = MakeUnique<Entry>();
413   entry->mPromise = promise;
414   entry->mArgs = StorageDeleteArgs(nsString(aKey));
415 
416   RunRequest(std::move(entry));
417 
418   return promise.forget();
419 }
420 
Keys(ErrorResult & aRv)421 already_AddRefed<Promise> CacheStorage::Keys(ErrorResult& aRv) {
422   NS_ASSERT_OWNINGTHREAD(CacheStorage);
423 
424   if (!HasStorageAccess()) {
425     aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
426     return nullptr;
427   }
428 
429   if (NS_WARN_IF(NS_FAILED(mStatus))) {
430     aRv.Throw(mStatus);
431     return nullptr;
432   }
433 
434   RefPtr<Promise> promise = Promise::Create(mGlobal, aRv);
435   if (NS_WARN_IF(!promise)) {
436     return nullptr;
437   }
438 
439   auto entry = MakeUnique<Entry>();
440   entry->mPromise = promise;
441   entry->mArgs = StorageKeysArgs();
442 
443   RunRequest(std::move(entry));
444 
445   return promise.forget();
446 }
447 
448 // static
Constructor(const GlobalObject & aGlobal,CacheStorageNamespace aNamespace,nsIPrincipal * aPrincipal,ErrorResult & aRv)449 already_AddRefed<CacheStorage> CacheStorage::Constructor(
450     const GlobalObject& aGlobal, CacheStorageNamespace aNamespace,
451     nsIPrincipal* aPrincipal, ErrorResult& aRv) {
452   if (NS_WARN_IF(!NS_IsMainThread())) {
453     aRv.Throw(NS_ERROR_FAILURE);
454     return nullptr;
455   }
456 
457   // TODO: remove Namespace in favor of CacheStorageNamespace
458   static_assert(DEFAULT_NAMESPACE == (uint32_t)CacheStorageNamespace::Content,
459                 "Default namespace should match webidl Content enum");
460   static_assert(
461       CHROME_ONLY_NAMESPACE == (uint32_t)CacheStorageNamespace::Chrome,
462       "Chrome namespace should match webidl Chrome enum");
463   static_assert(NUMBER_OF_NAMESPACES == CacheStorageNamespaceValues::Count,
464                 "Number of namespace should match webidl count");
465 
466   Namespace ns = static_cast<Namespace>(aNamespace);
467   nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
468 
469   bool privateBrowsing = false;
470   if (nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global)) {
471     RefPtr<Document> doc = window->GetExtantDoc();
472     if (doc) {
473       nsCOMPtr<nsILoadContext> loadContext = doc->GetLoadContext();
474       privateBrowsing = loadContext && loadContext->UsePrivateBrowsing();
475     }
476   }
477 
478   if (privateBrowsing) {
479     RefPtr<CacheStorage> ref = new CacheStorage(NS_ERROR_DOM_SECURITY_ERR);
480     return ref.forget();
481   }
482 
483   // Create a CacheStorage object bypassing the trusted origin checks
484   // since this is a chrome-only constructor.
485   return CreateOnMainThread(ns, global, aPrincipal,
486                             true /* force trusted origin */, aRv);
487 }
488 
GetParentObject() const489 nsISupports* CacheStorage::GetParentObject() const { return mGlobal; }
490 
WrapObject(JSContext * aContext,JS::Handle<JSObject * > aGivenProto)491 JSObject* CacheStorage::WrapObject(JSContext* aContext,
492                                    JS::Handle<JSObject*> aGivenProto) {
493   return mozilla::dom::CacheStorage_Binding::Wrap(aContext, this, aGivenProto);
494 }
495 
DestroyInternal(CacheStorageChild * aActor)496 void CacheStorage::DestroyInternal(CacheStorageChild* aActor) {
497   NS_ASSERT_OWNINGTHREAD(CacheStorage);
498   MOZ_DIAGNOSTIC_ASSERT(mActor);
499   MOZ_DIAGNOSTIC_ASSERT(mActor == aActor);
500   MOZ_DIAGNOSTIC_ASSERT(!NS_FAILED(mStatus));
501   mActor->ClearListener();
502   mActor = nullptr;
503   mStatus = NS_ERROR_UNEXPECTED;
504 
505   // Note that we will never get an actor again in case another request is
506   // made before this object is destructed.
507 }
508 
GetGlobalObject() const509 nsIGlobalObject* CacheStorage::GetGlobalObject() const { return mGlobal; }
510 
511 #ifdef DEBUG
AssertOwningThread() const512 void CacheStorage::AssertOwningThread() const {
513   NS_ASSERT_OWNINGTHREAD(CacheStorage);
514 }
515 #endif
516 
GetIPCManager()517 PBackgroundChild* CacheStorage::GetIPCManager() {
518   // This is true because CacheStorage always uses IgnoreBody for requests.
519   // So we should never need to get the IPC manager during Request or
520   // Response serialization.
521   MOZ_CRASH("CacheStorage does not implement TypeUtils::GetIPCManager()");
522 }
523 
~CacheStorage()524 CacheStorage::~CacheStorage() {
525   NS_ASSERT_OWNINGTHREAD(CacheStorage);
526   if (mActor) {
527     mActor->StartDestroyFromListener();
528     // DestroyInternal() is called synchronously by StartDestroyFromListener().
529     // So we should have already cleared the mActor.
530     MOZ_DIAGNOSTIC_ASSERT(!mActor);
531   }
532 }
533 
RunRequest(UniquePtr<Entry> aEntry)534 void CacheStorage::RunRequest(UniquePtr<Entry> aEntry) {
535   MOZ_ASSERT(mActor);
536 
537   AutoChildOpArgs args(this, aEntry->mArgs, 1);
538 
539   if (aEntry->mRequest) {
540     ErrorResult rv;
541     args.Add(*aEntry->mRequest, IgnoreBody, IgnoreInvalidScheme, rv);
542     if (NS_WARN_IF(rv.Failed())) {
543       aEntry->mPromise->MaybeReject(std::move(rv));
544       return;
545     }
546   }
547 
548   mActor->ExecuteOp(mGlobal, aEntry->mPromise, this, args.SendAsOpArgs());
549 }
550 
GetOpenMode() const551 OpenMode CacheStorage::GetOpenMode() const {
552   return mNamespace == CHROME_ONLY_NAMESPACE ? OpenMode::Eager : OpenMode::Lazy;
553 }
554 
HasStorageAccess() const555 bool CacheStorage::HasStorageAccess() const {
556   NS_ASSERT_OWNINGTHREAD(CacheStorage);
557 
558   StorageAccess access;
559 
560   if (NS_IsMainThread()) {
561     nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mGlobal);
562     if (NS_WARN_IF(!window)) {
563       return true;
564     }
565 
566     access = StorageAllowedForWindow(window);
567   } else {
568     WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
569     MOZ_ASSERT(workerPrivate);
570 
571     access = workerPrivate->StorageAccess();
572   }
573 
574   return access > StorageAccess::ePrivateBrowsing;
575 }
576 
577 }  // namespace mozilla::dom::cache
578