Bug 1866402 - Reduce code duplication in QuotaManager::OpenStorageDirectory and QuotaManager::OpenClientDirectory; r=dom-storage-reviewers,jari

Differential Revision: https://phabricator.services.mozilla.com/D223460
This commit is contained in:
Jan Varga 2024-10-14 10:06:14 +00:00
parent 9b838294ef
commit 61a43c79f8
2 changed files with 95 additions and 98 deletions

View File

@ -11,6 +11,7 @@
#include "ClientUsageArray.h"
#include "Flatten.h"
#include "FirstInitializationAttemptsImpl.h"
#include "InitializationUtils.h"
#include "GroupInfo.h"
#include "GroupInfoPair.h"
#include "NormalOriginOperationBase.h"
@ -5214,42 +5215,11 @@ RefPtr<UniversalDirectoryLockPromise> QuotaManager::OpenStorageDirectory(
return BoolPromise::CreateAndReject(aRejectValue, __func__);
})
->Then(GetCurrentSerialEventTarget(), __func__,
[self = RefPtr(this),
storageDirectoryLock = std::move(storageDirectoryLock)](
const BoolPromise::ResolveOrRejectValue& aValue) mutable {
if (aValue.IsReject()) {
SafeDropDirectoryLockIfNotDropped(storageDirectoryLock);
return BoolPromise::CreateAndReject(aValue.RejectValue(),
__func__);
}
if (!storageDirectoryLock) {
return BoolPromise::CreateAndResolve(true, __func__);
}
return self->InitializeStorage(std::move(storageDirectoryLock));
})
->Then(
GetCurrentSerialEventTarget(), __func__,
[self = RefPtr(this), temporaryStorageDirectoryLock =
std::move(temporaryStorageDirectoryLock)](
const BoolPromise::ResolveOrRejectValue& aValue) mutable {
if (aValue.IsReject()) {
SafeDropDirectoryLockIfNotDropped(temporaryStorageDirectoryLock);
return BoolPromise::CreateAndReject(aValue.RejectValue(),
__func__);
}
if (!temporaryStorageDirectoryLock) {
return BoolPromise::CreateAndResolve(true, __func__);
}
return self->InitializeTemporaryStorage(
std::move(temporaryStorageDirectoryLock));
})
MaybeInitialize(std::move(storageDirectoryLock), this,
&QuotaManager::InitializeStorage))
->Then(GetCurrentSerialEventTarget(), __func__,
MaybeInitialize(std::move(temporaryStorageDirectoryLock), this,
&QuotaManager::InitializeTemporaryStorage))
->Then(GetCurrentSerialEventTarget(), __func__,
[universalDirectoryLockPromise =
std::move(universalDirectoryLockPromise)](
@ -5360,70 +5330,28 @@ RefPtr<ClientDirectoryLockPromise> QuotaManager::OpenClientDirectory(
[](nsresult aRejectValue) {
return BoolPromise::CreateAndReject(aRejectValue, __func__);
})
->Then(
GetCurrentSerialEventTarget(), __func__,
[self = RefPtr(this),
storageDirectoryLock = std::move(storageDirectoryLock)](
const BoolPromise::ResolveOrRejectValue& aValue) mutable {
if (aValue.IsReject()) {
SafeDropDirectoryLockIfNotDropped(storageDirectoryLock);
return BoolPromise::CreateAndReject(aValue.RejectValue(),
__func__);
}
if (!storageDirectoryLock) {
return BoolPromise::CreateAndResolve(true, __func__);
}
return self->InitializeStorage(std::move(storageDirectoryLock));
})
->Then(GetCurrentSerialEventTarget(), __func__,
[self = RefPtr(this),
temporaryStorageDirectoryLock =
std::move(temporaryStorageDirectoryLock)](
const BoolPromise::ResolveOrRejectValue& aValue) mutable {
if (aValue.IsReject()) {
SafeDropDirectoryLockIfNotDropped(
temporaryStorageDirectoryLock);
MaybeInitialize(std::move(storageDirectoryLock), this,
&QuotaManager::InitializeStorage))
->Then(GetCurrentSerialEventTarget(), __func__,
MaybeInitialize(std::move(temporaryStorageDirectoryLock), this,
&QuotaManager::InitializeTemporaryStorage))
->Then(GetCurrentSerialEventTarget(), __func__,
MaybeInitialize(
std::move(originDirectoryLock),
[self = RefPtr(this), persistenceType,
principalInfo = std::move(principalInfo),
aCreateIfNonExistent](RefPtr<UniversalDirectoryLock>
originDirectoryLock) mutable {
if (persistenceType == PERSISTENCE_TYPE_PERSISTENT) {
return self->InitializePersistentOrigin(
principalInfo, std::move(originDirectoryLock));
}
return BoolPromise::CreateAndReject(aValue.RejectValue(),
__func__);
}
if (!temporaryStorageDirectoryLock) {
return BoolPromise::CreateAndResolve(true, __func__);
}
return self->InitializeTemporaryStorage(
std::move(temporaryStorageDirectoryLock));
})
->Then(
GetCurrentSerialEventTarget(), __func__,
[self = RefPtr(this), persistenceType,
principalInfo = std::move(principalInfo), aCreateIfNonExistent,
originDirectoryLock = std::move(originDirectoryLock)](
const BoolPromise::ResolveOrRejectValue& aValue) mutable {
if (aValue.IsReject()) {
SafeDropDirectoryLockIfNotDropped(originDirectoryLock);
return BoolPromise::CreateAndReject(aValue.RejectValue(),
__func__);
}
if (!originDirectoryLock) {
return BoolPromise::CreateAndResolve(true, __func__);
}
if (persistenceType == PERSISTENCE_TYPE_PERSISTENT) {
return self->InitializePersistentOrigin(
principalInfo, std::move(originDirectoryLock));
}
return self->InitializeTemporaryOrigin(
persistenceType, principalInfo, aCreateIfNonExistent,
std::move(originDirectoryLock));
})
return self->InitializeTemporaryOrigin(
persistenceType, principalInfo, aCreateIfNonExistent,
std::move(originDirectoryLock));
}))
->Then(GetCurrentSerialEventTarget(), __func__,
[clientDirectoryLock = std::move(clientDirectoryLock)](
const BoolPromise::ResolveOrRejectValue& aValue) mutable {

View File

@ -0,0 +1,69 @@
/* -*- 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"
namespace mozilla::dom::quota {
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_