diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /dom/indexedDB/FileInfoTImpl.h | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/indexedDB/FileInfoTImpl.h')
-rw-r--r-- | dom/indexedDB/FileInfoTImpl.h | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/dom/indexedDB/FileInfoTImpl.h b/dom/indexedDB/FileInfoTImpl.h new file mode 100644 index 0000000000..bcc1a2d7ac --- /dev/null +++ b/dom/indexedDB/FileInfoTImpl.h @@ -0,0 +1,165 @@ +/* -*- 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/. */ + +#ifndef mozilla_dom_indexeddb_fileinfotimpl_h__ +#define mozilla_dom_indexeddb_fileinfotimpl_h__ + +#include "FileInfoT.h" + +#include "mozilla/Mutex.h" +#include "nsIFile.h" + +namespace mozilla { +namespace dom { +namespace indexedDB { + +template <typename FileManager> +FileInfoT<FileManager>::FileInfoT( + const typename FileManager::FileManagerGuard& aGuard, + SafeRefPtr<FileManager> aFileManager, const int64_t aFileId, + const nsrefcnt aInitialDBRefCnt) + : mFileId(aFileId), + mDBRefCnt(aInitialDBRefCnt), + mFileManager(std::move(aFileManager)) { + MOZ_ASSERT(mFileId > 0); + MOZ_ASSERT(mFileManager); +} + +template <typename FileManager> +void FileInfoT<FileManager>::AddRef() { + AutoLock lock(FileManager::Mutex()); + + LockedAddRef(); +} + +template <typename FileManager> +void FileInfoT<FileManager>::Release(const bool aSyncDeleteFile) { + UpdateReferences(mRefCnt, -1, aSyncDeleteFile); +} + +template <typename FileManager> +void FileInfoT<FileManager>::UpdateDBRefs(int32_t aDelta) { + UpdateReferences(mDBRefCnt, aDelta); +} + +template <typename FileManager> +void FileInfoT<FileManager>::GetReferences(int32_t* const aRefCnt, + int32_t* const aDBRefCnt) { + AutoLock lock(FileManager::Mutex()); + + if (aRefCnt) { + *aRefCnt = mRefCnt; + } + + if (aDBRefCnt) { + *aDBRefCnt = mDBRefCnt; + } +} + +template <typename FileManager> +FileManager& FileInfoT<FileManager>::Manager() const { + return *mFileManager; +} + +template <typename FileManager> +int64_t FileInfoT<FileManager>::Id() const { + return mFileId; +} + +template <typename FileManager> +void FileInfoT<FileManager>::UpdateReferences(ThreadSafeAutoRefCnt& aRefCount, + const int32_t aDelta, + const bool aSyncDeleteFile) { + bool needsCleanup; + { + AutoLock lock(FileManager::Mutex()); + + aRefCount = aRefCount + aDelta; + + if (mRefCnt + mDBRefCnt > 0) { + return; + } + + mFileManager->RemoveFileInfo(Id(), lock); + + // If the file manager was already invalidated, we don't need to do any + // cleanup anymore. In that case, the entire origin directory has already + // been deleted by the quota manager, and we don't need to delete individual + // files. + needsCleanup = !mFileManager->Invalidated(); + } + + if (needsCleanup) { + if (aSyncDeleteFile) { + nsresult rv = mFileManager->SyncDeleteFile(Id()); + if (NS_FAILED(rv)) { + NS_WARNING("FileManager cleanup failed!"); + } + } else { + Cleanup(); + } + } + + delete this; +} + +template <typename FileManager> +void FileInfoT<FileManager>::LockedAddRef() { + FileManager::Mutex().AssertCurrentThreadOwns(); + + ++mRefCnt; +} + +template <typename FileManager> +bool FileInfoT<FileManager>::LockedClearDBRefs( + const typename FileManager::FileManagerGuard&) { + FileManager::Mutex().AssertCurrentThreadOwns(); + + mDBRefCnt = 0; + + if (mRefCnt) { + return true; + } + + // In this case, we are not responsible for removing the file info from the + // hashtable. It's up to FileManager which is the only caller of this method. + + MOZ_ASSERT(mFileManager->Invalidated()); + + delete this; + + return false; +} + +template <typename FileManager> +void FileInfoT<FileManager>::Cleanup() { + int64_t id = Id(); + + if (NS_FAILED(mFileManager->AsyncDeleteFile(id))) { + NS_WARNING("Failed to delete file asynchronously!"); + } +} + +template <typename FileManager> +nsCOMPtr<nsIFile> FileInfoT<FileManager>::GetFileForFileInfo() const { + const nsCOMPtr<nsIFile> directory = Manager().GetDirectory(); + if (NS_WARN_IF(!directory)) { + return nullptr; + } + + nsCOMPtr<nsIFile> file = FileManager::GetFileForId(directory, Id()); + if (NS_WARN_IF(!file)) { + return nullptr; + } + + return file; +} + +} // namespace indexedDB +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_indexeddb_fileinfotimpl_h__ |