summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/serviceworkers/chrome_helpers.js
blob: a438333e27917d5d77a3ee939aaada1a671d9a7e (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
let { classes: Cc, interfaces: Ci, utils: Cu } = Components;

Cu.import("resource://gre/modules/Task.jsm");

let swm = Cc["@mozilla.org/serviceworkers/manager;1"].
          getService(Ci.nsIServiceWorkerManager);

let EXAMPLE_URL = "https://example.com/chrome/dom/workers/test/serviceworkers/";

function waitForIframeLoad(iframe) {
  return new Promise(function (resolve) {
    iframe.onload = resolve;
  });
}

function waitForRegister(scope, callback) {
  return new Promise(function (resolve) {
    let listener = {
      onRegister: function (registration) {
        if (registration.scope !== scope) {
          return;
        }
        swm.removeListener(listener);
        resolve(callback ? callback(registration) : registration);
      }
    };
    swm.addListener(listener);
  });
}

function waitForUnregister(scope) {
  return new Promise(function (resolve) {
    let listener = {
      onUnregister: function (registration) {
        if (registration.scope !== scope) {
          return;
        }
        swm.removeListener(listener);
        resolve(registration);
      }
    };
    swm.addListener(listener);
  });
}

function waitForServiceWorkerRegistrationChange(registration, callback) {
  return new Promise(function (resolve) {
    let listener = {
      onChange: function () {
        registration.removeListener(listener);
        if (callback) {
          callback();
        }
        resolve(callback ? callback() : undefined);
      }
    };
    registration.addListener(listener);
  });
}

function waitForServiceWorkerShutdown() {
  return new Promise(function (resolve) {
    let observer = {
      observe: function (subject, topic, data) {
        if (topic !== "service-worker-shutdown") {
          return;
        }
        SpecialPowers.removeObserver(observer, "service-worker-shutdown");
        resolve();
      }
    };
    SpecialPowers.addObserver(observer, "service-worker-shutdown", false);
  });
}