Bug 1828719 - Remove omnijar Gradle project from srcdir r=geckoview-reviewers,nalexan...
[gecko.git] / storage / mozStorageService.h
blobfe98e6bd74d53570ca91bfa1334a464f3bdaca14
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
10 #include "nsCOMPtr.h"
11 #include "nsIFile.h"
12 #include "nsIMemoryReporter.h"
13 #include "nsIObserver.h"
14 #include "nsTArray.h"
15 #include "mozilla/Mutex.h"
16 #include "mozilla/UniquePtr.h"
17 #include "mozilla/intl/Collator.h"
19 #include "mozIStorageService.h"
21 class nsIMemoryReporter;
22 struct sqlite3_vfs;
23 namespace mozilla::intl {
24 class Collator;
27 namespace mozilla {
28 namespace storage {
30 class Connection;
31 class Service : public mozIStorageService,
32 public nsIObserver,
33 public nsIMemoryReporter {
34 public:
35 /**
36 * Initializes the service. This must be called before any other function!
38 nsresult initialize();
40 /**
41 * Compares two strings using the Service's locale-aware collation.
43 * @param aStr1
44 * The string to be compared against aStr2.
45 * @param aStr2
46 * The string to be compared against aStr1.
47 * @param aSensitivity
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
60 NS_DECL_NSIOBSERVER
61 NS_DECL_NSIMEMORYREPORTER
63 /**
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;
75 /**
76 * Registers the connection with the storage service. Connections are
77 * registered so they can be iterated over.
79 * @pre mRegistrationMutex is not held
81 * @param aConnection
82 * The connection to register.
84 void registerConnection(Connection* aConnection);
86 /**
87 * Unregisters the connection with the storage service.
89 * @pre mRegistrationMutex is not held
91 * @param aConnection
92 * The connection to unregister.
94 void unregisterConnection(Connection* aConnection);
96 /**
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
105 * it.
106 * @return The open connections.
108 void getConnections(nsTArray<RefPtr<Connection> >& aConnections);
110 private:
111 Service();
112 virtual ~Service();
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.
119 Mutex mMutex MOZ_UNANNOTATED;
121 struct AutoVFSRegistration {
122 int Init(UniquePtr<sqlite3_vfs> aVFS);
123 ~AutoVFSRegistration();
125 private:
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
131 // order.
132 AutoVFSRegistration mTelemetrySqliteVFS;
133 AutoVFSRegistration mTelemetryExclSqliteVFS;
134 AutoVFSRegistration mQuotaSqliteVFS;
135 AutoVFSRegistration mObfuscatingSqliteVFS;
136 AutoVFSRegistration mReadOnlyNoLockSqliteVFS;
139 * Protects mConnections.
141 Mutex mRegistrationMutex MOZ_UNANNOTATED;
144 * The list of connections we have created. Modifications to it are
145 * protected by |mRegistrationMutex|.
147 nsTArray<RefPtr<Connection> > mConnections;
150 * Frees as much heap memory as possible from all of the known open
151 * connections.
153 void minimizeMemory();
156 * Lazily creates and returns a collator created from the application's
157 * locale that all statements of all Connections of this Service may use.
158 * Since the collator's lifetime is that of the Service and no statement may
159 * execute outside the lifetime of the Service, this method returns a raw
160 * pointer.
162 mozilla::intl::Collator* getCollator();
165 * Lazily created collator that all statements of all Connections of this
166 * Service may use. The collator is created from the application's locale.
168 * @note The collator is not thread-safe since the options can be changed
169 * between calls. Access should be synchronized.
171 mozilla::UniquePtr<mozilla::intl::Collator> mCollator = nullptr;
173 nsCOMPtr<nsIFile> mProfileStorageFile;
175 nsCOMPtr<nsIMemoryReporter> mStorageSQLiteReporter;
177 static Service* gService;
179 mozilla::intl::Collator::Sensitivity mLastSensitivity;
182 } // namespace storage
183 } // namespace mozilla
185 #endif /* MOZSTORAGESERVICE_H */