From c23800e5f29bd6b95160056ef85e6cd4d49b55a1 Mon Sep 17 00:00:00 2001 From: Simon Giesecke Date: Thu, 25 Jun 2020 08:34:43 +0000 Subject: [PATCH] Bug 1641512 - Use Result as return type of GetUsageForOrigin. r=dom-workers-and-storage-reviewers,janv Differential Revision: https://phabricator.services.mozilla.com/D77452 --- dom/cache/QuotaClient.cpp | 20 ++++++++----- dom/cache/QuotaClientImpl.h | 8 ++--- dom/indexedDB/ActorsParent.cpp | 29 ++++++++++-------- dom/localstorage/ActorsParent.cpp | 22 ++++++-------- dom/quota/ActorsParent.cpp | 15 ++++++---- dom/quota/Client.h | 8 ++--- dom/simpledb/ActorsParent.cpp | 49 ++++++++++++++++--------------- 7 files changed, 79 insertions(+), 72 deletions(-) diff --git a/dom/cache/QuotaClient.cpp b/dom/cache/QuotaClient.cpp index fe339c677b3a..3eec399c0c25 100644 --- a/dom/cache/QuotaClient.cpp +++ b/dom/cache/QuotaClient.cpp @@ -220,13 +220,19 @@ nsresult CacheQuotaClient::InitOrigin(PersistenceType aPersistenceType, /* aInitializing*/ true, aUsageInfo); } -nsresult CacheQuotaClient::GetUsageForOrigin(PersistenceType aPersistenceType, - const nsACString& aGroup, - const nsACString& aOrigin, - const AtomicBool& aCanceled, - UsageInfo* aUsageInfo) { - return GetUsageForOriginInternal(aPersistenceType, aGroup, aOrigin, aCanceled, - /* aInitializing*/ false, aUsageInfo); +Result CacheQuotaClient::GetUsageForOrigin( + PersistenceType aPersistenceType, const nsACString& aGroup, + const nsACString& aOrigin, const AtomicBool& aCanceled) { + UsageInfo res; + + nsresult rv = + GetUsageForOriginInternal(aPersistenceType, aGroup, aOrigin, aCanceled, + /* aInitializing*/ false, &res); + if (NS_WARN_IF(NS_FAILED(rv))) { + return Err(rv); + } + + return res; } void CacheQuotaClient::OnOriginClearCompleted(PersistenceType aPersistenceType, diff --git a/dom/cache/QuotaClientImpl.h b/dom/cache/QuotaClientImpl.h index 62cf80651e66..c502a1aa7416 100644 --- a/dom/cache/QuotaClientImpl.h +++ b/dom/cache/QuotaClientImpl.h @@ -32,11 +32,9 @@ class CacheQuotaClient final : public quota::Client { const AtomicBool& aCanceled, UsageInfo* aUsageInfo) override; - virtual nsresult GetUsageForOrigin(PersistenceType aPersistenceType, - const nsACString& aGroup, - const nsACString& aOrigin, - const AtomicBool& aCanceled, - UsageInfo* aUsageInfo) override; + virtual Result GetUsageForOrigin( + PersistenceType aPersistenceType, const nsACString& aGroup, + const nsACString& aOrigin, const AtomicBool& aCanceled) override; virtual void OnOriginClearCompleted(PersistenceType aPersistenceType, const nsACString& aOrigin) override; diff --git a/dom/indexedDB/ActorsParent.cpp b/dom/indexedDB/ActorsParent.cpp index 9109b9f11711..7a72dfb852d8 100644 --- a/dom/indexedDB/ActorsParent.cpp +++ b/dom/indexedDB/ActorsParent.cpp @@ -8568,11 +8568,9 @@ class QuotaClient final : public mozilla::dom::quota::Client { const AtomicBool& aCanceled, UsageInfo* aUsageInfo) override; - nsresult GetUsageForOrigin(PersistenceType aPersistenceType, - const nsACString& aGroup, - const nsACString& aOrigin, - const AtomicBool& aCanceled, - UsageInfo* aUsageInfo) override; + Result GetUsageForOrigin( + PersistenceType aPersistenceType, const nsACString& aGroup, + const nsACString& aOrigin, const AtomicBool& aCanceled) override; void OnOriginClearCompleted(PersistenceType aPersistenceType, const nsACString& aOrigin) override; @@ -17193,16 +17191,21 @@ nsresult QuotaClient::InitOrigin(PersistenceType aPersistenceType, /* aInitializing*/ true, aUsageInfo); } -nsresult QuotaClient::GetUsageForOrigin(PersistenceType aPersistenceType, - const nsACString& aGroup, - const nsACString& aOrigin, - const AtomicBool& aCanceled, - UsageInfo* aUsageInfo) { +Result QuotaClient::GetUsageForOrigin( + PersistenceType aPersistenceType, const nsACString& aGroup, + const nsACString& aOrigin, const AtomicBool& aCanceled) { AssertIsOnIOThread(); - MOZ_ASSERT(aUsageInfo); - return GetUsageForOriginInternal(aPersistenceType, aGroup, aOrigin, aCanceled, - /* aInitializing*/ false, aUsageInfo); + UsageInfo res; + + nsresult rv = + GetUsageForOriginInternal(aPersistenceType, aGroup, aOrigin, aCanceled, + /* aInitializing*/ false, &res); + if (NS_WARN_IF(NS_FAILED(rv))) { + return Err(rv); + } + + return res; } nsresult QuotaClient::GetUsageForOriginInternal( diff --git a/dom/localstorage/ActorsParent.cpp b/dom/localstorage/ActorsParent.cpp index 40fea9ad06f2..85554af30171 100644 --- a/dom/localstorage/ActorsParent.cpp +++ b/dom/localstorage/ActorsParent.cpp @@ -2824,11 +2824,9 @@ class QuotaClient final : public mozilla::dom::quota::Client { const AtomicBool& aCanceled, UsageInfo* aUsageInfo) override; - nsresult GetUsageForOrigin(PersistenceType aPersistenceType, - const nsACString& aGroup, - const nsACString& aOrigin, - const AtomicBool& aCanceled, - UsageInfo* aUsageInfo) override; + Result GetUsageForOrigin( + PersistenceType aPersistenceType, const nsACString& aGroup, + const nsACString& aOrigin, const AtomicBool& aCanceled) override; nsresult AboutToClearOrigins( const Nullable& aPersistenceType, @@ -9062,14 +9060,11 @@ nsresult QuotaClient::InitOrigin(PersistenceType aPersistenceType, return NS_OK; } -nsresult QuotaClient::GetUsageForOrigin(PersistenceType aPersistenceType, - const nsACString& aGroup, - const nsACString& aOrigin, - const AtomicBool& aCanceled, - UsageInfo* aUsageInfo) { +Result QuotaClient::GetUsageForOrigin( + PersistenceType aPersistenceType, const nsACString& aGroup, + const nsACString& aOrigin, const AtomicBool& aCanceled) { AssertIsOnIOThread(); MOZ_ASSERT(aPersistenceType == PERSISTENCE_TYPE_DEFAULT); - MOZ_ASSERT(aUsageInfo); // We can't open the database at this point, since it can be already used // by the connection thread. Use the cached value instead. @@ -9077,13 +9072,14 @@ nsresult QuotaClient::GetUsageForOrigin(PersistenceType aPersistenceType, QuotaManager* quotaManager = QuotaManager::Get(); MOZ_ASSERT(quotaManager); + UsageInfo res; uint64_t usage; if (quotaManager->GetUsageForClient(PERSISTENCE_TYPE_DEFAULT, aGroup, aOrigin, Client::LS, usage)) { - aUsageInfo->IncrementDatabaseUsage(Some(usage)); + res.IncrementDatabaseUsage(Some(usage)); } - return NS_OK; + return res; } nsresult QuotaClient::AboutToClearOrigins( diff --git a/dom/quota/ActorsParent.cpp b/dom/quota/ActorsParent.cpp index fff61f50fd5f..1e9cf730efb2 100644 --- a/dom/quota/ActorsParent.cpp +++ b/dom/quota/ActorsParent.cpp @@ -47,6 +47,7 @@ #include "mozilla/net/MozURL.h" #include "mozilla/Mutex.h" #include "mozilla/Preferences.h" +#include "mozilla/ResultExtensions.h" #include "mozilla/Services.h" #include "mozilla/StaticPrefs_dom.h" #include "mozilla/StaticPtr.h" @@ -9146,14 +9147,18 @@ Result QuotaUsageRequestBase::GetUsageForOriginEntries( MOZ_ASSERT(client); if (aInitialized) { - rv = client->GetUsageForOrigin(aPersistenceType, aGroup, aOrigin, - mCanceled, &usageInfo); + auto usageInfoOrErr = client->GetUsageForOrigin(aPersistenceType, aGroup, + aOrigin, mCanceled); + if (NS_WARN_IF(usageInfoOrErr.isErr())) { + return usageInfoOrErr.propagateErr(); + } + usageInfo += usageInfoOrErr.inspect(); } else { rv = client->InitOrigin(aPersistenceType, aGroup, aOrigin, mCanceled, &usageInfo); - } - if (NS_WARN_IF(NS_FAILED(rv))) { - return Err(rv); + if (NS_WARN_IF(NS_FAILED(rv))) { + return Err(rv); + } } } if (NS_WARN_IF(NS_FAILED(rv))) { diff --git a/dom/quota/Client.h b/dom/quota/Client.h index a2f3339143f2..75e445b77fe6 100644 --- a/dom/quota/Client.h +++ b/dom/quota/Client.h @@ -98,11 +98,9 @@ class Client { const AtomicBool& aCanceled, UsageInfo* aUsageInfo) = 0; - virtual nsresult GetUsageForOrigin(PersistenceType aPersistenceType, - const nsACString& aGroup, - const nsACString& aOrigin, - const AtomicBool& aCanceled, - UsageInfo* aUsageInfo) = 0; + virtual Result GetUsageForOrigin( + PersistenceType aPersistenceType, const nsACString& aGroup, + const nsACString& aOrigin, const AtomicBool& aCanceled) = 0; // This method is called when origins are about to be cleared // (except the case when clearing is triggered by the origin eviction). diff --git a/dom/simpledb/ActorsParent.cpp b/dom/simpledb/ActorsParent.cpp index 237341ebb873..643e8abd1c54 100644 --- a/dom/simpledb/ActorsParent.cpp +++ b/dom/simpledb/ActorsParent.cpp @@ -452,11 +452,9 @@ class QuotaClient final : public mozilla::dom::quota::Client { const AtomicBool& aCanceled, UsageInfo* aUsageInfo) override; - nsresult GetUsageForOrigin(PersistenceType aPersistenceType, - const nsACString& aGroup, - const nsACString& aOrigin, - const AtomicBool& aCanceled, - UsageInfo* aUsageInfo) override; + Result GetUsageForOrigin( + PersistenceType aPersistenceType, const nsACString& aGroup, + const nsACString& aOrigin, const AtomicBool& aCanceled) override; void OnOriginClearCompleted(PersistenceType aPersistenceType, const nsACString& aOrigin) override; @@ -1632,21 +1630,23 @@ nsresult QuotaClient::InitOrigin(PersistenceType aPersistenceType, UsageInfo* aUsageInfo) { AssertIsOnIOThread(); - if (!aUsageInfo) { - return NS_OK; + if (aUsageInfo) { + auto usageInfoOrErr = + GetUsageForOrigin(aPersistenceType, aGroup, aOrigin, aCanceled); + if (NS_WARN_IF(usageInfoOrErr.isErr())) { + return usageInfoOrErr.unwrapErr(); + } + + *aUsageInfo = usageInfoOrErr.unwrap(); } - return GetUsageForOrigin(aPersistenceType, aGroup, aOrigin, aCanceled, - aUsageInfo); + return NS_OK; } -nsresult QuotaClient::GetUsageForOrigin(PersistenceType aPersistenceType, - const nsACString& aGroup, - const nsACString& aOrigin, - const AtomicBool& aCanceled, - UsageInfo* aUsageInfo) { +Result QuotaClient::GetUsageForOrigin( + PersistenceType aPersistenceType, const nsACString& aGroup, + const nsACString& aOrigin, const AtomicBool& aCanceled) { AssertIsOnIOThread(); - MOZ_ASSERT(aUsageInfo); QuotaManager* quotaManager = QuotaManager::Get(); MOZ_ASSERT(quotaManager); @@ -1655,14 +1655,14 @@ nsresult QuotaClient::GetUsageForOrigin(PersistenceType aPersistenceType, nsresult rv = quotaManager->GetDirectoryForOrigin(aPersistenceType, aOrigin, getter_AddRefs(directory)); if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + return Err(rv); } MOZ_ASSERT(directory); rv = directory->Append(NS_LITERAL_STRING(SDB_DIRECTORY_NAME)); if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + return Err(rv); } DebugOnly exists; @@ -1671,16 +1671,17 @@ nsresult QuotaClient::GetUsageForOrigin(PersistenceType aPersistenceType, nsCOMPtr entries; rv = directory->GetDirectoryEntries(getter_AddRefs(entries)); if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + return Err(rv); } + UsageInfo res; bool hasMore; while (NS_SUCCEEDED((rv = entries->HasMoreElements(&hasMore))) && hasMore && !aCanceled) { nsCOMPtr entry; rv = entries->GetNext(getter_AddRefs(entry)); if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + return Err(rv); } nsCOMPtr file = do_QueryInterface(entry); @@ -1689,19 +1690,19 @@ nsresult QuotaClient::GetUsageForOrigin(PersistenceType aPersistenceType, nsAutoString leafName; rv = file->GetLeafName(leafName); if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + return Err(rv); } if (StringEndsWith(leafName, kSDBSuffix)) { int64_t fileSize; rv = file->GetFileSize(&fileSize); if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + return Err(rv); } MOZ_ASSERT(fileSize >= 0); - aUsageInfo->IncrementDatabaseUsage(Some(uint64_t(fileSize))); + res.IncrementDatabaseUsage(Some(uint64_t(fileSize))); continue; } @@ -1709,10 +1710,10 @@ nsresult QuotaClient::GetUsageForOrigin(PersistenceType aPersistenceType, UNKNOWN_FILE_WARNING(leafName); } if (NS_WARN_IF(NS_FAILED(rv))) { - return rv; + return Err(rv); } - return NS_OK; + return res; } void QuotaClient::OnOriginClearCompleted(PersistenceType aPersistenceType,