From 0d54e75ac363d0b128443d6360569401f327b868 Mon Sep 17 00:00:00 2001 From: Jan Varga Date: Wed, 3 May 2023 02:05:11 +0000 Subject: [PATCH] Bug 1781201 - Stop using GetInfoFromPrincipal in the parent; r=hsingh Quota clients should use GetInfoFromValidatedPrinciplaInfo in the parent because that method will eventually generate unique anonymous origins for private browsing. This patch also moves some calls from the main thread to the PBackground thread because GetInfoFromValidatedPrinciplaInfo can run on any thread. Differential Revision: https://phabricator.services.mozilla.com/D176871 --- dom/cache/Context.cpp | 12 ++++-- dom/indexedDB/ActorsParent.cpp | 74 ++++++++++++++++------------------ dom/simpledb/ActorsParent.cpp | 36 ++++++++--------- 3 files changed, 59 insertions(+), 63 deletions(-) diff --git a/dom/cache/Context.cpp b/dom/cache/Context.cpp index 0254deb702eb..803d5a0ea5cf 100644 --- a/dom/cache/Context.cpp +++ b/dom/cache/Context.cpp @@ -18,6 +18,7 @@ #include "mozilla/dom/quota/DirectoryLock.h" #include "mozilla/dom/quota/QuotaManager.h" #include "mozilla/dom/quota/ResultExtensions.h" +#include "mozilla/ipc/PBackgroundSharedTypes.h" #include "mozIStorageConnection.h" #include "nsIPrincipal.h" #include "nsIRunnable.h" @@ -215,6 +216,7 @@ class Context::QuotaInitRunnable final : public nsIRunnable, SafeRefPtr mInitAction; nsCOMPtr mInitiatingEventTarget; nsresult mResult; + Maybe mPrincipalInfo; Maybe mDirectoryMetadata; RefPtr mDirectoryLock; State mState; @@ -326,10 +328,11 @@ Context::QuotaInitRunnable::Run() { nsCOMPtr principal = mManager->GetManagerId().Principal(); - QM_TRY_UNWRAP(auto principalMetadata, - QuotaManager::GetInfoFromPrincipal(principal)); + mozilla::ipc::PrincipalInfo principalInfo; + QM_TRY( + MOZ_TO_RESULT(PrincipalToPrincipalInfo(principal, &principalInfo))); - mDirectoryMetadata.emplace(std::move(principalMetadata)); + mPrincipalInfo.emplace(std::move(principalInfo)); mState = STATE_CREATE_QUOTA_MANAGER; @@ -354,6 +357,9 @@ Context::QuotaInitRunnable::Run() { break; } + mDirectoryMetadata.emplace( + QuotaManager::GetInfoFromValidatedPrincipalInfo(*mPrincipalInfo)); + QM_TRY(QuotaManager::EnsureCreated(), QM_PROPAGATE, [&resolver](const auto rv) { resolver->Resolve(rv); }); diff --git a/dom/indexedDB/ActorsParent.cpp b/dom/indexedDB/ActorsParent.cpp index 80843a735949..c2bfe5074b42 100644 --- a/dom/indexedDB/ActorsParent.cpp +++ b/dom/indexedDB/ActorsParent.cpp @@ -14633,14 +14633,6 @@ nsresult FactoryOp::Open() { } } - const DatabaseMetadata& metadata = mCommonParams.metadata(); - - QuotaManager::GetStorageId(metadata.persistenceType(), - mOriginMetadata.mOrigin, Client::IDB, mDatabaseId); - - mDatabaseId.Append('*'); - mDatabaseId.Append(NS_ConvertUTF16toUTF8(metadata.name())); - MOZ_ASSERT(permission == PermissionValue::kPermissionAllowed); mState = State::FinishOpen; @@ -14846,30 +14838,16 @@ Result FactoryOp::CheckPermission( mChromeWriteAccessAllowed = true; } - if (State::Initial == mState) { - mOriginMetadata = {QuotaManager::GetInfoForChrome(), persistenceType}; - - MOZ_ASSERT(QuotaManager::IsOriginInternal(mOriginMetadata.mOrigin)); - - mEnforcingQuota = false; - } - return PermissionValue::kPermissionAllowed; } MOZ_ASSERT(principalInfo.type() == PrincipalInfo::TContentPrincipalInfo); - QM_TRY_INSPECT(const auto& principal, - PrincipalInfoToPrincipal(principalInfo)); - - QM_TRY_UNWRAP(auto principalMetadata, - QuotaManager::GetInfoFromPrincipal(principal)); - QM_TRY_INSPECT( const auto& permission, - ([persistenceType, &origin = principalMetadata.mOrigin, - &principal = - *principal]() -> mozilla::Result { + ([persistenceType, + origin = QuotaManager::GetOriginFromValidatedPrincipalInfo( + principalInfo)]() -> mozilla::Result { if (persistenceType == PERSISTENCE_TYPE_PERSISTENT) { if (QuotaManager::IsOriginInternal(origin)) { return PermissionValue::kPermissionAllowed; @@ -14879,13 +14857,6 @@ Result FactoryOp::CheckPermission( return PermissionValue::kPermissionAllowed; })()); - if (permission != PermissionValue::kPermissionDenied && - State::Initial == mState) { - mOriginMetadata = {std::move(principalMetadata), persistenceType}; - - mEnforcingQuota = persistenceType != PERSISTENCE_TYPE_PERSISTENT; - } - return permission; } @@ -14942,7 +14913,7 @@ bool FactoryOp::CheckAtLeastOneAppHasPermission( nsresult FactoryOp::FinishOpen() { AssertIsOnOwningThread(); MOZ_ASSERT(mState == State::FinishOpen); - MOZ_ASSERT(!mOriginMetadata.mOrigin.IsEmpty()); + MOZ_ASSERT(mOriginMetadata.mOrigin.IsEmpty()); MOZ_ASSERT(!mDirectoryLock); if (NS_WARN_IF(QuotaClient::IsShuttingDownOnBackgroundThread()) || @@ -14951,11 +14922,35 @@ nsresult FactoryOp::FinishOpen() { return NS_ERROR_DOM_INDEXEDDB_UNKNOWN_ERR; } - QM_TRY(QuotaManager::EnsureCreated()); + const PrincipalInfo& principalInfo = mCommonParams.principalInfo(); - const PersistenceType persistenceType = - mCommonParams.metadata().persistenceType(); - MOZ_ASSERT(mOriginMetadata.mPersistenceType == persistenceType); + const DatabaseMetadata& metadata = mCommonParams.metadata(); + + const PersistenceType persistenceType = metadata.persistenceType(); + + if (principalInfo.type() == PrincipalInfo::TSystemPrincipalInfo) { + mOriginMetadata = {QuotaManager::GetInfoForChrome(), persistenceType}; + + MOZ_ASSERT(QuotaManager::IsOriginInternal(mOriginMetadata.mOrigin)); + + mEnforcingQuota = false; + } else { + MOZ_ASSERT(principalInfo.type() == PrincipalInfo::TContentPrincipalInfo); + + mOriginMetadata = { + QuotaManager::GetInfoFromValidatedPrincipalInfo(principalInfo), + persistenceType}; + + mEnforcingQuota = persistenceType != PERSISTENCE_TYPE_PERSISTENT; + } + + QuotaManager::GetStorageId(persistenceType, mOriginMetadata.mOrigin, + Client::IDB, mDatabaseId); + + mDatabaseId.Append('*'); + mDatabaseId.Append(NS_ConvertUTF16toUTF8(metadata.name())); + + QM_TRY(QuotaManager::EnsureCreated()); QuotaManager* const quotaManager = QuotaManager::Get(); MOZ_ASSERT(quotaManager); @@ -14964,7 +14959,7 @@ nsresult FactoryOp::FinishOpen() { // XXX: For what reason? QM_TRY_UNWRAP( mDatabaseFilePath, - ([this, quotaManager]() -> mozilla::Result { + ([this, metadata, quotaManager]() -> mozilla::Result { QM_TRY_INSPECT(const auto& dbFile, quotaManager->GetOriginDirectory(mOriginMetadata)); @@ -14972,8 +14967,7 @@ nsresult FactoryOp::FinishOpen() { NS_LITERAL_STRING_FROM_CSTRING(IDB_DIRECTORY_NAME)))); QM_TRY(MOZ_TO_RESULT(dbFile->Append( - GetDatabaseFilenameBase(mCommonParams.metadata().name()) + - kSQLiteSuffix))); + GetDatabaseFilenameBase(metadata.name()) + kSQLiteSuffix))); QM_TRY_RETURN( MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsString, dbFile, GetPath)); diff --git a/dom/simpledb/ActorsParent.cpp b/dom/simpledb/ActorsParent.cpp index 183b07a1d832..8735cd53b961 100644 --- a/dom/simpledb/ActorsParent.cpp +++ b/dom/simpledb/ActorsParent.cpp @@ -145,7 +145,7 @@ class Connection final : public PBackgroundSDBConnectionParent { PersistenceType GetPersistenceType() const { return mPersistenceType; } const PrincipalInfo& GetPrincipalInfo() const { - MOZ_ASSERT(NS_IsMainThread()); + AssertIsOnBackgroundThread(); return mPrincipalInfo; } @@ -1081,24 +1081,6 @@ nsresult OpenOp::Open() { return NS_ERROR_UNEXPECTED; } - PersistenceType persistenceType = GetConnection()->GetPersistenceType(); - - const PrincipalInfo& principalInfo = GetConnection()->GetPrincipalInfo(); - - if (principalInfo.type() == PrincipalInfo::TSystemPrincipalInfo) { - mOriginMetadata = {QuotaManager::GetInfoForChrome(), persistenceType}; - } else { - MOZ_ASSERT(principalInfo.type() == PrincipalInfo::TContentPrincipalInfo); - - QM_TRY_INSPECT(const auto& principal, - PrincipalInfoToPrincipal(principalInfo)); - - QM_TRY_UNWRAP(auto principalMetadata, - QuotaManager::GetInfoFromPrincipal(principal)); - - mOriginMetadata = {std::move(principalMetadata), persistenceType}; - } - mState = State::FinishOpen; MOZ_ALWAYS_SUCCEEDS(OwningEventTarget()->Dispatch(this, NS_DISPATCH_NORMAL)); @@ -1107,7 +1089,7 @@ nsresult OpenOp::Open() { nsresult OpenOp::FinishOpen() { AssertIsOnOwningThread(); - MOZ_ASSERT(!mOriginMetadata.mOrigin.IsEmpty()); + MOZ_ASSERT(mOriginMetadata.mOrigin.IsEmpty()); MOZ_ASSERT(!mDirectoryLock); MOZ_ASSERT(mState == State::FinishOpen); @@ -1116,6 +1098,20 @@ nsresult OpenOp::FinishOpen() { return NS_ERROR_ABORT; } + const PrincipalInfo& principalInfo = GetConnection()->GetPrincipalInfo(); + + PersistenceType persistenceType = GetConnection()->GetPersistenceType(); + + if (principalInfo.type() == PrincipalInfo::TSystemPrincipalInfo) { + mOriginMetadata = {QuotaManager::GetInfoForChrome(), persistenceType}; + } else { + MOZ_ASSERT(principalInfo.type() == PrincipalInfo::TContentPrincipalInfo); + + mOriginMetadata = { + QuotaManager::GetInfoFromValidatedPrincipalInfo(principalInfo), + persistenceType}; + } + if (gOpenConnections) { for (const auto& connection : *gOpenConnections) { if (connection->Origin() == mOriginMetadata.mOrigin &&