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/. */
7 #include "QuotaRequests.h"
10 #include "ActorsChild.h"
13 #include "mozilla/ErrorNames.h"
14 #include "mozilla/MacroForEach.h"
16 #include "nsIQuotaCallbacks.h"
17 #include "nsISupportsUtils.h"
18 #include "nsIVariant.h"
19 #include "nsStringFwd.h"
21 #include "mozilla/dom/quota/QuotaUsageRequestChild.h"
27 namespace mozilla::dom::quota
{
29 RequestBase::RequestBase() : mResultCode(NS_OK
), mHaveResultOrErrorCode(false) {
30 AssertIsOnOwningThread();
33 RequestBase::RequestBase(nsIPrincipal
* aPrincipal
)
34 : mPrincipal(aPrincipal
),
36 mHaveResultOrErrorCode(false) {
37 AssertIsOnOwningThread();
42 void RequestBase::AssertIsOnOwningThread() const {
43 NS_ASSERT_OWNINGTHREAD(RequestBase
);
48 void RequestBase::SetError(nsresult aRv
) {
49 AssertIsOnOwningThread();
50 MOZ_ASSERT(mResultCode
== NS_OK
);
51 MOZ_ASSERT(!mHaveResultOrErrorCode
);
54 mHaveResultOrErrorCode
= true;
59 NS_IMPL_CYCLE_COLLECTION_0(RequestBase
)
61 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(RequestBase
)
62 NS_INTERFACE_MAP_ENTRY(nsISupports
)
65 NS_IMPL_CYCLE_COLLECTING_ADDREF(RequestBase
)
66 NS_IMPL_CYCLE_COLLECTING_RELEASE(RequestBase
)
69 RequestBase::GetPrincipal(nsIPrincipal
** aPrincipal
) {
70 AssertIsOnOwningThread();
71 MOZ_ASSERT(aPrincipal
);
73 NS_IF_ADDREF(*aPrincipal
= mPrincipal
);
78 RequestBase::GetResultCode(nsresult
* aResultCode
) {
79 AssertIsOnOwningThread();
80 MOZ_ASSERT(aResultCode
);
82 if (!mHaveResultOrErrorCode
) {
83 return NS_ERROR_FAILURE
;
86 *aResultCode
= mResultCode
;
91 RequestBase::GetResultName(nsACString
& aResultName
) {
92 AssertIsOnOwningThread();
94 if (!mHaveResultOrErrorCode
) {
95 return NS_ERROR_FAILURE
;
98 mozilla::GetErrorName(mResultCode
, aResultName
);
102 UsageRequest::UsageRequest(nsIQuotaUsageCallback
* aCallback
)
103 : mCallback(aCallback
), mBackgroundActor(nullptr), mCanceled(false) {
104 AssertIsOnOwningThread();
105 MOZ_ASSERT(aCallback
);
108 UsageRequest::UsageRequest(nsIPrincipal
* aPrincipal
,
109 nsIQuotaUsageCallback
* aCallback
)
110 : RequestBase(aPrincipal
),
111 mCallback(aCallback
),
112 mBackgroundActor(nullptr),
114 AssertIsOnOwningThread();
115 MOZ_ASSERT(aPrincipal
);
116 MOZ_ASSERT(aCallback
);
119 UsageRequest::~UsageRequest() { AssertIsOnOwningThread(); }
121 void UsageRequest::SetBackgroundActor(
122 QuotaUsageRequestChild
* aBackgroundActor
) {
123 AssertIsOnOwningThread();
124 MOZ_ASSERT(aBackgroundActor
);
125 MOZ_ASSERT(!mBackgroundActor
);
127 mBackgroundActor
= aBackgroundActor
;
130 mBackgroundActor
->SendCancel();
134 void UsageRequest::SetResult(nsIVariant
* aResult
) {
135 AssertIsOnOwningThread();
137 MOZ_ASSERT(!mHaveResultOrErrorCode
);
141 mHaveResultOrErrorCode
= true;
146 NS_IMPL_CYCLE_COLLECTION_INHERITED(UsageRequest
, RequestBase
, mCallback
)
148 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(UsageRequest
)
149 NS_INTERFACE_MAP_ENTRY(nsIQuotaUsageRequest
)
150 NS_INTERFACE_MAP_END_INHERITING(RequestBase
)
152 NS_IMPL_ADDREF_INHERITED(UsageRequest
, RequestBase
)
153 NS_IMPL_RELEASE_INHERITED(UsageRequest
, RequestBase
)
156 UsageRequest::GetResult(nsIVariant
** aResult
) {
157 AssertIsOnOwningThread();
160 if (!mHaveResultOrErrorCode
) {
161 return NS_ERROR_FAILURE
;
164 NS_IF_ADDREF(*aResult
= mResult
);
169 UsageRequest::GetCallback(nsIQuotaUsageCallback
** aCallback
) {
170 AssertIsOnOwningThread();
171 MOZ_ASSERT(aCallback
);
173 NS_IF_ADDREF(*aCallback
= mCallback
);
178 UsageRequest::SetCallback(nsIQuotaUsageCallback
* aCallback
) {
179 AssertIsOnOwningThread();
181 mCallback
= aCallback
;
186 UsageRequest::Cancel() {
187 AssertIsOnOwningThread();
190 NS_WARNING("Canceled more than once?!");
191 return NS_ERROR_UNEXPECTED
;
194 if (mBackgroundActor
) {
195 mBackgroundActor
->SendCancel();
203 void UsageRequest::FireCallback() {
204 AssertIsOnOwningThread();
205 MOZ_ASSERT(mCallback
);
207 mCallback
->OnUsageResult(this);
213 Request::Request() { AssertIsOnOwningThread(); }
215 Request::Request(nsIPrincipal
* aPrincipal
) : RequestBase(aPrincipal
) {
216 AssertIsOnOwningThread();
217 MOZ_ASSERT(aPrincipal
);
220 Request::Request(nsIQuotaCallback
* aCallback
) : mCallback(aCallback
) {
221 AssertIsOnOwningThread();
222 MOZ_ASSERT(aCallback
);
225 Request::~Request() { AssertIsOnOwningThread(); }
227 void Request::SetResult(nsIVariant
* aResult
) {
228 AssertIsOnOwningThread();
230 MOZ_ASSERT(!mHaveResultOrErrorCode
);
234 mHaveResultOrErrorCode
= true;
240 Request::GetResult(nsIVariant
** aResult
) {
241 AssertIsOnOwningThread();
244 if (!mHaveResultOrErrorCode
) {
245 return NS_ERROR_FAILURE
;
248 NS_IF_ADDREF(*aResult
= mResult
);
252 NS_IMPL_CYCLE_COLLECTION_INHERITED(Request
, RequestBase
, mCallback
, mResult
)
254 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Request
)
255 NS_INTERFACE_MAP_ENTRY(nsIQuotaRequest
)
256 NS_INTERFACE_MAP_END_INHERITING(RequestBase
)
258 NS_IMPL_ADDREF_INHERITED(mozilla::dom::quota::Request
, RequestBase
)
259 NS_IMPL_RELEASE_INHERITED(mozilla::dom::quota::Request
, RequestBase
)
262 Request::GetCallback(nsIQuotaCallback
** aCallback
) {
263 AssertIsOnOwningThread();
264 MOZ_ASSERT(aCallback
);
266 NS_IF_ADDREF(*aCallback
= mCallback
);
271 Request::SetCallback(nsIQuotaCallback
* aCallback
) {
272 AssertIsOnOwningThread();
274 mCallback
= aCallback
;
278 void Request::FireCallback() {
279 AssertIsOnOwningThread();
282 mCallback
->OnComplete(this);
289 } // namespace mozilla::dom::quota