hUpdateLock(async () => {
      await Services.prefs.backupPrefFile(
        prefsDestFile,
        PreferencesBackupResource.addPrefsToIgnoreInBackup(
          lazy.ExperimentAPI.manager.store.getOriginalPrefValuesForAllActiveEnrollments()
        )
      );
    });

    // During recovery, we need to recompute verification hashes for any
    // custom engines, but only for engines that were originally passing
    // verification. We'll store the profile path at backup time in our
    // ManifestEntry so that we can do that verification check at recover-time.
    return { profilePath };
  }

  async recover(manifestEntry, recoveryPath, destProfilePath) {
    const SEARCH_PREF_FILENAME = "search.json.mozlz4";
    const RECOVERY_SEARCH_PREF_PATH = PathUtils.join(
      recoveryPath,
      SEARCH_PREF_FILENAME
    );

    if (await IOUtils.exists(RECOVERY_SEARCH_PREF_PATH)) {
      // search.json.mozlz4 may contain hash values that need to be recomputed
      // now that the profile directory has changed.
      let searchPrefs = await IOUtils.readJSON(RECOVERY_SEARCH_PREF_PATH, {
        decompress: true,
      });

      // ... but we only want to do this for engines that had valid verification
      // hashes for the original profile path.
      const ORIGINAL_PROFILE_PATH = manifestEntry.profilePath;

      if (ORIGINAL_PROFILE_PATH) {
        searchPrefs.engines = searchPrefs.engines.map(engine => {
          if (engine._metaData.loadPathHash) {
            let loadPath = engine._loadPath;
            if (
              engine._metaData.loadPathHash ==
              lazy.SearchUtils.getVerificationHash(
                loadPath,
                ORIGINAL_PROFILE_PATH
              )
            ) {
              engine._metaData.loadPathHash =
                lazy.SearchUtils.getVerificationHash(loadPath, destProfilePath);
            }
          }
          return engine;
        });

        if (
          searchPrefs.metaData.defaultEngineIdHash &&
          searchPrefs.metaData.defaultEngineIdHash ==
            lazy.SearchUtils.getVerificationHash(
              searchPrefs.metaData.defaultEngineId,
              ORIGINAL_PROFILE_PATH
            )
        ) {
          searchPrefs.metaData.defaultEngineIdHash =
            lazy.SearchUtils.getVerificationHash(
              searchPrefs.metaData.defaultEngineId,
              destProfilePath
            );
        }

        if (
          searchPrefs.metaData.privateDefaultEngineIdHash &&
          searchPrefs.metaData.privateDefaultEngineIdHash ==
            lazy.SearchUtils.getVerificationHash(
              searchPrefs.metaData.privateDefaultEngineId,
              ORIGINAL_PROFILE_PATH
            )
        ) {
          searchPrefs.metaData.privateDefaultEngineIdHash =
            lazy.SearchUtils.getVerificationHash(
              searchPrefs.metaData.privateDefaultEngineId,
              destProfilePath
            );
        }
      }

      await IOUtils.writeJSON(
        PathUtils.join(destProfilePath, SEARCH_PREF_FILENAME),
        searchPrefs,
        { compress: true }
      );
    }

    const simpleCopyFiles = [
      "prefs.js",
      "xulstore.json",
      "containers.json",
      "handlers.json",
      "user.js",
      "chrome",
    ];
    await BackupResource.copyFiles(
      recoveryPath,
      destProfilePath,
      simpleCopyFiles
    );

    // Append browser.backup.scheduled.last-backup-file to prefs.js with the
    // current timestamp.
    const LINEBREAK = AppConstants.platform === "win" ? "\r\n" : "\n";
    let prefsFile = await IOUtils.getFile(destProfilePath);
    prefsFile.append("prefs.js");
    // We should always have recovered a prefs.js but, if we didn't for any
    // reason, we can still write the timestamp.  Since we are creating the
    // prefs.js file, we need to add the preamble.
    const includePreamble = !(await IOUtils.exists(prefsFile.path));
    let addToPrefsJs = includePreamble ? Services.prefs.prefsJsPreamble : "";
    addToPrefsJs += `user_pref("${PROFILE_RESTORATION_DATE_PREF}", ${Math.round(Date.now() / 1000)});${LINEBREAK}`;
    await IOUtils.writeUTF8(prefsFile.path, addToPrefsJs, {
      mode: "appendOrCreate",
    });
    return null;
  }

  async measure(profilePath = PathUtils.profileDir) {
    const files = [
      "prefs.js",
      "xulstore.json",
      "containers.json",
      "handlers.json",
      "search.json.mozlz4",
      "user.js",
    ];
    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;
      }
    }

    const chromeDirectoryPath = PathUtils.join(profilePath, "chrome");
    let chromeDirectorySize =
      await BackupResource.getDirectorySize(chromeDirectoryPath);
    if (Number.isInteger(chromeDirectorySize)) {
      fullSize += chromeDirectorySize;
    }

    Glean.browserBackup.preferencesSize.set(fullSize);
  }
}
PK