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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
/* -*- 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 "StorageManager.h"
#include "mozilla/dom/PromiseWorkerProxy.h"
#include "mozilla/dom/quota/QuotaManagerService.h"
#include "mozilla/dom/StorageManagerBinding.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/ErrorResult.h"
#include "nsIQuotaCallbacks.h"
#include "nsIQuotaRequests.h"
#include "nsIQuotaResults.h"
#include "nsPIDOMWindow.h"
#include "QuotaManagerService.h"
using namespace mozilla::dom::workers;
using namespace mozilla::dom::quota;
namespace mozilla {
namespace dom {
namespace {
// This class is used to get quota usage callback.
class EstimateResolver final
: public nsIQuotaUsageCallback
{
class FinishWorkerRunnable;
// If this resolver was created for a window then mPromise must be non-null.
// Otherwise mProxy must be non-null.
RefPtr<Promise> mPromise;
RefPtr<PromiseWorkerProxy> mProxy;
nsresult mResultCode;
StorageEstimate mStorageEstimate;
public:
explicit EstimateResolver(Promise* aPromise)
: mPromise(aPromise)
, mResultCode(NS_OK)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aPromise);
}
explicit EstimateResolver(PromiseWorkerProxy* aProxy)
: mProxy(aProxy)
, mResultCode(NS_OK)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aProxy);
}
void
ResolveOrReject(Promise* aPromise);
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIQUOTAUSAGECALLBACK
private:
~EstimateResolver()
{ }
};
// This class is used to return promise on worker thread.
class EstimateResolver::FinishWorkerRunnable final
: public WorkerRunnable
{
RefPtr<EstimateResolver> mResolver;
public:
explicit FinishWorkerRunnable(EstimateResolver* aResolver)
: WorkerRunnable(aResolver->mProxy->GetWorkerPrivate())
, mResolver(aResolver)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aResolver);
}
virtual bool
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
};
class EstimateWorkerMainThreadRunnable
: public WorkerMainThreadRunnable
{
RefPtr<PromiseWorkerProxy> mProxy;
public:
EstimateWorkerMainThreadRunnable(WorkerPrivate* aWorkerPrivate,
PromiseWorkerProxy* aProxy)
: WorkerMainThreadRunnable(aWorkerPrivate,
NS_LITERAL_CSTRING("StorageManager :: Estimate"))
, mProxy(aProxy)
{
MOZ_ASSERT(aWorkerPrivate);
aWorkerPrivate->AssertIsOnWorkerThread();
MOZ_ASSERT(aProxy);
}
virtual bool
MainThreadRun() override;
};
nsresult
GetUsageForPrincipal(nsIPrincipal* aPrincipal,
nsIQuotaUsageCallback* aCallback,
nsIQuotaUsageRequest** aRequest)
{
MOZ_ASSERT(aPrincipal);
MOZ_ASSERT(aCallback);
MOZ_ASSERT(aRequest);
nsCOMPtr<nsIQuotaManagerService> qms = QuotaManagerService::GetOrCreate();
if (NS_WARN_IF(!qms)) {
return NS_ERROR_FAILURE;
}
nsresult rv = qms->GetUsageForPrincipal(aPrincipal, aCallback, true, aRequest);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
};
nsresult
GetStorageEstimate(nsIQuotaUsageRequest* aRequest,
StorageEstimate& aStorageEstimate)
{
MOZ_ASSERT(aRequest);
nsCOMPtr<nsIVariant> result;
nsresult rv = aRequest->GetResult(getter_AddRefs(result));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
nsID* iid;
nsCOMPtr<nsISupports> supports;
rv = result->GetAsInterface(&iid, getter_AddRefs(supports));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
free(iid);
nsCOMPtr<nsIQuotaOriginUsageResult> originUsageResult =
do_QueryInterface(supports);
MOZ_ASSERT(originUsageResult);
MOZ_ALWAYS_SUCCEEDS(
originUsageResult->GetUsage(&aStorageEstimate.mUsage.Construct()));
MOZ_ALWAYS_SUCCEEDS(
originUsageResult->GetLimit(&aStorageEstimate.mQuota.Construct()));
return NS_OK;
}
} // namespace
/*******************************************************************************
* Local class implementations
******************************************************************************/
void
EstimateResolver::ResolveOrReject(Promise* aPromise)
{
MOZ_ASSERT(aPromise);
if (NS_SUCCEEDED(mResultCode)) {
aPromise->MaybeResolve(mStorageEstimate);
} else {
aPromise->MaybeReject(mResultCode);
}
}
NS_IMPL_ISUPPORTS(EstimateResolver, nsIQuotaUsageCallback)
NS_IMETHODIMP
EstimateResolver::OnUsageResult(nsIQuotaUsageRequest *aRequest)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aRequest);
nsresult rv = aRequest->GetResultCode(&mResultCode);
if (NS_WARN_IF(NS_FAILED(rv))) {
mResultCode = rv;
} else if (NS_SUCCEEDED(mResultCode)) {
rv = GetStorageEstimate(aRequest, mStorageEstimate);
if (NS_WARN_IF(NS_FAILED(rv))) {
mResultCode = rv;
}
}
// In a main thread request.
if (!mProxy) {
MOZ_ASSERT(mPromise);
ResolveOrReject(mPromise);
return NS_OK;
}
// In a worker thread request.
MutexAutoLock lock(mProxy->Lock());
if (NS_WARN_IF(mProxy->CleanedUp())) {
return NS_ERROR_FAILURE;
}
RefPtr<FinishWorkerRunnable> runnable = new FinishWorkerRunnable(this);
if (NS_WARN_IF(!runnable->Dispatch())) {
return NS_ERROR_FAILURE;
}
return NS_OK;
}
bool
EstimateResolver::
FinishWorkerRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
{
MOZ_ASSERT(aWorkerPrivate);
aWorkerPrivate->AssertIsOnWorkerThread();
RefPtr<PromiseWorkerProxy> proxy = mResolver->mProxy;
MOZ_ASSERT(proxy);
RefPtr<Promise> promise = proxy->WorkerPromise();
MOZ_ASSERT(promise);
mResolver->ResolveOrReject(promise);
proxy->CleanUp();
return true;
}
bool
EstimateWorkerMainThreadRunnable::MainThreadRun()
{
MOZ_ASSERT(NS_IsMainThread());
nsCOMPtr<nsIPrincipal> principal;
{
MutexAutoLock lock(mProxy->Lock());
if (mProxy->CleanedUp()) {
return true;
}
principal = mProxy->GetWorkerPrivate()->GetPrincipal();
}
MOZ_ASSERT(principal);
RefPtr<EstimateResolver> resolver = new EstimateResolver(mProxy);
RefPtr<nsIQuotaUsageRequest> request;
nsresult rv =
GetUsageForPrincipal(principal, resolver, getter_AddRefs(request));
if (NS_WARN_IF(NS_FAILED(rv))) {
return false;
}
return true;
}
/*******************************************************************************
* StorageManager
******************************************************************************/
StorageManager::StorageManager(nsIGlobalObject* aGlobal)
: mOwner(aGlobal)
{
MOZ_ASSERT(aGlobal);
}
StorageManager::~StorageManager()
{
}
already_AddRefed<Promise>
StorageManager::Estimate(ErrorResult& aRv)
{
MOZ_ASSERT(mOwner);
RefPtr<Promise> promise = Promise::Create(mOwner, aRv);
if (NS_WARN_IF(!promise)) {
return nullptr;
}
if (NS_IsMainThread()) {
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(mOwner);
if (NS_WARN_IF(!window)) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
nsCOMPtr<nsIDocument> doc = window->GetExtantDoc();
if (NS_WARN_IF(!doc)) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
nsCOMPtr<nsIPrincipal> principal = doc->NodePrincipal();
MOZ_ASSERT(principal);
RefPtr<EstimateResolver> resolver = new EstimateResolver(promise);
RefPtr<nsIQuotaUsageRequest> request;
nsresult rv =
GetUsageForPrincipal(principal, resolver, getter_AddRefs(request));
if (NS_WARN_IF(NS_FAILED(rv))) {
aRv.Throw(rv);
return nullptr;
}
return promise.forget();
}
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
MOZ_ASSERT(workerPrivate);
RefPtr<PromiseWorkerProxy> promiseProxy =
PromiseWorkerProxy::Create(workerPrivate, promise);
if (NS_WARN_IF(!promiseProxy)) {
return nullptr;
}
RefPtr<EstimateWorkerMainThreadRunnable> runnnable =
new EstimateWorkerMainThreadRunnable(promiseProxy->GetWorkerPrivate(),
promiseProxy);
runnnable->Dispatch(Terminating, aRv);
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
return promise.forget();
}
// static
bool
StorageManager::PrefEnabled(JSContext* aCx, JSObject* aObj)
{
if (NS_IsMainThread()) {
return Preferences::GetBool("dom.storageManager.enabled");
}
WorkerPrivate* workerPrivate = GetWorkerPrivateFromContext(aCx);
MOZ_ASSERT(workerPrivate);
return workerPrivate->StorageManagerEnabled();
}
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(StorageManager, mOwner)
NS_IMPL_CYCLE_COLLECTING_ADDREF(StorageManager)
NS_IMPL_CYCLE_COLLECTING_RELEASE(StorageManager)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(StorageManager)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
JSObject*
StorageManager::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto)
{
return StorageManagerBinding::Wrap(aCx, this, aGivenProto);
}
} // namespace dom
} // namespace mozilla
|