summaryrefslogtreecommitdiffstats
path: root/browser/components/migration/MigrationWizardParent.sys.mjs
blob: dd9ef3b85cfbdb616435b7edcca819012a763898 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
/* 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/. */

import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
import { MigrationUtils } from "resource:///modules/MigrationUtils.sys.mjs";
import { E10SUtils } from "resource://gre/modules/E10SUtils.sys.mjs";

const lazy = {};

ChromeUtils.defineLazyGetter(lazy, "gFluentStrings", function () {
  return new Localization([
    "branding/brand.ftl",
    "browser/migrationWizard.ftl",
  ]);
});

ChromeUtils.defineESModuleGetters(lazy, {
  FirefoxProfileMigrator: "resource:///modules/FirefoxProfileMigrator.sys.mjs",
  InternalTestingProfileMigrator:
    "resource:///modules/InternalTestingProfileMigrator.sys.mjs",
  LoginCSVImport: "resource://gre/modules/LoginCSVImport.sys.mjs",
  MigrationWizardConstants:
    "chrome://browser/content/migration/migration-wizard-constants.mjs",
  PasswordFileMigrator: "resource:///modules/FileMigrators.sys.mjs",
});

if (AppConstants.platform == "macosx") {
  ChromeUtils.defineESModuleGetters(lazy, {
    SafariProfileMigrator: "resource:///modules/SafariProfileMigrator.sys.mjs",
  });
}

/**
 * Set to true once the first instance of MigrationWizardParent has received
 * a "GetAvailableMigrators" message.
 */
let gHasOpenedBefore = false;

/**
 * This class is responsible for communicating with MigrationUtils to do the
 * actual heavy-lifting of any kinds of migration work, based on messages from
 * the associated MigrationWizardChild.
 */
export class MigrationWizardParent extends JSWindowActorParent {
  constructor() {
    super();
    Services.telemetry.setEventRecordingEnabled("browser.migration", true);
  }

  didDestroy() {
    Services.obs.notifyObservers(this, "MigrationWizard:Destroyed");
    MigrationUtils.finishMigration();
  }

