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 mozilla_storage_Connection_h
8 #define mozilla_storage_Connection_h
10 #include "nsAutoPtr.h"
12 #include "mozilla/Atomics.h"
13 #include "mozilla/Mutex.h"
14 #include "nsProxyRelease.h"
15 #include "nsThreadUtils.h"
16 #include "nsIInterfaceRequestor.h"
18 #include "nsDataHashtable.h"
19 #include "mozIStorageProgressHandler.h"
20 #include "SQLiteMutex.h"
21 #include "mozIStorageConnection.h"
22 #include "mozStorageService.h"
23 #include "mozIStorageAsyncConnection.h"
24 #include "mozIStorageCompletionCallback.h"
26 #include "mozilla/Attributes.h"
38 class Connection final
: public mozIStorageConnection
,
39 public nsIInterfaceRequestor
{
41 NS_DECL_THREADSAFE_ISUPPORTS
42 NS_DECL_MOZISTORAGEASYNCCONNECTION
43 NS_DECL_MOZISTORAGECONNECTION
44 NS_DECL_NSIINTERFACEREQUESTOR
47 * Indicates if a database operation is synchronous or asynchronous.
49 * - Async operations may be called from any thread for all connections.
50 * - Sync operations may be called from any thread for sync connections, and
51 * from background threads for async connections.
53 enum ConnectionOperation
{ ASYNCHRONOUS
, SYNCHRONOUS
};
56 * Structure used to describe user functions on the database connection.
59 enum FunctionType
{ SIMPLE
, AGGREGATE
};
61 nsCOMPtr
<nsISupports
> function
;
68 * Pointer to the storage service. Held onto for the lifetime of the
71 * The flags to pass to sqlite3_open_v2.
72 * @param aSupportedOperations
73 * The operation types supported on this connection. All connections
74 * implement both the async (`mozIStorageAsyncConnection`) and sync
75 * (`mozIStorageConnection`) interfaces, but async connections may not
76 * call sync operations from the main thread.
77 * @param aIgnoreLockingMode
78 * If |true|, ignore locks in force on the file. Only usable with
79 * read-only connections. Defaults to false.
80 * Use with extreme caution. If sqlite ignores locks, reads may fail
81 * indicating database corruption (the database won't actually be
82 * corrupt) or produce wrong results without any indication that has
85 Connection(Service
* aService
, int aFlags
,
86 ConnectionOperation aSupportedOperations
,
87 bool aIgnoreLockingMode
= false);
90 * Creates the connection to an in-memory database.
92 nsresult
initialize();
95 * Creates the connection to the database.
97 * @param aDatabaseFile
98 * The nsIFile of the location of the database to open, or create if it
101 nsresult
initialize(nsIFile
* aDatabaseFile
);
104 * Creates the connection to the database.
107 * The nsIFileURL of the location of the database to open, or create if
110 nsresult
initialize(nsIFileURL
* aFileURL
);
113 * Same as initialize, but to be used on the async thread.
115 nsresult
initializeOnAsyncThread(nsIFile
* aStorageFile
);
118 * Fetches runtime status information for this connection.
120 * @param aStatusOption One of the SQLITE_DBSTATUS options defined at
121 * http://www.sqlite.org/c3ref/c_dbstatus_options.html
122 * @param [optional] aMaxValue if provided, will be set to the highest
123 * istantaneous value.
124 * @return the current value for the specified option.
126 int32_t getSqliteRuntimeStatus(int32_t aStatusOption
,
127 int32_t* aMaxValue
= nullptr);
129 * Registers/unregisters a commit hook callback.
131 * @param aCallbackFn a callback function to be invoked on transactions
132 * commit. Pass nullptr to unregister the current callback.
133 * @param [optional] aData if provided, will be passed to the callback.
134 * @see http://sqlite.org/c3ref/commit_hook.html
136 void setCommitHook(int (*aCallbackFn
)(void*), void* aData
= nullptr) {
137 MOZ_ASSERT(mDBConn
, "A connection must exist at this point");
138 ::sqlite3_commit_hook(mDBConn
, aCallbackFn
, aData
);
142 * Gets autocommit status.
144 bool getAutocommit() {
145 return mDBConn
&& static_cast<bool>(::sqlite3_get_autocommit(mDBConn
));
149 * Lazily creates and returns a background execution thread. In the future,
150 * the thread may be re-claimed if left idle, so you should call this
151 * method just before you dispatch and not save the reference.
153 * This must be called from the opener thread.
155 * @return an event target suitable for asynchronous statement execution.
156 * @note This method will return null once AsyncClose() has been called.
158 nsIEventTarget
* getAsyncExecutionTarget();
161 * Mutex used by asynchronous statements to protect state. The mutex is
162 * declared on the connection object because there is no contention between
163 * asynchronous statements (they are serialized on mAsyncExecutionThread).
164 * Currently protects:
165 * - Connection.mAsyncExecutionThreadShuttingDown
166 * - Connection.mConnectionClosed
167 * - AsyncExecuteStatements.mCancelRequested
169 Mutex sharedAsyncExecutionMutex
;
172 * Wraps the mutex that SQLite gives us from sqlite3_db_mutex. This is public
173 * because we already expose the sqlite3* native connection and proper
174 * operation of the deadlock detector requires everyone to use the same single
175 * SQLiteMutex instance for correctness.
177 SQLiteMutex sharedDBMutex
;
180 * References the thread this database was opened on. This MUST be thread it
183 const nsCOMPtr
<nsIThread
> threadOpenedOn
;
186 * Closes the SQLite database, and warns about any non-finalized statements.
188 nsresult
internalClose(sqlite3
* aDBConn
);
191 * Shuts down the passed-in async thread.
193 void shutdownAsyncThread();
196 * Obtains the filename of the connection. Useful for logging.
198 nsCString
getFilename();
201 * Creates an sqlite3 prepared statement object from an SQL string.
203 * @param aNativeConnection
204 * The underlying Sqlite connection to prepare the statement with.
206 * The SQL statement string to compile.
208 * New sqlite3_stmt object.
209 * @return the result from sqlite3_prepare_v2.
211 int prepareStatement(sqlite3
* aNativeConnection
, const nsCString
& aSQL
,
212 sqlite3_stmt
** _stmt
);
215 * Performs a sqlite3_step on aStatement, while properly handling
216 * SQLITE_LOCKED when not on the main thread by waiting until we are notified.
218 * @param aNativeConnection
219 * The underlying Sqlite connection to step the statement with.
221 * A pointer to a sqlite3_stmt object.
222 * @return the result from sqlite3_step.
224 int stepStatement(sqlite3
* aNativeConnection
, sqlite3_stmt
* aStatement
);
227 * Raw connection transaction management.
229 * @see BeginTransactionAs, CommitTransaction, RollbackTransaction.
231 nsresult
beginTransactionInternal(
232 sqlite3
* aNativeConnection
,
233 int32_t aTransactionType
= TRANSACTION_DEFERRED
);
234 nsresult
commitTransactionInternal(sqlite3
* aNativeConnection
);
235 nsresult
rollbackTransactionInternal(sqlite3
* aNativeConnection
);
238 * Indicates if this database connection is open.
240 inline bool connectionReady() { return mDBConn
!= nullptr; }
243 * Indicates if this database connection supports the given operation.
245 * @param aOperationType
246 * The operation type, sync or async.
247 * @return `true` if the operation is supported, `false` otherwise.
249 bool operationSupported(ConnectionOperation aOperationType
);
252 * Thread-aware version of connectionReady, results per caller's thread are:
253 * - owner thread: Same as connectionReady(). True means we have a valid,
254 * un-closed database connection and it's not going away until you invoke
255 * Close() or AsyncClose().
256 * - async thread: Returns true at all times because you can't schedule
257 * runnables against the async thread after AsyncClose() has been called.
258 * Therefore, the connection is still around if your code is running.
259 * - any other thread: Race-prone Lies! If you are main-thread code in
260 * mozStorageService iterating over the list of connections, you need to
261 * acquire the sharedAsyncExecutionMutex for the connection, invoke
262 * connectionReady() while holding it, and then continue to hold it while
263 * you do whatever you need to do. This is because of off-main-thread
264 * consumers like dom/cache and IndexedDB and other QuotaManager clients.
266 bool isConnectionReadyOnThisThread();
269 * True if this connection has inited shutdown.
274 * True if the underlying connection is closed.
275 * Any sqlite resources may be lost when this returns true, so nothing should
277 * This locks on sharedAsyncExecutionMutex.
282 * Same as isClosed(), but takes a proof-of-lock instead of locking
285 bool isClosed(MutexAutoLock
& lock
);
288 * True if the async execution thread is alive and able to be used (i.e., it
289 * is not in the process of shutting down.)
291 * This must be called from the opener thread.
293 bool isAsyncExecutionThreadAvailable();
295 nsresult
initializeClone(Connection
* aClone
, bool aReadOnly
);
299 nsresult
initializeInternal();
300 void initializeFailed();
303 * Sets the database into a closed state so no further actions can be
306 * @note mDBConn is set to nullptr in this method.
308 nsresult
setClosedState();
311 * Helper for calls to sqlite3_exec. Reports long delays to Telemetry.
313 * @param aNativeConnection
314 * The underlying Sqlite connection to execute the query with.
316 * SQL string to execute
317 * @return the result from sqlite3_exec.
319 int executeSql(sqlite3
* aNativeConnection
, const char* aSqlString
);
322 * Describes a certain primitive type in the database.
324 * Possible Values Are:
325 * INDEX - To check for the existence of an index
326 * TABLE - To check for the existence of a table
328 enum DatabaseElementType
{ INDEX
, TABLE
};
331 * Determines if the specified primitive exists.
333 * @param aElementType
334 * The type of element to check the existence of
335 * @param aElementName
336 * The name of the element to check for
337 * @returns true if element exists, false otherwise
339 nsresult
databaseElementExists(enum DatabaseElementType aElementType
,
340 const nsACString
& aElementName
, bool* _exists
);
342 bool findFunctionByInstance(nsISupports
* aInstance
);
344 static int sProgressHelper(void* aArg
);
345 // Generic progress handler
346 // Dispatch call to registered progress handler,
347 // if there is one. Do nothing in other cases.
348 int progressHandler();
351 * Like `operationSupported`, but throws (and, in a debug build, asserts) if
352 * the operation is unsupported.
354 nsresult
ensureOperationSupported(ConnectionOperation aOperationType
);
357 nsCOMPtr
<nsIFileURL
> mFileURL
;
358 nsCOMPtr
<nsIFile
> mDatabaseFile
;
361 * The filename that will be reported to telemetry for this connection. By
362 * default this will be the leaf of the path to the database file.
364 nsCString mTelemetryFilename
;
367 * Lazily created thread for asynchronous statement execution. Consumers
368 * should use getAsyncExecutionTarget rather than directly accessing this
371 * This must be modified only on the opener thread.
373 nsCOMPtr
<nsIThread
> mAsyncExecutionThread
;
376 * Set to true by Close() or AsyncClose() prior to shutdown.
378 * If false, we guarantee both that the underlying sqlite3 database
379 * connection is still open and that getAsyncExecutionTarget() can
380 * return a thread. Once true, either the sqlite3 database
381 * connection is being shutdown or it has been
382 * shutdown. Additionally, once true, getAsyncExecutionTarget()
385 * This variable should be accessed while holding the
386 * sharedAsyncExecutionMutex.
388 bool mAsyncExecutionThreadShuttingDown
;
391 * Set to true just prior to calling sqlite3_close on the
394 * This variable should be accessed while holding the
395 * sharedAsyncExecutionMutex.
397 bool mConnectionClosed
;
400 * Stores the default behavior for all transactions run on this connection.
402 mozilla::Atomic
<int32_t> mDefaultTransactionType
;
405 * Tracks if we have a transaction in progress or not. Access protected by
408 bool mTransactionInProgress
;
411 * Used to trigger cleanup logic only the first time our refcount hits 1. We
412 * may trigger a failsafe Close() that invokes SpinningSynchronousClose()
413 * which invokes AsyncClose() which may bump our refcount back up to 2 (and
414 * which will then fall back down to 1 again). It's also possible that the
415 * Service may bump our refcount back above 1 if getConnections() runs before
416 * we invoke unregisterConnection().
418 mozilla::Atomic
<bool> mDestroying
;
421 * Stores the mapping of a given function by name to its instance. Access is
422 * protected by sharedDBMutex.
424 nsDataHashtable
<nsCStringHashKey
, FunctionInfo
> mFunctions
;
427 * Stores the registered progress handler for the database connection. Access
428 * is protected by sharedDBMutex.
430 nsCOMPtr
<mozIStorageProgressHandler
> mProgressHandler
;
433 * Stores the flags we passed to sqlite3_open_v2.
438 * Stores whether we should ask sqlite3_open_v2 to ignore locking.
440 const bool mIgnoreLockingMode
;
442 // This is here for two reasons: 1) It's used to make sure that the
443 // connections do not outlive the service. 2) Our custom collating functions
444 // call its localeCompareStrings() method.
445 RefPtr
<Service
> mStorageService
;
448 * Indicates which operations are supported on this connection.
450 const ConnectionOperation mSupportedOperations
;
452 nsresult
synchronousClose();
456 * A Runnable designed to call a mozIStorageCompletionCallback on
457 * the appropriate thread.
459 class CallbackComplete final
: public Runnable
{
462 * @param aValue The result to pass to the callback. It must
463 * already be owned by the main thread.
464 * @param aCallback The callback. It must already be owned by the
467 CallbackComplete(nsresult aStatus
, nsISupports
* aValue
,
468 already_AddRefed
<mozIStorageCompletionCallback
> aCallback
)
469 : Runnable("storage::CallbackComplete"),
472 mCallback(aCallback
) {}
474 NS_IMETHOD
Run() override
{
475 MOZ_ASSERT(NS_IsMainThread());
476 nsresult rv
= mCallback
->Complete(mStatus
, mValue
);
478 // Ensure that we release on the main thread
486 nsCOMPtr
<nsISupports
> mValue
;
487 // This is a RefPtr<T> and not a nsCOMPtr<T> because
488 // nsCOMP<T> would cause an off-main thread QI, which
489 // is not a good idea (and crashes XPConnect).
490 RefPtr
<mozIStorageCompletionCallback
> mCallback
;
493 } // namespace storage
494 } // namespace mozilla
497 * Casting Connection to nsISupports is ambiguous.
498 * This method handles that.
500 inline nsISupports
* ToSupports(mozilla::storage::Connection
* p
) {
501 return NS_ISUPPORTS_CAST(mozIStorageAsyncConnection
*, p
);
504 #endif // mozilla_storage_Connection_h