Bug 1729469 [wpt PR 30378] - Stretch nested columns to the block-size of the outer...
[gecko.git] / dom / quota / QuotaRequests.cpp
blobee781831c8115e8db299114d175536b94cccbe2d
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"
22 namespace mozilla {
23 class Runnable;
26 namespace mozilla::dom::quota {
28 RequestBase::RequestBase() : mResultCode(NS_OK), mHaveResultOrErrorCode(false) {
29 AssertIsOnOwningThread();
32 RequestBase::RequestBase(nsIPrincipal* aPrincipal)
33 : mPrincipal(aPrincipal),
34 mResultCode(NS_OK),
35 mHaveResultOrErrorCode(false) {
36 AssertIsOnOwningThread();
39 #ifdef DEBUG
41 void RequestBase::AssertIsOnOwningThread() const {
42 NS_ASSERT_OWNINGTHREAD(RequestBase);
45 #endif // DEBUG
47 void RequestBase::SetError(nsresult aRv) {
48 AssertIsOnOwningThread();
49 MOZ_ASSERT(mResultCode == NS_OK);
50 MOZ_ASSERT(!mHaveResultOrErrorCode);
52 mResultCode = aRv;
53 mHaveResultOrErrorCode = true;
55 FireCallback();
58 NS_IMPL_CYCLE_COLLECTION_0(RequestBase)
60 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(RequestBase)
61 NS_INTERFACE_MAP_ENTRY(nsISupports)
62 NS_INTERFACE_MAP_END
64 NS_IMPL_CYCLE_COLLECTING_ADDREF(RequestBase)
65 NS_IMPL_CYCLE_COLLECTING_RELEASE(RequestBase)
67 NS_IMETHODIMP
68 RequestBase::GetPrincipal(nsIPrincipal** aPrincipal) {
69 AssertIsOnOwningThread();
70 MOZ_ASSERT(aPrincipal);
72 NS_IF_ADDREF(*aPrincipal = mPrincipal);
73 return NS_OK;
76 NS_IMETHODIMP
77 RequestBase::GetResultCode(nsresult* aResultCode) {
78 AssertIsOnOwningThread();
79 MOZ_ASSERT(aResultCode);
81 if (!mHaveResultOrErrorCode) {
82 return NS_ERROR_FAILURE;
85 *aResultCode = mResultCode;
86 return NS_OK;
89 NS_IMETHODIMP
90 RequestBase::GetResultName(nsACString& aResultName) {
91 AssertIsOnOwningThread();
93 if (!mHaveResultOrErrorCode) {
94 return NS_ERROR_FAILURE;
97 mozilla::GetErrorName(mResultCode, aResultName);
98 return NS_OK;
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),
112 mCanceled(false) {
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;
128 if (mCanceled) {
129 mBackgroundActor->SendCancel();
133 void UsageRequest::SetResult(nsIVariant* aResult) {
134 AssertIsOnOwningThread();
135 MOZ_ASSERT(aResult);
136 MOZ_ASSERT(!mHaveResultOrErrorCode);
138 mResult = aResult;
140 mHaveResultOrErrorCode = true;
142 FireCallback();
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)
154 NS_IMETHODIMP
155 UsageRequest::GetResult(nsIVariant** aResult) {
156 AssertIsOnOwningThread();
157 MOZ_ASSERT(aResult);
159 if (!mHaveResultOrErrorCode) {
160 return NS_ERROR_FAILURE;
163 NS_IF_ADDREF(*aResult = mResult);
164 return NS_OK;
167 NS_IMETHODIMP
168 UsageRequest::GetCallback(nsIQuotaUsageCallback** aCallback) {
169 AssertIsOnOwningThread();
170 MOZ_ASSERT(aCallback);
172 NS_IF_ADDREF(*aCallback = mCallback);
173 return NS_OK;
176 NS_IMETHODIMP
177 UsageRequest::SetCallback(nsIQuotaUsageCallback* aCallback) {
178 AssertIsOnOwningThread();
180 mCallback = aCallback;
181 return NS_OK;
184 NS_IMETHODIMP
185 UsageRequest::Cancel() {
186 AssertIsOnOwningThread();
188 if (mCanceled) {
189 NS_WARNING("Canceled more than once?!");
190 return NS_ERROR_UNEXPECTED;
193 if (mBackgroundActor) {
194 mBackgroundActor->SendCancel();
197 mCanceled = true;
199 return NS_OK;
202 void UsageRequest::FireCallback() {
203 AssertIsOnOwningThread();
204 MOZ_ASSERT(mCallback);
206 mCallback->OnUsageResult(this);
208 // Clean up.
209 mCallback = nullptr;
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();
228 MOZ_ASSERT(aResult);
229 MOZ_ASSERT(!mHaveResultOrErrorCode);
231 mResult = aResult;
233 mHaveResultOrErrorCode = true;
235 FireCallback();
238 NS_IMETHODIMP
239 Request::GetResult(nsIVariant** aResult) {
240 AssertIsOnOwningThread();
241 MOZ_ASSERT(aResult);
243 if (!mHaveResultOrErrorCode) {
244 return NS_ERROR_FAILURE;
247 NS_IF_ADDREF(*aResult = mResult);
248 return NS_OK;
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)
260 NS_IMETHODIMP
261 Request::GetCallback(nsIQuotaCallback** aCallback) {
262 AssertIsOnOwningThread();
263 MOZ_ASSERT(aCallback);
265 NS_IF_ADDREF(*aCallback = mCallback);
266 return NS_OK;
269 NS_IMETHODIMP
270 Request::SetCallback(nsIQuotaCallback* aCallback) {
271 AssertIsOnOwningThread();
273 mCallback = aCallback;
274 return NS_OK;
277 void Request::FireCallback() {
278 AssertIsOnOwningThread();
280 if (mCallback) {
281 mCallback->OnComplete(this);
283 // Clean up.
284 mCallback = nullptr;
288 } // namespace mozilla::dom::quota