Line data Source code
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 file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "ActorsChild.h"
8 :
9 : #include "nsVariant.h"
10 : #include "QuotaManagerService.h"
11 : #include "QuotaRequests.h"
12 : #include "QuotaResults.h"
13 :
14 : namespace mozilla {
15 : namespace dom {
16 : namespace quota {
17 :
18 : /*******************************************************************************
19 : * QuotaChild
20 : ******************************************************************************/
21 :
22 0 : QuotaChild::QuotaChild(QuotaManagerService* aService)
23 : : mService(aService)
24 : #ifdef DEBUG
25 0 : , mOwningThread(GetCurrentThreadEventTarget())
26 : #endif
27 : {
28 0 : AssertIsOnOwningThread();
29 0 : MOZ_ASSERT(aService);
30 :
31 0 : MOZ_COUNT_CTOR(quota::QuotaChild);
32 0 : }
33 :
34 0 : QuotaChild::~QuotaChild()
35 : {
36 0 : AssertIsOnOwningThread();
37 :
38 0 : MOZ_COUNT_DTOR(quota::QuotaChild);
39 0 : }
40 :
41 : #ifdef DEBUG
42 :
43 : void
44 0 : QuotaChild::AssertIsOnOwningThread() const
45 : {
46 0 : MOZ_ASSERT(mOwningThread);
47 :
48 : bool current;
49 0 : MOZ_ASSERT(NS_SUCCEEDED(mOwningThread->IsOnCurrentThread(¤t)));
50 0 : MOZ_ASSERT(current);
51 0 : }
52 :
53 : #endif // DEBUG
54 :
55 : void
56 0 : QuotaChild::ActorDestroy(ActorDestroyReason aWhy)
57 : {
58 0 : AssertIsOnOwningThread();
59 :
60 0 : if (mService) {
61 0 : mService->ClearBackgroundActor();
62 : #ifdef DEBUG
63 0 : mService = nullptr;
64 : #endif
65 : }
66 0 : }
67 :
68 : PQuotaUsageRequestChild*
69 0 : QuotaChild::AllocPQuotaUsageRequestChild(const UsageRequestParams& aParams)
70 : {
71 0 : AssertIsOnOwningThread();
72 :
73 0 : MOZ_CRASH("PQuotaUsageRequestChild actors should be manually constructed!");
74 : }
75 :
76 : bool
77 0 : QuotaChild::DeallocPQuotaUsageRequestChild(PQuotaUsageRequestChild* aActor)
78 : {
79 0 : AssertIsOnOwningThread();
80 0 : MOZ_ASSERT(aActor);
81 :
82 0 : delete static_cast<QuotaUsageRequestChild*>(aActor);
83 0 : return true;
84 : }
85 :
86 : PQuotaRequestChild*
87 0 : QuotaChild::AllocPQuotaRequestChild(const RequestParams& aParams)
88 : {
89 0 : AssertIsOnOwningThread();
90 :
91 0 : MOZ_CRASH("PQuotaRequestChild actors should be manually constructed!");
92 : }
93 :
94 : bool
95 0 : QuotaChild::DeallocPQuotaRequestChild(PQuotaRequestChild* aActor)
96 : {
97 0 : AssertIsOnOwningThread();
98 0 : MOZ_ASSERT(aActor);
99 :
100 0 : delete static_cast<QuotaRequestChild*>(aActor);
101 0 : return true;
102 : }
103 :
104 : /*******************************************************************************
105 : * QuotaUsageRequestChild
106 : ******************************************************************************/
107 :
108 0 : QuotaUsageRequestChild::QuotaUsageRequestChild(UsageRequest* aRequest)
109 0 : : mRequest(aRequest)
110 : {
111 0 : AssertIsOnOwningThread();
112 :
113 0 : MOZ_COUNT_CTOR(quota::QuotaUsageRequestChild);
114 0 : }
115 :
116 0 : QuotaUsageRequestChild::~QuotaUsageRequestChild()
117 : {
118 : // Can't assert owning thread here because the request is cleared.
119 :
120 0 : MOZ_COUNT_DTOR(quota::QuotaUsageRequestChild);
121 0 : }
122 :
123 : #ifdef DEBUG
124 :
125 : void
126 0 : QuotaUsageRequestChild::AssertIsOnOwningThread() const
127 : {
128 0 : MOZ_ASSERT(mRequest);
129 0 : mRequest->AssertIsOnOwningThread();
130 0 : }
131 :
132 : #endif // DEBUG
133 :
134 : void
135 0 : QuotaUsageRequestChild::HandleResponse(nsresult aResponse)
136 : {
137 0 : AssertIsOnOwningThread();
138 0 : MOZ_ASSERT(NS_FAILED(aResponse));
139 0 : MOZ_ASSERT(mRequest);
140 :
141 0 : mRequest->SetError(aResponse);
142 0 : }
143 :
144 : void
145 0 : QuotaUsageRequestChild::HandleResponse(const nsTArray<OriginUsage>& aResponse)
146 : {
147 0 : AssertIsOnOwningThread();
148 0 : MOZ_ASSERT(mRequest);
149 :
150 0 : RefPtr<nsVariant> variant = new nsVariant();
151 :
152 0 : if (aResponse.IsEmpty()) {
153 0 : variant->SetAsEmptyArray();
154 : } else {
155 0 : nsTArray<RefPtr<UsageResult>> usageResults;
156 :
157 0 : const uint32_t count = aResponse.Length();
158 :
159 0 : usageResults.SetCapacity(count);
160 :
161 0 : for (uint32_t index = 0; index < count; index++) {
162 0 : auto& originUsage = aResponse[index];
163 :
164 0 : RefPtr<UsageResult> usageResult = new UsageResult(originUsage.origin(),
165 0 : originUsage.persisted(),
166 0 : originUsage.usage());
167 :
168 0 : usageResults.AppendElement(usageResult.forget());
169 : }
170 :
171 0 : variant->SetAsArray(nsIDataType::VTYPE_INTERFACE_IS,
172 : &NS_GET_IID(nsIQuotaUsageResult),
173 0 : usageResults.Length(),
174 0 : static_cast<void*>(usageResults.Elements()));
175 : }
176 :
177 0 : mRequest->SetResult(variant);
178 0 : }
179 :
180 : void
181 0 : QuotaUsageRequestChild::HandleResponse(const OriginUsageResponse& aResponse)
182 : {
183 0 : AssertIsOnOwningThread();
184 0 : MOZ_ASSERT(mRequest);
185 :
186 : RefPtr<OriginUsageResult> result =
187 0 : new OriginUsageResult(aResponse.usage(),
188 0 : aResponse.fileUsage(),
189 0 : aResponse.limit());
190 :
191 0 : RefPtr<nsVariant> variant = new nsVariant();
192 0 : variant->SetAsInterface(NS_GET_IID(nsIQuotaOriginUsageResult), result);
193 :
194 0 : mRequest->SetResult(variant);
195 0 : }
196 :
197 : void
198 0 : QuotaUsageRequestChild::ActorDestroy(ActorDestroyReason aWhy)
199 : {
200 0 : AssertIsOnOwningThread();
201 :
202 0 : if (mRequest) {
203 0 : mRequest->ClearBackgroundActor();
204 : #ifdef DEBUG
205 0 : mRequest = nullptr;
206 : #endif
207 : }
208 0 : }
209 :
210 : mozilla::ipc::IPCResult
211 0 : QuotaUsageRequestChild::Recv__delete__(const UsageRequestResponse& aResponse)
212 : {
213 0 : AssertIsOnOwningThread();
214 0 : MOZ_ASSERT(mRequest);
215 :
216 0 : switch (aResponse.type()) {
217 : case UsageRequestResponse::Tnsresult:
218 0 : HandleResponse(aResponse.get_nsresult());
219 0 : break;
220 :
221 : case UsageRequestResponse::TAllUsageResponse:
222 0 : HandleResponse(aResponse.get_AllUsageResponse().originUsages());
223 0 : break;
224 :
225 : case UsageRequestResponse::TOriginUsageResponse:
226 0 : HandleResponse(aResponse.get_OriginUsageResponse());
227 0 : break;
228 :
229 : default:
230 0 : MOZ_CRASH("Unknown response type!");
231 : }
232 :
233 0 : return IPC_OK();
234 : }
235 :
236 : /*******************************************************************************
237 : * QuotaRequestChild
238 : ******************************************************************************/
239 :
240 0 : QuotaRequestChild::QuotaRequestChild(Request* aRequest)
241 0 : : mRequest(aRequest)
242 : {
243 0 : AssertIsOnOwningThread();
244 :
245 0 : MOZ_COUNT_CTOR(quota::QuotaRequestChild);
246 0 : }
247 :
248 0 : QuotaRequestChild::~QuotaRequestChild()
249 : {
250 0 : AssertIsOnOwningThread();
251 :
252 0 : MOZ_COUNT_DTOR(quota::QuotaRequestChild);
253 0 : }
254 :
255 : #ifdef DEBUG
256 :
257 : void
258 0 : QuotaRequestChild::AssertIsOnOwningThread() const
259 : {
260 0 : MOZ_ASSERT(mRequest);
261 0 : mRequest->AssertIsOnOwningThread();
262 0 : }
263 :
264 : #endif // DEBUG
265 :
266 : void
267 0 : QuotaRequestChild::HandleResponse(nsresult aResponse)
268 : {
269 0 : AssertIsOnOwningThread();
270 0 : MOZ_ASSERT(NS_FAILED(aResponse));
271 0 : MOZ_ASSERT(mRequest);
272 :
273 0 : mRequest->SetError(aResponse);
274 0 : }
275 :
276 : void
277 0 : QuotaRequestChild::HandleResponse()
278 : {
279 0 : AssertIsOnOwningThread();
280 0 : MOZ_ASSERT(mRequest);
281 :
282 0 : RefPtr<nsVariant> variant = new nsVariant();
283 0 : variant->SetAsVoid();
284 :
285 0 : mRequest->SetResult(variant);
286 0 : }
287 :
288 : void
289 0 : QuotaRequestChild::HandleResponse(bool aResponse)
290 : {
291 0 : AssertIsOnOwningThread();
292 0 : MOZ_ASSERT(mRequest);
293 :
294 0 : RefPtr<nsVariant> variant = new nsVariant();
295 0 : variant->SetAsBool(aResponse);
296 :
297 0 : mRequest->SetResult(variant);
298 0 : }
299 :
300 : void
301 0 : QuotaRequestChild::ActorDestroy(ActorDestroyReason aWhy)
302 : {
303 0 : AssertIsOnOwningThread();
304 0 : }
305 :
306 : mozilla::ipc::IPCResult
307 0 : QuotaRequestChild::Recv__delete__(const RequestResponse& aResponse)
308 : {
309 0 : AssertIsOnOwningThread();
310 0 : MOZ_ASSERT(mRequest);
311 :
312 0 : switch (aResponse.type()) {
313 : case RequestResponse::Tnsresult:
314 0 : HandleResponse(aResponse.get_nsresult());
315 0 : break;
316 :
317 : case RequestResponse::TInitResponse:
318 : case RequestResponse::TClearOriginResponse:
319 : case RequestResponse::TClearDataResponse:
320 : case RequestResponse::TClearAllResponse:
321 : case RequestResponse::TResetAllResponse:
322 : case RequestResponse::TPersistResponse:
323 0 : HandleResponse();
324 0 : break;
325 :
326 : case RequestResponse::TInitOriginResponse:
327 0 : HandleResponse(aResponse.get_InitOriginResponse().created());
328 0 : break;
329 :
330 : case RequestResponse::TPersistedResponse:
331 0 : HandleResponse(aResponse.get_PersistedResponse().persisted());
332 0 : break;
333 :
334 : default:
335 0 : MOZ_CRASH("Unknown response type!");
336 : }
337 :
338 0 : return IPC_OK();
339 : }
340 :
341 : } // namespace quota
342 : } // namespace dom
343 : } // namespace mozilla
|