SQLTransaction.cpp [plain text]
#include "config.h"
#include "SQLTransaction.h"
#if ENABLE(DATABASE)
#include "ChromeClient.h"
#include "Database.h"
#include "DatabaseAuthorizer.h"
#include "DatabaseDetails.h"
#include "DatabaseThread.h"
#include "ExceptionCode.h"
#include "Logging.h"
#include "Page.h"
#include "PlatformString.h"
#include "ScriptExecutionContext.h"
#include "Settings.h"
#include "SQLError.h"
#include "SQLiteTransaction.h"
#include "SQLResultSet.h"
#include "SQLStatement.h"
#include "SQLStatementCallback.h"
#include "SQLStatementErrorCallback.h"
#include "SQLTransactionClient.h"
#include "SQLTransactionCoordinator.h"
#include "SQLValue.h"
static const int DefaultQuotaSizeIncrease = 1048576;
namespace WebCore {
PassRefPtr<SQLTransaction> SQLTransaction::create(Database* db, PassRefPtr<SQLTransactionCallback> callback, PassRefPtr<SQLTransactionErrorCallback> errorCallback,
PassRefPtr<VoidCallback> successCallback, PassRefPtr<SQLTransactionWrapper> wrapper, bool readOnly)
{
return adoptRef(new SQLTransaction(db, callback, errorCallback, successCallback, wrapper, readOnly));
}
SQLTransaction::SQLTransaction(Database* db, PassRefPtr<SQLTransactionCallback> callback, PassRefPtr<SQLTransactionErrorCallback> errorCallback, PassRefPtr<VoidCallback> successCallback,
PassRefPtr<SQLTransactionWrapper> wrapper, bool readOnly)
: m_nextStep(&SQLTransaction::acquireLock)
, m_executeSqlAllowed(false)
, m_database(db)
, m_wrapper(wrapper)
, m_callback(callback)
, m_successCallback(successCallback)
, m_errorCallback(errorCallback)
, m_shouldRetryCurrentStatement(false)
, m_modifiedDatabase(false)
, m_lockAcquired(false)
, m_readOnly(readOnly)
{
ASSERT(m_database);
}
SQLTransaction::~SQLTransaction()
{
ASSERT(!m_sqliteTransaction);
}
void SQLTransaction::executeSQL(const String& sqlStatement, const Vector<SQLValue>& arguments, PassRefPtr<SQLStatementCallback> callback, PassRefPtr<SQLStatementErrorCallback> callbackError, ExceptionCode& e)
{
if (!m_executeSqlAllowed || m_database->stopped()) {
e = INVALID_STATE_ERR;
return;
}
bool readOnlyMode = m_readOnly;
if (!readOnlyMode) {
if (m_database->scriptExecutionContext()->isDatabaseReadOnly())
readOnlyMode = true;
}
RefPtr<SQLStatement> statement = SQLStatement::create(sqlStatement, arguments, callback, callbackError, readOnlyMode);
if (m_database->deleted())
statement->setDatabaseDeletedError();
if (!m_database->versionMatchesExpected())
statement->setVersionMismatchedError();
enqueueStatement(statement);
}
void SQLTransaction::enqueueStatement(PassRefPtr<SQLStatement> statement)
{
MutexLocker locker(m_statementMutex);
m_statementQueue.append(statement);
}
#ifndef NDEBUG
const char* SQLTransaction::debugStepName(SQLTransaction::TransactionStepMethod step)
{
if (step == &SQLTransaction::acquireLock)
return "acquireLock";
else if (step == &SQLTransaction::openTransactionAndPreflight)
return "openTransactionAndPreflight";
else if (step == &SQLTransaction::runStatements)
return "runStatements";
else if (step == &SQLTransaction::postflightAndCommit)
return "postflightAndCommit";
else if (step == &SQLTransaction::cleanupAfterTransactionErrorCallback)
return "cleanupAfterTransactionErrorCallback";
else if (step == &SQLTransaction::deliverTransactionCallback)
return "deliverTransactionCallback";
else if (step == &SQLTransaction::deliverTransactionErrorCallback)
return "deliverTransactionErrorCallback";
else if (step == &SQLTransaction::deliverStatementCallback)
return "deliverStatementCallback";
else if (step == &SQLTransaction::deliverQuotaIncreaseCallback)
return "deliverQuotaIncreaseCallback";
else if (step == &SQLTransaction::deliverSuccessCallback)
return "deliverSuccessCallback";
else if (step == &SQLTransaction::cleanupAfterSuccessCallback)
return "cleanupAfterSuccessCallback";
else
return "UNKNOWN";
}
#endif
void SQLTransaction::checkAndHandleClosedDatabase()
{
if (!m_database->stopped())
return;
LOG(StorageAPI, "Database was stopped - cancelling work for this transaction");
MutexLocker locker(m_statementMutex);
m_statementQueue.clear();
m_nextStep = 0;
if (currentThread() != database()->scriptExecutionContext()->databaseThread()->getThreadID())
return;
if (m_sqliteTransaction) {
m_sqliteTransaction->stop();
m_sqliteTransaction.clear();
}
if (m_lockAcquired)
m_database->transactionCoordinator()->releaseLock(this);
}
bool SQLTransaction::performNextStep()
{
LOG(StorageAPI, "Step %s\n", debugStepName(m_nextStep));
ASSERT(m_nextStep == &SQLTransaction::acquireLock ||
m_nextStep == &SQLTransaction::openTransactionAndPreflight ||
m_nextStep == &SQLTransaction::runStatements ||
m_nextStep == &SQLTransaction::postflightAndCommit ||
m_nextStep == &SQLTransaction::cleanupAfterSuccessCallback ||
m_nextStep == &SQLTransaction::cleanupAfterTransactionErrorCallback);
checkAndHandleClosedDatabase();
if (m_nextStep)
(this->*m_nextStep)();
return !m_nextStep;
}
void SQLTransaction::performPendingCallback()
{
LOG(StorageAPI, "Callback %s\n", debugStepName(m_nextStep));
ASSERT(m_nextStep == &SQLTransaction::deliverTransactionCallback ||
m_nextStep == &SQLTransaction::deliverTransactionErrorCallback ||
m_nextStep == &SQLTransaction::deliverStatementCallback ||
m_nextStep == &SQLTransaction::deliverQuotaIncreaseCallback ||
m_nextStep == &SQLTransaction::deliverSuccessCallback);
checkAndHandleClosedDatabase();
if (m_nextStep)
(this->*m_nextStep)();
}
void SQLTransaction::notifyDatabaseThreadIsShuttingDown()
{
ASSERT(currentThread() == database()->scriptExecutionContext()->databaseThread()->getThreadID());
m_sqliteTransaction.clear();
}
void SQLTransaction::acquireLock()
{
m_database->transactionCoordinator()->acquireLock(this);
}
void SQLTransaction::lockAcquired()
{
m_lockAcquired = true;
m_nextStep = &SQLTransaction::openTransactionAndPreflight;
LOG(StorageAPI, "Scheduling openTransactionAndPreflight immediately for transaction %p\n", this);
m_database->scheduleTransactionStep(this, true);
}
void SQLTransaction::openTransactionAndPreflight()
{
ASSERT(!m_database->m_sqliteDatabase.transactionInProgress());
ASSERT(m_lockAcquired);
LOG(StorageAPI, "Opening and preflighting transaction %p", this);
if (m_database->deleted()) {
m_transactionError = SQLError::create(0, "unable to open a transaction, because the user deleted the database");
handleTransactionError(false);
return;
}
if (!m_readOnly)
m_database->m_sqliteDatabase.setMaximumSize(m_database->maximumSize());
ASSERT(!m_sqliteTransaction);
m_sqliteTransaction.set(new SQLiteTransaction(m_database->m_sqliteDatabase, m_readOnly));
m_database->m_databaseAuthorizer->disable();
m_sqliteTransaction->begin();
m_database->m_databaseAuthorizer->enable();
if (!m_sqliteTransaction->inProgress()) {
ASSERT(!m_database->m_sqliteDatabase.transactionInProgress());
m_sqliteTransaction.clear();
m_transactionError = SQLError::create(0, "unable to open a transaction to the database");
handleTransactionError(false);
return;
}
if (m_wrapper && !m_wrapper->performPreflight(this)) {
ASSERT(!m_database->m_sqliteDatabase.transactionInProgress());
m_sqliteTransaction.clear();
m_transactionError = m_wrapper->sqlError();
if (!m_transactionError)
m_transactionError = SQLError::create(0, "unknown error occured setting up transaction");
handleTransactionError(false);
return;
}
m_nextStep = &SQLTransaction::deliverTransactionCallback;
LOG(StorageAPI, "Scheduling deliverTransactionCallback for transaction %p\n", this);
m_database->scheduleTransactionCallback(this);
}
void SQLTransaction::deliverTransactionCallback()
{
bool shouldDeliverErrorCallback = false;
if (m_callback) {
m_executeSqlAllowed = true;
shouldDeliverErrorCallback = !m_callback->handleEvent(m_database->scriptExecutionContext(), this);
m_executeSqlAllowed = false;
} else
shouldDeliverErrorCallback = true;
if (shouldDeliverErrorCallback) {
m_transactionError = SQLError::create(0, "the SQLTransactionCallback was null or threw an exception");
deliverTransactionErrorCallback();
} else
scheduleToRunStatements();
}
void SQLTransaction::scheduleToRunStatements()
{
m_nextStep = &SQLTransaction::runStatements;
LOG(StorageAPI, "Scheduling runStatements for transaction %p\n", this);
m_database->scheduleTransactionStep(this);
}
void SQLTransaction::runStatements()
{
ASSERT(m_lockAcquired);
do {
if (m_shouldRetryCurrentStatement && !m_sqliteTransaction->wasRolledBackBySqlite()) {
m_shouldRetryCurrentStatement = false;
m_database->m_sqliteDatabase.setMaximumSize(m_database->maximumSize());
} else {
if (m_currentStatement && m_currentStatement->lastExecutionFailedDueToQuota()) {
handleCurrentStatementError();
break;
}
getNextStatement();
}
} while (runCurrentStatement());
if (!m_currentStatement)
postflightAndCommit();
}
void SQLTransaction::getNextStatement()
{
m_currentStatement = 0;
MutexLocker locker(m_statementMutex);
if (!m_statementQueue.isEmpty()) {
m_currentStatement = m_statementQueue.first();
m_statementQueue.removeFirst();
}
}
bool SQLTransaction::runCurrentStatement()
{
if (!m_currentStatement)
return false;
m_database->m_databaseAuthorizer->reset();
if (m_currentStatement->execute(m_database.get())) {
if (m_database->m_databaseAuthorizer->lastActionChangedDatabase()) {
m_modifiedDatabase = true;
m_database->transactionClient()->didExecuteStatement(this);
}
if (m_currentStatement->hasStatementCallback()) {
m_nextStep = &SQLTransaction::deliverStatementCallback;
LOG(StorageAPI, "Scheduling deliverStatementCallback for transaction %p\n", this);
m_database->scheduleTransactionCallback(this);
return false;
}
return true;
}
if (m_currentStatement->lastExecutionFailedDueToQuota()) {
m_nextStep = &SQLTransaction::deliverQuotaIncreaseCallback;
LOG(StorageAPI, "Scheduling deliverQuotaIncreaseCallback for transaction %p\n", this);
m_database->scheduleTransactionCallback(this);
return false;
}
handleCurrentStatementError();
return false;
}
void SQLTransaction::handleCurrentStatementError()
{
if (m_currentStatement->hasStatementErrorCallback() && !m_sqliteTransaction->wasRolledBackBySqlite()) {
m_nextStep = &SQLTransaction::deliverStatementCallback;
LOG(StorageAPI, "Scheduling deliverStatementCallback for transaction %p\n", this);
m_database->scheduleTransactionCallback(this);
} else {
m_transactionError = m_currentStatement->sqlError();
if (!m_transactionError)
m_transactionError = SQLError::create(1, "the statement failed to execute");
handleTransactionError(false);
}
}
void SQLTransaction::deliverStatementCallback()
{
ASSERT(m_currentStatement);
m_executeSqlAllowed = true;
bool result = m_currentStatement->performCallback(this);
m_executeSqlAllowed = false;
if (result) {
m_transactionError = SQLError::create(0, "the statement callback raised an exception or statement error callback did not return false");
handleTransactionError(true);
} else
scheduleToRunStatements();
}
void SQLTransaction::deliverQuotaIncreaseCallback()
{
ASSERT(m_currentStatement);
ASSERT(!m_shouldRetryCurrentStatement);
m_shouldRetryCurrentStatement = m_database->transactionClient()->didExceedQuota(this);
m_nextStep = &SQLTransaction::runStatements;
LOG(StorageAPI, "Scheduling runStatements for transaction %p\n", this);
m_database->scheduleTransactionStep(this);
}
void SQLTransaction::postflightAndCommit()
{
ASSERT(m_lockAcquired);
if (m_wrapper && !m_wrapper->performPostflight(this)) {
m_transactionError = m_wrapper->sqlError();
if (!m_transactionError)
m_transactionError = SQLError::create(0, "unknown error occured setting up transaction");
handleTransactionError(false);
return;
}
ASSERT(m_sqliteTransaction);
m_database->m_databaseAuthorizer->disable();
m_sqliteTransaction->commit();
m_database->m_databaseAuthorizer->enable();
if (m_sqliteTransaction->inProgress()) {
m_transactionError = SQLError::create(0, "failed to commit the transaction");
handleTransactionError(false);
return;
}
m_database->incrementalVacuumIfNeeded();
if (m_modifiedDatabase)
m_database->transactionClient()->didCommitTransaction(this);
m_callback = 0;
m_errorCallback = 0;
if (m_successCallback) {
m_nextStep = &SQLTransaction::deliverSuccessCallback;
LOG(StorageAPI, "Scheduling deliverSuccessCallback for transaction %p\n", this);
m_database->scheduleTransactionCallback(this);
} else
cleanupAfterSuccessCallback();
}
void SQLTransaction::deliverSuccessCallback()
{
ASSERT(m_successCallback);
m_successCallback->handleEvent();
m_successCallback = 0;
m_nextStep = &SQLTransaction::cleanupAfterSuccessCallback;
LOG(StorageAPI, "Scheduling cleanupAfterSuccessCallback for transaction %p\n", this);
m_database->scheduleTransactionStep(this);
}
void SQLTransaction::cleanupAfterSuccessCallback()
{
ASSERT(m_lockAcquired);
LOG(StorageAPI, "Transaction %p is complete\n", this);
ASSERT(!m_database->m_sqliteDatabase.transactionInProgress());
m_sqliteTransaction.clear();
m_nextStep = 0;
m_database->transactionCoordinator()->releaseLock(this);
}
void SQLTransaction::handleTransactionError(bool inCallback)
{
if (m_errorCallback) {
if (inCallback)
deliverTransactionErrorCallback();
else {
m_nextStep = &SQLTransaction::deliverTransactionErrorCallback;
LOG(StorageAPI, "Scheduling deliverTransactionErrorCallback for transaction %p\n", this);
m_database->scheduleTransactionCallback(this);
}
return;
}
if (inCallback) {
m_nextStep = &SQLTransaction::cleanupAfterTransactionErrorCallback;
LOG(StorageAPI, "Scheduling cleanupAfterTransactionErrorCallback for transaction %p\n", this);
m_database->scheduleTransactionStep(this);
} else {
cleanupAfterTransactionErrorCallback();
}
}
void SQLTransaction::deliverTransactionErrorCallback()
{
ASSERT(m_transactionError);
if (m_errorCallback)
m_errorCallback->handleEvent(m_database->scriptExecutionContext(), m_transactionError.get());
m_nextStep = &SQLTransaction::cleanupAfterTransactionErrorCallback;
LOG(StorageAPI, "Scheduling cleanupAfterTransactionErrorCallback for transaction %p\n", this);
m_database->scheduleTransactionStep(this);
}
void SQLTransaction::cleanupAfterTransactionErrorCallback()
{
ASSERT(m_lockAcquired);
m_database->m_databaseAuthorizer->disable();
if (m_sqliteTransaction) {
m_sqliteTransaction->rollback();
ASSERT(!m_database->m_sqliteDatabase.transactionInProgress());
m_sqliteTransaction.clear();
}
m_database->m_databaseAuthorizer->enable();
{
MutexLocker locker(m_statementMutex);
m_statementQueue.clear();
}
LOG(StorageAPI, "Transaction %p is complete with an error\n", this);
ASSERT(!m_database->m_sqliteDatabase.transactionInProgress());
m_nextStep = 0;
m_callback = 0;
m_errorCallback = 0;
m_database->transactionCoordinator()->releaseLock(this);
}
}
#endif // ENABLE(DATABASE)