tor-browser

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

migration-dialog-window.js (3305B)


      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 file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const lazy = {};
      8 
      9 ChromeUtils.defineESModuleGetters(lazy, {
     10  MigrationUtils: "resource:///modules/MigrationUtils.sys.mjs",
     11  MigrationWizardConstants:
     12    "chrome://browser/content/migration/migration-wizard-constants.mjs",
     13 });
     14 
     15 /**
     16 * This file manages a MigrationWizard embedded in a dialog that runs
     17 * in a top-level dialog window. It's main responsibility is to listen
     18 * for dialog-specific events from the embedded MigrationWizard and to
     19 * respond appropriately to them.
     20 *
     21 * A single object argument is expected to be passed when opening
     22 * this dialog.
     23 *
     24 * @param {object} window.arguments.0
     25 * @param {object} window.arguments.0.options
     26 *   A series of options for configuring the dialog. See
     27 *   MigrationUtils.showMigrationWizard for a description of this
     28 *   object.
     29 */
     30 
     31 const MigrationDialog = {
     32  _wiz: null,
     33 
     34  init() {
     35    addEventListener("load", this);
     36  },
     37 
     38  onLoad() {
     39    this._wiz = document.getElementById("wizard");
     40    this._wiz.addEventListener("MigrationWizard:Close", this);
     41    document.addEventListener("keypress", this);
     42 
     43    let args = window.arguments[0];
     44    // When opened via nsIWindowWatcher.openWindow, the arguments are
     45    // passed through C++, and they arrive to us wrapped as an XPCOM
     46    // object. We use wrappedJSObject to get at the underlying JS
     47    // object.
     48    if (args instanceof Ci.nsISupports) {
     49      args = args.wrappedJSObject;
     50    }
     51 
     52    let observer = new ResizeObserver(() => {
     53      window.sizeToContent();
     54    });
     55    observer.observe(this._wiz);
     56 
     57    customElements.whenDefined("migration-wizard").then(() => {
     58      if (args.options?.skipSourceSelection) {
     59        // This is an automigration for a profile refresh, so begin migration
     60        // automatically once ready.
     61        this.doProfileRefresh(
     62          args.options.migratorKey,
     63          args.options.migrator,
     64          args.options.profileId
     65        );
     66      } else {
     67        this._wiz.requestState();
     68      }
     69    });
     70  },
     71 
     72  handleEvent(event) {
     73    switch (event.type) {
     74      case "load": {
     75        this.onLoad();
     76        break;
     77      }
     78      case "keypress": {
     79        if (event.keyCode == KeyEvent.DOM_VK_ESCAPE) {
     80          window.close();
     81        }
     82        break;
     83      }
     84      case "MigrationWizard:Close": {
     85        window.close();
     86        break;
     87      }
     88    }
     89  },
     90 
     91  async doProfileRefresh(migratorKey, migrator, profileId) {
     92    let profile = { id: profileId };
     93    let resourceTypeData = await migrator.getMigrateData(profile);
     94    let resourceTypeStrs = [];
     95    for (let type in lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES) {
     96      if (resourceTypeData & lazy.MigrationUtils.resourceTypes[type]) {
     97        resourceTypeStrs.push(
     98          lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES[type]
     99        );
    100      }
    101    }
    102 
    103    this._wiz.doAutoImport(migratorKey, profile, resourceTypeStrs);
    104    this._wiz.addEventListener(
    105      "MigrationWizard:DoneMigration",
    106      () => {
    107        setTimeout(() => {
    108          window.close();
    109        }, 5000);
    110      },
    111      { once: true }
    112    );
    113  },
    114 };
    115 
    116 MigrationDialog.init();