summaryrefslogtreecommitdiffstats
path: root/browser/components/backup/resources/AddonsBackupResource.sys.mjs
blob: 29b51b8a7fe13f4b0cf407f98c0172dc8d238a26 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* 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";

/**
 * Backup for addons and extensions files and data.
 */
export class AddonsBackupResource extends BackupResource {
  static get key() {
    return "addons";
  }

  static get requiresEncryption() {
    return false;
  }

  async backup(stagingPath, profilePath = PathUtils.profileDir) {
    // Files and directories to backup.
    let toCopy = [
      "extensions.json",
      "extension-settings.json",
      "extension-preferences.json",
      "addonStartup.json.lz4",
      "browser-extension-data",
      "extension-store-permissions",
    ];
    await BackupResource.copyFiles(profilePath, stagingPath, toCopy);

    // Backup only the XPIs in the extensions directory.
    let xpiFiles = [];
    let extensionsXPIDirectoryPath = PathUtils.join(profilePath, "extensions");
    let xpiDirectoryChildren = await IOUtils.getChildren(
      extensionsXPIDirectoryPath,
      {
        ignoreAbsent: true,
      }
    );
    for (const childFilePath of xpiDirectoryChildren) {
      if (childFilePath.endsWith(".xpi")) {
        let childFileName = PathUtils.filename(childFilePath);
        xpiFiles.push(childFileName);
      }
    }
    // Create the extensions directory in the staging directory.
    let stagingExtensionsXPIDirectoryPath = PathUtils.join(
      stagingPath,
      "extensions"
    );
    await IOUtils.makeDirectory(stagingExtensionsXPIDirectoryPath);
    // Copy all found XPIs to the staging directory.
    await BackupResource.copyFiles(
      extensionsXPIDirectoryPath,
      stagingExtensionsXPIDirectoryPath,
      xpiFiles
    );

    // Copy storage sync database.
    let databases = ["storage-sync-v2.sqlite"];
    await BackupResource.copySqliteDatabases(
      profilePath,
      stagingPath,
      databases
    );

    return null;
  }

  async recover(_manifestEntry, recoveryPath, destProfilePath) {
    const files = [
      "extensions.json",
      "extension-settings.json",
      "extension-preferences.json",
      "addonStartup.json.lz4",
      "browser-extension-data",
      "extension-store-permissions",
      "extensions",
      "storage-sync-v2.sqlite",
    ];
    await BackupResource.copyFiles(recoveryPath, destProfilePath, files);

    return null;
  }

  async measure(profilePath = PathUtils.profileDir) {
    // Report the total size of the extension json files.
    const jsonFiles = [
      "extensions.json",
      "extension-settings.json",
      "extension-preferences.json",
      "addonStartup.json.lz4",
    ];
    let extensionsJsonSize = 0;
    for (const filePath of jsonFiles) {
      let resourcePath = PathUtils.join(profilePath, filePath);
      let resourceSize = await BackupResource.getFileSize(resourcePath);
      if (Number.isInteger(resourceSize)) {
        extensionsJsonSize += resourceSize;
      }
    }
    Glean.browserBackup.extensionsJsonSize.set(extensionsJsonSize);

    // Report the size of permissions store data, if present.
    let extensionStorePermissionsDataPath = PathUtils.join(
      profilePath,
      "extension-store-permissions",
      "data.safe.bin"
    );
    let extensionStorePermissionsDataSize = await BackupResource.getFileSize(
      extensionStorePermissionsDataPath
    );
    if (Number.isInteger(extensionStorePermissionsDataSize)) {
      Glean.browserBackup.extensionStorePermissionsDataSize.set(
        extensionStorePermissionsDataSize
      );
    }

    // Report the size of extensions storage sync database.
    let storageSyncPath = PathUtils.join(profilePath, "storage-sync-v2.sqlite");
    let storageSyncSize = await BackupResource.getFileSize(storageSyncPath);
    Glean.browserBackup.storageSyncSize.set(storageSyncSize);

    // Report the total size of XPI files in the extensions directory.
    let extensionsXPIDirectoryPath = PathUtils.join(profilePath, "extensions");
    let extensionsXPIDirectorySize = await BackupResource.getDirectorySize(
      extensionsXPIDirectoryPath,
      {
        shouldExclude: (filePath, fileType) =>
          fileType !== "regular" || !filePath.endsWith(".xpi"),
      }
    );
    Glean.browserBackup.extensionsXpiDirectorySize.set(
      extensionsXPIDirectorySize
    );

    // Report the total size of the browser extension data.
    let browserExtensionDataPath = PathUtils.join(
      profilePath,
      "browser-extension-data"
    );
    let browserExtensionDataSize = await BackupResource.getDirectorySize(
      browserExtensionDataPath
    );
    Glean.browserBackup.browserExtensionDataSize.set(browserExtensionDataSize);

    // Report the size of all moz-extension IndexedDB databases.
    let defaultStoragePath = PathUtils.join(profilePath, "storage", "default");
    let extensionsStorageSize = await BackupResource.getDirectorySize(
      defaultStoragePath,
      {
        shouldExclude: (filePath, _fileType, parentPath) => {
          if (
            parentPath == defaultStoragePath &&
            !PathUtils.filename(filePath).startsWith("moz-extension")
          ) {
            return true;
          }
          return false;
        },
      }
    );
    if (Number.isInteger(extensionsStorageSize)) {
      Glean.browserBackup.extensionsStorageSize.set(extensionsStorageSize);
    }
  }
}