This source file includes following definitions.
- getDatabaseIdentifier
- trace
- processPendingTransactions
- acquireLock
- releaseLock
- shutdown
#include "config.h"
#include "modules/webdatabase/SQLTransactionCoordinator.h"
#include "modules/webdatabase/DatabaseBackend.h"
#include "modules/webdatabase/SQLTransactionBackend.h"
namespace WebCore {
static String getDatabaseIdentifier(SQLTransactionBackend* transaction)
{
DatabaseBackend* database = transaction->database();
ASSERT(database);
return database->stringIdentifier();
}
SQLTransactionCoordinator::SQLTransactionCoordinator()
: m_isShuttingDown(false)
{
}
void SQLTransactionCoordinator::trace(Visitor* visitor)
{
visitor->trace(m_coordinationInfoMap);
}
void SQLTransactionCoordinator::processPendingTransactions(CoordinationInfo& info)
{
if (info.activeWriteTransaction || info.pendingTransactions.isEmpty())
return;
RefPtrWillBeRawPtr<SQLTransactionBackend> firstPendingTransaction = info.pendingTransactions.first();
if (firstPendingTransaction->isReadOnly()) {
do {
firstPendingTransaction = info.pendingTransactions.takeFirst();
info.activeReadTransactions.add(firstPendingTransaction);
firstPendingTransaction->lockAcquired();
} while (!info.pendingTransactions.isEmpty() && info.pendingTransactions.first()->isReadOnly());
} else if (info.activeReadTransactions.isEmpty()) {
info.pendingTransactions.removeFirst();
info.activeWriteTransaction = firstPendingTransaction;
firstPendingTransaction->lockAcquired();
}
}
void SQLTransactionCoordinator::acquireLock(SQLTransactionBackend* transaction)
{
ASSERT(!m_isShuttingDown);
String dbIdentifier = getDatabaseIdentifier(transaction);
CoordinationInfoHeapMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier);
if (coordinationInfoIterator == m_coordinationInfoMap.end()) {
CoordinationInfo& info = m_coordinationInfoMap.add(dbIdentifier, CoordinationInfo()).storedValue->value;
info.pendingTransactions.append(transaction);
processPendingTransactions(info);
} else {
CoordinationInfo& info = coordinationInfoIterator->value;
info.pendingTransactions.append(transaction);
processPendingTransactions(info);
}
}
void SQLTransactionCoordinator::releaseLock(SQLTransactionBackend* transaction)
{
if (m_isShuttingDown)
return;
String dbIdentifier = getDatabaseIdentifier(transaction);
CoordinationInfoHeapMap::iterator coordinationInfoIterator = m_coordinationInfoMap.find(dbIdentifier);
ASSERT_WITH_SECURITY_IMPLICATION(coordinationInfoIterator != m_coordinationInfoMap.end());
CoordinationInfo& info = coordinationInfoIterator->value;
if (transaction->isReadOnly()) {
ASSERT(info.activeReadTransactions.contains(transaction));
info.activeReadTransactions.remove(transaction);
} else {
ASSERT(info.activeWriteTransaction == transaction);
info.activeWriteTransaction = nullptr;
}
processPendingTransactions(info);
}
void SQLTransactionCoordinator::shutdown()
{
m_isShuttingDown = true;
for (CoordinationInfoHeapMap::iterator coordinationInfoIterator = m_coordinationInfoMap.begin();
coordinationInfoIterator != m_coordinationInfoMap.end(); ++coordinationInfoIterator) {
CoordinationInfo& info = coordinationInfoIterator->value;
if (info.activeWriteTransaction)
info.activeWriteTransaction->notifyDatabaseThreadIsShuttingDown();
for (WillBeHeapHashSet<RefPtrWillBeMember<SQLTransactionBackend> >::iterator activeReadTransactionsIterator =
info.activeReadTransactions.begin();
activeReadTransactionsIterator != info.activeReadTransactions.end();
++activeReadTransactionsIterator) {
(*activeReadTransactionsIterator)->notifyDatabaseThreadIsShuttingDown();
}
while (!info.pendingTransactions.isEmpty()) {
RefPtrWillBeRawPtr<SQLTransactionBackend> transaction = info.pendingTransactions.first();
transaction->notifyDatabaseThreadIsShuttingDown();
}
}
m_coordinationInfoMap.clear();
}
}