Wed, 31 Dec 2014 13:27:57 +0100
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 mozStorageAsyncStatementExecution_h |
michael@0 | 8 | #define mozStorageAsyncStatementExecution_h |
michael@0 | 9 | |
michael@0 | 10 | #include "nscore.h" |
michael@0 | 11 | #include "nsTArray.h" |
michael@0 | 12 | #include "nsAutoPtr.h" |
michael@0 | 13 | #include "mozilla/Mutex.h" |
michael@0 | 14 | #include "mozilla/TimeStamp.h" |
michael@0 | 15 | #include "mozilla/Attributes.h" |
michael@0 | 16 | #include "nsIRunnable.h" |
michael@0 | 17 | |
michael@0 | 18 | #include "SQLiteMutex.h" |
michael@0 | 19 | #include "mozIStoragePendingStatement.h" |
michael@0 | 20 | #include "mozIStorageStatementCallback.h" |
michael@0 | 21 | #include "mozStorageHelper.h" |
michael@0 | 22 | |
michael@0 | 23 | struct sqlite3_stmt; |
michael@0 | 24 | |
michael@0 | 25 | namespace mozilla { |
michael@0 | 26 | namespace storage { |
michael@0 | 27 | |
michael@0 | 28 | class Connection; |
michael@0 | 29 | class ResultSet; |
michael@0 | 30 | class StatementData; |
michael@0 | 31 | |
michael@0 | 32 | class AsyncExecuteStatements MOZ_FINAL : public nsIRunnable |
michael@0 | 33 | , public mozIStoragePendingStatement |
michael@0 | 34 | { |
michael@0 | 35 | public: |
michael@0 | 36 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 37 | NS_DECL_NSIRUNNABLE |
michael@0 | 38 | NS_DECL_MOZISTORAGEPENDINGSTATEMENT |
michael@0 | 39 | |
michael@0 | 40 | /** |
michael@0 | 41 | * Describes the state of execution. |
michael@0 | 42 | */ |
michael@0 | 43 | enum ExecutionState { |
michael@0 | 44 | PENDING = -1, |
michael@0 | 45 | COMPLETED = mozIStorageStatementCallback::REASON_FINISHED, |
michael@0 | 46 | CANCELED = mozIStorageStatementCallback::REASON_CANCELED, |
michael@0 | 47 | ERROR = mozIStorageStatementCallback::REASON_ERROR |
michael@0 | 48 | }; |
michael@0 | 49 | |
michael@0 | 50 | typedef nsTArray<StatementData> StatementDataArray; |
michael@0 | 51 | |
michael@0 | 52 | /** |
michael@0 | 53 | * Executes a statement in the background, and passes results back to the |
michael@0 | 54 | * caller. |
michael@0 | 55 | * |
michael@0 | 56 | * @param aStatements |
michael@0 | 57 | * The statements to execute and possibly bind in the background. |
michael@0 | 58 | * Ownership is transfered from the caller. |
michael@0 | 59 | * @param aConnection |
michael@0 | 60 | * The connection that created the statements to execute. |
michael@0 | 61 | * @param aNativeConnection |
michael@0 | 62 | * The native Sqlite connection that created the statements to execute. |
michael@0 | 63 | * @param aCallback |
michael@0 | 64 | * The callback that is notified of results, completion, and errors. |
michael@0 | 65 | * @param _stmt |
michael@0 | 66 | * The handle to control the execution of the statements. |
michael@0 | 67 | */ |
michael@0 | 68 | static nsresult execute(StatementDataArray &aStatements, |
michael@0 | 69 | Connection *aConnection, |
michael@0 | 70 | sqlite3 *aNativeConnection, |
michael@0 | 71 | mozIStorageStatementCallback *aCallback, |
michael@0 | 72 | mozIStoragePendingStatement **_stmt); |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Indicates when events on the calling thread should run or not. Certain |
michael@0 | 76 | * events posted back to the calling thread should call this see if they |
michael@0 | 77 | * should run or not. |
michael@0 | 78 | * |
michael@0 | 79 | * @pre mMutex is not held |
michael@0 | 80 | * |
michael@0 | 81 | * @returns true if the event should notify still, false otherwise. |
michael@0 | 82 | */ |
michael@0 | 83 | bool shouldNotify(); |
michael@0 | 84 | |
michael@0 | 85 | private: |
michael@0 | 86 | AsyncExecuteStatements(StatementDataArray &aStatements, |
michael@0 | 87 | Connection *aConnection, |
michael@0 | 88 | sqlite3 *aNativeConnection, |
michael@0 | 89 | mozIStorageStatementCallback *aCallback); |
michael@0 | 90 | ~AsyncExecuteStatements(); |
michael@0 | 91 | |
michael@0 | 92 | /** |
michael@0 | 93 | * Binds and then executes a given statement until completion, an error |
michael@0 | 94 | * occurs, or we are canceled. If aLastStatement is true, we should set |
michael@0 | 95 | * mState accordingly. |
michael@0 | 96 | * |
michael@0 | 97 | * @pre mMutex is not held |
michael@0 | 98 | * |
michael@0 | 99 | * @param aData |
michael@0 | 100 | * The StatementData to bind, execute, and then process. |
michael@0 | 101 | * @param aLastStatement |
michael@0 | 102 | * Indicates if this is the last statement or not. If it is, we have |
michael@0 | 103 | * to set the proper state. |
michael@0 | 104 | * @returns true if we should continue to process statements, false otherwise. |
michael@0 | 105 | */ |
michael@0 | 106 | bool bindExecuteAndProcessStatement(StatementData &aData, |
michael@0 | 107 | bool aLastStatement); |
michael@0 | 108 | |
michael@0 | 109 | /** |
michael@0 | 110 | * Executes a given statement until completion, an error occurs, or we are |
michael@0 | 111 | * canceled. If aLastStatement is true, we should set mState accordingly. |
michael@0 | 112 | * |
michael@0 | 113 | * @pre mMutex is not held |
michael@0 | 114 | * |
michael@0 | 115 | * @param aStatement |
michael@0 | 116 | * The statement to execute and then process. |
michael@0 | 117 | * @param aLastStatement |
michael@0 | 118 | * Indicates if this is the last statement or not. If it is, we have |
michael@0 | 119 | * to set the proper state. |
michael@0 | 120 | * @returns true if we should continue to process statements, false otherwise. |
michael@0 | 121 | */ |
michael@0 | 122 | bool executeAndProcessStatement(sqlite3_stmt *aStatement, |
michael@0 | 123 | bool aLastStatement); |
michael@0 | 124 | |
michael@0 | 125 | /** |
michael@0 | 126 | * Executes a statement to completion, properly handling any error conditions. |
michael@0 | 127 | * |
michael@0 | 128 | * @pre mMutex is not held |
michael@0 | 129 | * |
michael@0 | 130 | * @param aStatement |
michael@0 | 131 | * The statement to execute to completion. |
michael@0 | 132 | * @returns true if results were obtained, false otherwise. |
michael@0 | 133 | */ |
michael@0 | 134 | bool executeStatement(sqlite3_stmt *aStatement); |
michael@0 | 135 | |
michael@0 | 136 | /** |
michael@0 | 137 | * Builds a result set up with a row from a given statement. If we meet the |
michael@0 | 138 | * right criteria, go ahead and notify about this results too. |
michael@0 | 139 | * |
michael@0 | 140 | * @pre mMutex is not held |
michael@0 | 141 | * |
michael@0 | 142 | * @param aStatement |
michael@0 | 143 | * The statement to get the row data from. |
michael@0 | 144 | */ |
michael@0 | 145 | nsresult buildAndNotifyResults(sqlite3_stmt *aStatement); |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * Notifies callback about completion, and does any necessary cleanup. |
michael@0 | 149 | * |
michael@0 | 150 | * @pre mMutex is not held |
michael@0 | 151 | */ |
michael@0 | 152 | nsresult notifyComplete(); |
michael@0 | 153 | |
michael@0 | 154 | /** |
michael@0 | 155 | * Notifies callback about an error. |
michael@0 | 156 | * |
michael@0 | 157 | * @pre mMutex is not held |
michael@0 | 158 | * @pre mDBMutex is not held |
michael@0 | 159 | * |
michael@0 | 160 | * @param aErrorCode |
michael@0 | 161 | * The error code defined in mozIStorageError for the error. |
michael@0 | 162 | * @param aMessage |
michael@0 | 163 | * The error string, if any. |
michael@0 | 164 | * @param aError |
michael@0 | 165 | * The error object to notify the caller with. |
michael@0 | 166 | */ |
michael@0 | 167 | nsresult notifyError(int32_t aErrorCode, const char *aMessage); |
michael@0 | 168 | nsresult notifyError(mozIStorageError *aError); |
michael@0 | 169 | |
michael@0 | 170 | /** |
michael@0 | 171 | * Notifies the callback about a result set. |
michael@0 | 172 | * |
michael@0 | 173 | * @pre mMutex is not held |
michael@0 | 174 | */ |
michael@0 | 175 | nsresult notifyResults(); |
michael@0 | 176 | |
michael@0 | 177 | /** |
michael@0 | 178 | * Tests whether the current statements should be wrapped in an explicit |
michael@0 | 179 | * transaction. |
michael@0 | 180 | * |
michael@0 | 181 | * @return true if an explicit transaction is needed, false otherwise. |
michael@0 | 182 | */ |
michael@0 | 183 | bool statementsNeedTransaction(); |
michael@0 | 184 | |
michael@0 | 185 | StatementDataArray mStatements; |
michael@0 | 186 | nsRefPtr<Connection> mConnection; |
michael@0 | 187 | sqlite3 *mNativeConnection; |
michael@0 | 188 | bool mHasTransaction; |
michael@0 | 189 | mozIStorageStatementCallback *mCallback; |
michael@0 | 190 | nsCOMPtr<nsIThread> mCallingThread; |
michael@0 | 191 | nsRefPtr<ResultSet> mResultSet; |
michael@0 | 192 | |
michael@0 | 193 | /** |
michael@0 | 194 | * The maximum amount of time we want to wait between results. Defined by |
michael@0 | 195 | * MAX_MILLISECONDS_BETWEEN_RESULTS and set at construction. |
michael@0 | 196 | */ |
michael@0 | 197 | const TimeDuration mMaxWait; |
michael@0 | 198 | |
michael@0 | 199 | /** |
michael@0 | 200 | * The start time since our last set of results. |
michael@0 | 201 | */ |
michael@0 | 202 | TimeStamp mIntervalStart; |
michael@0 | 203 | |
michael@0 | 204 | /** |
michael@0 | 205 | * Indicates our state of execution. |
michael@0 | 206 | */ |
michael@0 | 207 | ExecutionState mState; |
michael@0 | 208 | |
michael@0 | 209 | /** |
michael@0 | 210 | * Indicates if we should try to cancel at a cancelation point. |
michael@0 | 211 | */ |
michael@0 | 212 | bool mCancelRequested; |
michael@0 | 213 | |
michael@0 | 214 | /** |
michael@0 | 215 | * This is the mutex that protects our state from changing between threads. |
michael@0 | 216 | * This includes the following variables: |
michael@0 | 217 | * - mCancelRequested is only set on the calling thread while the lock is |
michael@0 | 218 | * held. It is always read from within the lock on the background thread, |
michael@0 | 219 | * but not on the calling thread (see shouldNotify for why). |
michael@0 | 220 | */ |
michael@0 | 221 | Mutex &mMutex; |
michael@0 | 222 | |
michael@0 | 223 | /** |
michael@0 | 224 | * The wrapped SQLite recursive connection mutex. We use it whenever we call |
michael@0 | 225 | * sqlite3_step and care about having reliable error messages. By taking it |
michael@0 | 226 | * prior to the call and holding it until the point where we no longer care |
michael@0 | 227 | * about the error message, the user gets reliable error messages. |
michael@0 | 228 | */ |
michael@0 | 229 | SQLiteMutex &mDBMutex; |
michael@0 | 230 | |
michael@0 | 231 | /** |
michael@0 | 232 | * The instant at which the request was started. |
michael@0 | 233 | * |
michael@0 | 234 | * Used by telemetry. |
michael@0 | 235 | */ |
michael@0 | 236 | TimeStamp mRequestStartDate; |
michael@0 | 237 | }; |
michael@0 | 238 | |
michael@0 | 239 | } // namespace storage |
michael@0 | 240 | } // namespace mozilla |
michael@0 | 241 | |
michael@0 | 242 | #endif // mozStorageAsyncStatementExecution_h |