no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / toolkit / content / aboutServiceWorkers.js
blobf57753239cecaed018b3da171ff6c28aca54d7bd
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
7 var gSWM;
8 var gSWCount = 0;
10 function init() {
11   let enabled = Services.prefs.getBoolPref("dom.serviceWorkers.enabled");
12   if (!enabled) {
13     let div = document.getElementById("warning_not_enabled");
14     div.classList.add("active");
15     return;
16   }
18   gSWM = Cc["@mozilla.org/serviceworkers/manager;1"].getService(
19     Ci.nsIServiceWorkerManager
20   );
21   if (!gSWM) {
22     dump(
23       "AboutServiceWorkers: Failed to get the ServiceWorkerManager service!\n"
24     );
25     return;
26   }
28   let data = gSWM.getAllRegistrations();
29   if (!data) {
30     dump("AboutServiceWorkers: Failed to retrieve the registrations.\n");
31     return;
32   }
34   let length = data.length;
35   if (!length) {
36     let div = document.getElementById("warning_no_serviceworkers");
37     div.classList.add("active");
38     return;
39   }
41   let ps = undefined;
42   try {
43     ps = Cc["@mozilla.org/push/Service;1"].getService(Ci.nsIPushService);
44   } catch (e) {
45     dump("Could not acquire PushService\n");
46   }
48   for (let i = 0; i < length; ++i) {
49     let info = data.queryElementAt(i, Ci.nsIServiceWorkerRegistrationInfo);
50     if (!info) {
51       dump(
52         "AboutServiceWorkers: Invalid nsIServiceWorkerRegistrationInfo interface.\n"
53       );
54       continue;
55     }
57     display(info, ps);
58   }
61 async function display(info, pushService) {
62   let parent = document.getElementById("serviceworkers");
64   let div = document.createElement("div");
65   parent.appendChild(div);
67   let title = document.createElement("h2");
68   document.l10n.setAttributes(title, "origin-title", {
69     originTitle: info.principal.origin,
70   });
71   div.appendChild(title);
73   let list = document.createElement("ul");
74   div.appendChild(list);
76   function createItem(l10nId, value, makeLink) {
77     let item = document.createElement("li");
78     list.appendChild(item);
79     let bold = document.createElement("strong");
80     bold.setAttribute("data-l10n-name", "item-label");
81     item.appendChild(bold);
82     // Falsey values like "" are still valid values, so check exactly against
83     // undefined for the cases where the caller did not provide any value.
84     if (value === undefined) {
85       document.l10n.setAttributes(item, l10nId);
86     } else if (makeLink) {
87       let link = document.createElement("a");
88       link.setAttribute("target", "_blank");
89       link.setAttribute("data-l10n-name", "link");
90       link.setAttribute("href", value);
91       item.appendChild(link);
92       document.l10n.setAttributes(item, l10nId, { url: value });
93     } else {
94       document.l10n.setAttributes(item, l10nId, { name: value });
95     }
96     return item;
97   }
99   createItem("scope", info.scope);
100   createItem("script-spec", info.scriptSpec, true);
101   let currentWorkerURL = info.activeWorker ? info.activeWorker.scriptSpec : "";
102   createItem("current-worker-url", currentWorkerURL, true);
103   let activeCacheName = info.activeWorker ? info.activeWorker.cacheName : "";
104   createItem("active-cache-name", activeCacheName);
105   let waitingCacheName = info.waitingWorker ? info.waitingWorker.cacheName : "";
106   createItem("waiting-cache-name", waitingCacheName);
108   let pushItem = createItem("push-end-point-waiting");
109   if (pushService) {
110     pushService.getSubscription(
111       info.scope,
112       info.principal,
113       (status, pushRecord) => {
114         if (Components.isSuccessCode(status)) {
115           document.l10n.setAttributes(pushItem, "push-end-point-result", {
116             name: JSON.stringify(pushRecord),
117           });
118         } else {
119           dump("about:serviceworkers - retrieving push registration failed\n");
120         }
121       }
122     );
123   }
125   let unregisterButton = document.createElement("button");
126   document.l10n.setAttributes(unregisterButton, "unregister-button");
127   div.appendChild(unregisterButton);
129   let loadingMessage = document.createElement("span");
130   document.l10n.setAttributes(loadingMessage, "waiting");
131   loadingMessage.classList.add("inactive");
132   div.appendChild(loadingMessage);
134   unregisterButton.onclick = function () {
135     let cb = {
136       unregisterSucceeded() {
137         parent.removeChild(div);
139         if (!--gSWCount) {
140           let div = document.getElementById("warning_no_serviceworkers");
141           div.classList.add("active");
142         }
143       },
145       async unregisterFailed() {
146         let [alertMsg] = await document.l10n.formatValues([
147           { id: "unregister-error" },
148         ]);
149         alert(alertMsg);
150       },
152       QueryInterface: ChromeUtils.generateQI([
153         "nsIServiceWorkerUnregisterCallback",
154       ]),
155     };
157     loadingMessage.classList.remove("inactive");
158     gSWM.propagateUnregister(info.principal, cb, info.scope);
159   };
161   let sep = document.createElement("hr");
162   div.appendChild(sep);
164   ++gSWCount;
167 window.addEventListener(
168   "DOMContentLoaded",
169   function () {
170     init();
171   },
172   { once: true }