summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/accountcreation/AccountCreationUtils.jsm
blob: f4efb96b2d527c2052df5dc8e1c3965a2a75d2d4 (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
/* 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/. */
/**
 * Some common, generic functions
 */

const EXPORTED_SYMBOLS = ["AccountCreationUtils"];

const lazy = {};

ChromeUtils.defineModuleGetter(
  lazy,
  "Sanitizer",
  "resource:///modules/accountcreation/Sanitizer.jsm"
);

const { AddonManager } = ChromeUtils.importESModule(
  "resource://gre/modules/AddonManager.sys.mjs"
);
const { ConsoleAPI } = ChromeUtils.importESModule(
  "resource://gre/modules/Console.sys.mjs"
);
const { clearInterval, clearTimeout, setTimeout } = ChromeUtils.importESModule(
  "resource://gre/modules/Timer.sys.mjs"
);

// --------------------------
// Low level, basic functions

function assert(test, errorMsg) {
  if (!test) {
    throw new NotReached(
      errorMsg ? errorMsg : "Programming bug. Assertion failed, see log."
    );
  }
}

function makeCallback(obj, func) {
  return func.bind(obj);
}

/**
 * Runs the given function sometime later
 *
 * Currently implemented using setTimeout(), but
 * can later be replaced with an nsITimer impl,
 * when code wants to use it in a module.
 *
 * @see |TimeoutAbortable|
 */
function runAsync(func) {
  return setTimeout(func, 0);
}

/**
 * Reads UTF8 data from a URL.
 *
 * @param uri {nsIURI} - what you want to read
 * @returns {Array of String} the contents of the file, one string per line
 */
function readURLasUTF8(uri) {
  assert(uri instanceof Ci.nsIURI, "uri must be an nsIURI");
  let chan = Services.io.newChannelFromURI(
    uri,
    null,
    Services.scriptSecurityManager.getSystemPrincipal(),
    null,
    Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
    Ci.nsIContentPolicy.TYPE_OTHER
  );
  let is = Cc["@mozilla.org/intl/converter-input-stream;1"].createInstance(
    Ci.nsIConverterInputStream
  );
  is.init(
    chan.open(),
    "UTF-8",
    1024,
    Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER
  );

  let content = "";
  let strOut = {};
  try {
    while (is.readString(1024, strOut) != 0) {
      content += strOut.value;
    }
  } finally {
    is.close();
  }

  return content;
  // TODO this has a numeric error message. We need to ship translations
  // into human language.
}

/**
 * @param bundleURI {String} - chrome URL to properties file
 * @returns nsIStringBundle
 */
function getStringBundle(bundleURI) {
  try {
    return Services.strings.createBundle(bundleURI);
  } catch (e) {
    throw new Exception(
      "Failed to get stringbundle URI <" + bundleURI + ">. Error: " + e
    );
  }
}

// ---------
// Exception

function Exception(msg) {
  this._message = msg;
  this.stack = Components.stack.formattedStack;
}
Exception.prototype = {
  get message() {
    return this._message;
  },
  toString() {
    return this._message;
  },
};

function NotReached(msg) {
  Exception.call(this, msg); // call super constructor
  console.error(this);
}
// Make NotReached extend Exception.
NotReached.prototype = Object.create(Exception.prototype);
NotReached.prototype.constructor = NotReached;

// ---------
// Abortable

/**
 * A handle for an async function which you can cancel.
 * The async function will return an object of this type (a subtype)
 * and you can call cancel() when you feel like killing the function.
 */
function Abortable() {}
Abortable.prototype = {
  cancel(e) {},
};

function CancelledException(msg) {
  Exception.call(this, msg);
}
CancelledException.prototype = Object.create(Exception.prototype);
CancelledException.prototype.constructor = CancelledException;

function UserCancelledException(msg) {
  // The user knows they cancelled so I don't see a need
  // for a message to that effect.
  if (!msg) {
    msg = "User cancelled";
  }
  CancelledException.call(this, msg);
}
UserCancelledException.prototype = Object.create(CancelledException.prototype);
UserCancelledException.prototype.constructor = UserCancelledException;

/**
 * Utility implementation, for waiting for a promise to resolve,
 * but allowing its result to be cancelled.
 */
function PromiseAbortable(promise, successCallback, errorCallback) {
  Abortable.call(this); // call super constructor
  let complete = false;
  this.cancel = function (e) {
    if (!complete) {
      complete = true;
      errorCallback(e || new CancelledException());
    }
  };
  promise
    .then(function (result) {
      if (!complete) {
        successCallback(result);
        complete = true;
      }
    })
    .catch(function (e) {
      if (!complete) {
        complete = true;
        errorCallback(e);
      }
    });
}
PromiseAbortable.prototype = Object.create(Abortable.prototype);
PromiseAbortable.prototype.constructor = PromiseAbortable;

/**
 * Utility implementation, for allowing to abort a setTimeout.
 * Use like: return new TimeoutAbortable(setTimeout(function(){ ... }, 0));
 *
 * @param setTimeoutID {Integer} - Return value of setTimeout()
 */
function TimeoutAbortable(setTimeoutID) {
  Abortable.call(this); // call super constructor
  this._id = setTimeoutID;
}
TimeoutAbortable.prototype = Object.create(Abortable.prototype);
TimeoutAbortable.prototype.constructor = TimeoutAbortable;
TimeoutAbortable.prototype.cancel = function () {
  clearTimeout(this._id);
};

/**
 * Utility implementation, for allowing to abort a setTimeout.
 * Use like: return new TimeoutAbortable(setTimeout(function(){ ... }, 0));
 *
 * @param setIntervalID {Integer} - Return value of setInterval()
 */
function IntervalAbortable(setIntervalID) {
  Abortable.call(this); // call super constructor
  this._id = setIntervalID;
}
IntervalAbortable.prototype = Object.create(Abortable.prototype);
IntervalAbortable.prototype.constructor = IntervalAbortable;
IntervalAbortable.prototype.cancel = function () {
  clearInterval(this._id);
};

/**
 * Allows you to make several network calls,
 * but return only one |Abortable| object.
 */
function SuccessiveAbortable() {
  Abortable.call(this); // call super constructor
  this._current = null;
}
SuccessiveAbortable.prototype = {
  __proto__: Abortable.prototype,
  get current() {
    return this._current;
  },
  set current(abortable) {
    assert(
      abortable instanceof Abortable || abortable == null,
      "need an Abortable object (or null)"
    );
    this._current = abortable;
  },
  cancel(e) {
    if (this._current) {
      this._current.cancel(e);
    }
  },
};

/**
 * Allows you to make several network calls in parallel.
 */
function ParallelAbortable() {
  Abortable.call(this); // call super constructor
  // { Array of ParallelCall }
  this._calls = [];
  // { Array of Function }
  this._finishedObservers = [];
}
ParallelAbortable.prototype = {
  __proto__: Abortable.prototype,
  /**
   * @returns {Array of ParallelCall}
   */
  get results() {
    return this._calls;
  },
  /**
   * @returns {ParallelCall}
   */
  addCall() {
    let call = new ParallelCall(this);
    call.position = this._calls.length;
    this._calls.push(call);
    return call;
  },
  /**
   * Observers will be called once one of the functions
   * finishes, i.e. returns successfully or fails.
   *
   * @param {Function({ParallelCall} call)} func
   */
  addOneFinishedObserver(func) {
    assert(typeof func == "function");
    this._finishedObservers.push(func);
  },
  /**
   * Will be called once *all* of the functions finished,
   * It gives you a list of all functions that succeeded or failed,
   * respectively.
   *
   * @param {Function(
   *    {Array of ParallelCall} succeeded,
   *    {Array of ParallelCall} failed
   *   )} func
   */
  addAllFinishedObserver(func) {
    assert(typeof func == "function");
    this.addOneFinishedObserver(() => {
      if (this._calls.some(call => !call.finished)) {
        return;
      }
      let succeeded = this._calls.filter(call => call.succeeded);
      let failed = this._calls.filter(call => !call.succeeded);
      func(succeeded, failed);
    });
  },
  _notifyFinished(call) {
    for (let observer of this._finishedObservers) {
      try {
        observer(call);
      } catch (e) {
        console.error(e);
      }
    }
  },
  cancel(e) {
    for (let call of this._calls) {
      if (!call.finished && call.callerAbortable) {
        call.callerAbortable.cancel(e);
      }
    }
  },
};

/**
 * Returned by ParallelAbortable.addCall().
 * Do not create this object directly
 *
 * @param {ParallelAbortable} parallelAbortable - The controlling ParallelAbortable
 */
function ParallelCall(parallelAbortable) {
  assert(parallelAbortable instanceof ParallelAbortable);
  // {ParallelAbortable} the parent
  this._parallelAbortable = parallelAbortable;
  // {Abortable} Abortable of the caller function that should run in parallel
  this.callerAbortable = null;
  // {Integer} the order in which the function was added, and its priority
  this.position = null;
  // {boolean} false = running, pending, false = success or failure
  this.finished = false;
  // {boolean} if finished: true = returned with success, false = returned with error
  this.succeeded = false;
  // {Exception} if failed: the error or exception that the caller function returned
  this.e = null;
  // {Object} if succeeded: the result of the caller function
  this.result = null;

  this._time = Date.now();
}
ParallelCall.prototype = {
  /**
   * Returns a successCallback(result) function that you pass
   * to your function that runs in parallel.
   *
   * @returns {Function(result)} successCallback
   */
  successCallback() {
    return result => {
      ddump(
        "call " +
          this.position +
          " took " +
          (Date.now() - this._time) +
          "ms and succeeded" +
          (this.callerAbortable && this.callerAbortable._url
            ? " at <" + this.callerAbortable._url + ">"
            : "")
      );
      this.result = result;
      this.finished = true;
      this.succeeded = true;
      this._parallelAbortable._notifyFinished(this);
    };
  },
  /**
   * Returns an errorCallback(e) function that you pass
   * to your function that runs in parallel.
   *
   * @returns {Function(e)} errorCallback
   */
  errorCallback() {
    return e => {
      ddump(
        "call " +
          this.position +
          " took " +
          (Date.now() - this._time) +
          "ms and failed with " +
          (typeof e.code == "number" ? e.code + " " : "") +
          (e.toString()
            ? e.toString()
            : "unknown error, probably no host connection") +
          (this.callerAbortable && this.callerAbortable._url
            ? " at <" + this.callerAbortable._url + ">"
            : "")
      );
      this.e = e;
      this.finished = true;
      this.succeeded = false;
      this._parallelAbortable._notifyFinished(this);
    };
  },
  /**
   * Call your function that needs to run in parallel
   * and pass the resulting |Abortable| of your function here.
   *
   * @param {Abortable} abortable
   */
  setAbortable(abortable) {
    assert(abortable instanceof Abortable);
    this.callerAbortable = abortable;
  },
};

/**
 * Runs several calls in parallel.
 * Returns the result of the "highest" priority call that succeeds.
 * Unlike Promise.race(), does not return the fastest,
 * but the first in the order they were added.
 * So, the order in which the calls were added determines their priority,
 * with the first to be added being the most desirable.
 *
 * E.g. the first failed, the second is pending, the third succeeded, and the forth is pending.
 * It aborts the forth (because the third succeeded), and it waits for the second to return.
 * If the second succeeds, it is the result, otherwise the third is the result.
 *
 * @param {Function(
 *     {Object} result - Result of winner call
 *     {ParallelCall} call - Winner call info
 *   )} successCallback -  A call returned successfully
 * @param {Function(e, allErrors)} errorCallback - All calls failed.
 *     {Exception} e - The first CancelledException, and otherwise
 *       the exception returned by the first call.
 *     This is just to adhere to the standard API of errorCallback(e).
 *     {Array of Exception} allErrors - The exceptions from all calls.
 */
function PriorityOrderAbortable(successCallback, errorCallback) {
  assert(typeof successCallback == "function");
  assert(typeof errorCallback == "function");
  ParallelAbortable.call(this); // call super constructor
  this._successfulCall = null;

  this.addOneFinishedObserver(finishedCall => {
    for (let call of this._calls) {
      if (!call.finished) {
        if (this._successfulCall) {
          // abort
          if (call.callerAbortable) {
            call.callerAbortable.cancel(
              new NoLongerNeededException("Another higher call succeeded")
            );
          }
          continue;
        }
        // It's pending. do nothing and wait for it.
        return;
      }
      if (!call.succeeded) {
        // it failed. ignore it.
        continue;
      }
      if (this._successfulCall) {
        // we already have a winner. ignore it.
        continue;
      }
      try {
        successCallback(call.result, call);
        // This is the winner.
        this._successfulCall = call;
      } catch (e) {
        console.error(e);
        // If the handler failed with this data, treat this call as failed.
        call.e = e;
        call.succeeded = false;
      }
    }
    if (!this._successfulCall) {
      // all failed
      let allErrors = this._calls.map(call => call.e);
      let e =
        allErrors.find(e => e instanceof CancelledException) || allErrors[0];
      errorCallback(e, allErrors); // see docs above
    }
  });
}
PriorityOrderAbortable.prototype = Object.create(ParallelAbortable.prototype);
PriorityOrderAbortable.prototype.constructor = PriorityOrderAbortable;

function NoLongerNeededException(msg) {
  CancelledException.call(this, msg);
}
NoLongerNeededException.prototype = Object.create(CancelledException.prototype);
NoLongerNeededException.prototype.constructor = NoLongerNeededException;

// -------------------
// High level features

/**
 * Allows you to install an addon.
 *
 * Example:
 * var installer = new AddonInstaller({ xpiURL : "https://...xpi", id: "...", ...});
 * installer.install();
 *
 * @param {object} args - Contains parameters:
 * @param {string} name (Optional) - Name of the addon (not important)
 * @param {string} id (Optional) - Addon ID
 * If you pass an ID, and the addon is already installed (and the version matches),
 * then install() will do nothing.
 * After the XPI is downloaded, the ID will be verified. If it doesn't match, the
 * install will fail.
 * If you don't pass an ID, these checks will be skipped and the addon be installed
 * unconditionally.
 * It is recommended to pass at least an ID, because it can confuse some addons
 * to be reloaded at runtime.
 * @param {string} minVersion (Optional) - Minimum version of the addon
 * If you pass a minVersion (in addition to ID), and the installed addon is older than this,
 * the install will be done anyway. If the downloaded addon has a lower version,
 * the install will fail.
 * If you do not pass a minVersion, there will be no version check.
 * @param {URL} xpiURL - Where to download the XPI from
 */
function AddonInstaller(args) {
  Abortable.call(this);
  this._name = lazy.Sanitizer.label(args.name);
  this._id = lazy.Sanitizer.string(args.id);
  this._minVersion = lazy.Sanitizer.string(args.minVersion);
  this._url = lazy.Sanitizer.url(args.xpiURL);
}
AddonInstaller.prototype = Object.create(Abortable.prototype);
AddonInstaller.prototype.constructor = AddonInstaller;

/**
 * Checks whether the passed-in addon matches the
 * id and minVersion requested by the caller.
 *
 * @param {nsIAddon} addon
 * @returns {boolean} is OK
 */
AddonInstaller.prototype.matches = function (addon) {
  return (
    !this._id ||
    (this._id == addon.id &&
      (!this._minVersion ||
        Services.vc.compare(addon.version, this._minVersion) >= 0))
  );
};

/**
 * Start the installation
 *
 * @throws Exception in case of failure
 */
AddonInstaller.prototype.install = async function () {
  if (await this.isInstalled()) {
    return;
  }
  await this._installDirect();
};

/**
 * Checks whether we already have an addon installed that matches the
 * id and minVersion requested by the caller.
 *
 * @returns {boolean} is already installed and enabled
 */
AddonInstaller.prototype.isInstalled = async function () {
  if (!this._id) {
    return false;
  }
  var addon = await AddonManager.getAddonByID(this._id);
  return addon && this.matches(addon) && addon.isActive;
};

/**
 * Checks whether we already have an addon but it is disabled.
 *
 * @returns {boolean} is already installed but disabled
 */
AddonInstaller.prototype.isDisabled = async function () {
  if (!this._id) {
    return false;
  }
  let addon = await AddonManager.getAddonByID(this._id);
  return addon && !addon.isActive;
};

/**
 * Downloads and installs the addon.
 * The downloaded XPI will be checked using prompt().
 */
AddonInstaller.prototype._installDirect = async function () {
  var installer = (this._installer = await AddonManager.getInstallForURL(
    this._url,
    { name: this._name }
  ));
  installer.promptHandler = makeCallback(this, this.prompt);
  await installer.install(); // throws, if failed

  var addon = await AddonManager.getAddonByID(this._id);
  await addon.enable();

  // Wait for addon startup code to finish
  // Fixes: verify password fails with NOT_AVAILABLE in createIncomingServer()
  if ("startupPromise" in addon) {
    await addon.startupPromise;
  }
  let wait = ms => new Promise(resolve => setTimeout(resolve, ms));
  await wait(1000);
};

/**
 * Install confirmation. You may override this, if needed.
 *
 * @throws Exception If you want to cancel install, then throw an exception.
 */
AddonInstaller.prototype.prompt = async function (info) {
  if (!this.matches(info.addon)) {
    // happens only when we got the wrong XPI
    throw new Exception(
      "The downloaded addon XPI does not match the minimum requirements"
    );
  }
};

AddonInstaller.prototype.cancel = function () {
  if (this._installer) {
    try {
      this._installer.cancel();
    } catch (e) {
      // if install failed
      ddump(e);
    }
  }
};

// ------------
// Debug output

function deepCopy(org) {
  if (typeof org == "undefined") {
    return undefined;
  }
  if (org == null) {
    return null;
  }
  if (typeof org == "string") {
    return org;
  }
  if (typeof org == "number") {
    return org;
  }
  if (typeof org == "boolean") {
    return org;
  }
  if (typeof org == "function") {
    return org;
  }
  if (typeof org != "object") {
    throw new Error("can't copy objects of type " + typeof org + " yet");
  }

  // TODO still instanceof org != instanceof copy
  // var result = new org.constructor();
  var result = {};
  if (typeof org.length != "undefined") {
    result = [];
  }
  for (var prop in org) {
    result[prop] = deepCopy(org[prop]);
  }
  return result;
}

var gAccountSetupLogger = new ConsoleAPI({
  prefix: "mail.setup",
  maxLogLevel: "warn",
  maxLogLevelPref: "mail.setup.loglevel",
});

function ddump(text) {
  gAccountSetupLogger.info(text);
}

function alertPrompt(alertTitle, alertMsg) {
  Services.prompt.alert(
    Services.wm.getMostRecentWindow(""),
    alertTitle,
    alertMsg
  );
}

var AccountCreationUtils = {
  Abortable,
  AddonInstaller,
  alertPrompt,
  assert,
  CancelledException,
  ddump,
  deepCopy,
  Exception,
  gAccountSetupLogger,
  getStringBundle,
  NotReached,
  PriorityOrderAbortable,
  PromiseAbortable,
  readURLasUTF8,
  runAsync,
  SuccessiveAbortable,
  TimeoutAbortable,
  UserCancelledException,
};