tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Client.h (5575B)


      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/. */
      6 
      7 #ifndef mozilla_dom_quota_client_h__
      8 #define mozilla_dom_quota_client_h__
      9 
     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"
     19 
     20 // XXX Remove this dependency.
     21 #include "mozilla/dom/LocalStorageCommon.h"
     22 
     23 class nsIFile;
     24 
     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"
     30 
     31 // Deprecated
     32 #define ASMJSCACHE_DIRECTORY_NAME "asmjs"
     33 
     34 namespace mozilla::dom {
     35 template <typename T>
     36 struct Nullable;
     37 }
     38 
     39 namespace mozilla::dom::quota {
     40 
     41 struct OriginMetadata;
     42 class OriginScope;
     43 class PersistenceScope;
     44 class QuotaManager;
     45 class UsageInfo;
     46 
     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>;
     53 
     54  enum Type {
     55    IDB = 0,
     56    // APPCACHE,
     57    DOMCACHE,
     58    SDB,
     59    FILESYSTEM,
     60    LS,
     61    TYPE_MAX
     62  };
     63 
     64  class DirectoryLockIdTable final {
     65    nsTHashSet<uint64_t> mIds;
     66 
     67   public:
     68    void Put(const int64_t aId) { mIds.Insert(aId); }
     69 
     70    bool Has(const int64_t aId) const { return mIds.Contains(aId); }
     71 
     72    bool Filled() const { return mIds.Count(); }
     73  };
     74 
     75  static Type TypeMax() {
     76    if (NextGenLocalStorageEnabled()) {
     77      return TYPE_MAX;
     78    }
     79    return LS;
     80  }
     81 
     82  static bool IsValidType(Type aType);
     83 
     84  static bool TypeToText(Type aType, nsAString& aText, const fallible_t&);
     85 
     86  // TODO: Rename other similar methods to use String/CString instead of Text.
     87  static nsAutoString TypeToString(Type aType);
     88 
     89  static nsAutoCString TypeToText(Type aType);
     90 
     91  static bool TypeFromText(const nsAString& aText, Type& aType,
     92                           const fallible_t&);
     93 
     94  static Type TypeFromText(const nsACString& aText);
     95 
     96  static char TypeToPrefix(Type aType);
     97 
     98  static bool TypeFromPrefix(char aPrefix, Type& aType, const fallible_t&);
     99 
    100  static bool IsDeprecatedClient(const nsAString& aText) {
    101    return aText.EqualsLiteral(ASMJSCACHE_DIRECTORY_NAME);
    102  }
    103 
    104  template <typename T>
    105  static bool IsLockForObjectContainedInLockTable(
    106      const T& aObject, const DirectoryLockIdTable& aIds);
    107 
    108  template <typename T>
    109  static bool IsLockForObjectAcquiredAndContainedInLockTable(
    110      const T& aObject, const DirectoryLockIdTable& aIds);
    111 
    112  NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
    113 
    114  virtual Type GetType() = 0;
    115 
    116  // Methods which are called on the IO thread.
    117  virtual nsresult UpgradeStorageFrom1_0To2_0(nsIFile* aDirectory) {
    118    return NS_OK;
    119  }
    120 
    121  virtual nsresult UpgradeStorageFrom2_0To2_1(nsIFile* aDirectory) {
    122    return NS_OK;
    123  }
    124 
    125  virtual nsresult UpgradeStorageFrom2_1To2_2(nsIFile* aDirectory) {
    126    return NS_OK;
    127  }
    128 
    129  virtual Result<UsageInfo, nsresult> InitOrigin(
    130      PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
    131      const AtomicBool& aCanceled) = 0;
    132 
    133  virtual nsresult InitOriginWithoutTracking(
    134      PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
    135      const AtomicBool& aCanceled) = 0;
    136 
    137  virtual Result<UsageInfo, nsresult> GetUsageForOrigin(
    138      PersistenceType aPersistenceType, const OriginMetadata& aOriginMetadata,
    139      const AtomicBool& aCanceled) = 0;
    140 
    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;
    147  }
    148 
    149  virtual void OnOriginClearCompleted(
    150      const OriginMetadata& aOriginMetadata) = 0;
    151 
    152  virtual void OnRepositoryClearCompleted(PersistenceType aPersistenceType) = 0;
    153 
    154  virtual void ReleaseIOThreadObjects() = 0;
    155 
    156  // Methods which are called on the background thread.
    157  virtual void AbortOperationsForLocks(
    158      const DirectoryLockIdTable& aDirectoryLockIds) = 0;
    159 
    160  virtual void AbortOperationsForProcess(ContentParentId aContentParentId) = 0;
    161 
    162  virtual void AbortAllOperations() = 0;
    163 
    164  virtual void StartIdleMaintenance() = 0;
    165 
    166  virtual void StopIdleMaintenance() = 0;
    167 
    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();
    175 
    176  // Returns true if there is work that needs to be waited for.
    177  bool InitiateShutdownWorkThreads();
    178  void FinalizeShutdownWorkThreads();
    179 
    180  virtual nsCString GetShutdownStatus() const = 0;
    181  virtual bool IsShutdownCompleted() const = 0;
    182  virtual void ForceKillActors() = 0;
    183 
    184 private:
    185  virtual void InitiateShutdown() = 0;
    186  virtual void FinalizeShutdown() = 0;
    187 
    188 protected:
    189  virtual ~Client() = default;
    190 };
    191 
    192 }  // namespace mozilla::dom::quota
    193 
    194 #endif  // mozilla_dom_quota_client_h__