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
{
32 * Initializes the service. This must be called before any other function!
34 nsresult
initialize();
37 * Compares two strings using the Service's locale-aware collation.
40 * The string to be compared against aStr2.
42 * The string to be compared against aStr1.
43 * @param aComparisonStrength
44 * The sorting strength, one of the nsICollation constants.
45 * @return aStr1 - aStr2. That is, if aStr1 < aStr2, returns a negative
46 * number. If aStr1 > aStr2, returns a positive number. If
47 * aStr1 == aStr2, returns 0.
49 int localeCompareStrings(const nsAString
& aStr1
, const nsAString
& aStr2
,
50 int32_t aComparisonStrength
);
52 static already_AddRefed
<Service
> getSingleton();
54 NS_DECL_THREADSAFE_ISUPPORTS
55 NS_DECL_MOZISTORAGESERVICE
57 NS_DECL_NSIMEMORYREPORTER
60 * Returns a boolean value indicating whether or not the given page size is
61 * valid (currently understood as a power of 2 between 512 and 65536).
63 static bool pageSizeIsValid(int32_t aPageSize
) {
64 return aPageSize
== 512 || aPageSize
== 1024 || aPageSize
== 2048 ||
65 aPageSize
== 4096 || aPageSize
== 8192 || aPageSize
== 16384 ||
66 aPageSize
== 32768 || aPageSize
== 65536;
69 static const int32_t kDefaultPageSize
= 32768;
72 * Registers the connection with the storage service. Connections are
73 * registered so they can be iterated over.
75 * @pre mRegistrationMutex is not held
78 * The connection to register.
80 void registerConnection(Connection
* aConnection
);
83 * Unregisters the connection with the storage service.
85 * @pre mRegistrationMutex is not held
88 * The connection to unregister.
90 void unregisterConnection(Connection
* aConnection
);
93 * Gets the list of open connections. Note that you must test each
94 * connection with mozIStorageConnection::connectionReady before doing
95 * anything with it, and skip it if it's not ready.
97 * @pre mRegistrationMutex is not held
100 * An inout param; it is cleared and the connections are appended to
102 * @return The open connections.
104 void getConnections(nsTArray
<RefPtr
<Connection
> >& aConnections
);
111 * Used for 1) locking around calls when initializing connections so that we
112 * can ensure that the state of sqlite3_enable_shared_cache is sane and 2)
113 * synchronizing access to mLocaleCollation.
117 struct AutoVFSRegistration
{
118 int Init(UniquePtr
<sqlite3_vfs
> aVFS
);
119 ~AutoVFSRegistration();
122 UniquePtr
<sqlite3_vfs
> mVFS
;
125 // The order of these members should match the order of Init calls in
126 // initialize(), to ensure that the unregistration takes place in the reverse
128 AutoVFSRegistration mTelemetrySqliteVFS
;
129 AutoVFSRegistration mTelemetryExclSqliteVFS
;
130 AutoVFSRegistration mObfuscatingSqliteVFS
;
133 * Protects mConnections.
135 Mutex mRegistrationMutex
;
138 * The list of connections we have created. Modifications to it are
139 * protected by |mRegistrationMutex|.
141 nsTArray
<RefPtr
<Connection
> > mConnections
;
144 * Frees as much heap memory as possible from all of the known open
147 void minimizeMemory();
150 * Lazily creates and returns a collation created from the application's
151 * locale that all statements of all Connections of this Service may use.
152 * Since the collation's lifetime is that of the Service and no statement may
153 * execute outside the lifetime of the Service, this method returns a raw
156 nsICollation
* getLocaleCollation();
159 * Lazily created collation that all statements of all Connections of this
160 * Service may use. The collation is created from the application's locale.
162 * @note Collation implementations are platform-dependent and in general not
163 * thread-safe. Access to this collation should be synchronized.
165 nsCOMPtr
<nsICollation
> mLocaleCollation
;
167 nsCOMPtr
<nsIFile
> mProfileStorageFile
;
169 nsCOMPtr
<nsIMemoryReporter
> mStorageSQLiteReporter
;
171 static Service
* gService
;
174 } // namespace storage
175 } // namespace mozilla
177 #endif /* MOZSTORAGESERVICE_H */