Bug 1345375 - Use the FullZoomChange event instead of browser-fullZoom observers since FullZoomChange works on MediaDocuments. r=dao a=gchang
authorJared Wein <jwein@mozilla.com>
Tue, 14 Mar 2017 19:58:38 -0400
changeset 395362 135f5507e658d1cec40b4a3b03bed2974cc4f878
parent 395361 8d6bf64a18bfb6e6bfcdda811b8e73b60bf1a002
child 395363 75b07f6e17b971a63a24f0c9222eb2f0c8c46a05
push id1468
push userasasaki@mozilla.com
push dateMon, 05 Jun 2017 19:31:07 +0000
treeherdermozilla-release@0641fc6ee9d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersdao, gchang
bugs1345375
milestone54.0a2
Bug 1345375 - Use the FullZoomChange event instead of browser-fullZoom observers since FullZoomChange works on MediaDocuments. r=dao a=gchang MozReview-Commit-ID: 9SYHLc2FluC
browser/components/customizableui/CustomizableWidgets.jsm
browser/modules/URLBarZoom.jsm
browser/modules/test/browser/browser_urlBar_zoom.js
toolkit/content/browser-content.js
--- a/browser/components/customizableui/CustomizableWidgets.jsm
+++ b/browser/components/customizableui/CustomizableWidgets.jsm
@@ -724,19 +724,18 @@ const CustomizableWidgets = [
           zoomFactor = Math.round(window.ZoomManager.zoom * 100);
         } catch (e) {}
         zoomResetButton.setAttribute("label", CustomizableUI.getLocalizedProperty(
           buttons[1], "label", [updateDisplay ? zoomFactor : 100]
         ));
       }
 
       // Register ourselves with the service so we know when the zoom prefs change.
-      Services.obs.addObserver(updateZoomResetButton, "browser-fullZoom:zoomChange", false);
-      Services.obs.addObserver(updateZoomResetButton, "browser-fullZoom:zoomReset", false);
       Services.obs.addObserver(updateZoomResetButton, "browser-fullZoom:location-change", false);
