2009-04-18 01:19:31 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
* vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2010-10-06 02:01:22 +04:00
|
|
|
#ifndef mozilla_storage_Connection_h
|
|
|
|
#define mozilla_storage_Connection_h
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2004-10-09 18:33:01 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2018-02-26 07:50:42 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2009-09-03 00:30:24 +04:00
|
|
|
#include "mozilla/Mutex.h"
|
2013-06-27 17:00:59 +04:00
|
|
|
#include "nsProxyRelease.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2010-10-06 02:01:22 +04:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2004-10-09 18:33:01 +04:00
|
|
|
|
2010-08-27 23:42:58 +04:00
|
|
|
#include "nsDataHashtable.h"
|
2007-07-08 01:14:51 +04:00
|
|
|
#include "mozIStorageProgressHandler.h"
|
2009-07-23 02:18:33 +04:00
|
|
|
#include "SQLiteMutex.h"
|
2004-10-09 04:04:10 +04:00
|
|
|
#include "mozIStorageConnection.h"
|
2009-07-15 21:49:05 +04:00
|
|
|
#include "mozStorageService.h"
|
2013-06-27 17:00:59 +04:00
|
|
|
#include "mozIStorageAsyncConnection.h"
|
|
|
|
#include "mozIStorageCompletionCallback.h"
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2012-06-13 07:35:10 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-06-29 09:42:56 +04:00
|
|
|
#include "sqlite3.h"
|
2004-10-09 04:04:10 +04:00
|
|
|
|
|
|
|
class nsIFile;
|
2012-12-17 23:25:10 +04:00
|
|
|
class nsIFileURL;
|
2008-10-29 06:53:19 +03:00
|
|
|
class nsIEventTarget;
|
|
|
|
class nsIThread;
|
2004-10-09 04:04:10 +04:00
|
|
|
|
2009-04-18 01:19:31 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace storage {
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class Connection final : public mozIStorageConnection,
|
2015-03-27 21:52:19 +03:00
|
|
|
public nsIInterfaceRequestor {
|
2004-10-09 04:04:10 +04:00
|
|
|
public:
|
2013-07-19 06:24:15 +04:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-06-27 17:00:59 +04:00
|
|
|
NS_DECL_MOZISTORAGEASYNCCONNECTION
|
2009-04-18 01:19:31 +04:00
|
|
|
NS_DECL_MOZISTORAGECONNECTION
|
2010-10-06 02:01:22 +04:00
|
|
|
NS_DECL_NSIINTERFACEREQUESTOR
|
2009-04-18 01:19:31 +04:00
|
|
|
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
/**
|
|
|
|
* Indicates if a database operation is synchronous or asynchronous.
|
|
|
|
*
|
|
|
|
* - Async operations may be called from any thread for all connections.
|
|
|
|
* - Sync operations may be called from any thread for sync connections, and
|
|
|
|
* from background threads for async connections.
|
|
|
|
*/
|
|
|
|
enum ConnectionOperation { ASYNCHRONOUS, SYNCHRONOUS };
|
|
|
|
|
2010-08-27 23:42:58 +04:00
|
|
|
/**
|
|
|
|
* Structure used to describe user functions on the database connection.
|
|
|
|
*/
|
|
|
|
struct FunctionInfo {
|
2020-06-08 13:00:24 +03:00
|
|
|
nsCOMPtr<mozIStorageFunction> function;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t numArgs;
|
2010-08-27 23:42:58 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param aService
|
|
|
|
* Pointer to the storage service. Held onto for the lifetime of the
|
|
|
|
* connection.
|
|
|
|
* @param aFlags
|
|
|
|
* The flags to pass to sqlite3_open_v2.
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
* @param aSupportedOperations
|
|
|
|
* The operation types supported on this connection. All connections
|
|
|
|
* implement both the async (`mozIStorageAsyncConnection`) and sync
|
|
|
|
* (`mozIStorageConnection`) interfaces, but async connections may not
|
|
|
|
* call sync operations from the main thread.
|
2016-07-18 18:46:45 +03:00
|
|
|
* @param aIgnoreLockingMode
|
|
|
|
* If |true|, ignore locks in force on the file. Only usable with
|
|
|
|
* read-only connections. Defaults to false.
|
|
|
|
* Use with extreme caution. If sqlite ignores locks, reads may fail
|
|
|
|
* indicating database corruption (the database won't actually be
|
|
|
|
* corrupt) or produce wrong results without any indication that has
|
|
|
|
* happened.
|
2010-08-27 23:42:58 +04:00
|
|
|
*/
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
Connection(Service* aService, int aFlags,
|
|
|
|
ConnectionOperation aSupportedOperations,
|
2016-07-18 18:46:45 +03:00
|
|
|
bool aIgnoreLockingMode = false);
|
2009-04-18 01:19:31 +04:00
|
|
|
|
2012-12-17 23:25:10 +04:00
|
|
|
/**
|
|
|
|
* Creates the connection to an in-memory database.
|
|
|
|
*/
|
|
|
|
nsresult initialize();
|
|
|
|
|
2009-04-18 01:19:31 +04:00
|
|
|
/**
|
|
|
|
* Creates the connection to the database.
|
|
|
|
*
|
|
|
|
* @param aDatabaseFile
|
|
|
|
* The nsIFile of the location of the database to open, or create if it
|
2012-12-17 23:25:10 +04:00
|
|
|
* does not exist.
|
2009-04-18 01:19:31 +04:00
|
|
|
*/
|
2012-12-17 23:25:10 +04:00
|
|
|
nsresult initialize(nsIFile* aDatabaseFile);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the connection to the database.
|
|
|
|
*
|
|
|
|
* @param aFileURL
|
|
|
|
* The nsIFileURL of the location of the database to open, or create if
|
|
|
|
* it does not exist.
|
|
|
|
*/
|
2020-08-11 23:55:33 +03:00
|
|
|
nsresult initialize(nsIFileURL* aFileURL,
|
|
|
|
const nsACString& aTelemetryFilename);
|
2009-04-18 01:19:31 +04:00
|
|
|
|
2017-06-16 18:43:23 +03:00
|
|
|
/**
|
|
|
|
* Same as initialize, but to be used on the async thread.
|
|
|
|
*/
|
|
|
|
nsresult initializeOnAsyncThread(nsIFile* aStorageFile);
|
|
|
|
|
2014-04-24 13:54:09 +04:00
|
|
|
/**
|
|
|
|
* Fetches runtime status information for this connection.
|
|
|
|
*
|
|
|
|
* @param aStatusOption One of the SQLITE_DBSTATUS options defined at
|
|
|
|
* http://www.sqlite.org/c3ref/c_dbstatus_options.html
|
|
|
|
* @param [optional] aMaxValue if provided, will be set to the highest
|
|
|
|
* istantaneous value.
|
|
|
|
* @return the current value for the specified option.
|
|
|
|
*/
|
|
|
|
int32_t getSqliteRuntimeStatus(int32_t aStatusOption,
|
|
|
|
int32_t* aMaxValue = nullptr);
|
|
|
|
/**
|
|
|
|
* Registers/unregisters a commit hook callback.
|
|
|
|
*
|
|
|
|
* @param aCallbackFn a callback function to be invoked on transactions
|
|
|
|
* commit. Pass nullptr to unregister the current callback.
|
|
|
|
* @param [optional] aData if provided, will be passed to the callback.
|
|
|
|
* @see http://sqlite.org/c3ref/commit_hook.html
|
|
|
|
*/
|
|
|
|
void setCommitHook(int (*aCallbackFn)(void*), void* aData = nullptr) {
|
|
|
|
MOZ_ASSERT(mDBConn, "A connection must exist at this point");
|
|
|
|
::sqlite3_commit_hook(mDBConn, aCallbackFn, aData);
|
|
|
|
};
|
2009-04-18 01:19:31 +04:00
|
|
|
|
|
|
|
/**
|
2014-09-10 14:46:14 +04:00
|
|
|
* Gets autocommit status.
|
|
|
|
*/
|
|
|
|
bool getAutocommit() {
|
2015-04-10 14:16:00 +03:00
|
|
|
return mDBConn && static_cast<bool>(::sqlite3_get_autocommit(mDBConn));
|
2014-09-10 14:46:14 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2009-04-18 01:19:31 +04:00
|
|
|
* Lazily creates and returns a background execution thread. In the future,
|
|
|
|
* the thread may be re-claimed if left idle, so you should call this
|
|
|
|
* method just before you dispatch and not save the reference.
|
2009-06-17 23:12:43 +04:00
|
|
|
*
|
2017-06-16 18:43:23 +03:00
|
|
|
* This must be called from the opener thread.
|
2017-06-02 00:46:53 +03:00
|
|
|
*
|
2017-06-16 18:43:23 +03:00
|
|
|
* @return an event target suitable for asynchronous statement execution.
|
|
|
|
* @note This method will return null once AsyncClose() has been called.
|
2009-04-18 01:19:31 +04:00
|
|
|
*/
|
2010-03-24 10:32:40 +03:00
|
|
|
nsIEventTarget* getAsyncExecutionTarget();
|
2008-10-29 06:53:19 +03:00
|
|
|
|
2009-09-03 00:30:24 +04:00
|
|
|
/**
|
|
|
|
* Mutex used by asynchronous statements to protect state. The mutex is
|
|
|
|
* declared on the connection object because there is no contention between
|
2014-04-24 13:54:12 +04:00
|
|
|
* asynchronous statements (they are serialized on mAsyncExecutionThread).
|
|
|
|
* Currently protects:
|
|
|
|
* - Connection.mAsyncExecutionThreadShuttingDown
|
|
|
|
* - Connection.mConnectionClosed
|
|
|
|
* - AsyncExecuteStatements.mCancelRequested
|
2009-09-03 00:30:24 +04:00
|
|
|
*/
|
|
|
|
Mutex sharedAsyncExecutionMutex;
|
|
|
|
|
2010-03-24 10:32:40 +03:00
|
|
|
/**
|
|
|
|
* Wraps the mutex that SQLite gives us from sqlite3_db_mutex. This is public
|
|
|
|
* because we already expose the sqlite3* native connection and proper
|
|
|
|
* operation of the deadlock detector requires everyone to use the same single
|
|
|
|
* SQLiteMutex instance for correctness.
|
|
|
|
*/
|
|
|
|
SQLiteMutex sharedDBMutex;
|
|
|
|
|
2009-12-02 21:53:46 +03:00
|
|
|
/**
|
2009-11-09 20:58:34 +03:00
|
|
|
* References the thread this database was opened on. This MUST be thread it
|
|
|
|
* is closed on.
|
2009-12-02 21:53:46 +03:00
|
|
|
*/
|
|
|
|
const nsCOMPtr<nsIThread> threadOpenedOn;
|
|
|
|
|
2009-11-09 20:58:34 +03:00
|
|
|
/**
|
|
|
|
* Closes the SQLite database, and warns about any non-finalized statements.
|
|
|
|
*/
|
2014-04-24 13:54:12 +04:00
|
|
|
nsresult internalClose(sqlite3* aDBConn);
|
2009-11-09 20:58:34 +03:00
|
|
|
|
2015-04-14 17:11:54 +03:00
|
|
|
/**
|
|
|
|
* Shuts down the passed-in async thread.
|
|
|
|
*/
|
2017-06-16 18:43:23 +03:00
|
|
|
void shutdownAsyncThread();
|
2015-04-14 17:11:54 +03:00
|
|
|
|
2010-05-20 03:22:19 +04:00
|
|
|
/**
|
|
|
|
* Obtains the filename of the connection. Useful for logging.
|
|
|
|
*/
|
|
|
|
nsCString getFilename();
|
|
|
|
|
2011-12-07 00:12:55 +04:00
|
|
|
/**
|
|
|
|
* Creates an sqlite3 prepared statement object from an SQL string.
|
|
|
|
*
|
2014-04-24 13:54:12 +04:00
|
|
|
* @param aNativeConnection
|
|
|
|
* The underlying Sqlite connection to prepare the statement with.
|
2011-12-07 00:12:55 +04:00
|
|
|
* @param aSQL
|
|
|
|
* The SQL statement string to compile.
|
|
|
|
* @param _stmt
|
|
|
|
* New sqlite3_stmt object.
|
|
|
|
* @return the result from sqlite3_prepare_v2.
|
|
|
|
*/
|
2014-04-24 13:54:12 +04:00
|
|
|
int prepareStatement(sqlite3* aNativeConnection, const nsCString& aSQL,
|
|
|
|
sqlite3_stmt** _stmt);
|
2011-12-07 00:12:55 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a sqlite3_step on aStatement, while properly handling
|
|
|
|
* SQLITE_LOCKED when not on the main thread by waiting until we are notified.
|
|
|
|
*
|
2014-04-24 13:54:12 +04:00
|
|
|
* @param aNativeConnection
|
|
|
|
* The underlying Sqlite connection to step the statement with.
|
2011-12-07 00:12:55 +04:00
|
|
|
* @param aStatement
|
|
|
|
* A pointer to a sqlite3_stmt object.
|
|
|
|
* @return the result from sqlite3_step.
|
|
|
|
*/
|
2014-04-24 13:54:12 +04:00
|
|
|
int stepStatement(sqlite3* aNativeConnection, sqlite3_stmt* aStatement);
|
2011-12-07 00:12:55 +04:00
|
|
|
|
2014-04-24 13:54:15 +04:00
|
|
|
/**
|
|
|
|
* Raw connection transaction management.
|
|
|
|
*
|
|
|
|
* @see BeginTransactionAs, CommitTransaction, RollbackTransaction.
|
|
|
|
*/
|
|
|
|
nsresult beginTransactionInternal(
|
Bug 1613835 - Use a SQLite API call to check for in-progress transactions. r=mak
Previously, `mozIStorageConnection#transactionInProgress` returned true
only if a transaction was started via `beginTransaction()`. This meant
that manually executing `BEGIN`, as `Sqlite.jsm` and the Rust bindings
do, wouldn't accurately report if a transaction was in progress.
Similarly, the flag wasn't accurate in cases where SQLite automatically
rolled back a transaction.
Fortunately, SQLite provides the `sqlite3_get_autocommit()` function,
which we can use to determine if a transaction is open or not. This
commit refactors the `transactionInProgress` getter, along with all
`Connection` methods that depend on it, to use the SQLite API instead
of managing that state on the connection. `mozStorageTransaction` and
`Sqlite.jsm` still use their own flags to decide whether to commit
their transactions, for reasons explained in the IDL comment.
This commit also moves `transactionInProgress` to
`mozIStorageAsyncConnection`, so that `Sqlite.jsm` can use it, and
exposes it to Rust.
Differential Revision: https://phabricator.services.mozilla.com/D63732
--HG--
extra : moz-landing-system : lando
2020-03-04 01:57:39 +03:00
|
|
|
const SQLiteMutexAutoLock& aProofOfLock, sqlite3* aNativeConnection,
|
2014-04-24 13:54:15 +04:00
|
|
|
int32_t aTransactionType = TRANSACTION_DEFERRED);
|
Bug 1613835 - Use a SQLite API call to check for in-progress transactions. r=mak
Previously, `mozIStorageConnection#transactionInProgress` returned true
only if a transaction was started via `beginTransaction()`. This meant
that manually executing `BEGIN`, as `Sqlite.jsm` and the Rust bindings
do, wouldn't accurately report if a transaction was in progress.
Similarly, the flag wasn't accurate in cases where SQLite automatically
rolled back a transaction.
Fortunately, SQLite provides the `sqlite3_get_autocommit()` function,
which we can use to determine if a transaction is open or not. This
commit refactors the `transactionInProgress` getter, along with all
`Connection` methods that depend on it, to use the SQLite API instead
of managing that state on the connection. `mozStorageTransaction` and
`Sqlite.jsm` still use their own flags to decide whether to commit
their transactions, for reasons explained in the IDL comment.
This commit also moves `transactionInProgress` to
`mozIStorageAsyncConnection`, so that `Sqlite.jsm` can use it, and
exposes it to Rust.
Differential Revision: https://phabricator.services.mozilla.com/D63732
--HG--
extra : moz-landing-system : lando
2020-03-04 01:57:39 +03:00
|
|
|
nsresult commitTransactionInternal(const SQLiteMutexAutoLock& aProofOfLock,
|
|
|
|
sqlite3* aNativeConnection);
|
|
|
|
nsresult rollbackTransactionInternal(const SQLiteMutexAutoLock& aProofOfLock,
|
|
|
|
sqlite3* aNativeConnection);
|
2014-04-24 13:54:15 +04:00
|
|
|
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
/**
|
2019-04-02 21:49:21 +03:00
|
|
|
* Indicates if this database connection is open.
|
|
|
|
*/
|
|
|
|
inline bool connectionReady() { return mDBConn != nullptr; }
|
|
|
|
|
Bug 1613835 - Use a SQLite API call to check for in-progress transactions. r=mak
Previously, `mozIStorageConnection#transactionInProgress` returned true
only if a transaction was started via `beginTransaction()`. This meant
that manually executing `BEGIN`, as `Sqlite.jsm` and the Rust bindings
do, wouldn't accurately report if a transaction was in progress.
Similarly, the flag wasn't accurate in cases where SQLite automatically
rolled back a transaction.
Fortunately, SQLite provides the `sqlite3_get_autocommit()` function,
which we can use to determine if a transaction is open or not. This
commit refactors the `transactionInProgress` getter, along with all
`Connection` methods that depend on it, to use the SQLite API instead
of managing that state on the connection. `mozStorageTransaction` and
`Sqlite.jsm` still use their own flags to decide whether to commit
their transactions, for reasons explained in the IDL comment.
This commit also moves `transactionInProgress` to
`mozIStorageAsyncConnection`, so that `Sqlite.jsm` can use it, and
exposes it to Rust.
Differential Revision: https://phabricator.services.mozilla.com/D63732
--HG--
extra : moz-landing-system : lando
2020-03-04 01:57:39 +03:00
|
|
|
/**
|
|
|
|
* Indicates if this database connection has an open transaction. Because
|
|
|
|
* multiple threads can execute statements on the same connection, this method
|
|
|
|
* requires proof that the caller is holding `sharedDBMutex`.
|
|
|
|
*
|
|
|
|
* Per the SQLite docs, `sqlite3_get_autocommit` returns 0 if autocommit mode
|
|
|
|
* is disabled. `BEGIN` disables autocommit mode, and `COMMIT`, `ROLLBACK`, or
|
|
|
|
* an automatic rollback re-enables it.
|
|
|
|
*/
|
|
|
|
inline bool transactionInProgress(const SQLiteMutexAutoLock& aProofOfLock) {
|
|
|
|
return !getAutocommit();
|
|
|
|
}
|
|
|
|
|
2019-04-02 21:49:21 +03:00
|
|
|
/**
|
|
|
|
* Indicates if this database connection supports the given operation.
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
*
|
|
|
|
* @param aOperationType
|
|
|
|
* The operation type, sync or async.
|
2019-04-02 21:49:21 +03:00
|
|
|
* @return `true` if the operation is supported, `false` otherwise.
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
*/
|
2019-04-02 21:49:21 +03:00
|
|
|
bool operationSupported(ConnectionOperation aOperationType);
|
2012-05-09 18:32:14 +04:00
|
|
|
|
2012-05-08 22:58:56 +04:00
|
|
|
/**
|
2017-06-16 18:43:23 +03:00
|
|
|
* Thread-aware version of connectionReady, results per caller's thread are:
|
|
|
|
* - owner thread: Same as connectionReady(). True means we have a valid,
|
|
|
|
* un-closed database connection and it's not going away until you invoke
|
|
|
|
* Close() or AsyncClose().
|
|
|
|
* - async thread: Returns true at all times because you can't schedule
|
|
|
|
* runnables against the async thread after AsyncClose() has been called.
|
|
|
|
* Therefore, the connection is still around if your code is running.
|
|
|
|
* - any other thread: Race-prone Lies! If you are main-thread code in
|
|
|
|
* mozStorageService iterating over the list of connections, you need to
|
|
|
|
* acquire the sharedAsyncExecutionMutex for the connection, invoke
|
|
|
|
* connectionReady() while holding it, and then continue to hold it while
|
|
|
|
* you do whatever you need to do. This is because of off-main-thread
|
|
|
|
* consumers like dom/cache and IndexedDB and other QuotaManager clients.
|
|
|
|
*/
|
|
|
|
bool isConnectionReadyOnThisThread();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if this connection has inited shutdown.
|
2014-04-24 13:54:12 +04:00
|
|
|
*/
|
|
|
|
bool isClosing();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the underlying connection is closed.
|
|
|
|
* Any sqlite resources may be lost when this returns true, so nothing should
|
|
|
|
* try to use them.
|
2017-06-16 18:43:23 +03:00
|
|
|
* This locks on sharedAsyncExecutionMutex.
|
2012-05-08 22:58:56 +04:00
|
|
|
*/
|
2014-04-24 13:54:12 +04:00
|
|
|
bool isClosed();
|
2013-06-27 17:00:59 +04:00
|
|
|
|
2017-06-02 00:46:53 +03:00
|
|
|
/**
|
2017-06-16 18:43:23 +03:00
|
|
|
* Same as isClosed(), but takes a proof-of-lock instead of locking
|
|
|
|
* internally.
|
|
|
|
*/
|
|
|
|
bool isClosed(MutexAutoLock& lock);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* True if the async execution thread is alive and able to be used (i.e., it
|
|
|
|
* is not in the process of shutting down.)
|
|
|
|
*
|
|
|
|
* This must be called from the opener thread.
|
|
|
|
*/
|
2017-06-02 00:46:53 +03:00
|
|
|
bool isAsyncExecutionThreadAvailable();
|
|
|
|
|
2013-06-27 17:00:59 +04:00
|
|
|
nsresult initializeClone(Connection* aClone, bool aReadOnly);
|
|
|
|
|
2020-08-11 23:55:33 +03:00
|
|
|
/**
|
|
|
|
* Records a status from a sqlite statement.
|
|
|
|
*
|
|
|
|
* @param srv The sqlite result for the failure or SQLITE_OK.
|
|
|
|
*/
|
|
|
|
void RecordQueryStatus(int srv);
|
|
|
|
|
2004-10-09 04:04:10 +04:00
|
|
|
private:
|
2009-04-18 01:19:31 +04:00
|
|
|
~Connection();
|
2015-05-19 19:17:10 +03:00
|
|
|
nsresult initializeInternal();
|
2017-12-07 09:34:18 +03:00
|
|
|
void initializeFailed();
|
2012-12-17 23:25:10 +04:00
|
|
|
|
2020-08-11 23:55:33 +03:00
|
|
|
/**
|
|
|
|
* Records the status of an attempt to load a sqlite database to telemetry.
|
|
|
|
*
|
|
|
|
* @param rv The state of the load, success or failure.
|
|
|
|
*/
|
|
|
|
void RecordOpenStatus(nsresult rv);
|
|
|
|
|
2009-11-09 20:58:34 +03:00
|
|
|
/**
|
|
|
|
* Sets the database into a closed state so no further actions can be
|
|
|
|
* performed.
|
|
|
|
*
|
2013-07-31 19:44:58 +04:00
|
|
|
* @note mDBConn is set to nullptr in this method.
|
2009-11-09 20:58:34 +03:00
|
|
|
*/
|
|
|
|
nsresult setClosedState();
|
|
|
|
|
2012-03-21 21:26:48 +04:00
|
|
|
/**
|
|
|
|
* Helper for calls to sqlite3_exec. Reports long delays to Telemetry.
|
|
|
|
*
|
2014-04-24 13:54:15 +04:00
|
|
|
* @param aNativeConnection
|
|
|
|
* The underlying Sqlite connection to execute the query with.
|
2012-03-21 21:26:48 +04:00
|
|
|
* @param aSqlString
|
|
|
|
* SQL string to execute
|
|
|
|
* @return the result from sqlite3_exec.
|
|
|
|
*/
|
2014-04-24 13:54:15 +04:00
|
|
|
int executeSql(sqlite3* aNativeConnection, const char* aSqlString);
|
2012-03-21 21:26:48 +04:00
|
|
|
|
2009-04-18 01:19:31 +04:00
|
|
|
/**
|
|
|
|
* Describes a certain primitive type in the database.
|
|
|
|
*
|
|
|
|
* Possible Values Are:
|
|
|
|
* INDEX - To check for the existence of an index
|
|
|
|
* TABLE - To check for the existence of a table
|
|
|
|
*/
|
|
|
|
enum DatabaseElementType { INDEX, TABLE };
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines if the specified primitive exists.
|
|
|
|
*
|
|
|
|
* @param aElementType
|
|
|
|
* The type of element to check the existence of
|
|
|
|
* @param aElementName
|
|
|
|
* The name of the element to check for
|
|
|
|
* @returns true if element exists, false otherwise
|
|
|
|
*/
|
|
|
|
nsresult databaseElementExists(enum DatabaseElementType aElementType,
|
|
|
|
const nsACString& aElementName, bool* _exists);
|
|
|
|
|
2020-06-08 13:00:24 +03:00
|
|
|
bool findFunctionByInstance(mozIStorageFunction* aInstance);
|
2009-04-18 01:19:31 +04:00
|
|
|
|
|
|
|
static int sProgressHelper(void* aArg);
|
|
|
|
// Generic progress handler
|
|
|
|
// Dispatch call to registered progress handler,
|
|
|
|
// if there is one. Do nothing in other cases.
|
|
|
|
int progressHandler();
|
|
|
|
|
2019-04-02 21:49:21 +03:00
|
|
|
/**
|
|
|
|
* Like `operationSupported`, but throws (and, in a debug build, asserts) if
|
|
|
|
* the operation is unsupported.
|
|
|
|
*/
|
|
|
|
nsresult ensureOperationSupported(ConnectionOperation aOperationType);
|
|
|
|
|
2009-04-18 01:19:31 +04:00
|
|
|
sqlite3* mDBConn;
|
2012-12-17 23:25:10 +04:00
|
|
|
nsCOMPtr<nsIFileURL> mFileURL;
|
2009-04-18 01:19:31 +04:00
|
|
|
nsCOMPtr<nsIFile> mDatabaseFile;
|
|
|
|
|
2015-05-19 19:17:10 +03:00
|
|
|
/**
|
|
|
|
* The filename that will be reported to telemetry for this connection. By
|
|
|
|
* default this will be the leaf of the path to the database file.
|
|
|
|
*/
|
|
|
|
nsCString mTelemetryFilename;
|
|
|
|
|
2009-04-18 01:19:31 +04:00
|
|
|
/**
|
|
|
|
* Lazily created thread for asynchronous statement execution. Consumers
|
|
|
|
* should use getAsyncExecutionTarget rather than directly accessing this
|
|
|
|
* field.
|
2017-06-02 00:46:53 +03:00
|
|
|
*
|
2017-06-16 18:43:23 +03:00
|
|
|
* This must be modified only on the opener thread.
|
2009-04-18 01:19:31 +04:00
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIThread> mAsyncExecutionThread;
|
2013-09-04 20:51:45 +04:00
|
|
|
|
2009-04-18 01:19:31 +04:00
|
|
|
/**
|
2013-09-04 20:51:45 +04:00
|
|
|
* Set to true by Close() or AsyncClose() prior to shutdown.
|
|
|
|
*
|
|
|
|
* If false, we guarantee both that the underlying sqlite3 database
|
|
|
|
* connection is still open and that getAsyncExecutionTarget() can
|
|
|
|
* return a thread. Once true, either the sqlite3 database
|
|
|
|
* connection is being shutdown or it has been
|
|
|
|
* shutdown. Additionally, once true, getAsyncExecutionTarget()
|
|
|
|
* returns null.
|
|
|
|
*
|
|
|
|
* This variable should be accessed while holding the
|
2014-04-24 13:54:12 +04:00
|
|
|
* sharedAsyncExecutionMutex.
|
2009-04-18 01:19:31 +04:00
|
|
|
*/
|
2009-11-09 20:58:34 +03:00
|
|
|
bool mAsyncExecutionThreadShuttingDown;
|
2009-04-18 01:19:31 +04:00
|
|
|
|
2014-04-24 13:54:12 +04:00
|
|
|
/**
|
|
|
|
* Set to true just prior to calling sqlite3_close on the
|
|
|
|
* connection.
|
|
|
|
*
|
|
|
|
* This variable should be accessed while holding the
|
|
|
|
* sharedAsyncExecutionMutex.
|
|
|
|
*/
|
|
|
|
bool mConnectionClosed;
|
|
|
|
|
2009-07-23 02:18:33 +04:00
|
|
|
/**
|
2018-03-01 09:44:40 +03:00
|
|
|
* Stores the default behavior for all transactions run on this connection.
|
|
|
|
*/
|
|
|
|
mozilla::Atomic<int32_t> mDefaultTransactionType;
|
|
|
|
|
2018-02-26 07:50:42 +03:00
|
|
|
/**
|
|
|
|
* Used to trigger cleanup logic only the first time our refcount hits 1. We
|
|
|
|
* may trigger a failsafe Close() that invokes SpinningSynchronousClose()
|
|
|
|
* which invokes AsyncClose() which may bump our refcount back up to 2 (and
|
|
|
|
* which will then fall back down to 1 again). It's also possible that the
|
|
|
|
* Service may bump our refcount back above 1 if getConnections() runs before
|
|
|
|
* we invoke unregisterConnection().
|
|
|
|
*/
|
|
|
|
mozilla::Atomic<bool> mDestroying;
|
|
|
|
|
2009-07-23 02:18:33 +04:00
|
|
|
/**
|
|
|
|
* Stores the mapping of a given function by name to its instance. Access is
|
2014-04-24 13:54:09 +04:00
|
|
|
* protected by sharedDBMutex.
|
2009-07-23 02:18:33 +04:00
|
|
|
*/
|
2010-08-27 23:42:58 +04:00
|
|
|
nsDataHashtable<nsCStringHashKey, FunctionInfo> mFunctions;
|
2009-04-18 01:19:31 +04:00
|
|
|
|
2009-07-23 02:18:33 +04:00
|
|
|
/**
|
|
|
|
* Stores the registered progress handler for the database connection. Access
|
2014-04-24 13:54:09 +04:00
|
|
|
* is protected by sharedDBMutex.
|
2009-07-23 02:18:33 +04:00
|
|
|
*/
|
2009-04-18 01:19:31 +04:00
|
|
|
nsCOMPtr<mozIStorageProgressHandler> mProgressHandler;
|
|
|
|
|
2010-08-27 23:42:58 +04:00
|
|
|
/**
|
|
|
|
* Stores the flags we passed to sqlite3_open_v2.
|
|
|
|
*/
|
|
|
|
const int mFlags;
|
|
|
|
|
2016-07-18 18:46:45 +03:00
|
|
|
/**
|
|
|
|
* Stores whether we should ask sqlite3_open_v2 to ignore locking.
|
|
|
|
*/
|
|
|
|
const bool mIgnoreLockingMode;
|
|
|
|
|
2009-07-15 21:49:05 +04:00
|
|
|
// This is here for two reasons: 1) It's used to make sure that the
|
|
|
|
// connections do not outlive the service. 2) Our custom collating functions
|
|
|
|
// call its localeCompareStrings() method.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Service> mStorageService;
|
2013-06-27 17:00:59 +04:00
|
|
|
|
|
|
|
/**
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
* Indicates which operations are supported on this connection.
|
2013-06-27 17:00:59 +04:00
|
|
|
*/
|
Bug 1482608 - Add basic Rust bindings for mozStorage. r=nika,asuth,mak
This commit wraps just enough of the mozStorage API to support the
bookmarks mirror. It's not complete: for example, there's no way
to open, clone, or close a connection, because the mirror handles
that from JS. The wrapper also omits shutdown blocking and retrying on
`SQLITE_BUSY`.
This commit also changes the behavior of sync and async mozStorage
connections. Async (`mozIStorageAsyncConnection`) methods may be called
from any thread on any connection. Sync (`mozIStorageConnection`)
methods may be called from any thread on a sync connection, and from
background threads on an async connection. All connections now QI
to `mozIStorageConnection`, but attempting to call a sync method on
an async connection from the main thread throws.
Finally, this commit exposes an `OpenedConnection::unsafeRawConnection`
getter in Sqlite.jsm, for JS code to access the underlying connection.
Differential Revision: https://phabricator.services.mozilla.com/D20073
--HG--
extra : moz-landing-system : lando
2019-03-25 07:49:18 +03:00
|
|
|
const ConnectionOperation mSupportedOperations;
|
|
|
|
|
|
|
|
nsresult synchronousClose();
|
2013-06-27 17:00:59 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A Runnable designed to call a mozIStorageCompletionCallback on
|
|
|
|
* the appropriate thread.
|
|
|
|
*/
|
2016-04-26 03:23:21 +03:00
|
|
|
class CallbackComplete final : public Runnable {
|
2013-06-27 17:00:59 +04:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @param aValue The result to pass to the callback. It must
|
|
|
|
* already be owned by the main thread.
|
|
|
|
* @param aCallback The callback. It must already be owned by the
|
|
|
|
* main thread.
|
|
|
|
*/
|
|
|
|
CallbackComplete(nsresult aStatus, nsISupports* aValue,
|
|
|
|
already_AddRefed<mozIStorageCompletionCallback> aCallback)
|
2017-01-18 03:50:34 +03:00
|
|
|
: Runnable("storage::CallbackComplete"),
|
|
|
|
mStatus(aStatus),
|
2013-06-27 17:00:59 +04:00
|
|
|
mValue(aValue),
|
|
|
|
mCallback(aCallback) {}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2013-06-27 17:00:59 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
nsresult rv = mCallback->Complete(mStatus, mValue);
|
|
|
|
|
|
|
|
// Ensure that we release on the main thread
|
|
|
|
mValue = nullptr;
|
|
|
|
mCallback = nullptr;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsresult mStatus;
|
|
|
|
nsCOMPtr<nsISupports> mValue;
|
2015-10-18 08:24:48 +03:00
|
|
|
// This is a RefPtr<T> and not a nsCOMPtr<T> because
|
2013-06-27 17:00:59 +04:00
|
|
|
// nsCOMP<T> would cause an off-main thread QI, which
|
|
|
|
// is not a good idea (and crashes XPConnect).
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<mozIStorageCompletionCallback> mCallback;
|
2004-10-09 04:04:10 +04:00
|
|
|
};
|
|
|
|
|
2009-04-18 01:19:31 +04:00
|
|
|
} // namespace storage
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2016-06-30 01:56:41 +03:00
|
|
|
/**
|
|
|
|
* Casting Connection to nsISupports is ambiguous.
|
|
|
|
* This method handles that.
|
|
|
|
*/
|
|
|
|
inline nsISupports* ToSupports(mozilla::storage::Connection* p) {
|
|
|
|
return NS_ISUPPORTS_CAST(mozIStorageAsyncConnection*, p);
|
|
|
|
}
|
|
|
|
|
2010-10-06 02:01:22 +04:00
|
|
|
#endif // mozilla_storage_Connection_h
|