BoundStorageKeyChild.cpp (2750B)
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 "BoundStorageKeyChild.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 #include "mozilla/dom/cache/PCacheStorageChild.h" 14 15 namespace mozilla::dom::cache { 16 17 BoundStorageKeyChild::BoundStorageKeyChild( 18 BoundStorageKeyChildListener* aListener) 19 : mListener(aListener), mDelayedDestroy(false) { 20 MOZ_COUNT_CTOR(BoundStorageKeyChild); 21 MOZ_DIAGNOSTIC_ASSERT(mListener); 22 } 23 24 BoundStorageKeyChild::~BoundStorageKeyChild() { 25 MOZ_COUNT_DTOR(BoundStorageKeyChild); 26 NS_ASSERT_OWNINGTHREAD(BoundStorageKeyChild); 27 MOZ_DIAGNOSTIC_ASSERT(!mListener); 28 } 29 30 void BoundStorageKeyChild::ClearListener() { 31 NS_ASSERT_OWNINGTHREAD(BoundStorageKeyChild); 32 MOZ_DIAGNOSTIC_ASSERT(mListener); 33 mListener = nullptr; 34 } 35 36 void BoundStorageKeyChild::StartDestroyFromListener() { 37 NS_ASSERT_OWNINGTHREAD(BoundStorageKeyChild); 38 39 StartDestroy(); 40 } 41 42 void BoundStorageKeyChild::DestroyInternal() { 43 BoundStorageKeyChildListener* listener = mListener; 44 45 // Theoretically we can get double called if the right race happens. Handle 46 // that by just ignoring the second StartDestroy() call. 47 if (!listener) { 48 return; 49 } 50 51 listener->OnActorDestroy(this); 52 53 // listener should call ClearListener() in OnActorDestroy() 54 MOZ_DIAGNOSTIC_ASSERT(!mListener); 55 } 56 57 void BoundStorageKeyChild::StartDestroy() { 58 // StartDestroy() can get called from either child actor or the 59 // CacheWorkerRef. 60 NS_ASSERT_OWNINGTHREAD(BoundStorageKeyChild); 61 62 if (NumChildActors() != 0) { 63 mDelayedDestroy = true; 64 return; 65 } 66 DestroyInternal(); 67 } 68 69 void BoundStorageKeyChild::NoteDeletedActor() { 70 // Check to see if DestroyInternal was delayed because of active CacheOpChilds 71 // when StartDestroy was called from WorkerRef notification. If the last 72 // CacheOpChild is getting destructed; it's the time for us to SendTearDown to 73 // the other side. 74 if (mDelayedDestroy && NumChildActors() == 0) { 75 DestroyInternal(); 76 } 77 } 78 79 void BoundStorageKeyChild::ActorDestroy(ActorDestroyReason aReason) { 80 NS_ASSERT_OWNINGTHREAD(BoundStorageKeyChild); 81 BoundStorageKeyChildListener* listener = mListener; 82 if (listener) { 83 listener->OnActorDestroy(this); 84 // listener should call ClearListener() in OnActorDestroy() 85 MOZ_DIAGNOSTIC_ASSERT(!mListener); 86 } 87 } 88 89 } // namespace mozilla::dom::cache