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"
10 #include "QuotaManagerService.h"
11 #include "QuotaRequests.h"
12 #include "QuotaResults.h"
17 #include "mozilla/Assertions.h"
18 #include "mozilla/dom/quota/PQuotaRequest.h"
21 #include "nsIEventTarget.h"
22 #include "nsIQuotaResults.h"
23 #include "nsISupports.h"
24 #include "nsIVariant.h"
26 #include "nsThreadUtils.h"
27 #include "nsVariant.h"
29 namespace mozilla::dom::quota
{
31 /*******************************************************************************
33 ******************************************************************************/
35 QuotaChild::QuotaChild(QuotaManagerService
* aService
)
39 mOwningThread(GetCurrentSerialEventTarget())
42 AssertIsOnOwningThread();
45 MOZ_COUNT_CTOR(quota::QuotaChild
);
48 QuotaChild::~QuotaChild() {
49 AssertIsOnOwningThread();
51 MOZ_COUNT_DTOR(quota::QuotaChild
);
56 void QuotaChild::AssertIsOnOwningThread() const {
57 MOZ_ASSERT(mOwningThread
);
60 MOZ_ASSERT(NS_SUCCEEDED(mOwningThread
->IsOnCurrentThread(¤t
)));
66 void QuotaChild::ActorDestroy(ActorDestroyReason aWhy
) {
67 AssertIsOnOwningThread();
70 mService
->ClearBackgroundActor();
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();
88 delete static_cast<QuotaRequestChild
*>(aActor
);
92 /*******************************************************************************
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
);
110 void QuotaRequestChild::AssertIsOnOwningThread() const {
111 MOZ_ASSERT(mRequest
);
112 mRequest
->AssertIsOnOwningThread();
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
),
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());
203 case RequestResponse::TStorageNameResponse
:
204 HandleResponse(aResponse
.get_StorageNameResponse().name());
207 case RequestResponse::TPersistResponse
:
211 case RequestResponse::TGetFullOriginMetadataResponse
:
212 HandleResponse(aResponse
.get_GetFullOriginMetadataResponse());
215 case RequestResponse::TPersistedResponse
:
216 HandleResponse(aResponse
.get_PersistedResponse().persisted());
219 case RequestResponse::TEstimateResponse
:
220 HandleResponse(aResponse
.get_EstimateResponse());
224 MOZ_CRASH("Unknown response type!");
230 } // namespace mozilla::dom::quota