Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / dom / quota / QuotaRequests.cpp
blob41991b16cd54be6d4db49f1a474af938ea53f153
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"
9 // Local includes
10 #include "ActorsChild.h"
12 // Global includes
13 #include "mozilla/ErrorNames.h"
14 #include "mozilla/MacroForEach.h"
15 #include "nsDebug.h"
16 #include "nsIQuotaCallbacks.h"
17 #include "nsISupportsUtils.h"
18 #include "nsIVariant.h"
19 #include "nsStringFwd.h"
20 #include "nscore.h"
21 #include "mozilla/dom/quota/QuotaUsageRequestChild.h"
23 namespace mozilla {
24 class Runnable;
27 namespace mozilla::dom::quota {
29 RequestBase::RequestBase() : mResultCode(NS_OK), mHaveResultOrErrorCode(false) {
30 AssertIsOnOwningThread();
33 RequestBase::RequestBase(nsIPrincipal* aPrincipal)
34 : mPrincipal(aPrincipal),
35 mResultCode(NS_OK),
36 mHaveResultOrErrorCode(false) {
37 AssertIsOnOwningThread();
40 #ifdef DEBUG
42 void RequestBase::AssertIsOnOwningThread() const {
43 NS_ASSERT_OWNINGTHREAD(RequestBase);
46 #endif // DEBUG
48 void RequestBase::SetError(nsresult aRv) {
49 AssertIsOnOwningThread();
50 MOZ_ASSERT(mResultCode == NS_OK);
51 MOZ_ASSERT(!mHaveResultOrErrorCode);
53 mResultCode = aRv;
54 mHaveResultOrErrorCode = true;
56 FireCallback();
59 NS_IMPL_CYCLE_COLLECTION_0(RequestBase)
61 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(RequestBase)
62 NS_INTERFACE_MAP_ENTRY(nsISupports)
63 NS_INTERFACE_MAP_END
65 NS_IMPL_CYCLE_COLLECTING_ADDREF(RequestBase)
66 NS_IMPL_CYCLE_COLLECTING_RELEASE(RequestBase)
68 NS_IMETHODIMP
69 RequestBase::GetPrincipal(nsIPrincipal** aPrincipal) {
70 AssertIsOnOwningThread();
71 MOZ_ASSERT(aPrincipal);
73 NS_IF_ADDREF(*aPrincipal = mPrincipal);
74 return NS_OK;
77 NS_IMETHODIMP
78 RequestBase::GetResultCode(nsresult* aResultCode) {
79 AssertIsOnOwningThread();
80 MOZ_ASSERT(aResultCode);
82 if (!mHaveResultOrErrorCode) {
83 return NS_ERROR_FAILURE;
86 *aResultCode = mResultCode;
87 return NS_OK;
90 NS_IMETHODIMP
91 RequestBase::GetResultName(nsACString& aResultName) {
92 AssertIsOnOwningThread();
94 if (!mHaveResultOrErrorCode) {
95 return NS_ERROR_FAILURE;
98 mozilla::GetErrorName(mResultCode, aResultName);
99 return NS_OK;
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),
113 mCanceled(false) {
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;
129 if (mCanceled) {
130 mBackgroundActor->SendCancel();
134 void UsageRequest::SetResult(nsIVariant* aResult) {
135 AssertIsOnOwningThread();
136 MOZ_ASSERT(aResult);
137 MOZ_ASSERT(!mHaveResultOrErrorCode);
139 mResult = aResult;
141 mHaveResultOrErrorCode = true;
143 FireCallback();
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)
155 NS_IMETHODIMP
156 UsageRequest::GetResult(nsIVariant** aResult) {
157 AssertIsOnOwningThread();
158 MOZ_ASSERT(aResult);
160 if (!mHaveResultOrErrorCode) {
161 return NS_ERROR_FAILURE;
164 NS_IF_ADDREF(*aResult = mResult);
165 return NS_OK;
168 NS_IMETHODIMP
169 UsageRequest::GetCallback(nsIQuotaUsageCallback** aCallback) {
170 AssertIsOnOwningThread();
171 MOZ_ASSERT(aCallback);
173 NS_IF_ADDREF(*aCallback = mCallback);
174 return NS_OK;
177 NS_IMETHODIMP
178 UsageRequest::SetCallback(nsIQuotaUsageCallback* aCallback) {
179 AssertIsOnOwningThread();
181 mCallback = aCallback;
182 return NS_OK;
185 NS_IMETHODIMP
186 UsageRequest::Cancel() {
187 AssertIsOnOwningThread();
189 if (mCanceled) {
190 NS_WARNING("Canceled more than once?!");
191 return NS_ERROR_UNEXPECTED;
194 if (mBackgroundActor) {
195 mBackgroundActor->SendCancel();
198 mCanceled = true;
200 return NS_OK;
203 void UsageRequest::FireCallback() {
204 AssertIsOnOwningThread();
205 MOZ_ASSERT(mCallback);
207 mCallback->OnUsageResult(this);
209 // Clean up.
210 mCallback = nullptr;
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();
229 MOZ_ASSERT(aResult);
230 MOZ_ASSERT(!mHaveResultOrErrorCode);
232 mResult = aResult;
234 mHaveResultOrErrorCode = true;
236 FireCallback();
239 NS_IMETHODIMP
240 Request::GetResult(nsIVariant** aResult) {
241 AssertIsOnOwningThread();
242 MOZ_ASSERT(aResult);
244 if (!mHaveResultOrErrorCode) {
245 return NS_ERROR_FAILURE;
248 NS_IF_ADDREF(*aResult = mResult);
249 return NS_OK;
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)
261 NS_IMETHODIMP
262 Request::GetCallback(nsIQuotaCallback** aCallback) {
263 AssertIsOnOwningThread();
264 MOZ_ASSERT(aCallback);
266 NS_IF_ADDREF(*aCallback = mCallback);
267 return NS_OK;
270 NS_IMETHODIMP
271 Request::SetCallback(nsIQuotaCallback* aCallback) {
272 AssertIsOnOwningThread();
274 mCallback = aCallback;
275 return NS_OK;
278 void Request::FireCallback() {
279 AssertIsOnOwningThread();
281 if (mCallback) {
282 mCallback->OnComplete(this);
284 // Clean up.
285 mCallback = nullptr;
289 } // namespace mozilla::dom::quota