storage/src/mozStorageService.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/storage/src/mozStorageService.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,197 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef MOZSTORAGESERVICE_H
    1.11 +#define MOZSTORAGESERVICE_H
    1.12 +
    1.13 +#include "nsCOMPtr.h"
    1.14 +#include "nsICollation.h"
    1.15 +#include "nsIFile.h"
    1.16 +#include "nsIMemoryReporter.h"
    1.17 +#include "nsIObserver.h"
    1.18 +#include "nsTArray.h"
    1.19 +#include "mozilla/Mutex.h"
    1.20 +
    1.21 +#include "mozIStorageService.h"
    1.22 +
    1.23 +class nsIMemoryReporter;
    1.24 +class nsIXPConnect;
    1.25 +struct sqlite3_vfs;
    1.26 +
    1.27 +namespace mozilla {
    1.28 +namespace storage {
    1.29 +
    1.30 +class Connection;
    1.31 +class Service : public mozIStorageService
    1.32 +              , public nsIObserver
    1.33 +              , public nsIMemoryReporter
    1.34 +{
    1.35 +public:
    1.36 +  /**
    1.37 +   * Initializes the service.  This must be called before any other function!
    1.38 +   */
    1.39 +  nsresult initialize();
    1.40 +
    1.41 +  /**
    1.42 +   * Compares two strings using the Service's locale-aware collation.
    1.43 +   *
    1.44 +   * @param  aStr1
    1.45 +   *         The string to be compared against aStr2.
    1.46 +   * @param  aStr2
    1.47 +   *         The string to be compared against aStr1.
    1.48 +   * @param  aComparisonStrength
    1.49 +   *         The sorting strength, one of the nsICollation constants.
    1.50 +   * @return aStr1 - aStr2.  That is, if aStr1 < aStr2, returns a negative
    1.51 +   *         number.  If aStr1 > aStr2, returns a positive number.  If
    1.52 +   *         aStr1 == aStr2, returns 0.
    1.53 +   */
    1.54 +  int localeCompareStrings(const nsAString &aStr1,
    1.55 +                           const nsAString &aStr2,
    1.56 +                           int32_t aComparisonStrength);
    1.57 +
    1.58 +  static Service *getSingleton();
    1.59 +
    1.60 +  NS_DECL_THREADSAFE_ISUPPORTS
    1.61 +  NS_DECL_MOZISTORAGESERVICE
    1.62 +  NS_DECL_NSIOBSERVER
    1.63 +  NS_DECL_NSIMEMORYREPORTER
    1.64 +
    1.65 +  /**
    1.66 +   * Obtains an already AddRefed pointer to XPConnect.  This is used by
    1.67 +   * language helpers.
    1.68 +   */
    1.69 +  static already_AddRefed<nsIXPConnect> getXPConnect();
    1.70 +
    1.71 +  /**
    1.72 +   * Obtains the cached data for the toolkit.storage.synchronous preference.
    1.73 +   */
    1.74 +  static int32_t getSynchronousPref();
    1.75 +
    1.76 +  /**
    1.77 +   * Obtains the default page size for this platform. The default value is
    1.78 +   * specified in the SQLite makefile (SQLITE_DEFAULT_PAGE_SIZE) but it may be
    1.79 +   * overriden with the PREF_TS_PAGESIZE hidden preference.
    1.80 +   */
    1.81 +  static int32_t getDefaultPageSize()
    1.82 +  {
    1.83 +    return sDefaultPageSize;
    1.84 +  }
    1.85 +
    1.86 +  /**
    1.87 +   * Returns a boolean value indicating whether or not the given page size is
    1.88 +   * valid (currently understood as a power of 2 between 512 and 65536).
    1.89 +   */
    1.90 +  static bool pageSizeIsValid(int32_t aPageSize)
    1.91 +  {
    1.92 +    return aPageSize == 512 || aPageSize == 1024 || aPageSize == 2048 ||
    1.93 +           aPageSize == 4096 || aPageSize == 8192 || aPageSize == 16384 ||
    1.94 +           aPageSize == 32768 || aPageSize == 65536;
    1.95 +  }
    1.96 +
    1.97 +  /**
    1.98 +   * Registers the connection with the storage service.  Connections are
    1.99 +   * registered so they can be iterated over.
   1.100 +   *
   1.101 +   * @pre mRegistrationMutex is not held
   1.102 +   *
   1.103 +   * @param  aConnection
   1.104 +   *         The connection to register.
   1.105 +   */
   1.106 +  void registerConnection(Connection *aConnection);
   1.107 +
   1.108 +  /**
   1.109 +   * Unregisters the connection with the storage service.
   1.110 +   *
   1.111 +   * @pre mRegistrationMutex is not held
   1.112 +   *
   1.113 +   * @param  aConnection
   1.114 +   *         The connection to unregister.
   1.115 +   */
   1.116 +  void unregisterConnection(Connection *aConnection);
   1.117 +
   1.118 +  /**
   1.119 +   * Gets the list of open connections.  Note that you must test each
   1.120 +   * connection with mozIStorageConnection::connectionReady before doing
   1.121 +   * anything with it, and skip it if it's not ready.
   1.122 +   *
   1.123 +   * @pre mRegistrationMutex is not held
   1.124 +   *
   1.125 +   * @param  aConnections
   1.126 +   *         An inout param;  it is cleared and the connections are appended to
   1.127 +   *         it.
   1.128 +   * @return The open connections.
   1.129 +   */
   1.130 +  void getConnections(nsTArray<nsRefPtr<Connection> >& aConnections);
   1.131 +
   1.132 +private:
   1.133 +  Service();
   1.134 +  virtual ~Service();
   1.135 +
   1.136 +  /**
   1.137 +   * Used for 1) locking around calls when initializing connections so that we
   1.138 +   * can ensure that the state of sqlite3_enable_shared_cache is sane and 2)
   1.139 +   * synchronizing access to mLocaleCollation.
   1.140 +   */
   1.141 +  Mutex mMutex;
   1.142 +  
   1.143 +  sqlite3_vfs *mSqliteVFS;
   1.144 +
   1.145 +  /**
   1.146 +   * Protects mConnections.
   1.147 +   */
   1.148 +  Mutex mRegistrationMutex;
   1.149 +
   1.150 +  /**
   1.151 +   * The list of connections we have created.  Modifications to it are
   1.152 +   * protected by |mRegistrationMutex|.
   1.153 +   */
   1.154 +  nsTArray<nsRefPtr<Connection> > mConnections;
   1.155 +
   1.156 +  /**
   1.157 +   * Frees as much heap memory as possible from all of the known open
   1.158 +   * connections.
   1.159 +   */
   1.160 +  void minimizeMemory();
   1.161 +
   1.162 +  /**
   1.163 +   * Shuts down the storage service, freeing all of the acquired resources.
   1.164 +   */
   1.165 +  void shutdown();
   1.166 +
   1.167 +  /**
   1.168 +   * Lazily creates and returns a collation created from the application's
   1.169 +   * locale that all statements of all Connections of this Service may use.
   1.170 +   * Since the collation's lifetime is that of the Service and no statement may
   1.171 +   * execute outside the lifetime of the Service, this method returns a raw
   1.172 +   * pointer.
   1.173 +   */
   1.174 +  nsICollation *getLocaleCollation();
   1.175 +
   1.176 +  /**
   1.177 +   * Lazily created collation that all statements of all Connections of this
   1.178 +   * Service may use.  The collation is created from the application's locale.
   1.179 +   *
   1.180 +   * @note Collation implementations are platform-dependent and in general not
   1.181 +   *       thread-safe.  Access to this collation should be synchronized.
   1.182 +   */
   1.183 +  nsCOMPtr<nsICollation> mLocaleCollation;
   1.184 +
   1.185 +  nsCOMPtr<nsIFile> mProfileStorageFile;
   1.186 +
   1.187 +  nsCOMPtr<nsIMemoryReporter> mStorageSQLiteReporter;
   1.188 +
   1.189 +  static Service *gService;
   1.190 +
   1.191 +  static nsIXPConnect *sXPConnect;
   1.192 +
   1.193 +  static int32_t sSynchronousPref;
   1.194 +  static int32_t sDefaultPageSize;
   1.195 +};
   1.196 +
   1.197 +} // namespace storage
   1.198 +} // namespace mozilla
   1.199 +
   1.200 +#endif /* MOZSTORAGESERVICE_H */

mercurial