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

Differential Revision: https://phabricator.services.mozilla.com/D223710
This commit is contained in:
Jan Varga 2024-10-14 10:06:14 +00:00
parent 61a43c79f8
commit 31af6ee2f0
2 changed files with 47 additions and 57 deletions

View File

@ -5163,35 +5163,22 @@ RefPtr<UniversalDirectoryLockPromise> QuotaManager::OpenStorageDirectory(
nsTArray<RefPtr<BoolPromise>> promises;
RefPtr<UniversalDirectoryLock> storageDirectoryLock =
CreateDirectoryLockInternal(PersistenceScope::CreateFromNull(),
OriginScope::FromNull(),
Nullable<Client::Type>(),
/* aExclusive */ false);
if (mStorageInitialized &&
!IsDirectoryLockBlockedByUninitStorageOperation(storageDirectoryLock)) {
storageDirectoryLock = nullptr;
} else {
promises.AppendElement(storageDirectoryLock->Acquire());
}
CreateDirectoryLockForInitialization(
*this, PersistenceScope::CreateFromNull(), OriginScope::FromNull(),
mStorageInitialized, IsDirectoryLockBlockedByUninitStorageOperation,
MakeBackInserter(promises));
RefPtr<UniversalDirectoryLock> temporaryStorageDirectoryLock;
if (aInitializeOrigins &&
MatchesBestEffortPersistenceScope(aPersistenceScope)) {
temporaryStorageDirectoryLock = CreateDirectoryLockInternal(
temporaryStorageDirectoryLock = CreateDirectoryLockForInitialization(
*this,
PersistenceScope::CreateFromSet(PERSISTENCE_TYPE_TEMPORARY,
PERSISTENCE_TYPE_DEFAULT),
OriginScope::FromNull(), Nullable<Client::Type>(),
/* aExclusive */ false);
if (mTemporaryStorageInitialized &&
!IsDirectoryLockBlockedByUninitStorageOperation(
temporaryStorageDirectoryLock)) {
temporaryStorageDirectoryLock = nullptr;
} else {
promises.AppendElement(temporaryStorageDirectoryLock->Acquire());
}
OriginScope::FromNull(), mTemporaryStorageInitialized,
IsDirectoryLockBlockedByUninitStorageOperation,
MakeBackInserter(promises));
}
RefPtr<UniversalDirectoryLock> universalDirectoryLock =
@ -5262,54 +5249,34 @@ RefPtr<ClientDirectoryLockPromise> QuotaManager::OpenClientDirectory(
nsTArray<RefPtr<BoolPromise>> promises;
RefPtr<UniversalDirectoryLock> storageDirectoryLock =
CreateDirectoryLockInternal(PersistenceScope::CreateFromNull(),
OriginScope::FromNull(),
Nullable<Client::Type>(),
/* aExclusive */ false);
if (mStorageInitialized &&
!IsDirectoryLockBlockedByUninitStorageOperation(storageDirectoryLock)) {
storageDirectoryLock = nullptr;
} else {
promises.AppendElement(storageDirectoryLock->Acquire());
}
CreateDirectoryLockForInitialization(
*this, PersistenceScope::CreateFromNull(), OriginScope::FromNull(),
mStorageInitialized, IsDirectoryLockBlockedByUninitStorageOperation,
MakeBackInserter(promises));
RefPtr<UniversalDirectoryLock> temporaryStorageDirectoryLock;
if (IsBestEffortPersistenceType(persistenceType)) {
temporaryStorageDirectoryLock = CreateDirectoryLockInternal(
temporaryStorageDirectoryLock = CreateDirectoryLockForInitialization(
*this,
PersistenceScope::CreateFromSet(PERSISTENCE_TYPE_TEMPORARY,
PERSISTENCE_TYPE_DEFAULT),
OriginScope::FromNull(), Nullable<Client::Type>(),
/* aExclusive */ false);
if (mTemporaryStorageInitialized &&
!IsDirectoryLockBlockedByUninitStorageOperation(
temporaryStorageDirectoryLock)) {
temporaryStorageDirectoryLock = nullptr;
} else {
promises.AppendElement(temporaryStorageDirectoryLock->Acquire());
}
OriginScope::FromNull(), mTemporaryStorageInitialized,
IsDirectoryLockBlockedByUninitStorageOperation,
MakeBackInserter(promises));
}
RefPtr<UniversalDirectoryLock> originDirectoryLock =
CreateDirectoryLockInternal(
PersistenceScope::CreateFromValue(persistenceType),
OriginScope::FromOrigin(aClientMetadata.mOrigin),
Nullable<Client::Type>(), /* aExclusive */ false);
const bool originInitialized =
persistenceType == PERSISTENCE_TYPE_PERSISTENT
? IsPersistentOriginInitialized(principalInfo)
: IsTemporaryOriginInitialized(persistenceType, principalInfo);
if (originInitialized &&
!IsDirectoryLockBlockedByUninitStorageOrUninitOriginsOperation(
originDirectoryLock)) {
originDirectoryLock = nullptr;
} else {
promises.AppendElement(originDirectoryLock->Acquire());
}
RefPtr<UniversalDirectoryLock> originDirectoryLock =
CreateDirectoryLockForInitialization(
*this, PersistenceScope::CreateFromValue(persistenceType),
OriginScope::FromOrigin(aClientMetadata.mOrigin), originInitialized,
IsDirectoryLockBlockedByUninitStorageOrUninitOriginsOperation,
MakeBackInserter(promises));
RefPtr<ClientDirectoryLock> clientDirectoryLock =
CreateDirectoryLock(aClientMetadata, /* aExclusive */ false);

View File

@ -14,9 +14,32 @@
#include "mozilla/dom/quota/DirectoryLock.h"
#include "mozilla/dom/quota/DirectoryLockInlines.h"
#include "mozilla/dom/quota/ForwardDecls.h"
#include "mozilla/dom/quota/QuotaManager.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,
Nullable<Client::Type>(),
/* aExclusive */ false);
if (aAlreadyInitialized &&
!std::forward<UninitChecker>(aUninitChecker)(directoryLock)) {
return nullptr;
}
auto iter = std::forward<PromiseArrayIter>(aPromiseArrayIter);
*iter = directoryLock->Acquire();
++iter;
return directoryLock;
}
template <typename Callable>
class MaybeInitializeHelper final {
public: