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
;
28 class Service
: public mozIStorageService
30 , public nsIMemoryReporter
34 * Initializes the service. This must be called before any other function!
36 nsresult
initialize();
39 * Compares two strings using the Service's locale-aware collation.
42 * The string to be compared against aStr2.
44 * The string to be compared against aStr1.
45 * @param aComparisonStrength
46 * The sorting strength, one of the nsICollation constants.
47 * @return aStr1 - aStr2. That is, if aStr1 < aStr2, returns a negative
48 * number. If aStr1 > aStr2, returns a positive number. If
49 * aStr1 == aStr2, returns 0.
51 int localeCompareStrings(const nsAString
&aStr1
,
52 const nsAString
&aStr2
,
53 int32_t aComparisonStrength
);
55 static Service
*getSingleton();
57 NS_DECL_THREADSAFE_ISUPPORTS
58 NS_DECL_MOZISTORAGESERVICE
60 NS_DECL_NSIMEMORYREPORTER
63 * Obtains an already AddRefed pointer to XPConnect. This is used by
66 static already_AddRefed
<nsIXPConnect
> getXPConnect();
69 * Obtains the cached data for the toolkit.storage.synchronous preference.
71 static int32_t getSynchronousPref();
74 * Obtains the default page size for this platform. The default value is
75 * specified in the SQLite makefile (SQLITE_DEFAULT_PAGE_SIZE) but it may be
76 * overriden with the PREF_TS_PAGESIZE hidden preference.
78 static int32_t getDefaultPageSize()
80 return sDefaultPageSize
;
84 * Returns a boolean value indicating whether or not the given page size is
85 * valid (currently understood as a power of 2 between 512 and 65536).
87 static bool pageSizeIsValid(int32_t aPageSize
)
89 return aPageSize
== 512 || aPageSize
== 1024 || aPageSize
== 2048 ||
90 aPageSize
== 4096 || aPageSize
== 8192 || aPageSize
== 16384 ||
91 aPageSize
== 32768 || aPageSize
== 65536;
95 * Registers the connection with the storage service. Connections are
96 * registered so they can be iterated over.
98 * @pre mRegistrationMutex is not held
101 * The connection to register.
103 void registerConnection(Connection
*aConnection
);
106 * Unregisters the connection with the storage service.
108 * @pre mRegistrationMutex is not held
111 * The connection to unregister.
113 void unregisterConnection(Connection
*aConnection
);
116 * Gets the list of open connections. Note that you must test each
117 * connection with mozIStorageConnection::connectionReady before doing
118 * anything with it, and skip it if it's not ready.
120 * @pre mRegistrationMutex is not held
122 * @param aConnections
123 * An inout param; it is cleared and the connections are appended to
125 * @return The open connections.
127 void getConnections(nsTArray
<nsRefPtr
<Connection
> >& aConnections
);
134 * Used for 1) locking around calls when initializing connections so that we
135 * can ensure that the state of sqlite3_enable_shared_cache is sane and 2)
136 * synchronizing access to mLocaleCollation.
140 sqlite3_vfs
*mSqliteVFS
;
143 * Protects mConnections.
145 Mutex mRegistrationMutex
;
148 * The list of connections we have created. Modifications to it are
149 * protected by |mRegistrationMutex|.
151 nsTArray
<nsRefPtr
<Connection
> > mConnections
;
154 * Frees as much heap memory as possible from all of the known open
157 void minimizeMemory();
160 * Shuts down the storage service, freeing all of the acquired resources.
165 * Lazily creates and returns a collation created from the application's
166 * locale that all statements of all Connections of this Service may use.
167 * Since the collation's lifetime is that of the Service and no statement may
168 * execute outside the lifetime of the Service, this method returns a raw
171 nsICollation
*getLocaleCollation();
174 * Lazily created collation that all statements of all Connections of this
175 * Service may use. The collation is created from the application's locale.
177 * @note Collation implementations are platform-dependent and in general not
178 * thread-safe. Access to this collation should be synchronized.
180 nsCOMPtr
<nsICollation
> mLocaleCollation
;
182 nsCOMPtr
<nsIFile
> mProfileStorageFile
;
184 nsCOMPtr
<nsIMemoryReporter
> mStorageSQLiteReporter
;
186 static Service
*gService
;
188 static nsIXPConnect
*sXPConnect
;
190 static int32_t sSynchronousPref
;
191 static int32_t sDefaultPageSize
;
194 } // namespace storage
195 } // namespace mozilla
197 #endif /* MOZSTORAGESERVICE_H */