This source file includes following definitions.
- create
- m_readOnly
- trace
- hasCallback
- hasSuccessCallback
- hasErrorCallback
- setBackend
- stateFunctionFor
- requestTransitToState
- nextStateForTransactionError
- deliverTransactionCallback
- deliverTransactionErrorCallback
- deliverStatementCallback
- deliverQuotaIncreaseCallback
- deliverSuccessCallback
- unreachableState
- sendToBackendState
- performPendingCallback
- executeSQL
- computeNextStateAndCleanupIfNeeded
- clearCallbackWrappers
- releaseErrorCallback
#include "config.h"
#include "modules/webdatabase/SQLTransaction.h"
#include "bindings/v8/ExceptionState.h"
#include "core/dom/ExceptionCode.h"
#include "core/html/VoidCallback.h"
#include "platform/Logging.h"
#include "modules/webdatabase/AbstractSQLTransactionBackend.h"
#include "modules/webdatabase/Database.h"
#include "modules/webdatabase/DatabaseAuthorizer.h"
#include "modules/webdatabase/DatabaseContext.h"
#include "modules/webdatabase/SQLError.h"
#include "modules/webdatabase/SQLStatementCallback.h"
#include "modules/webdatabase/SQLStatementErrorCallback.h"
#include "modules/webdatabase/SQLTransactionCallback.h"
#include "modules/webdatabase/SQLTransactionClient.h"
#include "modules/webdatabase/SQLTransactionErrorCallback.h"
#include "wtf/StdLibExtras.h"
#include "wtf/Vector.h"
namespace WebCore {
PassRefPtrWillBeRawPtr<SQLTransaction> SQLTransaction::create(Database* db, PassOwnPtr<SQLTransactionCallback> callback,
PassOwnPtr<VoidCallback> successCallback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback,
bool readOnly)
{
return adoptRefWillBeNoop(new SQLTransaction(db, callback, successCallback, errorCallback, readOnly));
}
SQLTransaction::SQLTransaction(Database* db, PassOwnPtr<SQLTransactionCallback> callback,
PassOwnPtr<VoidCallback> successCallback, PassOwnPtr<SQLTransactionErrorCallback> errorCallback,
bool readOnly)
: m_database(db)
, m_callbackWrapper(callback, db->executionContext())
, m_successCallbackWrapper(successCallback, db->executionContext())
, m_errorCallbackWrapper(errorCallback, db->executionContext())
, m_executeSqlAllowed(false)
, m_readOnly(readOnly)
{
ASSERT(m_database);
ScriptWrappable::init(this);
}
void SQLTransaction::trace(Visitor* visitor)
{
visitor->trace(m_database);
visitor->trace(m_backend);
}
bool SQLTransaction::hasCallback() const
{
return m_callbackWrapper.hasCallback();
}
bool SQLTransaction::hasSuccessCallback() const
{
return m_successCallbackWrapper.hasCallback();
}
bool SQLTransaction::hasErrorCallback() const
{
return m_errorCallbackWrapper.hasCallback();
}
void SQLTransaction::setBackend(AbstractSQLTransactionBackend* backend)
{
ASSERT(!m_backend);
m_backend = backend;
}
SQLTransaction::StateFunction SQLTransaction::stateFunctionFor(SQLTransactionState state)
{
static const StateFunction stateFunctions[] = {
&SQLTransaction::unreachableState,
&SQLTransaction::unreachableState,
&SQLTransaction::unreachableState,
&SQLTransaction::unreachableState,
&SQLTransaction::sendToBackendState,
&SQLTransaction::unreachableState,
&SQLTransaction::sendToBackendState,
&SQLTransaction::sendToBackendState,
&SQLTransaction::deliverTransactionCallback,
&SQLTransaction::deliverTransactionErrorCallback,
&SQLTransaction::deliverStatementCallback,
&SQLTransaction::deliverQuotaIncreaseCallback,
&SQLTransaction::deliverSuccessCallback
};
ASSERT(WTF_ARRAY_LENGTH(stateFunctions) == static_cast<int>(SQLTransactionState::NumberOfStates));
ASSERT(state < SQLTransactionState::NumberOfStates);
return stateFunctions[static_cast<int>(state)];
}
void SQLTransaction::requestTransitToState(SQLTransactionState nextState)
{
WTF_LOG(StorageAPI, "Scheduling %s for transaction %p\n", nameForSQLTransactionState(nextState), this);
m_requestedState = nextState;
m_database->scheduleTransactionCallback(this);
}
SQLTransactionState SQLTransaction::nextStateForTransactionError()
{
ASSERT(m_transactionError);
if (m_errorCallbackWrapper.hasCallback())
return SQLTransactionState::DeliverTransactionErrorCallback;
return SQLTransactionState::CleanupAfterTransactionErrorCallback;
}
SQLTransactionState SQLTransaction::deliverTransactionCallback()
{
bool shouldDeliverErrorCallback = false;
OwnPtr<SQLTransactionCallback> callback = m_callbackWrapper.unwrap();
if (callback) {
m_executeSqlAllowed = true;
shouldDeliverErrorCallback = !callback->handleEvent(this);
m_executeSqlAllowed = false;
}
SQLTransactionState nextState = SQLTransactionState::RunStatements;
if (shouldDeliverErrorCallback) {
m_database->reportStartTransactionResult(5, SQLError::UNKNOWN_ERR, 0);
m_transactionError = SQLErrorData::create(SQLError::UNKNOWN_ERR, "the SQLTransactionCallback was null or threw an exception");
nextState = SQLTransactionState::DeliverTransactionErrorCallback;
}
m_database->reportStartTransactionResult(0, -1, 0);
return nextState;
}
SQLTransactionState SQLTransaction::deliverTransactionErrorCallback()
{
OwnPtr<SQLTransactionErrorCallback> errorCallback = m_errorCallbackWrapper.unwrap();
if (errorCallback) {
if (!m_transactionError) {
ASSERT(m_backend->transactionError());
m_transactionError = SQLErrorData::create(*m_backend->transactionError());
}
ASSERT(m_transactionError);
RefPtrWillBeRawPtr<SQLError> error = SQLError::create(*m_transactionError);
errorCallback->handleEvent(error.get());
m_transactionError = nullptr;
}
clearCallbackWrappers();
return SQLTransactionState::CleanupAfterTransactionErrorCallback;
}
SQLTransactionState SQLTransaction::deliverStatementCallback()
{
m_executeSqlAllowed = true;
AbstractSQLStatement* currentAbstractStatement = m_backend->currentStatement();
SQLStatement* currentStatement = static_cast<SQLStatement*>(currentAbstractStatement);
ASSERT(currentStatement);
bool result = currentStatement->performCallback(this);
m_executeSqlAllowed = false;
if (result) {
m_database->reportCommitTransactionResult(2, SQLError::UNKNOWN_ERR, 0);
m_transactionError = SQLErrorData::create(SQLError::UNKNOWN_ERR, "the statement callback raised an exception or statement error callback did not return false");
return nextStateForTransactionError();
}
return SQLTransactionState::RunStatements;
}
SQLTransactionState SQLTransaction::deliverQuotaIncreaseCallback()
{
ASSERT(m_backend->currentStatement());
bool shouldRetryCurrentStatement = m_database->transactionClient()->didExceedQuota(database());
m_backend->setShouldRetryCurrentStatement(shouldRetryCurrentStatement);
return SQLTransactionState::RunStatements;
}
SQLTransactionState SQLTransaction::deliverSuccessCallback()
{
OwnPtr<VoidCallback> successCallback = m_successCallbackWrapper.unwrap();
if (successCallback)
successCallback->handleEvent();
clearCallbackWrappers();
return SQLTransactionState::CleanupAndTerminate;
}
SQLTransactionState SQLTransaction::unreachableState()
{
ASSERT_NOT_REACHED();
return SQLTransactionState::End;
}
SQLTransactionState SQLTransaction::sendToBackendState()
{
ASSERT(m_nextState != SQLTransactionState::Idle);
m_backend->requestTransitToState(m_nextState);
return SQLTransactionState::Idle;
}
void SQLTransaction::performPendingCallback()
{
computeNextStateAndCleanupIfNeeded();
runStateMachine();
}
void SQLTransaction::executeSQL(const String& sqlStatement, const Vector<SQLValue>& arguments, PassOwnPtr<SQLStatementCallback> callback, PassOwnPtr<SQLStatementErrorCallback> callbackError, ExceptionState& exceptionState)
{
if (!m_executeSqlAllowed) {
exceptionState.throwDOMException(InvalidStateError, "SQL execution is disallowed.");
return;
}
if (!m_database->opened()) {
exceptionState.throwDOMException(InvalidStateError, "The database has not been opened.");
return;
}
int permissions = DatabaseAuthorizer::ReadWriteMask;
if (!m_database->databaseContext()->allowDatabaseAccess())
permissions |= DatabaseAuthorizer::NoAccessMask;
else if (m_readOnly)
permissions |= DatabaseAuthorizer::ReadOnlyMask;
OwnPtr<SQLStatement> statement = SQLStatement::create(m_database.get(), callback, callbackError);
m_backend->executeSQL(statement.release(), sqlStatement, arguments, permissions);
}
bool SQLTransaction::computeNextStateAndCleanupIfNeeded()
{
if (m_database->opened() && !m_database->isInterrupted()) {
setStateToRequestedState();
ASSERT(m_nextState == SQLTransactionState::End
|| m_nextState == SQLTransactionState::DeliverTransactionCallback
|| m_nextState == SQLTransactionState::DeliverTransactionErrorCallback
|| m_nextState == SQLTransactionState::DeliverStatementCallback
|| m_nextState == SQLTransactionState::DeliverQuotaIncreaseCallback
|| m_nextState == SQLTransactionState::DeliverSuccessCallback);
WTF_LOG(StorageAPI, "Callback %s\n", nameForSQLTransactionState(m_nextState));
return false;
}
clearCallbackWrappers();
m_nextState = SQLTransactionState::CleanupAndTerminate;
return true;
}
void SQLTransaction::clearCallbackWrappers()
{
m_callbackWrapper.clear();
m_successCallbackWrapper.clear();
m_errorCallbackWrapper.clear();
}
PassOwnPtr<SQLTransactionErrorCallback> SQLTransaction::releaseErrorCallback()
{
return m_errorCallbackWrapper.unwrap();
}
}