StorageAreaSync.cpp [plain text]
#include "config.h"
#include "StorageAreaSync.h"
#include "EventNames.h"
#include "FileSystem.h"
#include "HTMLElement.h"
#include "SQLiteDatabaseTracker.h"
#include "SQLiteFileSystem.h"
#include "SQLiteStatement.h"
#include "SQLiteTransaction.h"
#include "SecurityOrigin.h"
#include "StorageAreaImpl.h"
#include "StorageSyncManager.h"
#include "StorageTracker.h"
#include "SuddenTermination.h"
#include <wtf/Functional.h>
#include <wtf/MainThread.h>
#include <wtf/text/CString.h>
namespace WebCore {
static const double StorageSyncInterval = 1.0;
static const int MaxiumItemsToSync = 100;
inline StorageAreaSync::StorageAreaSync(PassRefPtr<StorageSyncManager> storageSyncManager, PassRefPtr<StorageAreaImpl> storageArea, const String& databaseIdentifier)
: m_syncTimer(this, &StorageAreaSync::syncTimerFired)
, m_itemsCleared(false)
, m_finalSyncScheduled(false)
, m_storageArea(storageArea)
, m_syncManager(storageSyncManager)
, m_databaseIdentifier(databaseIdentifier.isolatedCopy())
, m_clearItemsWhileSyncing(false)
, m_syncScheduled(false)
, m_syncInProgress(false)
, m_databaseOpenFailed(false)
, m_syncCloseDatabase(false)
, m_importComplete(false)
{
ASSERT(isMainThread());
ASSERT(m_storageArea);
ASSERT(m_syncManager);
m_syncManager->dispatch(bind(&StorageAreaSync::performImport, this));
}
PassRefPtr<StorageAreaSync> StorageAreaSync::create(PassRefPtr<StorageSyncManager> storageSyncManager, PassRefPtr<StorageAreaImpl> storageArea, const String& databaseIdentifier)
{
RefPtr<StorageAreaSync> area = adoptRef(new StorageAreaSync(storageSyncManager, storageArea, databaseIdentifier));
return area.release();
}
StorageAreaSync::~StorageAreaSync()
{
ASSERT(isMainThread());
ASSERT(!m_syncTimer.isActive());
ASSERT(m_finalSyncScheduled);
}
void StorageAreaSync::scheduleFinalSync()
{
ASSERT(isMainThread());
blockUntilImportComplete();
m_storageArea = 0;
if (m_syncTimer.isActive())
m_syncTimer.stop();
else {
disableSuddenTermination();
}
m_finalSyncScheduled = true;
syncTimerFired(&m_syncTimer);
m_syncManager->dispatch(bind(&StorageAreaSync::deleteEmptyDatabase, this));
}
void StorageAreaSync::scheduleItemForSync(const String& key, const String& value)
{
ASSERT(isMainThread());
ASSERT(!m_finalSyncScheduled);
m_changedItems.set(key, value);
if (!m_syncTimer.isActive()) {
m_syncTimer.startOneShot(StorageSyncInterval);
disableSuddenTermination();
}
}
void StorageAreaSync::scheduleClear()
{
ASSERT(isMainThread());
ASSERT(!m_finalSyncScheduled);
m_changedItems.clear();
m_itemsCleared = true;
if (!m_syncTimer.isActive()) {
m_syncTimer.startOneShot(StorageSyncInterval);
disableSuddenTermination();
}
}
void StorageAreaSync::scheduleCloseDatabase()
{
ASSERT(isMainThread());
ASSERT(!m_finalSyncScheduled);
if (!m_database.isOpen())
return;
m_syncCloseDatabase = true;
if (!m_syncTimer.isActive()) {
m_syncTimer.startOneShot(StorageSyncInterval);
disableSuddenTermination();
}
}
void StorageAreaSync::syncTimerFired(Timer<StorageAreaSync>*)
{
ASSERT(isMainThread());
bool partialSync = false;
{
MutexLocker locker(m_syncLock);
if (m_syncInProgress && !m_finalSyncScheduled) {
ASSERT(!m_syncTimer.isActive());
m_syncTimer.startOneShot(StorageSyncInterval);
return;
}
if (m_itemsCleared) {
m_itemsPendingSync.clear();
m_clearItemsWhileSyncing = true;
m_itemsCleared = false;
}
HashMap<String, String>::iterator changed_it = m_changedItems.begin();
HashMap<String, String>::iterator changed_end = m_changedItems.end();
for (int count = 0; changed_it != changed_end; ++count, ++changed_it) {
if (count >= MaxiumItemsToSync && !m_finalSyncScheduled) {
partialSync = true;
break;
}
m_itemsPendingSync.set(changed_it->key.isolatedCopy(), changed_it->value.isolatedCopy());
}
if (partialSync) {
HashMap<String, String>::iterator pending_it = m_itemsPendingSync.begin();
HashMap<String, String>::iterator pending_end = m_itemsPendingSync.end();
for (; pending_it != pending_end; ++pending_it)
m_changedItems.remove(pending_it->key);
}
if (!m_syncScheduled) {
m_syncScheduled = true;
disableSuddenTermination();
m_syncManager->dispatch(bind(&StorageAreaSync::performSync, this));
}
}
if (partialSync) {
ASSERT(!m_syncTimer.isActive());
m_syncTimer.startOneShot(StorageSyncInterval);
} else {
enableSuddenTermination();
m_changedItems.clear();
}
}
void StorageAreaSync::openDatabase(OpenDatabaseParamType openingStrategy)
{
ASSERT(!isMainThread());
ASSERT(!m_database.isOpen());
ASSERT(!m_databaseOpenFailed);
SQLiteTransactionInProgressAutoCounter transactionCounter;
String databaseFilename = m_syncManager->fullDatabaseFilename(m_databaseIdentifier);
if (!fileExists(databaseFilename) && openingStrategy == SkipIfNonExistent)
return;
if (databaseFilename.isEmpty()) {
LOG_ERROR("Filename for local storage database is empty - cannot open for persistent storage");
markImported();
m_databaseOpenFailed = true;
return;
}
StorageTracker::tracker().cancelDeletingOrigin(m_databaseIdentifier);
if (!m_database.open(databaseFilename)) {
LOG_ERROR("Failed to open database file %s for local storage", databaseFilename.utf8().data());
markImported();
m_databaseOpenFailed = true;
return;
}
migrateItemTableIfNeeded();
if (!m_database.executeCommand("CREATE TABLE IF NOT EXISTS ItemTable (key TEXT UNIQUE ON CONFLICT REPLACE, value BLOB NOT NULL ON CONFLICT FAIL)")) {
LOG_ERROR("Failed to create table ItemTable for local storage");
markImported();
m_databaseOpenFailed = true;
return;
}
StorageTracker::tracker().setOriginDetails(m_databaseIdentifier, databaseFilename);
}
void StorageAreaSync::migrateItemTableIfNeeded()
{
if (!m_database.tableExists("ItemTable"))
return;
{
SQLiteStatement query(m_database, "SELECT value FROM ItemTable LIMIT 1");
if (query.isColumnDeclaredAsBlob(0))
return;
}
static const char* commands[] = {
"DROP TABLE IF EXISTS ItemTable2",
"CREATE TABLE ItemTable2 (key TEXT UNIQUE ON CONFLICT REPLACE, value BLOB NOT NULL ON CONFLICT FAIL)",
"INSERT INTO ItemTable2 SELECT * from ItemTable",
"DROP TABLE ItemTable",
"ALTER TABLE ItemTable2 RENAME TO ItemTable",
0,
};
SQLiteTransaction transaction(m_database, false);
transaction.begin();
for (size_t i = 0; commands[i]; ++i) {
if (!m_database.executeCommand(commands[i])) {
LOG_ERROR("Failed to migrate table ItemTable for local storage when executing: %s", commands[i]);
transaction.rollback();
ASSERT_NOT_REACHED();
if (!m_database.executeCommand("ALTER TABLE ItemTable RENAME TO Backup_ItemTable"))
LOG_ERROR("Failed to save ItemTable after migration job failed.");
return;
}
}
transaction.commit();
}
void StorageAreaSync::performImport()
{
ASSERT(!isMainThread());
ASSERT(!m_database.isOpen());
openDatabase(SkipIfNonExistent);
if (!m_database.isOpen()) {
markImported();
return;
}
SQLiteStatement query(m_database, "SELECT key, value FROM ItemTable");
if (query.prepare() != SQLResultOk) {
LOG_ERROR("Unable to select items from ItemTable for local storage");
markImported();
return;
}
HashMap<String, String> itemMap;
int result = query.step();
while (result == SQLResultRow) {
itemMap.set(query.getColumnText(0), query.getColumnBlobAsString(1));
result = query.step();
}
if (result != SQLResultDone) {
LOG_ERROR("Error reading items from ItemTable for local storage");
markImported();
return;
}
m_storageArea->importItems(itemMap);
markImported();
}
void StorageAreaSync::markImported()
{
MutexLocker locker(m_importLock);
m_importComplete = true;
m_importCondition.signal();
}
void StorageAreaSync::blockUntilImportComplete()
{
ASSERT(isMainThread());
if (!m_storageArea)
return;
MutexLocker locker(m_importLock);
while (!m_importComplete)
m_importCondition.wait(m_importLock);
m_storageArea = 0;
}
void StorageAreaSync::sync(bool clearItems, const HashMap<String, String>& items)
{
ASSERT(!isMainThread());
if (items.isEmpty() && !clearItems && !m_syncCloseDatabase)
return;
if (m_databaseOpenFailed)
return;
if (!m_database.isOpen() && m_syncCloseDatabase) {
m_syncCloseDatabase = false;
return;
}
if (!m_database.isOpen())
openDatabase(CreateIfNonExistent);
if (!m_database.isOpen())
return;
if (m_syncCloseDatabase) {
m_syncCloseDatabase = false;
m_database.close();
return;
}
SQLiteTransactionInProgressAutoCounter transactionCounter;
if (clearItems) {
SQLiteStatement clear(m_database, "DELETE FROM ItemTable");
if (clear.prepare() != SQLResultOk) {
LOG_ERROR("Failed to prepare clear statement - cannot write to local storage database");
return;
}
int result = clear.step();
if (result != SQLResultDone) {
LOG_ERROR("Failed to clear all items in the local storage database - %i", result);
return;
}
}
SQLiteStatement insert(m_database, "INSERT INTO ItemTable VALUES (?, ?)");
if (insert.prepare() != SQLResultOk) {
LOG_ERROR("Failed to prepare insert statement - cannot write to local storage database");
return;
}
SQLiteStatement remove(m_database, "DELETE FROM ItemTable WHERE key=?");
if (remove.prepare() != SQLResultOk) {
LOG_ERROR("Failed to prepare delete statement - cannot write to local storage database");
return;
}
HashMap<String, String>::const_iterator end = items.end();
SQLiteTransaction transaction(m_database);
transaction.begin();
for (HashMap<String, String>::const_iterator it = items.begin(); it != end; ++it) {
SQLiteStatement& query = it->value.isNull() ? remove : insert;
query.bindText(1, it->key);
if (!it->value.isNull())
query.bindBlob(2, it->value);
int result = query.step();
if (result != SQLResultDone) {
LOG_ERROR("Failed to update item in the local storage database - %i", result);
break;
}
query.reset();
}
transaction.commit();
}
void StorageAreaSync::performSync()
{
ASSERT(!isMainThread());
bool clearItems;
HashMap<String, String> items;
{
MutexLocker locker(m_syncLock);
ASSERT(m_syncScheduled);
clearItems = m_clearItemsWhileSyncing;
m_itemsPendingSync.swap(items);
m_clearItemsWhileSyncing = false;
m_syncScheduled = false;
m_syncInProgress = true;
}
sync(clearItems, items);
{
MutexLocker locker(m_syncLock);
m_syncInProgress = false;
}
enableSuddenTermination();
}
void StorageAreaSync::deleteEmptyDatabase()
{
ASSERT(!isMainThread());
if (!m_database.isOpen())
return;
SQLiteStatement query(m_database, "SELECT COUNT(*) FROM ItemTable");
if (query.prepare() != SQLResultOk) {
LOG_ERROR("Unable to count number of rows in ItemTable for local storage");
return;
}
int result = query.step();
if (result != SQLResultRow) {
LOG_ERROR("No results when counting number of rows in ItemTable for local storage");
return;
}
int count = query.getColumnInt(0);
if (!count) {
query.finalize();
m_database.close();
if (StorageTracker::tracker().isActive())
StorageTracker::tracker().deleteOriginWithIdentifier(m_databaseIdentifier);
else {
String databaseFilename = m_syncManager->fullDatabaseFilename(m_databaseIdentifier);
if (!SQLiteFileSystem::deleteDatabaseFile(databaseFilename))
LOG_ERROR("Failed to delete database file %s\n", databaseFilename.utf8().data());
}
}
}
void StorageAreaSync::scheduleSync()
{
syncTimerFired(&m_syncTimer);
}
}