Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / dom / quota / Client.h
blob160a339c0b0ab7f8b12749794f1a41f64386778d
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_quota_client_h__
8 #define mozilla_dom_quota_client_h__
10 #include "ErrorList.h"
11 #include "mozilla/Atomics.h"
12 #include "mozilla/Result.h"
13 #include "mozilla/dom/ipc/IdType.h"
14 #include "mozilla/dom/quota/PersistenceType.h"
15 #include "nsHashKeys.h"
16 #include "nsISupports.h"
17 #include "nsStringFwd.h"
18 #include "nsTHashSet.h"
20 // XXX Remove this dependency.
21 #include "mozilla/dom/LocalStorageCommon.h"
23 class nsIFile;
25 #define IDB_DIRECTORY_NAME "idb"
26 #define DOMCACHE_DIRECTORY_NAME "cache"
27 #define SDB_DIRECTORY_NAME "sdb"
28 #define FILESYSTEM_DIRECTORY_NAME "fs"
29 #define LS_DIRECTORY_NAME "ls"
31 // Deprecated
32 #define ASMJSCACHE_DIRECTORY_NAME "asmjs"
34 namespace mozilla::dom {
35 template <typename T>
36 struct Nullable;
39 namespace mozilla::dom::quota {
41 struct OriginMetadata;
42 class OriginScope;
43 class PersistenceScope;
44 class QuotaManager;
45 class UsageInfo;
47 // An abstract interface for quota manager clients.
48 // Each storage API must provide an implementation of this interface in order
49 // to participate in centralized quota and storage handling.
50 class Client {
51 public:
52 using AtomicBool = Atomic<bool>;
54 enum Type {
55 IDB = 0,
56 // APPCACHE,
57 DOMCACHE,
58 SDB,
59 FILESYSTEM,
60 LS,
61 TYPE_MAX
64 class DirectoryLockIdTable final {
65 nsTHashSet<uint64_t> mIds;
67 public:
68 void Put(const int64_t aId) { mIds.Insert(aId); }
70 bool Has(const int64_t aId) const { return mIds.Contains(aId); }
72 bool Filled() const { return mIds.Count(); }
75 static Type TypeMax() {
76 if (NextGenLocalStorageEnabled()) {
77 return TYPE_MAX;
79 return LS;
82 static bool IsValidType(Type aType);
84 static bool TypeToText(Type aType, nsAString& aText, const fallible_t&);
86 // TODO: Rename other similar methods to use String/CString instead of Text.
87 static nsAutoString TypeToString(Type aType);
89 static nsAutoCString TypeToText(Type aType);
91 static bool TypeFromText(const nsAString& aText, Type& aType,
92 const fallible_t&);
94 static Type TypeFromText(const nsACString& aText);
96 static char TypeToPrefix(Type aType);
98 static bool TypeFromPrefix(char aPrefix, Type& aType, const fallible_t&);
100 static bool IsDeprecatedClient(const nsAString& aText) {
101 return aText.EqualsLiteral(ASMJSCACHE_DIRECTORY_NAME);
104 template <typename T>
105 static bool IsLockForObjectContainedInLockTable(
106 const T& aObject, const DirectoryLockIdTable& aIds);
108 template <typename T>
109 static bool IsLockForObjectAcquiredAndContainedInLockTable(
110 const T& aObject, const DirectoryLockIdTable& aIds);
112 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
114 virtual Type GetType() = 0;
116 // Methods which are called on the IO thread.
117 virtual nsresult UpgradeStorageFrom1_0To2_0(nsIFile* aDirectory) {
118 return NS_OK;
121 virtual nsresult UpgradeStorageFrom2_0To2_1(nsIFile* aDirectory) {
122 return NS_OK;
125 virtual nsresult UpgradeStorageFrom2_1To2_2(nsIFile* aDirectory) {
126 return NS_OK;
129 virtual Result<UsageInfo, nsresult> InitOrigin(
130 PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
131 const AtomicBool& aCanceled) = 0;
133 virtual nsresult InitOriginWithoutTracking(
134 PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
135 const AtomicBool& aCanceled) = 0;
137 virtual Result<UsageInfo, nsresult> GetUsageForOrigin(
138 PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
139 const AtomicBool& aCanceled) = 0;
141 // This method is called when origins are about to be cleared
142 // (except the case when clearing is triggered by the origin eviction).
143 virtual nsresult AboutToClearOrigins(
144 const PersistenceScope& aPersistenceScope,
145 const OriginScope& aOriginScope) {
146 return NS_OK;
149 virtual void OnOriginClearCompleted(
150 const OriginMetadata& aOriginMetadata) = 0;
152 virtual void OnRepositoryClearCompleted(PersistenceType aPersistenceType) = 0;
154 virtual void ReleaseIOThreadObjects() = 0;
156 // Methods which are called on the background thread.
157 virtual void AbortOperationsForLocks(
158 const DirectoryLockIdTable& aDirectoryLockIds) = 0;
160 virtual void AbortOperationsForProcess(ContentParentId aContentParentId) = 0;
162 virtual void AbortAllOperations() = 0;
164 virtual void StartIdleMaintenance() = 0;
166 virtual void StopIdleMaintenance() = 0;
168 // Both variants just check for QuotaManager::IsShuttingDown()
169 // but assert to be on the right thread.
170 // They must not be used for re-entrance checks.
171 // Deprecated: This distinction is not needed anymore.
172 // QuotaClients should call QuotaManager::IsShuttingDown instead.
173 static bool IsShuttingDownOnBackgroundThread();
174 static bool IsShuttingDownOnNonBackgroundThread();
176 // Returns true if there is work that needs to be waited for.
177 bool InitiateShutdownWorkThreads();
178 void FinalizeShutdownWorkThreads();
180 virtual nsCString GetShutdownStatus() const = 0;
181 virtual bool IsShutdownCompleted() const = 0;
182 virtual void ForceKillActors() = 0;
184 private:
185 virtual void InitiateShutdown() = 0;
186 virtual void FinalizeShutdown() = 0;
188 protected:
189 virtual ~Client() = default;
192 } // namespace mozilla::dom::quota
194 #endif // mozilla_dom_quota_client_h__