1 /*
2 * Copyright (C) 2007, 2008, 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "config.h"
30 #include "modules/webdatabase/SQLTransactionBackend.h"
31
32 #include "platform/Logging.h"
33 #include "modules/webdatabase/sqlite/SQLValue.h"
34 #include "modules/webdatabase/sqlite/SQLiteTransaction.h"
35 #include "modules/webdatabase/AbstractSQLTransaction.h"
36 #include "modules/webdatabase/Database.h" // FIXME: Should only be used in the frontend.
37 #include "modules/webdatabase/DatabaseAuthorizer.h"
38 #include "modules/webdatabase/DatabaseBackend.h"
39 #include "modules/webdatabase/DatabaseContext.h"
40 #include "modules/webdatabase/DatabaseThread.h"
41 #include "modules/webdatabase/DatabaseTracker.h"
42 #include "modules/webdatabase/SQLError.h"
43 #include "modules/webdatabase/SQLStatementBackend.h"
44 #include "modules/webdatabase/SQLTransactionClient.h"
45 #include "modules/webdatabase/SQLTransactionCoordinator.h"
46 #include "wtf/StdLibExtras.h"
47
48
49 // How does a SQLTransaction work?
50 // ==============================
51 // The SQLTransaction is a state machine that executes a series of states / steps.
52 //
53 // The work of the transaction states are defined in section of 4.3.2 of the
54 // webdatabase spec: http://dev.w3.org/html5/webdatabase/#processing-model
55 //
56 // the State Transition Graph at a glance:
57 // ======================================
58 //
59 // Backend . Frontend
60 // (works with SQLiteDatabase) . (works with Script)
61 // =========================== . ===================
62 // .
63 // 1. Idle .
64 // v .
65 // 2. AcquireLock .
66 // v .
67 // 3. OpenTransactionAndPreflight ------------------------------------------.
68 // | . |
69 // `-------------------------------> 8. DeliverTransactionCallback --. |
70 // . | v v
71 // ,-------------------------------------' 9. DeliverTransactionErrorCallback +
72 // | . ^ ^ ^ |
73 // v . | | | |
74 // 4. RunStatements -----------------------------------------------------' | | |
75 // | ^ ^ | ^ | . | | |
76 // |--------' | | | `------------> 10. DeliverStatementCallback +-----' | |
77 // | | | `---------------------------------------------' | |
78 // | | `-----------------> 11. DeliverQuotaIncreaseCallback + | |
79 // | `-----------------------------------------------------' | |
80 // v . | |
81 // 5. PostflightAndCommit --+--------------------------------------------------' |
82 // |----------> 12. DeliverSuccessCallback + |
83 // ,--------------------' . | |
84 // v . | |
85 // 6. CleanupAndTerminate <-----------------------------------------' |
86 // v ^ . |
87 // 0. End | . |
88 // | . |
89 // 7: CleanupAfterTransactionErrorCallback <----------------------------'
90 // .
91 //
92 // the States and State Transitions:
93 // ================================
94 // 0. SQLTransactionState::End
95 // - the end state.
96 //
97 // 1. SQLTransactionState::Idle
98 // - placeholder state while waiting on frontend/backend, etc. See comment on
99 // "State transitions between SQLTransaction and SQLTransactionBackend"
100 // below.
101 //
102 // 2. SQLTransactionState::AcquireLock (runs in backend)
103 // - this is the start state.
104 // - acquire the "lock".
105 // - on "lock" acquisition, goto SQLTransactionState::OpenTransactionAndPreflight.
106 //
107 // 3. SQLTransactionState::openTransactionAndPreflight (runs in backend)
108 // - Sets up an SQLiteTransaction.
109 // - begin the SQLiteTransaction.
110 // - call the SQLTransactionWrapper preflight if available.
111 // - schedule script callback.
112 // - on error, goto SQLTransactionState::DeliverTransactionErrorCallback.
113 // - goto SQLTransactionState::DeliverTransactionCallback.
114 //
115 // 4. SQLTransactionState::DeliverTransactionCallback (runs in frontend)
116 // - invoke the script function callback() if available.
117 // - on error, goto SQLTransactionState::DeliverTransactionErrorCallback.
118 // - goto SQLTransactionState::RunStatements.
119 //
120 // 5. SQLTransactionState::DeliverTransactionErrorCallback (runs in frontend)
121 // - invoke the script function errorCallback if available.
122 // - goto SQLTransactionState::CleanupAfterTransactionErrorCallback.
123 //
124 // 6. SQLTransactionState::RunStatements (runs in backend)
125 // - while there are statements {
126 // - run a statement.
127 // - if statementCallback is available, goto SQLTransactionState::DeliverStatementCallback.
128 // - on error,
129 // goto SQLTransactionState::DeliverQuotaIncreaseCallback, or
130 // goto SQLTransactionState::DeliverStatementCallback, or
131 // goto SQLTransactionState::deliverTransactionErrorCallback.
132 // }
133 // - goto SQLTransactionState::PostflightAndCommit.
134 //
135 // 7. SQLTransactionState::DeliverStatementCallback (runs in frontend)
136 // - invoke script statement callback (assume available).
137 // - on error, goto SQLTransactionState::DeliverTransactionErrorCallback.
138 // - goto SQLTransactionState::RunStatements.
139 //
140 // 8. SQLTransactionState::DeliverQuotaIncreaseCallback (runs in frontend)
141 // - give client a chance to increase the quota.
142 // - goto SQLTransactionState::RunStatements.
143 //
144 // 9. SQLTransactionState::PostflightAndCommit (runs in backend)
145 // - call the SQLTransactionWrapper postflight if available.
146 // - commit the SQLiteTansaction.
147 // - on error, goto SQLTransactionState::DeliverTransactionErrorCallback.
148 // - if successCallback is available, goto SQLTransactionState::DeliverSuccessCallback.
149 // else goto SQLTransactionState::CleanupAndTerminate.
150 //
151 // 10. SQLTransactionState::DeliverSuccessCallback (runs in frontend)
152 // - invoke the script function successCallback() if available.
153 // - goto SQLTransactionState::CleanupAndTerminate.
154 //
155 // 11. SQLTransactionState::CleanupAndTerminate (runs in backend)
156 // - stop and clear the SQLiteTransaction.
157 // - release the "lock".
158 // - goto SQLTransactionState::End.
159 //
160 // 12. SQLTransactionState::CleanupAfterTransactionErrorCallback (runs in backend)
161 // - rollback the SQLiteTransaction.
162 // - goto SQLTransactionState::CleanupAndTerminate.
163 //
164 // State transitions between SQLTransaction and SQLTransactionBackend
165 // ==================================================================
166 // As shown above, there are state transitions that crosses the boundary between
167 // the frontend and backend. For example,
168 //
169 // OpenTransactionAndPreflight (state 3 in the backend)
170 // transitions to DeliverTransactionCallback (state 8 in the frontend),
171 // which in turn transitions to RunStatements (state 4 in the backend).
172 //
173 // This cross boundary transition is done by posting transition requests to the
174 // other side and letting the other side's state machine execute the state
175 // transition in the appropriate thread (i.e. the script thread for the frontend,
176 // and the database thread for the backend).
177 //
178 // Logically, the state transitions work as shown in the graph above. But
179 // physically, the transition mechanism uses the Idle state (both in the frontend
180 // and backend) as a waiting state for further activity. For example, taking a
181 // closer look at the 3 state transition example above, what actually happens
182 // is as follows:
183 //
184 // Step 1:
185 // ======
186 // In the frontend thread:
187 // - waiting quietly is Idle. Not doing any work.
188 //
189 // In the backend:
190 // - is in OpenTransactionAndPreflight, and doing its work.
191 // - when done, it transits to the backend DeliverTransactionCallback.
192 // - the backend DeliverTransactionCallback sends a request to the frontend
193 // to transit to DeliverTransactionCallback, and then itself transits to
194 // Idle.
195 //
196 // Step 2:
197 // ======
198 // In the frontend thread:
199 // - transits to DeliverTransactionCallback and does its work.
200 // - when done, it transits to the frontend RunStatements.
201 // - the frontend RunStatements sends a request to the backend to transit
202 // to RunStatements, and then itself transits to Idle.
203 //
204 // In the backend:
205 // - waiting quietly in Idle.
206 //
207 // Step 3:
208 // ======
209 // In the frontend thread:
210 // - waiting quietly is Idle. Not doing any work.
211 //
212 // In the backend:
213 // - transits to RunStatements, and does its work.
214 // ...
215 //
216 // So, when the frontend or backend are not active, they will park themselves in
217 // their Idle states. This means their m_nextState is set to Idle, but they never
218 // actually run the corresponding state function. Note: for both the frontend and
219 // backend, the state function for Idle is unreachableState().
220 //
221 // The states that send a request to their peer across the front/back boundary
222 // are implemented with just 2 functions: SQLTransaction::sendToBackendState()
223 // and SQLTransactionBackend::sendToFrontendState(). These state functions do
224 // nothing but sends a request to the other side to transit to the current
225 // state (indicated by m_nextState), and then transits itself to the Idle state
226 // to wait for further action.
227
228
229 // The Life-Cycle of a SQLTransaction i.e. Who's keeping the SQLTransaction alive?
230 // ==============================================================================
231 // The RefPtr chain goes something like this:
232 //
233 // At birth (in DatabaseBackend::runTransaction()):
234 // ====================================================
235 // DatabaseBackend // Deque<RefPtr<SQLTransactionBackend> > m_transactionQueue points to ...
236 // --> SQLTransactionBackend // RefPtr<SQLTransaction> m_frontend points to ...
237 // --> SQLTransaction // RefPtr<SQLTransactionBackend> m_backend points to ...
238 // --> SQLTransactionBackend // which is a circular reference.
239 //
240 // Note: there's a circular reference between the SQLTransaction front-end and
241 // back-end. This circular reference is established in the constructor of the
242 // SQLTransactionBackend. The circular reference will be broken by calling
243 // doCleanup() to nullify m_frontend. This is done at the end of the transaction's
244 // clean up state (i.e. when the transaction should no longer be in use thereafter),
245 // or if the database was interrupted. See comments on "What happens if a transaction
246 // is interrupted?" below for details.
247 //
248 // After scheduling the transaction with the DatabaseThread (DatabaseBackend::scheduleTransaction()):
249 // ======================================================================================================
250 // DatabaseThread // MessageQueue<DatabaseTask> m_queue points to ...
251 // --> DatabaseTransactionTask // RefPtr<SQLTransactionBackend> m_transaction points to ...
252 // --> SQLTransactionBackend // RefPtr<SQLTransaction> m_frontend points to ...
253 // --> SQLTransaction // RefPtr<SQLTransactionBackend> m_backend points to ...
254 // --> SQLTransactionBackend // which is a circular reference.
255 //
256 // When executing the transaction (in DatabaseThread::databaseThread()):
257 // ====================================================================
258 // OwnPtr<DatabaseTask> task; // points to ...
259 // --> DatabaseTransactionTask // RefPtr<SQLTransactionBackend> m_transaction points to ...
260 // --> SQLTransactionBackend // RefPtr<SQLTransaction> m_frontend;
261 // --> SQLTransaction // RefPtr<SQLTransactionBackend> m_backend points to ...
262 // --> SQLTransactionBackend // which is a circular reference.
263 //
264 // At the end of cleanupAndTerminate():
265 // ===================================
266 // At the end of the cleanup state, the SQLTransactionBackend::m_frontend is nullified.
267 // If by then, a JSObject wrapper is referring to the SQLTransaction, then the reference
268 // chain looks like this:
269 //
270 // JSObjectWrapper
271 // --> SQLTransaction // in RefPtr<SQLTransactionBackend> m_backend points to ...
272 // --> SQLTransactionBackend // which no longer points back to its SQLTransaction.
273 //
274 // When the GC collects the corresponding JSObject, the above chain will be cleaned up
275 // and deleted.
276 //
277 // If there is no JSObject wrapper referring to the SQLTransaction when the cleanup
278 // states nullify SQLTransactionBackend::m_frontend, the SQLTransaction will deleted then.
279 // However, there will still be a DatabaseTask pointing to the SQLTransactionBackend (see
280 // the "When executing the transaction" chain above). This will keep the
281 // SQLTransactionBackend alive until DatabaseThread::databaseThread() releases its
282 // task OwnPtr.
283 //
284 // What happens if a transaction is interrupted?
285 // ============================================
286 // If the transaction is interrupted half way, it won't get to run to state
287 // CleanupAndTerminate, and hence, would not have called SQLTransactionBackend's
288 // doCleanup(). doCleanup() is where we nullify SQLTransactionBackend::m_frontend
289 // to break the reference cycle between the frontend and backend. Hence, we need
290 // to cleanup the transaction by other means.
291 //
292 // Note: calling SQLTransactionBackend::notifyDatabaseThreadIsShuttingDown()
293 // is effectively the same as calling SQLTransactionBackend::doClean().
294 //
295 // In terms of who needs to call doCleanup(), there are 5 phases in the
296 // SQLTransactionBackend life-cycle. These are the phases and how the clean
297 // up is done:
298 //
299 // Phase 1. After Birth, before scheduling
300 //
301 // - To clean up, DatabaseThread::databaseThread() will call
302 // DatabaseBackend::close() during its shutdown.
303 // - DatabaseBackend::close() will iterate
304 // DatabaseBackend::m_transactionQueue and call
305 // notifyDatabaseThreadIsShuttingDown() on each transaction there.
306 //
307 // Phase 2. After scheduling, before state AcquireLock
308 //
309 // - If the interruption occures before the DatabaseTransactionTask is
310 // scheduled in DatabaseThread::m_queue but hasn't gotten to execute
311 // (i.e. DatabaseTransactionTask::performTask() has not been called),
312 // then the DatabaseTransactionTask may get destructed before it ever
313 // gets to execute.
314 // - To clean up, the destructor will check if the task's m_wasExecuted is
315 // set. If not, it will call notifyDatabaseThreadIsShuttingDown() on
316 // the task's transaction.
317 //
318 // Phase 3. After state AcquireLock, before "lockAcquired"
319 //
320 // - In this phase, the transaction would have been added to the
321 // SQLTransactionCoordinator's CoordinationInfo's pendingTransactions.
322 // - To clean up, during shutdown, DatabaseThread::databaseThread() calls
323 // SQLTransactionCoordinator::shutdown(), which calls
324 // notifyDatabaseThreadIsShuttingDown().
325 //
326 // Phase 4: After "lockAcquired", before state CleanupAndTerminate
327 //
328 // - In this phase, the transaction would have been added either to the
329 // SQLTransactionCoordinator's CoordinationInfo's activeWriteTransaction
330 // or activeReadTransactions.
331 // - To clean up, during shutdown, DatabaseThread::databaseThread() calls
332 // SQLTransactionCoordinator::shutdown(), which calls
333 // notifyDatabaseThreadIsShuttingDown().
334 //
335 // Phase 5: After state CleanupAndTerminate
336 //
337 // - This is how a transaction ends normally.
338 // - state CleanupAndTerminate calls doCleanup().
339
340
341 namespace WebCore {
342
create(DatabaseBackend * db,PassRefPtr<AbstractSQLTransaction> frontend,PassRefPtr<SQLTransactionWrapper> wrapper,bool readOnly)343 PassRefPtr<SQLTransactionBackend> SQLTransactionBackend::create(DatabaseBackend* db,
344 PassRefPtr<AbstractSQLTransaction> frontend, PassRefPtr<SQLTransactionWrapper> wrapper, bool readOnly)
345 {
346 return adoptRef(new SQLTransactionBackend(db, frontend, wrapper, readOnly));
347 }
348
SQLTransactionBackend(DatabaseBackend * db,PassRefPtr<AbstractSQLTransaction> frontend,PassRefPtr<SQLTransactionWrapper> wrapper,bool readOnly)349 SQLTransactionBackend::SQLTransactionBackend(DatabaseBackend* db,
350 PassRefPtr<AbstractSQLTransaction> frontend, PassRefPtr<SQLTransactionWrapper> wrapper, bool readOnly)
351 : m_frontend(frontend)
352 , m_database(db)
353 , m_wrapper(wrapper)
354 , m_hasCallback(m_frontend->hasCallback())
355 , m_hasSuccessCallback(m_frontend->hasSuccessCallback())
356 , m_hasErrorCallback(m_frontend->hasErrorCallback())
357 , m_shouldRetryCurrentStatement(false)
358 , m_modifiedDatabase(false)
359 , m_lockAcquired(false)
360 , m_readOnly(readOnly)
361 , m_hasVersionMismatch(false)
362 {
363 ASSERT(m_database);
364 m_frontend->setBackend(this);
365 m_requestedState = SQLTransactionState::AcquireLock;
366 }
367
~SQLTransactionBackend()368 SQLTransactionBackend::~SQLTransactionBackend()
369 {
370 ASSERT(!m_sqliteTransaction);
371 }
372
doCleanup()373 void SQLTransactionBackend::doCleanup()
374 {
375 if (!m_frontend)
376 return;
377 m_frontend = 0; // Break the reference cycle. See comment about the life-cycle above.
378
379 ASSERT(database()->databaseContext()->databaseThread()->isDatabaseThread());
380
381 MutexLocker locker(m_statementMutex);
382 m_statementQueue.clear();
383
384 if (m_sqliteTransaction) {
385 // In the event we got here because of an interruption or error (i.e. if
386 // the transaction is in progress), we should roll it back here. Clearing
387 // m_sqliteTransaction invokes SQLiteTransaction's destructor which does
388 // just that. We might as well do this unconditionally and free up its
389 // resources because we're already terminating.
390 m_sqliteTransaction.clear();
391 }
392
393 // Release the lock on this database
394 if (m_lockAcquired)
395 m_database->transactionCoordinator()->releaseLock(this);
396
397 // Do some aggresive clean up here except for m_database.
398 //
399 // We can't clear m_database here because the frontend may asynchronously
400 // invoke SQLTransactionBackend::requestTransitToState(), and that function
401 // uses m_database to schedule a state transition. This may occur because
402 // the frontend (being in another thread) may already be on the way to
403 // requesting our next state before it detects an interruption.
404 //
405 // There is no harm in letting it finish making the request. It'll set
406 // m_requestedState, but we won't execute a transition to that state because
407 // we've already shut down the transaction.
408 //
409 // We also can't clear m_currentStatementBackend and m_transactionError.
410 // m_currentStatementBackend may be accessed asynchronously by the
411 // frontend's deliverStatementCallback() state. Similarly,
412 // m_transactionError may be accessed by deliverTransactionErrorCallback().
413 // This occurs if requests for transition to those states have already been
414 // registered with the frontend just prior to a clean up request arriving.
415 //
416 // So instead, let our destructor handle their clean up since this
417 // SQLTransactionBackend is guaranteed to not destruct until the frontend
418 // is also destructing.
419
420 m_wrapper = 0;
421 }
422
currentStatement()423 AbstractSQLStatement* SQLTransactionBackend::currentStatement()
424 {
425 return m_currentStatementBackend->frontend();
426 }
427
transactionError()428 PassRefPtr<SQLError> SQLTransactionBackend::transactionError()
429 {
430 return m_transactionError;
431 }
432
setShouldRetryCurrentStatement(bool shouldRetry)433 void SQLTransactionBackend::setShouldRetryCurrentStatement(bool shouldRetry)
434 {
435 ASSERT(!m_shouldRetryCurrentStatement);
436 m_shouldRetryCurrentStatement = shouldRetry;
437 }
438
stateFunctionFor(SQLTransactionState state)439 SQLTransactionBackend::StateFunction SQLTransactionBackend::stateFunctionFor(SQLTransactionState state)
440 {
441 static const StateFunction stateFunctions[] = {
442 &SQLTransactionBackend::unreachableState, // 0. end
443 &SQLTransactionBackend::unreachableState, // 1. idle
444 &SQLTransactionBackend::acquireLock, // 2.
445 &SQLTransactionBackend::openTransactionAndPreflight, // 3.
446 &SQLTransactionBackend::runStatements, // 4.
447 &SQLTransactionBackend::postflightAndCommit, // 5.
448 &SQLTransactionBackend::cleanupAndTerminate, // 6.
449 &SQLTransactionBackend::cleanupAfterTransactionErrorCallback, // 7.
450 &SQLTransactionBackend::sendToFrontendState, // 8. deliverTransactionCallback
451 &SQLTransactionBackend::sendToFrontendState, // 9. deliverTransactionErrorCallback
452 &SQLTransactionBackend::sendToFrontendState, // 10. deliverStatementCallback
453 &SQLTransactionBackend::sendToFrontendState, // 11. deliverQuotaIncreaseCallback
454 &SQLTransactionBackend::sendToFrontendState // 12. deliverSuccessCallback
455 };
456
457 ASSERT(WTF_ARRAY_LENGTH(stateFunctions) == static_cast<int>(SQLTransactionState::NumberOfStates));
458 ASSERT(state < SQLTransactionState::NumberOfStates);
459
460 return stateFunctions[static_cast<int>(state)];
461 }
462
enqueueStatementBackend(PassRefPtr<SQLStatementBackend> statementBackend)463 void SQLTransactionBackend::enqueueStatementBackend(PassRefPtr<SQLStatementBackend> statementBackend)
464 {
465 MutexLocker locker(m_statementMutex);
466 m_statementQueue.append(statementBackend);
467 }
468
computeNextStateAndCleanupIfNeeded()469 void SQLTransactionBackend::computeNextStateAndCleanupIfNeeded()
470 {
471 // Only honor the requested state transition if we're not supposed to be
472 // cleaning up and shutting down:
473 if (m_database->opened() && !m_database->isInterrupted()) {
474 setStateToRequestedState();
475 ASSERT(m_nextState == SQLTransactionState::AcquireLock
476 || m_nextState == SQLTransactionState::OpenTransactionAndPreflight
477 || m_nextState == SQLTransactionState::RunStatements
478 || m_nextState == SQLTransactionState::PostflightAndCommit
479 || m_nextState == SQLTransactionState::CleanupAndTerminate
480 || m_nextState == SQLTransactionState::CleanupAfterTransactionErrorCallback);
481
482 WTF_LOG(StorageAPI, "State %s\n", nameForSQLTransactionState(m_nextState));
483 return;
484 }
485
486 // If we get here, then we should be shutting down. Do clean up if needed:
487 if (m_nextState == SQLTransactionState::End)
488 return;
489 m_nextState = SQLTransactionState::End;
490
491 // If the database was stopped, don't do anything and cancel queued work
492 WTF_LOG(StorageAPI, "Database was stopped or interrupted - cancelling work for this transaction");
493
494 // The current SQLite transaction should be stopped, as well
495 if (m_sqliteTransaction) {
496 m_sqliteTransaction->stop();
497 m_sqliteTransaction.clear();
498 }
499
500 // Terminate the frontend state machine. This also gets the frontend to
501 // call computeNextStateAndCleanupIfNeeded() and clear its wrappers
502 // if needed.
503 m_frontend->requestTransitToState(SQLTransactionState::End);
504
505 // Redirect to the end state to abort, clean up, and end the transaction.
506 doCleanup();
507 }
508
performNextStep()509 void SQLTransactionBackend::performNextStep()
510 {
511 computeNextStateAndCleanupIfNeeded();
512 runStateMachine();
513 }
514
executeSQL(PassOwnPtr<AbstractSQLStatement> statement,const String & sqlStatement,const Vector<SQLValue> & arguments,int permissions)515 void SQLTransactionBackend::executeSQL(PassOwnPtr<AbstractSQLStatement> statement,
516 const String& sqlStatement, const Vector<SQLValue>& arguments, int permissions)
517 {
518 RefPtr<SQLStatementBackend> statementBackend;
519 statementBackend = SQLStatementBackend::create(statement, sqlStatement, arguments, permissions);
520 enqueueStatementBackend(statementBackend);
521 }
522
notifyDatabaseThreadIsShuttingDown()523 void SQLTransactionBackend::notifyDatabaseThreadIsShuttingDown()
524 {
525 ASSERT(database()->databaseContext()->databaseThread()->isDatabaseThread());
526
527 // If the transaction is in progress, we should roll it back here, since this
528 // is our last opportunity to do something related to this transaction on the
529 // DB thread. Amongst other work, doCleanup() will clear m_sqliteTransaction
530 // which invokes SQLiteTransaction's destructor, which will do the roll back
531 // if necessary.
532 doCleanup();
533 }
534
acquireLock()535 SQLTransactionState SQLTransactionBackend::acquireLock()
536 {
537 m_database->transactionCoordinator()->acquireLock(this);
538 return SQLTransactionState::Idle;
539 }
540
lockAcquired()541 void SQLTransactionBackend::lockAcquired()
542 {
543 m_lockAcquired = true;
544 requestTransitToState(SQLTransactionState::OpenTransactionAndPreflight);
545 }
546
openTransactionAndPreflight()547 SQLTransactionState SQLTransactionBackend::openTransactionAndPreflight()
548 {
549 ASSERT(!m_database->sqliteDatabase().transactionInProgress());
550 ASSERT(m_lockAcquired);
551
552 WTF_LOG(StorageAPI, "Opening and preflighting transaction %p", this);
553
554 // Set the maximum usage for this transaction if this transactions is not read-only
555 if (!m_readOnly)
556 m_database->sqliteDatabase().setMaximumSize(m_database->maximumSize());
557
558 ASSERT(!m_sqliteTransaction);
559 m_sqliteTransaction = adoptPtr(new SQLiteTransaction(m_database->sqliteDatabase(), m_readOnly));
560
561 m_database->resetDeletes();
562 m_database->disableAuthorizer();
563 m_sqliteTransaction->begin();
564 m_database->enableAuthorizer();
565
566 // Spec 4.3.2.1+2: Open a transaction to the database, jumping to the error callback if that fails
567 if (!m_sqliteTransaction->inProgress()) {
568 ASSERT(!m_database->sqliteDatabase().transactionInProgress());
569 m_database->reportStartTransactionResult(2, SQLError::DATABASE_ERR, m_database->sqliteDatabase().lastError());
570 m_transactionError = SQLError::create(SQLError::DATABASE_ERR, "unable to begin transaction",
571 m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
572 m_sqliteTransaction.clear();
573 return nextStateForTransactionError();
574 }
575
576 // Note: We intentionally retrieve the actual version even with an empty expected version.
577 // In multi-process browsers, we take this opportinutiy to update the cached value for
578 // the actual version. In single-process browsers, this is just a map lookup.
579 String actualVersion;
580 if (!m_database->getActualVersionForTransaction(actualVersion)) {
581 m_database->reportStartTransactionResult(3, SQLError::DATABASE_ERR, m_database->sqliteDatabase().lastError());
582 m_transactionError = SQLError::create(SQLError::DATABASE_ERR, "unable to read version",
583 m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
584 m_database->disableAuthorizer();
585 m_sqliteTransaction.clear();
586 m_database->enableAuthorizer();
587 return nextStateForTransactionError();
588 }
589 m_hasVersionMismatch = !m_database->expectedVersion().isEmpty() && (m_database->expectedVersion() != actualVersion);
590
591 // Spec 4.3.2.3: Perform preflight steps, jumping to the error callback if they fail
592 if (m_wrapper && !m_wrapper->performPreflight(this)) {
593 m_database->disableAuthorizer();
594 m_sqliteTransaction.clear();
595 m_database->enableAuthorizer();
596 m_transactionError = m_wrapper->sqlError();
597 if (!m_transactionError) {
598 m_database->reportStartTransactionResult(4, SQLError::UNKNOWN_ERR, 0);
599 m_transactionError = SQLError::create(SQLError::UNKNOWN_ERR, "unknown error occurred during transaction preflight");
600 }
601 return nextStateForTransactionError();
602 }
603
604 // Spec 4.3.2.4: Invoke the transaction callback with the new SQLTransaction object
605 if (m_hasCallback)
606 return SQLTransactionState::DeliverTransactionCallback;
607
608 // If we have no callback to make, skip pass to the state after:
609 return SQLTransactionState::RunStatements;
610 }
611
runStatements()612 SQLTransactionState SQLTransactionBackend::runStatements()
613 {
614 ASSERT(m_lockAcquired);
615 SQLTransactionState nextState;
616
617 // If there is a series of statements queued up that are all successful and have no associated
618 // SQLStatementCallback objects, then we can burn through the queue
619 do {
620 if (m_shouldRetryCurrentStatement && !m_sqliteTransaction->wasRolledBackBySqlite()) {
621 m_shouldRetryCurrentStatement = false;
622 // FIXME - Another place that needs fixing up after <rdar://problem/5628468> is addressed.
623 // See ::openTransactionAndPreflight() for discussion
624
625 // Reset the maximum size here, as it was increased to allow us to retry this statement.
626 // m_shouldRetryCurrentStatement is set to true only when a statement exceeds
627 // the quota, which can happen only in a read-write transaction. Therefore, there
628 // is no need to check here if the transaction is read-write.
629 m_database->sqliteDatabase().setMaximumSize(m_database->maximumSize());
630 } else {
631 // If the current statement has already been run, failed due to quota constraints, and we're not retrying it,
632 // that means it ended in an error. Handle it now
633 if (m_currentStatementBackend && m_currentStatementBackend->lastExecutionFailedDueToQuota()) {
634 return nextStateForCurrentStatementError();
635 }
636
637 // Otherwise, advance to the next statement
638 getNextStatement();
639 }
640 nextState = runCurrentStatementAndGetNextState();
641 } while (nextState == SQLTransactionState::RunStatements);
642
643 return nextState;
644 }
645
getNextStatement()646 void SQLTransactionBackend::getNextStatement()
647 {
648 m_currentStatementBackend = 0;
649
650 MutexLocker locker(m_statementMutex);
651 if (!m_statementQueue.isEmpty())
652 m_currentStatementBackend = m_statementQueue.takeFirst();
653 }
654
runCurrentStatementAndGetNextState()655 SQLTransactionState SQLTransactionBackend::runCurrentStatementAndGetNextState()
656 {
657 if (!m_currentStatementBackend) {
658 // No more statements to run. So move on to the next state.
659 return SQLTransactionState::PostflightAndCommit;
660 }
661
662 m_database->resetAuthorizer();
663
664 if (m_hasVersionMismatch)
665 m_currentStatementBackend->setVersionMismatchedError(Database::from(m_database.get()));
666
667 if (m_currentStatementBackend->execute(m_database.get())) {
668 if (m_database->lastActionChangedDatabase()) {
669 // Flag this transaction as having changed the database for later delegate notification
670 m_modifiedDatabase = true;
671 }
672
673 if (m_currentStatementBackend->hasStatementCallback()) {
674 return SQLTransactionState::DeliverStatementCallback;
675 }
676
677 // If we get here, then the statement doesn't have a callback to invoke.
678 // We can move on to the next statement. Hence, stay in this state.
679 return SQLTransactionState::RunStatements;
680 }
681
682 if (m_currentStatementBackend->lastExecutionFailedDueToQuota()) {
683 return SQLTransactionState::DeliverQuotaIncreaseCallback;
684 }
685
686 return nextStateForCurrentStatementError();
687 }
688
nextStateForCurrentStatementError()689 SQLTransactionState SQLTransactionBackend::nextStateForCurrentStatementError()
690 {
691 // Spec 4.3.2.6.6: error - Call the statement's error callback, but if there was no error callback,
692 // or the transaction was rolled back, jump to the transaction error callback
693 if (m_currentStatementBackend->hasStatementErrorCallback() && !m_sqliteTransaction->wasRolledBackBySqlite())
694 return SQLTransactionState::DeliverStatementCallback;
695
696 m_transactionError = m_currentStatementBackend->sqlError();
697 if (!m_transactionError) {
698 m_database->reportCommitTransactionResult(1, SQLError::DATABASE_ERR, 0);
699 m_transactionError = SQLError::create(SQLError::DATABASE_ERR, "the statement failed to execute");
700 }
701 return nextStateForTransactionError();
702 }
703
postflightAndCommit()704 SQLTransactionState SQLTransactionBackend::postflightAndCommit()
705 {
706 ASSERT(m_lockAcquired);
707
708 // Spec 4.3.2.7: Perform postflight steps, jumping to the error callback if they fail.
709 if (m_wrapper && !m_wrapper->performPostflight(this)) {
710 m_transactionError = m_wrapper->sqlError();
711 if (!m_transactionError) {
712 m_database->reportCommitTransactionResult(3, SQLError::UNKNOWN_ERR, 0);
713 m_transactionError = SQLError::create(SQLError::UNKNOWN_ERR, "unknown error occurred during transaction postflight");
714 }
715 return nextStateForTransactionError();
716 }
717
718 // Spec 4.3.2.7: Commit the transaction, jumping to the error callback if that fails.
719 ASSERT(m_sqliteTransaction);
720
721 m_database->disableAuthorizer();
722 m_sqliteTransaction->commit();
723 m_database->enableAuthorizer();
724
725 // If the commit failed, the transaction will still be marked as "in progress"
726 if (m_sqliteTransaction->inProgress()) {
727 if (m_wrapper)
728 m_wrapper->handleCommitFailedAfterPostflight(this);
729 m_database->reportCommitTransactionResult(4, SQLError::DATABASE_ERR, m_database->sqliteDatabase().lastError());
730 m_transactionError = SQLError::create(SQLError::DATABASE_ERR, "unable to commit transaction",
731 m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
732 return nextStateForTransactionError();
733 }
734
735 m_database->reportCommitTransactionResult(0, -1, 0); // OK
736
737 // Vacuum the database if anything was deleted.
738 if (m_database->hadDeletes())
739 m_database->incrementalVacuumIfNeeded();
740
741 // The commit was successful. If the transaction modified this database, notify the delegates.
742 if (m_modifiedDatabase)
743 m_database->transactionClient()->didCommitWriteTransaction(database());
744
745 // Spec 4.3.2.8: Deliver success callback, if there is one.
746 return SQLTransactionState::DeliverSuccessCallback;
747 }
748
cleanupAndTerminate()749 SQLTransactionState SQLTransactionBackend::cleanupAndTerminate()
750 {
751 ASSERT(m_lockAcquired);
752
753 // Spec 4.3.2.9: End transaction steps. There is no next step.
754 WTF_LOG(StorageAPI, "Transaction %p is complete\n", this);
755 ASSERT(!m_database->sqliteDatabase().transactionInProgress());
756
757 // Phase 5 cleanup. See comment on the SQLTransaction life-cycle above.
758 doCleanup();
759 m_database->inProgressTransactionCompleted();
760 return SQLTransactionState::End;
761 }
762
nextStateForTransactionError()763 SQLTransactionState SQLTransactionBackend::nextStateForTransactionError()
764 {
765 ASSERT(m_transactionError);
766 if (m_hasErrorCallback)
767 return SQLTransactionState::DeliverTransactionErrorCallback;
768
769 // No error callback, so fast-forward to the next state and rollback the
770 // transaction.
771 return SQLTransactionState::CleanupAfterTransactionErrorCallback;
772 }
773
cleanupAfterTransactionErrorCallback()774 SQLTransactionState SQLTransactionBackend::cleanupAfterTransactionErrorCallback()
775 {
776 ASSERT(m_lockAcquired);
777
778 WTF_LOG(StorageAPI, "Transaction %p is complete with an error\n", this);
779 m_database->disableAuthorizer();
780 if (m_sqliteTransaction) {
781 // Spec 4.3.2.10: Rollback the transaction.
782 m_sqliteTransaction->rollback();
783
784 ASSERT(!m_database->sqliteDatabase().transactionInProgress());
785 m_sqliteTransaction.clear();
786 }
787 m_database->enableAuthorizer();
788
789 ASSERT(!m_database->sqliteDatabase().transactionInProgress());
790
791 return SQLTransactionState::CleanupAndTerminate;
792 }
793
794 // requestTransitToState() can be called from the frontend. Hence, it should
795 // NOT be modifying SQLTransactionBackend in general. The only safe field to
796 // modify is m_requestedState which is meant for this purpose.
requestTransitToState(SQLTransactionState nextState)797 void SQLTransactionBackend::requestTransitToState(SQLTransactionState nextState)
798 {
799 WTF_LOG(StorageAPI, "Scheduling %s for transaction %p\n", nameForSQLTransactionState(nextState), this);
800 m_requestedState = nextState;
801 ASSERT(m_requestedState != SQLTransactionState::End);
802 m_database->scheduleTransactionStep(this);
803 }
804
805 // This state function is used as a stub function to plug unimplemented states
806 // in the state dispatch table. They are unimplemented because they should
807 // never be reached in the course of correct execution.
unreachableState()808 SQLTransactionState SQLTransactionBackend::unreachableState()
809 {
810 ASSERT_NOT_REACHED();
811 return SQLTransactionState::End;
812 }
813
sendToFrontendState()814 SQLTransactionState SQLTransactionBackend::sendToFrontendState()
815 {
816 ASSERT(m_nextState != SQLTransactionState::Idle);
817 m_frontend->requestTransitToState(m_nextState);
818 return SQLTransactionState::Idle;
819 }
820
821 } // namespace WebCore
822