Bug 1671369 - Change GetBodyUsage to return a Result. r=dom-workers-and-storage-reviewers,ttung

Differential Revision: https://phabricator.services.mozilla.com/D93769
This commit is contained in:
Simon Giesecke 2020-11-17 09:04:23 +00:00
Родитель 923504afa0
Коммит 56ab947139
1 изменённых файлов: 71 добавлений и 71 удалений

142
dom/cache/QuotaClient.cpp поставляемый
Просмотреть файл

@ -34,85 +34,82 @@ using mozilla::ipc::AssertIsOnBackgroundThread;
namespace {
nsresult GetBodyUsage(nsIFile* aMorgueDir, const Atomic<bool>& aCanceled,
UsageInfo* aUsageInfo, const bool aInitializing) {
Result<UsageInfo, nsresult> GetBodyUsage(nsIFile& aMorgueDir,
const Atomic<bool>& aCanceled,
const bool aInitializing) {
AssertIsOnIOThread();
nsCOMPtr<nsIDirectoryEnumerator> entries;
nsresult rv = aMorgueDir->GetDirectoryEntries(getter_AddRefs(entries));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
UsageInfo usageInfo;
nsCOMPtr<nsIFile> bodyDir;
while (NS_SUCCEEDED(rv = entries->GetNextFile(getter_AddRefs(bodyDir))) &&
bodyDir && !aCanceled) {
if (NS_WARN_IF(QuotaManager::IsShuttingDown())) {
return NS_ERROR_ABORT;
}
bool isDir;
rv = bodyDir->IsDirectory(&isDir);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
// XXX The following loop (including the cancellation check) is very similar
// to QuotaClient::GetDatabaseFilenames in dom/indexedDB/ActorsParent.cpp
// (Also, it is a fallible variant of std::reduce)
CACHE_TRY_INSPECT(const auto& entries, MOZ_TO_RESULT_INVOKE_TYPED(
nsCOMPtr<nsIDirectoryEnumerator>,
aMorgueDir, GetDirectoryEntries));
if (!isDir) {
QuotaInfo dummy;
DebugOnly<nsresult> result =
RemoveNsIFile(dummy, bodyDir, /* aTrackQuota */ false);
// Try to remove the unexpected files, and keep moving on even if it fails
// because it might be created by virus or the operation system
MOZ_ASSERT(NS_SUCCEEDED(result));
continue;
}
CACHE_TRY(CollectEach(
[&entries, &aCanceled]() -> Result<nsCOMPtr<nsIFile>, nsresult> {
if (aCanceled) {
return nsCOMPtr<nsIFile>{};
}
const QuotaInfo dummy;
const auto getUsage = [&aUsageInfo](nsIFile* bodyFile,
const nsACString& leafName,
bool& fileDeleted) {
MOZ_DIAGNOSTIC_ASSERT(bodyFile);
Unused << leafName;
CACHE_TRY_RETURN(MOZ_TO_RESULT_INVOKE_TYPED(nsCOMPtr<nsIFile>, entries,
GetNextFile));
},
[&usageInfo, aInitializing](
const nsCOMPtr<nsIFile>& bodyDir) -> Result<Ok, nsresult> {
CACHE_TRY(OkIf(!QuotaManager::IsShuttingDown()), Err(NS_ERROR_ABORT));
int64_t fileSize;
nsresult rv = bodyFile->GetFileSize(&fileSize);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
MOZ_DIAGNOSTIC_ASSERT(fileSize >= 0);
// FIXME: Separate file usage and database usage in OriginInfo so that the
// workaround for treating body file size as database usage can be
// removed.
//
// This is needed because we want to remove the mutex lock for padding
// files. The lock is needed because the padding file is accessed on the
// QM IO thread while getting origin usage and is accessed on the Cache IO
// thread in normal Cache operations.
// Using the cached usage in QM while getting origin usage can remove the
// access on the QM IO thread and thus we can remove the mutex lock.
// However, QM only separates usage types in initialization, and the
// separation is gone after that. So, before extending the separation of
// usage types in QM, this is a workaround to avoid the file usage
// mismatching in our tests. Note that file usage hasn't been exposed to
// users yet.
*aUsageInfo += DatabaseUsageType(Some(fileSize));
CACHE_TRY_INSPECT(const bool& isDir,
MOZ_TO_RESULT_INVOKE(bodyDir, IsDirectory));
fileDeleted = false;
if (!isDir) {
const DebugOnly<nsresult> result =
RemoveNsIFile(QuotaInfo{}, bodyDir, /* aTrackQuota */ false);
// Try to remove the unexpected files, and keep moving on even if it
// fails because it might be created by virus or the operation system
MOZ_ASSERT(NS_SUCCEEDED(result));
return Ok{};
}
return NS_OK;
};
rv = BodyTraverseFiles(dummy, bodyDir, getUsage,
/* aCanRemoveFiles */
aInitializing,
/* aTrackQuota */ false);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
const auto getUsage = [&usageInfo](nsIFile* bodyFile,
const nsACString& leafName,
bool& fileDeleted) -> nsresult {
MOZ_DIAGNOSTIC_ASSERT(bodyFile);
Unused << leafName;
return NS_OK;
CACHE_TRY_INSPECT(const int64_t& fileSize,
MOZ_TO_RESULT_INVOKE(bodyFile, GetFileSize));
MOZ_DIAGNOSTIC_ASSERT(fileSize >= 0);
// FIXME: Separate file usage and database usage in OriginInfo so that
// the workaround for treating body file size as database usage can be
// removed.
//
// This is needed because we want to remove the mutex lock for padding
// files. The lock is needed because the padding file is accessed on
// the QM IO thread while getting origin usage and is accessed on the
// Cache IO thread in normal Cache operations. Using the cached usage
// in QM while getting origin usage can remove the access on the QM IO
// thread and thus we can remove the mutex lock. However, QM only
// separates usage types in initialization, and the separation is gone
// after that. So, before extending the separation of usage types in
// QM, this is a workaround to avoid the file usage mismatching in our
// tests. Note that file usage hasn't been exposed to users yet.
usageInfo += DatabaseUsageType(Some(fileSize));
fileDeleted = false;
return NS_OK;
};
CACHE_TRY(BodyTraverseFiles(QuotaInfo{}, bodyDir, getUsage,
/* aCanRemoveFiles */
aInitializing,
/* aTrackQuota */ false));
return Ok{};
}));
return usageInfo;
}
Result<int64_t, nsresult> LockedGetPaddingSizeFromDB(
@ -399,6 +396,7 @@ Result<UsageInfo, nsresult> CacheQuotaClient::GetUsageForOriginInternal(
// XXX The following loop (including the cancellation check) is very similar
// to QuotaClient::GetDatabaseFilenames in dom/indexedDB/ActorsParent.cpp
// (Also, it is a fallible variant of std::reduce)
CACHE_TRY_INSPECT(const auto& entries,
MOZ_TO_RESULT_INVOKE_TYPED(nsCOMPtr<nsIDirectoryEnumerator>,
dir, GetDirectoryEntries));
@ -427,7 +425,9 @@ Result<UsageInfo, nsresult> CacheQuotaClient::GetUsageForOriginInternal(
if (leafName.EqualsLiteral("morgue")) {
// XXX This didn't use to warn for NS_ERROR_ABORT, should we keep
// that? (but it was and is propagated)
CACHE_TRY(GetBodyUsage(file, aCanceled, &usageInfo, aInitializing));
CACHE_TRY_INSPECT(const auto& bodyUsageInfo,
GetBodyUsage(*file, aCanceled, aInitializing));
usageInfo += bodyUsageInfo;
} else {
NS_WARNING("Unknown Cache directory found!");
}