This source file includes following definitions.
- m_transactionClient
- rollbackIfInProgress
- trace
- executeSQL
- begin
- execute
- commit
- rollback
#include "config.h"
#include "modules/webdatabase/SQLTransactionBackendSync.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
#include "core/dom/ExecutionContext.h"
#include "modules/webdatabase/sqlite/SQLValue.h"
#include "modules/webdatabase/sqlite/SQLiteTransaction.h"
#include "modules/webdatabase/DatabaseAuthorizer.h"
#include "modules/webdatabase/DatabaseContext.h"
#include "modules/webdatabase/DatabaseSync.h"
#include "modules/webdatabase/SQLError.h"
#include "modules/webdatabase/SQLResultSet.h"
#include "modules/webdatabase/SQLStatementSync.h"
#include "modules/webdatabase/SQLTransactionClient.h"
#include "modules/webdatabase/SQLTransactionSync.h"
#include "modules/webdatabase/SQLTransactionSyncCallback.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefPtr.h"
namespace WebCore {
SQLTransactionBackendSync::SQLTransactionBackendSync(DatabaseSync* db, PassOwnPtr<SQLTransactionSyncCallback> callback, bool readOnly)
: m_database(db)
, m_callback(callback)
, m_readOnly(readOnly)
, m_hasVersionMismatch(false)
, m_modifiedDatabase(false)
, m_transactionClient(adoptPtr(new SQLTransactionClient()))
{
ASSERT(m_database->executionContext()->isContextThread());
}
void SQLTransactionBackendSync::rollbackIfInProgress()
{
ASSERT(!m_database->executionContext() || m_database->executionContext()->isContextThread());
if (m_sqliteTransaction && m_sqliteTransaction->inProgress())
rollback();
}
SQLTransactionBackendSync::~SQLTransactionBackendSync()
{
#if ENABLE(OILPAN)
ASSERT(!m_sqliteTransaction || !m_sqliteTransaction->inProgress());
#else
rollbackIfInProgress();
#endif
}
void SQLTransactionBackendSync::trace(Visitor* visitor)
{
visitor->trace(m_database);
}
PassRefPtrWillBeRawPtr<SQLResultSet> SQLTransactionBackendSync::executeSQL(const String& sqlStatement, const Vector<SQLValue>& arguments, ExceptionState& exceptionState)
{
ASSERT(m_database->executionContext()->isContextThread());
m_database->setLastErrorMessage("");
if (!m_database->opened()) {
m_database->setLastErrorMessage("cannot executeSQL because the database is not open");
exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMessage);
return nullptr;
}
if (m_hasVersionMismatch) {
m_database->setLastErrorMessage("cannot executeSQL because there is a version mismatch");
exceptionState.throwDOMException(VersionError, SQLError::versionErrorMessage);
return nullptr;
}
if (sqlStatement.isEmpty())
return nullptr;
int permissions = DatabaseAuthorizer::ReadWriteMask;
if (!m_database->databaseContext()->allowDatabaseAccess())
permissions |= DatabaseAuthorizer::NoAccessMask;
else if (m_readOnly)
permissions |= DatabaseAuthorizer::ReadOnlyMask;
SQLStatementSync statement(sqlStatement, arguments, permissions);
m_database->resetAuthorizer();
bool retryStatement = true;
RefPtrWillBeRawPtr<SQLResultSet> resultSet;
while (retryStatement) {
retryStatement = false;
resultSet = statement.execute(m_database.get(), exceptionState);
if (!resultSet) {
if (m_sqliteTransaction->wasRolledBackBySqlite())
return nullptr;
if (exceptionState.code() == QuotaExceededError) {
if (m_transactionClient->didExceedQuota(database())) {
exceptionState.clearException();
retryStatement = true;
} else {
m_database->setLastErrorMessage("there was not enough remaining storage space");
return nullptr;
}
}
}
}
if (m_database->lastActionChangedDatabase())
m_modifiedDatabase = true;
return resultSet.release();
}
void SQLTransactionBackendSync::begin(ExceptionState& exceptionState)
{
ASSERT(m_database->executionContext()->isContextThread());
if (!m_database->opened()) {
m_database->reportStartTransactionResult(1, SQLError::UNKNOWN_ERR, 0);
m_database->setLastErrorMessage("cannot begin transaction because the database is not open");
exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMessage);
return;
}
ASSERT(!m_database->sqliteDatabase().transactionInProgress());
if (!m_readOnly)
m_database->sqliteDatabase().setMaximumSize(m_database->maximumSize());
ASSERT(!m_sqliteTransaction);
m_sqliteTransaction = adoptPtr(new SQLiteTransaction(m_database->sqliteDatabase(), m_readOnly));
m_database->resetDeletes();
m_database->disableAuthorizer();
m_sqliteTransaction->begin();
m_database->enableAuthorizer();
if (!m_sqliteTransaction->inProgress()) {
ASSERT(!m_database->sqliteDatabase().transactionInProgress());
m_database->reportStartTransactionResult(2, SQLError::DATABASE_ERR, m_database->sqliteDatabase().lastError());
m_database->setLastErrorMessage("unable to begin transaction",
m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
m_sqliteTransaction.clear();
exceptionState.throwDOMException(SQLDatabaseError, "Unable to begin transaction.");
return;
}
String actualVersion;
if (!m_database->getActualVersionForTransaction(actualVersion)) {
m_database->reportStartTransactionResult(3, SQLError::DATABASE_ERR, m_database->sqliteDatabase().lastError());
m_database->setLastErrorMessage("unable to read version",
m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
rollback();
exceptionState.throwDOMException(SQLDatabaseError, "Unable to read version.");
return;
}
m_hasVersionMismatch = !m_database->expectedVersion().isEmpty() && (m_database->expectedVersion() != actualVersion);
m_database->reportStartTransactionResult(0, -1, 0);
}
void SQLTransactionBackendSync::execute(ExceptionState& exceptionState)
{
ASSERT(m_database->executionContext()->isContextThread());
if (!m_database->opened() || (m_callback && !m_callback->handleEvent(SQLTransactionSync::from(this)))) {
if (m_database->lastErrorMessage().isEmpty())
m_database->setLastErrorMessage("failed to execute transaction callback");
m_callback.clear();
exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMessage);
return;
}
m_callback.clear();
}
void SQLTransactionBackendSync::commit(ExceptionState& exceptionState)
{
ASSERT(m_database->executionContext()->isContextThread());
if (!m_database->opened()) {
m_database->reportCommitTransactionResult(1, SQLError::UNKNOWN_ERR, 0);
m_database->setLastErrorMessage("unable to commit transaction because the database is not open.");
exceptionState.throwDOMException(UnknownError, SQLError::unknownErrorMessage);
return;
}
ASSERT(m_sqliteTransaction);
m_database->disableAuthorizer();
m_sqliteTransaction->commit();
m_database->enableAuthorizer();
if (m_sqliteTransaction->inProgress()) {
m_database->reportCommitTransactionResult(2, SQLError::DATABASE_ERR, m_database->sqliteDatabase().lastError());
m_database->setLastErrorMessage("unable to commit transaction",
m_database->sqliteDatabase().lastError(), m_database->sqliteDatabase().lastErrorMsg());
exceptionState.throwDOMException(SQLDatabaseError, "Unable to commit transaction.");
return;
}
m_sqliteTransaction.clear();
if (m_database->hadDeletes())
m_database->incrementalVacuumIfNeeded();
if (m_modifiedDatabase)
m_transactionClient->didCommitWriteTransaction(database());
m_database->reportCommitTransactionResult(0, -1, 0);
}
void SQLTransactionBackendSync::rollback()
{
ASSERT(!m_database->executionContext() || m_database->executionContext()->isContextThread());
m_database->disableAuthorizer();
if (m_sqliteTransaction) {
m_sqliteTransaction->rollback();
m_sqliteTransaction.clear();
}
m_database->enableAuthorizer();
ASSERT(!m_database->sqliteDatabase().transactionInProgress());
}
}