Bug 1252661 fix visibility state of pocket menu items, r=gijs
authorShane Caraveo <scaraveo@mozilla.com>
Fri, 04 Mar 2016 14:38:13 -0800
changeset 323254 50f9111794457fb3d992a0c517dbbe1057dbdd0a
parent 323253 269d8cec9a9b143ef4947f5cb37c915c5ee607db
child 323255 92a3b28fa06a6c30f285ae2c0ab8c61666fda623
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-beta@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgijs
bugs1252661
milestone47.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 1252661 fix visibility state of pocket menu items, r=gijs
browser/extensions/pocket/bootstrap.js
toolkit/components/reader/AboutReader.jsm
--- a/browser/extensions/pocket/bootstrap.js
+++ b/browser/extensions/pocket/bootstrap.js
@@ -280,36 +280,54 @@ var PocketContextMenu = {
     }
     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: function() {
+    // 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);
-    mm.broadcastAsyncMessage("Reader:AddButton",
-                             { id: "pocket-button",
-                               title: gPocketBundle.GetStringFromName("pocket-button.tooltiptext"),
-                               image: "chrome://pocket/content/panels/img/pocket.svg#pocket-mark" });
   },
   shutdown: function() {
     let mm = Services.mm;
     mm.removeMessageListener("Reader:OnSetup", this);
     mm.removeMessageListener("Reader:Clicked-pocket-button", this);
-    mm.broadcastAsyncMessage("Reader:RemoveButton", { id: "pocket-button" });
+    this.hidden = true;
+  },
+  update: function() {
+    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.svg#pocket-mark" });
+    }
   },
   receiveMessage: function(message) {
     switch (message.name) {
       case "Reader:OnSetup": {
-        // tell the reader about our button.  A chrome url here doesn't work, but
-        // we can use the resoure url.
+        // Tell the reader about our button.
         message.target.messageManager.
           sendAsyncMessage("Reader:AddButton", { id: "pocket-button",
                                                  title: gPocketBundle.GetStringFromName("pocket-button.tooltiptext"),
                                                  image: "chrome://pocket/content/panels/img/pocket.svg#pocket-mark"});
         break;
       }
       case "Reader:Clicked-pocket-button": {
         let doc = message.target.ownerDocument;
@@ -351,20 +369,20 @@ 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);
     AboutSaved.register();
     AboutSignup.register();
-    CreatePocketWidget(reason);
+    PocketReader.startup();
     CustomizableUI.addListener(this);
+    CreatePocketWidget(reason);
     PocketContextMenu.init();
-    PocketReader.startup();
 
     if (reason != APP_STARTUP) {
       for (let win of allBrowserWindows()) {
         this.setWindowScripts(win);
         this.addStyles(win);
         this.updateWindow(win);
       }
     }
@@ -454,44 +472,32 @@ var PocketOverlay = {
         "id": "panelMenu_pocketSeparator"
       });
       // nextSibling is no-id toolbarseparator
       // insert separator first then button
       sib = sib.nextSibling;
       sib.parentNode.insertBefore(sep, sib);
       sib.parentNode.insertBefore(menu, sib);
     }
-
-    this.updatePocketItemVisibility(document);
   },
-  onWidgetAdded: function(aWidgetId, aArea, aPosition) {
-    for (let win of allBrowserWindows()) {
-      this.updatePocketItemVisibility(win.document);
+  onWidgetAfterDOMChange: function(aWidgetNode) {
+    if (aWidgetNode.id != "pocket-button") {
+      return;
     }
-  },
-  onWidgetRemoved: function(aWidgetId, aArea, aPosition) {
-    for (let win of allBrowserWindows()) {
-      this.updatePocketItemVisibility(win.document);
-    }
-  },
-  updatePocketItemVisibility: function(doc) {
+    let doc = aWidgetNode.ownerDocument;
     let hidden = !CustomizableUI.getPlacementOfWidget("pocket-button");
     for (let prefix of ["panelMenu_", "menu_", "BMB_"]) {
       let element = doc.getElementById(prefix + "pocket");
       if (element) {
         element.hidden = hidden;
         doc.getElementById(prefix + "pocketSeparator").hidden = hidden;
       }
     }
     // enable or disable reader button
-    if (hidden) {
-      PocketReader.shutdown();
-    } else {
-      PocketReader.startup();
-    }
+    PocketReader.hidden = hidden;
   },
 
   addStyles: function(win) {
     let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils);
     utils.addSheet(this._cachedSheet, this._sheetType);
   },
 
   removeStyles: function(win) {
--- a/toolkit/components/reader/AboutReader.jsm
+++ b/toolkit/components/reader/AboutReader.jsm
@@ -168,17 +168,18 @@ AboutReader.prototype = {
       // Triggered by Android user pressing BACK while the banner font-dropdown is open.
       case "Reader:CloseDropdown": {
         // Just close it.
         this._closeDropdown();
         break;
       }
 
       case "Reader:AddButton": {
-        if (message.data.id && message.data.image) {
+        if (message.data.id && message.data.image &&
+            !this._doc.getElementById(message.data.id)) {
           let btn = this._doc.createElement("button");
           btn.setAttribute("class", "button");
           btn.setAttribute("style", "background-image: url('" + message.data.image + "')");
           btn.setAttribute("id", message.data.id);
           if (message.data.title)
             btn.setAttribute("title", message.data.title);
           if (message.data.text)
             btn.textContent = message.data.text;
@@ -188,17 +189,18 @@ AboutReader.prototype = {
             this._mm.sendAsyncMessage("Reader:Clicked-" + button.getAttribute("id"), { article: this._article });
           });
         }
         break;
       }
       case "Reader:RemoveButton": {
         if (message.data.id) {
           let btn = this._doc.getElementById(message.data.id);
-          btn.remove();
+          if (btn)
+            btn.remove();
         }
         break;
       }
     }
   },
 
   handleEvent: function(aEvent) {
     if (!aEvent.isTrusted)