CacheStorageChild.cpp (3352B)
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/CacheStorageChild.h" 8 9 #include "mozilla/dom/cache/CacheChild.h" 10 #include "mozilla/dom/cache/CacheOpChild.h" 11 #include "mozilla/dom/cache/CacheStorage.h" 12 #include "mozilla/dom/cache/CacheWorkerRef.h" 13 14 namespace mozilla::dom::cache { 15 16 // declared in ActorUtils.h 17 void DeallocPCacheStorageChild(PCacheStorageChild* aActor) { delete aActor; } 18 19 CacheStorageChild::CacheStorageChild(CacheStorageChildListener* aListener, 20 SafeRefPtr<CacheWorkerRef> aWorkerRef, 21 ActorChild* aParentActor) 22 : mParentActor(aParentActor), mListener(aListener), mDelayedDestroy(false) { 23 MOZ_COUNT_CTOR(cache::CacheStorageChild); 24 MOZ_DIAGNOSTIC_ASSERT(mListener); 25 26 SetWorkerRef(std::move(aWorkerRef)); 27 } 28 29 CacheStorageChild::~CacheStorageChild() { 30 MOZ_COUNT_DTOR(cache::CacheStorageChild); 31 NS_ASSERT_OWNINGTHREAD(CacheStorageChild); 32 MOZ_DIAGNOSTIC_ASSERT(!mListener); 33 } 34 35 void CacheStorageChild::ClearListener() { 36 NS_ASSERT_OWNINGTHREAD(CacheStorageChild); 37 MOZ_DIAGNOSTIC_ASSERT(mListener); 38 mListener = nullptr; 39 } 40 41 void CacheStorageChild::StartDestroyFromListener() { 42 NS_ASSERT_OWNINGTHREAD(CacheStorageChild); 43 44 StartDestroy(); 45 } 46 47 void CacheStorageChild::DestroyInternal() { 48 CacheStorageChildListener* listener = mListener; 49 50 // StartDestroy() can get called from either CacheStorage or the 51 // CacheWorkerRef. 52 // Theoretically we can get double called if the right race happens. Handle 53 // that by just ignoring the second StartDestroy() call. 54 if (!listener) { 55 return; 56 } 57 58 listener->OnActorDestroy(this); 59 60 // CacheStorage listener should call ClearListener() in OnActorDestroy() 61 MOZ_DIAGNOSTIC_ASSERT(!mListener); 62 63 // Start actor destruction from parent process 64 QM_WARNONLY_TRY(OkIf(SendTeardown())); 65 } 66 67 void CacheStorageChild::StartDestroy() { 68 NS_ASSERT_OWNINGTHREAD(CacheStorageChild); 69 70 if (NumChildActors() != 0) { 71 mDelayedDestroy = true; 72 return; 73 } 74 DestroyInternal(); 75 } 76 77 void CacheStorageChild::NoteDeletedActor() { 78 // Check to see if DestroyInternal was delayed because of active CacheOpChilds 79 // when StartDestroy was called from WorkerRef notification. If the last 80 // CacheOpChild is getting destructed; it's the time for us to SendTearDown to 81 // the other side. 82 if (NumChildActors() == 0 && mDelayedDestroy) { 83 DestroyInternal(); 84 } 85 } 86 87 void CacheStorageChild::ActorDestroy(ActorDestroyReason aReason) { 88 NS_ASSERT_OWNINGTHREAD(CacheStorageChild); 89 CacheStorageChildListener* listener = mListener; 90 if (listener) { 91 listener->OnActorDestroy(this); 92 // CacheStorage listener should call ClearListener() in OnActorDestroy() 93 MOZ_DIAGNOSTIC_ASSERT(!mListener); 94 } 95 96 if (mParentActor) { 97 mParentActor->NoteDeletedActor(); 98 } 99 100 RemoveWorkerRef(); 101 } 102 103 PCacheOpChild* CacheStorageChild::AllocPCacheOpChild( 104 const CacheOpArgs& aOpArgs) { 105 MOZ_CRASH("CacheOpChild should be manually constructed."); 106 return nullptr; 107 } 108 } // namespace mozilla::dom::cache