Bug 1236605 - Fix pocket style addition performance, r?mixedpuppy
--- a/browser/extensions/pocket/bootstrap.js
+++ b/browser/extensions/pocket/bootstrap.js
@@ -20,16 +20,19 @@ XPCOMUtils.defineLazyModuleGetter(this,
"resource://gre/modules/AddonManager.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "ReaderMode",
"resource://gre/modules/ReaderMode.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Pocket",
"chrome://pocket/content/Pocket.jsm");
XPCOMUtils.defineLazyGetter(this, "gPocketBundle", function() {
return Services.strings.createBundle("chrome://pocket/locale/pocket.properties");
});
+XPCOMUtils.defineLazyGetter(this, "gPocketStyleURI", function() {
+ return Services.io.newURI("chrome://pocket/skin/pocket.css", null, null);
+});
const PREF_BRANCH = "extensions.pocket.";
const PREFS = {
enabled: true, // bug 1229937, figure out ui tour support
api: "api.getpocket.com",
site: "getpocket.com",
oAuthConsumerKey: "40249-e88c401e1b1f2242d9e441c4"
@@ -281,20 +284,22 @@ function pktUIGetter(prop, window) {
},
configurable: true,
enumerable: true
};
}
var PocketOverlay = {
startup: function(reason) {
+ let styleSheetService = Cc["@mozilla.org/content/style-sheet-service;1"]
+ .getService(Ci.nsIStyleSheetService);
+ this._sheetType = styleSheetService.AUTHOR_SHEET;
+ this._cachedSheet = styleSheetService.preloadSheet(gPocketStyleURI,
+ this._sheetType);
CreatePocketWidget(reason);
- Services.obs.addObserver(this,
- "browser-delayed-startup-finished",
- false);
CustomizableUI.addListener(this);
PocketContextMenu.init();
PocketReader.startup();
if (reason == ADDON_ENABLE) {
for (let win of allBrowserWindows()) {
this.setWindowScripts(win);
this.addStyles(win);
@@ -317,19 +322,17 @@ var PocketOverlay = {
delete window.Pocket;
delete window.pktUI;
delete window.pktUIMessaging;
}
CustomizableUI.destroyWidget("pocket-button");
PocketContextMenu.shutdown();
PocketReader.shutdown();
},
- observe: function(aSubject, aTopic, aData) {
- // new browser window, initialize the "overlay"
- let window = aSubject;
+ onWindowOpened: function(window) {
this.setWindowScripts(window);
this.addStyles(window);
this.updateWindow(window);
},
setWindowScripts: function(window) {
XPCOMUtils.defineLazyModuleGetter(window, "Pocket",
"chrome://pocket/content/Pocket.jsm");
// Can't use XPCOMUtils for these because the scripts try to define the variables
@@ -430,31 +433,23 @@ var PocketOverlay = {
if (hidden) {
PocketReader.shutdown();
} else {
PocketReader.startup();
}
},
addStyles: function(win) {
- let xmlPI = win.document.createProcessingInstruction("xml-stylesheet",
- "type=\"text/css\" href=\"chrome://pocket/skin/pocket.css\"");
- win.document.insertBefore(xmlPI, win.document.documentElement);
+ let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
+ utils.addSheet(this._cachedSheet, this._sheetType);
},
removeStyles: function(win) {
- let el = win.document.documentElement.previousSibling;
- while (el) {
- if (el.nodeType == el.PROCESSING_INSTRUCTION_NODE &&
- el.sheet && el.sheet.href == "chrome://pocket/skin/pocket.css") {
- el.remove();
- break;
- }
- el = el.previousSibling;
- }
+ let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
+ utils.removeSheet(gPocketStyleURI, this._sheetType);
}
}
// use enabled pref as a way for tests (e.g. test_contextmenu.html) to disable
// the addon when running.
function prefObserver(aSubject, aTopic, aData) {
let enabled = Services.prefs.getBoolPref("extensions.pocket.enabled");