Bug 1850460 - Removed file build/build-clang/revert-llvmorg-18-init-3787-gb6a1473f97d...
[gecko.git] / dom / cache / CacheWorkerRef.cpp
blob992de976e191ff3d1a90c93892323892ce300ef3
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/. */
7 #include "mozilla/dom/cache/CacheWorkerRef.h"
9 #include "mozilla/dom/cache/ActorChild.h"
10 #include "mozilla/dom/WorkerPrivate.h"
11 #include "mozilla/dom/WorkerRef.h"
13 namespace mozilla::dom::cache {
15 // static
16 SafeRefPtr<CacheWorkerRef> CacheWorkerRef::Create(WorkerPrivate* aWorkerPrivate,
17 Behavior aBehavior) {
18 MOZ_DIAGNOSTIC_ASSERT(aWorkerPrivate);
20 // XXX This looks as if this could be simplified now by moving into the ctor
21 // of CacheWorkerRef, since we can now use SafeRefPtrFromThis in the ctor
22 auto workerRef =
23 MakeSafeRefPtr<CacheWorkerRef>(aBehavior, ConstructorGuard{});
24 auto notify = [workerRef = workerRef.clonePtr()] { workerRef->Notify(); };
25 if (aBehavior == eStrongWorkerRef) {
26 workerRef->mStrongWorkerRef = StrongWorkerRef::Create(
27 aWorkerPrivate, "CacheWorkerRef-Strong", std::move(notify));
28 } else {
29 MOZ_ASSERT(aBehavior == eIPCWorkerRef);
30 workerRef->mIPCWorkerRef = IPCWorkerRef::Create(
31 aWorkerPrivate, "CacheWorkerRef-IPC", std::move(notify));
34 if (NS_WARN_IF(!workerRef->mIPCWorkerRef && !workerRef->mStrongWorkerRef)) {
35 return nullptr;
38 return workerRef;
41 // static
42 SafeRefPtr<CacheWorkerRef> CacheWorkerRef::PreferBehavior(
43 SafeRefPtr<CacheWorkerRef> aCurrentRef, Behavior aBehavior) {
44 if (!aCurrentRef) {
45 return nullptr;
48 SafeRefPtr<CacheWorkerRef> orig = std::move(aCurrentRef);
49 if (orig->mBehavior == aBehavior) {
50 return orig;
53 WorkerPrivate* workerPrivate = nullptr;
54 if (orig->mBehavior == eStrongWorkerRef) {
55 workerPrivate = orig->mStrongWorkerRef->Private();
56 } else {
57 MOZ_ASSERT(orig->mBehavior == eIPCWorkerRef);
58 workerPrivate = orig->mIPCWorkerRef->Private();
61 MOZ_ASSERT(workerPrivate);
63 SafeRefPtr<CacheWorkerRef> replace = Create(workerPrivate, aBehavior);
64 return static_cast<bool>(replace) ? std::move(replace) : std::move(orig);
67 void CacheWorkerRef::AddActor(ActorChild& aActor) {
68 NS_ASSERT_OWNINGTHREAD(CacheWorkerRef);
69 MOZ_ASSERT(!mActorList.Contains(&aActor));
71 mActorList.AppendElement(WrapNotNullUnchecked(&aActor));
73 // Allow an actor to be added after we've entered the Notifying case. We
74 // can't stop the actor creation from racing with out destruction of the
75 // other actors and we need to wait for this extra one to close as well.
76 // Signal it should destroy itself right away.
77 if (mNotified) {
78 aActor.StartDestroy();
82 void CacheWorkerRef::RemoveActor(ActorChild& aActor) {
83 NS_ASSERT_OWNINGTHREAD(CacheWorkerRef);
85 #if defined(RELEASE_OR_BETA)
86 mActorList.RemoveElement(&aActor);
87 #else
88 MOZ_DIAGNOSTIC_ASSERT(mActorList.RemoveElement(&aActor));
89 #endif
91 MOZ_ASSERT(!mActorList.Contains(&aActor));
93 if (mActorList.IsEmpty()) {
94 mStrongWorkerRef = nullptr;
95 mIPCWorkerRef = nullptr;
99 bool CacheWorkerRef::Notified() const { return mNotified; }
101 void CacheWorkerRef::Notify() {
102 NS_ASSERT_OWNINGTHREAD(CacheWorkerRef);
104 mNotified = true;
106 // Start the asynchronous destruction of our actors. These will call back
107 // into RemoveActor() once the actor is destroyed.
108 for (const auto& actor : mActorList) {
109 actor->StartDestroy();
113 CacheWorkerRef::CacheWorkerRef(Behavior aBehavior, ConstructorGuard)
114 : mBehavior(aBehavior), mNotified(false) {}
116 CacheWorkerRef::~CacheWorkerRef() {
117 NS_ASSERT_OWNINGTHREAD(CacheWorkerRef);
118 MOZ_DIAGNOSTIC_ASSERT(mActorList.IsEmpty());
121 } // namespace mozilla::dom::cache