summaryrefslogtreecommitdiffstats
path: root/dom/quota/Client.h
blob: 2ff227e2423ca29fa9ea7302a5c13c475101083c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_quota_client_h__
#define mozilla_dom_quota_client_h__

#include "ErrorList.h"
#include "mozilla/Atomics.h"
#include "mozilla/Result.h"
#include "mozilla/dom/ipc/IdType.h"
#include "mozilla/dom/quota/PersistenceType.h"
#include "nsHashKeys.h"
#include "nsISupports.h"
#include "nsStringFwd.h"
#include "nsTHashSet.h"

// XXX Remove this dependency.
#include "mozilla/dom/LocalStorageCommon.h"

class nsIFile;

#define IDB_DIRECTORY_NAME "idb"
#define DOMCACHE_DIRECTORY_NAME "cache"
#define SDB_DIRECTORY_NAME "sdb"
#define FILESYSTEM_DIRECTORY_NAME "fs"
#define LS_DIRECTORY_NAME "ls"

// Deprecated
#define ASMJSCACHE_DIRECTORY_NAME "asmjs"

namespace mozilla::dom {
template <typename T>
struct Nullable;
}

namespace mozilla::dom::quota {

struct OriginMetadata;
class OriginScope;
class QuotaManager;
class UsageInfo;

// An abstract interface for quota manager clients.
// Each storage API must provide an implementation of this interface in order
// to participate in centralized quota and storage handling.
class Client {
 public:
  using AtomicBool = Atomic<bool>;

  enum Type {
    IDB = 0,
    // APPCACHE,
    DOMCACHE,
    SDB,
    FILESYSTEM,
    LS,
    TYPE_MAX
  };

  class DirectoryLockIdTable final {
    nsTHashSet<uint64_t> mIds;

   public:
    void Put(const int64_t aId) { mIds.Insert(aId); }

    bool Has(const int64_t aId) const { return mIds.Contains(aId); }

    bool Filled() const { return mIds.Count(); }
  };

  static Type TypeMax() {
    if (NextGenLocalStorageEnabled()) {
      return TYPE_MAX;
    }
    return LS;
  }

  static bool IsValidType(Type aType);

  static bool TypeToText(Type aType, nsAString& aText, const fallible_t&);

  // TODO: Rename other similar methods to use String/CString instead of Text.
  static nsAutoString TypeToString(Type aType);

  static nsAutoCString TypeToText(Type aType);

  static bool TypeFromText(const nsAString& aText, Type& aType,
                           const fallible_t&);

  static Type TypeFromText(const nsACString& aText);

  static char TypeToPrefix(Type aType);

  static bool TypeFromPrefix(char aPrefix, Type& aType, const fallible_t&);

  static bool IsDeprecatedClient(const nsAString& aText) {
    return aText.EqualsLiteral(ASMJSCACHE_DIRECTORY_NAME);
  }

  template <typename T>
  static bool IsLockForObjectContainedInLockTable(
      const T& aObject, const DirectoryLockIdTable& aIds);

  template <typename T>
  static bool IsLockForObjectAcquiredAndContainedInLockTable(
      const T& aObject, const DirectoryLockIdTable& aIds);

  NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING

  virtual Type GetType() = 0;

  // Methods which are called on the IO thread.
  virtual nsresult UpgradeStorageFrom1_0To2_0(nsIFile* aDirectory) {
    return NS_OK;
  }

  virtual nsresult UpgradeStorageFrom2_0To2_1(nsIFile* aDirectory) {
    return NS_OK;
  }

  virtual nsresult UpgradeStorageFrom2_1To2_2(nsIFile* aDirectory) {
    return NS_OK;
  }

  virtual Result<UsageInfo, nsresult> InitOrigin(
      PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
      const AtomicBool& aCanceled) = 0;

  virtual nsresult InitOriginWithoutTracking(
      PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
      const AtomicBool& aCanceled) = 0;

  virtual Result<UsageInfo, nsresult> GetUsageForOrigin(
      PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
      const AtomicBool& aCanceled) = 0;

  // This method is called when origins are about to be cleared
  // (except the case when clearing is triggered by the origin eviction).
  virtual nsresult AboutToClearOrigins(
      const Nullable<PersistenceType>& aPersistenceType,
      const OriginScope& aOriginScope) {
    return NS_OK;
  }

  virtual void OnOriginClearCompleted(PersistenceType aPersistenceType,
                                      const nsACString& aOrigin) = 0;

  virtual void OnRepositoryClearCompleted(PersistenceType aPersistenceType) = 0;

  virtual void ReleaseIOThreadObjects() = 0;

  // Methods which are called on the background thread.
  virtual void AbortOperationsForLocks(
      const DirectoryLockIdTable& aDirectoryLockIds) = 0;

  virtual void AbortOperationsForProcess(ContentParentId aContentParentId) = 0;

  virtual void AbortAllOperations() = 0;

  virtual void StartIdleMaintenance() = 0;

  virtual void StopIdleMaintenance() = 0;

  // Both variants just check for QuotaManager::IsShuttingDown()
  // but assert to be on the right thread.
  // They must not be used for re-entrance checks.
  // Deprecated: This distinction is not needed anymore.
  // QuotaClients should call QuotaManager::IsShuttingDown instead.
  static bool IsShuttingDownOnBackgroundThread();
  static bool IsShuttingDownOnNonBackgroundThread();

  // Returns true if there is work that needs to be waited for.
  bool InitiateShutdownWorkThreads();
  void FinalizeShutdownWorkThreads();

  virtual nsCString GetShutdownStatus() const = 0;
  virtual bool IsShutdownCompleted() const = 0;
  virtual void ForceKillActors() = 0;

 private:
  virtual void InitiateShutdown() = 0;
  virtual void FinalizeShutdown() = 0;

 protected:
  virtual ~Client() = default;
};

}  // namespace mozilla::dom::quota

#endif  // mozilla_dom_quota_client_h__