Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / dom / quota / ActorsChild.cpp
bloba0d24ec5090ae4e7e3e7b1a85bc4874179d35d5e
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/. */
7 #include "ActorsChild.h"
9 // Local includes
10 #include "QuotaManagerService.h"
11 #include "QuotaRequests.h"
12 #include "QuotaResults.h"
14 // Global includes
15 #include <new>
16 #include <utility>
17 #include "mozilla/Assertions.h"
18 #include "mozilla/dom/quota/PQuotaRequest.h"
19 #include "nsError.h"
20 #include "nsID.h"
21 #include "nsIEventTarget.h"
22 #include "nsIQuotaResults.h"
23 #include "nsISupports.h"
24 #include "nsIVariant.h"
25 #include "nsString.h"
26 #include "nsThreadUtils.h"
27 #include "nsVariant.h"
29 namespace mozilla::dom::quota {
31 /*******************************************************************************
32 * QuotaChild
33 ******************************************************************************/
35 QuotaChild::QuotaChild(QuotaManagerService* aService)
36 : mService(aService)
37 #ifdef DEBUG
39 mOwningThread(GetCurrentSerialEventTarget())
40 #endif
42 AssertIsOnOwningThread();
43 MOZ_ASSERT(aService);
45 MOZ_COUNT_CTOR(quota::QuotaChild);
48 QuotaChild::~QuotaChild() {
49 AssertIsOnOwningThread();
51 MOZ_COUNT_DTOR(quota::QuotaChild);
54 #ifdef DEBUG
56 void QuotaChild::AssertIsOnOwningThread() const {
57 MOZ_ASSERT(mOwningThread);
59 bool current;
60 MOZ_ASSERT(NS_SUCCEEDED(mOwningThread->IsOnCurrentThread(&current)));
61 MOZ_ASSERT(current);
64 #endif // DEBUG
66 void QuotaChild::ActorDestroy(ActorDestroyReason aWhy) {
67 AssertIsOnOwningThread();
69 if (mService) {
70 mService->ClearBackgroundActor();
71 #ifdef DEBUG
72 mService = nullptr;
73 #endif
77 PQuotaRequestChild* QuotaChild::AllocPQuotaRequestChild(
78 const RequestParams& aParams) {
79 AssertIsOnOwningThread();
81 MOZ_CRASH("PQuotaRequestChild actors should be manually constructed!");
84 bool QuotaChild::DeallocPQuotaRequestChild(PQuotaRequestChild* aActor) {
85 AssertIsOnOwningThread();
86 MOZ_ASSERT(aActor);
88 delete static_cast<QuotaRequestChild*>(aActor);
89 return true;
92 /*******************************************************************************
93 * QuotaRequestChild
94 ******************************************************************************/
96 QuotaRequestChild::QuotaRequestChild(Request* aRequest) : mRequest(aRequest) {
97 AssertIsOnOwningThread();
99 MOZ_COUNT_CTOR(quota::QuotaRequestChild);
102 QuotaRequestChild::~QuotaRequestChild() {
103 AssertIsOnOwningThread();
105 MOZ_COUNT_DTOR(quota::QuotaRequestChild);
108 #ifdef DEBUG
110 void QuotaRequestChild::AssertIsOnOwningThread() const {
111 MOZ_ASSERT(mRequest);
112 mRequest->AssertIsOnOwningThread();
115 #endif // DEBUG
117 void QuotaRequestChild::HandleResponse(nsresult aResponse) {
118 AssertIsOnOwningThread();
119 MOZ_ASSERT(NS_FAILED(aResponse));
120 MOZ_ASSERT(mRequest);
122 mRequest->SetError(aResponse);
125 void QuotaRequestChild::HandleResponse() {
126 AssertIsOnOwningThread();
127 MOZ_ASSERT(mRequest);
129 RefPtr<nsVariant> variant = new nsVariant();
130 variant->SetAsVoid();
132 mRequest->SetResult(variant);
135 void QuotaRequestChild::HandleResponse(bool aResponse) {
136 AssertIsOnOwningThread();
137 MOZ_ASSERT(mRequest);
139 RefPtr<nsVariant> variant = new nsVariant();
140 variant->SetAsBool(aResponse);
142 mRequest->SetResult(variant);
145 void QuotaRequestChild::HandleResponse(const nsAString& aResponse) {
146 AssertIsOnOwningThread();
147 MOZ_ASSERT(mRequest);
149 RefPtr<nsVariant> variant = new nsVariant();
150 variant->SetAsAString(aResponse);
152 mRequest->SetResult(variant);
155 void QuotaRequestChild::HandleResponse(const EstimateResponse& aResponse) {
156 AssertIsOnOwningThread();
157 MOZ_ASSERT(mRequest);
159 RefPtr<EstimateResult> result =
160 new EstimateResult(aResponse.usage(), aResponse.limit());
162 RefPtr<nsVariant> variant = new nsVariant();
163 variant->SetAsInterface(NS_GET_IID(nsIQuotaEstimateResult), result);
165 mRequest->SetResult(variant);
168 void QuotaRequestChild::HandleResponse(
169 const GetFullOriginMetadataResponse& aResponse) {
170 AssertIsOnOwningThread();
171 MOZ_ASSERT(mRequest);
173 RefPtr<nsVariant> variant = new nsVariant();
175 if (aResponse.maybeFullOriginMetadata()) {
176 RefPtr<FullOriginMetadataResult> result =
177 new FullOriginMetadataResult(*aResponse.maybeFullOriginMetadata());
179 variant->SetAsInterface(NS_GET_IID(nsIQuotaFullOriginMetadataResult),
180 result);
182 } else {
183 variant->SetAsVoid();
186 mRequest->SetResult(variant);
189 void QuotaRequestChild::ActorDestroy(ActorDestroyReason aWhy) {
190 AssertIsOnOwningThread();
193 mozilla::ipc::IPCResult QuotaRequestChild::Recv__delete__(
194 const RequestResponse& aResponse) {
195 AssertIsOnOwningThread();
196 MOZ_ASSERT(mRequest);
198 switch (aResponse.type()) {
199 case RequestResponse::Tnsresult:
200 HandleResponse(aResponse.get_nsresult());
201 break;
203 case RequestResponse::TStorageNameResponse:
204 HandleResponse(aResponse.get_StorageNameResponse().name());
205 break;
207 case RequestResponse::TPersistResponse:
208 HandleResponse();
209 break;
211 case RequestResponse::TGetFullOriginMetadataResponse:
212 HandleResponse(aResponse.get_GetFullOriginMetadataResponse());
213 break;
215 case RequestResponse::TPersistedResponse:
216 HandleResponse(aResponse.get_PersistedResponse().persisted());
217 break;
219 case RequestResponse::TEstimateResponse:
220 HandleResponse(aResponse.get_EstimateResponse());
221 break;
223 default:
224 MOZ_CRASH("Unknown response type!");
227 return IPC_OK();
230 } // namespace mozilla::dom::quota