Bug 1686838 [wpt PR 27194] - Update wpt metadata, a=testonly
[gecko.git] / storage / mozStorageService.h
blob9b1e973f52813791618c0d25f65651ee581ad818
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 "nsICollation.h"
12 #include "nsIFile.h"
13 #include "nsIMemoryReporter.h"
14 #include "nsIObserver.h"
15 #include "nsTArray.h"
16 #include "mozilla/Mutex.h"
18 #include "mozIStorageService.h"
20 class nsIMemoryReporter;
21 struct sqlite3_vfs;
23 namespace mozilla {
24 namespace storage {
26 class Connection;
27 class Service : public mozIStorageService,
28 public nsIObserver,
29 public nsIMemoryReporter {
30 public:
31 /**
32 * Initializes the service. This must be called before any other function!
34 nsresult initialize();
36 /**
37 * Compares two strings using the Service's locale-aware collation.
39 * @param aStr1
40 * The string to be compared against aStr2.
41 * @param 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
56 NS_DECL_NSIOBSERVER
57 NS_DECL_NSIMEMORYREPORTER
59 /**
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;
71 /**
72 * Registers the connection with the storage service. Connections are
73 * registered so they can be iterated over.
75 * @pre mRegistrationMutex is not held
77 * @param aConnection
78 * The connection to register.
80 void registerConnection(Connection* aConnection);
82 /**
83 * Unregisters the connection with the storage service.
85 * @pre mRegistrationMutex is not held
87 * @param aConnection
88 * The connection to unregister.
90 void unregisterConnection(Connection* aConnection);
92 /**
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
99 * @param aConnections
100 * An inout param; it is cleared and the connections are appended to
101 * it.
102 * @return The open connections.
104 void getConnections(nsTArray<RefPtr<Connection> >& aConnections);
106 private:
107 Service();
108 virtual ~Service();
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.
115 Mutex mMutex;
117 struct AutoVFSRegistration {
118 int Init(UniquePtr<sqlite3_vfs> aVFS);
119 ~AutoVFSRegistration();
121 private:
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
127 // order.
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
145 * connections.
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
154 * pointer.
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 */