Backed out changeset a8d1b2a81511 (bug 1853184) for causing python vcs failures CLOSE...
[gecko.git] / dom / indexedDB / FileInfoManager.h
blobeff435138654f7ca5fd762400afc4ca6593853d6
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_FILEINFOMANAGER_H_
8 #define DOM_INDEXEDDB_FILEINFOMANAGER_H_
10 #include "mozilla/Attributes.h"
11 #include "mozilla/Mutex.h"
12 #include "mozilla/StaticMutex.h"
13 #include "nsTHashMap.h"
14 #include "nsHashKeys.h"
15 #include "nsISupportsImpl.h"
16 #include "FileInfo.h"
17 #include "FlippedOnce.h"
19 namespace mozilla::dom::indexedDB {
21 class FileInfoManagerBase {
22 public:
23 bool Invalidated() const { return mInvalidated; }
25 protected:
26 bool AssertValid() const {
27 if (NS_WARN_IF(Invalidated())) {
28 MOZ_ASSERT(false);
29 return false;
32 return true;
35 void Invalidate() { mInvalidated.Flip(); }
37 private:
38 FlippedOnce<false> mInvalidated;
41 template <typename FileManager>
42 class FileInfoManager : public FileInfoManagerBase {
43 public:
44 using FileInfoType = FileInfo<FileManager>;
45 using MutexType = StaticMutex;
46 using AutoLockType = mozilla::detail::BaseAutoLock<MutexType&>;
48 [[nodiscard]] SafeRefPtr<FileInfoType> GetFileInfo(int64_t aId) const {
49 return AcquireFileInfo([this, aId] { return mFileInfos.MaybeGet(aId); });
52 [[nodiscard]] SafeRefPtr<FileInfoType> CreateFileInfo() {
53 return AcquireFileInfo([this] {
54 const int64_t id = ++mLastFileId;
56 auto fileInfo =
57 MakeNotNull<FileInfoType*>(FileInfoManagerGuard{},
58 SafeRefPtr{static_cast<FileManager*>(this),
59 AcquireStrongRefFromRawPtr{}},
60 id);
62 mFileInfos.InsertOrUpdate(id, fileInfo);
63 return Some(fileInfo);
64 });
67 void RemoveFileInfo(const int64_t aId, const AutoLockType& aFileMutexLock) {
68 #ifdef DEBUG
69 aFileMutexLock.AssertOwns(FileManager::Mutex());
70 #endif
71 mFileInfos.Remove(aId);
74 nsresult Invalidate() {
75 AutoLockType lock(FileManager::Mutex());
77 FileInfoManagerBase::Invalidate();
79 mFileInfos.RemoveIf([](const auto& iter) {
80 FileInfoType* info = iter.Data();
81 MOZ_ASSERT(info);
83 return !info->LockedClearDBRefs(FileInfoManagerGuard{});
84 });
86 return NS_OK;
89 struct FileInfoManagerGuard {
90 FileInfoManagerGuard() = default;
93 private:
94 // Runs the given aFileInfoTableOp operation, which must return a FileInfo*,
95 // under the FileManager lock, acquires a strong reference to the returned
96 // object under the lock, and returns the strong reference.
97 template <typename FileInfoTableOp>
98 [[nodiscard]] SafeRefPtr<FileInfoType> AcquireFileInfo(
99 const FileInfoTableOp& aFileInfoTableOp) const {
100 if (!AssertValid()) {
101 // In release, the assertions are disabled.
102 return nullptr;
105 // We cannot simply change this to SafeRefPtr<FileInfo>, because
106 // FileInfo::AddRef also acquires the FileManager::Mutex.
107 auto fileInfo = [&aFileInfoTableOp]() -> RefPtr<FileInfoType> {
108 AutoLockType lock(FileManager::Mutex());
110 const auto maybeFileInfo = aFileInfoTableOp();
111 if (maybeFileInfo) {
112 const auto& fileInfo = maybeFileInfo.ref();
113 fileInfo->LockedAddRef();
114 return dont_AddRef(fileInfo.get());
117 return {};
118 }();
120 return SafeRefPtr{std::move(fileInfo)};
123 protected:
124 #ifdef DEBUG
125 ~FileInfoManager() { MOZ_ASSERT(mFileInfos.IsEmpty()); }
126 #else
127 ~FileInfoManager() = default;
128 #endif
130 // Access to the following fields must be protected by
131 // FileManager::Mutex()
132 int64_t mLastFileId = 0;
133 nsTHashMap<nsUint64HashKey, NotNull<FileInfoType*>> mFileInfos;
136 } // namespace mozilla::dom::indexedDB
138 #endif // DOM_INDEXEDDB_FILEINFOMANAGER_H_