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
12 #include "nsIMemoryReporter.h"
13 #include "nsIObserver.h"
15 #include "mozilla/Mutex.h"
16 #include "mozilla/UniquePtr.h"
17 #include "mozilla/intl/Collator.h"
19 #include "mozIStorageService.h"
21 class nsIMemoryReporter
;
23 namespace mozilla::intl
{
31 class Service
: public mozIStorageService
,
33 public nsIMemoryReporter
{
36 * Initializes the service. This must be called before any other function!
38 nsresult
initialize();
41 * Compares two strings using the Service's locale-aware collation.
44 * The string to be compared against aStr2.
46 * The string to be compared against aStr1.
48 * The sorting sensitivity.
49 * @return aStr1 - aStr2. That is, if aStr1 < aStr2, returns a negative
50 * number. If aStr1 > aStr2, returns a positive number. If
51 * aStr1 == aStr2, returns 0.
53 int localeCompareStrings(const nsAString
& aStr1
, const nsAString
& aStr2
,
54 mozilla::intl::Collator::Sensitivity aSensitivity
);
56 static already_AddRefed
<Service
> getSingleton();
58 NS_DECL_THREADSAFE_ISUPPORTS
59 NS_DECL_MOZISTORAGESERVICE
61 NS_DECL_NSIMEMORYREPORTER
64 * Returns a boolean value indicating whether or not the given page size is
65 * valid (currently understood as a power of 2 between 512 and 65536).
67 static bool pageSizeIsValid(int32_t aPageSize
) {
68 return aPageSize
== 512 || aPageSize
== 1024 || aPageSize
== 2048 ||
69 aPageSize
== 4096 || aPageSize
== 8192 || aPageSize
== 16384 ||
70 aPageSize
== 32768 || aPageSize
== 65536;
73 static const int32_t kDefaultPageSize
= 32768;
76 * Registers the connection with the storage service. Connections are
77 * registered so they can be iterated over.
79 * @pre mRegistrationMutex is not held
82 * The connection to register.
84 void registerConnection(Connection
* aConnection
);
87 * Unregisters the connection with the storage service.
89 * @pre mRegistrationMutex is not held
92 * The connection to unregister.
94 void unregisterConnection(Connection
* aConnection
);
97 * Gets the list of open connections. Note that you must test each
98 * connection with mozIStorageConnection::connectionReady before doing
99 * anything with it, and skip it if it's not ready.
101 * @pre mRegistrationMutex is not held
103 * @param aConnections
104 * An inout param; it is cleared and the connections are appended to
106 * @return The open connections.
108 void getConnections(nsTArray
<RefPtr
<Connection
> >& aConnections
);
115 * Used for 1) locking around calls when initializing connections so that we
116 * can ensure that the state of sqlite3_enable_shared_cache is sane and 2)
117 * synchronizing access to mLocaleCollation.
121 struct AutoVFSRegistration
{
122 int Init(UniquePtr
<sqlite3_vfs
> aVFS
);
123 ~AutoVFSRegistration();
126 UniquePtr
<sqlite3_vfs
> mVFS
;
129 // The order of these members should match the order of Init calls in
130 // initialize(), to ensure that the unregistration takes place in the reverse
132 AutoVFSRegistration mTelemetrySqliteVFS
;
133 AutoVFSRegistration mTelemetryExclSqliteVFS
;
134 AutoVFSRegistration mObfuscatingSqliteVFS
;
137 * Protects mConnections.
139 Mutex mRegistrationMutex
;
142 * The list of connections we have created. Modifications to it are
143 * protected by |mRegistrationMutex|.
145 nsTArray
<RefPtr
<Connection
> > mConnections
;
148 * Frees as much heap memory as possible from all of the known open
151 void minimizeMemory();
154 * Lazily creates and returns a collator created from the application's
155 * locale that all statements of all Connections of this Service may use.
156 * Since the collator's lifetime is that of the Service and no statement may
157 * execute outside the lifetime of the Service, this method returns a raw
160 mozilla::intl::Collator
* getCollator();
163 * Lazily created collator that all statements of all Connections of this
164 * Service may use. The collator is created from the application's locale.
166 * @note The collator is not thread-safe since the options can be changed
167 * between calls. Access should be synchronized.
169 mozilla::UniquePtr
<mozilla::intl::Collator
> mCollator
= nullptr;
171 nsCOMPtr
<nsIFile
> mProfileStorageFile
;
173 nsCOMPtr
<nsIMemoryReporter
> mStorageSQLiteReporter
;
175 static Service
* gService
;
177 mozilla::intl::Collator::Sensitivity mLastSensitivity
;
180 } // namespace storage
181 } // namespace mozilla
183 #endif /* MOZSTORAGESERVICE_H */