  /**
   * General message handler function for messages received from the
   * associated MigrationWizardChild JSWindowActor.
   *
   * @param {ReceiveMessageArgument} message
   *   The message received from the MigrationWizardChild.
   * @returns {Promise}
   */
  async receiveMessage(message) {
    // Some belt-and-suspenders here, mainly because the migration-wizard
    // component can be embedded in less privileged content pages, so let's
    // make sure that any messages from content are coming from the privileged
    // about content process type.
    if (
      !this.browsingContext.currentWindowGlobal.isInProcess &&
      this.browsingContext.currentRemoteType !=
        E10SUtils.PRIVILEGEDABOUT_REMOTE_TYPE
    ) {
      throw new Error(
        "MigrationWizardParent: received message from the wrong content process type."
      );
    }

    switch (message.name) {
      case "GetAvailableMigrators": {
        let start = Cu.now();

        let availableMigrators = [];
        for (const key of MigrationUtils.availableMigratorKeys) {
          availableMigrators.push(this.#getMigratorAndProfiles(key));
        }

        // Wait for all getMigrator calls to resolve in parallel
        let results = await Promise.all(availableMigrators);

        for (const migrator of MigrationUtils.availableFileMigrators.values()) {
          results.push(await this.#serializeFileMigrator(migrator));
        }

        // Each migrator might give us a single MigratorProfileInstance,
        // or an Array of them, so we flatten them out and filter out
        // any that ended up going wrong and returning null from the
        // #getMigratorAndProfiles call.
        let filteredResults = results
          .flat()
          .filter(result => result)
          .sort((a, b) => {
            return b.lastModifiedDate - a.lastModifiedDate;
          });

        let elapsed = Cu.now() - start;
        if (!gHasOpenedBefore) {
          gHasOpenedBefore = true;
          Services.telemetry.scalarSet(
            "migration.time_to_produce_migrator_list",
            elapsed
          );
        }

        return filteredResults;
      }

      case "Migrate": {
        let { migrationDetails, extraArgs } = message.data;
        if (
          migrationDetails.type ==
          lazy.MigrationWizardConstants.MIGRATOR_TYPES.BROWSER
        ) {
          return this.#doBrowserMigration(migrationDetails, extraArgs);
        } else if (
          migrationDetails.type ==
          lazy.MigrationWizardConstants.MIGRATOR_TYPES.FILE
        ) {
          let window = this.browsingContext.topChromeWindow;
          await this.#doFileMigration(window, migrationDetails.key);
          return extraArgs;
        }
        break;
      }

      case "CheckPermissions": {
        if (
          message.data.type ==
          lazy.MigrationWizardConstants.MIGRATOR_TYPES.BROWSER
        ) {
          let migrator = await MigrationUtils.getMigrator(message.data.key);
          return migrator.hasPermissions();
        }
        return true;
      }

      case "RequestSafariPermissions": {
        let safariMigrator = await MigrationUtils.getMigrator("safari");
        return safariMigrator.getPermissions(
          this.browsingContext.topChromeWindow
        );
      }

      case "SelectSafariPasswordFile": {
        return this.#selectSafariPasswordFile(
          this.browsingContext.topChromeWindow
        );
      }

      case "RecordEvent": {
        this.#recordEvent(message.data.type, message.data.args);
        break;
      }

      case "OpenAboutAddons": {
        let browser = this.browsingContext.top.embedderElement;
        this.#openAboutAddons(browser);
        break;
      }

      case "GetPermissions": {
        let migrator = await MigrationUtils.getMigrator(message.data.key);
        return migrator.getPermissions(this.browsingContext.topChromeWindow);
      }
    }

    return null;
  }

  /**
   * Used for recording telemetry in the migration wizard.
   *
   * @param {string} type
   *   The type of event being recorded.
   * @param {object} args
   *   The data to pass to telemetry when the event is recorded.
   */
  #recordEvent(type, args = null) {
    Services.telemetry.recordEvent(
      "browser.migration",
      type,
      "wizard",
      null,
      args
    );
  }

  /**
   * Gets the FileMigrator associated with the passed in key, and then opens
   * a native file picker configured for that migrator. Once the user selects
   * a file from the native file picker, this is then passed to the
   * FileMigrator.migrate method.
   *
   * As the migration occurs, this will send UpdateProgress messages to the
   * MigrationWizardChild to show the beginning and then the ending state of
   * the migration.
   *
   * @param {DOMWindow} window
   *   The window that the native file picker should be associated with. This
   *   cannot be null. See nsIFilePicker.init for more details.
   * @param {string} key
   *   The unique identification key for a file migrator.
   * @returns {Promise<undefined>}
   *   Resolves once the file migrator's migrate method has resolved.
   */
  async #doFileMigration(window, key) {
    let fileMigrator = MigrationUtils.getFileMigrator(key);
    let filePickerConfig = await fileMigrator.getFilePickerConfig();

    let { result, path } = await new Promise(resolve => {
      let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
      fp.init(
        window.browsingContext,
        filePickerConfig.title,
        Ci.nsIFilePicker.modeOpen
      );

      for (let filter of filePickerConfig.filters) {
        fp.appendFilter(filter.title, filter.extensionPattern);
      }
      fp.appendFilters(Ci.nsIFilePicker.filterAll);
      fp.open(async fileOpenResult => {
        resolve({ result: fileOpenResult, path: fp.file.path });
      });
    });

    if (result == Ci.nsIFilePicker.returnCancel) {
      // If the user cancels out of the file picker, the migration wizard should
      // still be in the state that lets the user re-open the file picker if
      // they closed it by accident, so we don't have to do anything else here.
      return;
    }

