storage/src/mozStorageConnection.h

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef mozilla_storage_Connection_h
michael@0 8 #define mozilla_storage_Connection_h
michael@0 9
michael@0 10 #include "nsAutoPtr.h"
michael@0 11 #include "nsCOMPtr.h"
michael@0 12 #include "mozilla/Mutex.h"
michael@0 13 #include "nsProxyRelease.h"
michael@0 14 #include "nsThreadUtils.h"
michael@0 15 #include "nsIInterfaceRequestor.h"
michael@0 16
michael@0 17 #include "nsDataHashtable.h"
michael@0 18 #include "mozIStorageProgressHandler.h"
michael@0 19 #include "SQLiteMutex.h"
michael@0 20 #include "mozIStorageConnection.h"
michael@0 21 #include "mozStorageService.h"
michael@0 22 #include "mozIStorageAsyncConnection.h"
michael@0 23 #include "mozIStorageCompletionCallback.h"
michael@0 24
michael@0 25 #include "nsIMutableArray.h"
michael@0 26 #include "mozilla/Attributes.h"
michael@0 27
michael@0 28 #include "sqlite3.h"
michael@0 29
michael@0 30 struct PRLock;
michael@0 31 class nsIFile;
michael@0 32 class nsIFileURL;
michael@0 33 class nsIEventTarget;
michael@0 34 class nsIThread;
michael@0 35
michael@0 36 namespace mozilla {
michael@0 37 namespace storage {
michael@0 38
michael@0 39 class Connection MOZ_FINAL : public mozIStorageConnection
michael@0 40 , public nsIInterfaceRequestor
michael@0 41 {
michael@0 42 public:
michael@0 43 NS_DECL_THREADSAFE_ISUPPORTS
michael@0 44 NS_DECL_MOZISTORAGEASYNCCONNECTION
michael@0 45 NS_DECL_MOZISTORAGECONNECTION
michael@0 46 NS_DECL_NSIINTERFACEREQUESTOR
michael@0 47
michael@0 48 /**
michael@0 49 * Structure used to describe user functions on the database connection.
michael@0 50 */
michael@0 51 struct FunctionInfo {
michael@0 52 enum FunctionType {
michael@0 53 SIMPLE,
michael@0 54 AGGREGATE
michael@0 55 };
michael@0 56
michael@0 57 nsCOMPtr<nsISupports> function;
michael@0 58 FunctionType type;
michael@0 59 int32_t numArgs;
michael@0 60 };
michael@0 61
michael@0 62 /**
michael@0 63 * @param aService
michael@0 64 * Pointer to the storage service. Held onto for the lifetime of the
michael@0 65 * connection.
michael@0 66 * @param aFlags
michael@0 67 * The flags to pass to sqlite3_open_v2.
michael@0 68 * @param aAsyncOnly
michael@0 69 * If |true|, the Connection only implements asynchronous interface:
michael@0 70 * - |mozIStorageAsyncConnection|;
michael@0 71 * If |false|, the result also implements synchronous interface:
michael@0 72 * - |mozIStorageConnection|.
michael@0 73 */
michael@0 74 Connection(Service *aService, int aFlags, bool aAsyncOnly);
michael@0 75
michael@0 76 /**
michael@0 77 * Creates the connection to an in-memory database.
michael@0 78 */
michael@0 79 nsresult initialize();
michael@0 80
michael@0 81 /**
michael@0 82 * Creates the connection to the database.
michael@0 83 *
michael@0 84 * @param aDatabaseFile
michael@0 85 * The nsIFile of the location of the database to open, or create if it
michael@0 86 * does not exist.
michael@0 87 */
michael@0 88 nsresult initialize(nsIFile *aDatabaseFile);
michael@0 89
michael@0 90 /**
michael@0 91 * Creates the connection to the database.
michael@0 92 *
michael@0 93 * @param aFileURL
michael@0 94 * The nsIFileURL of the location of the database to open, or create if it
michael@0 95 * does not exist.
michael@0 96 */
michael@0 97 nsresult initialize(nsIFileURL *aFileURL);
michael@0 98
michael@0 99 /**
michael@0 100 * Fetches runtime status information for this connection.
michael@0 101 *
michael@0 102 * @param aStatusOption One of the SQLITE_DBSTATUS options defined at
michael@0 103 * http://www.sqlite.org/c3ref/c_dbstatus_options.html
michael@0 104 * @param [optional] aMaxValue if provided, will be set to the highest
michael@0 105 * istantaneous value.
michael@0 106 * @return the current value for the specified option.
michael@0 107 */
michael@0 108 int32_t getSqliteRuntimeStatus(int32_t aStatusOption,
michael@0 109 int32_t* aMaxValue=nullptr);
michael@0 110 /**
michael@0 111 * Registers/unregisters a commit hook callback.
michael@0 112 *
michael@0 113 * @param aCallbackFn a callback function to be invoked on transactions
michael@0 114 * commit. Pass nullptr to unregister the current callback.
michael@0 115 * @param [optional] aData if provided, will be passed to the callback.
michael@0 116 * @see http://sqlite.org/c3ref/commit_hook.html
michael@0 117 */
michael@0 118 void setCommitHook(int (*aCallbackFn)(void *) , void *aData=nullptr) {
michael@0 119 MOZ_ASSERT(mDBConn, "A connection must exist at this point");
michael@0 120 ::sqlite3_commit_hook(mDBConn, aCallbackFn, aData);
michael@0 121 };
michael@0 122
michael@0 123 /**
michael@0 124 * Lazily creates and returns a background execution thread. In the future,
michael@0 125 * the thread may be re-claimed if left idle, so you should call this
michael@0 126 * method just before you dispatch and not save the reference.
michael@0 127 *
michael@0 128 * @returns an event target suitable for asynchronous statement execution.
michael@0 129 */
michael@0 130 nsIEventTarget *getAsyncExecutionTarget();
michael@0 131
michael@0 132 /**
michael@0 133 * Mutex used by asynchronous statements to protect state. The mutex is
michael@0 134 * declared on the connection object because there is no contention between
michael@0 135 * asynchronous statements (they are serialized on mAsyncExecutionThread).
michael@0 136 * Currently protects:
michael@0 137 * - Connection.mAsyncExecutionThreadShuttingDown
michael@0 138 * - Connection.mAsyncExecutionThread
michael@0 139 * - Connection.mConnectionClosed
michael@0 140 * - AsyncExecuteStatements.mCancelRequested
michael@0 141 */
michael@0 142 Mutex sharedAsyncExecutionMutex;
michael@0 143
michael@0 144 /**
michael@0 145 * Wraps the mutex that SQLite gives us from sqlite3_db_mutex. This is public
michael@0 146 * because we already expose the sqlite3* native connection and proper
michael@0 147 * operation of the deadlock detector requires everyone to use the same single
michael@0 148 * SQLiteMutex instance for correctness.
michael@0 149 */
michael@0 150 SQLiteMutex sharedDBMutex;
michael@0 151
michael@0 152 /**
michael@0 153 * References the thread this database was opened on. This MUST be thread it is
michael@0 154 * closed on.
michael@0 155 */
michael@0 156 const nsCOMPtr<nsIThread> threadOpenedOn;
michael@0 157
michael@0 158 /**
michael@0 159 * Closes the SQLite database, and warns about any non-finalized statements.
michael@0 160 */
michael@0 161 nsresult internalClose(sqlite3 *aDBConn);
michael@0 162
michael@0 163 /**
michael@0 164 * Obtains the filename of the connection. Useful for logging.
michael@0 165 */
michael@0 166 nsCString getFilename();
michael@0 167
michael@0 168 /**
michael@0 169 * Creates an sqlite3 prepared statement object from an SQL string.
michael@0 170 *
michael@0 171 * @param aNativeConnection
michael@0 172 * The underlying Sqlite connection to prepare the statement with.
michael@0 173 * @param aSQL
michael@0 174 * The SQL statement string to compile.
michael@0 175 * @param _stmt
michael@0 176 * New sqlite3_stmt object.
michael@0 177 * @return the result from sqlite3_prepare_v2.
michael@0 178 */
michael@0 179 int prepareStatement(sqlite3* aNativeConnection,
michael@0 180 const nsCString &aSQL, sqlite3_stmt **_stmt);
michael@0 181
michael@0 182 /**
michael@0 183 * Performs a sqlite3_step on aStatement, while properly handling SQLITE_LOCKED
michael@0 184 * when not on the main thread by waiting until we are notified.
michael@0 185 *
michael@0 186 * @param aNativeConnection
michael@0 187 * The underlying Sqlite connection to step the statement with.
michael@0 188 * @param aStatement
michael@0 189 * A pointer to a sqlite3_stmt object.
michael@0 190 * @return the result from sqlite3_step.
michael@0 191 */
michael@0 192 int stepStatement(sqlite3* aNativeConnection, sqlite3_stmt* aStatement);
michael@0 193
michael@0 194 /**
michael@0 195 * Raw connection transaction management.
michael@0 196 *
michael@0 197 * @see BeginTransactionAs, CommitTransaction, RollbackTransaction.
michael@0 198 */
michael@0 199 nsresult beginTransactionInternal(sqlite3 *aNativeConnection,
michael@0 200 int32_t aTransactionType=TRANSACTION_DEFERRED);
michael@0 201 nsresult commitTransactionInternal(sqlite3 *aNativeConnection);
michael@0 202 nsresult rollbackTransactionInternal(sqlite3 *aNativeConnection);
michael@0 203
michael@0 204 bool connectionReady();
michael@0 205
michael@0 206 /**
michael@0 207 * True if this connection is shutting down but not yet closed.
michael@0 208 */
michael@0 209 bool isClosing();
michael@0 210
michael@0 211 /**
michael@0 212 * True if the underlying connection is closed.
michael@0 213 * Any sqlite resources may be lost when this returns true, so nothing should
michael@0 214 * try to use them.
michael@0 215 */
michael@0 216 bool isClosed();
michael@0 217
michael@0 218 nsresult initializeClone(Connection *aClone, bool aReadOnly);
michael@0 219
michael@0 220 private:
michael@0 221 ~Connection();
michael@0 222 nsresult initializeInternal(nsIFile *aDatabaseFile);
michael@0 223
michael@0 224 /**
michael@0 225 * Sets the database into a closed state so no further actions can be
michael@0 226 * performed.
michael@0 227 *
michael@0 228 * @note mDBConn is set to nullptr in this method.
michael@0 229 */
michael@0 230 nsresult setClosedState();
michael@0 231
michael@0 232 /**
michael@0 233 * Helper for calls to sqlite3_exec. Reports long delays to Telemetry.
michael@0 234 *
michael@0 235 * @param aNativeConnection
michael@0 236 * The underlying Sqlite connection to execute the query with.
michael@0 237 * @param aSqlString
michael@0 238 * SQL string to execute
michael@0 239 * @return the result from sqlite3_exec.
michael@0 240 */
michael@0 241 int executeSql(sqlite3 *aNativeConnection, const char *aSqlString);
michael@0 242
michael@0 243 /**
michael@0 244 * Describes a certain primitive type in the database.
michael@0 245 *
michael@0 246 * Possible Values Are:
michael@0 247 * INDEX - To check for the existence of an index
michael@0 248 * TABLE - To check for the existence of a table
michael@0 249 */
michael@0 250 enum DatabaseElementType {
michael@0 251 INDEX,
michael@0 252 TABLE
michael@0 253 };
michael@0 254
michael@0 255 /**
michael@0 256 * Determines if the specified primitive exists.
michael@0 257 *
michael@0 258 * @param aElementType
michael@0 259 * The type of element to check the existence of
michael@0 260 * @param aElementName
michael@0 261 * The name of the element to check for
michael@0 262 * @returns true if element exists, false otherwise
michael@0 263 */
michael@0 264 nsresult databaseElementExists(enum DatabaseElementType aElementType,
michael@0 265 const nsACString& aElementName,
michael@0 266 bool *_exists);
michael@0 267
michael@0 268 bool findFunctionByInstance(nsISupports *aInstance);
michael@0 269
michael@0 270 static int sProgressHelper(void *aArg);
michael@0 271 // Generic progress handler
michael@0 272 // Dispatch call to registered progress handler,
michael@0 273 // if there is one. Do nothing in other cases.
michael@0 274 int progressHandler();
michael@0 275
michael@0 276 sqlite3 *mDBConn;
michael@0 277 nsCOMPtr<nsIFileURL> mFileURL;
michael@0 278 nsCOMPtr<nsIFile> mDatabaseFile;
michael@0 279
michael@0 280 /**
michael@0 281 * Lazily created thread for asynchronous statement execution. Consumers
michael@0 282 * should use getAsyncExecutionTarget rather than directly accessing this
michael@0 283 * field.
michael@0 284 */
michael@0 285 nsCOMPtr<nsIThread> mAsyncExecutionThread;
michael@0 286
michael@0 287 /**
michael@0 288 * Set to true by Close() or AsyncClose() prior to shutdown.
michael@0 289 *
michael@0 290 * If false, we guarantee both that the underlying sqlite3 database
michael@0 291 * connection is still open and that getAsyncExecutionTarget() can
michael@0 292 * return a thread. Once true, either the sqlite3 database
michael@0 293 * connection is being shutdown or it has been
michael@0 294 * shutdown. Additionally, once true, getAsyncExecutionTarget()
michael@0 295 * returns null.
michael@0 296 *
michael@0 297 * This variable should be accessed while holding the
michael@0 298 * sharedAsyncExecutionMutex.
michael@0 299 */
michael@0 300 bool mAsyncExecutionThreadShuttingDown;
michael@0 301
michael@0 302 /**
michael@0 303 * Set to true just prior to calling sqlite3_close on the
michael@0 304 * connection.
michael@0 305 *
michael@0 306 * This variable should be accessed while holding the
michael@0 307 * sharedAsyncExecutionMutex.
michael@0 308 */
michael@0 309 bool mConnectionClosed;
michael@0 310
michael@0 311 /**
michael@0 312 * Tracks if we have a transaction in progress or not. Access protected by
michael@0 313 * sharedDBMutex.
michael@0 314 */
michael@0 315 bool mTransactionInProgress;
michael@0 316
michael@0 317 /**
michael@0 318 * Stores the mapping of a given function by name to its instance. Access is
michael@0 319 * protected by sharedDBMutex.
michael@0 320 */
michael@0 321 nsDataHashtable<nsCStringHashKey, FunctionInfo> mFunctions;
michael@0 322
michael@0 323 /**
michael@0 324 * Stores the registered progress handler for the database connection. Access
michael@0 325 * is protected by sharedDBMutex.
michael@0 326 */
michael@0 327 nsCOMPtr<mozIStorageProgressHandler> mProgressHandler;
michael@0 328
michael@0 329 /**
michael@0 330 * Stores the flags we passed to sqlite3_open_v2.
michael@0 331 */
michael@0 332 const int mFlags;
michael@0 333
michael@0 334 // This is here for two reasons: 1) It's used to make sure that the
michael@0 335 // connections do not outlive the service. 2) Our custom collating functions
michael@0 336 // call its localeCompareStrings() method.
michael@0 337 nsRefPtr<Service> mStorageService;
michael@0 338
michael@0 339 /**
michael@0 340 * If |false|, this instance supports synchronous operations
michael@0 341 * and it can be cast to |mozIStorageConnection|.
michael@0 342 */
michael@0 343 const bool mAsyncOnly;
michael@0 344 };
michael@0 345
michael@0 346
michael@0 347 /**
michael@0 348 * A Runnable designed to call a mozIStorageCompletionCallback on
michael@0 349 * the appropriate thread.
michael@0 350 */
michael@0 351 class CallbackComplete MOZ_FINAL : public nsRunnable
michael@0 352 {
michael@0 353 public:
michael@0 354 /**
michael@0 355 * @param aValue The result to pass to the callback. It must
michael@0 356 * already be owned by the main thread.
michael@0 357 * @param aCallback The callback. It must already be owned by the
michael@0 358 * main thread.
michael@0 359 */
michael@0 360 CallbackComplete(nsresult aStatus,
michael@0 361 nsISupports* aValue,
michael@0 362 already_AddRefed<mozIStorageCompletionCallback> aCallback)
michael@0 363 : mStatus(aStatus)
michael@0 364 , mValue(aValue)
michael@0 365 , mCallback(aCallback)
michael@0 366 {
michael@0 367 }
michael@0 368
michael@0 369 NS_IMETHOD Run() {
michael@0 370 MOZ_ASSERT(NS_IsMainThread());
michael@0 371 nsresult rv = mCallback->Complete(mStatus, mValue);
michael@0 372
michael@0 373 // Ensure that we release on the main thread
michael@0 374 mValue = nullptr;
michael@0 375 mCallback = nullptr;
michael@0 376 return rv;
michael@0 377 }
michael@0 378
michael@0 379 private:
michael@0 380 nsresult mStatus;
michael@0 381 nsCOMPtr<nsISupports> mValue;
michael@0 382 // This is a nsRefPtr<T> and not a nsCOMPtr<T> because
michael@0 383 // nsCOMP<T> would cause an off-main thread QI, which
michael@0 384 // is not a good idea (and crashes XPConnect).
michael@0 385 nsRefPtr<mozIStorageCompletionCallback> mCallback;
michael@0 386 };
michael@0 387
michael@0 388 } // namespace storage
michael@0 389 } // namespace mozilla
michael@0 390
michael@0 391 #endif // mozilla_storage_Connection_h

mercurial