michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZSTORAGESERVICE_H michael@0: #define MOZSTORAGESERVICE_H michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsICollation.h" michael@0: #include "nsIFile.h" michael@0: #include "nsIMemoryReporter.h" michael@0: #include "nsIObserver.h" michael@0: #include "nsTArray.h" michael@0: #include "mozilla/Mutex.h" michael@0: michael@0: #include "mozIStorageService.h" michael@0: michael@0: class nsIMemoryReporter; michael@0: class nsIXPConnect; michael@0: struct sqlite3_vfs; michael@0: michael@0: namespace mozilla { michael@0: namespace storage { michael@0: michael@0: class Connection; michael@0: class Service : public mozIStorageService michael@0: , public nsIObserver michael@0: , public nsIMemoryReporter michael@0: { michael@0: public: michael@0: /** michael@0: * Initializes the service. This must be called before any other function! michael@0: */ michael@0: nsresult initialize(); michael@0: michael@0: /** michael@0: * Compares two strings using the Service's locale-aware collation. michael@0: * michael@0: * @param aStr1 michael@0: * The string to be compared against aStr2. michael@0: * @param aStr2 michael@0: * The string to be compared against aStr1. michael@0: * @param aComparisonStrength michael@0: * The sorting strength, one of the nsICollation constants. michael@0: * @return aStr1 - aStr2. That is, if aStr1 < aStr2, returns a negative michael@0: * number. If aStr1 > aStr2, returns a positive number. If michael@0: * aStr1 == aStr2, returns 0. michael@0: */ michael@0: int localeCompareStrings(const nsAString &aStr1, michael@0: const nsAString &aStr2, michael@0: int32_t aComparisonStrength); michael@0: michael@0: static Service *getSingleton(); michael@0: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_MOZISTORAGESERVICE michael@0: NS_DECL_NSIOBSERVER michael@0: NS_DECL_NSIMEMORYREPORTER michael@0: michael@0: /** michael@0: * Obtains an already AddRefed pointer to XPConnect. This is used by michael@0: * language helpers. michael@0: */ michael@0: static already_AddRefed getXPConnect(); michael@0: michael@0: /** michael@0: * Obtains the cached data for the toolkit.storage.synchronous preference. michael@0: */ michael@0: static int32_t getSynchronousPref(); michael@0: michael@0: /** michael@0: * Obtains the default page size for this platform. The default value is michael@0: * specified in the SQLite makefile (SQLITE_DEFAULT_PAGE_SIZE) but it may be michael@0: * overriden with the PREF_TS_PAGESIZE hidden preference. michael@0: */ michael@0: static int32_t getDefaultPageSize() michael@0: { michael@0: return sDefaultPageSize; michael@0: } michael@0: michael@0: /** michael@0: * Returns a boolean value indicating whether or not the given page size is michael@0: * valid (currently understood as a power of 2 between 512 and 65536). michael@0: */ michael@0: static bool pageSizeIsValid(int32_t aPageSize) michael@0: { michael@0: return aPageSize == 512 || aPageSize == 1024 || aPageSize == 2048 || michael@0: aPageSize == 4096 || aPageSize == 8192 || aPageSize == 16384 || michael@0: aPageSize == 32768 || aPageSize == 65536; michael@0: } michael@0: michael@0: /** michael@0: * Registers the connection with the storage service. Connections are michael@0: * registered so they can be iterated over. michael@0: * michael@0: * @pre mRegistrationMutex is not held michael@0: * michael@0: * @param aConnection michael@0: * The connection to register. michael@0: */ michael@0: void registerConnection(Connection *aConnection); michael@0: michael@0: /** michael@0: * Unregisters the connection with the storage service. michael@0: * michael@0: * @pre mRegistrationMutex is not held michael@0: * michael@0: * @param aConnection michael@0: * The connection to unregister. michael@0: */ michael@0: void unregisterConnection(Connection *aConnection); michael@0: michael@0: /** michael@0: * Gets the list of open connections. Note that you must test each michael@0: * connection with mozIStorageConnection::connectionReady before doing michael@0: * anything with it, and skip it if it's not ready. michael@0: * michael@0: * @pre mRegistrationMutex is not held michael@0: * michael@0: * @param aConnections michael@0: * An inout param; it is cleared and the connections are appended to michael@0: * it. michael@0: * @return The open connections. michael@0: */ michael@0: void getConnections(nsTArray >& aConnections); michael@0: michael@0: private: michael@0: Service(); michael@0: virtual ~Service(); michael@0: michael@0: /** michael@0: * Used for 1) locking around calls when initializing connections so that we michael@0: * can ensure that the state of sqlite3_enable_shared_cache is sane and 2) michael@0: * synchronizing access to mLocaleCollation. michael@0: */ michael@0: Mutex mMutex; michael@0: michael@0: sqlite3_vfs *mSqliteVFS; michael@0: michael@0: /** michael@0: * Protects mConnections. michael@0: */ michael@0: Mutex mRegistrationMutex; michael@0: michael@0: /** michael@0: * The list of connections we have created. Modifications to it are michael@0: * protected by |mRegistrationMutex|. michael@0: */ michael@0: nsTArray > mConnections; michael@0: michael@0: /** michael@0: * Frees as much heap memory as possible from all of the known open michael@0: * connections. michael@0: */ michael@0: void minimizeMemory(); michael@0: michael@0: /** michael@0: * Shuts down the storage service, freeing all of the acquired resources. michael@0: */ michael@0: void shutdown(); michael@0: michael@0: /** michael@0: * Lazily creates and returns a collation created from the application's michael@0: * locale that all statements of all Connections of this Service may use. michael@0: * Since the collation's lifetime is that of the Service and no statement may michael@0: * execute outside the lifetime of the Service, this method returns a raw michael@0: * pointer. michael@0: */ michael@0: nsICollation *getLocaleCollation(); michael@0: michael@0: /** michael@0: * Lazily created collation that all statements of all Connections of this michael@0: * Service may use. The collation is created from the application's locale. michael@0: * michael@0: * @note Collation implementations are platform-dependent and in general not michael@0: * thread-safe. Access to this collation should be synchronized. michael@0: */ michael@0: nsCOMPtr mLocaleCollation; michael@0: michael@0: nsCOMPtr mProfileStorageFile; michael@0: michael@0: nsCOMPtr mStorageSQLiteReporter; michael@0: michael@0: static Service *gService; michael@0: michael@0: static nsIXPConnect *sXPConnect; michael@0: michael@0: static int32_t sSynchronousPref; michael@0: static int32_t sDefaultPageSize; michael@0: }; michael@0: michael@0: } // namespace storage michael@0: } // namespace mozilla michael@0: michael@0: #endif /* MOZSTORAGESERVICE_H */