    let progress = {};
    for (let resourceType of fileMigrator.displayedResourceTypes) {
      progress[resourceType] = {
        value: lazy.MigrationWizardConstants.PROGRESS_VALUE.LOADING,
        message: "",
      };
    }

    let [progressHeaderString, successHeaderString] =
      await lazy.gFluentStrings.formatValues([
        fileMigrator.progressHeaderL10nID,
        fileMigrator.successHeaderL10nID,
      ]);

    this.sendAsyncMessage("UpdateFileImportProgress", {
      title: progressHeaderString,
      progress,
    });

    let migrationResult;
    try {
      migrationResult = await fileMigrator.migrate(path);
    } catch (e) {
      this.sendAsyncMessage("FileImportProgressError", {
        migratorKey: key,
        fileImportErrorMessage: e.message,
      });
      return;
    }

    let successProgress = {};
    for (let resourceType in migrationResult) {
      successProgress[resourceType] = {
        value: lazy.MigrationWizardConstants.PROGRESS_VALUE.SUCCESS,
        message: migrationResult[resourceType],
      };
    }
    this.sendAsyncMessage("UpdateFileImportProgress", {
      title: successHeaderString,
      progress: successProgress,
    });
  }

  /**
   * Handles a request to open a native file picker to get the path to a
   * CSV file that contains passwords exported from Safari. The returned
   * path is in the form of a string, or `null` if the user cancelled the
   * native picker.
   *
   * @param {DOMWindow} window
   *   The window that the native file picker should be associated with. This
   *   cannot be null. See nsIFilePicker.init for more details.
   * @returns {Promise<string|null>}
   */
  async #selectSafariPasswordFile(window) {
    let fileMigrator = MigrationUtils.getFileMigrator(
      lazy.PasswordFileMigrator.key
    );
    let filePickerConfig = await fileMigrator.getFilePickerConfig();

    let { result, path } = await new Promise(resolve => {
      let fp = Cc["@mozilla.org/filepicker;1"].createInstance(Ci.nsIFilePicker);
      fp.init(
        window.browsingContext,
        filePickerConfig.title,
        Ci.nsIFilePicker.modeOpen
      );

      for (let filter of filePickerConfig.filters) {
        fp.appendFilter(filter.title, filter.extensionPattern);
      }
      fp.appendFilters(Ci.nsIFilePicker.filterAll);
      fp.open(async fileOpenResult => {
        resolve({ result: fileOpenResult, path: fp.file.path });
      });
    });

    if (result == Ci.nsIFilePicker.returnCancel) {
      // If the user cancels out of the file picker, the migration wizard should
      // still be in the state that lets the user re-open the file picker if
      // they closed it by accident, so we don't have to do anything else here.
      return null;
    }

    return path;
  }

  /**
   * Calls into MigrationUtils to perform a migration given the parameters
   * sent via the wizard.
   *
   * @param {MigrationDetails} migrationDetails
   *   See migration-wizard.mjs for a definition of MigrationDetails.
   * @param {object} extraArgs
   *   Extra argument object that will be passed to the Event Telemetry for
   *   finishing the migration. This was initialized in the child actor, and
   *   will be sent back down to it to write to Telemetry once migration
   *   completes.
   *
   * @returns {Promise<object>}
   *   Resolves once the Migration:Ended observer notification has fired,
   *   passing the extraArgs for Telemetry back with any relevant properties
   *   updated.
   */
  async #doBrowserMigration(migrationDetails, extraArgs) {
    Services.telemetry
      .getHistogramById("FX_MIGRATION_SOURCE_BROWSER")
      .add(MigrationUtils.getSourceIdForTelemetry(migrationDetails.key));

    let migrator = await MigrationUtils.getMigrator(migrationDetails.key);
    let availableResourceTypes = await migrator.getMigrateData(
      migrationDetails.profile
    );
    let resourceTypesToMigrate = 0;
    let progress = {};
    let migrationUsageHist =
      Services.telemetry.getKeyedHistogramById("FX_MIGRATION_USAGE");

    for (let resourceTypeName of migrationDetails.resourceTypes) {
      let resourceType = MigrationUtils.resourceTypes[resourceTypeName];
      if (availableResourceTypes & resourceType) {
        resourceTypesToMigrate |= resourceType;
        progress[resourceTypeName] = {
          value: lazy.MigrationWizardConstants.PROGRESS_VALUE.LOADING,
          message: "",
        };

        if (!migrationDetails.autoMigration) {
          migrationUsageHist.add(migrationDetails.key, Math.log2(resourceType));
        }
      }
    }

    if (
      migrationDetails.key == lazy.SafariProfileMigrator?.key &&
      migrationDetails.safariPasswordFilePath
    ) {
      // The caller supplied a password export file for Safari. We're going to
      // pretend that there was a PASSWORDS resource for Safari to represent
      // the state of importing from that file.
      progress[
        lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.PASSWORDS
      ] = {
        value: lazy.MigrationWizardConstants.PROGRESS_VALUE.LOADING,
        message: "",
      };

      this.sendAsyncMessage("UpdateProgress", {
        key: migrationDetails.key,
        progress,
      });

      try {
        let summary = await lazy.LoginCSVImport.importFromCSV(
          migrationDetails.safariPasswordFilePath
        );
        let quantity = summary.filter(entry => entry.result == "added").length;

        progress[
          lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.PASSWORDS
        ] = {
          value: lazy.MigrationWizardConstants.PROGRESS_VALUE.SUCCESS,
          message: await lazy.gFluentStrings.formatValue(
            "migration-wizard-progress-success-passwords",
            {
              quantity,
            }
          ),
        };
      } catch (e) {
        progress[
          lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.PASSWORDS
        ] = {
          value: lazy.MigrationWizardConstants.PROGRESS_VALUE.WARNING,
          message: await lazy.gFluentStrings.formatValue(
            "migration-passwords-from-file-no-valid-data"
          ),
        };
      }
    }

    this.sendAsyncMessage("UpdateProgress", {
      key: migrationDetails.key,
      progress,
    });

    // It's possible that only a Safari password file path was sent up, and
    // there's nothing left to migrate, in which case we're done here.
    if (
      migrationDetails.safariPasswordFilePath &&
      !migrationDetails.resourceTypes.length
    ) {
      return extraArgs;
    }

    try {
      await migrator.migrate(
        resourceTypesToMigrate,
        false,
        migrationDetails.profile,
        async (resourceTypeNum, success, details) => {
          // Unfortunately, MigratorBase hands us the the numeric value of the
          // MigrationUtils.resourceType for this callback. For now, we'll just
          // do a look-up to map it to the right constant.
          let foundResourceTypeName;
          for (let resourceTypeName in MigrationUtils.resourceTypes) {
            if (
              MigrationUtils.resourceTypes[resourceTypeName] == resourceTypeNum
            ) {
              foundResourceTypeName = resourceTypeName;
              break;
            }
          }

          if (!foundResourceTypeName) {
            console.error(
              "Could not find a resource type for value: ",
              resourceTypeNum
            );
          } else {
            if (!success) {
              Services.telemetry
                .getKeyedHistogramById("FX_MIGRATION_ERRORS")
                .add(migrationDetails.key, Math.log2(resourceTypeNum));
            }
            if (
              foundResourceTypeName ==
              lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.EXTENSIONS
            ) {
              if (!success) {
                // did not match any extensions
                extraArgs.extensions =
                  lazy.MigrationWizardConstants.EXTENSIONS_IMPORT_RESULT.NONE_MATCHED;
                progress[foundResourceTypeName] = {
                  value: lazy.MigrationWizardConstants.PROGRESS_VALUE.WARNING,
                  message: await lazy.gFluentStrings.formatValue(
                    "migration-wizard-progress-no-matched-extensions"
                  ),
                  linkURL: Services.urlFormatter.formatURLPref(
                    "extensions.getAddons.link.url"
                  ),
                  linkText: await lazy.gFluentStrings.formatValue(
                    "migration-wizard-progress-extensions-addons-link"
                  ),
                };
              } else if (
                details?.progressValue ==
                lazy.MigrationWizardConstants.PROGRESS_VALUE.SUCCESS
              ) {
                // did match all extensions
                extraArgs.extensions =
                  lazy.MigrationWizardConstants.EXTENSIONS_IMPORT_RESULT.ALL_MATCHED;
                progress[foundResourceTypeName] = {
                  value: lazy.MigrationWizardConstants.PROGRESS_VALUE.SUCCESS,
                  message: await lazy.gFluentStrings.formatValue(
                    "migration-wizard-progress-success-extensions",
                    {
                      quantity: details.totalExtensions.length,
                    }
                  ),
                };
              } else if (
                details?.progressValue ==
                lazy.MigrationWizardConstants.PROGRESS_VALUE.INFO
              ) {
                // did match some extensions
                extraArgs.extensions =
                  lazy.MigrationWizardConstants.EXTENSIONS_IMPORT_RESULT.PARTIAL_MATCH;
                progress[foundResourceTypeName] = {
                  value: lazy.MigrationWizardConstants.PROGRESS_VALUE.INFO,
                  message: await lazy.gFluentStrings.formatValue(
                    "migration-wizard-progress-partial-success-extensions",
                    {
                      matched: details.importedExtensions.length,
                      quantity: details.totalExtensions.length,
                    }
                  ),
                  linkURL:
                    Services.urlFormatter.formatURLPref("app.support.baseURL") +
                    "import-data-another-browser",
                  linkText: await lazy.gFluentStrings.formatValue(
                    "migration-wizard-progress-extensions-support-link"
                  ),
                };
              }
            } else {
              progress[foundResourceTypeName] = {
                value: success
                  ? lazy.MigrationWizardConstants.PROGRESS_VALUE.SUCCESS
                  : lazy.MigrationWizardConstants.PROGRESS_VALUE.WARNING,
                message: await this.#getStringForImportQuantity(
                  migrationDetails.key,
                  foundResourceTypeName
                ),
              };
            }
            this.sendAsyncMessage("UpdateProgress", {
              key: migrationDetails.key,
              progress,
            });
          }
        }
      );
    } catch (e) {
      console.error(e);
    }

    return extraArgs;
  }

  /**
   * @typedef {object} MigratorProfileInstance
   *   An object that describes a single user profile (or the default
   *   user profile) for a particular migrator.
   * @property {string} key
   *   The unique identification key for a migrator.
   * @property {string} displayName
   *   The display name for the migrator that will be shown to the user
   *   in the wizard.
   * @property {string[]} resourceTypes
   *   An array of strings, where each string represents a resource type
   *   that can be imported for this migrator and profile. The strings
   *   should be one of the key values of
   *   MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.
   *
   *   Example: ["HISTORY", "FORMDATA", "PASSWORDS", "BOOKMARKS"]
   * @property {object|null} profile
   *   A description of the user profile that the migrator can import.
   * @property {string} profile.id
   *   A unique ID for the user profile.
   * @property {string} profile.name
   *   The display name for the user profile.
   */

  /**
   * Asynchronously fetches a migrator for a particular key, and then
   * also gets any user profiles that exist on for that migrator. Resolves
   * to null if something goes wrong getting information about the migrator
   * or any of the user profiles.
   *
   * @param {string} key
   *   The unique identification key for a migrator.
   * @returns {Promise<MigratorProfileInstance[]|null>}
   */
  async #getMigratorAndProfiles(key) {
    try {
      let migrator = await MigrationUtils.getMigrator(key);
      if (!migrator?.enabled) {
        return null;
      }

      if (!(await migrator.hasPermissions())) {
        // If we're unable to get permissions for this migrator, then we
        // just don't bother showing it.
        let permissionsPath = await migrator.canGetPermissions();
        if (!permissionsPath) {
          return null;
        }
        return this.#serializeMigratorAndProfile(
          migrator,
          null,
          false /* hasPermissions */,
          permissionsPath
        );
      }

      let sourceProfiles = await migrator.getSourceProfiles();
      if (Array.isArray(sourceProfiles)) {
        if (!sourceProfiles.length) {
          return null;
        }

        Services.telemetry.keyedScalarAdd(
          "migration.discovered_migrators",
          key,
          sourceProfiles.length
        );

        let result = [];
        for (let profile of sourceProfiles) {
          result.push(
            await this.#serializeMigratorAndProfile(migrator, profile)
          );
        }
        return result;
      }

      Services.telemetry.keyedScalarAdd(
        "migration.discovered_migrators",
        key,
        1
      );
      return this.#serializeMigratorAndProfile(migrator, sourceProfiles);
    } catch (e) {
      console.error(`Could not get migrator with key ${key}`, e);
    }
    return null;
  }

  /**
   * Asynchronously fetches information about what resource types can be
   * migrated for a particular migrator and user profile, and then packages
   * the migrator, user profile data, and resource type data into an object
   * that can be sent down to the MigrationWizardChild.
   *
   * @param {MigratorBase} migrator
   *   A migrator subclass of MigratorBase.
   * @param {object|null} profileObj
   *   The user profile object representing the profile to get information
   *   about. This object is usually gotten by calling getSourceProfiles on
   *   the migrator.
   * @param {boolean} [hasPermissions=true]
   *   Whether or not the migrator has permission to read the data for the
   *   other browser. It is expected that the caller will have already
   *   computed this by calling hasPermissions() on the migrator, and
   *   passing the result into this method. This is true by default.
   * @param {string} [permissionsPath=undefined]
   *   The path that the selected migrator needs read access to in order to
   *   do a migration, in the event that hasPermissions is false. This is
   *   undefined if hasPermissions is true.
   * @returns {Promise<MigratorProfileInstance>}
   */
  async #serializeMigratorAndProfile(
    migrator,
    profileObj,
    hasPermissions = true,
    permissionsPath
  ) {
    let [profileMigrationData, lastModifiedDate] = await Promise.all([
      migrator.getMigrateData(profileObj),
      migrator.getLastUsedDate(),
    ]);

    let availableResourceTypes = [];

    // Even if we don't have permissions, we'll show the resources available
    // for Safari. For Safari, the workflow is to request permissions only
    // after the resources have been selected.
    if (
      hasPermissions ||
      migrator.constructor.key == lazy.SafariProfileMigrator?.key
    ) {
      for (let resourceType in MigrationUtils.resourceTypes) {
        // Normally, we check each possible resourceType to see if we have one or
        // more corresponding resourceTypes in profileMigrationData. The exception
        // is for Safari, where the migrator does not expose a PASSWORDS resource
        // type, but we allow the user to express that they'd like to import
        // passwords from it anyways. This is because the Safari migration flow is
        // special, and allows the user to import passwords from a file exported
        // from Safari.
        if (
          profileMigrationData & MigrationUtils.resourceTypes[resourceType] ||
          (migrator.constructor.key == lazy.SafariProfileMigrator?.key &&
            MigrationUtils.resourceTypes[resourceType] ==
              MigrationUtils.resourceTypes.PASSWORDS &&
            Services.prefs.getBoolPref(
              "signon.management.page.fileImport.enabled",
              false
            ))
        ) {
          availableResourceTypes.push(resourceType);
        }
      }
    }

    let displayName;

    if (migrator.constructor.key == lazy.InternalTestingProfileMigrator.key) {
      // In the case of the InternalTestingProfileMigrator, which is never seen
      // by users outside of testing, we don't make our localization community
      // localize it's display name, and just display the ID instead.
      displayName = migrator.constructor.displayNameL10nID;
    } else {
      displayName = await lazy.gFluentStrings.formatValue(
        migrator.constructor.displayNameL10nID
      );
    }

    return {
      type: lazy.MigrationWizardConstants.MIGRATOR_TYPES.BROWSER,
      key: migrator.constructor.key,
      displayName,
      brandImage: migrator.constructor.brandImage,
      resourceTypes: availableResourceTypes,
      profile: profileObj,
      lastModifiedDate,
      hasPermissions,
      permissionsPath,
    };
  }

  /**
   * Returns the "success" string for a particular resource type after
   * migration has completed.
   *
   * @param {string} migratorKey
   *   The key for the migrator being used.
   * @param {string} resourceTypeStr
   *   A string mapping to one of the key values of
   *   MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.
   * @returns {Promise<string>}
   *   The success string for the resource type after migration has completed.
   */
  #getStringForImportQuantity(migratorKey, resourceTypeStr) {
    if (migratorKey == lazy.FirefoxProfileMigrator.key) {
      return "";
    }

    switch (resourceTypeStr) {
      case lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.BOOKMARKS: {
        let quantity = MigrationUtils.getImportedCount("bookmarks");
        let stringID = "migration-wizard-progress-success-bookmarks";

        if (
          lazy.MigrationWizardConstants.USES_FAVORITES.includes(migratorKey)
        ) {
          stringID = "migration-wizard-progress-success-favorites";
        }

        return lazy.gFluentStrings.formatValue(stringID, {
          quantity,
        });
      }
      case lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.HISTORY: {
        return lazy.gFluentStrings.formatValue(
          "migration-wizard-progress-success-history",
          {
            maxAgeInDays: MigrationUtils.HISTORY_MAX_AGE_IN_DAYS,
          }
        );
      }
      case lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.PASSWORDS: {
        let quantity = MigrationUtils.getImportedCount("logins");
        return lazy.gFluentStrings.formatValue(
          "migration-wizard-progress-success-passwords",
          {
            quantity,
          }
        );
      }
      case lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES.FORMDATA: {
        return lazy.gFluentStrings.formatValue(
          "migration-wizard-progress-success-formdata"
        );
      }
      case lazy.MigrationWizardConstants.DISPLAYED_RESOURCE_TYPES
        .PAYMENT_METHODS: {
        let quantity = MigrationUtils.getImportedCount("cards");
        return lazy.gFluentStrings.formatValue(
          "migration-wizard-progress-success-payment-methods",
          {
            quantity,
          }
        );
      }
      default: {
        return "";
      }
    }
  }

  /**
   * Returns a Promise that resolves to a serializable representation of a
   * FileMigrator for sending down to the MigrationWizard.
   *
   * @param {FileMigrator} fileMigrator
   *   The FileMigrator to serialize.
   * @returns {Promise<object|null>}
   *   The serializable representation of the FileMigrator, or null if the
   *   migrator is disabled.
   */
  async #serializeFileMigrator(fileMigrator) {
    if (!fileMigrator.enabled) {
      return null;
    }

    return {
      type: lazy.MigrationWizardConstants.MIGRATOR_TYPES.FILE,
      key: fileMigrator.constructor.key,
      displayName: await lazy.gFluentStrings.formatValue(
        fileMigrator.constructor.displayNameL10nID
      ),
      brandImage: fileMigrator.constructor.brandImage,
      resourceTypes: [],
    };
  }

  /**
   * Opens the about:addons page in a new background tab in the same window
   * as the passed browser.
   *
   * @param {Element} browser
   *   The browser element requesting that about:addons opens.
   */
  #openAboutAddons(browser) {
    let window = browser.ownerGlobal;
    window.openTrustedLinkIn("about:addons", "tab", { inBackground: true });
  }
}