Bug 1514724 - define script getters in the browser instead of via a module and iterating over windows, r=mconley

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

--HG--
extra : rebase_source : e146dfb7df7626abcd38cd6d44c6fed1523cb111
This commit is contained in:
Gijs Kruitbosch 2018-12-28 11:27:59 +00:00
Родитель 40b32451fd
Коммит ccfd35873f
4 изменённых файлов: 5 добавлений и 50 удалений

Просмотреть файл

@ -43,6 +43,7 @@ XPCOMUtils.defineLazyModuleGetters(this, {
PlacesUIUtils: "resource:///modules/PlacesUIUtils.jsm",
PlacesTransactions: "resource://gre/modules/PlacesTransactions.jsm",
PluralForm: "resource://gre/modules/PluralForm.jsm",
Pocket: "chrome://pocket/content/Pocket.jsm",
PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
ProcessHangMonitor: "resource:///modules/ProcessHangMonitor.jsm",
PromiseUtils: "resource://gre/modules/PromiseUtils.jsm",
@ -139,6 +140,8 @@ if (AppConstants.NIGHTLY_BUILD) {
"chrome://browser/content/browser-webrender.js");
}
XPCOMUtils.defineLazyScriptGetter(this, "pktUI", "chrome://pocket/content/main.js");
// lazy service getters
XPCOMUtils.defineLazyServiceGetters(this, {

Просмотреть файл

@ -307,21 +307,6 @@ var PocketReader = {
};
function pktUIGetter(prop, window) {
return {
get() {
// delete any getters for properties loaded from main.js so we only load main.js once
delete window.pktUI;
delete window.pktApi;
delete window.pktUIMessaging;
Services.scriptloader.loadSubScript("chrome://pocket/content/main.js", window);
return window[prop];
},
configurable: true,
enumerable: true,
};
}
var PocketOverlay = {
startup() {
Services.obs.addObserver(this, "browser-delayed-startup-finished");
@ -329,7 +314,7 @@ var PocketOverlay = {
PocketPageAction.init();
PocketContextMenu.init();
for (let win of browserWindows()) {
this.onWindowOpened(win);
this.updateWindow(win);
}
},
shutdown() {
@ -344,11 +329,6 @@ var PocketOverlay = {
if (element)
element.remove();
}
// remove script getters/objects
window.Pocket = undefined;
window.pktApi = undefined;
window.pktUI = undefined;
window.pktUIMessaging = undefined;
}
PocketContextMenu.shutdown();
@ -356,24 +336,9 @@ var PocketOverlay = {
},
observe(subject, topic, detail) {
if (topic == "browser-delayed-startup-finished") {
this.onWindowOpened(subject);
this.updateWindow(subject);
}
},
onWindowOpened(window) {
if (window.hasOwnProperty("pktUI"))
return;
this.setWindowScripts(window);
this.updateWindow(window);
},
setWindowScripts(window) {
ChromeUtils.defineModuleGetter(window, "Pocket",
"chrome://pocket/content/Pocket.jsm");
// Can't use XPCOMUtils for these because the scripts try to define the variables
// on window, and so the defineProperty inside defineLazyGetter fails.
Object.defineProperty(window, "pktApi", pktUIGetter("pktApi", window));
Object.defineProperty(window, "pktUI", pktUIGetter("pktUI", window));
Object.defineProperty(window, "pktUIMessaging", pktUIGetter("pktUIMessaging", window));
},
// called for each window as it is opened
updateWindow(window) {
// insert our three menu items

Просмотреть файл

@ -15,7 +15,6 @@ add_task(async function test_setup() {
add_task(async function() {
await promisePocketEnabled();
checkWindowProperties(true, ["Pocket", "pktUI", "pktUIMessaging"]);
checkElements(true, ["pocket-button", "appMenu-library-pocket-button"]);
let buttonBox = document.getElementById("pocket-button-box");
is(buttonBox.hidden, false, "Button should not have been hidden");
@ -41,7 +40,6 @@ add_task(async function() {
await promisePocketDisabled();
checkWindowProperties(false, ["Pocket", "pktUI", "pktUIMessaging"]);
checkElements(false, ["appMenu-library-pocket-button",
"context-pocket", "context-savelinktopocket"]);
buttonBox = document.getElementById("pocket-button-box");

Просмотреть файл

@ -31,11 +31,6 @@ function promisePocketDisabled() {
Services.prefs.setBoolPref("extensions.pocket.enabled", false);
return BrowserTestUtils.waitForCondition(() => {
return !PageActions.actionForID("pocket");
}).then(() => {
// wait for a full unload of pocket
return BrowserTestUtils.waitForCondition(() => {
return !window.hasOwnProperty("pktUI") || !window.pktUI;
});
});
}
@ -48,12 +43,6 @@ function promisePocketReset() {
return promisePocketDisabled();
}
function checkWindowProperties(expectPresent, l) {
for (let name of l) {
is(window.hasOwnProperty(name) && !!window[name], expectPresent, "property " + name + (expectPresent ? " is" : " is not") + " present");
}
}
function checkElements(expectPresent, l) {
for (let id of l) {
let el = document.getElementById(id) || gNavToolbox.palette.querySelector("#" + id);