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/. */
7 #ifndef MOZSTORAGESERVICE_H
8 #define MOZSTORAGESERVICE_H
11 #include "nsICollation.h"
13 #include "nsIMemoryReporter.h"
14 #include "nsIObserver.h"
16 #include "mozilla/Mutex.h"
18 #include "mozIStorageService.h"
20 class nsIMemoryReporter
;
27 class Service
: public mozIStorageService
29 , public nsIMemoryReporter
33 * Initializes the service. This must be called before any other function!
35 nsresult
initialize();
38 * Compares two strings using the Service's locale-aware collation.
41 * The string to be compared against aStr2.
43 * The string to be compared against aStr1.
44 * @param aComparisonStrength
45 * The sorting strength, one of the nsICollation constants.
46 * @return aStr1 - aStr2. That is, if aStr1 < aStr2, returns a negative
47 * number. If aStr1 > aStr2, returns a positive number. If
48 * aStr1 == aStr2, returns 0.
50 int localeCompareStrings(const nsAString
&aStr1
,
51 const nsAString
&aStr2
,
52 int32_t aComparisonStrength
);
54 static already_AddRefed
<Service
> getSingleton();
56 NS_DECL_THREADSAFE_ISUPPORTS
57 NS_DECL_MOZISTORAGESERVICE
59 NS_DECL_NSIMEMORYREPORTER
62 * Obtains the cached data for the toolkit.storage.synchronous preference.
64 static int32_t getSynchronousPref();
67 * Obtains the default page size for this platform. The default value is
68 * specified in the SQLite makefile (SQLITE_DEFAULT_PAGE_SIZE) but it may be
69 * overriden with the PREF_TS_PAGESIZE hidden preference.
71 static int32_t getDefaultPageSize()
73 return sDefaultPageSize
;
77 * Returns a boolean value indicating whether or not the given page size is
78 * valid (currently understood as a power of 2 between 512 and 65536).
80 static bool pageSizeIsValid(int32_t aPageSize
)
82 return aPageSize
== 512 || aPageSize
== 1024 || aPageSize
== 2048 ||
83 aPageSize
== 4096 || aPageSize
== 8192 || aPageSize
== 16384 ||
84 aPageSize
== 32768 || aPageSize
== 65536;
88 * Registers the connection with the storage service. Connections are
89 * registered so they can be iterated over.
91 * @pre mRegistrationMutex is not held
94 * The connection to register.
96 void registerConnection(Connection
*aConnection
);
99 * Unregisters the connection with the storage service.
101 * @pre mRegistrationMutex is not held
104 * The connection to unregister.
106 void unregisterConnection(Connection
*aConnection
);
109 * Gets the list of open connections. Note that you must test each
110 * connection with mozIStorageConnection::connectionReady before doing
111 * anything with it, and skip it if it's not ready.
113 * @pre mRegistrationMutex is not held
115 * @param aConnections
116 * An inout param; it is cleared and the connections are appended to
118 * @return The open connections.
120 void getConnections(nsTArray
<RefPtr
<Connection
> >& aConnections
);
127 * Used for 1) locking around calls when initializing connections so that we
128 * can ensure that the state of sqlite3_enable_shared_cache is sane and 2)
129 * synchronizing access to mLocaleCollation.
133 sqlite3_vfs
*mSqliteVFS
;
136 * Protects mConnections.
138 Mutex mRegistrationMutex
;
141 * The list of connections we have created. Modifications to it are
142 * protected by |mRegistrationMutex|.
144 nsTArray
<RefPtr
<Connection
> > mConnections
;
147 * Frees as much heap memory as possible from all of the known open
150 void minimizeMemory();
153 * Lazily creates and returns a collation created from the application's
154 * locale that all statements of all Connections of this Service may use.
155 * Since the collation's lifetime is that of the Service and no statement may
156 * execute outside the lifetime of the Service, this method returns a raw
159 nsICollation
*getLocaleCollation();
162 * Lazily created collation that all statements of all Connections of this
163 * Service may use. The collation is created from the application's locale.
165 * @note Collation implementations are platform-dependent and in general not
166 * thread-safe. Access to this collation should be synchronized.
168 nsCOMPtr
<nsICollation
> mLocaleCollation
;
170 nsCOMPtr
<nsIFile
> mProfileStorageFile
;
172 nsCOMPtr
<nsIMemoryReporter
> mStorageSQLiteReporter
;
174 static Service
*gService
;
176 static int32_t sSynchronousPref
;
177 static int32_t sDefaultPageSize
;
180 } // namespace storage
181 } // namespace mozilla
183 #endif /* MOZSTORAGESERVICE_H */