Skip to content

Commit

Permalink
Bug 1736844 - Add interrupt flag to storage service methods opening d…
Browse files Browse the repository at this point in the history
…atabase; r=dom-storage-reviewers,mak,janv

Differential Revision: https://phabricator.services.mozilla.com/D129023
  • Loading branch information
jjjalkanen committed Feb 17, 2022
1 parent 9118563 commit 781b5ef
Show file tree
Hide file tree
Showing 20 changed files with 499 additions and 315 deletions.
9 changes: 6 additions & 3 deletions dom/cache/DBAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ Result<nsCOMPtr<mozIStorageConnection>, nsresult> OpenDBConnection(
// Expression.
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, storageService,
OpenDatabaseWithFileURL, dbFileUrl, ""_ns),
OpenDatabaseWithFileURL, dbFileUrl, ""_ns,
mozIStorageService::CONNECTION_DEFAULT),
// Predicate.
IsDatabaseCorruptionError,
// Fallback.
Expand All @@ -197,7 +198,8 @@ Result<nsCOMPtr<mozIStorageConnection>, nsresult> OpenDBConnection(

QM_TRY_RETURN(MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, storageService,
OpenDatabaseWithFileURL, dbFileUrl, ""_ns));
OpenDatabaseWithFileURL, dbFileUrl, ""_ns,
mozIStorageService::CONNECTION_DEFAULT));
})));

// Check the schema to make sure it is not too old.
Expand All @@ -211,7 +213,8 @@ Result<nsCOMPtr<mozIStorageConnection>, nsresult> OpenDBConnection(

QM_TRY_UNWRAP(conn, MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, storageService,
OpenDatabaseWithFileURL, dbFileUrl, ""_ns));
OpenDatabaseWithFileURL, dbFileUrl, ""_ns,
mozIStorageService::CONNECTION_DEFAULT));
}

QM_TRY(MOZ_TO_RESULT(db::InitializeConnection(*conn)));
Expand Down
3 changes: 2 additions & 1 deletion dom/indexedDB/ActorsParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,8 @@ Result<MovingNotNull<nsCOMPtr<mozIStorageConnection>>, nsresult> OpenDatabase(
QM_TRY_UNWRAP(auto connection,
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, aStorageService,
OpenDatabaseWithFileURL, &aFileURL, telemetryFilename));
OpenDatabaseWithFileURL, &aFileURL, telemetryFilename,
mozIStorageService::CONNECTION_DEFAULT));

return WrapMovingNotNull(std::move(connection));
}
Expand Down
33 changes: 19 additions & 14 deletions dom/localstorage/ActorsParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,9 @@ Result<nsCOMPtr<mozIStorageConnection>, nsresult> CreateStorageConnection(
auto connection,
OrElseIf(
// Expression.
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCOMPtr<mozIStorageConnection>,
storageService, OpenDatabase,
&aDBFile),
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, storageService, OpenDatabase,
&aDBFile, mozIStorageService::CONNECTION_DEFAULT),
// Predicate.
IsDatabaseCorruptionError,
// Fallback.
Expand Down Expand Up @@ -540,7 +540,7 @@ Result<nsCOMPtr<mozIStorageConnection>, nsresult> CreateStorageConnection(

QM_TRY_RETURN(MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, storageService, OpenDatabase,
&aDBFile));
&aDBFile, mozIStorageService::CONNECTION_DEFAULT));
})));

QM_TRY(MOZ_TO_RESULT(SetDefaultPragmas(connection)));
Expand Down Expand Up @@ -693,9 +693,10 @@ Result<nsCOMPtr<mozIStorageConnection>, nsresult> GetStorageConnection(
MOZ_SELECT_OVERLOAD(do_GetService),
MOZ_STORAGE_SERVICE_CONTRACTID));

QM_TRY_UNWRAP(auto connection, MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss,
OpenDatabase, databaseFile));
QM_TRY_UNWRAP(auto connection,
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss, OpenDatabase,
databaseFile, mozIStorageService::CONNECTION_DEFAULT));

