summaryrefslogtreecommitdiffstats
path: root/toolkit/content/aboutServiceWorkers.js
blob: f57753239cecaed018b3da171ff6c28aca54d7bd (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
/* 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";

var gSWM;
var gSWCount = 0;

function init() {
  let enabled = Services.prefs.getBoolPref("dom.serviceWorkers.enabled");
  if (!enabled) {
    let div = document.getElementById("warning_not_enabled");
    div.classList.add("active");
    return;
  }

  gSWM = Cc["@mozilla.org/serviceworkers/manager;1"].getService(
    Ci.nsIServiceWorkerManager
  );
  if (!gSWM) {
    dump(
      "AboutServiceWorkers: Failed to get the ServiceWorkerManager service!\n"
    );
    return;
  }

  let data = gSWM.getAllRegistrations();
  if (!data) {
    dump("AboutServiceWorkers: Failed to retrieve the registrations.\n");
    return;
  }

  let length = data.length;
  if (!length) {
    let div = document.getElementById("warning_no_serviceworkers");
    div.classList.add("active");
    return;
  }

  let ps = undefined;
  try {
    ps = Cc["@mozilla.org/push/Service;1"].getService(Ci.nsIPushService);
  } catch (e) {
    dump("Could not acquire PushService\n");
  }

  for (let i = 0; i < length; ++i) {
    let info = data.queryElementAt(i, Ci.nsIServiceWorkerRegistrationInfo);
    if (!info) {
      dump(
        "AboutServiceWorkers: Invalid nsIServiceWorkerRegistrationInfo interface.\n"
      );
      continue;
    }

    display(info, ps);
  }
}

async function display(info, pushService) {
  let parent = document.getElementById("serviceworkers");

  let div = document.createElement("div");
  parent.appendChild(div);

  let title = document.createElement("h2");
  document.l10n.setAttributes(title, "origin-title", {
    originTitle: info.principal.origin,
  });
  div.appendChild(title);

  let list = document.createElement("ul");
  div.appendChild(list);

  function createItem(l10nId, value, makeLink) {
    let item = document.createElement("li");
    list.appendChild(item);
    let bold = document.createElement("strong");
    bold.setAttribute("data-l10n-name", "item-label");
    item.appendChild(bold);
    // Falsey values like "" are still valid values, so check exactly against
    // undefined for the cases where the caller did not provide any value.
    if (value === undefined) {
      document.l10n.setAttributes(item, l10nId);
    } else if (makeLink) {
      let link = document.createElement("a");
      link.setAttribute("target", "_blank");
      link.setAttribute("data-l10n-name", "link");
      link.setAttribute("href", value);
      item.appendChild(link);
      document.l10n.setAttributes(item, l10nId, { url: value });
    } else {
      document.l10n.setAttributes(item, l10nId, { name: value });
    }
    return item;
  }

  createItem("scope", info.scope);
  createItem("script-spec", info.scriptSpec, true);
  let currentWorkerURL = info.activeWorker ? info.activeWorker.scriptSpec : "";
  createItem("current-worker-url", currentWorkerURL, true);
  let activeCacheName = info.activeWorker ? info.activeWorker.cacheName : "";
  createItem("active-cache-name", activeCacheName);
  let waitingCacheName = info.waitingWorker ? info.waitingWorker.cacheName : "";
  createItem("waiting-cache-name", waitingCacheName);

  let pushItem = createItem("push-end-point-waiting");
  if (pushService) {
    pushService.getSubscription(
      info.scope,
      info.principal,
      (status, pushRecord) => {
        if (Components.isSuccessCode(status)) {
          document.l10n.setAttributes(pushItem, "push-end-point-result", {
            name: JSON.stringify(pushRecord),
          });
        } else {
          dump("about:serviceworkers - retrieving push registration failed\n");
        }
      }
    );
  }

  let unregisterButton = document.createElement("button");
  document.l10n.setAttributes(unregisterButton, "unregister-button");
  div.appendChild(unregisterButton);

  let loadingMessage = document.createElement("span");
  document.l10n.setAttributes(loadingMessage, "waiting");
  loadingMessage.classList.add("inactive");
  div.appendChild(loadingMessage);

  unregisterButton.onclick = function () {
    let cb = {
      unregisterSucceeded() {
        parent.removeChild(div);

        if (!--gSWCount) {
          let div = document.getElementById("warning_no_serviceworkers");
          div.classList.add("active");
        }
      },

      async unregisterFailed() {
        let [alertMsg] = await document.l10n.formatValues([
          { id: "unregister-error" },
        ]);
        alert(alertMsg);
      },

      QueryInterface: ChromeUtils.generateQI([
        "nsIServiceWorkerUnregisterCallback",
      ]),
    };

    loadingMessage.classList.remove("inactive");
    gSWM.propagateUnregister(info.principal, cb, info.scope);
  };

  let sep = document.createElement("hr");
  div.appendChild(sep);

  ++gSWCount;
}

window.addEventListener(
  "DOMContentLoaded",
  function () {
    init();
  },
  { once: true }
);