forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 1
/
StorageManager.cpp
781 lines (607 loc) · 20.8 KB
/
StorageManager.cpp
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/* -*- 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 <cstdint>
#include <cstdlib>
#include <utility>
#include "ErrorList.h"
#include "MainThreadUtils.h"
#include "js/CallArgs.h"
#include "js/TypeDecls.h"
#include "mozilla/Attributes.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/MacroForEach.h"
#include "mozilla/Maybe.h"
#include "mozilla/Mutex.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Telemetry.h"
#include "mozilla/TelemetryScalarEnums.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/PromiseWorkerProxy.h"
#include "mozilla/dom/StorageManagerBinding.h"
#include "mozilla/dom/WorkerCommon.h"
#include "mozilla/dom/WorkerPrivate.h"
#include "mozilla/dom/WorkerRunnable.h"
#include "mozilla/dom/WorkerStatus.h"
#include "mozilla/dom/quota/QuotaManagerService.h"
#include "nsContentPermissionHelper.h"
#include "nsContentUtils.h"
#include "nsDebug.h"
#include "nsError.h"
#include "nsIGlobalObject.h"
#include "nsIPrincipal.h"
#include "nsIQuotaCallbacks.h"
#include "nsIQuotaManagerService.h"
#include "nsIQuotaRequests.h"
#include "nsIQuotaResults.h"
#include "nsIVariant.h"
#include "nsLiteralString.h"
#include "nsPIDOMWindow.h"
#include "nsString.h"
#include "nsStringFlags.h"
#include "nsTLiteralString.h"
#include "nscore.h"
class JSObject;
struct JSContext;
struct nsID;
namespace mozilla {
class Runnable;
}
using namespace mozilla::dom::quota;
namespace mozilla::dom {
namespace {
// This class is used to get quota usage, request persist and check persisted
// status callbacks.
class RequestResolver final : public nsIQuotaCallback {
public:
enum Type { Estimate, Persist, Persisted };
private:
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;
const Type mType;
bool mPersisted;
public:
RequestResolver(Type aType, Promise* aPromise)
: mPromise(aPromise),
mResultCode(NS_OK),
mType(aType),
mPersisted(false) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aPromise);
}
RequestResolver(Type aType, PromiseWorkerProxy* aProxy)
: mProxy(aProxy), mResultCode(NS_OK), mType(aType), mPersisted(false) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aProxy);
}
void ResolveOrReject();
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIQUOTACALLBACK
private:
~RequestResolver() = default;
nsresult GetStorageEstimate(nsIVariant* aResult);
nsresult GetPersisted(nsIVariant* aResult);
nsresult OnCompleteInternal(nsIQuotaRequest* aRequest);
nsresult Finish();
};
// This class is used to return promise on worker thread.
class RequestResolver::FinishWorkerRunnable final : public WorkerRunnable {
RefPtr<RequestResolver> mResolver;
public:
explicit FinishWorkerRunnable(RequestResolver* aResolver)
: WorkerRunnable(aResolver->mProxy->GetWorkerPrivate()),
mResolver(aResolver) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aResolver);
}
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
};
class EstimateWorkerMainThreadRunnable final : public WorkerMainThreadRunnable {
RefPtr<PromiseWorkerProxy> mProxy;
public:
EstimateWorkerMainThreadRunnable(WorkerPrivate* aWorkerPrivate,
PromiseWorkerProxy* aProxy)
: WorkerMainThreadRunnable(aWorkerPrivate,
"StorageManager :: Estimate"_ns),
mProxy(aProxy) {
MOZ_ASSERT(aWorkerPrivate);
aWorkerPrivate->AssertIsOnWorkerThread();
MOZ_ASSERT(aProxy);
}
bool MainThreadRun() override;
};
class PersistedWorkerMainThreadRunnable final
: public WorkerMainThreadRunnable {
RefPtr<PromiseWorkerProxy> mProxy;
public:
PersistedWorkerMainThreadRunnable(WorkerPrivate* aWorkerPrivate,
PromiseWorkerProxy* aProxy)
: WorkerMainThreadRunnable(aWorkerPrivate,
"StorageManager :: Persisted"_ns),
mProxy(aProxy) {
MOZ_ASSERT(aWorkerPrivate);
aWorkerPrivate->AssertIsOnWorkerThread();
MOZ_ASSERT(aProxy);
}
bool MainThreadRun() override;
};
/*******************************************************************************
* PersistentStoragePermissionRequest
******************************************************************************/
class PersistentStoragePermissionRequest final
: public ContentPermissionRequestBase {
RefPtr<Promise> mPromise;
public:
PersistentStoragePermissionRequest(nsIPrincipal* aPrincipal,
nsPIDOMWindowInner* aWindow,
Promise* aPromise)
: ContentPermissionRequestBase(aPrincipal, aWindow,
"dom.storageManager"_ns,
"persistent-storage"_ns),
mPromise(aPromise) {
MOZ_ASSERT(aWindow);
MOZ_ASSERT(aPromise);
}
nsresult Start();
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(PersistentStoragePermissionRequest,
ContentPermissionRequestBase)
// nsIContentPermissionRequest
NS_IMETHOD Cancel(void) override;
NS_IMETHOD Allow(JS::Handle<JS::Value> choices) override;
private:
~PersistentStoragePermissionRequest() = default;
};
nsresult Estimate(nsIPrincipal* aPrincipal, nsIQuotaCallback* aCallback,
nsIQuotaRequest** aRequest) {
MOZ_ASSERT(aPrincipal);
MOZ_ASSERT(aCallback);
MOZ_ASSERT(aRequest);
// Firefox and Quota Manager have always used the schemeless origin group
// (https://storage.spec.whatwg.org/#schemeless-origin-group) for quota limit
// purposes. This has been to prevent a site/eTLD+1 from claiming more than
// its fair share of storage through the use of sub-domains. Because the limit
// is at the group level and the usage needs to make sense in the context of
// that limit, we also expose the group usage. Bug 1374970 reflects this
// reality and bug 1305665 tracks our plan to eliminate our use of groups for
// this.
nsCOMPtr<nsIQuotaManagerService> qms = QuotaManagerService::GetOrCreate();
if (NS_WARN_IF(!qms)) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIQuotaRequest> request;
nsresult rv = qms->Estimate(aPrincipal, getter_AddRefs(request));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
MOZ_ALWAYS_SUCCEEDS(request->SetCallback(aCallback));
request.forget(aRequest);
return NS_OK;
};
nsresult Persisted(nsIPrincipal* aPrincipal, nsIQuotaCallback* aCallback,
nsIQuotaRequest** aRequest) {
MOZ_ASSERT(aPrincipal);
MOZ_ASSERT(aCallback);
MOZ_ASSERT(aRequest);
nsCOMPtr<nsIQuotaManagerService> qms = QuotaManagerService::GetOrCreate();
if (NS_WARN_IF(!qms)) {
return NS_ERROR_FAILURE;
}
nsCOMPtr<nsIQuotaRequest> request;
nsresult rv = qms->Persisted(aPrincipal, getter_AddRefs(request));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// All the methods in nsIQuotaManagerService shouldn't synchronously fire
// any callbacks when they are being executed. Even when a result is ready,
// a new runnable should be dispatched to current thread to fire the callback
// asynchronously. It's safe to set the callback after we call Persisted().
MOZ_ALWAYS_SUCCEEDS(request->SetCallback(aCallback));
request.forget(aRequest);
return NS_OK;
};
already_AddRefed<Promise> ExecuteOpOnMainOrWorkerThread(
nsIGlobalObject* aGlobal, RequestResolver::Type aType, ErrorResult& aRv) {
MOZ_ASSERT(aGlobal);
MOZ_ASSERT_IF(aType == RequestResolver::Type::Persist, NS_IsMainThread());
RefPtr<Promise> promise = Promise::Create(aGlobal, aRv);
if (NS_WARN_IF(!promise)) {
return nullptr;
}
if (NS_IsMainThread()) {
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal);
if (NS_WARN_IF(!window)) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
nsCOMPtr<Document> doc = window->GetExtantDoc();
if (NS_WARN_IF(!doc)) {
aRv.Throw(NS_ERROR_FAILURE);
return nullptr;
}
nsCOMPtr<nsIPrincipal> principal = doc->NodePrincipal();
MOZ_ASSERT(principal);
// Storage Standard 7. API
// If origin is an opaque origin, then reject promise with a TypeError.
if (principal->GetIsNullPrincipal()) {
switch (aType) {
case RequestResolver::Type::Persisted:
promise->MaybeRejectWithTypeError(
"persisted() called for opaque origin");
break;
case RequestResolver::Type::Persist:
promise->MaybeRejectWithTypeError(
"persist() called for opaque origin");
break;
case RequestResolver::Type::Estimate:
promise->MaybeRejectWithTypeError(
"estimate() called for opaque origin");
break;
}
return promise.forget();
}
switch (aType) {
case RequestResolver::Type::Persisted: {
RefPtr<RequestResolver> resolver =
new RequestResolver(RequestResolver::Type::Persisted, promise);
RefPtr<nsIQuotaRequest> request;
aRv = Persisted(principal, resolver, getter_AddRefs(request));
break;
}
case RequestResolver::Type::Persist: {
RefPtr<PersistentStoragePermissionRequest> request =
new PersistentStoragePermissionRequest(principal, window, promise);
// In private browsing mode, no permission prompt.
if (nsContentUtils::IsInPrivateBrowsing(doc)) {
aRv = request->Cancel();
} else if (!request->CheckPermissionDelegate()) {
aRv = request->Cancel();
} else {
aRv = request->Start();
}
break;
}
case RequestResolver::Type::Estimate: {
RefPtr<RequestResolver> resolver =
new RequestResolver(RequestResolver::Type::Estimate, promise);
RefPtr<nsIQuotaRequest> request;
aRv = Estimate(principal, resolver, getter_AddRefs(request));
break;
}
default:
MOZ_CRASH("Invalid aRequest type!");
}
if (NS_WARN_IF(aRv.Failed())) {
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;
}
switch (aType) {
case RequestResolver::Type::Estimate: {
RefPtr<EstimateWorkerMainThreadRunnable> runnnable =
new EstimateWorkerMainThreadRunnable(promiseProxy->GetWorkerPrivate(),
promiseProxy);
runnnable->Dispatch(Canceling, aRv);
break;
}
case RequestResolver::Type::Persisted: {
RefPtr<PersistedWorkerMainThreadRunnable> runnnable =
new PersistedWorkerMainThreadRunnable(
promiseProxy->GetWorkerPrivate(), promiseProxy);
runnnable->Dispatch(Canceling, aRv);
break;
}
default:
MOZ_CRASH("Invalid aRequest type");
}
if (NS_WARN_IF(aRv.Failed())) {
return nullptr;
}
return promise.forget();
};
} // namespace
/*******************************************************************************
* Local class implementations
******************************************************************************/
void RequestResolver::ResolveOrReject() {
class MOZ_STACK_CLASS AutoCleanup final {
RefPtr<PromiseWorkerProxy> mProxy;
public:
explicit AutoCleanup(PromiseWorkerProxy* aProxy) : mProxy(aProxy) {
MOZ_ASSERT(aProxy);
}
~AutoCleanup() {
MOZ_ASSERT(mProxy);
mProxy->CleanUp();
}
};
RefPtr<Promise> promise;
Maybe<AutoCleanup> autoCleanup;
if (mPromise) {
promise = mPromise;
} else {
MOZ_ASSERT(mProxy);
promise = mProxy->WorkerPromise();
// Only clean up for worker case.
autoCleanup.emplace(mProxy);
}
MOZ_ASSERT(promise);
if (mType == Type::Estimate) {
if (NS_SUCCEEDED(mResultCode)) {
promise->MaybeResolve(mStorageEstimate);
} else {
promise->MaybeRejectWithTypeError(
"Internal error while estimating storage usage");
}
return;
}
MOZ_ASSERT(mType == Type::Persist || mType == Type::Persisted);
if (NS_SUCCEEDED(mResultCode)) {
promise->MaybeResolve(mPersisted);
} else {
promise->MaybeResolve(false);
}
}
NS_IMPL_ISUPPORTS(RequestResolver, nsIQuotaCallback)
nsresult RequestResolver::GetStorageEstimate(nsIVariant* aResult) {
MOZ_ASSERT(aResult);
MOZ_ASSERT(mType == Type::Estimate);
MOZ_ASSERT(aResult->GetDataType() == nsIDataType::VTYPE_INTERFACE_IS);
nsID* iid;
nsCOMPtr<nsISupports> supports;
nsresult rv = aResult->GetAsInterface(&iid, getter_AddRefs(supports));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
free(iid);
nsCOMPtr<nsIQuotaEstimateResult> estimateResult = do_QueryInterface(supports);
MOZ_ASSERT(estimateResult);
MOZ_ALWAYS_SUCCEEDS(
estimateResult->GetUsage(&mStorageEstimate.mUsage.Construct()));
MOZ_ALWAYS_SUCCEEDS(
estimateResult->GetLimit(&mStorageEstimate.mQuota.Construct()));
return NS_OK;
}
nsresult RequestResolver::GetPersisted(nsIVariant* aResult) {
MOZ_ASSERT(aResult);
MOZ_ASSERT(mType == Type::Persist || mType == Type::Persisted);
#ifdef DEBUG
uint16_t dataType = aResult->GetDataType();
#endif
if (mType == Type::Persist) {
MOZ_ASSERT(dataType == nsIDataType::VTYPE_VOID);
mPersisted = true;
return NS_OK;
}
MOZ_ASSERT(dataType == nsIDataType::VTYPE_BOOL);
bool persisted;
nsresult rv = aResult->GetAsBool(&persisted);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
mPersisted = persisted;
return NS_OK;
}
nsresult RequestResolver::OnCompleteInternal(nsIQuotaRequest* aRequest) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aRequest);
nsresult resultCode;
nsresult rv = aRequest->GetResultCode(&resultCode);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if (NS_FAILED(resultCode)) {
return resultCode;
}
nsCOMPtr<nsIVariant> result;
rv = aRequest->GetResult(getter_AddRefs(result));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
if (mType == Type::Estimate) {
rv = GetStorageEstimate(result);
} else {
MOZ_ASSERT(mType == Type::Persist || mType == Type::Persisted);
rv = GetPersisted(result);
}
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
nsresult RequestResolver::Finish() {
// In a main thread request.
if (!mProxy) {
MOZ_ASSERT(mPromise);
ResolveOrReject();
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;
}
NS_IMETHODIMP
RequestResolver::OnComplete(nsIQuotaRequest* aRequest) {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aRequest);
mResultCode = OnCompleteInternal(aRequest);
nsresult rv = Finish();
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
return NS_OK;
}
bool RequestResolver::FinishWorkerRunnable::WorkerRun(
JSContext* aCx, WorkerPrivate* aWorkerPrivate) {
MOZ_ASSERT(aCx);
MOZ_ASSERT(aWorkerPrivate);
aWorkerPrivate->AssertIsOnWorkerThread();
MOZ_ASSERT(mResolver);
mResolver->ResolveOrReject();
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<RequestResolver> resolver =
new RequestResolver(RequestResolver::Type::Estimate, mProxy);
RefPtr<nsIQuotaRequest> request;
nsresult rv = Estimate(principal, resolver, getter_AddRefs(request));
if (NS_WARN_IF(NS_FAILED(rv))) {
return false;
}
return true;
}
bool PersistedWorkerMainThreadRunnable::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<RequestResolver> resolver =
new RequestResolver(RequestResolver::Type::Persisted, mProxy);
RefPtr<nsIQuotaRequest> request;
nsresult rv = Persisted(principal, resolver, getter_AddRefs(request));
if (NS_WARN_IF(NS_FAILED(rv))) {
return false;
}
return true;
}
nsresult PersistentStoragePermissionRequest::Start() {
MOZ_ASSERT(NS_IsMainThread());
PromptResult pr;
#ifdef MOZ_WIDGET_ANDROID
// on Android calling `ShowPrompt` here calls
// `nsContentPermissionUtils::AskPermission` once, and a response of
// `PromptResult::Pending` calls it again. This results in multiple requests
// for storage access, so we check the prompt prefs only to ensure we only
// request it once.
pr = CheckPromptPrefs();
#else
nsresult rv = ShowPrompt(pr);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
#endif
if (pr == PromptResult::Granted) {
return Allow(JS::UndefinedHandleValue);
}
if (pr == PromptResult::Denied) {
return Cancel();
}
return nsContentPermissionUtils::AskPermission(this, mWindow);
}
NS_IMPL_ISUPPORTS_CYCLE_COLLECTION_INHERITED_0(
PersistentStoragePermissionRequest, ContentPermissionRequestBase)
NS_IMPL_CYCLE_COLLECTION_INHERITED(PersistentStoragePermissionRequest,
ContentPermissionRequestBase, mPromise)
NS_IMETHODIMP
PersistentStoragePermissionRequest::Cancel() {
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mPromise);
RefPtr<RequestResolver> resolver =
new RequestResolver(RequestResolver::Type::Persisted, mPromise);
RefPtr<nsIQuotaRequest> request;
return Persisted(mPrincipal, resolver, getter_AddRefs(request));
}
NS_IMETHODIMP
PersistentStoragePermissionRequest::Allow(JS::Handle<JS::Value> aChoices) {
MOZ_ASSERT(NS_IsMainThread());
RefPtr<RequestResolver> resolver =
new RequestResolver(RequestResolver::Type::Persist, mPromise);
nsCOMPtr<nsIQuotaManagerService> qms = QuotaManagerService::GetOrCreate();
if (NS_WARN_IF(!qms)) {
return NS_ERROR_FAILURE;
}
RefPtr<nsIQuotaRequest> request;
nsresult rv = qms->Persist(mPrincipal, getter_AddRefs(request));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
MOZ_ALWAYS_SUCCEEDS(request->SetCallback(resolver));
return NS_OK;
}
/*******************************************************************************
* StorageManager
******************************************************************************/
StorageManager::StorageManager(nsIGlobalObject* aGlobal) : mOwner(aGlobal) {
MOZ_ASSERT(aGlobal);
}
StorageManager::~StorageManager() = default;
already_AddRefed<Promise> StorageManager::Persisted(ErrorResult& aRv) {
MOZ_ASSERT(mOwner);
return ExecuteOpOnMainOrWorkerThread(mOwner, RequestResolver::Type::Persisted,
aRv);
}
already_AddRefed<Promise> StorageManager::Persist(ErrorResult& aRv) {
MOZ_ASSERT(mOwner);
Telemetry::ScalarAdd(Telemetry::ScalarID::NAVIGATOR_STORAGE_PERSIST_COUNT, 1);
return ExecuteOpOnMainOrWorkerThread(mOwner, RequestResolver::Type::Persist,
aRv);
}
already_AddRefed<Promise> StorageManager::Estimate(ErrorResult& aRv) {
MOZ_ASSERT(mOwner);
Telemetry::ScalarAdd(Telemetry::ScalarID::NAVIGATOR_STORAGE_ESTIMATE_COUNT,
1);
return ExecuteOpOnMainOrWorkerThread(mOwner, RequestResolver::Type::Estimate,
aRv);
}
already_AddRefed<Promise> StorageManager::GetDirectory(ErrorResult& aRv) {
RefPtr<Promise> promise = Promise::Create(GetParentObject(), aRv);
if (aRv.Failed()) {
return nullptr;
}
promise->MaybeReject(NS_ERROR_NOT_IMPLEMENTED);
return promise.forget();
}
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 StorageManager_Binding::Wrap(aCx, this, aGivenProto);
}
} // namespace mozilla::dom