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 "nsIMutableArray.h"
27 #include "mozilla/Attributes.h"
39 class Connection final
: public mozIStorageConnection
40 , public nsIInterfaceRequestor
43 NS_DECL_THREADSAFE_ISUPPORTS
44 NS_DECL_MOZISTORAGEASYNCCONNECTION
45 NS_DECL_MOZISTORAGECONNECTION
46 NS_DECL_NSIINTERFACEREQUESTOR
49 * Structure used to describe user functions on the database connection.
57 nsCOMPtr
<nsISupports
> function
;
64 * Pointer to the storage service. Held onto for the lifetime of the
67 * The flags to pass to sqlite3_open_v2.
69 * If |true|, the Connection only implements asynchronous interface:
70 * - |mozIStorageAsyncConnection|;
71 * If |false|, the result also implements synchronous interface:
72 * - |mozIStorageConnection|.
73 * @param aIgnoreLockingMode
74 * If |true|, ignore locks in force on the file. Only usable with
75 * read-only connections. Defaults to false.
76 * Use with extreme caution. If sqlite ignores locks, reads may fail
77 * indicating database corruption (the database won't actually be
78 * corrupt) or produce wrong results without any indication that has
81 Connection(Service
*aService
, int aFlags
, bool aAsyncOnly
,
82 bool aIgnoreLockingMode
= false);
85 * Creates the connection to an in-memory database.
87 nsresult
initialize();
90 * Creates the connection to the database.
92 * @param aDatabaseFile
93 * The nsIFile of the location of the database to open, or create if it
96 nsresult
initialize(nsIFile
*aDatabaseFile
);
99 * Creates the connection to the database.
102 * The nsIFileURL of the location of the database to open, or create if it
105 nsresult
initialize(nsIFileURL
*aFileURL
);
108 * Same as initialize, but to be used on the async thread.
110 nsresult
initializeOnAsyncThread(nsIFile
* aStorageFile
);
113 * Fetches runtime status information for this connection.
115 * @param aStatusOption One of the SQLITE_DBSTATUS options defined at
116 * http://www.sqlite.org/c3ref/c_dbstatus_options.html
117 * @param [optional] aMaxValue if provided, will be set to the highest
118 * istantaneous value.
119 * @return the current value for the specified option.
121 int32_t getSqliteRuntimeStatus(int32_t aStatusOption
,
122 int32_t* aMaxValue
=nullptr);
124 * Registers/unregisters a commit hook callback.
126 * @param aCallbackFn a callback function to be invoked on transactions
127 * commit. Pass nullptr to unregister the current callback.
128 * @param [optional] aData if provided, will be passed to the callback.
129 * @see http://sqlite.org/c3ref/commit_hook.html
131 void setCommitHook(int (*aCallbackFn
)(void *) , void *aData
=nullptr) {
132 MOZ_ASSERT(mDBConn
, "A connection must exist at this point");
133 ::sqlite3_commit_hook(mDBConn
, aCallbackFn
, aData
);
137 * Gets autocommit status.
139 bool getAutocommit() {
140 return mDBConn
&& static_cast<bool>(::sqlite3_get_autocommit(mDBConn
));
144 * Lazily creates and returns a background execution thread. In the future,
145 * the thread may be re-claimed if left idle, so you should call this
146 * method just before you dispatch and not save the reference.
148 * This must be called from the opener thread.
150 * @return an event target suitable for asynchronous statement execution.
151 * @note This method will return null once AsyncClose() has been called.
153 nsIEventTarget
*getAsyncExecutionTarget();
156 * Mutex used by asynchronous statements to protect state. The mutex is
157 * declared on the connection object because there is no contention between
158 * asynchronous statements (they are serialized on mAsyncExecutionThread).
159 * Currently protects:
160 * - Connection.mAsyncExecutionThreadShuttingDown
161 * - Connection.mConnectionClosed
162 * - AsyncExecuteStatements.mCancelRequested
164 Mutex sharedAsyncExecutionMutex
;
167 * Wraps the mutex that SQLite gives us from sqlite3_db_mutex. This is public
168 * because we already expose the sqlite3* native connection and proper
169 * operation of the deadlock detector requires everyone to use the same single
170 * SQLiteMutex instance for correctness.
172 SQLiteMutex sharedDBMutex
;
175 * References the thread this database was opened on. This MUST be thread it is
178 const nsCOMPtr
<nsIThread
> threadOpenedOn
;
181 * Closes the SQLite database, and warns about any non-finalized statements.
183 nsresult
internalClose(sqlite3
*aDBConn
);
186 * Shuts down the passed-in async thread.
188 void shutdownAsyncThread();
191 * Obtains the filename of the connection. Useful for logging.
193 nsCString
getFilename();
196 * Creates an sqlite3 prepared statement object from an SQL string.
198 * @param aNativeConnection
199 * The underlying Sqlite connection to prepare the statement with.
201 * The SQL statement string to compile.
203 * New sqlite3_stmt object.
204 * @return the result from sqlite3_prepare_v2.
206 int prepareStatement(sqlite3
* aNativeConnection
,
207 const nsCString
&aSQL
, sqlite3_stmt
**_stmt
);
210 * Performs a sqlite3_step on aStatement, while properly handling SQLITE_LOCKED
211 * when not on the main thread by waiting until we are notified.
213 * @param aNativeConnection
214 * The underlying Sqlite connection to step the statement with.
216 * A pointer to a sqlite3_stmt object.
217 * @return the result from sqlite3_step.
219 int stepStatement(sqlite3
* aNativeConnection
, sqlite3_stmt
* aStatement
);
222 * Raw connection transaction management.
224 * @see BeginTransactionAs, CommitTransaction, RollbackTransaction.
226 nsresult
beginTransactionInternal(sqlite3
*aNativeConnection
,
227 int32_t aTransactionType
=TRANSACTION_DEFERRED
);
228 nsresult
commitTransactionInternal(sqlite3
*aNativeConnection
);
229 nsresult
rollbackTransactionInternal(sqlite3
*aNativeConnection
);
231 bool connectionReady();
234 * Thread-aware version of connectionReady, results per caller's thread are:
235 * - owner thread: Same as connectionReady(). True means we have a valid,
236 * un-closed database connection and it's not going away until you invoke
237 * Close() or AsyncClose().
238 * - async thread: Returns true at all times because you can't schedule
239 * runnables against the async thread after AsyncClose() has been called.
240 * Therefore, the connection is still around if your code is running.
241 * - any other thread: Race-prone Lies! If you are main-thread code in
242 * mozStorageService iterating over the list of connections, you need to
243 * acquire the sharedAsyncExecutionMutex for the connection, invoke
244 * connectionReady() while holding it, and then continue to hold it while
245 * you do whatever you need to do. This is because of off-main-thread
246 * consumers like dom/cache and IndexedDB and other QuotaManager clients.
248 bool isConnectionReadyOnThisThread();
251 * True if this connection has inited shutdown.
256 * True if the underlying connection is closed.
257 * Any sqlite resources may be lost when this returns true, so nothing should
259 * This locks on sharedAsyncExecutionMutex.
264 * Same as isClosed(), but takes a proof-of-lock instead of locking internally.
266 bool isClosed(MutexAutoLock
& lock
);
269 * True if the async execution thread is alive and able to be used (i.e., it
270 * is not in the process of shutting down.)
272 * This must be called from the opener thread.
274 bool isAsyncExecutionThreadAvailable();
276 nsresult
initializeClone(Connection
*aClone
, bool aReadOnly
);
280 nsresult
initializeInternal();
281 void initializeFailed();
284 * Sets the database into a closed state so no further actions can be
287 * @note mDBConn is set to nullptr in this method.
289 nsresult
setClosedState();
292 * Helper for calls to sqlite3_exec. Reports long delays to Telemetry.
294 * @param aNativeConnection
295 * The underlying Sqlite connection to execute the query with.
297 * SQL string to execute
298 * @return the result from sqlite3_exec.
300 int executeSql(sqlite3
*aNativeConnection
, const char *aSqlString
);
303 * Describes a certain primitive type in the database.
305 * Possible Values Are:
306 * INDEX - To check for the existence of an index
307 * TABLE - To check for the existence of a table
309 enum DatabaseElementType
{
315 * Determines if the specified primitive exists.
317 * @param aElementType
318 * The type of element to check the existence of
319 * @param aElementName
320 * The name of the element to check for
321 * @returns true if element exists, false otherwise
323 nsresult
databaseElementExists(enum DatabaseElementType aElementType
,
324 const nsACString
& aElementName
,
327 bool findFunctionByInstance(nsISupports
*aInstance
);
329 static int sProgressHelper(void *aArg
);
330 // Generic progress handler
331 // Dispatch call to registered progress handler,
332 // if there is one. Do nothing in other cases.
333 int progressHandler();
336 nsCOMPtr
<nsIFileURL
> mFileURL
;
337 nsCOMPtr
<nsIFile
> mDatabaseFile
;
340 * The filename that will be reported to telemetry for this connection. By
341 * default this will be the leaf of the path to the database file.
343 nsCString mTelemetryFilename
;
346 * Lazily created thread for asynchronous statement execution. Consumers
347 * should use getAsyncExecutionTarget rather than directly accessing this
350 * This must be modified only on the opener thread.
352 nsCOMPtr
<nsIThread
> mAsyncExecutionThread
;
355 * Set to true by Close() or AsyncClose() prior to shutdown.
357 * If false, we guarantee both that the underlying sqlite3 database
358 * connection is still open and that getAsyncExecutionTarget() can
359 * return a thread. Once true, either the sqlite3 database
360 * connection is being shutdown or it has been
361 * shutdown. Additionally, once true, getAsyncExecutionTarget()
364 * This variable should be accessed while holding the
365 * sharedAsyncExecutionMutex.
367 bool mAsyncExecutionThreadShuttingDown
;
370 * Set to true just prior to calling sqlite3_close on the
373 * This variable should be accessed while holding the
374 * sharedAsyncExecutionMutex.
376 bool mConnectionClosed
;
379 * Stores the default behavior for all transactions run on this connection.
381 mozilla::Atomic
<int32_t> mDefaultTransactionType
;
384 * Tracks if we have a transaction in progress or not. Access protected by
387 bool mTransactionInProgress
;
390 * Used to trigger cleanup logic only the first time our refcount hits 1. We
391 * may trigger a failsafe Close() that invokes SpinningSynchronousClose()
392 * which invokes AsyncClose() which may bump our refcount back up to 2 (and
393 * which will then fall back down to 1 again). It's also possible that the
394 * Service may bump our refcount back above 1 if getConnections() runs before
395 * we invoke unregisterConnection().
397 mozilla::Atomic
<bool> mDestroying
;
400 * Stores the mapping of a given function by name to its instance. Access is
401 * protected by sharedDBMutex.
403 nsDataHashtable
<nsCStringHashKey
, FunctionInfo
> mFunctions
;
406 * Stores the registered progress handler for the database connection. Access
407 * is protected by sharedDBMutex.
409 nsCOMPtr
<mozIStorageProgressHandler
> mProgressHandler
;
412 * Stores the flags we passed to sqlite3_open_v2.
417 * Stores whether we should ask sqlite3_open_v2 to ignore locking.
419 const bool mIgnoreLockingMode
;
421 // This is here for two reasons: 1) It's used to make sure that the
422 // connections do not outlive the service. 2) Our custom collating functions
423 // call its localeCompareStrings() method.
424 RefPtr
<Service
> mStorageService
;
427 * If |false|, this instance supports synchronous operations
428 * and it can be cast to |mozIStorageConnection|.
430 const bool mAsyncOnly
;
435 * A Runnable designed to call a mozIStorageCompletionCallback on
436 * the appropriate thread.
438 class CallbackComplete final
: public Runnable
442 * @param aValue The result to pass to the callback. It must
443 * already be owned by the main thread.
444 * @param aCallback The callback. It must already be owned by the
447 CallbackComplete(nsresult aStatus
,
449 already_AddRefed
<mozIStorageCompletionCallback
> aCallback
)
450 : Runnable("storage::CallbackComplete")
453 , mCallback(aCallback
)
457 NS_IMETHOD
Run() override
{
458 MOZ_ASSERT(NS_IsMainThread());
459 nsresult rv
= mCallback
->Complete(mStatus
, mValue
);
461 // Ensure that we release on the main thread
469 nsCOMPtr
<nsISupports
> mValue
;
470 // This is a RefPtr<T> and not a nsCOMPtr<T> because
471 // nsCOMP<T> would cause an off-main thread QI, which
472 // is not a good idea (and crashes XPConnect).
473 RefPtr
<mozIStorageCompletionCallback
> mCallback
;
476 } // namespace storage
477 } // namespace mozilla
480 * Casting Connection to nsISupports is ambiguous.
481 * This method handles that.
484 ToSupports(mozilla::storage::Connection
* p
)
486 return NS_ISUPPORTS_CAST(mozIStorageAsyncConnection
*, p
);
489 #endif // mozilla_storage_Connection_h