Bug 1236605 - Fix pocket style addition performance, r?mixedpuppy draft
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Thu, 07 Jan 2016 15:08:37 +0000
changeset 319755 c8ee70f584058728163ef137bcfb5e577952f594
parent 318213 ff863d8cdf7ad551bc4843e3614e09236400a6da
child 512639 a1ffeda961faa6dc7b1fc02d66259bb59cff7a53
push id9082
push usergijskruitbosch@gmail.com
push dateThu, 07 Jan 2016 18:12:57 +0000
reviewersmixedpuppy
bugs1236605
milestone46.0a1
Bug 1236605 - Fix pocket style addition performance, r?mixedpuppy
browser/extensions/pocket/bootstrap.js
--- 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");