QM_TRY(MOZ_TO_RESULT(SetDefaultPragmas(connection)));

Expand Down Expand Up @@ -744,8 +745,9 @@ CreateArchiveStorageConnection(const nsAString& aStoragePath) {
auto connection,
QM_OR_ELSE_WARN_IF(
// Expression.
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCOMPtr<mozIStorageConnection>, ss,
OpenUnsharedDatabase, archiveFile),
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss, OpenUnsharedDatabase,
archiveFile, mozIStorageService::CONNECTION_DEFAULT),
// Predicate.
IsDatabaseCorruptionError,
// Fallback. Don't throw an error, leave a corrupted ls-archive
Expand Down Expand Up @@ -839,8 +841,9 @@ Result<nsCOMPtr<mozIStorageConnection>, nsresult> CreateShadowStorageConnection(
auto connection,
QM_OR_ELSE_WARN_IF(
// Expression.
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCOMPtr<mozIStorageConnection>, ss,
OpenUnsharedDatabase, shadowFile),
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss, OpenUnsharedDatabase,
shadowFile, mozIStorageService::CONNECTION_DEFAULT),
// Predicate.
IsDatabaseCorruptionError,
// Fallback.
Expand All @@ -850,7 +853,7 @@ Result<nsCOMPtr<mozIStorageConnection>, nsresult> CreateShadowStorageConnection(

QM_TRY_RETURN(MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss, OpenUnsharedDatabase,
shadowFile));
shadowFile, mozIStorageService::CONNECTION_DEFAULT));
})));

QM_TRY(MOZ_TO_RESULT(SetShadowJournalMode(connection)));
Expand Down Expand Up @@ -879,7 +882,8 @@ Result<nsCOMPtr<mozIStorageConnection>, nsresult> CreateShadowStorageConnection(

QM_TRY_UNWRAP(connection, MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss,
OpenUnsharedDatabase, shadowFile));
OpenUnsharedDatabase, shadowFile,
mozIStorageService::CONNECTION_DEFAULT));

QM_TRY(MOZ_TO_RESULT(SetShadowJournalMode(connection)));

Expand Down Expand Up @@ -910,7 +914,8 @@ Result<nsCOMPtr<mozIStorageConnection>, nsresult> GetShadowStorageConnection(
MOZ_STORAGE_SERVICE_CONTRACTID));

QM_TRY_RETURN(MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss, OpenUnsharedDatabase, shadowFile));
nsCOMPtr<mozIStorageConnection>, ss, OpenUnsharedDatabase, shadowFile,
mozIStorageService::CONNECTION_DEFAULT));
}

nsresult AttachShadowDatabase(const nsAString& aBasePath,
Expand Down
59 changes: 32 additions & 27 deletions dom/quota/ActorsParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,8 @@ Result<nsCOMPtr<mozIStorageConnection>, nsresult> CreateWebAppsStoreConnection(
// Expression.
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, aStorageService,
OpenUnsharedDatabase, &aWebAppsStoreFile),
OpenUnsharedDatabase, &aWebAppsStoreFile,
mozIStorageService::CONNECTION_DEFAULT),
// Predicate.
IsDatabaseCorruptionError,
// Fallback. Don't throw an error, leave a corrupted
Expand Down Expand Up @@ -5669,10 +5670,11 @@ Result<Ok, nsresult> QuotaManager::CopyLocalStorageArchiveFromWebAppsStore(
GetLocalStorageArchiveTmpFile(*mStoragePath));

if (journalMode.EqualsLiteral("wal")) {
QM_TRY_INSPECT(const auto& lsArchiveTmpConnection,
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss,
OpenUnsharedDatabase, lsArchiveTmpFile));
QM_TRY_INSPECT(
const auto& lsArchiveTmpConnection,
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss, OpenUnsharedDatabase,
lsArchiveTmpFile, mozIStorageService::CONNECTION_DEFAULT));

