gecko-dev/toolkit/content/aboutServiceWorkers.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

168 lines
5.2 KiB
JavaScript

/* 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";
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
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);
if (info.principal.appId) {
let b2gtitle = document.createElement("h3");
let trueFalse = info.principal.isInIsolatedMozBrowserElement ? "true" : "false";
document.l10n.setAttributes(b2gtitle, "app-title", { appId: info.principal.appId, isInIsolatedElement: trueFalse });
div.appendChild(b2gtitle);
}
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);
if (!value) {
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 updateButton = document.createElement("button");
document.l10n.setAttributes(updateButton, "update-button");
updateButton.onclick = function() {
gSWM.propagateSoftUpdate(info.principal.originAttributes, info.scope);
};
div.appendChild(updateButton);
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([Ci.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});