Bug 1754448 - Clean up the code for Storage::hasActiveSnapshot; r=dom-storage-reviewers,jari,webidl,smaug

Differential Revision: https://phabricator.services.mozilla.com/D138635
This commit is contained in:
Jan Varga 2022-02-17 15:09:58 +00:00
Родитель 475ea61051
Коммит 05c83e6276
7 изменённых файлов: 32 добавлений и 22 удалений

Просмотреть файл

@ -143,6 +143,10 @@ void LSDatabase::NoteFinishedSnapshot(LSSnapshot* aSnapshot) {
} }
} }
// All these methods assert `!mAllowedToClose` because they shoudn't be called
// if the database is being closed. Callers should first check the state by
// calling `IsAlloweToClose` and eventually obtain a new database.
nsresult LSDatabase::GetLength(LSObject* aObject, uint32_t* aResult) { nsresult LSDatabase::GetLength(LSObject* aObject, uint32_t* aResult) {
AssertIsOnOwningThread(); AssertIsOnOwningThread();
MOZ_ASSERT(aObject); MOZ_ASSERT(aObject);
@ -311,6 +315,14 @@ nsresult LSDatabase::EndExplicitSnapshot() {
return NS_OK; return NS_OK;
} }
bool LSDatabase::HasSnapshot() const {
AssertIsOnOwningThread();
MOZ_ASSERT(mActor);
MOZ_ASSERT(!mAllowedToClose);
return !!mSnapshot;
}
nsresult LSDatabase::EnsureSnapshot(LSObject* aObject, const nsAString& aKey, nsresult LSDatabase::EnsureSnapshot(LSObject* aObject, const nsAString& aKey,
bool aExplicit) { bool aExplicit) {
MOZ_ASSERT(aObject); MOZ_ASSERT(aObject);

Просмотреть файл

@ -55,12 +55,6 @@ class LSDatabase final {
mActor = nullptr; mActor = nullptr;
} }
bool HasActiveSnapshot() const {
AssertIsOnOwningThread();
return !!mSnapshot;
}
bool IsAllowedToClose() const { bool IsAllowedToClose() const {
AssertIsOnOwningThread(); AssertIsOnOwningThread();
@ -92,6 +86,8 @@ class LSDatabase final {
nsresult EndExplicitSnapshot(); nsresult EndExplicitSnapshot();
bool HasSnapshot() const;
private: private:
~LSDatabase(); ~LSDatabase();

Просмотреть файл

@ -860,22 +860,23 @@ void LSObject::EndExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
mInExplicitSnapshot = false; mInExplicitSnapshot = false;
} }
bool LSObject::GetHasActiveSnapshot(nsIPrincipal& aSubjectPrincipal, bool LSObject::GetHasSnapshot(nsIPrincipal& aSubjectPrincipal,
ErrorResult& aError) { ErrorResult& aError) {
AssertIsOnOwningThread(); AssertIsOnOwningThread();
if (!CanUseStorage(aSubjectPrincipal)) { if (!CanUseStorage(aSubjectPrincipal)) {
aError.Throw(NS_ERROR_DOM_SECURITY_ERR); aError.Throw(NS_ERROR_DOM_SECURITY_ERR);
return 0; return false;
} }
if (mDatabase && mDatabase->HasActiveSnapshot()) { // We can't call `HasSnapshot` on the database if it's being closed, but we
MOZ_ASSERT(!mDatabase->IsAllowedToClose()); // know that a database which is being closed can't have a snapshot, so we
// return false in that case directly here.
return true; if (!mDatabase || mDatabase->IsAllowedToClose()) {
return false;
} }
return false; return mDatabase->HasSnapshot();
} }
NS_IMPL_ADDREF_INHERITED(LSObject, Storage) NS_IMPL_ADDREF_INHERITED(LSObject, Storage)

Просмотреть файл

@ -182,8 +182,8 @@ class LSObject final : public Storage {
void EndExplicitSnapshot(nsIPrincipal& aSubjectPrincipal, void EndExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
ErrorResult& aError) override; ErrorResult& aError) override;
bool GetHasActiveSnapshot(nsIPrincipal& aSubjectPrincipal, bool GetHasSnapshot(nsIPrincipal& aSubjectPrincipal,
ErrorResult& aError) override; ErrorResult& aError) override;
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////

Просмотреть файл

@ -84,5 +84,5 @@ async function testSteps() {
await returnToEventLoop(); await returnToEventLoop();
ok(!storage.hasActiveSnapshot, "Snapshot successfully finished"); ok(!storage.hasSnapshot, "Snapshot successfully finished");
} }

Просмотреть файл

@ -121,8 +121,8 @@ class Storage : public nsISupports, public nsWrapperCache {
virtual void EndExplicitSnapshot(nsIPrincipal& aSubjectPrincipal, virtual void EndExplicitSnapshot(nsIPrincipal& aSubjectPrincipal,
ErrorResult& aRv) {} ErrorResult& aRv) {}
virtual bool GetHasActiveSnapshot(nsIPrincipal& aSubjectPrincipal, virtual bool GetHasSnapshot(nsIPrincipal& aSubjectPrincipal,
ErrorResult& aRv) { ErrorResult& aRv) {
return false; return false;
} }

Просмотреть файл

@ -73,9 +73,10 @@ partial interface Storage {
void endExplicitSnapshot(); void endExplicitSnapshot();
/** /**
* Returns true if the underlying database has been opened and it has an * Returns true if the underlying database has been opened, the database is
* active snapshot (initialized implicitly or explicitly). * not being closed and it has a snapshot (initialized implicitly or
* explicitly).
*/ */
[Throws, NeedsSubjectPrincipal, Pref="dom.storage.testing"] [Throws, NeedsSubjectPrincipal, Pref="dom.storage.testing"]
readonly attribute boolean hasActiveSnapshot; readonly attribute boolean hasSnapshot;
}; };