Bug 1721452 - Delete keyframes database from Snapshots.jsm. r=mak

Differential Revision: https://phabricator.services.mozilla.com/D120379
This commit is contained in:
Harry Twyford
2021-07-23 15:51:19 +00:00
parent 68a332f511
commit 0cc6070f1a
2 changed files with 72 additions and 0 deletions

View File

@@ -10,6 +10,8 @@ const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
const VERSION_PREF = "browser.places.snapshots.version";
XPCOMUtils.defineLazyModuleGetters(this, {
PlacesUtils: "resource://gre/modules/PlacesUtils.jsm",
Services: "resource://gre/modules/Services.jsm",
@@ -225,6 +227,7 @@ const Snapshots = new (class Snapshots {
* Returns snapshots in order of descending last interaction time.
*/
async query({ limit = 100, includeTombstones = false } = {}) {
await this.#ensureVersionUpdates();
let db = await PlacesUtils.promiseDBConnection();
let whereStatement = "";
@@ -249,6 +252,37 @@ const Snapshots = new (class Snapshots {
return rows.map(row => this.#translateRow(row));
}
/**
* Ensures that the database is migrated to the latest version. Migrations
* should be exception-safe: don't throw an uncaught Error, or else we'll skip
* subsequent migrations.
*/
async #ensureVersionUpdates() {
let dbVersion = Services.prefs.getIntPref(VERSION_PREF, 0);
try {
if (dbVersion < 1) {
try {
// Delete legacy keyframes.sqlite DB.
let profileDir = await PathUtils.getProfileDir();
let pathToKeyframes = PathUtils.join(profileDir, "keyframes.sqlite");
await IOUtils.remove(pathToKeyframes);
} catch (ex) {
console.warn(`Failed to delete keyframes.sqlite: ${ex}`);
}
}
} finally {
Services.prefs.setIntPref(VERSION_PREF, this.currentVersion);
}
}
/**
* Returns the database's most recent version number.
* @returns {number}
*/
get currentVersion() {
return 1;
}
/**
* Translates a database row to a Snapshot.
*

View File

@@ -8,6 +8,12 @@
const TEST_URL1 = "https://example.com/";
const TEST_URL2 = "https://example.com/12345";
const TEST_URL3 = "https://example.com/14235";
const VERSION_PREF = "browser.places.snapshots.version";
XPCOMUtils.defineLazyModuleGetters(this, {
Services: "resource://gre/modules/Services.jsm",
Sqlite: "resource://gre/modules/Sqlite.jsm",
});
add_task(async function setup() {
let now = Date.now();
@@ -137,3 +143,35 @@ add_task(async function test_delete_snapshot() {
userPersisted: true,
});
});
add_task(async function deleteKeyframesDb() {
Services.prefs.setIntPref(VERSION_PREF, 0);
let profileDir = await PathUtils.getProfileDir();
let pathToKeyframes = PathUtils.join(profileDir, "keyframes.sqlite");
try {
let db = await Sqlite.openConnection({
path: pathToKeyframes,
});
await db.close();
Assert.ok(
await IOUtils.exists(pathToKeyframes),
"Sanity check: keyframes.sqlite exists."
);
await Snapshots.query({ url: TEST_URL1 });
Assert.ok(
!(await IOUtils.exists(pathToKeyframes)),
"Keyframes.sqlite was deleted."
);
} catch (ex) {
console.warn(`Error occured in deleteKeyframesDb: ${ex}`);
}
Assert.equal(
Services.prefs.getIntPref(VERSION_PREF, 0),
Snapshots.currentVersion,
"Calling Snapshots.query successfully updated to the most recent schema version."
);
});