summaryrefslogtreecommitdiffstats
path: root/browser/components/migration/content/migration-dialog-window.js
blob: b16a348a35a976f0a3b5d40b47c5bf60ff00bc3a (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
/* 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 http://mozilla.org/MPL/2.0/. */

"use strict";

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  MigrationUtils: "resource:///modules/MigrationUtils.sys.mjs",
  MigrationWizardConstants:
    "chrome://browser/content/migration/migration-wizard-constants.mjs",
});

/**
 * This file manages a MigrationWizard embedded in a dialog that runs
 * in a top-level dialog window. It's main responsibility is to listen
 * for dialog-specific events from the embedded MigrationWizard and to
 * respond appropriately to them.
 *
 * A single object argument is expected to be passed when opening
 * this dialog.
 *
 * @param {object} window.arguments.0
 * @param {object} window.arguments.0.options
 *   A series of options for configuring the dialog. See
 *   MigrationUtils.showMigrationWizard for a description of this
 *   object.
 */

const MigrationDialog = {
  _wiz: null,

  init() {
    addEventListener("load", this);
  },

  onLoad() {
    this._wiz = document.getElementById("wizard");
    this._wiz.addEventListener("MigrationWizard:Close", this);
    document.addEventListener("keypress", this);

    let args = window.arguments[0];
    // When opened via nsIWindowWatcher.openWindow, the arguments are
    // passed through C++, and they arrive to us wrapped as an XPCOM
    // object. We use wrappedJSObject to get at the underlying JS
    // object.
    if (args instanceof Ci.nsISupports) {
      args = args.wrappedJSObject;
    }

    let observer = new ResizeObserver(() => {
      window.sizeToContent();
    });
    observer.observe(this._wiz);

    customElements.whenDefined("migration-wizard").then(() => {
      if (args.options?.skipSourceSelection) {
        // This is an automigration for a profile refresh, so begin migration
        // automatically once ready.
        this.doProfileRefresh(
          args.options.migratorKey,
          args.options.migrator,
          args.options.profileId
        );
      } else {
        this._wiz.requestState();
      }
    });
  },

  handleEvent(event) {
    switch (event.type) {
      case "load": {
        this.onLoad();
        break;
      }
      case "keypress": {
        if (event.keyCode == KeyEvent.DOM_VK_ESCAPE) {
          window.close();
        }
        break;
      }
      case "MigrationWizard:Close": {
        window.close();
        break;
      }
    }
  },

  async doProfileRefresh(migratorKey, migrator, profileId) {
    let profile = { id: profileId };
    let resourceTypeData = await migrator.getMigrateData(profile);
    let resourceTypeStrs = [];
    for (let type in lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES) {
      if (resourceTypeData & lazy.MigrationUtils.resourceTypes[type]) {
        resourceTypeStrs.push(
          lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES[type]
        );
      }
    }

    this._wiz.doAutoImport(migratorKey, profile, resourceTypeStrs);
    this._wiz.addEventListener(
      "MigrationWizard:DoneMigration",
      () => {
        setTimeout(() => {
          window.close();
        }, 5000);
      },
      { once: true }
    );
  },
};

MigrationDialog.init();