Bug 1164942 - Do not load pocket's jsm or other scripts until used with extra getter to satisfy tests. r=jaws, a=lizzard
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Fri, 15 May 2015 14:25:57 +0100
changeset 267543 dc93318d4ffdf086e3d1694a05318add457ac677
parent 267542 93711677dff40f9a94833af0fa6dec83a6ae964c
child 267544 4cdf696cb410e08921bbbd3e0af65a1e17bd8360
push id830
push userraliiev@mozilla.com
push dateFri, 19 Jun 2015 19:24:37 +0000
treeherdermozilla-release@932614382a68 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjaws, lizzard
bugs1164942
milestone39.0
Bug 1164942 - Do not load pocket's jsm or other scripts until used with extra getter to satisfy tests. r=jaws, a=lizzard
browser/base/content/browser.js
browser/base/content/browser.xul
browser/components/customizableui/CustomizableWidgets.jsm
browser/components/customizableui/content/panelUI.js
browser/components/pocket/Pocket.jsm
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -43,16 +43,45 @@ XPCOMUtils.defineLazyServiceGetter(this,
                                    "@mozilla.org/browser/favicon-service;1",
                                    "mozIAsyncFavicons");
 XPCOMUtils.defineLazyServiceGetter(this, "gDNSService",
                                    "@mozilla.org/network/dns-service;1",
                                    "nsIDNSService");
 XPCOMUtils.defineLazyModuleGetter(this, "Pocket",
                                   "resource:///modules/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", {
+  get: function() {
+    // Avoid this getter running again:
+    delete window.pktApi;
+    Services.scriptloader.loadSubScript("chrome://browser/content/pocket/pktApi.js", window);
+    return window.pktApi;
+  },
+  configurable: true,
+  enumerable: true
+});
+
+function pktUIGetter(prop) {
+  return {
+    get: function() {
+      // Avoid either of these getters running again:
+      delete window.pktUI;
+      delete window.pktUIMessaging;
+      Services.scriptloader.loadSubScript("chrome://browser/content/pocket/main.js", window);
+      return window[prop];
+    },
+    configurable: true,
+    enumerable: true
+  };
+}
+Object.defineProperty(window, "pktUI", pktUIGetter("pktUI"));
+Object.defineProperty(window, "pktUIMessaging", pktUIGetter("pktUIMessaging"));
+
 const nsIWebNavigation = Ci.nsIWebNavigation;
 
 var gLastBrowserCharset = null;
 var gProxyFavIcon = null;
 var gLastValidURLStr = "";
 var gInPrintPreviewMode = false;
 var gContextMenu = null; // nsContextMenu instance
 var gMultiProcessBrowser =
@@ -4219,17 +4248,16 @@ var XULBrowserWindow = {
       }
 
       if (gURLBar) {
         URLBarSetURI(aLocationURI);
 
         BookmarkingUI.onLocationChange();
         SocialUI.updateState(location);
         UITour.onLocationChange(location);
-        Pocket.onLocationChange(browser, aLocationURI);
       }
 
       // Utility functions for disabling find
       var shouldDisableFind = function shouldDisableFind(aDocument) {
         let docElt = aDocument.documentElement;
         return docElt && docElt.getAttribute("disablefastfind") == "true";
       }
 
--- a/browser/base/content/browser.xul
+++ b/browser/base/content/browser.xul
@@ -1300,12 +1300,9 @@
   </svg:svg>
 
 </vbox>
 # <iframe id="tab-view"> is dynamically appended as the 2nd child of #tab-view-deck.
 #     Introducing the iframe dynamically, as needed, was found to be better than
 #     starting with an empty iframe here in browser.xul from a Ts standpoint.
 </deck>
 
-<script type="application/javascript" src="chrome://browser/content/pocket/pktApi.js"/>
-<script type="application/javascript" src="chrome://browser/content/pocket/main.js"/>
-
 </window>
--- a/browser/components/customizableui/CustomizableWidgets.jsm
+++ b/browser/components/customizableui/CustomizableWidgets.jsm
@@ -1125,18 +1125,23 @@ if (Services.prefs.getBoolPref("browser.
     let pocketButton = {
       id: "pocket-button",
       defaultArea: CustomizableUI.AREA_NAVBAR,
       introducedInVersion: "pref",
       type: "view",
       viewId: "PanelUI-pocketView",
       label: label,
       tooltiptext: tooltiptext,
-      onViewShowing: Pocket.onPanelViewShowing,
-      onViewHiding: Pocket.onPanelViewHiding,
+      // Use forwarding functions here to avoid loading Pocket.jsm on startup:
+      onViewShowing: function() {
+        return Pocket.onPanelViewShowing.apply(this, arguments);
+      },
+      onViewHiding: function() {
+        return Pocket.onPanelViewHiding.apply(this, arguments);
+      },
 
       // If the user has the "classic" Pocket add-on installed, use that instead
       // and destroy the widget.
       conditionalDestroyPromise: new Promise(resolve => {
         AddonManager.getAddonByID("isreaditlater@ideashower.com", addon => {
           resolve(addon && addon.isActive);
         });
       }),
--- a/browser/components/customizableui/content/panelUI.js
+++ b/browser/components/customizableui/content/panelUI.js
@@ -1,18 +1,16 @@
 /* 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/. */
 
 XPCOMUtils.defineLazyModuleGetter(this, "CustomizableUI",
                                   "resource:///modules/CustomizableUI.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "ScrollbarSampler",
                                   "resource:///modules/ScrollbarSampler.jsm");
-XPCOMUtils.defineLazyModuleGetter(this, "Pocket",
-                                  "resource:///modules/Pocket.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "Promise",
                                   "resource://gre/modules/Promise.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "ShortcutUtils",
                                   "resource://gre/modules/ShortcutUtils.jsm");
 
 /**
  * Maintains the state and dispatches events for the main menu panel.
  */
--- a/browser/components/pocket/Pocket.jsm
+++ b/browser/components/pocket/Pocket.jsm
@@ -61,40 +61,16 @@ let Pocket = {
     window.pktUI.pocketPanelDidShow();
   },
 
   onPanelViewHiding(event) {
     let window = event.target.ownerDocument.defaultView;
     window.pktUI.pocketPanelDidHide(event);
   },
 
-  // Called on tab/urlbar/location changes and after customization. Update
-  // anything that is tab specific.
-  onLocationChange(browser, locationURI) {
-    if (!locationURI) {
-      return;
-    }
-    let widget = CustomizableUI.getWidget("pocket-button");
-    for (let instance of widget.instances) {
-      let node = instance.node;
-      if (!node ||
-          node.ownerDocument != browser.ownerDocument) {
-        continue;
-      }
-      if (node) {
-        let win = browser.ownerDocument.defaultView;
-        node.disabled = win.pktApi.isUserLoggedIn() &&
-                        !locationURI.schemeIs("http") &&
-                        !locationURI.schemeIs("https") &&
-                        !(locationURI.schemeIs("about") &&
-                          locationURI.spec.toLowerCase().startsWith("about:reader?url="));
-      }
-    }
-  },
-
   _urlToSave: null,
   _titleToSave: null,
   savePage(browser, url, title) {
     let document = browser.ownerDocument;
     let pocketWidget = document.getElementById("pocket-button");
     let placement = CustomizableUI.getPlacementOfWidget("pocket-button");
     if (!placement)
       return;