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"
26 namespace mozilla::dom::quota
{
28 RequestBase::RequestBase() : mResultCode(NS_OK
), mHaveResultOrErrorCode(false) {
29 AssertIsOnOwningThread();
32 RequestBase::RequestBase(nsIPrincipal
* aPrincipal
)
33 : mPrincipal(aPrincipal
),
35 mHaveResultOrErrorCode(false) {
36 AssertIsOnOwningThread();
41 void RequestBase::AssertIsOnOwningThread() const {
42 NS_ASSERT_OWNINGTHREAD(RequestBase
);
47 void RequestBase::SetError(nsresult aRv
) {
48 AssertIsOnOwningThread();
49 MOZ_ASSERT(mResultCode
== NS_OK
);
50 MOZ_ASSERT(!mHaveResultOrErrorCode
);
53 mHaveResultOrErrorCode
= true;
58 NS_IMPL_CYCLE_COLLECTION_0(RequestBase
)
60 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(RequestBase
)
61 NS_INTERFACE_MAP_ENTRY(nsISupports
)
64 NS_IMPL_CYCLE_COLLECTING_ADDREF(RequestBase
)
65 NS_IMPL_CYCLE_COLLECTING_RELEASE(RequestBase
)
68 RequestBase::GetPrincipal(nsIPrincipal
** aPrincipal
) {
69 AssertIsOnOwningThread();
70 MOZ_ASSERT(aPrincipal
);
72 NS_IF_ADDREF(*aPrincipal
= mPrincipal
);
77 RequestBase::GetResultCode(nsresult
* aResultCode
) {
78 AssertIsOnOwningThread();
79 MOZ_ASSERT(aResultCode
);
81 if (!mHaveResultOrErrorCode
) {
82 return NS_ERROR_FAILURE
;
85 *aResultCode
= mResultCode
;
90 RequestBase::GetResultName(nsACString
& aResultName
) {
91 AssertIsOnOwningThread();
93 if (!mHaveResultOrErrorCode
) {
94 return NS_ERROR_FAILURE
;
97 mozilla::GetErrorName(mResultCode
, aResultName
);
101 UsageRequest::UsageRequest(nsIQuotaUsageCallback
* aCallback
)
102 : mCallback(aCallback
), mBackgroundActor(nullptr), mCanceled(false) {
103 AssertIsOnOwningThread();
104 MOZ_ASSERT(aCallback
);
107 UsageRequest::UsageRequest(nsIPrincipal
* aPrincipal
,
108 nsIQuotaUsageCallback
* aCallback
)
109 : RequestBase(aPrincipal
),
110 mCallback(aCallback
),
111 mBackgroundActor(nullptr),
113 AssertIsOnOwningThread();
114 MOZ_ASSERT(aPrincipal
);
115 MOZ_ASSERT(aCallback
);
118 UsageRequest::~UsageRequest() { AssertIsOnOwningThread(); }
120 void UsageRequest::SetBackgroundActor(
121 QuotaUsageRequestChild
* aBackgroundActor
) {
122 AssertIsOnOwningThread();
123 MOZ_ASSERT(aBackgroundActor
);
124 MOZ_ASSERT(!mBackgroundActor
);
126 mBackgroundActor
= aBackgroundActor
;
129 mBackgroundActor
->SendCancel();
133 void UsageRequest::SetResult(nsIVariant
* aResult
) {
134 AssertIsOnOwningThread();
136 MOZ_ASSERT(!mHaveResultOrErrorCode
);
140 mHaveResultOrErrorCode
= true;
145 NS_IMPL_CYCLE_COLLECTION_INHERITED(UsageRequest
, RequestBase
, mCallback
)
147 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(UsageRequest
)
148 NS_INTERFACE_MAP_ENTRY(nsIQuotaUsageRequest
)
149 NS_INTERFACE_MAP_END_INHERITING(RequestBase
)
151 NS_IMPL_ADDREF_INHERITED(UsageRequest
, RequestBase
)
152 NS_IMPL_RELEASE_INHERITED(UsageRequest
, RequestBase
)
155 UsageRequest::GetResult(nsIVariant
** aResult
) {
156 AssertIsOnOwningThread();
159 if (!mHaveResultOrErrorCode
) {
160 return NS_ERROR_FAILURE
;
163 NS_IF_ADDREF(*aResult
= mResult
);
168 UsageRequest::GetCallback(nsIQuotaUsageCallback
** aCallback
) {
169 AssertIsOnOwningThread();
170 MOZ_ASSERT(aCallback
);
172 NS_IF_ADDREF(*aCallback
= mCallback
);
177 UsageRequest::SetCallback(nsIQuotaUsageCallback
* aCallback
) {
178 AssertIsOnOwningThread();
180 mCallback
= aCallback
;
185 UsageRequest::Cancel() {
186 AssertIsOnOwningThread();
189 NS_WARNING("Canceled more than once?!");
190 return NS_ERROR_UNEXPECTED
;
193 if (mBackgroundActor
) {
194 mBackgroundActor
->SendCancel();
202 void UsageRequest::FireCallback() {
203 AssertIsOnOwningThread();
204 MOZ_ASSERT(mCallback
);
206 mCallback
->OnUsageResult(this);
212 Request::Request() { AssertIsOnOwningThread(); }
214 Request::Request(nsIPrincipal
* aPrincipal
) : RequestBase(aPrincipal
) {
215 AssertIsOnOwningThread();
216 MOZ_ASSERT(aPrincipal
);
219 Request::Request(nsIQuotaCallback
* aCallback
) : mCallback(aCallback
) {
220 AssertIsOnOwningThread();
221 MOZ_ASSERT(aCallback
);
224 Request::~Request() { AssertIsOnOwningThread(); }
226 void Request::SetResult(nsIVariant
* aResult
) {
227 AssertIsOnOwningThread();
229 MOZ_ASSERT(!mHaveResultOrErrorCode
);
233 mHaveResultOrErrorCode
= true;
239 Request::GetResult(nsIVariant
** aResult
) {
240 AssertIsOnOwningThread();
243 if (!mHaveResultOrErrorCode
) {
244 return NS_ERROR_FAILURE
;
247 NS_IF_ADDREF(*aResult
= mResult
);
251 NS_IMPL_CYCLE_COLLECTION_INHERITED(Request
, RequestBase
, mCallback
, mResult
)
253 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Request
)
254 NS_INTERFACE_MAP_ENTRY(nsIQuotaRequest
)
255 NS_INTERFACE_MAP_END_INHERITING(RequestBase
)
257 NS_IMPL_ADDREF_INHERITED(mozilla::dom::quota::Request
, RequestBase
)
258 NS_IMPL_RELEASE_INHERITED(mozilla::dom::quota::Request
, RequestBase
)
261 Request::GetCallback(nsIQuotaCallback
** aCallback
) {
262 AssertIsOnOwningThread();
263 MOZ_ASSERT(aCallback
);
265 NS_IF_ADDREF(*aCallback
= mCallback
);
270 Request::SetCallback(nsIQuotaCallback
* aCallback
) {
271 AssertIsOnOwningThread();
273 mCallback
= aCallback
;
277 void Request::FireCallback() {
278 AssertIsOnOwningThread();
281 mCallback
->OnComplete(this);
288 } // namespace mozilla::dom::quota