+      window.addEventListener("FullZoomChange", updateZoomResetButton);
 
       if (inPanel) {
         let panel = aDocument.getElementById(kPanelId);
         panel.addEventListener("popupshowing", updateZoomResetButton);
       } else {
         if (inToolbar) {
           let container = window.gBrowser.tabContainer;
           container.addEventListener("TabSelect", updateZoomResetButton);
@@ -803,19 +802,18 @@ const CustomizableWidgets = [
           updateZoomResetButton();
         }.bind(this),
 
         onWidgetInstanceRemoved: function(aWidgetId, aDoc) {
           if (aWidgetId != this.id || aDoc != aDocument)
             return;
 
           CustomizableUI.removeListener(listener);
-          Services.obs.removeObserver(updateZoomResetButton, "browser-fullZoom:zoomChange");
-          Services.obs.removeObserver(updateZoomResetButton, "browser-fullZoom:zoomReset");
           Services.obs.removeObserver(updateZoomResetButton, "browser-fullZoom:location-change");
+          window.removeEventListener("FullZoomChange", updateZoomResetButton);
           let panel = aDoc.getElementById(kPanelId);
           panel.removeEventListener("popupshowing", updateZoomResetButton);
           let container = aDoc.defaultView.gBrowser.tabContainer;
           container.removeEventListener("TabSelect", updateZoomResetButton);
         }.bind(this),
 
         onCustomizeStart(aWindow) {
           if (aWindow.document == aDocument) {
--- a/browser/modules/URLBarZoom.jsm
+++ b/browser/modules/URLBarZoom.jsm
@@ -7,81 +7,85 @@
 
 this.EXPORTED_SYMBOLS = [ "URLBarZoom" ];
 
 Components.utils.import("resource://gre/modules/Services.jsm");
 
 var URLBarZoom = {
   init(aWindow) {
     aWindow.addEventListener("EndSwapDocShells", onEndSwapDocShells, true);
+    aWindow.addEventListener("FullZoomChange", onFullZoomChange);
     aWindow.addEventListener("unload", () => {
       aWindow.removeEventListener("EndSwapDocShells", onEndSwapDocShells, true);
+      aWindow.removeEventListener("FullZoomChange", onFullZoomChange);
     }, {once: true});
   },
 }
 
-function fullZoomObserver(aSubject, aTopic) {
+function fullZoomLocationChangeObserver(aSubject, aTopic) {
   // If the tab was the last one in its window and has been dragged to another
   // window, the original browser's window will be unavailable here. Since that
   // window is closing, we can just ignore this notification.
   if (!aSubject.ownerGlobal) {
     return;
   }
 
-  let animate = (aTopic != "browser-fullZoom:location-change");
-  updateZoomButton(aSubject, animate);
+  updateZoomButton(aSubject, false);
 }
 
 function onEndSwapDocShells(event) {
   updateZoomButton(event.originalTarget);
 }
 
+function onFullZoomChange(event) {
+  let browser;
+  if (event.target.nodeType == event.target.DOCUMENT_NODE) {
+    // In non-e10s, the event is dispatched on the contentDocument
+    // so we need to jump through some hoops to get to the <xul:browser>.
+    let gBrowser = event.currentTarget.gBrowser;
+    let topDoc = event.target.defaultView.top.document;
+    browser = gBrowser.getBrowserForDocument(topDoc);
+  } else {
+    browser = event.originalTarget;
+  }
+  updateZoomButton(browser, true);
+}
+
   /**
    * Updates the zoom button in the location bar.
    *
    * @param {object} aBrowser The browser that the zoomed content resides in.
    * @param {boolean} aAnimate Should be True for all cases unless the zoom
    *   change is related to tab switching. Optional
-   * @param {number} aValue The value that should be used for the zoom control.
-   *   If not provided then the value will be read from the window. Useful
-   *   if the data on the window may be stale.
    */
-function updateZoomButton(aBrowser, aAnimate = false, aValue = undefined) {
+function updateZoomButton(aBrowser, aAnimate = false) {
   let win = aBrowser.ownerGlobal;
   if (aBrowser != win.gBrowser.selectedBrowser) {
     return;
   }
 
   let customizableZoomControls = win.document.getElementById("zoom-controls");
   let zoomResetButton = win.document.getElementById("urlbar-zoom-button");
 
   // Ensure that zoom controls haven't already been added to browser in Customize Mode
   if (customizableZoomControls &&
       customizableZoomControls.getAttribute("cui-areatype") == "toolbar") {
     zoomResetButton.hidden = true;
     return;
   }
 
-  let zoomFactor = Math.round((aValue || win.ZoomManager.zoom) * 100);
+  let zoomFactor = Math.round(win.ZoomManager.zoom * 100);
   if (zoomFactor != 100) {
-    // Check if zoom button is visible and update label if it is
-    if (zoomResetButton.hidden) {
-      zoomResetButton.hidden = false;
-    }
+    zoomResetButton.hidden = false;
     if (aAnimate) {
       zoomResetButton.setAttribute("animate", "true");
     } else {
       zoomResetButton.removeAttribute("animate");
     }
     zoomResetButton.setAttribute("label",
         win.gNavigatorBundle.getFormattedString("urlbar-zoom-button.label", [zoomFactor]));
   } else {
     // Hide button if zoom is at 100%
     zoomResetButton.hidden = true;
   }
 }
 
-Services.obs.addObserver(fullZoomObserver, "browser-fullZoom:zoomChange", false);
-Services.obs.addObserver(fullZoomObserver, "browser-fullZoom:zoomReset", false);
-Services.obs.addObserver(fullZoomObserver, "browser-fullZoom:location-change", false);
-Services.mm.addMessageListener("SyntheticDocument:ZoomChange", function(aMessage) {
-  updateZoomButton(aMessage.target, true, aMessage.data.value);
-});
+Services.obs.addObserver(fullZoomLocationChangeObserver, "browser-fullZoom:location-change", false);
--- a/browser/modules/test/browser/browser_urlBar_zoom.js
+++ b/browser/modules/test/browser/browser_urlBar_zoom.js
@@ -19,55 +19,38 @@ add_task(function* () {
   yield labelUpdatePromise;
   info("Zoom increased to " + Math.floor(ZoomManager.zoom * 100) + "%");
   is(zoomResetButton.hidden, false, "Zoom reset button is now visible");
   let pageZoomLevel = Math.floor(ZoomManager.zoom * 100);
   let expectedZoomLevel = 110;
   let buttonZoomLevel = parseInt(zoomResetButton.getAttribute("label"), 10);
   is(buttonZoomLevel, expectedZoomLevel, ("Button label updated successfully to " + Math.floor(ZoomManager.zoom * 100) + "%"));
 
-  let zoomResetPromise = promiseObserverNotification("browser-fullZoom:zoomReset");
+  let zoomResetPromise = BrowserTestUtils.waitForEvent(window, "FullZoomChange");
   zoomResetButton.click();
   yield zoomResetPromise;
   pageZoomLevel = Math.floor(ZoomManager.zoom * 100);
   expectedZoomLevel = 100;
   is(pageZoomLevel, expectedZoomLevel, "Clicking zoom button successfully resets browser zoom to 100%");
   is(zoomResetButton.hidden, true, "Zoom reset button returns to being hidden");
-
 });
 
 add_task(function* () {
   info("Confirm that URL bar zoom button doesn't appear when customizable zoom widget is added to toolbar");
   CustomizableUI.addWidgetToArea("zoom-controls", CustomizableUI.AREA_NAVBAR);
   let zoomCustomizableWidget = document.getElementById("zoom-reset-button");
   let zoomResetButton = document.getElementById("urlbar-zoom-button");
-  let zoomChangePromise = promiseObserverNotification("browser-fullZoom:zoomChange");
+  let zoomChangePromise = BrowserTestUtils.waitForEvent(window, "FullZoomChange");
   FullZoom.enlarge();
   yield zoomChangePromise;
   is(zoomResetButton.hidden, true, "URL zoom button remains hidden despite zoom increase");
   is(parseInt(zoomCustomizableWidget.label, 10), 110, "Customizable zoom widget's label has updated to " + zoomCustomizableWidget.label);
 });
 
 add_task(function* asyncCleanup() {
   // reset zoom level and customizable widget
   ZoomManager.zoom = initialPageZoom;
   is(ZoomManager.zoom, 1, "Zoom level was restored");
   if (document.getElementById("zoom-controls")) {
     CustomizableUI.removeWidgetFromArea("zoom-controls", CustomizableUI.AREA_NAVBAR);
     ok(!document.getElementById("zoom-controls"), "Customizable zoom widget removed from toolbar");
   }
-
 });
-
-function promiseObserverNotification(aObserver) {
-  let deferred = Promise.defer();
-  function notificationCallback(e) {
-    Services.obs.removeObserver(notificationCallback, aObserver);
-    clearTimeout(timeoutId);
-    deferred.resolve();
-  }
-  let timeoutId = setTimeout(() => {
-    Services.obs.removeObserver(notificationCallback, aObserver);
-    deferred.reject("Notification '" + aObserver + "' did not happen within 20 seconds.");
-  }, kTimeoutInMS);
-  Services.obs.addObserver(notificationCallback, aObserver, false);
-  return deferred.promise;
-}
--- a/toolkit/content/browser-content.js
+++ b/toolkit/content/browser-content.js
@@ -1772,30 +1772,16 @@ let DateTimePickerListener = {
       default:
         break;
     }
   },
 }
 
 DateTimePickerListener.init();
 
-let URLBarZoom = {
-  init() {
-    addEventListener("FullZoomChange", e => {
-      if (!e.target.mozSyntheticDocument) {
-        return;
-      }
-      sendSyncMessage("SyntheticDocument:ZoomChange",
-        {"value": docShell.contentViewer.fullZoom});
-    });
-  }
-};
-
-URLBarZoom.init();
-
 addEventListener("mozshowdropdown", event => {
   if (!event.isTrusted)
     return;
 
   if (!SelectContentHelper.open) {
     new SelectContentHelper(event.target, {isOpenedViaTouch: false}, this);
   }
 });