From bbaae1d4014a120b3e0ad5017b3b6ffe7179afa6 Mon Sep 17 00:00:00 2001 From: Jan Varga Date: Thu, 1 Sep 2022 19:35:55 +0000 Subject: [PATCH] Bug 1758092 - Use QM_TRY for error handling in FileSystemDataManager::BeginOpen; r=dom-storage-reviewers,jari This patch introduces new generic functions for handling the case when a rejected promise needs to be returned. First consumers are added to FileSystemDataManager::BeginOpen. Differential Revision: https://phabricator.services.mozilla.com/D156162 --- .../datamodel/FileSystemDataManager.cpp | 61 +++++++++---------- dom/quota/QuotaCommon.cpp | 18 +++++- dom/quota/QuotaCommon.h | 15 +++++ 3 files changed, 60 insertions(+), 34 deletions(-) diff --git a/dom/fs/parent/datamodel/FileSystemDataManager.cpp b/dom/fs/parent/datamodel/FileSystemDataManager.cpp index 98d5868c3017..b7727f460e68 100644 --- a/dom/fs/parent/datamodel/FileSystemDataManager.cpp +++ b/dom/fs/parent/datamodel/FileSystemDataManager.cpp @@ -375,32 +375,28 @@ RefPtr FileSystemDataManager::BeginOpen() { // XXX The connection should be initialized off the PBackground thread. // These have to be done on the PBackground thread - auto connectionRes = fs::data::GetStorageConnection(mOriginMetadata.mOrigin); - if (NS_WARN_IF(connectionRes.isErr())) { - return BoolPromise::CreateAndReject(connectionRes.unwrapErr().NSResult(), - __func__); - } - auto connection = connectionRes.unwrap(); + + QM_TRY_UNWRAP(auto connection, + fs::data::GetStorageConnection(mOriginMetadata.mOrigin), + CreateAndRejectBoolPromiseFromQMResult); + QM_TRY_UNWRAP(DatabaseVersion version, SchemaVersion001::InitializeConnection(connection, mOriginMetadata.mOrigin), - BoolPromise::CreateAndReject(NS_ERROR_FAILURE, __func__)); - - auto quotaManagerRes = quota::QuotaManager::GetOrCreate(); - if (NS_WARN_IF(quotaManagerRes.isErr())) { - return BoolPromise::CreateAndReject(NS_ERROR_FAILURE, __func__); - } - RefPtr quotamanager = quotaManagerRes.unwrap(); + CreateAndRejectBoolPromiseFromQMResult); if (1 == version) { mDatabaseManager = MakeUnique(std::move(connection)); } + QM_TRY_UNWRAP(const NotNull> quotaManager, + quota::QuotaManager::GetOrCreate(), CreateAndRejectBoolPromise); + // XXX Take a directory lock here so our origin isn't evicted InvokeAsync( - quotamanager->IOThread(), __func__, + quotaManager->IOThread(), __func__, [self = RefPtr(this)]() mutable { auto autoProxyReleaseManager = MakeScopeExit([&self] { nsCOMPtr target = @@ -410,24 +406,25 @@ RefPtr FileSystemDataManager::BeginOpen() { self.forget()); }); - quota::QuotaManager* quotamanager = quota::QuotaManager::Get(); - if (NS_WARN_IF(!quotamanager)) { - return BoolPromise::CreateAndReject(NS_ERROR_UNEXPECTED, __func__); - } - nsresult rv = quotamanager->EnsureStorageIsInitialized(); - if (NS_WARN_IF(NS_FAILED(rv))) { - return BoolPromise::CreateAndReject(rv, __func__); - } - rv = quotamanager->EnsureTemporaryStorageIsInitialized(); - if (NS_WARN_IF(NS_FAILED(rv))) { - return BoolPromise::CreateAndReject(rv, __func__); - } - auto result = quotamanager->EnsureTemporaryOriginIsInitialized( - quota::PERSISTENCE_TYPE_DEFAULT, self->mOriginMetadata); - if (result.isOk()) { - return BoolPromise::CreateAndResolve(true, __func__); - } - return BoolPromise::CreateAndReject(result.unwrapErr(), __func__); + quota::QuotaManager* quotaManager = quota::QuotaManager::Get(); + QM_TRY(MOZ_TO_RESULT(quotaManager), CreateAndRejectBoolPromise) + + QM_TRY(MOZ_TO_RESULT(quotaManager->EnsureStorageIsInitialized()), + CreateAndRejectBoolPromise); + + QM_TRY( + MOZ_TO_RESULT(quotaManager->EnsureTemporaryStorageIsInitialized()), + CreateAndRejectBoolPromise); + + QM_TRY_INSPECT( + const auto& dirInfo, + quotaManager->EnsureTemporaryOriginIsInitialized( + quota::PERSISTENCE_TYPE_DEFAULT, self->mOriginMetadata), + CreateAndRejectBoolPromise); + + Unused << dirInfo; + + return BoolPromise::CreateAndResolve(true, __func__); }) ->Then(MutableIOTargetPtr(), __func__, [self = RefPtr(this)]( diff --git a/dom/quota/QuotaCommon.cpp b/dom/quota/QuotaCommon.cpp index 21e73d1ca355..2d47ca6714e2 100644 --- a/dom/quota/QuotaCommon.cpp +++ b/dom/quota/QuotaCommon.cpp @@ -12,6 +12,7 @@ #include "mozIStorageConnection.h" #include "mozIStorageStatement.h" #include "mozilla/ErrorNames.h" +#include "mozilla/MozPromise.h" #include "mozilla/Logging.h" #include "mozilla/Telemetry.h" #include "mozilla/TelemetryComms.h" @@ -35,7 +36,19 @@ # include "nsILocalFileWin.h" #endif -namespace mozilla::dom::quota { +namespace mozilla { + +RefPtr CreateAndRejectBoolPromise(const char* aFunc, + nsresult aRv) { + return CreateAndRejectMozPromise(aFunc, aRv); +} + +RefPtr CreateAndRejectBoolPromiseFromQMResult( + const char* aFunc, const QMResult& aRv) { + return CreateAndRejectMozPromise(aFunc, aRv); +} + +namespace dom::quota { using namespace mozilla::Telemetry; @@ -617,4 +630,5 @@ Result WarnIfFileIsUnknown(nsIFile& aFile, } #endif -} // namespace mozilla::dom::quota +} // namespace dom::quota +} // namespace mozilla diff --git a/dom/quota/QuotaCommon.h b/dom/quota/QuotaCommon.h index 136819f20fc6..e48cd30e9611 100644 --- a/dom/quota/QuotaCommon.h +++ b/dom/quota/QuotaCommon.h @@ -1075,6 +1075,21 @@ auto ErrToDefaultOk(const nsresult aValue) -> Result { return V{}; } +template +auto CreateAndRejectMozPromise(const char* aFunc, const RejectValueT& aRv) + -> decltype(auto) { + if constexpr (std::is_same_v) { + return MozPromiseType::CreateAndReject(aRv, aFunc); + } else if constexpr (std::is_same_v) { + return MozPromiseType::CreateAndReject(aRv.NSResult(), aFunc); + } +} + +RefPtr CreateAndRejectBoolPromise(const char* aFunc, nsresult aRv); + +RefPtr CreateAndRejectBoolPromiseFromQMResult(const char* aFunc, + const QMResult& aRv); + // Like Rust's collect with a step function, not a generic iterator/range. // // Cond must be a function type with a return type to Result, where