Bug 1853320 [wpt PR 41940] - [FLEDGE] Split up runs of /trusted-scoring-signals.https...
[gecko.git] / storage / mozStorageService.h
blobfc8686f1223875292bfaa15c5a7fef9019fbed97
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::storage {
29 class Connection;
30 class Service : public mozIStorageService,
31 public nsIObserver,
32 public nsIMemoryReporter {
33 public:
34 /**
35 * Initializes the service. This must be called before any other function!
37 nsresult initialize();
39 /**
40 * Compares two strings using the Service's locale-aware collation.
42 * @param aStr1
43 * The string to be compared against aStr2.
44 * @param aStr2
45 * The string to be compared against aStr1.
46 * @param aSensitivity
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
59 NS_DECL_NSIOBSERVER
60 NS_DECL_NSIMEMORYREPORTER
62 /**
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;
74 /**
75 * Registers the connection with the storage service. Connections are
76 * registered so they can be iterated over.
78 * @pre mRegistrationMutex is not held
80 * @param aConnection
81 * The connection to register.
83 void registerConnection(Connection* aConnection);
85 /**
86 * Unregisters the connection with the storage service.
88 * @pre mRegistrationMutex is not held
90 * @param aConnection
91 * The connection to unregister.
93 void unregisterConnection(Connection* aConnection);
95 /**
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
104 * it.
105 * @return The open connections.
107 void getConnections(nsTArray<RefPtr<Connection> >& aConnections);
109 private:
110 Service();
111 virtual ~Service();
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();
124 private:
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
130 // order.
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
150 * connections.
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
159 * pointer.
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 */