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_
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"
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());
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());
58 *aDBRefCnt
= mDBRefCnt
;
62 template <typename FileManager
>
63 FileManager
& FileInfo
<FileManager
>::Manager() const {
67 template <typename FileManager
>
68 void FileInfo
<FileManager
>::UpdateReferences(ThreadSafeAutoRefCnt
& aRefCount
,
70 const bool aSyncDeleteFile
) {
73 AutoLockType
lock(FileManager::Mutex());
75 aRefCount
= aRefCount
+ aDelta
;
77 if (mRefCnt
+ mDBRefCnt
> 0) {
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
87 needsCleanup
= !mFileManager
->Invalidated();
91 if (aSyncDeleteFile
) {
92 QM_WARNONLY_TRY(QM_TO_RESULT(mFileManager
->SyncDeleteFile(Id())));
101 template <typename FileManager
>
102 void FileInfo
<FileManager
>::LockedAddRef() {
103 FileManager::Mutex().AssertCurrentThreadOwns();
108 template <typename FileManager
>
109 bool FileInfo
<FileManager
>::LockedClearDBRefs(
110 const typename
FileManager::FileInfoManagerGuard
&) {
111 FileManager::Mutex().AssertCurrentThreadOwns();
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());
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
)) {
141 nsCOMPtr
<nsIFile
> file
= FileManager::GetFileForId(directory
, Id());
142 if (NS_WARN_IF(!file
)) {
149 } // namespace mozilla::dom::indexedDB
151 #endif // DOM_INDEXEDDB_FILEINFOIMPL_H_