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/ActorChild.h"
8 
9 #include "mozilla/dom/cache/CacheWorkerRef.h"
10 #include "nsThreadUtils.h"
11 
12 namespace mozilla::dom::cache {
13 
SetWorkerRef(SafeRefPtr<CacheWorkerRef> aWorkerRef)14 void ActorChild::SetWorkerRef(SafeRefPtr<CacheWorkerRef> aWorkerRef) {
15   // Some of the Cache actors can have multiple DOM objects associated with
16   // them.  In this case the workerRef will be added multiple times.  This is
17   // permitted, but the workerRef should be the same each time.
18   if (mWorkerRef) {
19     // XXX Here, we don't use aWorkerRef, so we unnecessarily add-refed... This
20     // might be a case to show in the raw pointer guideline as a possible
21     // exception, if warranted by performance analyses.
22 
23     MOZ_DIAGNOSTIC_ASSERT(mWorkerRef == aWorkerRef);
24     return;
25   }
26 
27   mWorkerRef = std::move(aWorkerRef);
28   if (mWorkerRef) {
29     mWorkerRef->AddActor(*this);
30   }
31 }
32 
RemoveWorkerRef()33 void ActorChild::RemoveWorkerRef() {
34   MOZ_ASSERT_IF(!NS_IsMainThread(), mWorkerRef);
35   if (mWorkerRef) {
36     mWorkerRef->RemoveActor(*this);
37     mWorkerRef = nullptr;
38   }
39 }
40 
GetWorkerRefPtr() const41 const SafeRefPtr<CacheWorkerRef>& ActorChild::GetWorkerRefPtr() const {
42   return mWorkerRef;
43 }
44 
WorkerRefNotified() const45 bool ActorChild::WorkerRefNotified() const {
46   return mWorkerRef && mWorkerRef->Notified();
47 }
48 
49 ActorChild::ActorChild() = default;
50 
~ActorChild()51 ActorChild::~ActorChild() { MOZ_DIAGNOSTIC_ASSERT(!mWorkerRef); }
52 
53 }  // namespace mozilla::dom::cache
54