tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PlacesBackupResource.sys.mjs (2649B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
      4 
      5 import { BackupResource } from "resource:///modules/backup/BackupResource.sys.mjs";
      6 import { MeasurementUtils } from "resource:///modules/backup/MeasurementUtils.sys.mjs";
      7 
      8 const lazy = {};
      9 
     10 ChromeUtils.defineESModuleGetters(lazy, {
     11  PlacesDBUtils: "resource://gre/modules/PlacesDBUtils.sys.mjs",
     12 });
     13 
     14 /**
     15 * Class representing Places database related files within a user profile.
     16 */
     17 export class PlacesBackupResource extends BackupResource {
     18  static get key() {
     19    return "places";
     20  }
     21 
     22  static get requiresEncryption() {
     23    return false;
     24  }
     25 
     26  static get priority() {
     27    return 1;
     28  }
     29 
     30  static get canBackupResource() {
     31    return BackupResource.backingUpPlaces;
     32  }
     33 
     34  async backup(
     35    stagingPath,
     36    profilePath = PathUtils.profileDir,
     37    _isEncrypting = false
     38  ) {
     39    // These are copied in parallel because they're attached[1], and we don't
     40    // want them to get out of sync with one another.
     41    //
     42    // [1]: https://www.sqlite.org/lang_attach.html
     43    let timedCopies = [
     44      MeasurementUtils.measure(
     45        Glean.browserBackup.placesTime,
     46        BackupResource.copySqliteDatabases(profilePath, stagingPath, [
     47          "places.sqlite",
     48        ])
     49      ),
     50      MeasurementUtils.measure(
     51        Glean.browserBackup.faviconsTime,
     52        BackupResource.copySqliteDatabases(profilePath, stagingPath, [
     53          "favicons.sqlite",
     54        ])
     55      ),
     56    ];
     57    await Promise.all(timedCopies);
     58 
     59    // Now that both databases are copied, open the places db copy to remove
     60    // downloaded files, since they won't be valid in the restored profile.
     61    await lazy.PlacesDBUtils.removeDownloadsMetadataFromDb(
     62      PathUtils.join(stagingPath, "places.sqlite")
     63    );
     64 
     65    return null;
     66  }
     67 
     68  async recover(manifestEntry, recoveryPath, destProfilePath) {
     69    const simpleCopyFiles = ["places.sqlite", "favicons.sqlite"];
     70    await BackupResource.copyFiles(
     71      recoveryPath,
     72      destProfilePath,
     73      simpleCopyFiles
     74    );
     75    return null;
     76  }
     77 
     78  async measure(profilePath = PathUtils.profileDir) {
     79    let placesDBPath = PathUtils.join(profilePath, "places.sqlite");
     80    let faviconsDBPath = PathUtils.join(profilePath, "favicons.sqlite");
     81    let placesDBSize = await BackupResource.getFileSize(placesDBPath);
     82    let faviconsDBSize = await BackupResource.getFileSize(faviconsDBPath);
     83 
     84    Glean.browserBackup.placesSize.set(placesDBSize);
     85    Glean.browserBackup.faviconsSize.set(faviconsDBSize);
     86  }
     87 }