summaryrefslogtreecommitdiffstats
path: root/browser/components/backup/resources/MiscDataBackupResource.sys.mjs
blob: 97224f0e310c26a02ab9ec997bec12e6b19dd938 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* 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, {
  Sqlite: "resource://gre/modules/Sqlite.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",
    ];

    for (let fileName of files) {
      let sourcePath = PathUtils.join(profilePath, fileName);
      let destPath = PathUtils.join(stagingPath, fileName);
      if (await IOUtils.exists(sourcePath)) {
        await IOUtils.copy(sourcePath, destPath, { recursive: true });
      }
    }

    const sqliteDatabases = ["protections.sqlite"];

    for (let fileName of sqliteDatabases) {
      let sourcePath = PathUtils.join(profilePath, fileName);
      let destPath = PathUtils.join(stagingPath, fileName);
      let connection;

      try {
        connection = await lazy.Sqlite.openConnection({
          path: sourcePath,
          readOnly: true,
        });

        await connection.backup(destPath);
      } finally {
        await connection.close();
      }
    }

    // Bug 1890585 - we don't currently have the ability to copy the
    // chrome-privileged IndexedDB databases under storage/permanent/chrome, so
    // we'll just skip that for now.

    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);
  }
}