// The archive will only be used for lazy data migration. There won't be
// any concurrent readers and writers that could benefit from Write-Ahead
Expand Down Expand Up @@ -5715,10 +5717,10 @@ Result<Ok, nsresult> QuotaManager::CopyLocalStorageArchiveFromWebAppsStore(

Unused << created;

QM_TRY_UNWRAP(
auto lsArchiveConnection,
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCOMPtr<mozIStorageConnection>, ss,
OpenUnsharedDatabase, &aLsArchiveFile));
QM_TRY_UNWRAP(auto lsArchiveConnection,
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss, OpenUnsharedDatabase,
&aLsArchiveFile, mozIStorageService::CONNECTION_DEFAULT));

QM_TRY(MOZ_TO_RESULT(
StorageDBUpdater::CreateCurrentSchema(lsArchiveConnection)));
Expand Down Expand Up @@ -5753,9 +5755,10 @@ QuotaManager::CreateLocalStorageArchiveConnection(
MOZ_STORAGE_SERVICE_CONTRACTID));

// This may return NS_ERROR_FILE_CORRUPTED too.
QM_TRY_UNWRAP(auto connection, MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss,
OpenUnsharedDatabase, &aLsArchiveFile));
QM_TRY_UNWRAP(auto connection,
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss, OpenUnsharedDatabase,
&aLsArchiveFile, mozIStorageService::CONNECTION_DEFAULT));

// The legacy LS implementation removes the database and creates an empty one
// when the schema can't be updated. The same effect can be achieved here by
Expand Down Expand Up @@ -6122,10 +6125,10 @@ Result<Ok, nsresult> QuotaManager::CreateEmptyLocalStorageArchive(
MOZ_SELECT_OVERLOAD(do_GetService),
MOZ_STORAGE_SERVICE_CONTRACTID));

QM_TRY_UNWRAP(
const auto connection,
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCOMPtr<mozIStorageConnection>, ss,
OpenUnsharedDatabase, &aLsArchiveFile));
QM_TRY_UNWRAP(const auto connection,
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss, OpenUnsharedDatabase,
&aLsArchiveFile, mozIStorageService::CONNECTION_DEFAULT));

QM_TRY(MOZ_TO_RESULT(StorageDBUpdater::CreateCurrentSchema(connection)));

Expand Down Expand Up @@ -6157,24 +6160,26 @@ nsresult QuotaManager::EnsureStorageIsInitialized() {
MOZ_SELECT_OVERLOAD(do_GetService),
MOZ_STORAGE_SERVICE_CONTRACTID));

QM_TRY_UNWRAP(auto connection,
QM_OR_ELSE_WARN_IF(
// Expression.
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss,
OpenUnsharedDatabase, storageFile),
// Predicate.
IsDatabaseCorruptionError,
// Fallback.
ErrToDefaultOk<nsCOMPtr<mozIStorageConnection>>));
QM_TRY_UNWRAP(
auto connection,
QM_OR_ELSE_WARN_IF(
// Expression.
MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss, OpenUnsharedDatabase,
storageFile, mozIStorageService::CONNECTION_DEFAULT),
// Predicate.
IsDatabaseCorruptionError,
// Fallback.
ErrToDefaultOk<nsCOMPtr<mozIStorageConnection>>));

if (!connection) {
// Nuke the database file.
QM_TRY(MOZ_TO_RESULT(storageFile->Remove(false)));

QM_TRY_UNWRAP(connection, MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
nsCOMPtr<mozIStorageConnection>, ss,
OpenUnsharedDatabase, storageFile));
OpenUnsharedDatabase, storageFile,
mozIStorageService::CONNECTION_DEFAULT));
}

