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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "OriginOperationBase.h"
9 #include "mozilla/Assertions.h"
10 #include "mozilla/MozPromise.h"
11 #include "mozilla/dom/fs/TargetPtrHolder.h"
12 #include "mozilla/dom/quota/QuotaManager.h"
13 #include "mozilla/dom/quota/ResultExtensions.h"
15 #include "nsIThread.h"
16 #include "nsThreadUtils.h"
18 namespace mozilla::dom::quota
{
20 OriginOperationBase::OriginOperationBase(
21 MovingNotNull
<RefPtr
<QuotaManager
>>&& aQuotaManager
, const char* aName
)
22 : BackgroundThreadObject(GetCurrentSerialEventTarget()),
23 mQuotaManager(std::move(aQuotaManager
)),
25 mActorDestroyed(false)
26 #ifdef QM_COLLECTING_OPERATION_TELEMETRY
31 AssertIsOnOwningThread();
34 OriginOperationBase::~OriginOperationBase() {
35 AssertIsOnOwningThread();
36 MOZ_ASSERT(mActorDestroyed
);
39 void OriginOperationBase::RunImmediately() {
40 AssertIsOnOwningThread();
42 [self
= RefPtr(this)]() {
43 if (QuotaManager::IsShuttingDown()) {
44 return BoolPromise::CreateAndReject(NS_ERROR_ABORT
, __func__
);
47 QM_TRY(MOZ_TO_RESULT(self
->DoInit(*self
->mQuotaManager
)),
48 CreateAndRejectBoolPromise
);
53 ->Then(GetCurrentSerialEventTarget(), __func__
,
54 [self
= RefPtr(this)](
55 const BoolPromise::ResolveOrRejectValue
& aValue
) {
56 if (aValue
.IsReject()) {
57 return BoolPromise::CreateAndReject(aValue
.RejectValue(),
61 // Give derived classes the occasion to add additional debug
62 // only checks after the opening was successfully finished on
63 // the current thread before passing the work to the IO thread.
64 QM_TRY(MOZ_TO_RESULT(self
->DirectoryOpen()),
65 CreateAndRejectBoolPromise
);
67 return BoolPromise::CreateAndResolve(true, __func__
);
70 ->Then(mQuotaManager
->IOThread(), __func__
,
71 [selfHolder
= fs::TargetPtrHolder(this)](
72 const BoolPromise::ResolveOrRejectValue
& aValue
) {
73 if (aValue
.IsReject()) {
74 return BoolPromise::CreateAndReject(aValue
.RejectValue(),
78 QM_TRY(MOZ_TO_RESULT(selfHolder
->DoDirectoryWork(
79 *selfHolder
->mQuotaManager
)),
80 CreateAndRejectBoolPromise
);
82 return BoolPromise::CreateAndResolve(true, __func__
);
84 ->Then(GetCurrentSerialEventTarget(), __func__
,
85 [self
= RefPtr(this)](
86 const BoolPromise::ResolveOrRejectValue
& aValue
) {
87 if (aValue
.IsReject()) {
88 MOZ_ASSERT(NS_SUCCEEDED(self
->mResultCode
));
90 self
->mResultCode
= aValue
.RejectValue();
97 nsresult
OriginOperationBase::DoInit(QuotaManager
& aQuotaManager
) {
98 AssertIsOnOwningThread();
104 nsresult
OriginOperationBase::DirectoryOpen() {
105 AssertIsOnOwningThread();
111 } // namespace mozilla::dom::quota