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
{
27 namespace mozilla::storage
{
30 class Service
: public mozIStorageService
,
32 public nsIMemoryReporter
{
35 * Initializes the service. This must be called before any other function!
37 nsresult
initialize();
40 * Compares two strings using the Service's locale-aware collation.
43 * The string to be compared against aStr2.
45 * The string to be compared against aStr1.
47 * The sorting sensitivity.
48 * @return aStr1 - aStr2. That is, if aStr1 < aStr2, returns a negative
49 * number. If aStr1 > aStr2, returns a positive number. If
50 * aStr1 == aStr2, returns 0.
52 int localeCompareStrings(const nsAString
& aStr1
, const nsAString
& aStr2
,
53 mozilla::intl::Collator::Sensitivity aSensitivity
);
55 static already_AddRefed
<Service
> getSingleton();
57 NS_DECL_THREADSAFE_ISUPPORTS
58 NS_DECL_MOZISTORAGESERVICE
60 NS_DECL_NSIMEMORYREPORTER
63 * Returns a boolean value indicating whether or not the given page size is
64 * valid (currently understood as a power of 2 between 512 and 65536).
66 static bool pageSizeIsValid(int32_t aPageSize
) {
67 return aPageSize
== 512 || aPageSize
== 1024 || aPageSize
== 2048 ||
68 aPageSize
== 4096 || aPageSize
== 8192 || aPageSize
== 16384 ||
69 aPageSize
== 32768 || aPageSize
== 65536;
72 static const int32_t kDefaultPageSize
= 32768;
75 * Registers the connection with the storage service. Connections are
76 * registered so they can be iterated over.
78 * @pre mRegistrationMutex is not held
81 * The connection to register.
83 void registerConnection(Connection
* aConnection
);
86 * Unregisters the connection with the storage service.
88 * @pre mRegistrationMutex is not held
91 * The connection to unregister.
93 void unregisterConnection(Connection
* aConnection
);
96 * Gets the list of open connections. Note that you must test each
97 * connection with mozIStorageConnection::connectionReady before doing
98 * anything with it, and skip it if it's not ready.
100 * @pre mRegistrationMutex is not held
102 * @param aConnections
103 * An inout param; it is cleared and the connections are appended to
105 * @return The open connections.
107 void getConnections(nsTArray
<RefPtr
<Connection
> >& aConnections
);
114 * Used for 1) locking around calls when initializing connections so that we
115 * can ensure that the state of sqlite3_enable_shared_cache is sane and 2)
116 * synchronizing access to mLocaleCollation.
118 Mutex mMutex MOZ_UNANNOTATED
;
120 struct AutoVFSRegistration
{
121 int Init(UniquePtr
<sqlite3_vfs
> aVFS
);
122 ~AutoVFSRegistration();
125 UniquePtr
<sqlite3_vfs
> mVFS
;
128 // The order of these members should match the order of Init calls in
129 // initialize(), to ensure that the unregistration takes place in the reverse
131 AutoVFSRegistration mBaseSqliteVFS
;
132 AutoVFSRegistration mBaseExclSqliteVFS
;
133 AutoVFSRegistration mQuotaSqliteVFS
;
134 AutoVFSRegistration mObfuscatingSqliteVFS
;
135 AutoVFSRegistration mReadOnlyNoLockSqliteVFS
;
138 * Protects mConnections.
140 Mutex mRegistrationMutex MOZ_UNANNOTATED
;
143 * The list of connections we have created. Modifications to it are
144 * protected by |mRegistrationMutex|.
146 nsTArray
<RefPtr
<Connection
> > mConnections
;
149 * Frees as much heap memory as possible from all of the known open
152 void minimizeMemory();
155 * Lazily creates and returns a collator created from the application's
156 * locale that all statements of all Connections of this Service may use.
157 * Since the collator's lifetime is that of the Service and no statement may
158 * execute outside the lifetime of the Service, this method returns a raw
161 mozilla::intl::Collator
* getCollator();
164 * Lazily created collator that all statements of all Connections of this
165 * Service may use. The collator is created from the application's locale.
167 * @note The collator is not thread-safe since the options can be changed
168 * between calls. Access should be synchronized.
170 mozilla::UniquePtr
<mozilla::intl::Collator
> mCollator
= nullptr;
172 nsCOMPtr
<nsIFile
> mProfileStorageFile
;
174 nsCOMPtr
<nsIMemoryReporter
> mStorageSQLiteReporter
;
176 static Service
* gService
;
178 mozilla::intl::Collator::Sensitivity mLastSensitivity
;
181 } // namespace mozilla::storage
183 #endif /* MOZSTORAGESERVICE_H */