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