Files
tubestation/dom/quota/InitializationUtils.h
Jan Varga 74c99915a3 Bug 1957334 - QM: Replace uses of Nullable<Client::Type> with ClientStorageScope; r=dom-storage-reviewers,jstutte
`Nullable<ClientType>` currently allows specifying either a concrete client
type or all client types, but does not support more flexible scoping options.

`ClientStorageScope` is designed to address that limitation, with future
support planned for special purpose scopes, such as locking the metadata file
shared by client directories.

This patch updates existing code, particularly around directory locks, to use
`ClientStorageScope` in place of `Nullable<ClientType>`.

Support for additional scope types will be added in a follow-up patch. This
patch focuses on transitioning to the new abstraction.

Differential Revision: https://phabricator.services.mozilla.com/D243630
2025-04-10 05:06:55 +00:00

96 lines
3.2 KiB
C++

/* -*- 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/. */
#ifndef DOM_QUOTA_INITIALIZATIONUTILS_H_
#define DOM_QUOTA_INITIALIZATIONUTILS_H_
#include <functional>
#include "mozilla/MozPromise.h"
#include "mozilla/RefPtr.h"
#include "mozilla/dom/quota/DirectoryLock.h"
#include "mozilla/dom/quota/DirectoryLockInlines.h"
#include "mozilla/dom/quota/ForwardDecls.h"
#include "mozilla/dom/quota/QuotaManager.h"
#include "mozilla/dom/quota/UniversalDirectoryLock.h"
namespace mozilla::dom::quota {
template <typename UninitChecker, typename PromiseArrayIter>
RefPtr<UniversalDirectoryLock> CreateDirectoryLockForInitialization(
QuotaManager& aQuotaManager, const PersistenceScope& aPersistenceScope,
const OriginScope& aOriginScope, const bool aAlreadyInitialized,
UninitChecker&& aUninitChecker, PromiseArrayIter&& aPromiseArrayIter) {
RefPtr<UniversalDirectoryLock> directoryLock =
aQuotaManager.CreateDirectoryLockInternal(
aPersistenceScope, aOriginScope, ClientStorageScope::CreateFromNull(),
/* aExclusive */ false);
auto prepareInfo = directoryLock->Prepare();
if (aAlreadyInitialized &&
!std::forward<UninitChecker>(aUninitChecker)(prepareInfo)) {
return nullptr;
}
auto iter = std::forward<PromiseArrayIter>(aPromiseArrayIter);
*iter = directoryLock->Acquire(std::move(prepareInfo));
++iter;
return directoryLock;
}
template <typename Callable>
class MaybeInitializeHelper final {
public:
MaybeInitializeHelper(RefPtr<UniversalDirectoryLock> aDirectoryLock,
Callable&& aCallable)
: mDirectoryLock(std::move(aDirectoryLock)),
mCallable(std::move(aCallable)) {}
RefPtr<BoolPromise> operator()(
const BoolPromise::ResolveOrRejectValue& aValue) {
if (aValue.IsReject()) {
SafeDropDirectoryLockIfNotDropped(mDirectoryLock);
return BoolPromise::CreateAndReject(aValue.RejectValue(), __func__);
}
if (!mDirectoryLock) {
return BoolPromise::CreateAndResolve(true, __func__);
}
return mCallable(std::move(mDirectoryLock));
}
private:
RefPtr<UniversalDirectoryLock> mDirectoryLock;
Callable mCallable;
};
template <typename Callable>
auto MaybeInitialize(RefPtr<UniversalDirectoryLock> aDirectoryLock,
Callable&& aCallable) {
return MaybeInitializeHelper(std::move(aDirectoryLock),
std::forward<Callable>(aCallable));
}
auto MaybeInitialize(RefPtr<UniversalDirectoryLock> aDirectoryLock,
RefPtr<QuotaManager> aQuotaManager,
RefPtr<BoolPromise> (QuotaManager::*aMethod)(
RefPtr<UniversalDirectoryLock>)) {
return MaybeInitializeHelper(
std::move(aDirectoryLock),
[quotaManager = std::move(aQuotaManager),
method = aMethod](RefPtr<UniversalDirectoryLock> aDirectoryLock) {
return (quotaManager->*method)(std::move(aDirectoryLock));
});
}
} // namespace mozilla::dom::quota
#endif // DOM_QUOTA_INITIALIZATIONUTILS_H_