Bug 1936278 - Prevent search mode chiclet from being dismissed when clicking in page...
[gecko.git] / dom / indexedDB / FileInfoImpl.h
blobb7ffa3be8ca62ee396956ba43b1b4e689a0f1bbd
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef DOM_INDEXEDDB_FILEINFOIMPL_H_
8 #define DOM_INDEXEDDB_FILEINFOIMPL_H_
10 #include "FileInfo.h"
12 #include "mozilla/dom/QMResult.h"
13 #include "mozilla/dom/quota/QuotaCommon.h"
14 #include "mozilla/dom/quota/ResultExtensions.h"
15 #include "mozilla/Mutex.h"
16 #include "nsIFile.h"
18 namespace mozilla::dom::indexedDB {
20 template <typename FileManager>
21 FileInfo<FileManager>::FileInfo(
22 const typename FileManager::FileInfoManagerGuard& aGuard,
23 SafeRefPtr<FileManager> aFileManager, const int64_t aFileId,
24 const nsrefcnt aInitialDBRefCnt)
25 : FileInfoBase{aFileId},
26 mDBRefCnt(aInitialDBRefCnt),
27 mFileManager(std::move(aFileManager)) {
28 MOZ_ASSERT(mFileManager);
31 template <typename FileManager>
32 void FileInfo<FileManager>::AddRef() {
33 AutoLockType lock(FileManager::Mutex());
35 LockedAddRef();
38 template <typename FileManager>
39 void FileInfo<FileManager>::Release(const bool aSyncDeleteFile) {
40 UpdateReferences(mRefCnt, -1, aSyncDeleteFile);
43 template <typename FileManager>
44 void FileInfo<FileManager>::UpdateDBRefs(int32_t aDelta) {
45 UpdateReferences(mDBRefCnt, aDelta);
48 template <typename FileManager>
49 void FileInfo<FileManager>::GetReferences(int32_t* const aRefCnt,
50 int32_t* const aDBRefCnt) {
51 AutoLockType lock(FileManager::Mutex());
53 if (aRefCnt) {
54 *aRefCnt = mRefCnt;
57 if (aDBRefCnt) {
58 *aDBRefCnt = mDBRefCnt;
62 template <typename FileManager>
63 FileManager& FileInfo<FileManager>::Manager() const {
64 return *mFileManager;
67 template <typename FileManager>
68 void FileInfo<FileManager>::UpdateReferences(ThreadSafeAutoRefCnt& aRefCount,
69 const int32_t aDelta,
70 const bool aSyncDeleteFile) {
71 bool needsCleanup;
73 AutoLockType lock(FileManager::Mutex());
75 aRefCount = aRefCount + aDelta;
77 if (mRefCnt + mDBRefCnt > 0) {
78 return;
81 mFileManager->RemoveFileInfo(Id(), lock);
83 // If the FileManager was already invalidated, we don't need to do any
84 // cleanup anymore. In that case, the entire origin directory has already
85 // been deleted by the quota manager, and we don't need to delete individual
86 // files.
87 needsCleanup = !mFileManager->Invalidated();
90 if (needsCleanup) {
91 if (aSyncDeleteFile) {
92 QM_WARNONLY_TRY(QM_TO_RESULT(mFileManager->SyncDeleteFile(Id())));
93 } else {
94 Cleanup();
98 delete this;
101 template <typename FileManager>
102 void FileInfo<FileManager>::LockedAddRef() {
103 FileManager::Mutex().AssertCurrentThreadOwns();
105 ++mRefCnt;
108 template <typename FileManager>
109 bool FileInfo<FileManager>::LockedClearDBRefs(
110 const typename FileManager::FileInfoManagerGuard&) {
111 FileManager::Mutex().AssertCurrentThreadOwns();
113 mDBRefCnt = 0;
115 if (mRefCnt) {
116 return true;
119 // In this case, we are not responsible for removing the FileInfo from the
120 // hashtable. It's up to FileManager which is the only caller of this method.
122 MOZ_ASSERT(mFileManager->Invalidated());
124 delete this;
126 return false;
129 template <typename FileManager>
130 void FileInfo<FileManager>::Cleanup() {
131 QM_WARNONLY_TRY(QM_TO_RESULT(mFileManager->AsyncDeleteFile(Id())));
134 template <typename FileManager>
135 nsCOMPtr<nsIFile> FileInfo<FileManager>::GetFileForFileInfo() const {
136 const nsCOMPtr<nsIFile> directory = Manager().GetDirectory();
137 if (NS_WARN_IF(!directory)) {
138 return nullptr;
141 nsCOMPtr<nsIFile> file = FileManager::GetFileForId(directory, Id());
142 if (NS_WARN_IF(!file)) {
143 return nullptr;
146 return file;
149 } // namespace mozilla::dom::indexedDB
151 #endif // DOM_INDEXEDDB_FILEINFOIMPL_H_