Files
tubestation/browser/components/backup/resources/MiscDataBackupResource.sys.mjs

103 lines
3.1 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";
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
ActivityStreamStorage:
"resource://activity-stream/lib/ActivityStreamStorage.sys.mjs",
});
/**
* Class representing miscellaneous files for telemetry, site storage,
* media device origin mapping, chrome privileged IndexedDB databases,
* and Mozilla Accounts within a user profile.
*/
export class MiscDataBackupResource extends BackupResource {
static get key() {
return "miscellaneous";
}
static get requiresEncryption() {
return false;
}
async backup(stagingPath, profilePath = PathUtils.profileDir) {
const files = [
"times.json",
"enumerate_devices.txt",
"SiteSecurityServiceState.bin",
];
await BackupResource.copyFiles(profilePath, stagingPath, files);
const sqliteDatabases = ["protections.sqlite"];
await BackupResource.copySqliteDatabases(
profilePath,
stagingPath,
sqliteDatabases
);
// Bug 1890585 - we don't currently have the ability to copy the
// chrome-privileged IndexedDB databases under storage/permanent/chrome.
// Instead, we'll manually export any IndexedDB data we need to backup
// to a separate JSON file.
// The first IndexedDB database we want to back up is the ActivityStream
// one - specifically, the "snippets" table, as this contains information
// on ASRouter impressions, blocked messages, message group impressions,
// etc.
const SNIPPETS_TABLE_NAME = "snippets";
let storage = new lazy.ActivityStreamStorage({
storeNames: [SNIPPETS_TABLE_NAME],
});
let snippetsTable = await storage.getDbTable(SNIPPETS_TABLE_NAME);
let snippetsObj = {};
for (let key of await snippetsTable.getAllKeys()) {
snippetsObj[key] = await snippetsTable.get(key);
}
let snippetsBackupFile = PathUtils.join(
stagingPath,
"activity-stream-snippets.json"
);
await IOUtils.writeJSON(snippetsBackupFile, snippetsObj);
return null;
}
async measure(profilePath = PathUtils.profileDir) {
const files = [
"times.json",
"enumerate_devices.txt",
"protections.sqlite",
"SiteSecurityServiceState.bin",
];
let fullSize = 0;
for (let filePath of files) {
let resourcePath = PathUtils.join(profilePath, filePath);
let resourceSize = await BackupResource.getFileSize(resourcePath);
if (Number.isInteger(resourceSize)) {
fullSize += resourceSize;
}
}
let chromeIndexedDBDirPath = PathUtils.join(
profilePath,
"storage",
"permanent",
"chrome"
);
let chromeIndexedDBDirSize = await BackupResource.getDirectorySize(
chromeIndexedDBDirPath
);
if (Number.isInteger(chromeIndexedDBDirSize)) {
fullSize += chromeIndexedDBDirSize;
}
Glean.browserBackup.miscDataSize.set(fullSize);
}
}