Bug 1498295 - simplify pocket reader code a bit, r=mconley
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Tue, 08 Jan 2019 12:36:08 +0000
changeset 509985 587eb607c9ad7116c3827eaa1b34d707d73a3352
parent 509984 3e906dc0b1e81089d1c65e0c7e23db8d8923f704
child 509986 8ff55d20525c275f7db9edb4fe1d4e238f7cba65
push id10547
push userffxbld-merge
push dateMon, 21 Jan 2019 13:03:58 +0000
treeherdermozilla-beta@24ec1916bffe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconley
bugs1498295
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1498295 - simplify pocket reader code a bit, r=mconley Differential Revision: https://phabricator.services.mozilla.com/D15525
browser/components/nsBrowserGlue.js
browser/components/pocket/content/SaveToPocket.jsm
--- a/browser/components/nsBrowserGlue.js
+++ b/browser/components/nsBrowserGlue.js
@@ -1449,17 +1449,16 @@ BrowserGlue.prototype = {
       this.pingCentre.uninit();
     }
 
     PageThumbs.uninit();
     NewTabUtils.uninit();
     AboutPrivateBrowsingHandler.uninit();
     AutoCompletePopup.uninit();
     DateTimePickerParent.uninit();
-    SaveToPocket.uninit();
 
     // Browser errors are only collected on Nightly, but telemetry for
     // them is collected on all channels.
     if (AppConstants.MOZ_DATA_REPORTING) {
       this.browserErrorReporter.uninit();
     }
 
     Normandy.uninit();
--- a/browser/components/pocket/content/SaveToPocket.jsm
+++ b/browser/components/pocket/content/SaveToPocket.jsm
@@ -25,20 +25,16 @@ function createElementWithAttrs(document
   let element = document.createXULElement(type);
   Object.keys(attrs).forEach(function(attr) {
     element.setAttribute(attr, attrs[attr]);
   });
   return element;
 }
 
 
-function isPocketEnabled() {
-  return PocketPageAction.enabled;
-}
-
 var PocketPageAction = {
   pageAction: null,
   urlbarNode: null,
 
   get enabled() {
     return !!this.pageAction;
   },
 
@@ -186,17 +182,17 @@ var PocketContextMenu = {
         if (element)
           element.remove();
       }
     }
   },
   observe(aSubject, aTopic, aData) {
     let subject = aSubject.wrappedJSObject;
     let document = subject.menu.ownerDocument;
-    let pocketEnabled = isPocketEnabled();
+    let pocketEnabled = SaveToPocket.prefEnabled;
 
     let showSaveCurrentPageToPocket = !(subject.onTextInput || subject.onLink ||
                                         subject.isContentSelected || subject.onImage ||
                                         subject.onCanvas || subject.onVideo || subject.onAudio);
     let targetUrl = subject.onLink ? subject.linkUrl : subject.pageUrl;
     let targetURI = Services.io.newURI(targetUrl);
     let canPocket = pocketEnabled && (targetURI.schemeIs("http") || targetURI.schemeIs("https") ||
                     (targetURI.schemeIs("about") && ReaderMode.getOriginalUrl(targetUrl)));
@@ -235,87 +231,19 @@ var PocketContextMenu = {
       } else {
         sibling.parentNode.appendChild(menu);
       }
     }
     menu.hidden = !showSaveLinkToPocket;
   },
 };
 
