diff --git a/dom/cache/Action.h b/dom/cache/Action.h index a9d861c3ed1d..0e7c54b66030 100644 --- a/dom/cache/Action.h +++ b/dom/cache/Action.h @@ -55,9 +55,10 @@ class Action : public SafeRefCounted { // Note: Action should hold Resolver ref until its ready to call Resolve(). // Note: The "target" thread is determined when the Action is scheduled on // Context. The Action should not assume any particular thread is used. - virtual void RunOnTarget(SafeRefPtr aResolver, - const Maybe& aClientMetadata, - Data* aOptionalData) = 0; + virtual void RunOnTarget( + SafeRefPtr aResolver, + const Maybe& aDirectoryMetadata, + Data* aOptionalData) = 0; // Called on initiating thread when the Action is canceled. The Action is // responsible for calling Resolver::Resolve() as normal; either with a diff --git a/dom/cache/Context.cpp b/dom/cache/Context.cpp index 67d7d33dbee0..318a60f4911c 100644 --- a/dom/cache/Context.cpp +++ b/dom/cache/Context.cpp @@ -25,14 +25,14 @@ namespace { using mozilla::dom::cache::Action; -using mozilla::dom::cache::ClientMetadata; +using mozilla::dom::cache::CacheDirectoryMetadata; class NullAction final : public Action { public: NullAction() = default; virtual void RunOnTarget(mozilla::SafeRefPtr aResolver, - const mozilla::Maybe&, + const mozilla::Maybe&, Data*) override { // Resolve success immediately. This Action does no actual work. MOZ_DIAGNOSTIC_ASSERT(aResolver); @@ -216,7 +216,7 @@ class Context::QuotaInitRunnable final : public nsIRunnable, SafeRefPtr mInitAction; nsCOMPtr mInitiatingEventTarget; nsresult mResult; - Maybe mClientMetadata; + Maybe mDirectoryMetadata; RefPtr mDirectoryLock; State mState; Atomic mCanceled; @@ -233,9 +233,10 @@ void Context::QuotaInitRunnable::OpenDirectory() { MOZ_DIAGNOSTIC_ASSERT(QuotaManager::Get()); RefPtr directoryLock = - QuotaManager::Get()->CreateDirectoryLock( - PERSISTENCE_TYPE_DEFAULT, *mClientMetadata, quota::Client::DOMCACHE, - /* aExclusive */ false); + QuotaManager::Get()->CreateDirectoryLock(PERSISTENCE_TYPE_DEFAULT, + *mDirectoryMetadata, + quota::Client::DOMCACHE, + /* aExclusive */ false); // DirectoryLock::Acquire() will hold a reference to us as a listener. We will // then get DirectoryLockAcquired() on the owning thread when it is safe to @@ -253,7 +254,7 @@ void Context::QuotaInitRunnable::DirectoryLockAcquired(DirectoryLock* aLock) { mDirectoryLock = aLock; MOZ_DIAGNOSTIC_ASSERT(mDirectoryLock->Id() >= 0); - mClientMetadata->mDirectoryLockId = mDirectoryLock->Id(); + mDirectoryMetadata->mDirectoryLockId = mDirectoryLock->Id(); if (mCanceled) { Complete(NS_ERROR_ABORT); @@ -353,7 +354,7 @@ Context::QuotaInitRunnable::Run() { QM_TRY_UNWRAP(auto principalMetadata, QuotaManager::GetInfoFromPrincipal(principal)); - mClientMetadata.emplace(std::move(principalMetadata)); + mDirectoryMetadata.emplace(std::move(principalMetadata)); mState = STATE_CREATE_QUOTA_MANAGER; @@ -416,10 +417,10 @@ Context::QuotaInitRunnable::Run() { QM_TRY( MOZ_TO_RESULT(quotaManager->EnsureTemporaryStorageIsInitialized())); - QM_TRY_UNWRAP(mClientMetadata->mDir, + QM_TRY_UNWRAP(mDirectoryMetadata->mDir, quotaManager ->EnsureTemporaryOriginIsInitialized( - PERSISTENCE_TYPE_DEFAULT, *mClientMetadata) + PERSISTENCE_TYPE_DEFAULT, *mDirectoryMetadata) .map([](const auto& res) { return res.first; })); mState = STATE_RUN_ON_TARGET; @@ -444,7 +445,7 @@ Context::QuotaInitRunnable::Run() { // Execute the provided initialization Action. The Action must Resolve() // before returning. - mInitAction->RunOnTarget(resolver.clonePtr(), mClientMetadata, mData); + mInitAction->RunOnTarget(resolver.clonePtr(), mDirectoryMetadata, mData); MOZ_DIAGNOSTIC_ASSERT(resolver->Resolved()); mData = nullptr; @@ -453,7 +454,7 @@ Context::QuotaInitRunnable::Run() { // the marker file. If it wasn't opened successfully, then no need to // create a marker file anyway. if (NS_SUCCEEDED(resolver->Result())) { - MOZ_ALWAYS_SUCCEEDS(CreateMarkerFile(*mClientMetadata)); + MOZ_ALWAYS_SUCCEEDS(CreateMarkerFile(*mDirectoryMetadata)); } break; @@ -462,7 +463,8 @@ Context::QuotaInitRunnable::Run() { case STATE_COMPLETING: { NS_ASSERT_OWNINGTHREAD(QuotaInitRunnable); mInitAction->CompleteOnInitiatingThread(mResult); - mContext->OnQuotaInit(mResult, mClientMetadata, mDirectoryLock.forget()); + mContext->OnQuotaInit(mResult, mDirectoryMetadata, + mDirectoryLock.forget()); mState = STATE_COMPLETE; // Explicitly cleanup here as the destructor could fire on any of @@ -493,12 +495,12 @@ class Context::ActionRunnable final : public nsIRunnable, public: ActionRunnable(SafeRefPtr aContext, Data* aData, nsISerialEventTarget* aTarget, SafeRefPtr aAction, - const Maybe& aClientMetadata) + const Maybe& aDirectoryMetadata) : mContext(std::move(aContext)), mData(aData), mTarget(aTarget), mAction(std::move(aAction)), - mClientMetadata(aClientMetadata), + mDirectoryMetadata(aDirectoryMetadata), mInitiatingThread(GetCurrentEventTarget()), mState(STATE_INIT), mResult(NS_OK), @@ -507,7 +509,7 @@ class Context::ActionRunnable final : public nsIRunnable, // mData may be nullptr MOZ_DIAGNOSTIC_ASSERT(mTarget); MOZ_DIAGNOSTIC_ASSERT(mAction); - // mClientMetadata.mDir may be nullptr if QuotaInitRunnable failed + // mDirectoryMetadata.mDir may be nullptr if QuotaInitRunnable failed MOZ_DIAGNOSTIC_ASSERT(mInitiatingThread); } @@ -587,7 +589,7 @@ class Context::ActionRunnable final : public nsIRunnable, RefPtr mData; nsCOMPtr mTarget; SafeRefPtr mAction; - const Maybe mClientMetadata; + const Maybe mDirectoryMetadata; nsCOMPtr mInitiatingThread; State mState; nsresult mResult; @@ -649,7 +651,7 @@ Context::ActionRunnable::Run() { mExecutingRunOnTarget = true; mState = STATE_RUNNING; - mAction->RunOnTarget(SafeRefPtrFromThis(), mClientMetadata, mData); + mAction->RunOnTarget(SafeRefPtrFromThis(), mDirectoryMetadata, mData); mData = nullptr; @@ -921,8 +923,8 @@ Context::~Context() { // Note, this may set the mOrphanedData flag. mManager->RemoveContext(*this); - if (mClientMetadata && mClientMetadata->mDir && !mOrphanedData) { - MOZ_ALWAYS_SUCCEEDS(DeleteMarkerFile(*mClientMetadata)); + if (mDirectoryMetadata && mDirectoryMetadata->mDir && !mOrphanedData) { + MOZ_ALWAYS_SUCCEEDS(DeleteMarkerFile(*mDirectoryMetadata)); } if (mNextContext) { @@ -979,7 +981,7 @@ void Context::DispatchAction(SafeRefPtr aAction, bool aDoomData) { auto runnable = MakeSafeRefPtr(SafeRefPtrFromThis(), mData, mTarget, - std::move(aAction), mClientMetadata); + std::move(aAction), mDirectoryMetadata); if (aDoomData) { mData = nullptr; @@ -994,16 +996,16 @@ void Context::DispatchAction(SafeRefPtr aAction, bool aDoomData) { AddActivity(*runnable); } -void Context::OnQuotaInit(nsresult aRv, - const Maybe& aClientMetadata, - already_AddRefed aDirectoryLock) { +void Context::OnQuotaInit( + nsresult aRv, const Maybe& aDirectoryMetadata, + already_AddRefed aDirectoryLock) { NS_ASSERT_OWNINGTHREAD(Context); MOZ_DIAGNOSTIC_ASSERT(mInitRunnable); mInitRunnable = nullptr; - if (aClientMetadata) { - mClientMetadata.emplace(*aClientMetadata); + if (aDirectoryMetadata) { + mDirectoryMetadata.emplace(*aDirectoryMetadata); } // Always save the directory lock to ensure QuotaManager does not shutdown diff --git a/dom/cache/Context.h b/dom/cache/Context.h index 075908622131..b9ca4ab4dfc4 100644 --- a/dom/cache/Context.h +++ b/dom/cache/Context.h @@ -177,7 +177,8 @@ class Context final : public SafeRefCounted { void Init(Maybe aOldContext); void Start(); void DispatchAction(SafeRefPtr aAction, bool aDoomData = false); - void OnQuotaInit(nsresult aRv, const Maybe& aClientMetadata, + void OnQuotaInit(nsresult aRv, + const Maybe& aDirectoryMetadata, already_AddRefed aDirectoryLock); SafeRefPtr CreateThreadsafeHandle(); @@ -191,7 +192,7 @@ class Context final : public SafeRefCounted { RefPtr mData; State mState; bool mOrphanedData; - Maybe mClientMetadata; + Maybe mDirectoryMetadata; RefPtr mInitRunnable; SafeRefPtr mInitAction; nsTArray mPendingActions; diff --git a/dom/cache/DBAction.cpp b/dom/cache/DBAction.cpp index 002facb27b29..831faeff00af 100644 --- a/dom/cache/DBAction.cpp +++ b/dom/cache/DBAction.cpp @@ -32,19 +32,20 @@ using mozilla::dom::quota::PersistenceType; namespace { -nsresult WipeDatabase(const ClientMetadata& aClientMetadata, nsIFile& aDBFile) { +nsresult WipeDatabase(const CacheDirectoryMetadata& aDirectoryMetadata, + nsIFile& aDBFile) { QM_TRY_INSPECT(const auto& dbDir, MOZ_TO_RESULT_INVOKE_TYPED( nsCOMPtr, aDBFile, GetParent)); - QM_TRY(MOZ_TO_RESULT(RemoveNsIFile(aClientMetadata, aDBFile))); + QM_TRY(MOZ_TO_RESULT(RemoveNsIFile(aDirectoryMetadata, aDBFile))); // Note, the -wal journal file will be automatically deleted by sqlite when // the new database is created. No need to explicitly delete it here. // Delete the morgue as well. - QM_TRY(MOZ_TO_RESULT(BodyDeleteDir(aClientMetadata, *dbDir))); + QM_TRY(MOZ_TO_RESULT(BodyDeleteDir(aDirectoryMetadata, *dbDir))); - QM_TRY(MOZ_TO_RESULT(WipePaddingFile(aClientMetadata, dbDir))); + QM_TRY(MOZ_TO_RESULT(WipePaddingFile(aDirectoryMetadata, dbDir))); return NS_OK; } @@ -55,13 +56,14 @@ DBAction::DBAction(Mode aMode) : mMode(aMode) {} DBAction::~DBAction() = default; -void DBAction::RunOnTarget(SafeRefPtr aResolver, - const Maybe& aClientMetadata, - Data* aOptionalData) { +void DBAction::RunOnTarget( + SafeRefPtr aResolver, + const Maybe& aDirectoryMetadata, + Data* aOptionalData) { MOZ_ASSERT(!NS_IsMainThread()); MOZ_DIAGNOSTIC_ASSERT(aResolver); - MOZ_DIAGNOSTIC_ASSERT(aClientMetadata); - MOZ_DIAGNOSTIC_ASSERT(aClientMetadata->mDir); + MOZ_DIAGNOSTIC_ASSERT(aDirectoryMetadata); + MOZ_DIAGNOSTIC_ASSERT(aDirectoryMetadata->mDir); if (IsCanceled()) { aResolver->Resolve(NS_ERROR_ABORT); @@ -73,7 +75,7 @@ void DBAction::RunOnTarget(SafeRefPtr aResolver, }; QM_TRY_INSPECT(const auto& dbDir, - CloneFileAndAppend(*(aClientMetadata->mDir), u"cache"_ns), + CloneFileAndAppend(*(aDirectoryMetadata->mDir), u"cache"_ns), QM_VOID, resolveErr); nsCOMPtr conn; @@ -85,7 +87,7 @@ void DBAction::RunOnTarget(SafeRefPtr aResolver, // If there is no previous Action, then we must open one. if (!conn) { - QM_TRY_UNWRAP(conn, OpenConnection(*aClientMetadata, *dbDir), QM_VOID, + QM_TRY_UNWRAP(conn, OpenConnection(*aDirectoryMetadata, *dbDir), QM_VOID, resolveErr); MOZ_DIAGNOSTIC_ASSERT(conn); @@ -101,13 +103,13 @@ void DBAction::RunOnTarget(SafeRefPtr aResolver, } } - RunWithDBOnTarget(std::move(aResolver), *aClientMetadata, dbDir, conn); + RunWithDBOnTarget(std::move(aResolver), *aDirectoryMetadata, dbDir, conn); } Result, nsresult> DBAction::OpenConnection( - const ClientMetadata& aClientMetadata, nsIFile& aDBDir) { + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aDBDir) { MOZ_ASSERT(!NS_IsMainThread()); - MOZ_DIAGNOSTIC_ASSERT(aClientMetadata.mDirectoryLockId >= 0); + MOZ_DIAGNOSTIC_ASSERT(aDirectoryMetadata.mDirectoryLockId >= 0); QM_TRY_INSPECT(const bool& exists, MOZ_TO_RESULT_INVOKE(aDBDir, Exists)); @@ -119,30 +121,30 @@ Result, nsresult> DBAction::OpenConnection( QM_TRY_INSPECT(const auto& dbFile, CloneFileAndAppend(aDBDir, kCachesSQLiteFilename)); - QM_TRY_RETURN(OpenDBConnection(aClientMetadata, *dbFile)); + QM_TRY_RETURN(OpenDBConnection(aDirectoryMetadata, *dbFile)); } SyncDBAction::SyncDBAction(Mode aMode) : DBAction(aMode) {} SyncDBAction::~SyncDBAction() = default; -void SyncDBAction::RunWithDBOnTarget(SafeRefPtr aResolver, - const ClientMetadata& aClientMetadata, - nsIFile* aDBDir, - mozIStorageConnection* aConn) { +void SyncDBAction::RunWithDBOnTarget( + SafeRefPtr aResolver, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, + mozIStorageConnection* aConn) { MOZ_ASSERT(!NS_IsMainThread()); MOZ_DIAGNOSTIC_ASSERT(aResolver); MOZ_DIAGNOSTIC_ASSERT(aDBDir); MOZ_DIAGNOSTIC_ASSERT(aConn); - nsresult rv = RunSyncWithDBOnTarget(aClientMetadata, aDBDir, aConn); + nsresult rv = RunSyncWithDBOnTarget(aDirectoryMetadata, aDBDir, aConn); aResolver->Resolve(rv); } Result, nsresult> OpenDBConnection( - const ClientMetadata& aClientMetadata, nsIFile& aDBFile) { + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aDBFile) { MOZ_ASSERT(!NS_IsMainThread()); - MOZ_DIAGNOSTIC_ASSERT(aClientMetadata.mDirectoryLockId >= -1); + MOZ_DIAGNOSTIC_ASSERT(aDirectoryMetadata.mDirectoryLockId >= -1); // Use our default file:// protocol handler directly to construct the database // URL. This avoids any problems if a plugin registers a custom file:// @@ -156,9 +158,9 @@ Result, nsresult> OpenDBConnection( NewFileURIMutator, &aDBFile)); const nsCString directoryLockIdClause = - aClientMetadata.mDirectoryLockId >= 0 + aDirectoryMetadata.mDirectoryLockId >= 0 ? "&directoryLockId="_ns + - IntToCString(aClientMetadata.mDirectoryLockId) + IntToCString(aDirectoryMetadata.mDirectoryLockId) : EmptyCString(); nsCOMPtr dbFileUrl; @@ -182,14 +184,14 @@ Result, nsresult> OpenDBConnection( // Predicate. IsDatabaseCorruptionError, // Fallback. - ([&aClientMetadata, &aDBFile, &storageService, + ([&aDirectoryMetadata, &aDBFile, &storageService, &dbFileUrl](const nsresult rv) -> Result, nsresult> { NS_WARNING("Cache database corrupted. Recreating empty database."); // There is nothing else we can do to recover. Also, this data // can be deleted by QuotaManager at any time anyways. - QM_TRY(MOZ_TO_RESULT(WipeDatabase(aClientMetadata, aDBFile))); + QM_TRY(MOZ_TO_RESULT(WipeDatabase(aDirectoryMetadata, aDBFile))); QM_TRY_RETURN(MOZ_TO_RESULT_INVOKE_TYPED( nsCOMPtr, storageService, @@ -203,7 +205,7 @@ Result, nsresult> OpenDBConnection( // Close existing connection before wiping database. conn = nullptr; - QM_TRY(MOZ_TO_RESULT(WipeDatabase(aClientMetadata, aDBFile))); + QM_TRY(MOZ_TO_RESULT(WipeDatabase(aDirectoryMetadata, aDBFile))); QM_TRY_UNWRAP(conn, MOZ_TO_RESULT_INVOKE_TYPED( nsCOMPtr, storageService, diff --git a/dom/cache/DBAction.h b/dom/cache/DBAction.h index 3700512b24bd..2f16aa6dafae 100644 --- a/dom/cache/DBAction.h +++ b/dom/cache/DBAction.h @@ -19,7 +19,7 @@ namespace dom { namespace cache { Result, nsresult> OpenDBConnection( - const ClientMetadata& aClientMetadata, nsIFile& aDBFile); + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aDBFile); class DBAction : public Action { protected: @@ -35,18 +35,18 @@ class DBAction : public Action { // Just as the resolver must be ref'd until resolve, you may also // ref the DB connection. The connection can only be referenced from the // target thread and must be released upon resolve. - virtual void RunWithDBOnTarget(SafeRefPtr aResolver, - const ClientMetadata& aClientMetadata, - nsIFile* aDBDir, - mozIStorageConnection* aConn) = 0; + virtual void RunWithDBOnTarget( + SafeRefPtr aResolver, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, + mozIStorageConnection* aConn) = 0; private: void RunOnTarget(SafeRefPtr aResolver, - const Maybe& aClientMetadata, + const Maybe& aDirectoryMetadata, Data* aOptionalData) override; Result, nsresult> OpenConnection( - const ClientMetadata& aClientMetadata, nsIFile& aDBDir); + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aDBDir); const Mode mMode; }; @@ -58,15 +58,15 @@ class SyncDBAction : public DBAction { // Action objects are deleted through their base pointer virtual ~SyncDBAction(); - virtual nsresult RunSyncWithDBOnTarget(const ClientMetadata& aClientMetadata, - nsIFile* aDBDir, - mozIStorageConnection* aConn) = 0; + virtual nsresult RunSyncWithDBOnTarget( + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, + mozIStorageConnection* aConn) = 0; private: - virtual void RunWithDBOnTarget(SafeRefPtr aResolver, - const ClientMetadata& aClientMetadata, - nsIFile* aDBDir, - mozIStorageConnection* aConn) override; + virtual void RunWithDBOnTarget( + SafeRefPtr aResolver, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, + mozIStorageConnection* aConn) override; }; } // namespace cache diff --git a/dom/cache/FileUtils.cpp b/dom/cache/FileUtils.cpp index 6965fccc4677..7f0916eb3f4c 100644 --- a/dom/cache/FileUtils.cpp +++ b/dom/cache/FileUtils.cpp @@ -116,18 +116,18 @@ nsresult BodyCreateDir(nsIFile& aBaseDir) { return NS_OK; } -nsresult BodyDeleteDir(const ClientMetadata& aClientMetadata, +nsresult BodyDeleteDir(const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir) { QM_TRY_INSPECT(const auto& bodyDir, CloneFileAndAppend(aBaseDir, kMorgueDirectory)); - QM_TRY(MOZ_TO_RESULT(RemoveNsIFileRecursively(aClientMetadata, *bodyDir))); + QM_TRY(MOZ_TO_RESULT(RemoveNsIFileRecursively(aDirectoryMetadata, *bodyDir))); return NS_OK; } Result>, nsresult> BodyStartWriteStream( - const ClientMetadata& aClientMetadata, nsIFile& aBaseDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, nsIInputStream& aSource, void* aClosure, nsAsyncCopyCallbackFun aCallback) { MOZ_DIAGNOSTIC_ASSERT(aClosure); MOZ_DIAGNOSTIC_ASSERT(aCallback); @@ -155,7 +155,7 @@ Result>, nsresult> BodyStartWriteStream( QM_TRY_INSPECT( const auto& fileStream, - CreateFileOutputStream(PERSISTENCE_TYPE_DEFAULT, aClientMetadata, + CreateFileOutputStream(PERSISTENCE_TYPE_DEFAULT, aDirectoryMetadata, Client::DOMCACHE, tmpFile.get())); const auto compressed = @@ -201,22 +201,22 @@ nsresult BodyFinalizeWrite(nsIFile& aBaseDir, const nsID& aId) { } Result>, nsresult> BodyOpen( - const ClientMetadata& aClientMetadata, nsIFile& aBaseDir, const nsID& aId) { + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, + const nsID& aId) { QM_TRY_INSPECT(const auto& finalFile, BodyIdToFile(aBaseDir, aId, BODY_FILE_FINAL)); - QM_TRY_RETURN(CreateFileInputStream(PERSISTENCE_TYPE_DEFAULT, aClientMetadata, - Client::DOMCACHE, finalFile.get()) - .map([](NotNull>&& stream) { - return WrapNotNullUnchecked( - nsCOMPtr{stream.get()}); - })); + QM_TRY_RETURN( + CreateFileInputStream(PERSISTENCE_TYPE_DEFAULT, aDirectoryMetadata, + Client::DOMCACHE, finalFile.get()) + .map([](NotNull>&& stream) { + return WrapNotNullUnchecked(nsCOMPtr{stream.get()}); + })); } -nsresult BodyMaybeUpdatePaddingSize(const ClientMetadata& aClientMetadata, - nsIFile& aBaseDir, const nsID& aId, - const uint32_t aPaddingInfo, - int64_t* aPaddingSizeInOut) { +nsresult BodyMaybeUpdatePaddingSize( + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, + const nsID& aId, const uint32_t aPaddingInfo, int64_t* aPaddingSizeInOut) { MOZ_DIAGNOSTIC_ASSERT(aPaddingSizeInOut); QM_TRY_INSPECT(const auto& bodyFile, @@ -227,7 +227,7 @@ nsresult BodyMaybeUpdatePaddingSize(const ClientMetadata& aClientMetadata, int64_t fileSize = 0; RefPtr quotaObject = quotaManager->GetQuotaObject( - PERSISTENCE_TYPE_DEFAULT, aClientMetadata, Client::DOMCACHE, + PERSISTENCE_TYPE_DEFAULT, aDirectoryMetadata, Client::DOMCACHE, bodyFile.get(), -1, &fileSize); MOZ_DIAGNOSTIC_ASSERT(quotaObject); MOZ_DIAGNOSTIC_ASSERT(fileSize >= 0); @@ -249,32 +249,33 @@ nsresult BodyMaybeUpdatePaddingSize(const ClientMetadata& aClientMetadata, return NS_OK; } -nsresult BodyDeleteFiles(const ClientMetadata& aClientMetadata, +nsresult BodyDeleteFiles(const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, const nsTArray& aIdList) { for (const auto id : aIdList) { QM_TRY_INSPECT(const auto& bodyDir, BodyGetCacheDir(aBaseDir, id)); const auto removeFileForId = - [&aClientMetadata, &id]( + [&aDirectoryMetadata, &id]( nsIFile& bodyFile, const nsACString& leafName) -> Result { nsID fileId; QM_TRY(OkIf(fileId.Parse(leafName.BeginReading())), true, - ([&aClientMetadata, &bodyFile](const auto) { + ([&aDirectoryMetadata, &bodyFile](const auto) { DebugOnly result = RemoveNsIFile( - aClientMetadata, bodyFile, /* aTrackQuota */ false); + aDirectoryMetadata, bodyFile, /* aTrackQuota */ false); MOZ_ASSERT(NS_SUCCEEDED(result)); })); if (id.Equals(fileId)) { - DebugOnly result = RemoveNsIFile(aClientMetadata, bodyFile); + DebugOnly result = + RemoveNsIFile(aDirectoryMetadata, bodyFile); MOZ_ASSERT(NS_SUCCEEDED(result)); return true; } return false; }; - QM_TRY(MOZ_TO_RESULT(BodyTraverseFiles(aClientMetadata, *bodyDir, + QM_TRY(MOZ_TO_RESULT(BodyTraverseFiles(aDirectoryMetadata, *bodyDir, removeFileForId, /* aCanRemoveFiles */ false, /* aTrackQuota */ true))); @@ -346,9 +347,9 @@ nsresult DirectoryPaddingWrite(nsIFile& aBaseDir, } // namespace -nsresult BodyDeleteOrphanedFiles(const ClientMetadata& aClientMetadata, - nsIFile& aBaseDir, - const nsTArray& aKnownBodyIdList) { +nsresult BodyDeleteOrphanedFiles( + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, + const nsTArray& aKnownBodyIdList) { // body files are stored in a directory structure like: // // /morgue/01/{01fdddb2-884d-4c3d-95ba-0c8062f6c325}.final @@ -360,21 +361,21 @@ nsresult BodyDeleteOrphanedFiles(const ClientMetadata& aClientMetadata, // Iterate over all the intermediate morgue subdirs QM_TRY(quota::CollectEachFile( *dir, - [&aClientMetadata, &aKnownBodyIdList]( + [&aDirectoryMetadata, &aKnownBodyIdList]( const nsCOMPtr& subdir) -> Result { QM_TRY_INSPECT(const auto& dirEntryKind, GetDirEntryKind(*subdir)); switch (dirEntryKind) { case nsIFileKind::ExistsAsDirectory: { const auto removeOrphanedFiles = - [&aClientMetadata, &aKnownBodyIdList]( + [&aDirectoryMetadata, &aKnownBodyIdList]( nsIFile& bodyFile, const nsACString& leafName) -> Result { // Finally, parse the uuid out of the name. If it fails to parse, // then ignore the file. - auto cleanup = MakeScopeExit([&aClientMetadata, &bodyFile] { + auto cleanup = MakeScopeExit([&aDirectoryMetadata, &bodyFile] { DebugOnly result = - RemoveNsIFile(aClientMetadata, bodyFile); + RemoveNsIFile(aDirectoryMetadata, bodyFile); MOZ_ASSERT(NS_SUCCEEDED(result)); }); @@ -395,7 +396,7 @@ nsresult BodyDeleteOrphanedFiles(const ClientMetadata& aClientMetadata, // a warning in the reports is not desired). QM_TRY(QM_OR_ELSE_LOG_VERBOSE_IF( // Expression. - MOZ_TO_RESULT(BodyTraverseFiles(aClientMetadata, *subdir, + MOZ_TO_RESULT(BodyTraverseFiles(aDirectoryMetadata, *subdir, removeOrphanedFiles, /* aCanRemoveFiles */ true, /* aTrackQuota */ true)), @@ -409,8 +410,9 @@ nsresult BodyDeleteOrphanedFiles(const ClientMetadata& aClientMetadata, case nsIFileKind::ExistsAsFile: { // If a file got in here somehow, try to remove it and move on - DebugOnly result = RemoveNsIFile(aClientMetadata, *subdir, - /* aTrackQuota */ false); + DebugOnly result = + RemoveNsIFile(aDirectoryMetadata, *subdir, + /* aTrackQuota */ false); MOZ_ASSERT(NS_SUCCEEDED(result)); break; } @@ -429,9 +431,9 @@ nsresult BodyDeleteOrphanedFiles(const ClientMetadata& aClientMetadata, namespace { Result, nsresult> GetMarkerFileHandle( - const ClientMetadata& aClientMetadata) { + const CacheDirectoryMetadata& aDirectoryMetadata) { QM_TRY_UNWRAP(auto marker, - CloneFileAndAppend(*aClientMetadata.mDir, u"cache"_ns)); + CloneFileAndAppend(*aDirectoryMetadata.mDir, u"cache"_ns)); QM_TRY(MOZ_TO_RESULT(marker->Append(u"context_open.marker"_ns))); @@ -440,8 +442,8 @@ Result, nsresult> GetMarkerFileHandle( } // namespace -nsresult CreateMarkerFile(const ClientMetadata& aClientMetadata) { - QM_TRY_INSPECT(const auto& marker, GetMarkerFileHandle(aClientMetadata)); +nsresult CreateMarkerFile(const CacheDirectoryMetadata& aDirectoryMetadata) { + QM_TRY_INSPECT(const auto& marker, GetMarkerFileHandle(aDirectoryMetadata)); // Callers call this function without checking if the file already exists // (idempotent usage). QM_OR_ELSE_WARN_IF is not used here since we just want @@ -474,11 +476,11 @@ nsresult CreateMarkerFile(const ClientMetadata& aClientMetadata) { return NS_OK; } -nsresult DeleteMarkerFile(const ClientMetadata& aClientMetadata) { - QM_TRY_INSPECT(const auto& marker, GetMarkerFileHandle(aClientMetadata)); +nsresult DeleteMarkerFile(const CacheDirectoryMetadata& aDirectoryMetadata) { + QM_TRY_INSPECT(const auto& marker, GetMarkerFileHandle(aDirectoryMetadata)); DebugOnly result = - RemoveNsIFile(aClientMetadata, *marker, /* aTrackQuota */ false); + RemoveNsIFile(aDirectoryMetadata, *marker, /* aTrackQuota */ false); MOZ_ASSERT(NS_SUCCEEDED(result)); // Again, no fsync is necessary. If the OS crashes before the file @@ -489,18 +491,19 @@ nsresult DeleteMarkerFile(const ClientMetadata& aClientMetadata) { return NS_OK; } -bool MarkerFileExists(const ClientMetadata& aClientMetadata) { - QM_TRY_INSPECT(const auto& marker, GetMarkerFileHandle(aClientMetadata), +bool MarkerFileExists(const CacheDirectoryMetadata& aDirectoryMetadata) { + QM_TRY_INSPECT(const auto& marker, GetMarkerFileHandle(aDirectoryMetadata), false); QM_TRY_RETURN(MOZ_TO_RESULT_INVOKE(marker, Exists), false); } -nsresult RemoveNsIFileRecursively(const Maybe& aClientMetadata, - nsIFile& aFile, const bool aTrackQuota) { +nsresult RemoveNsIFileRecursively( + const Maybe& aDirectoryMetadata, nsIFile& aFile, + const bool aTrackQuota) { // XXX This assertion proves that we can remove aTrackQuota and just check // aClientMetadata - MOZ_DIAGNOSTIC_ASSERT_IF(aTrackQuota, aClientMetadata); + MOZ_DIAGNOSTIC_ASSERT_IF(aTrackQuota, aDirectoryMetadata); QM_TRY_INSPECT(const auto& dirEntryKind, GetDirEntryKind(aFile)); @@ -511,10 +514,10 @@ nsresult RemoveNsIFileRecursively(const Maybe& aClientMetadata, // one to update their usages to the QuotaManager. QM_TRY(quota::CollectEachFile( aFile, - [&aClientMetadata, &aTrackQuota]( + [&aDirectoryMetadata, &aTrackQuota]( const nsCOMPtr& file) -> Result { - QM_TRY(MOZ_TO_RESULT( - RemoveNsIFileRecursively(aClientMetadata, *file, aTrackQuota))); + QM_TRY(MOZ_TO_RESULT(RemoveNsIFileRecursively(aDirectoryMetadata, + *file, aTrackQuota))); return Ok{}; })); @@ -525,7 +528,7 @@ nsresult RemoveNsIFileRecursively(const Maybe& aClientMetadata, break; case nsIFileKind::ExistsAsFile: - return RemoveNsIFile(aClientMetadata, aFile, aTrackQuota); + return RemoveNsIFile(aDirectoryMetadata, aFile, aTrackQuota); case nsIFileKind::DoesNotExist: // Ignore files that got removed externally while iterating. @@ -535,11 +538,11 @@ nsresult RemoveNsIFileRecursively(const Maybe& aClientMetadata, return NS_OK; } -nsresult RemoveNsIFile(const Maybe& aClientMetadata, +nsresult RemoveNsIFile(const Maybe& aDirectoryMetadata, nsIFile& aFile, const bool aTrackQuota) { // XXX This assertion proves that we can remove aTrackQuota and just check // aClientMetadata - MOZ_DIAGNOSTIC_ASSERT_IF(aTrackQuota, aClientMetadata); + MOZ_DIAGNOSTIC_ASSERT_IF(aTrackQuota, aDirectoryMetadata); int64_t fileSize = 0; if (aTrackQuota) { @@ -570,21 +573,23 @@ nsresult RemoveNsIFile(const Maybe& aClientMetadata, if (fileSize > 0) { MOZ_ASSERT(aTrackQuota); - DecreaseUsageForClientMetadata(*aClientMetadata, fileSize); + DecreaseUsageForDirectoryMetadata(*aDirectoryMetadata, fileSize); } return NS_OK; } -void DecreaseUsageForClientMetadata(const ClientMetadata& aClientMetadata, - const int64_t aUpdatingSize) { +void DecreaseUsageForDirectoryMetadata( + const CacheDirectoryMetadata& aDirectoryMetadata, + const int64_t aUpdatingSize) { MOZ_DIAGNOSTIC_ASSERT(aUpdatingSize > 0); QuotaManager* quotaManager = QuotaManager::Get(); MOZ_DIAGNOSTIC_ASSERT(quotaManager); quotaManager->DecreaseUsageForClient( - quota::ClientMetadata{aClientMetadata, Client::DOMCACHE}, aUpdatingSize); + quota::ClientMetadata{aDirectoryMetadata, Client::DOMCACHE}, + aUpdatingSize); } bool DirectoryPaddingFileExists(nsIFile& aBaseDir, diff --git a/dom/cache/FileUtils.h b/dom/cache/FileUtils.h index 017aaa1783fa..3804dd9a6fa2 100644 --- a/dom/cache/FileUtils.h +++ b/dom/cache/FileUtils.h @@ -31,13 +31,13 @@ nsresult BodyCreateDir(nsIFile& aBaseDir); // Note that this function can only be used during the initialization of the // database. We're unlikely to be able to delete the DB successfully past // that point due to the file being in use. -nsresult BodyDeleteDir(const ClientMetadata& aClientMetadata, +nsresult BodyDeleteDir(const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir); // Returns a Result with a success value with the body id and, optionally, the // copy context. Result>, nsresult> BodyStartWriteStream( - const ClientMetadata& aClientMetadata, nsIFile& aBaseDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, nsIInputStream& aSource, void* aClosure, nsAsyncCopyCallbackFun aCallback); void BodyCancelWrite(nsISupports& aCopyContext); @@ -45,19 +45,19 @@ void BodyCancelWrite(nsISupports& aCopyContext); nsresult BodyFinalizeWrite(nsIFile& aBaseDir, const nsID& aId); Result>, nsresult> BodyOpen( - const ClientMetadata& aClientMetadata, nsIFile& aBaseDir, const nsID& aId); + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, + const nsID& aId); -nsresult BodyMaybeUpdatePaddingSize(const ClientMetadata& aClientMetadata, - nsIFile& aBaseDir, const nsID& aId, - uint32_t aPaddingInfo, - int64_t* aPaddingSizeInOut); +nsresult BodyMaybeUpdatePaddingSize( + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, + const nsID& aId, uint32_t aPaddingInfo, int64_t* aPaddingSizeInOut); -nsresult BodyDeleteFiles(const ClientMetadata& aClientMetadata, +nsresult BodyDeleteFiles(const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, const nsTArray& aIdList); -nsresult BodyDeleteOrphanedFiles(const ClientMetadata& aClientMetadata, - nsIFile& aBaseDir, - const nsTArray& aKnownBodyIdList); +nsresult BodyDeleteOrphanedFiles( + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBaseDir, + const nsTArray& aKnownBodyIdList); // If aCanRemoveFiles is true, that means we are safe to touch the files which // can be accessed in other threads. @@ -65,53 +65,54 @@ nsresult BodyDeleteOrphanedFiles(const ClientMetadata& aClientMetadata, // created by other threads. Note that if the files are not expected, we should // be safe to remove them in any case. template -nsresult BodyTraverseFiles(const Maybe& aClientMetadata, - nsIFile& aBodyDir, const Func& aHandleFileFunc, - bool aCanRemoveFiles, bool aTrackQuota = true); +nsresult BodyTraverseFiles( + const Maybe& aDirectoryMetadata, nsIFile& aBodyDir, + const Func& aHandleFileFunc, bool aCanRemoveFiles, bool aTrackQuota = true); // XXX Remove this method when all callers properly wrap aClientMetadata with // Some/Nothing template -nsresult BodyTraverseFiles(const ClientMetadata& aClientMetadata, +nsresult BodyTraverseFiles(const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aBodyDir, const Func& aHandleFileFunc, bool aCanRemoveFiles, bool aTrackQuota = true) { - return BodyTraverseFiles(Some(aClientMetadata), aBodyDir, aHandleFileFunc, + return BodyTraverseFiles(Some(aDirectoryMetadata), aBodyDir, aHandleFileFunc, aCanRemoveFiles, aTrackQuota); } -nsresult CreateMarkerFile(const ClientMetadata& aClientMetadata); +nsresult CreateMarkerFile(const CacheDirectoryMetadata& aDirectoryMetadata); -nsresult DeleteMarkerFile(const ClientMetadata& aClientMetadata); +nsresult DeleteMarkerFile(const CacheDirectoryMetadata& aDirectoryMetadata); -bool MarkerFileExists(const ClientMetadata& aClientMetadata); +bool MarkerFileExists(const CacheDirectoryMetadata& aDirectoryMetadata); -nsresult RemoveNsIFileRecursively(const Maybe& aClientMetadata, - nsIFile& aFile, bool aTrackQuota = true); +nsresult RemoveNsIFileRecursively( + const Maybe& aDirectoryMetadata, nsIFile& aFile, + bool aTrackQuota = true); // XXX Remove this method when all callers properly wrap aClientMetadata with // Some/Nothing -inline nsresult RemoveNsIFileRecursively(const ClientMetadata& aClientMetadata, - nsIFile& aFile, - bool aTrackQuota = true) { - return RemoveNsIFileRecursively(Some(aClientMetadata), aFile, aTrackQuota); +inline nsresult RemoveNsIFileRecursively( + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aFile, + bool aTrackQuota = true) { + return RemoveNsIFileRecursively(Some(aDirectoryMetadata), aFile, aTrackQuota); } // Delete a file that you think exists. If the file doesn't exist, an error // will not be returned, but warning telemetry will be generated! So only call // this on files that you know exist (idempotent usage, but it's not // recommended). -nsresult RemoveNsIFile(const Maybe& aClientMetadata, +nsresult RemoveNsIFile(const Maybe& aDirectoryMetadata, nsIFile& aFile, bool aTrackQuota = true); // XXX Remove this method when all callers properly wrap aClientMetadata with // Some/Nothing -inline nsresult RemoveNsIFile(const ClientMetadata& aClientMetadata, +inline nsresult RemoveNsIFile(const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile& aFile, bool aTrackQuota = true) { - return RemoveNsIFile(Some(aClientMetadata), aFile, aTrackQuota); + return RemoveNsIFile(Some(aDirectoryMetadata), aFile, aTrackQuota); } -void DecreaseUsageForClientMetadata(const ClientMetadata& aClientMetadata, - int64_t aUpdatingSize); +void DecreaseUsageForDirectoryMetadata( + const CacheDirectoryMetadata& aDirectoryMetadata, int64_t aUpdatingSize); /** * This function is used to check if the directory padding file is existed. diff --git a/dom/cache/FileUtilsImpl.h b/dom/cache/FileUtilsImpl.h index e67f0b136e09..53fdc4e61b56 100644 --- a/dom/cache/FileUtilsImpl.h +++ b/dom/cache/FileUtilsImpl.h @@ -15,12 +15,13 @@ namespace dom { namespace cache { template -nsresult BodyTraverseFiles(const Maybe& aClientMetadata, - nsIFile& aBodyDir, const Func& aHandleFileFunc, - const bool aCanRemoveFiles, const bool aTrackQuota) { +nsresult BodyTraverseFiles( + const Maybe& aDirectoryMetadata, nsIFile& aBodyDir, + const Func& aHandleFileFunc, const bool aCanRemoveFiles, + const bool aTrackQuota) { // XXX This assertion proves that we can remove aTrackQuota and just check // aClientMetadata.isSome() - MOZ_DIAGNOSTIC_ASSERT_IF(aTrackQuota, aClientMetadata); + MOZ_DIAGNOSTIC_ASSERT_IF(aTrackQuota, aDirectoryMetadata); #ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED { @@ -40,7 +41,7 @@ nsresult BodyTraverseFiles(const Maybe& aClientMetadata, FlippedOnce isEmpty; QM_TRY(quota::CollectEachFile( aBodyDir, - [&isEmpty, &aClientMetadata, aTrackQuota, &aHandleFileFunc, + [&isEmpty, &aDirectoryMetadata, aTrackQuota, &aHandleFileFunc, aCanRemoveFiles](const nsCOMPtr& file) -> Result { QM_TRY_INSPECT(const auto& dirEntryKind, quota::GetDirEntryKind(*file)); @@ -48,7 +49,7 @@ nsresult BodyTraverseFiles(const Maybe& aClientMetadata, case quota::nsIFileKind::ExistsAsDirectory: { // If it's a directory somehow, try to remove it and move on DebugOnly result = RemoveNsIFileRecursively( - aClientMetadata, *file, /* aTrackQuota */ false); + aDirectoryMetadata, *file, /* aTrackQuota */ false); MOZ_ASSERT(NS_SUCCEEDED(result)); break; } @@ -62,7 +63,7 @@ nsresult BodyTraverseFiles(const Maybe& aClientMetadata, if (StringEndsWith(leafName, ".tmp"_ns)) { if (aCanRemoveFiles) { DebugOnly result = - RemoveNsIFile(aClientMetadata, *file, aTrackQuota); + RemoveNsIFile(aDirectoryMetadata, *file, aTrackQuota); MOZ_ASSERT(NS_SUCCEEDED(result)); return Ok{}; } @@ -75,7 +76,7 @@ nsresult BodyTraverseFiles(const Maybe& aClientMetadata, // If its not, try to remove it and move on. if (!maybeEndingOk) { DebugOnly result = RemoveNsIFile( - aClientMetadata, *file, /* aTrackQuota */ false); + aDirectoryMetadata, *file, /* aTrackQuota */ false); MOZ_ASSERT(NS_SUCCEEDED(result)); return Ok{}; } @@ -101,7 +102,7 @@ nsresult BodyTraverseFiles(const Maybe& aClientMetadata, if (isEmpty && aCanRemoveFiles) { DebugOnly result = RemoveNsIFileRecursively( - aClientMetadata, aBodyDir, /* aTrackQuota */ false); + aDirectoryMetadata, aBodyDir, /* aTrackQuota */ false); MOZ_ASSERT(NS_SUCCEEDED(result)); } diff --git a/dom/cache/Manager.cpp b/dom/cache/Manager.cpp index efcdba7840ae..04518596ff01 100644 --- a/dom/cache/Manager.cpp +++ b/dom/cache/Manager.cpp @@ -75,7 +75,7 @@ class SetupAction final : public SyncDBAction { SetupAction() : SyncDBAction(DBAction::Create) {} virtual nsresult RunSyncWithDBOnTarget( - const ClientMetadata& aClientMetadata, nsIFile* aDBDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, mozIStorageConnection* aConn) override { MOZ_DIAGNOSTIC_ASSERT(aDBDir); @@ -94,7 +94,7 @@ class SetupAction final : public SyncDBAction { // // Note, this must be done after any schema version updates to // ensure our DBSchema methods work correctly. - if (MarkerFileExists(aClientMetadata)) { + if (MarkerFileExists(aDirectoryMetadata)) { NS_WARNING("Cache not shutdown cleanly! Cleaning up stale data..."); mozStorageTransaction trans(aConn, false, mozIStorageConnection::TRANSACTION_IMMEDIATE); @@ -109,19 +109,19 @@ class SetupAction final : public SyncDBAction { const CheckedInt64& overallDeletedPaddingSize, Reduce( orphanedCacheIdList, CheckedInt64(0), - [aConn, &aClientMetadata, &aDBDir]( + [aConn, &aDirectoryMetadata, &aDBDir]( CheckedInt64 oldValue, const Maybe& element) -> Result { QM_TRY_INSPECT(const auto& deletionInfo, db::DeleteCacheId(*aConn, *element)); QM_TRY(MOZ_TO_RESULT( - BodyDeleteFiles(aClientMetadata, *aDBDir, + BodyDeleteFiles(aDirectoryMetadata, *aDBDir, deletionInfo.mDeletedBodyIdList))); if (deletionInfo.mDeletedPaddingSize > 0) { - DecreaseUsageForClientMetadata( - aClientMetadata, deletionInfo.mDeletedPaddingSize); + DecreaseUsageForDirectoryMetadata( + aDirectoryMetadata, deletionInfo.mDeletedPaddingSize); } return oldValue + deletionInfo.mDeletedPaddingSize; @@ -130,8 +130,8 @@ class SetupAction final : public SyncDBAction { // Clean up orphaned body objects QM_TRY_INSPECT(const auto& knownBodyIdList, db::GetKnownBodyIds(*aConn)); - QM_TRY(MOZ_TO_RESULT( - BodyDeleteOrphanedFiles(aClientMetadata, *aDBDir, knownBodyIdList))); + QM_TRY(MOZ_TO_RESULT(BodyDeleteOrphanedFiles(aDirectoryMetadata, *aDBDir, + knownBodyIdList))); // Commit() explicitly here, because we want to ensure the padding file // has the correct content. @@ -172,11 +172,11 @@ class DeleteOrphanedBodyAction final : public Action { : mDeletedBodyIdList{aBodyId} {} void RunOnTarget(SafeRefPtr aResolver, - const Maybe& aClientMetadata, + const Maybe& aDirectoryMetadata, Data*) override { MOZ_DIAGNOSTIC_ASSERT(aResolver); - MOZ_DIAGNOSTIC_ASSERT(aClientMetadata); - MOZ_DIAGNOSTIC_ASSERT(aClientMetadata->mDir); + MOZ_DIAGNOSTIC_ASSERT(aDirectoryMetadata); + MOZ_DIAGNOSTIC_ASSERT(aDirectoryMetadata->mDir); // Note that since DeleteOrphanedBodyAction isn't used while the context is // being initialized, we don't need to check for cancellation here. @@ -186,11 +186,11 @@ class DeleteOrphanedBodyAction final : public Action { }; QM_TRY_INSPECT(const auto& dbDir, - CloneFileAndAppend(*aClientMetadata->mDir, u"cache"_ns), + CloneFileAndAppend(*aDirectoryMetadata->mDir, u"cache"_ns), QM_VOID, resolve); - QM_TRY(MOZ_TO_RESULT( - BodyDeleteFiles(*aClientMetadata, *dbDir, mDeletedBodyIdList)), + QM_TRY(MOZ_TO_RESULT(BodyDeleteFiles(*aDirectoryMetadata, *dbDir, + mDeletedBodyIdList)), QM_VOID, resolve); aResolver->Resolve(NS_OK); @@ -527,9 +527,9 @@ class Manager::DeleteOrphanedCacheAction final : public SyncDBAction { mCacheId(aCacheId) {} virtual nsresult RunSyncWithDBOnTarget( - const ClientMetadata& aClientMetadata, nsIFile* aDBDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, mozIStorageConnection* aConn) override { - mClientMetadata.emplace(aClientMetadata); + mDirectoryMetadata.emplace(aDirectoryMetadata); mozStorageTransaction trans(aConn, false, mozIStorageConnection::TRANSACTION_IMMEDIATE); @@ -556,8 +556,8 @@ class Manager::DeleteOrphanedCacheAction final : public SyncDBAction { mManager->NoteOrphanedBodyIdList(mDeletionInfo.mDeletedBodyIdList); if (mDeletionInfo.mDeletedPaddingSize > 0) { - DecreaseUsageForClientMetadata(*mClientMetadata, - mDeletionInfo.mDeletedPaddingSize); + DecreaseUsageForDirectoryMetadata(*mDirectoryMetadata, + mDeletionInfo.mDeletedPaddingSize); } // ensure we release the manager on the initiating thread @@ -568,7 +568,7 @@ class Manager::DeleteOrphanedCacheAction final : public SyncDBAction { SafeRefPtr mManager; const CacheId mCacheId; DeletionInfo mDeletionInfo; - Maybe mClientMetadata; + Maybe mDirectoryMetadata; }; // ---------------------------------------------------------------------------- @@ -585,7 +585,7 @@ class Manager::CacheMatchAction final : public Manager::BaseAction { mFoundResponse(false) {} virtual nsresult RunSyncWithDBOnTarget( - const ClientMetadata& aClientMetadata, nsIFile* aDBDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, mozIStorageConnection* aConn) override { MOZ_DIAGNOSTIC_ASSERT(aDBDir); @@ -607,7 +607,7 @@ class Manager::CacheMatchAction final : public Manager::BaseAction { nsCOMPtr stream; if (mArgs.openMode() == OpenMode::Eager) { QM_TRY_UNWRAP(stream, - BodyOpen(aClientMetadata, *aDBDir, mResponse.mBodyId)); + BodyOpen(aDirectoryMetadata, *aDBDir, mResponse.mBodyId)); } mStreamList->Add(mResponse.mBodyId, std::move(stream)); @@ -651,7 +651,7 @@ class Manager::CacheMatchAllAction final : public Manager::BaseAction { mStreamList(std::move(aStreamList)) {} virtual nsresult RunSyncWithDBOnTarget( - const ClientMetadata& aClientMetadata, nsIFile* aDBDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, mozIStorageConnection* aConn) override { MOZ_DIAGNOSTIC_ASSERT(aDBDir); @@ -668,7 +668,7 @@ class Manager::CacheMatchAllAction final : public Manager::BaseAction { nsCOMPtr stream; if (mArgs.openMode() == OpenMode::Eager) { - QM_TRY_UNWRAP(stream, BodyOpen(aClientMetadata, *aDBDir, + QM_TRY_UNWRAP(stream, BodyOpen(aDirectoryMetadata, *aDBDir, mSavedResponses[i].mBodyId)); } @@ -734,10 +734,10 @@ class Manager::CachePutAllAction final : public DBAction { private: ~CachePutAllAction() = default; - virtual void RunWithDBOnTarget(SafeRefPtr aResolver, - const ClientMetadata& aClientMetadata, - nsIFile* aDBDir, - mozIStorageConnection* aConn) override { + virtual void RunWithDBOnTarget( + SafeRefPtr aResolver, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, + mozIStorageConnection* aConn) override { MOZ_DIAGNOSTIC_ASSERT(aResolver); MOZ_DIAGNOSTIC_ASSERT(aDBDir); MOZ_DIAGNOSTIC_ASSERT(aConn); @@ -757,20 +757,20 @@ class Manager::CachePutAllAction final : public DBAction { mResolver = std::move(aResolver); mDBDir = aDBDir; mConn = aConn; - mClientMetadata.emplace(aClientMetadata); + mDirectoryMetadata.emplace(aDirectoryMetadata); // File bodies are streamed to disk via asynchronous copying. Start // this copying now. Each copy will eventually result in a call // to OnAsyncCopyComplete(). - const nsresult rv = [this, &aClientMetadata]() -> nsresult { + const nsresult rv = [this, &aDirectoryMetadata]() -> nsresult { QM_TRY(CollectEachInRange( - mList, [this, &aClientMetadata](auto& entry) -> nsresult { + mList, [this, &aDirectoryMetadata](auto& entry) -> nsresult { QM_TRY(MOZ_TO_RESULT( - StartStreamCopy(aClientMetadata, entry, RequestStream, + StartStreamCopy(aDirectoryMetadata, entry, RequestStream, &mExpectedAsyncCopyCompletions))); QM_TRY(MOZ_TO_RESULT( - StartStreamCopy(aClientMetadata, entry, ResponseStream, + StartStreamCopy(aDirectoryMetadata, entry, ResponseStream, &mExpectedAsyncCopyCompletions))); return NS_OK; @@ -858,7 +858,7 @@ class Manager::CachePutAllAction final : public DBAction { if (e.mResponse.type() == ResponseType::Opaque) { // It'll generate padding if we've not set it yet. QM_TRY(MOZ_TO_RESULT(BodyMaybeUpdatePaddingSize( - *mClientMetadata, *mDBDir, e.mResponseBodyId, + *mDirectoryMetadata, *mDBDir, e.mResponseBodyId, e.mResponse.paddingInfo(), &e.mResponse.paddingSize()))); MOZ_DIAGNOSTIC_ASSERT(INT64_MAX - e.mResponse.paddingSize() >= @@ -915,7 +915,8 @@ class Manager::CachePutAllAction final : public DBAction { mManager->NoteOrphanedBodyIdList(mDeletedBodyIdList); if (mDeletedPaddingSize > 0) { - DecreaseUsageForClientMetadata(*mClientMetadata, mDeletedPaddingSize); + DecreaseUsageForDirectoryMetadata(*mDirectoryMetadata, + mDeletedPaddingSize); } Listener* listener = mManager->GetListener(mListenerId); @@ -950,8 +951,9 @@ class Manager::CachePutAllAction final : public DBAction { enum StreamId { RequestStream, ResponseStream }; - nsresult StartStreamCopy(const ClientMetadata& aClientMetadata, Entry& aEntry, - StreamId aStreamId, uint32_t* aCopyCountOut) { + nsresult StartStreamCopy(const CacheDirectoryMetadata& aDirectoryMetadata, + Entry& aEntry, StreamId aStreamId, + uint32_t* aCopyCountOut) { MOZ_ASSERT(mTarget->IsOnCurrentThread()); MOZ_DIAGNOSTIC_ASSERT(aCopyCountOut); @@ -970,8 +972,8 @@ class Manager::CachePutAllAction final : public DBAction { } QM_TRY_INSPECT((const auto& [bodyId, copyContext]), - BodyStartWriteStream(aClientMetadata, *mDBDir, *source, this, - AsyncCopyCompleteFunc)); + BodyStartWriteStream(aDirectoryMetadata, *mDBDir, *source, + this, AsyncCopyCompleteFunc)); if (aStreamId == RequestStream) { aEntry.mRequestBodyId = bodyId; @@ -1034,9 +1036,10 @@ class Manager::CachePutAllAction final : public DBAction { // Clean up any files we might have written before hitting the error. if (NS_FAILED(aRv)) { - BodyDeleteFiles(*mClientMetadata, *mDBDir, mBodyIdWrittenList); + BodyDeleteFiles(*mDirectoryMetadata, *mDBDir, mBodyIdWrittenList); if (mUpdatedPaddingSize > 0) { - DecreaseUsageForClientMetadata(*mClientMetadata, mUpdatedPaddingSize); + DecreaseUsageForDirectoryMetadata(*mDirectoryMetadata, + mUpdatedPaddingSize); } } @@ -1079,7 +1082,7 @@ class Manager::CachePutAllAction final : public DBAction { Mutex mMutex; nsTArray> mCopyContextList; - Maybe mClientMetadata; + Maybe mDirectoryMetadata; // Track how much pad amount has been added for new entries so that it can be // removed if an error occurs. int64_t mUpdatedPaddingSize; @@ -1099,9 +1102,9 @@ class Manager::CacheDeleteAction final : public Manager::BaseAction { mSuccess(false) {} virtual nsresult RunSyncWithDBOnTarget( - const ClientMetadata& aClientMetadata, nsIFile* aDBDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, mozIStorageConnection* aConn) override { - mClientMetadata.emplace(aClientMetadata); + mDirectoryMetadata.emplace(aDirectoryMetadata); mozStorageTransaction trans(aConn, false, mozIStorageConnection::TRANSACTION_IMMEDIATE); @@ -1137,8 +1140,8 @@ class Manager::CacheDeleteAction final : public Manager::BaseAction { mManager->NoteOrphanedBodyIdList(mDeletionInfo.mDeletedBodyIdList); if (mDeletionInfo.mDeletedPaddingSize > 0) { - DecreaseUsageForClientMetadata(*mClientMetadata, - mDeletionInfo.mDeletedPaddingSize); + DecreaseUsageForDirectoryMetadata(*mDirectoryMetadata, + mDeletionInfo.mDeletedPaddingSize); } aListener->OnOpComplete(std::move(aRv), CacheDeleteResult(mSuccess)); @@ -1153,7 +1156,7 @@ class Manager::CacheDeleteAction final : public Manager::BaseAction { const CacheDeleteArgs mArgs; bool mSuccess; DeletionInfo mDeletionInfo; - Maybe mClientMetadata; + Maybe mDirectoryMetadata; }; // ---------------------------------------------------------------------------- @@ -1169,7 +1172,7 @@ class Manager::CacheKeysAction final : public Manager::BaseAction { mStreamList(std::move(aStreamList)) {} virtual nsresult RunSyncWithDBOnTarget( - const ClientMetadata& aClientMetadata, nsIFile* aDBDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, mozIStorageConnection* aConn) override { MOZ_DIAGNOSTIC_ASSERT(aDBDir); @@ -1186,7 +1189,7 @@ class Manager::CacheKeysAction final : public Manager::BaseAction { nsCOMPtr stream; if (mArgs.openMode() == OpenMode::Eager) { - QM_TRY_UNWRAP(stream, BodyOpen(aClientMetadata, *aDBDir, + QM_TRY_UNWRAP(stream, BodyOpen(aDirectoryMetadata, *aDBDir, mSavedRequests[i].mBodyId)); } @@ -1228,7 +1231,7 @@ class Manager::StorageMatchAction final : public Manager::BaseAction { mFoundResponse(false) {} virtual nsresult RunSyncWithDBOnTarget( - const ClientMetadata& aClientMetadata, nsIFile* aDBDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, mozIStorageConnection* aConn) override { MOZ_DIAGNOSTIC_ASSERT(aDBDir); @@ -1251,8 +1254,8 @@ class Manager::StorageMatchAction final : public Manager::BaseAction { nsCOMPtr stream; if (mArgs.openMode() == OpenMode::Eager) { - QM_TRY_UNWRAP(stream, - BodyOpen(aClientMetadata, *aDBDir, mSavedResponse.mBodyId)); + QM_TRY_UNWRAP(stream, BodyOpen(aDirectoryMetadata, *aDBDir, + mSavedResponse.mBodyId)); } mStreamList->Add(mSavedResponse.mBodyId, std::move(stream)); @@ -1291,7 +1294,7 @@ class Manager::StorageHasAction final : public Manager::BaseAction { mCacheFound(false) {} virtual nsresult RunSyncWithDBOnTarget( - const ClientMetadata& aClientMetadata, nsIFile* aDBDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, mozIStorageConnection* aConn) override { QM_TRY_INSPECT(const auto& maybeCacheId, db::StorageGetCacheId(*aConn, mNamespace, mArgs.key())); @@ -1323,7 +1326,7 @@ class Manager::StorageOpenAction final : public Manager::BaseAction { mCacheId(INVALID_CACHE_ID) {} virtual nsresult RunSyncWithDBOnTarget( - const ClientMetadata& aClientMetadata, nsIFile* aDBDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, mozIStorageConnection* aConn) override { // Cache does not exist, create it instead mozStorageTransaction trans(aConn, false, @@ -1378,7 +1381,7 @@ class Manager::StorageDeleteAction final : public Manager::BaseAction { mCacheId(INVALID_CACHE_ID) {} virtual nsresult RunSyncWithDBOnTarget( - const ClientMetadata& aClientMetadata, nsIFile* aDBDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, mozIStorageConnection* aConn) override { mozStorageTransaction trans(aConn, false, mozIStorageConnection::TRANSACTION_IMMEDIATE); @@ -1443,7 +1446,7 @@ class Manager::StorageKeysAction final : public Manager::BaseAction { : BaseAction(std::move(aManager), aListenerId), mNamespace(aNamespace) {} virtual nsresult RunSyncWithDBOnTarget( - const ClientMetadata& aClientMetadata, nsIFile* aDBDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, mozIStorageConnection* aConn) override { QM_TRY_UNWRAP(mKeys, db::StorageGetKeys(*aConn, mNamespace)); @@ -1473,11 +1476,11 @@ class Manager::OpenStreamAction final : public Manager::BaseAction { mBodyId(aBodyId) {} virtual nsresult RunSyncWithDBOnTarget( - const ClientMetadata& aClientMetadata, nsIFile* aDBDir, + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aDBDir, mozIStorageConnection* aConn) override { MOZ_DIAGNOSTIC_ASSERT(aDBDir); - QM_TRY_UNWRAP(mBodyStream, BodyOpen(aClientMetadata, *aDBDir, mBodyId)); + QM_TRY_UNWRAP(mBodyStream, BodyOpen(aDirectoryMetadata, *aDBDir, mBodyId)); return NS_OK; } diff --git a/dom/cache/QuotaClient.cpp b/dom/cache/QuotaClient.cpp index 67da7a03bc39..8074de6f683b 100644 --- a/dom/cache/QuotaClient.cpp +++ b/dom/cache/QuotaClient.cpp @@ -122,15 +122,15 @@ Result GetBodyUsage(nsIFile& aMorgueDir, Result GetPaddingSizeFromDB( nsIFile& aDir, nsIFile& aDBFile, const OriginMetadata& aOriginMetadata) { - ClientMetadata clientMetadata(aOriginMetadata); - // clientMetadata.mDirectoryLockId must be -1 (which is default for new - // ClientMetadata) because this method should only be called from + CacheDirectoryMetadata directoryMetadata(aOriginMetadata); + // directoryMetadata.mDirectoryLockId must be -1 (which is default for new + // CacheDirectoryMetadata) because this method should only be called from // QuotaClient::InitOrigin when the temporary storage hasn't been initialized // yet. At that time, the in-memory objects (e.g. OriginInfo) are only being // created so it doesn't make sense to tunnel quota information to // TelemetryVFS to get corresponding QuotaObject instance for the SQLite // file). - MOZ_DIAGNOSTIC_ASSERT(clientMetadata.mDirectoryLockId == -1); + MOZ_DIAGNOSTIC_ASSERT(directoryMetadata.mDirectoryLockId == -1); #ifdef DEBUG { @@ -139,7 +139,8 @@ Result GetPaddingSizeFromDB( } #endif - QM_TRY_INSPECT(const auto& conn, OpenDBConnection(clientMetadata, aDBFile)); + QM_TRY_INSPECT(const auto& conn, + OpenDBConnection(directoryMetadata, aDBFile)); // Make sure that the database has the latest schema before we try to read // from it. We have to do this because GetPaddingSizeFromDB is called @@ -434,7 +435,7 @@ nsresult CacheQuotaClient::RestorePaddingFileInternal( } nsresult CacheQuotaClient::WipePaddingFileInternal( - const ClientMetadata& aClientMetadata, nsIFile* aBaseDir) { + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aBaseDir) { MOZ_ASSERT(!NS_IsMainThread()); MOZ_DIAGNOSTIC_ASSERT(aBaseDir); @@ -467,7 +468,7 @@ nsresult CacheQuotaClient::WipePaddingFileInternal( }())); if (paddingSize > 0) { - DecreaseUsageForClientMetadata(aClientMetadata, paddingSize); + DecreaseUsageForDirectoryMetadata(aDirectoryMetadata, paddingSize); } QM_TRY(MOZ_TO_RESULT( @@ -516,7 +517,7 @@ nsresult RestorePaddingFile(nsIFile* aBaseDir, mozIStorageConnection* aConn) { } // static -nsresult WipePaddingFile(const ClientMetadata& aClientMetadata, +nsresult WipePaddingFile(const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aBaseDir) { MOZ_ASSERT(!NS_IsMainThread()); MOZ_DIAGNOSTIC_ASSERT(aBaseDir); @@ -525,7 +526,7 @@ nsresult WipePaddingFile(const ClientMetadata& aClientMetadata, MOZ_DIAGNOSTIC_ASSERT(cacheQuotaClient); QM_TRY(MOZ_TO_RESULT( - cacheQuotaClient->WipePaddingFileInternal(aClientMetadata, aBaseDir))); + cacheQuotaClient->WipePaddingFileInternal(aDirectoryMetadata, aBaseDir))); return NS_OK; } diff --git a/dom/cache/QuotaClient.h b/dom/cache/QuotaClient.h index fc394925d30e..fc01ca0907c1 100644 --- a/dom/cache/QuotaClient.h +++ b/dom/cache/QuotaClient.h @@ -39,7 +39,7 @@ already_AddRefed CreateQuotaClient(); nsresult RestorePaddingFile(nsIFile* aBaseDir, mozIStorageConnection* aConn); -nsresult WipePaddingFile(const ClientMetadata& aClientMetadata, +nsresult WipePaddingFile(const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aBaseDir); extern const nsLiteralString kCachesSQLiteFilename; diff --git a/dom/cache/QuotaClientImpl.h b/dom/cache/QuotaClientImpl.h index 7d151bb1e6c1..c150836f25d1 100644 --- a/dom/cache/QuotaClientImpl.h +++ b/dom/cache/QuotaClientImpl.h @@ -116,8 +116,8 @@ class CacheQuotaClient final : public quota::Client { nsresult RestorePaddingFileInternal(nsIFile* aBaseDir, mozIStorageConnection* aConn); - nsresult WipePaddingFileInternal(const ClientMetadata& aClientMetadata, - nsIFile* aBaseDir); + nsresult WipePaddingFileInternal( + const CacheDirectoryMetadata& aDirectoryMetadata, nsIFile* aBaseDir); private: ~CacheQuotaClient(); diff --git a/dom/cache/Types.h b/dom/cache/Types.h index 7696ae7669d7..2c354e465fd5 100644 --- a/dom/cache/Types.h +++ b/dom/cache/Types.h @@ -29,17 +29,17 @@ static const Namespace INVALID_NAMESPACE = NUMBER_OF_NAMESPACES; using CacheId = int64_t; static const CacheId INVALID_CACHE_ID = -1; -struct ClientMetadata : quota::ClientMetadata { +struct CacheDirectoryMetadata : quota::ClientMetadata { nsCOMPtr mDir; int64_t mDirectoryLockId = -1; - explicit ClientMetadata(quota::PrincipalMetadata aPrincipalMetadata) + explicit CacheDirectoryMetadata(quota::PrincipalMetadata aPrincipalMetadata) : quota::ClientMetadata( quota::OriginMetadata{std::move(aPrincipalMetadata), quota::PERSISTENCE_TYPE_DEFAULT}, quota::Client::Type::DOMCACHE) {} - explicit ClientMetadata(quota::OriginMetadata aOriginMetadata) + explicit CacheDirectoryMetadata(quota::OriginMetadata aOriginMetadata) : quota::ClientMetadata(std::move(aOriginMetadata), quota::Client::Type::DOMCACHE) { MOZ_DIAGNOSTIC_ASSERT(aOriginMetadata.mPersistenceType ==