summaryrefslogtreecommitdiffstats
path: root/dom/quota/QuotaRequests.cpp
blob: ee781831c8115e8db299114d175536b94cccbe2d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "QuotaRequests.h"

// Local includes
#include "ActorsChild.h"

// Global includes
#include "mozilla/ErrorNames.h"
#include "mozilla/MacroForEach.h"
#include "nsDebug.h"
#include "nsIQuotaCallbacks.h"
#include "nsISupportsUtils.h"
#include "nsIVariant.h"
#include "nsStringFwd.h"
#include "nscore.h"

namespace mozilla {
class Runnable;
}

namespace mozilla::dom::quota {

RequestBase::RequestBase() : mResultCode(NS_OK), mHaveResultOrErrorCode(false) {
  AssertIsOnOwningThread();
}

RequestBase::RequestBase(nsIPrincipal* aPrincipal)
    : mPrincipal(aPrincipal),
      mResultCode(NS_OK),
      mHaveResultOrErrorCode(false) {
  AssertIsOnOwningThread();
}

#ifdef DEBUG

void RequestBase::AssertIsOnOwningThread() const {
  NS_ASSERT_OWNINGTHREAD(RequestBase);
}

#endif  // DEBUG

void RequestBase::SetError(nsresult aRv) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(mResultCode == NS_OK);
  MOZ_ASSERT(!mHaveResultOrErrorCode);

  mResultCode = aRv;
  mHaveResultOrErrorCode = true;

  FireCallback();
}

NS_IMPL_CYCLE_COLLECTION_0(RequestBase)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(RequestBase)
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(RequestBase)
NS_IMPL_CYCLE_COLLECTING_RELEASE(RequestBase)

NS_IMETHODIMP
RequestBase::GetPrincipal(nsIPrincipal** aPrincipal) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aPrincipal);

  NS_IF_ADDREF(*aPrincipal = mPrincipal);
  return NS_OK;
}

NS_IMETHODIMP
RequestBase::GetResultCode(nsresult* aResultCode) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aResultCode);

  if (!mHaveResultOrErrorCode) {
    return NS_ERROR_FAILURE;
  }

  *aResultCode = mResultCode;
  return NS_OK;
}

NS_IMETHODIMP
RequestBase::GetResultName(nsACString& aResultName) {
  AssertIsOnOwningThread();

  if (!mHaveResultOrErrorCode) {
    return NS_ERROR_FAILURE;
  }

  mozilla::GetErrorName(mResultCode, aResultName);
  return NS_OK;
}

UsageRequest::UsageRequest(nsIQuotaUsageCallback* aCallback)
    : mCallback(aCallback), mBackgroundActor(nullptr), mCanceled(false) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aCallback);
}

UsageRequest::UsageRequest(nsIPrincipal* aPrincipal,
                           nsIQuotaUsageCallback* aCallback)
    : RequestBase(aPrincipal),
      mCallback(aCallback),
      mBackgroundActor(nullptr),
      mCanceled(false) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aPrincipal);
  MOZ_ASSERT(aCallback);
}

UsageRequest::~UsageRequest() { AssertIsOnOwningThread(); }

void UsageRequest::SetBackgroundActor(
    QuotaUsageRequestChild* aBackgroundActor) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aBackgroundActor);
  MOZ_ASSERT(!mBackgroundActor);

  mBackgroundActor = aBackgroundActor;

  if (mCanceled) {
    mBackgroundActor->SendCancel();
  }
}

void UsageRequest::SetResult(nsIVariant* aResult) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aResult);
  MOZ_ASSERT(!mHaveResultOrErrorCode);

  mResult = aResult;

  mHaveResultOrErrorCode = true;

  FireCallback();
}

NS_IMPL_CYCLE_COLLECTION_INHERITED(UsageRequest, RequestBase, mCallback)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(UsageRequest)
  NS_INTERFACE_MAP_ENTRY(nsIQuotaUsageRequest)
NS_INTERFACE_MAP_END_INHERITING(RequestBase)

NS_IMPL_ADDREF_INHERITED(UsageRequest, RequestBase)
NS_IMPL_RELEASE_INHERITED(UsageRequest, RequestBase)

NS_IMETHODIMP
UsageRequest::GetResult(nsIVariant** aResult) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aResult);

  if (!mHaveResultOrErrorCode) {
    return NS_ERROR_FAILURE;
  }

  NS_IF_ADDREF(*aResult = mResult);
  return NS_OK;
}

NS_IMETHODIMP
UsageRequest::GetCallback(nsIQuotaUsageCallback** aCallback) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aCallback);

  NS_IF_ADDREF(*aCallback = mCallback);
  return NS_OK;
}

NS_IMETHODIMP
UsageRequest::SetCallback(nsIQuotaUsageCallback* aCallback) {
  AssertIsOnOwningThread();

  mCallback = aCallback;
  return NS_OK;
}

NS_IMETHODIMP
UsageRequest::Cancel() {
  AssertIsOnOwningThread();

  if (mCanceled) {
    NS_WARNING("Canceled more than once?!");
    return NS_ERROR_UNEXPECTED;
  }

  if (mBackgroundActor) {
    mBackgroundActor->SendCancel();
  }

  mCanceled = true;

  return NS_OK;
}

void UsageRequest::FireCallback() {
  AssertIsOnOwningThread();
  MOZ_ASSERT(mCallback);

  mCallback->OnUsageResult(this);

  // Clean up.
  mCallback = nullptr;
}

Request::Request() { AssertIsOnOwningThread(); }

Request::Request(nsIPrincipal* aPrincipal) : RequestBase(aPrincipal) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aPrincipal);
}

Request::Request(nsIQuotaCallback* aCallback) : mCallback(aCallback) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aCallback);
}

Request::~Request() { AssertIsOnOwningThread(); }

void Request::SetResult(nsIVariant* aResult) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aResult);
  MOZ_ASSERT(!mHaveResultOrErrorCode);

  mResult = aResult;

  mHaveResultOrErrorCode = true;

  FireCallback();
}

NS_IMETHODIMP
Request::GetResult(nsIVariant** aResult) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aResult);

  if (!mHaveResultOrErrorCode) {
    return NS_ERROR_FAILURE;
  }

  NS_IF_ADDREF(*aResult = mResult);
  return NS_OK;
}

NS_IMPL_CYCLE_COLLECTION_INHERITED(Request, RequestBase, mCallback, mResult)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Request)
  NS_INTERFACE_MAP_ENTRY(nsIQuotaRequest)
NS_INTERFACE_MAP_END_INHERITING(RequestBase)

NS_IMPL_ADDREF_INHERITED(mozilla::dom::quota::Request, RequestBase)
NS_IMPL_RELEASE_INHERITED(mozilla::dom::quota::Request, RequestBase)

NS_IMETHODIMP
Request::GetCallback(nsIQuotaCallback** aCallback) {
  AssertIsOnOwningThread();
  MOZ_ASSERT(aCallback);

  NS_IF_ADDREF(*aCallback = mCallback);
  return NS_OK;
}

NS_IMETHODIMP
Request::SetCallback(nsIQuotaCallback* aCallback) {
  AssertIsOnOwningThread();

  mCallback = aCallback;
  return NS_OK;
}

void Request::FireCallback() {
  AssertIsOnOwningThread();

  if (mCallback) {
    mCallback->OnComplete(this);

    // Clean up.
    mCallback = nullptr;
  }
}

}  // namespace mozilla::dom::quota