-// PocketReader
-// Listen for reader mode setup and add our button to the reader toolbar
-var PocketReader = {
-  _hidden: true,
-  get hidden() {
-    return this._hidden;
-  },
-  set hidden(hide) {
-    hide = !!hide;
-    if (hide === this._hidden)
-      return;
-    this._hidden = hide;
-    this.update();
-  },
-  startup() {
-    // Setup the listeners, update will be called when the widget is added,
-    // no need to do that now.
-    let mm = Services.mm;
-    mm.addMessageListener("Reader:OnSetup", this);
-    mm.addMessageListener("Reader:Clicked-pocket-button", this);
-  },
-  shutdown() {
-    let mm = Services.mm;
-    mm.removeMessageListener("Reader:OnSetup", this);
-    mm.removeMessageListener("Reader:Clicked-pocket-button", this);
-    this.hidden = true;
-  },
-  update() {
-    if (this.hidden) {
-      Services.mm.broadcastAsyncMessage("Reader:RemoveButton", { id: "pocket-button" });
-    } else {
-      Services.mm.broadcastAsyncMessage("Reader:AddButton", {
-        id: "pocket-button",
-        title: gPocketBundle.GetStringFromName("pocket-button.tooltiptext"),
-        image: "chrome://pocket/content/panels/img/pocket-outline.svg",
-        width: 20,
-        height: 20,
-      });
-    }
-  },
-  receiveMessage(message) {
-    switch (message.name) {
-      case "Reader:OnSetup": {
-        // Tell the reader about our button.
-        if (this.hidden)
-          break;
-        message.target.messageManager.
-          sendAsyncMessage("Reader:AddButton", {
-            id: "pocket-button",
-            title: gPocketBundle.GetStringFromName("pocket-button.tooltiptext"),
-            image: "chrome://pocket/content/panels/img/pocket-outline.svg",
-            width: 20,
-            height: 20,
-          });
-        break;
-      }
-      case "Reader:Clicked-pocket-button": {
-        if (PocketPageAction.pageAction) {
-          PocketPageAction.pageAction.doCommand(message.target.ownerGlobal);
-        }
-        break;
-      }
-    }
-  },
-};
-
-
 var PocketOverlay = {
   startup() {
     Services.obs.addObserver(this, "browser-delayed-startup-finished");
-    PocketReader.startup();
     PocketPageAction.init();
     PocketContextMenu.init();
     for (let win of browserWindows()) {
       this.updateWindow(win);
     }
   },
   shutdown() {
     Services.obs.removeObserver(this, "browser-delayed-startup-finished");
@@ -327,71 +255,93 @@ var PocketOverlay = {
         let element = window.document.getElementById(id) ||
                       window.gNavToolbox.palette.querySelector("#" + id);
         if (element)
           element.remove();
       }
     }
 
     PocketContextMenu.shutdown();
-    PocketReader.shutdown();
   },
   observe(subject, topic, detail) {
     if (topic == "browser-delayed-startup-finished") {
       this.updateWindow(subject);
     }
   },
   // called for each window as it is opened
   updateWindow(window) {
     // insert our three menu items
     let document = window.document;
-    let hidden = !isPocketEnabled();
+    let hidden = !SaveToPocket.prefEnabled;
 
     // Add to library panel
     let sib = document.getElementById("appMenu-library-history-button");
     if (sib && !document.getElementById("appMenu-library-pocket-button")) {
       let menu = createElementWithAttrs(document, "toolbarbutton", {
         "id": "appMenu-library-pocket-button",
         "label": gPocketBundle.GetStringFromName("pocketMenuitem.label"),
         "class": "subviewbutton subviewbutton-iconic",
         "oncommand": "Pocket.openList(event)",
         "hidden": hidden,
       });
       sib.parentNode.insertBefore(menu, sib);
     }
-
-    // enable or disable reader button
-    PocketReader.hidden = hidden;
   },
 };
 
-// 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");
-  if (enabled)
-    PocketOverlay.startup();
-  else
-    PocketOverlay.shutdown();
-}
-
 function browserWindows() {
   return Services.wm.getEnumerator("navigator:browser");
 }
 
 var SaveToPocket = {
   init() {
     // migrate enabled pref
     if (Services.prefs.prefHasUserValue("browser.pocket.enabled")) {
       Services.prefs.setBoolPref("extensions.pocket.enabled", Services.prefs.getBoolPref("browser.pocket.enabled"));
       Services.prefs.clearUserPref("browser.pocket.enabled");
     }
-    // watch pref change and enable/disable if necessary
-    Services.prefs.addObserver("extensions.pocket.enabled", prefObserver);
-    if (!Services.prefs.getBoolPref("extensions.pocket.enabled"))
-      return;
-    PocketOverlay.startup();
+    // Only define the pref getter now, so we don't get notified for the
+    // migrated pref above.
+    XPCOMUtils.defineLazyPreferenceGetter(
+      this, "prefEnabled", "extensions.pocket.enabled", true, this.onPrefChange.bind(this));
+    if (this.prefEnabled) {
+      PocketOverlay.startup();
+    }
+    Services.mm.addMessageListener("Reader:OnSetup", this);
+    Services.mm.addMessageListener("Reader:Clicked-pocket-button", this);
+  },
+
+  _readerButtonData: {
+    id: "pocket-button",
+    title: gPocketBundle.GetStringFromName("pocket-button.tooltiptext"),
+    image: "chrome://pocket/content/panels/img/pocket-outline.svg",
+    width: 20,
+    height: 20,
   },
 
-  uninit() {
-    Services.prefs.removeObserver("extensions.pocket.enabled", prefObserver);
+  onPrefChange(pref, oldValue, newValue) {
+    if (!newValue) {
+      Services.mm.broadcastAsyncMessage("Reader:RemoveButton", { id: "pocket-button" });
+      PocketOverlay.shutdown();
+    } else {
+      PocketOverlay.startup();
+      Services.mm.broadcastAsyncMessage("Reader:AddButton", this._readerButtonData);
+    }
+  },
+
+  receiveMessage(message) {
+    if (!this.prefEnabled) {
+      return;
+    }
+    switch (message.name) {
+      case "Reader:OnSetup": {
+        // Tell the reader about our button.
+        message.target.messageManager.
+          sendAsyncMessage("Reader:AddButton", this._readerButtonData);
+        break;
+      }
+      case "Reader:Clicked-pocket-button": {
+        PocketPageAction.pageAction.doCommand(message.target.ownerGlobal);
+        break;
+      }
+    }
   },
 };