0
|
1 |
/*
|
|
2 |
* Copyright (C) 2007, 2008 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 "SQLTransaction.h"
|
|
31 |
|
|
32 |
#if ENABLE(DATABASE)
|
|
33 |
|
|
34 |
#include "ChromeClient.h"
|
|
35 |
#include "Database.h"
|
|
36 |
#include "DatabaseAuthorizer.h"
|
|
37 |
#include "DatabaseDetails.h"
|
|
38 |
#include "Document.h"
|
|
39 |
#include "ExceptionCode.h"
|
|
40 |
#include "Logging.h"
|
|
41 |
#include "Page.h"
|
|
42 |
#include "PlatformString.h"
|
|
43 |
#include "Settings.h"
|
|
44 |
#include "SQLError.h"
|
|
45 |
#include "SQLiteTransaction.h"
|
|
46 |
#include "SQLResultSet.h"
|
|
47 |
#include "SQLStatement.h"
|
|
48 |
#include "SQLStatementCallback.h"
|
|
49 |
#include "SQLStatementErrorCallback.h"
|
|
50 |
#include "SQLTransactionClient.h"
|
|
51 |
#include "SQLTransactionCoordinator.h"
|
|
52 |
#include "SQLValue.h"
|
|
53 |
|
|
54 |
// There's no way of knowing exactly how much more space will be required when a statement hits the quota limit.
|
|
55 |
// For now, we'll arbitrarily choose currentQuota + 1mb.
|
|
56 |
// In the future we decide to track if a size increase wasn't enough, and ask for larger-and-larger increases until its enough.
|
|
57 |
static const int DefaultQuotaSizeIncrease = 1048576;
|
|
58 |
|
|
59 |
namespace WebCore {
|
|
60 |
|
|
61 |
PassRefPtr<SQLTransaction> SQLTransaction::create(Database* db, PassRefPtr<SQLTransactionCallback> callback, PassRefPtr<SQLTransactionErrorCallback> errorCallback,
|
|
62 |
PassRefPtr<VoidCallback> successCallback, PassRefPtr<SQLTransactionWrapper> wrapper, bool readOnly)
|
|
63 |
{
|
|
64 |
return adoptRef(new SQLTransaction(db, callback, errorCallback, successCallback, wrapper, readOnly));
|
|
65 |
}
|
|
66 |
|
|
67 |
SQLTransaction::SQLTransaction(Database* db, PassRefPtr<SQLTransactionCallback> callback, PassRefPtr<SQLTransactionErrorCallback> errorCallback, PassRefPtr<VoidCallback> successCallback,
|
|
68 |
PassRefPtr<SQLTransactionWrapper> wrapper, bool readOnly)
|
|
69 |
: m_nextStep(&SQLTransaction::acquireLock)
|
|
70 |
, m_executeSqlAllowed(false)
|
|
71 |
, m_database(db)
|
|
72 |
, m_wrapper(wrapper)
|
|
73 |
, m_callback(callback)
|
|
74 |
, m_successCallback(successCallback)
|
|
75 |
, m_errorCallback(errorCallback)
|
|
76 |
, m_shouldRetryCurrentStatement(false)
|
|
77 |
, m_modifiedDatabase(false)
|
|
78 |
, m_lockAcquired(false)
|
|
79 |
, m_readOnly(readOnly)
|
|
80 |
{
|
|
81 |
ASSERT(m_database);
|
|
82 |
}
|
|
83 |
|
|
84 |
SQLTransaction::~SQLTransaction()
|
|
85 |
{
|
|
86 |
}
|
|
87 |
|
|
88 |
void SQLTransaction::executeSQL(const String& sqlStatement, const Vector<SQLValue>& arguments, PassRefPtr<SQLStatementCallback> callback, PassRefPtr<SQLStatementErrorCallback> callbackError, ExceptionCode& e)
|
|
89 |
{
|
|
90 |
if (!m_executeSqlAllowed || m_database->stopped()) {
|
|
91 |
e = INVALID_STATE_ERR;
|
|
92 |
return;
|
|
93 |
}
|
|
94 |
|
|
95 |
bool readOnlyMode = m_readOnly;
|
|
96 |
if (!readOnlyMode) {
|
|
97 |
Page* page = m_database->document()->page();
|
|
98 |
if (!page || page->settings()->privateBrowsingEnabled())
|
|
99 |
readOnlyMode = true;
|
|
100 |
}
|
|
101 |
|
|
102 |
RefPtr<SQLStatement> statement = SQLStatement::create(sqlStatement, arguments, callback, callbackError, readOnlyMode);
|
|
103 |
|
|
104 |
if (m_database->deleted())
|
|
105 |
statement->setDatabaseDeletedError();
|
|
106 |
|
|
107 |
if (!m_database->versionMatchesExpected())
|
|
108 |
statement->setVersionMismatchedError();
|
|
109 |
|
|
110 |
enqueueStatement(statement);
|
|
111 |
}
|
|
112 |
|
|
113 |
void SQLTransaction::enqueueStatement(PassRefPtr<SQLStatement> statement)
|
|
114 |
{
|
|
115 |
MutexLocker locker(m_statementMutex);
|
|
116 |
m_statementQueue.append(statement);
|
|
117 |
}
|
|
118 |
|
|
119 |
#ifndef NDEBUG
|
|
120 |
const char* SQLTransaction::debugStepName(SQLTransaction::TransactionStepMethod step)
|
|
121 |
{
|
|
122 |
if (step == &SQLTransaction::acquireLock)
|
|
123 |
return "acquireLock";
|
|
124 |
else if (step == &SQLTransaction::openTransactionAndPreflight)
|
|
125 |
return "openTransactionAndPreflight";
|
|
126 |
else if (step == &SQLTransaction::runStatements)
|
|
127 |
return "runStatements";
|
|
128 |
else if (step == &SQLTransaction::postflightAndCommit)
|
|
129 |
return "postflightAndCommit";
|
|
130 |
else if (step == &SQLTransaction::cleanupAfterTransactionErrorCallback)
|
|
131 |
return "cleanupAfterTransactionErrorCallback";
|
|
132 |
else if (step == &SQLTransaction::deliverTransactionCallback)
|
|
133 |
return "deliverTransactionCallback";
|
|
134 |
else if (step == &SQLTransaction::deliverTransactionErrorCallback)
|
|
135 |
return "deliverTransactionErrorCallback";
|
|
136 |
else if (step == &SQLTransaction::deliverStatementCallback)
|
|
137 |
return "deliverStatementCallback";
|
|
138 |
else if (step == &SQLTransaction::deliverQuotaIncreaseCallback)
|
|
139 |
return "deliverQuotaIncreaseCallback";
|
|
140 |
else if (step == &SQLTransaction::deliverSuccessCallback)
|
|
141 |
return "deliverSuccessCallback";
|
|
142 |
else if (step == &SQLTransaction::cleanupAfterSuccessCallback)
|
|
143 |
return "cleanupAfterSuccessCallback";
|
|
144 |
else
|
|
145 |
return "UNKNOWN";
|
|
146 |
}
|
|
147 |
#endif
|
|
148 |
|
|
149 |
void SQLTransaction::checkAndHandleClosedDatabase()
|
|
150 |
{
|
|
151 |
if (!m_database->stopped())
|
|
152 |
return;
|
|
153 |
|
|
154 |
// If the database was stopped, don't do anything and cancel queued work
|
|
155 |
LOG(StorageAPI, "Database was stopped - cancelling work for this transaction");
|
|
156 |
MutexLocker locker(m_statementMutex);
|
|
157 |
m_statementQueue.clear();
|
|
158 |
m_nextStep = 0;
|
|
159 |
|
|
160 |
// The current SQLite transaction should be stopped, as well
|
|
161 |
if (m_sqliteTransaction) {
|
|
162 |
m_sqliteTransaction->stop();
|
|
163 |
m_sqliteTransaction.clear();
|
|
164 |
}
|
|
165 |
|
|
166 |
if (m_lockAcquired)
|
|
167 |
m_database->transactionCoordinator()->releaseLock(this);
|
|
168 |
}
|
|
169 |
|
|
170 |
|
|
171 |
bool SQLTransaction::performNextStep()
|
|
172 |
{
|
|
173 |
LOG(StorageAPI, "Step %s\n", debugStepName(m_nextStep));
|
|
174 |
|
|
175 |
ASSERT(m_nextStep == &SQLTransaction::acquireLock ||
|
|
176 |
m_nextStep == &SQLTransaction::openTransactionAndPreflight ||
|
|
177 |
m_nextStep == &SQLTransaction::runStatements ||
|
|
178 |
m_nextStep == &SQLTransaction::postflightAndCommit ||
|
|
179 |
m_nextStep == &SQLTransaction::cleanupAfterSuccessCallback ||
|
|
180 |
m_nextStep == &SQLTransaction::cleanupAfterTransactionErrorCallback);
|
|
181 |
|
|
182 |
checkAndHandleClosedDatabase();
|
|
183 |
|
|
184 |
if (m_nextStep)
|
|
185 |
(this->*m_nextStep)();
|
|
186 |
|
|
187 |
// If there is no nextStep after performing the above step, the transaction is complete
|
|
188 |
return !m_nextStep;
|
|
189 |
}
|
|
190 |
|
|
191 |
void SQLTransaction::performPendingCallback()
|
|
192 |
{
|
|
193 |
LOG(StorageAPI, "Callback %s\n", debugStepName(m_nextStep));
|
|
194 |
|
|
195 |
ASSERT(m_nextStep == &SQLTransaction::deliverTransactionCallback ||
|
|
196 |
m_nextStep == &SQLTransaction::deliverTransactionErrorCallback ||
|
|
197 |
m_nextStep == &SQLTransaction::deliverStatementCallback ||
|
|
198 |
m_nextStep == &SQLTransaction::deliverQuotaIncreaseCallback ||
|
|
199 |
m_nextStep == &SQLTransaction::deliverSuccessCallback);
|
|
200 |
|
|
201 |
checkAndHandleClosedDatabase();
|
|
202 |
|
|
203 |
if (m_nextStep)
|
|
204 |
(this->*m_nextStep)();
|
|
205 |
}
|
|
206 |
|
|
207 |
void SQLTransaction::acquireLock()
|
|
208 |
{
|
|
209 |
m_database->transactionCoordinator()->acquireLock(this);
|
|
210 |
}
|
|
211 |
|
|
212 |
void SQLTransaction::lockAcquired()
|
|
213 |
{
|
|
214 |
m_lockAcquired = true;
|
|
215 |
m_nextStep = &SQLTransaction::openTransactionAndPreflight;
|
|
216 |
LOG(StorageAPI, "Scheduling openTransactionAndPreflight immediately for transaction %p\n", this);
|
|
217 |
m_database->scheduleTransactionStep(this, true);
|
|
218 |
}
|
|
219 |
|
|
220 |
void SQLTransaction::openTransactionAndPreflight()
|
|
221 |
{
|
|
222 |
ASSERT(!m_database->m_sqliteDatabase.transactionInProgress());
|
|
223 |
ASSERT(m_lockAcquired);
|
|
224 |
|
|
225 |
LOG(StorageAPI, "Opening and preflighting transaction %p", this);
|
|
226 |
|
|
227 |
// If the database was deleted, jump to the error callback
|
|
228 |
if (m_database->deleted()) {
|
|
229 |
m_transactionError = SQLError::create(0, "unable to open a transaction, because the user deleted the database");
|
|
230 |
handleTransactionError(false);
|
|
231 |
return;
|
|
232 |
}
|
|
233 |
|
|
234 |
// Set the maximum usage for this transaction if this transactions is not read-only
|
|
235 |
if (!m_readOnly)
|
|
236 |
m_database->m_sqliteDatabase.setMaximumSize(m_database->maximumSize());
|
|
237 |
|
|
238 |
ASSERT(!m_sqliteTransaction);
|
|
239 |
m_sqliteTransaction.set(new SQLiteTransaction(m_database->m_sqliteDatabase, m_readOnly));
|
|
240 |
|
|
241 |
m_database->m_databaseAuthorizer->disable();
|
|
242 |
m_sqliteTransaction->begin();
|
|
243 |
m_database->m_databaseAuthorizer->enable();
|
|
244 |
|
|
245 |
// Transaction Steps 1+2 - Open a transaction to the database, jumping to the error callback if that fails
|
|
246 |
if (!m_sqliteTransaction->inProgress()) {
|
|
247 |
ASSERT(!m_database->m_sqliteDatabase.transactionInProgress());
|
|
248 |
m_sqliteTransaction.clear();
|
|
249 |
m_transactionError = SQLError::create(0, "unable to open a transaction to the database");
|
|
250 |
handleTransactionError(false);
|
|
251 |
return;
|
|
252 |
}
|
|
253 |
|
|
254 |
// Transaction Steps 3 - Peform preflight steps, jumping to the error callback if they fail
|
|
255 |
if (m_wrapper && !m_wrapper->performPreflight(this)) {
|
|
256 |
ASSERT(!m_database->m_sqliteDatabase.transactionInProgress());
|
|
257 |
m_sqliteTransaction.clear();
|
|
258 |
m_transactionError = m_wrapper->sqlError();
|
|
259 |
if (!m_transactionError)
|
|
260 |
m_transactionError = SQLError::create(0, "unknown error occured setting up transaction");
|
|
261 |
|
|
262 |
handleTransactionError(false);
|
|
263 |
return;
|
|
264 |
}
|
|
265 |
|
|
266 |
// Transaction Step 4 - Invoke the transaction callback with the new SQLTransaction object
|
|
267 |
m_nextStep = &SQLTransaction::deliverTransactionCallback;
|
|
268 |
LOG(StorageAPI, "Scheduling deliverTransactionCallback for transaction %p\n", this);
|
|
269 |
m_database->scheduleTransactionCallback(this);
|
|
270 |
}
|
|
271 |
|
|
272 |
void SQLTransaction::deliverTransactionCallback()
|
|
273 |
{
|
|
274 |
bool shouldDeliverErrorCallback = false;
|
|
275 |
|
|
276 |
if (m_callback) {
|
|
277 |
m_executeSqlAllowed = true;
|
|
278 |
m_callback->handleEvent(this, shouldDeliverErrorCallback);
|
|
279 |
m_executeSqlAllowed = false;
|
|
280 |
} else
|
|
281 |
shouldDeliverErrorCallback = true;
|
|
282 |
|
|
283 |
// Transaction Step 5 - If the transaction callback was null or raised an exception, jump to the error callback
|
|
284 |
if (shouldDeliverErrorCallback) {
|
|
285 |
m_transactionError = SQLError::create(0, "the SQLTransactionCallback was null or threw an exception");
|
|
286 |
deliverTransactionErrorCallback();
|
|
287 |
} else
|
|
288 |
scheduleToRunStatements();
|
|
289 |
}
|
|
290 |
|
|
291 |
void SQLTransaction::scheduleToRunStatements()
|
|
292 |
{
|
|
293 |
m_nextStep = &SQLTransaction::runStatements;
|
|
294 |
LOG(StorageAPI, "Scheduling runStatements for transaction %p\n", this);
|
|
295 |
m_database->scheduleTransactionStep(this);
|
|
296 |
}
|
|
297 |
|
|
298 |
void SQLTransaction::runStatements()
|
|
299 |
{
|
|
300 |
ASSERT(m_lockAcquired);
|
|
301 |
|
|
302 |
// If there is a series of statements queued up that are all successful and have no associated
|
|
303 |
// SQLStatementCallback objects, then we can burn through the queue
|
|
304 |
do {
|
|
305 |
if (m_shouldRetryCurrentStatement) {
|
|
306 |
m_shouldRetryCurrentStatement = false;
|
|
307 |
// FIXME - Another place that needs fixing up after <rdar://problem/5628468> is addressed.
|
|
308 |
// See ::openTransactionAndPreflight() for discussion
|
|
309 |
|
|
310 |
// Reset the maximum size here, as it was increased to allow us to retry this statement.
|
|
311 |
// m_shouldRetryCurrentStatement is set to true only when a statement exceeds
|
|
312 |
// the quota, which can happen only in a read-write transaction. Therefore, there
|
|
313 |
// is no need to check here if the transaction is read-write.
|
|
314 |
m_database->m_sqliteDatabase.setMaximumSize(m_database->maximumSize());
|
|
315 |
} else {
|
|
316 |
// If the current statement has already been run, failed due to quota constraints, and we're not retrying it,
|
|
317 |
// that means it ended in an error. Handle it now
|
|
318 |
if (m_currentStatement && m_currentStatement->lastExecutionFailedDueToQuota()) {
|
|
319 |
handleCurrentStatementError();
|
|
320 |
break;
|
|
321 |
}
|
|
322 |
|
|
323 |
// Otherwise, advance to the next statement
|
|
324 |
getNextStatement();
|
|
325 |
}
|
|
326 |
} while (runCurrentStatement());
|
|
327 |
|
|
328 |
// If runCurrentStatement() returned false, that means either there was no current statement to run,
|
|
329 |
// or the current statement requires a callback to complete. In the later case, it also scheduled
|
|
330 |
// the callback or performed any other additional work so we can return
|
|
331 |
if (!m_currentStatement)
|
|
332 |
postflightAndCommit();
|
|
333 |
}
|
|
334 |
|
|
335 |
void SQLTransaction::getNextStatement()
|
|
336 |
{
|
|
337 |
m_currentStatement = 0;
|
|
338 |
|
|
339 |
MutexLocker locker(m_statementMutex);
|
|
340 |
if (!m_statementQueue.isEmpty()) {
|
|
341 |
m_currentStatement = m_statementQueue.first();
|
|
342 |
m_statementQueue.removeFirst();
|
|
343 |
}
|
|
344 |
}
|
|
345 |
|
|
346 |
bool SQLTransaction::runCurrentStatement()
|
|
347 |
{
|
|
348 |
if (!m_currentStatement)
|
|
349 |
return false;
|
|
350 |
|
|
351 |
m_database->m_databaseAuthorizer->reset();
|
|
352 |
|
|
353 |
if (m_currentStatement->execute(m_database.get())) {
|
|
354 |
if (m_database->m_databaseAuthorizer->lastActionChangedDatabase()) {
|
|
355 |
// Flag this transaction as having changed the database for later delegate notification
|
|
356 |
m_modifiedDatabase = true;
|
|
357 |
// Also dirty the size of this database file for calculating quota usage
|
|
358 |
m_database->transactionClient()->didExecuteStatement(this);
|
|
359 |
}
|
|
360 |
|
|
361 |
if (m_currentStatement->hasStatementCallback()) {
|
|
362 |
m_nextStep = &SQLTransaction::deliverStatementCallback;
|
|
363 |
LOG(StorageAPI, "Scheduling deliverStatementCallback for transaction %p\n", this);
|
|
364 |
m_database->scheduleTransactionCallback(this);
|
|
365 |
return false;
|
|
366 |
}
|
|
367 |
return true;
|
|
368 |
}
|
|
369 |
|
|
370 |
if (m_currentStatement->lastExecutionFailedDueToQuota()) {
|
|
371 |
m_nextStep = &SQLTransaction::deliverQuotaIncreaseCallback;
|
|
372 |
LOG(StorageAPI, "Scheduling deliverQuotaIncreaseCallback for transaction %p\n", this);
|
|
373 |
m_database->scheduleTransactionCallback(this);
|
|
374 |
return false;
|
|
375 |
}
|
|
376 |
|
|
377 |
handleCurrentStatementError();
|
|
378 |
|
|
379 |
return false;
|
|
380 |
}
|
|
381 |
|
|
382 |
void SQLTransaction::handleCurrentStatementError()
|
|
383 |
{
|
|
384 |
// Transaction Steps 6.error - Call the statement's error callback, but if there was no error callback,
|
|
385 |
// jump to the transaction error callback
|
|
386 |
if (m_currentStatement->hasStatementErrorCallback()) {
|
|
387 |
m_nextStep = &SQLTransaction::deliverStatementCallback;
|
|
388 |
LOG(StorageAPI, "Scheduling deliverStatementCallback for transaction %p\n", this);
|
|
389 |
m_database->scheduleTransactionCallback(this);
|
|
390 |
} else {
|
|
391 |
m_transactionError = m_currentStatement->sqlError();
|
|
392 |
if (!m_transactionError)
|
|
393 |
m_transactionError = SQLError::create(1, "the statement failed to execute");
|
|
394 |
handleTransactionError(false);
|
|
395 |
}
|
|
396 |
}
|
|
397 |
|
|
398 |
void SQLTransaction::deliverStatementCallback()
|
|
399 |
{
|
|
400 |
ASSERT(m_currentStatement);
|
|
401 |
|
|
402 |
// Transaction Step 6.6 and 6.3(error) - If the statement callback went wrong, jump to the transaction error callback
|
|
403 |
// Otherwise, continue to loop through the statement queue
|
|
404 |
m_executeSqlAllowed = true;
|
|
405 |
bool result = m_currentStatement->performCallback(this);
|
|
406 |
m_executeSqlAllowed = false;
|
|
407 |
|
|
408 |
if (result) {
|
|
409 |
m_transactionError = SQLError::create(0, "the statement callback raised an exception or statement error callback did not return false");
|
|
410 |
handleTransactionError(true);
|
|
411 |
} else
|
|
412 |
scheduleToRunStatements();
|
|
413 |
}
|
|
414 |
|
|
415 |
void SQLTransaction::deliverQuotaIncreaseCallback()
|
|
416 |
{
|
|
417 |
ASSERT(m_currentStatement);
|
|
418 |
ASSERT(!m_shouldRetryCurrentStatement);
|
|
419 |
|
|
420 |
m_shouldRetryCurrentStatement = m_database->transactionClient()->didExceedQuota(this);
|
|
421 |
|
|
422 |
m_nextStep = &SQLTransaction::runStatements;
|
|
423 |
LOG(StorageAPI, "Scheduling runStatements for transaction %p\n", this);
|
|
424 |
m_database->scheduleTransactionStep(this);
|
|
425 |
}
|
|
426 |
|
|
427 |
void SQLTransaction::postflightAndCommit()
|
|
428 |
{
|
|
429 |
ASSERT(m_lockAcquired);
|
|
430 |
|
|
431 |
// Transaction Step 7 - Peform postflight steps, jumping to the error callback if they fail
|
|
432 |
if (m_wrapper && !m_wrapper->performPostflight(this)) {
|
|
433 |
m_transactionError = m_wrapper->sqlError();
|
|
434 |
if (!m_transactionError)
|
|
435 |
m_transactionError = SQLError::create(0, "unknown error occured setting up transaction");
|
|
436 |
handleTransactionError(false);
|
|
437 |
return;
|
|
438 |
}
|
|
439 |
|
|
440 |
// Transacton Step 8+9 - Commit the transaction, jumping to the error callback if that fails
|
|
441 |
ASSERT(m_sqliteTransaction);
|
|
442 |
|
|
443 |
m_database->m_databaseAuthorizer->disable();
|
|
444 |
m_sqliteTransaction->commit();
|
|
445 |
m_database->m_databaseAuthorizer->enable();
|
|
446 |
|
|
447 |
// If the commit failed, the transaction will still be marked as "in progress"
|
|
448 |
if (m_sqliteTransaction->inProgress()) {
|
|
449 |
m_transactionError = SQLError::create(0, "failed to commit the transaction");
|
|
450 |
handleTransactionError(false);
|
|
451 |
return;
|
|
452 |
}
|
|
453 |
|
|
454 |
// The commit was successful, notify the delegates if the transaction modified this database
|
|
455 |
if (m_modifiedDatabase)
|
|
456 |
m_database->transactionClient()->didCommitTransaction(this);
|
|
457 |
|
|
458 |
// Now release our unneeded callbacks, to break reference cycles.
|
|
459 |
m_callback = 0;
|
|
460 |
m_errorCallback = 0;
|
|
461 |
|
|
462 |
// Transaction Step 10 - Deliver success callback, if there is one
|
|
463 |
if (m_successCallback) {
|
|
464 |
m_nextStep = &SQLTransaction::deliverSuccessCallback;
|
|
465 |
LOG(StorageAPI, "Scheduling deliverSuccessCallback for transaction %p\n", this);
|
|
466 |
m_database->scheduleTransactionCallback(this);
|
|
467 |
} else
|
|
468 |
cleanupAfterSuccessCallback();
|
|
469 |
}
|
|
470 |
|
|
471 |
void SQLTransaction::deliverSuccessCallback()
|
|
472 |
{
|
|
473 |
// Transaction Step 10 - Deliver success callback
|
|
474 |
ASSERT(m_successCallback);
|
|
475 |
m_successCallback->handleEvent();
|
|
476 |
|
|
477 |
// Release the last callback to break reference cycle
|
|
478 |
m_successCallback = 0;
|
|
479 |
|
|
480 |
// Schedule a "post-success callback" step to return control to the database thread in case there
|
|
481 |
// are further transactions queued up for this Database
|
|
482 |
m_nextStep = &SQLTransaction::cleanupAfterSuccessCallback;
|
|
483 |
LOG(StorageAPI, "Scheduling cleanupAfterSuccessCallback for transaction %p\n", this);
|
|
484 |
m_database->scheduleTransactionStep(this);
|
|
485 |
}
|
|
486 |
|
|
487 |
void SQLTransaction::cleanupAfterSuccessCallback()
|
|
488 |
{
|
|
489 |
ASSERT(m_lockAcquired);
|
|
490 |
|
|
491 |
// Transaction Step 11 - End transaction steps
|
|
492 |
// There is no next step
|
|
493 |
LOG(StorageAPI, "Transaction %p is complete\n", this);
|
|
494 |
ASSERT(!m_database->m_sqliteDatabase.transactionInProgress());
|
|
495 |
m_nextStep = 0;
|
|
496 |
|
|
497 |
// Release the lock on this database
|
|
498 |
m_database->transactionCoordinator()->releaseLock(this);
|
|
499 |
}
|
|
500 |
|
|
501 |
void SQLTransaction::handleTransactionError(bool inCallback)
|
|
502 |
{
|
|
503 |
if (m_errorCallback) {
|
|
504 |
if (inCallback)
|
|
505 |
deliverTransactionErrorCallback();
|
|
506 |
else {
|
|
507 |
m_nextStep = &SQLTransaction::deliverTransactionErrorCallback;
|
|
508 |
LOG(StorageAPI, "Scheduling deliverTransactionErrorCallback for transaction %p\n", this);
|
|
509 |
m_database->scheduleTransactionCallback(this);
|
|
510 |
}
|
|
511 |
return;
|
|
512 |
}
|
|
513 |
|
|
514 |
// No error callback, so fast-forward to:
|
|
515 |
// Transaction Step 12 - Rollback the transaction.
|
|
516 |
if (inCallback) {
|
|
517 |
m_nextStep = &SQLTransaction::cleanupAfterTransactionErrorCallback;
|
|
518 |
LOG(StorageAPI, "Scheduling cleanupAfterTransactionErrorCallback for transaction %p\n", this);
|
|
519 |
m_database->scheduleTransactionStep(this);
|
|
520 |
} else {
|
|
521 |
cleanupAfterTransactionErrorCallback();
|
|
522 |
}
|
|
523 |
}
|
|
524 |
|
|
525 |
void SQLTransaction::deliverTransactionErrorCallback()
|
|
526 |
{
|
|
527 |
ASSERT(m_transactionError);
|
|
528 |
|
|
529 |
// Transaction Step 12 - If exists, invoke error callback with the last
|
|
530 |
// error to have occurred in this transaction.
|
|
531 |
if (m_errorCallback)
|
|
532 |
m_errorCallback->handleEvent(m_transactionError.get());
|
|
533 |
|
|
534 |
m_nextStep = &SQLTransaction::cleanupAfterTransactionErrorCallback;
|
|
535 |
LOG(StorageAPI, "Scheduling cleanupAfterTransactionErrorCallback for transaction %p\n", this);
|
|
536 |
m_database->scheduleTransactionStep(this);
|
|
537 |
}
|
|
538 |
|
|
539 |
void SQLTransaction::cleanupAfterTransactionErrorCallback()
|
|
540 |
{
|
|
541 |
ASSERT(m_lockAcquired);
|
|
542 |
|
|
543 |
m_database->m_databaseAuthorizer->disable();
|
|
544 |
if (m_sqliteTransaction) {
|
|
545 |
// Transaction Step 12 - Rollback the transaction.
|
|
546 |
m_sqliteTransaction->rollback();
|
|
547 |
|
|
548 |
ASSERT(!m_database->m_sqliteDatabase.transactionInProgress());
|
|
549 |
m_sqliteTransaction.clear();
|
|
550 |
}
|
|
551 |
m_database->m_databaseAuthorizer->enable();
|
|
552 |
|
|
553 |
// Transaction Step 12 - Any still-pending statements in the transaction are discarded.
|
|
554 |
{
|
|
555 |
MutexLocker locker(m_statementMutex);
|
|
556 |
m_statementQueue.clear();
|
|
557 |
}
|
|
558 |
|
|
559 |
// Transaction is complete! There is no next step
|
|
560 |
LOG(StorageAPI, "Transaction %p is complete with an error\n", this);
|
|
561 |
ASSERT(!m_database->m_sqliteDatabase.transactionInProgress());
|
|
562 |
m_nextStep = 0;
|
|
563 |
|
|
564 |
// Now release our callbacks, to break reference cycles.
|
|
565 |
m_callback = 0;
|
|
566 |
m_errorCallback = 0;
|
|
567 |
|
|
568 |
// Now release the lock on this database
|
|
569 |
m_database->transactionCoordinator()->releaseLock(this);
|
|
570 |
}
|
|
571 |
|
|
572 |
} // namespace WebCore
|
|
573 |
|
|
574 |
#endif // ENABLE(DATABASE)
|