Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions Source/WebKit/NetworkProcess/storage/SQLiteStorageArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,17 @@ HashMap<String, String> SQLiteStorageArea::allItems()
}

Expected<void, StorageError> SQLiteStorageArea::setItem(IPC::Connection::UniqueID connection, StorageAreaImplIdentifier storageAreaImplID, String&& key, String&& value, const String& urlString)
{
String oldValue;
if (auto valueOrError = getItem(key))
oldValue = valueOrError.value();
auto settingOutcome = setItem(key, value, true);
if (settingOutcome)
dispatchEvents(connection, storageAreaImplID, key, oldValue, value, urlString);
return settingOutcome;
}

Expected<void, StorageError> SQLiteStorageArea::setItem(const String& key, const String& value, bool handleDatabaseCorruption)
{
ASSERT(!isMainRunLoop());

Expand All @@ -334,9 +345,6 @@ Expected<void, StorageError> SQLiteStorageArea::setItem(IPC::Connection::UniqueI
return makeUnexpected(StorageError::QuotaExceeded);

startTransactionIfNecessary();
String oldValue;
if (auto valueOrError = getItem(key))
oldValue = valueOrError.value();

auto statement = cachedStatement(StatementType::SetItem);
if (!statement || statement->bindText(1, key) || statement->bindBlob(2, value)) {
Expand All @@ -346,12 +354,13 @@ Expected<void, StorageError> SQLiteStorageArea::setItem(IPC::Connection::UniqueI

const auto result = statement->step();
if (result != SQLITE_DONE) {
RELEASE_LOG_ERROR(Storage, "SQLiteStorageArea::setItem failed on stepping statement (%d) - %s", m_database->lastError(), m_database->lastErrorMsg());
handleDatabaseCorruptionIfNeeded(result);
return makeUnexpected(StorageError::Database);
if (!handleDatabaseCorruption || !handleDatabaseCorruptionIfNeeded(result))
return makeUnexpected(StorageError::Database);
statement = cachedStatement(StatementType::SetItem);
if (!statement || statement->bindText(1, key) || statement->bindBlob(2, value) || statement->step() != SQLITE_DONE)
return makeUnexpected(StorageError::Database);
}

dispatchEvents(connection, storageAreaImplID, key, oldValue, value, urlString);
updateCacheIfNeeded(key, value);

return { };
Expand Down Expand Up @@ -454,11 +463,16 @@ bool SQLiteStorageArea::handleDatabaseCorruptionIfNeeded(int databaseError)
if (databaseError != SQLITE_CORRUPT && databaseError != SQLITE_NOTADB)
return false;

m_database = nullptr;
m_cache = std::nullopt;
m_cacheSize = std::nullopt;
HashMap<String, Value> cache(WTFMove(*m_cache));
close();
RELEASE_LOG(Storage, "SQLiteStorageArea::handleDatabaseCorruption deletes corrupted database file '%s'", m_path.utf8().data());
WebCore::SQLiteFileSystem::deleteDatabaseFile(m_path);

// Reconstruct database based on cache.
for (auto& [key, value] : cache)
if (auto* valueString = std::get_if<String>(&value))
setItem(key, *valueString, false);

return true;
}

Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/NetworkProcess/storage/SQLiteStorageArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class SQLiteStorageArea final : public StorageAreaBase {
void clear() final;
HashMap<String, String> allItems() final;
Expected<void, StorageError> setItem(IPC::Connection::UniqueID, StorageAreaImplIdentifier, String&& key, String&& value, const String& urlString) final;
Expected<void, StorageError> setItem(const String& key, const String& value, bool handleDatabaseCorruption);
Expected<void, StorageError> removeItem(IPC::Connection::UniqueID, StorageAreaImplIdentifier, const String& key, const String& urlString) final;
Expected<void, StorageError> clear(IPC::Connection::UniqueID, StorageAreaImplIdentifier, const String& urlString) final;

Expand Down