/src/mozilla-central/dom/cache/CacheChild.cpp
Line | Count | Source (jump to first uncovered line) |
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/CacheChild.h" |
8 | | |
9 | | #include "mozilla/Unused.h" |
10 | | #include "mozilla/dom/cache/ActorUtils.h" |
11 | | #include "mozilla/dom/cache/Cache.h" |
12 | | #include "mozilla/dom/cache/CacheOpChild.h" |
13 | | |
14 | | namespace mozilla { |
15 | | namespace dom { |
16 | | namespace cache { |
17 | | |
18 | | // Declared in ActorUtils.h |
19 | | PCacheChild* |
20 | | AllocPCacheChild() |
21 | 0 | { |
22 | 0 | return new CacheChild(); |
23 | 0 | } |
24 | | |
25 | | // Declared in ActorUtils.h |
26 | | void |
27 | | DeallocPCacheChild(PCacheChild* aActor) |
28 | 0 | { |
29 | 0 | delete aActor; |
30 | 0 | } |
31 | | |
32 | | CacheChild::CacheChild() |
33 | | : mListener(nullptr) |
34 | | , mNumChildActors(0) |
35 | | , mDelayedDestroy(false) |
36 | | , mLocked(false) |
37 | 0 | { |
38 | 0 | MOZ_COUNT_CTOR(cache::CacheChild); |
39 | 0 | } |
40 | | |
41 | | CacheChild::~CacheChild() |
42 | 0 | { |
43 | 0 | MOZ_COUNT_DTOR(cache::CacheChild); |
44 | 0 | NS_ASSERT_OWNINGTHREAD(CacheChild); |
45 | 0 | MOZ_DIAGNOSTIC_ASSERT(!mListener); |
46 | 0 | MOZ_DIAGNOSTIC_ASSERT(!mNumChildActors); |
47 | 0 | MOZ_DIAGNOSTIC_ASSERT(!mLocked); |
48 | 0 | } |
49 | | |
50 | | void |
51 | | CacheChild::SetListener(Cache* aListener) |
52 | 0 | { |
53 | 0 | NS_ASSERT_OWNINGTHREAD(CacheChild); |
54 | 0 | MOZ_DIAGNOSTIC_ASSERT(!mListener); |
55 | 0 | mListener = aListener; |
56 | 0 | MOZ_DIAGNOSTIC_ASSERT(mListener); |
57 | 0 | } |
58 | | |
59 | | void |
60 | | CacheChild::ClearListener() |
61 | 0 | { |
62 | 0 | NS_ASSERT_OWNINGTHREAD(CacheChild); |
63 | 0 | MOZ_DIAGNOSTIC_ASSERT(mListener); |
64 | 0 | mListener = nullptr; |
65 | 0 | } |
66 | | |
67 | | void |
68 | | CacheChild::ExecuteOp(nsIGlobalObject* aGlobal, Promise* aPromise, |
69 | | nsISupports* aParent, const CacheOpArgs& aArgs) |
70 | 0 | { |
71 | 0 | mNumChildActors += 1; |
72 | 0 | MOZ_ALWAYS_TRUE(SendPCacheOpConstructor( |
73 | 0 | new CacheOpChild(GetWorkerHolder(), aGlobal, aParent, aPromise), aArgs)); |
74 | 0 | } |
75 | | |
76 | | void |
77 | | CacheChild::StartDestroyFromListener() |
78 | 0 | { |
79 | 0 | NS_ASSERT_OWNINGTHREAD(CacheChild); |
80 | 0 |
|
81 | 0 | // The listener should be held alive by any async operations, so if it |
82 | 0 | // is going away then there must not be any child actors. This in turn |
83 | 0 | // ensures that StartDestroy() will not trigger the delayed path. |
84 | 0 | MOZ_DIAGNOSTIC_ASSERT(!mNumChildActors); |
85 | 0 |
|
86 | 0 | StartDestroy(); |
87 | 0 | } |
88 | | |
89 | | void |
90 | | CacheChild::StartDestroy() |
91 | 0 | { |
92 | 0 | NS_ASSERT_OWNINGTHREAD(CacheChild); |
93 | 0 |
|
94 | 0 | // If we have outstanding child actors, then don't destroy ourself yet. |
95 | 0 | // The child actors should be short lived and we should allow them to complete |
96 | 0 | // if possible. NoteDeletedActor() will call back into this Shutdown() |
97 | 0 | // method when the last child actor is gone. Also, delay destruction if we |
98 | 0 | // have been explicitly locked by someone using us on the stack. |
99 | 0 | if (mNumChildActors || mLocked) { |
100 | 0 | mDelayedDestroy = true; |
101 | 0 | return; |
102 | 0 | } |
103 | 0 | |
104 | 0 | RefPtr<Cache> listener = mListener; |
105 | 0 |
|
106 | 0 | // StartDestroy() can get called from either Cache or the WorkerHolder. |
107 | 0 | // Theoretically we can get double called if the right race happens. Handle |
108 | 0 | // that by just ignoring the second StartDestroy() call. |
109 | 0 | if (!listener) { |
110 | 0 | return; |
111 | 0 | } |
112 | 0 | |
113 | 0 | listener->DestroyInternal(this); |
114 | 0 |
|
115 | 0 | // Cache listener should call ClearListener() in DestroyInternal() |
116 | 0 | MOZ_DIAGNOSTIC_ASSERT(!mListener); |
117 | 0 |
|
118 | 0 | // Start actor destruction from parent process |
119 | 0 | Unused << SendTeardown(); |
120 | 0 | } |
121 | | |
122 | | void |
123 | | CacheChild::ActorDestroy(ActorDestroyReason aReason) |
124 | 0 | { |
125 | 0 | NS_ASSERT_OWNINGTHREAD(CacheChild); |
126 | 0 | RefPtr<Cache> listener = mListener; |
127 | 0 | if (listener) { |
128 | 0 | listener->DestroyInternal(this); |
129 | 0 | // Cache listener should call ClearListener() in DestroyInternal() |
130 | 0 | MOZ_DIAGNOSTIC_ASSERT(!mListener); |
131 | 0 | } |
132 | 0 |
|
133 | 0 | RemoveWorkerHolder(); |
134 | 0 | } |
135 | | |
136 | | PCacheOpChild* |
137 | | CacheChild::AllocPCacheOpChild(const CacheOpArgs& aOpArgs) |
138 | 0 | { |
139 | 0 | MOZ_CRASH("CacheOpChild should be manually constructed."); |
140 | 0 | return nullptr; |
141 | 0 | } |
142 | | |
143 | | bool |
144 | | CacheChild::DeallocPCacheOpChild(PCacheOpChild* aActor) |
145 | 0 | { |
146 | 0 | delete aActor; |
147 | 0 | NoteDeletedActor(); |
148 | 0 | return true; |
149 | 0 | } |
150 | | |
151 | | void |
152 | | CacheChild::NoteDeletedActor() |
153 | 0 | { |
154 | 0 | mNumChildActors -= 1; |
155 | 0 | MaybeFlushDelayedDestroy(); |
156 | 0 | } |
157 | | |
158 | | void |
159 | | CacheChild::MaybeFlushDelayedDestroy() |
160 | 0 | { |
161 | 0 | if (!mNumChildActors && !mLocked && mDelayedDestroy) { |
162 | 0 | StartDestroy(); |
163 | 0 | } |
164 | 0 | } |
165 | | |
166 | | void |
167 | | CacheChild::Lock() |
168 | 0 | { |
169 | 0 | NS_ASSERT_OWNINGTHREAD(CacheChild); |
170 | 0 | MOZ_DIAGNOSTIC_ASSERT(!mLocked); |
171 | 0 | mLocked = true; |
172 | 0 | } |
173 | | |
174 | | void |
175 | | CacheChild::Unlock() |
176 | 0 | { |
177 | 0 | NS_ASSERT_OWNINGTHREAD(CacheChild); |
178 | 0 | MOZ_DIAGNOSTIC_ASSERT(mLocked); |
179 | 0 | mLocked = false; |
180 | 0 | MaybeFlushDelayedDestroy(); |
181 | 0 | } |
182 | | |
183 | | } // namespace cache |
184 | | } // namespace dom |
185 | | } // namespace mozilla |