tor-browser

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

mozStorageService.h (5520B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
      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/. */
      6 
      7 #ifndef MOZSTORAGESERVICE_H
      8 #define MOZSTORAGESERVICE_H
      9 
     10 #include "nsCOMPtr.h"
     11 #include "nsIFile.h"
     12 #include "nsIMemoryReporter.h"
     13 #include "nsIObserver.h"
     14 #include "nsTArray.h"
     15 #include "mozilla/Mutex.h"
     16 #include "mozilla/UniquePtr.h"
     17 #include "mozilla/intl/Collator.h"
     18 
     19 #include "mozIStorageService.h"
     20 
     21 class nsIMemoryReporter;
     22 struct sqlite3_vfs;
     23 namespace mozilla::intl {
     24 class Collator;
     25 }
     26 
     27 namespace mozilla::storage {
     28 
     29 class Connection;
     30 class Service : public mozIStorageService,
     31                public nsIObserver,
     32                public nsIMemoryReporter {
     33 public:
     34  /**
     35   * Initializes the service.  This must be called before any other function!
     36   */
     37  nsresult initialize();
     38 
     39  /**
     40   * Compares two strings using the Service's locale-aware collation.
     41   *
     42   * @param  aStr1
     43   *         The string to be compared against aStr2.
     44   * @param  aStr2
     45   *         The string to be compared against aStr1.
     46   * @param  aSensitivity
     47   *         The sorting sensitivity.
     48   * @return aStr1 - aStr2.  That is, if aStr1 < aStr2, returns a negative
     49   *         number.  If aStr1 > aStr2, returns a positive number.  If
     50   *         aStr1 == aStr2, returns 0.
     51   */
     52  int localeCompareStrings(const nsAString& aStr1, const nsAString& aStr2,
     53                           mozilla::intl::Collator::Sensitivity aSensitivity);
     54 
     55  static already_AddRefed<Service> getSingleton();
     56 
     57  NS_DECL_THREADSAFE_ISUPPORTS
     58  NS_DECL_MOZISTORAGESERVICE
     59  NS_DECL_NSIOBSERVER
     60  NS_DECL_NSIMEMORYREPORTER
     61 
     62  /**
     63   * Returns a boolean value indicating whether or not the given page size is
     64   * valid (currently understood as a power of 2 between 512 and 65536).
     65   */
     66  static bool pageSizeIsValid(int32_t aPageSize) {
     67    return aPageSize == 512 || aPageSize == 1024 || aPageSize == 2048 ||
     68           aPageSize == 4096 || aPageSize == 8192 || aPageSize == 16384 ||
     69           aPageSize == 32768 || aPageSize == 65536;
     70  }
     71 
     72  static const int32_t kDefaultPageSize = 32768;
     73 
     74  /**
     75   * Registers the connection with the storage service.  Connections are
     76   * registered so they can be iterated over.
     77   *
     78   * @pre mRegistrationMutex is not held
     79   *
     80   * @param  aConnection
     81   *         The connection to register.
     82   */
     83  void registerConnection(Connection* aConnection);
     84 
     85  /**
     86   * Unregisters the connection with the storage service.
     87   *
     88   * @pre mRegistrationMutex is not held
     89   *
     90   * @param  aConnection
     91   *         The connection to unregister.
     92   */
     93  void unregisterConnection(Connection* aConnection);
     94 
     95  /**
     96   * Gets the list of open connections.  Note that you must test each
     97   * connection with mozIStorageConnection::connectionReady before doing
     98   * anything with it, and skip it if it's not ready.
     99   *
    100   * @pre mRegistrationMutex is not held
    101   *
    102   * @param  aConnections
    103   *         An inout param;  it is cleared and the connections are appended to
    104   *         it.
    105   * @return The open connections.
    106   */
    107  void getConnections(nsTArray<RefPtr<Connection> >& aConnections);
    108 
    109 private:
    110  Service();
    111  virtual ~Service();
    112 
    113  /**
    114   * Used for 1) locking around calls when initializing connections so that we
    115   * can ensure that the state of sqlite3_enable_shared_cache is sane and 2)
    116   * synchronizing access to mLocaleCollation.
    117   */
    118  Mutex mMutex MOZ_UNANNOTATED;
    119 
    120  struct AutoVFSRegistration {
    121    int Init(UniquePtr<sqlite3_vfs> aVFS);
    122    ~AutoVFSRegistration();
    123 
    124   private:
    125    UniquePtr<sqlite3_vfs> mVFS;
    126  };
    127 
    128  // The order of these members should match the order of Init calls in
    129  // initialize(), to ensure that the unregistration takes place in the reverse
    130  // order.
    131  AutoVFSRegistration mBaseSqliteVFS;
    132  AutoVFSRegistration mBaseExclSqliteVFS;
    133  AutoVFSRegistration mQuotaSqliteVFS;
    134  AutoVFSRegistration mObfuscatingSqliteVFS;
    135  AutoVFSRegistration mReadOnlyNoLockSqliteVFS;
    136 
    137  /**
    138   * Protects mConnections.
    139   */
    140  Mutex mRegistrationMutex MOZ_UNANNOTATED;
    141 
    142  /**
    143   * The list of connections we have created.  Modifications to it are
    144   * protected by |mRegistrationMutex|.
    145   */
    146  nsTArray<RefPtr<Connection> > mConnections;
    147 
    148  /**
    149   * Frees as much heap memory as possible from all of the known open
    150   * connections.
    151   */
    152  void minimizeMemory();
    153 
    154  /**
    155   * Lazily creates and returns a collator created from the application's
    156   * locale that all statements of all Connections of this Service may use.
    157   * Since the collator's lifetime is that of the Service and no statement may
    158   * execute outside the lifetime of the Service, this method returns a raw
    159   * pointer.
    160   */
    161  mozilla::intl::Collator* getCollator();
    162 
    163  /**
    164   * Lazily created collator that all statements of all Connections of this
    165   * Service may use.  The collator is created from the application's locale.
    166   *
    167   * @note The collator is not thread-safe since the options can be changed
    168   * between calls. Access should be synchronized.
    169   */
    170  mozilla::UniquePtr<mozilla::intl::Collator> mCollator = nullptr;
    171 
    172  nsCOMPtr<nsIFile> mProfileStorageFile;
    173 
    174  nsCOMPtr<nsIMemoryReporter> mStorageSQLiteReporter;
    175 
    176  static Service* gService;
    177 
    178  mozilla::intl::Collator::Sensitivity mLastSensitivity;
    179 };
    180 
    181 }  // namespace mozilla::storage
    182 
    183 #endif /* MOZSTORAGESERVICE_H */