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 #ifndef DOM_QUOTA_INITIALIZATIONUTILS_H_
8 #define DOM_QUOTA_INITIALIZATIONUTILS_H_
12 #include "mozilla/MozPromise.h"
13 #include "mozilla/RefPtr.h"
14 #include "mozilla/dom/quota/DirectoryLock.h"
15 #include "mozilla/dom/quota/DirectoryLockInlines.h"
16 #include "mozilla/dom/quota/ForwardDecls.h"
17 #include "mozilla/dom/quota/QuotaManager.h"
18 #include "mozilla/dom/quota/UniversalDirectoryLock.h"
20 namespace mozilla::dom::quota
{
22 template <typename UninitChecker
, typename PromiseArrayIter
>
23 RefPtr
<UniversalDirectoryLock
> CreateDirectoryLockForInitialization(
24 QuotaManager
& aQuotaManager
, const PersistenceScope
& aPersistenceScope
,
25 const OriginScope
& aOriginScope
, const bool aAlreadyInitialized
,
26 UninitChecker
&& aUninitChecker
, PromiseArrayIter
&& aPromiseArrayIter
) {
27 RefPtr
<UniversalDirectoryLock
> directoryLock
=
28 aQuotaManager
.CreateDirectoryLockInternal(aPersistenceScope
, aOriginScope
,
29 Nullable
<Client::Type
>(),
30 /* aExclusive */ false);
32 auto prepareInfo
= directoryLock
->Prepare();
34 if (aAlreadyInitialized
&&
35 !std::forward
<UninitChecker
>(aUninitChecker
)(prepareInfo
)) {
39 auto iter
= std::forward
<PromiseArrayIter
>(aPromiseArrayIter
);
40 *iter
= directoryLock
->Acquire(std::move(prepareInfo
));
46 template <typename Callable
>
47 class MaybeInitializeHelper final
{
49 MaybeInitializeHelper(RefPtr
<UniversalDirectoryLock
> aDirectoryLock
,
51 : mDirectoryLock(std::move(aDirectoryLock
)),
52 mCallable(std::move(aCallable
)) {}
54 RefPtr
<BoolPromise
> operator()(
55 const BoolPromise::ResolveOrRejectValue
& aValue
) {
56 if (aValue
.IsReject()) {
57 SafeDropDirectoryLockIfNotDropped(mDirectoryLock
);
59 return BoolPromise::CreateAndReject(aValue
.RejectValue(), __func__
);
62 if (!mDirectoryLock
) {
63 return BoolPromise::CreateAndResolve(true, __func__
);
66 return mCallable(std::move(mDirectoryLock
));
70 RefPtr
<UniversalDirectoryLock
> mDirectoryLock
;
74 template <typename Callable
>
75 auto MaybeInitialize(RefPtr
<UniversalDirectoryLock
> aDirectoryLock
,
76 Callable
&& aCallable
) {
77 return MaybeInitializeHelper(std::move(aDirectoryLock
),
78 std::forward
<Callable
>(aCallable
));
81 auto MaybeInitialize(RefPtr
<UniversalDirectoryLock
> aDirectoryLock
,
82 RefPtr
<QuotaManager
> aQuotaManager
,
83 RefPtr
<BoolPromise
> (QuotaManager::*aMethod
)(
84 RefPtr
<UniversalDirectoryLock
>)) {
85 return MaybeInitializeHelper(
86 std::move(aDirectoryLock
),
87 [quotaManager
= std::move(aQuotaManager
),
88 method
= aMethod
](RefPtr
<UniversalDirectoryLock
> aDirectoryLock
) {
89 return (quotaManager
->*method
)(std::move(aDirectoryLock
));
93 } // namespace mozilla::dom::quota
95 #endif // DOM_QUOTA_INITIALIZATIONUTILS_H_