Bug 1619592 - Rationalize PersistenceTypeTo* and PersistenceTypeFrom* methods; r=dom-workers-and-storage-reviewers,sg

Differential Revision: https://phabricator.services.mozilla.com/D65937

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jan Varga 2020-03-13 12:55:20 +00:00
Родитель b0060ff6a8
Коммит a87575bf55
10 изменённых файлов: 251 добавлений и 127 удалений

Просмотреть файл

@ -2930,8 +2930,10 @@ nsDOMWindowUtils::GetFileReferences(const nsAString& aDatabaseName, int64_t aId,
return NS_ERROR_UNEXPECTED;
}
quota::PersistenceType persistenceType =
quota::PersistenceTypeFromStorage(options.mStorage);
const quota::PersistenceType persistenceType =
options.mStorage.WasPassed()
? quota::PersistenceTypeFromStorageType(options.mStorage.Value())
: quota::PERSISTENCE_TYPE_DEFAULT;
RefPtr<IndexedDatabaseManager> mgr = IndexedDatabaseManager::Get();

Просмотреть файл

@ -13977,7 +13977,7 @@ void Database::Stringify(nsACString& aResult) const {
aResult.Append(kQuotaGenericDelimiter);
aResult.AppendLiteral("PersistenceType:");
aResult.Append(PersistenceTypeString(mPersistenceType));
aResult.Append(PersistenceTypeToString(mPersistenceType));
aResult.Append(kQuotaGenericDelimiter);
aResult.AppendLiteral("Closed:");
@ -18370,7 +18370,7 @@ nsresult Maintenance::DirectoryWork() {
// XXX This shouldn't be a special case...
persistenceTypeString.AssignLiteral("permanent");
} else {
PersistenceTypeToText(persistenceType, persistenceTypeString);
persistenceTypeString.Assign(PersistenceTypeToString(persistenceType));
}
nsCOMPtr<nsIFile> persistenceDir;
@ -18795,7 +18795,7 @@ void DatabaseMaintenance::Stringify(nsACString& aResult) const {
aResult.Append(kQuotaGenericDelimiter);
aResult.AppendLiteral("PersistenceType:");
aResult.Append(PersistenceTypeString(mPersistenceType));
aResult.Append(PersistenceTypeToString(mPersistenceType));
aResult.Append(kQuotaGenericDelimiter);
aResult.AppendLiteral("Duration:");
@ -20603,7 +20603,7 @@ void FactoryOp::Stringify(nsACString& aResult) const {
aResult.AppendLiteral("PersistenceType:");
aResult.Append(
PersistenceTypeString(mCommonParams.metadata().persistenceType()));
PersistenceTypeToString(mCommonParams.metadata().persistenceType()));
aResult.Append(kQuotaGenericDelimiter);
aResult.AppendLiteral("Origin:");

Просмотреть файл

@ -624,7 +624,7 @@ StorageType IDBDatabase::Storage() const {
AssertIsOnOwningThread();
MOZ_ASSERT(mSpec);
return PersistenceTypeToStorage(mSpec->metadata().persistenceType());
return PersistenceTypeToStorageType(mSpec->metadata().persistenceType());
}
RefPtr<IDBRequest> IDBDatabase::CreateMutableFile(

Просмотреть файл

@ -635,8 +635,9 @@ RefPtr<IDBOpenDBRequest> IDBFactory::OpenInternal(
if (isInternal) {
// Chrome privilege and internal origins always get persistent storage.
persistenceType = PERSISTENCE_TYPE_PERSISTENT;
} else if (isAddon || StaticPrefs::dom_indexedDB_storageOption_enabled()) {
persistenceType = PersistenceTypeFromStorage(aStorageType);
} else if ((isAddon || StaticPrefs::dom_indexedDB_storageOption_enabled()) &&
aStorageType.WasPassed()) {
persistenceType = PersistenceTypeFromStorageType(aStorageType.Value());
} else {
persistenceType = PERSISTENCE_TYPE_DEFAULT;
}

Просмотреть файл

@ -2997,7 +2997,7 @@ void DirectoryLockImpl::Log() const {
if (mPersistenceType.IsNull()) {
persistenceType.AssignLiteral("null");
} else {
PersistenceTypeToText(mPersistenceType.Value(), persistenceType);
persistenceType.Assign(PersistenceTypeToString(mPersistenceType.Value()));
}
QM_LOG((" mPersistenceType: %s", persistenceType.get()));
@ -4302,12 +4302,14 @@ nsresult QuotaManager::LoadQuota() {
return rv;
}
PersistenceType persistenceType;
rv = PersistenceTypeFromInt32(repositoryId, persistenceType);
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
const auto maybePersistenceType =
PersistenceTypeFromInt32(repositoryId, fallible);
if (NS_WARN_IF(maybePersistenceType.isNothing())) {
return NS_ERROR_FAILURE;
}
const PersistenceType persistenceType = maybePersistenceType.value();
nsCString origin;
rv = stmt->GetUTF8String(1, origin);
if (NS_WARN_IF(NS_FAILED(rv))) {
@ -6641,10 +6643,9 @@ nsresult QuotaManager::EnsureStorageIsInitialized() {
return rv;
}
nsCString name;
PersistenceTypeToText(persistenceType, name);
rv = insertStmt->BindUTF8StringByName(NS_LITERAL_CSTRING("name"), name);
rv = insertStmt->BindUTF8StringByName(
NS_LITERAL_CSTRING("name"),
PersistenceTypeToString(persistenceType));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}

Просмотреть файл

@ -0,0 +1,195 @@
/* -*- 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/. */
#include "PersistenceType.h"
namespace mozilla {
namespace dom {
namespace quota {
namespace {
constexpr auto kPersistentCString = NS_LITERAL_CSTRING("persistent");
constexpr auto kTemporaryCString = NS_LITERAL_CSTRING("temporary");
constexpr auto kDefaultCString = NS_LITERAL_CSTRING("default");
static_assert(PERSISTENCE_TYPE_PERSISTENT == 0 &&
PERSISTENCE_TYPE_TEMPORARY == 1 &&
PERSISTENCE_TYPE_DEFAULT == 2 &&
PERSISTENCE_TYPE_INVALID == 3,
"Incorrect enum values!");
template <PersistenceType type>
struct PersistenceTypeTraits;
template <>
struct PersistenceTypeTraits<PERSISTENCE_TYPE_PERSISTENT> {
template <typename T>
static T To();
static bool From(const nsACString& aString) {
return aString == kPersistentCString;
}
static bool From(const StorageType aStorageType) {
return aStorageType == StorageType::Persistent;
}
static bool From(const int32_t aInt32) { return aInt32 == 0; }
};
template <>
nsLiteralCString
PersistenceTypeTraits<PERSISTENCE_TYPE_PERSISTENT>::To<nsLiteralCString>() {
return kPersistentCString;
}
template <>
StorageType
PersistenceTypeTraits<PERSISTENCE_TYPE_PERSISTENT>::To<StorageType>() {
return StorageType::Persistent;
}
template <>
struct PersistenceTypeTraits<PERSISTENCE_TYPE_TEMPORARY> {
template <typename T>
static T To();
static bool From(const nsACString& aString) {
return aString == kTemporaryCString;
}
static bool From(const StorageType aStorageType) {
return aStorageType == StorageType::Temporary;
}
static bool From(const int32_t aInt32) { return aInt32 == 1; }
};
template <>
nsLiteralCString
PersistenceTypeTraits<PERSISTENCE_TYPE_TEMPORARY>::To<nsLiteralCString>() {
return kTemporaryCString;
}
template <>
StorageType
PersistenceTypeTraits<PERSISTENCE_TYPE_TEMPORARY>::To<StorageType>() {
return StorageType::Temporary;
}
template <>
struct PersistenceTypeTraits<PERSISTENCE_TYPE_DEFAULT> {
template <typename T>
static T To();
static bool From(const nsACString& aString) {
return aString == kDefaultCString;
}
static bool From(const StorageType aStorageType) {
return aStorageType == StorageType::Default;
}
static bool From(const int32_t aInt32) { return aInt32 == 2; }
};
template <>
nsLiteralCString
PersistenceTypeTraits<PERSISTENCE_TYPE_DEFAULT>::To<nsLiteralCString>() {
return kDefaultCString;
}
template <>
StorageType PersistenceTypeTraits<PERSISTENCE_TYPE_DEFAULT>::To<StorageType>() {
return StorageType::Default;
}
template <typename T>
Maybe<T> TypeTo_impl(const PersistenceType aPersistenceType) {
switch (aPersistenceType) {
case PERSISTENCE_TYPE_PERSISTENT:
return Some(PersistenceTypeTraits<PERSISTENCE_TYPE_PERSISTENT>::To<T>());
case PERSISTENCE_TYPE_TEMPORARY:
return Some(PersistenceTypeTraits<PERSISTENCE_TYPE_TEMPORARY>::To<T>());
case PERSISTENCE_TYPE_DEFAULT:
return Some(PersistenceTypeTraits<PERSISTENCE_TYPE_DEFAULT>::To<T>());
default:
return Nothing();
}
}
template <typename T>
Maybe<PersistenceType> TypeFrom_impl(const T& aData) {
if (PersistenceTypeTraits<PERSISTENCE_TYPE_PERSISTENT>::From(aData)) {
return Some(PERSISTENCE_TYPE_PERSISTENT);
}
if (PersistenceTypeTraits<PERSISTENCE_TYPE_TEMPORARY>::From(aData)) {
return Some(PERSISTENCE_TYPE_TEMPORARY);
}
if (PersistenceTypeTraits<PERSISTENCE_TYPE_DEFAULT>::From(aData)) {
return Some(PERSISTENCE_TYPE_DEFAULT);
}
return Nothing();
}
void BadPersistenceType() { MOZ_CRASH("Bad persistence type value!"); }
} // namespace
nsLiteralCString PersistenceTypeToString(
const PersistenceType aPersistenceType) {
const auto maybeString = TypeTo_impl<nsLiteralCString>(aPersistenceType);
if (maybeString.isNothing()) {
BadPersistenceType();
}
return maybeString.value();
}
Maybe<PersistenceType> PersistenceTypeFromString(const nsACString& aString,
const fallible_t&) {
return TypeFrom_impl(aString);
}
PersistenceType PersistenceTypeFromString(const nsACString& aString) {
const auto maybePersistenceType = TypeFrom_impl(aString);
if (maybePersistenceType.isNothing()) {
BadPersistenceType();
}
return maybePersistenceType.value();
}
StorageType PersistenceTypeToStorageType(
const PersistenceType aPersistenceType) {
const auto maybeStorageType = TypeTo_impl<StorageType>(aPersistenceType);
if (maybeStorageType.isNothing()) {
BadPersistenceType();
}
return maybeStorageType.value();
}
PersistenceType PersistenceTypeFromStorageType(const StorageType aStorageType) {
const auto maybePersistenceType = TypeFrom_impl(aStorageType);
if (maybePersistenceType.isNothing()) {
BadPersistenceType();
}
return maybePersistenceType.value();
}
Maybe<PersistenceType> PersistenceTypeFromInt32(const int32_t aInt32,
const fallible_t&) {
return TypeFrom_impl(aInt32);
}
} // namespace quota
} // namespace dom
} // namespace mozilla

Просмотреть файл

@ -29,104 +29,22 @@ static const PersistenceType kAllPersistenceTypes[] = {
PERSISTENCE_TYPE_PERSISTENT, PERSISTENCE_TYPE_TEMPORARY,
PERSISTENCE_TYPE_DEFAULT};
inline void PersistenceTypeToText(PersistenceType aPersistenceType,
nsACString& aText) {
switch (aPersistenceType) {
case PERSISTENCE_TYPE_PERSISTENT:
aText.AssignLiteral("persistent");
return;
case PERSISTENCE_TYPE_TEMPORARY:
aText.AssignLiteral("temporary");
return;
case PERSISTENCE_TYPE_DEFAULT:
aText.AssignLiteral("default");
return;
nsLiteralCString PersistenceTypeToString(PersistenceType aPersistenceType);
case PERSISTENCE_TYPE_INVALID:
default:
MOZ_CRASH("Bad persistence type value!");
}
}
Maybe<PersistenceType> PersistenceTypeFromString(const nsACString& aString,
const fallible_t&);
class PersistenceTypeString : public nsCString {
public:
explicit PersistenceTypeString(PersistenceType aPersistenceType) {
PersistenceTypeToText(aPersistenceType, *this);
}
};
PersistenceType PersistenceTypeFromString(const nsACString& aString);
inline PersistenceType PersistenceTypeFromText(const nsACString& aText) {
if (aText.EqualsLiteral("persistent")) {
return PERSISTENCE_TYPE_PERSISTENT;
}
StorageType PersistenceTypeToStorageType(PersistenceType aPersistenceType);
if (aText.EqualsLiteral("temporary")) {
return PERSISTENCE_TYPE_TEMPORARY;
}
PersistenceType PersistenceTypeFromStorageType(StorageType aStorageType);
if (aText.EqualsLiteral("default")) {
return PERSISTENCE_TYPE_DEFAULT;
}
MOZ_CRASH("Should never get here!");
}
inline nsresult PersistenceTypeFromInt32(int32_t aInt,
PersistenceType& aPersistenceType) {
static_assert(
PERSISTENCE_TYPE_PERSISTENT == 0 && PERSISTENCE_TYPE_TEMPORARY == 1 &&
PERSISTENCE_TYPE_DEFAULT == 2 && PERSISTENCE_TYPE_INVALID == 3,
"Incorrect enum values!");
if (aInt < PERSISTENCE_TYPE_PERSISTENT || aInt > PERSISTENCE_TYPE_DEFAULT) {
return NS_ERROR_FAILURE;
}
aPersistenceType = static_cast<PersistenceType>(aInt);
return NS_OK;
}
inline nsresult NullablePersistenceTypeFromText(
const nsACString& aText, Nullable<PersistenceType>* aPersistenceType) {
if (aText.IsVoid()) {
*aPersistenceType = Nullable<PersistenceType>();
return NS_OK;
}
if (aText.EqualsLiteral("persistent")) {
*aPersistenceType = Nullable<PersistenceType>(PERSISTENCE_TYPE_PERSISTENT);
return NS_OK;
}
if (aText.EqualsLiteral("temporary")) {
*aPersistenceType = Nullable<PersistenceType>(PERSISTENCE_TYPE_TEMPORARY);
return NS_OK;
}
if (aText.EqualsLiteral("default")) {
*aPersistenceType = Nullable<PersistenceType>(PERSISTENCE_TYPE_DEFAULT);
return NS_OK;
}
return NS_ERROR_FAILURE;
}
inline mozilla::dom::StorageType PersistenceTypeToStorage(
PersistenceType aPersistenceType) {
return mozilla::dom::StorageType(static_cast<int>(aPersistenceType));
}
inline PersistenceType PersistenceTypeFromStorage(
const Optional<mozilla::dom::StorageType>& aStorage) {
if (aStorage.WasPassed()) {
return PersistenceType(static_cast<int>(aStorage.Value()));
}
return PERSISTENCE_TYPE_DEFAULT;
}
Maybe<PersistenceType> PersistenceTypeFromInt32(int32_t aInt32,
const fallible_t&);
inline PersistenceType ComplementaryPersistenceType(
PersistenceType aPersistenceType) {
const PersistenceType aPersistenceType) {
MOZ_ASSERT(aPersistenceType == PERSISTENCE_TYPE_DEFAULT ||
aPersistenceType == PERSISTENCE_TYPE_TEMPORARY);

Просмотреть файл

@ -78,16 +78,16 @@ nsresult GetClearResetOriginParams(nsIPrincipal* aPrincipal,
return rv;
}
Nullable<PersistenceType> persistenceType;
rv = NullablePersistenceTypeFromText(aPersistenceType, &persistenceType);
if (NS_WARN_IF(NS_FAILED(rv))) {
return NS_ERROR_INVALID_ARG;
}
if (persistenceType.IsNull()) {
if (aPersistenceType.IsVoid()) {
aParams.persistenceTypeIsExplicit() = false;
} else {
aParams.persistenceType() = persistenceType.Value();
const auto maybePersistenceType =
PersistenceTypeFromString(aPersistenceType, fallible);
if (NS_WARN_IF(maybePersistenceType.isNothing())) {
return NS_ERROR_INVALID_ARG;
}
aParams.persistenceType() = maybePersistenceType.value();
aParams.persistenceTypeIsExplicit() = true;
}
@ -502,13 +502,13 @@ QuotaManagerService::InitStorageAndOrigin(nsIPrincipal* aPrincipal,
return rv;
}
Nullable<PersistenceType> persistenceType;
rv = NullablePersistenceTypeFromText(aPersistenceType, &persistenceType);
if (NS_WARN_IF(NS_FAILED(rv)) || persistenceType.IsNull()) {
const auto maybePersistenceType =
PersistenceTypeFromString(aPersistenceType, fallible);
if (NS_WARN_IF(maybePersistenceType.isNothing())) {
return NS_ERROR_INVALID_ARG;
}
params.persistenceType() = persistenceType.Value();
params.persistenceType() = maybePersistenceType.value();
if (aClientType.IsVoid()) {
params.clientTypeIsExplicit() = false;

Просмотреть файл

@ -68,6 +68,7 @@ UNIFIED_SOURCES += [
'FileStreams.cpp',
'MemoryOutputStream.cpp',
'nsIndexedDBProtocolHandler.cpp',
'PersistenceType.cpp',
'QuotaCommon.cpp',
'QuotaManagerService.cpp',
'QuotaRequests.cpp',

Просмотреть файл

@ -227,15 +227,21 @@ SDBConnection::Init(nsIPrincipal* aPrincipal,
return NS_ERROR_INVALID_ARG;
}
Nullable<PersistenceType> persistenceType;
rv = NullablePersistenceTypeFromText(aPersistenceType, &persistenceType);
if (NS_WARN_IF(NS_FAILED(rv))) {
return NS_ERROR_INVALID_ARG;
PersistenceType persistenceType;
if (aPersistenceType.IsVoid()) {
persistenceType = PERSISTENCE_TYPE_DEFAULT;
} else {
const auto maybePersistenceType =
PersistenceTypeFromString(aPersistenceType, fallible);
if (NS_WARN_IF(maybePersistenceType.isNothing())) {
return NS_ERROR_INVALID_ARG;
}
persistenceType = maybePersistenceType.value();
}
mPrincipalInfo = std::move(principalInfo);
mPersistenceType = persistenceType.IsNull() ? PERSISTENCE_TYPE_DEFAULT
: persistenceType.Value();
mPersistenceType = persistenceType;
return NS_OK;
}