summaryrefslogtreecommitdiffstats
path: root/browser/components/backup/BackupService.sys.mjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:14:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:14:29 +0000
commitfbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 (patch)
tree4c1ccaf5486d4f2009f9a338a98a83e886e29c97 /browser/components/backup/BackupService.sys.mjs
parentReleasing progress-linux version 124.0.1-1~progress7.99u1. (diff)
downloadfirefox-fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8.tar.xz
firefox-fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8.zip
Merging upstream version 125.0.1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'browser/components/backup/BackupService.sys.mjs')
-rw-r--r--browser/components/backup/BackupService.sys.mjs103
1 files changed, 103 insertions, 0 deletions
diff --git a/browser/components/backup/BackupService.sys.mjs b/browser/components/backup/BackupService.sys.mjs
new file mode 100644
index 0000000000..853f4768ce
--- /dev/null
+++ b/browser/components/backup/BackupService.sys.mjs
@@ -0,0 +1,103 @@
+/* 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 http://mozilla.org/MPL/2.0/. */
+
+import * as BackupResources from "resource:///modules/backup/BackupResources.sys.mjs";
+
+const lazy = {};
+
+ChromeUtils.defineLazyGetter(lazy, "logConsole", function () {
+ return console.createInstance({
+ prefix: "BackupService",
+ maxLogLevel: Services.prefs.getBoolPref("browser.backup.log", false)
+ ? "Debug"
+ : "Warn",
+ });
+});
+
+/**
+ * The BackupService class orchestrates the scheduling and creation of profile
+ * backups. It also does most of the heavy lifting for the restoration of a
+ * profile backup.
+ */
+export class BackupService {
+ /**
+ * The BackupService singleton instance.
+ *
+ * @static
+ * @type {BackupService|null}
+ */
+ static #instance = null;
+
+ /**
+ * Map of instantiated BackupResource classes.
+ *
+ * @type {Map<string, BackupResource>}
+ */
+ #resources = new Map();
+
+ /**
+ * Returns a reference to a BackupService singleton. If this is the first time
+ * that this getter is accessed, this causes the BackupService singleton to be
+ * be instantiated.
+ *
+ * @static
+ * @type {BackupService}
+ */
+ static init() {
+ if (this.#instance) {
+ return this.#instance;
+ }
+ this.#instance = new BackupService(BackupResources);
+ this.#instance.takeMeasurements();
+
+ return this.#instance;
+ }
+
+ /**
+ * Create a BackupService instance.
+ *
+ * @param {object} [backupResources=BackupResources] - Object containing BackupResource classes to associate with this service.
+ */
+ constructor(backupResources = BackupResources) {
+ lazy.logConsole.debug("Instantiated");
+
+ for (const resourceName in backupResources) {
+ let resource = BackupResources[resourceName];
+ this.#resources.set(resource.key, resource);
+ }
+ }
+
+ /**
+ * Take measurements of the current profile state for Telemetry.
+ *
+ * @returns {Promise<undefined>}
+ */
+ async takeMeasurements() {
+ lazy.logConsole.debug("Taking Telemetry measurements");
+
+ // Note: We're talking about kilobytes here, not kibibytes. That means
+ // 1000 bytes, and not 1024 bytes.
+ const BYTES_IN_KB = 1000;
+ const BYTES_IN_MB = 1000000;
+
+ // We'll start by measuring the available disk space on the storage
+ // device that the profile directory is on.
+ let profileDir = await IOUtils.getFile(PathUtils.profileDir);
+
+ let profDDiskSpaceBytes = profileDir.diskSpaceAvailable;
+
+ // Make the measurement fuzzier by rounding to the nearest 10MB.
+ let profDDiskSpaceMB =
+ Math.round(profDDiskSpaceBytes / BYTES_IN_MB / 100) * 100;
+
+ // And then record the value in kilobytes, since that's what everything
+ // else is going to be measured in.
+ Glean.browserBackup.profDDiskSpace.set(profDDiskSpaceMB * BYTES_IN_KB);
+
+ // Measure the size of each file we are going to backup.
+ for (let resourceClass of this.#resources.values()) {
+ await new resourceClass().measure(PathUtils.profileDir);
+ }
+ }
+}