Files
tubestation/browser/components/backup/resources/PlacesBackupResource.sys.mjs
Fred Chasen 14509a1366 Bug 1892007 - Add optional priority number to BackupResource. r=backup-reviewers,mconley
- Adds a static `priority` getter in BackupResource base class, which defaults to 0.
- Determine the order resources will be backed up sorted on that priority in `createBackup`, defaulting to unordered.
- Sets `PlacesBackupResource` position 1 so it will run first.

Differential Revision: https://phabricator.services.mozilla.com/D207934
2024-04-22 23:15:07 +00:00

91 lines
2.8 KiB
JavaScript

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { BackupResource } from "resource:///modules/backup/BackupResource.sys.mjs";
import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
BookmarkJSONUtils: "resource://gre/modules/BookmarkJSONUtils.sys.mjs",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.sys.mjs",
});
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"isBrowsingHistoryEnabled",
"places.history.enabled",
true
);
XPCOMUtils.defineLazyPreferenceGetter(
lazy,
"isSanitizeOnShutdownEnabled",
"privacy.sanitize.sanitizeOnShutdown",
false
);
/**
* Class representing Places database related files within a user profile.
*/
export class PlacesBackupResource extends BackupResource {
static get key() {
return "places";
}
static get requiresEncryption() {
return false;
}
static get priority() {
return 1;
}
async backup(stagingPath, profilePath = PathUtils.profileDir) {
let canBackupHistory =
!lazy.PrivateBrowsingUtils.permanentPrivateBrowsing &&
!lazy.isSanitizeOnShutdownEnabled &&
lazy.isBrowsingHistoryEnabled;
/**
* Do not backup places.sqlite and favicons.sqlite if users have history disabled, want history cleared on shutdown or are using permanent private browsing mode.
* Instead, export all existing bookmarks to a compressed JSON file that we can read when restoring the backup.
*/
if (!canBackupHistory) {
let bookmarksBackupFile = PathUtils.join(
stagingPath,
"bookmarks.jsonlz4"
);
await lazy.BookmarkJSONUtils.exportToFile(bookmarksBackupFile, {
compress: true,
});
return { bookmarksOnly: true };
}
// These are copied in parallel because they're attached[1], and we don't
// want them to get out of sync with one another.
//
// [1]: https://www.sqlite.org/lang_attach.html
await Promise.all([
BackupResource.copySqliteDatabases(profilePath, stagingPath, [
"places.sqlite",
]),
BackupResource.copySqliteDatabases(profilePath, stagingPath, [
"favicons.sqlite",
]),
]);
return null;
}
async measure(profilePath = PathUtils.profileDir) {
let placesDBPath = PathUtils.join(profilePath, "places.sqlite");
let faviconsDBPath = PathUtils.join(profilePath, "favicons.sqlite");
let placesDBSize = await BackupResource.getFileSize(placesDBPath);
let faviconsDBSize = await BackupResource.getFileSize(faviconsDBPath);
Glean.browserBackup.placesSize.set(placesDBSize);
Glean.browserBackup.faviconsSize.set(faviconsDBSize);
}
}