// We want extra durability for this important file.
Expand Down
3 changes: 3 additions & 0 deletions dom/storage/StorageDBThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -531,19 +531,22 @@ nsresult StorageDBThread::OpenDatabaseConnection() {
MOZ_ASSERT(mDatabaseFile);

rv = service->OpenUnsharedDatabase(mDatabaseFile,
mozIStorageService::CONNECTION_DEFAULT,
getter_AddRefs(mWorkerConnection));
if (rv == NS_ERROR_FILE_CORRUPTED) {
// delete the db and try opening again
rv = mDatabaseFile->Remove(false);
NS_ENSURE_SUCCESS(rv, rv);
rv = service->OpenUnsharedDatabase(mDatabaseFile,
mozIStorageService::CONNECTION_DEFAULT,
getter_AddRefs(mWorkerConnection));
}
} else {
MOZ_ASSERT(mPrivateBrowsingId == 1);

rv = service->OpenSpecialDatabase(kMozStorageMemoryStorageKey,
"lsprivatedb"_ns,
mozIStorageService::CONNECTION_DEFAULT,
getter_AddRefs(mWorkerConnection));
}
NS_ENSURE_SUCCESS(rv, rv);
Expand Down
10 changes: 6 additions & 4 deletions extensions/permissions/PermissionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,11 +761,13 @@ nsresult PermissionManager::OpenDatabase(nsIFile* aPermissionsFile) {
}
// cache a connection to the hosts database
if (mMemoryOnlyDB) {
rv =
storage->OpenSpecialDatabase(kMozStorageMemoryStorageKey, VoidCString(),
getter_AddRefs(data->mDBConn));
rv = storage->OpenSpecialDatabase(
kMozStorageMemoryStorageKey, VoidCString(),
mozIStorageService::CONNECTION_DEFAULT, getter_AddRefs(data->mDBConn));
} else {
rv = storage->OpenDatabase(aPermissionsFile, getter_AddRefs(data->mDBConn));
rv = storage->OpenDatabase(aPermissionsFile,
mozIStorageService::CONNECTION_DEFAULT,
getter_AddRefs(data->mDBConn));
}
return rv;
}
Expand Down
10 changes: 6 additions & 4 deletions netwerk/cookie/CookiePersistentStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,9 @@ CookiePersistentStorage::OpenDBResult CookiePersistentStorage::TryInitDB(
// open a connection to the cookie database, and only cache our connection
// and statements upon success. The connection is opened unshared to
// eliminate cache contention between the main and background threads.
rv = mStorageService->OpenUnsharedDatabase(mCookieFile,
getter_AddRefs(mSyncConn));
rv = mStorageService->OpenUnsharedDatabase(
mCookieFile, mozIStorageService::CONNECTION_DEFAULT,
getter_AddRefs(mSyncConn));
NS_ENSURE_SUCCESS(rv, RESULT_RETRY);
}

Expand Down Expand Up @@ -1785,8 +1786,9 @@ void CookiePersistentStorage::InitDBConn() {
nsresult CookiePersistentStorage::InitDBConnInternal() {
MOZ_ASSERT(NS_IsMainThread());

nsresult rv = mStorageService->OpenUnsharedDatabase(mCookieFile,
getter_AddRefs(mDBConn));
nsresult rv = mStorageService->OpenUnsharedDatabase(
mCookieFile, mozIStorageService::CONNECTION_DEFAULT,
getter_AddRefs(mDBConn));
NS_ENSURE_SUCCESS(rv, rv);

// Set up our listeners.
Expand Down
5 changes: 3 additions & 2 deletions storage/mozIStorageAsyncConnection.idl
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ interface mozIStorageAsyncConnection : nsISupports {
/**
* Causes any pending database operation to abort and return at the first
* opportunity.
* This can only be used on read-only connections that don't implement
* the mozIStorageConnection interface.
* @note this cannot be used on mozIStorageConnection unless the connection is
* explicitly marked as `interruptible`. For more details, please
* refer to CONNECTION_INTERRUPTIBLE in mozIStorageService.
* @note operations that are nearly complete may still be able to complete.
* @throws if used on an unsupported connection type, or a closed connection.
*/
Expand Down
Loading

0 comments on commit 781b5ef

Please sign in to comment.