Bug 1833854 - Part 4: Move all code that deals with maintaining invariants into a...
[gecko.git] / dom / quota / Client.h
blob2ff227e2423ca29fa9ea7302a5c13c475101083c
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 QuotaManager;
44 class UsageInfo;
46 // An abstract interface for quota manager clients.
47 // Each storage API must provide an implementation of this interface in order
48 // to participate in centralized quota and storage handling.
49 class Client {
50 public:
51 using AtomicBool = Atomic<bool>;
53 enum Type {
54 IDB = 0,
55 // APPCACHE,
56 DOMCACHE,
57 SDB,
58 FILESYSTEM,
59 LS,
60 TYPE_MAX
63 class DirectoryLockIdTable final {
64 nsTHashSet<uint64_t> mIds;
66 public:
67 void Put(const int64_t aId) { mIds.Insert(aId); }
69 bool Has(const int64_t aId) const { return mIds.Contains(aId); }
71 bool Filled() const { return mIds.Count(); }
74 static Type TypeMax() {
75 if (NextGenLocalStorageEnabled()) {
76 return TYPE_MAX;
78 return LS;
81 static bool IsValidType(Type aType);
83 static bool TypeToText(Type aType, nsAString& aText, const fallible_t&);
85 // TODO: Rename other similar methods to use String/CString instead of Text.
86 static nsAutoString TypeToString(Type aType);
88 static nsAutoCString TypeToText(Type aType);
90 static bool TypeFromText(const nsAString& aText, Type& aType,
91 const fallible_t&);
93 static Type TypeFromText(const nsACString& aText);
95 static char TypeToPrefix(Type aType);
97 static bool TypeFromPrefix(char aPrefix, Type& aType, const fallible_t&);
99 static bool IsDeprecatedClient(const nsAString& aText) {
100 return aText.EqualsLiteral(ASMJSCACHE_DIRECTORY_NAME);
103 template <typename T>
104 static bool IsLockForObjectContainedInLockTable(
105 const T& aObject, const DirectoryLockIdTable& aIds);
107 template <typename T>
108 static bool IsLockForObjectAcquiredAndContainedInLockTable(
109 const T& aObject, const DirectoryLockIdTable& aIds);
111 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
113 virtual Type GetType() = 0;
115 // Methods which are called on the IO thread.
116 virtual nsresult UpgradeStorageFrom1_0To2_0(nsIFile* aDirectory) {
117 return NS_OK;
120 virtual nsresult UpgradeStorageFrom2_0To2_1(nsIFile* aDirectory) {
121 return NS_OK;
124 virtual nsresult UpgradeStorageFrom2_1To2_2(nsIFile* aDirectory) {
125 return NS_OK;
128 virtual Result<UsageInfo, nsresult> InitOrigin(
129 PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
130 const AtomicBool& aCanceled) = 0;
132 virtual nsresult InitOriginWithoutTracking(
133 PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
134 const AtomicBool& aCanceled) = 0;
136 virtual Result<UsageInfo, nsresult> GetUsageForOrigin(
137 PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
138 const AtomicBool& aCanceled) = 0;
140 // This method is called when origins are about to be cleared
141 // (except the case when clearing is triggered by the origin eviction).
142 virtual nsresult AboutToClearOrigins(
143 const Nullable<PersistenceType>& aPersistenceType,
144 const OriginScope& aOriginScope) {
145 return NS_OK;
148 virtual void OnOriginClearCompleted(PersistenceType aPersistenceType,
149 const nsACString& aOrigin) = 0;
151 virtual void OnRepositoryClearCompleted(PersistenceType aPersistenceType) = 0;
153 virtual void ReleaseIOThreadObjects() = 0;
155 // Methods which are called on the background thread.
156 virtual void AbortOperationsForLocks(
157 const DirectoryLockIdTable& aDirectoryLockIds) = 0;
159 virtual void AbortOperationsForProcess(ContentParentId aContentParentId) = 0;
161 virtual void AbortAllOperations() = 0;
163 virtual void StartIdleMaintenance() = 0;
165 virtual void StopIdleMaintenance() = 0;
167 // Both variants just check for QuotaManager::IsShuttingDown()
168 // but assert to be on the right thread.
169 // They must not be used for re-entrance checks.
170 // Deprecated: This distinction is not needed anymore.
171 // QuotaClients should call QuotaManager::IsShuttingDown instead.
172 static bool IsShuttingDownOnBackgroundThread();
173 static bool IsShuttingDownOnNonBackgroundThread();
175 // Returns true if there is work that needs to be waited for.
176 bool InitiateShutdownWorkThreads();
177 void FinalizeShutdownWorkThreads();
179 virtual nsCString GetShutdownStatus() const = 0;
180 virtual bool IsShutdownCompleted() const = 0;
181 virtual void ForceKillActors() = 0;
183 private:
184 virtual void InitiateShutdown() = 0;
185 virtual void FinalizeShutdown() = 0;
187 protected:
188 virtual ~Client() = default;
191 } // namespace mozilla::dom::quota
193 #endif // mozilla_dom_quota_client_h__