Bug 1238310: Part 2 - Return a promise from FullZoom.reset that resolves on completion. r=adw
authorKris Maglione <maglione.k@gmail.com>
Sat, 23 Apr 2016 22:17:50 -0700
changeset 332380 660fd42c40243a9011595b694347ea42af0da06d
parent 332379 517ccf702ca23a525440b0617aae4dd9a88559f5
child 332381 620da4d591b8d8fb80a8bf19714445d898d20554
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersadw
bugs1238310
milestone48.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 1238310: Part 2 - Return a promise from FullZoom.reset that resolves on completion. r=adw MozReview-Commit-ID: Af9eV8sLE2h
browser/base/content/browser-fullZoom.js
--- a/browser/base/content/browser-fullZoom.js
+++ b/browser/base/content/browser-fullZoom.js
@@ -277,31 +277,30 @@ var FullZoom = {
     ZoomManager.setZoomForBrowser(browser, value);
     this._ignorePendingZoomAccesses(browser);
     this._applyZoomToPref(browser);
   },
 
   /**
    * Sets the zoom level of the page in the given browser to the global zoom
    * level.
+   *
+   * @return A promise which resolves when the zoom reset has been applied.
    */
   reset: function FullZoom_reset(browser = gBrowser.selectedBrowser) {
     let token = this._getBrowserToken(browser);
-    this._getGlobalValue(browser, function (value) {
+    let result = this._getGlobalValue(browser).then(value => {
       if (token.isCurrent) {
         ZoomManager.setZoomForBrowser(browser, value === undefined ? 1 : value);
         this._ignorePendingZoomAccesses(browser);
-        this._executeSoon(function () {
-          // _getGlobalValue may be either sync or async, so notify asyncly so
-          // observers are guaranteed consistent behavior.
-          Services.obs.notifyObservers(null, "browser-fullZoom:zoomReset", "");
-        });
+        Services.obs.notifyObservers(null, "browser-fullZoom:zoomReset", "");
       }
     });
     this._removePref(browser);
+    return result;
   },
 
   /**
    * Set the zoom level for a given browser.
    *
    * Per nsPresContext::setFullZoom, we can set the zoom to its current value
    * without significant impact on performance, as the setting is only applied
    * if it differs from the current setting.  In fact getting the zoom and then
@@ -339,17 +338,17 @@ var FullZoom = {
     if (aValue !== undefined) {
       ZoomManager.setZoomForBrowser(aBrowser, this._ensureValid(aValue));
       this._ignorePendingZoomAccesses(aBrowser);
       this._executeSoon(aCallback);
       return;
     }
 
     let token = this._getBrowserToken(aBrowser);
-    this._getGlobalValue(aBrowser, function (value) {
+    this._getGlobalValue(aBrowser).then(value => {
       if (token.isCurrent) {
         ZoomManager.setZoomForBrowser(aBrowser, value === undefined ? 1 : value);
         this._ignorePendingZoomAccesses(aBrowser);
       }
       this._executeSoon(aCallback);
     });
   },
 
@@ -473,42 +472,37 @@ var FullZoom = {
       return ZoomManager.MAX;
 
     return aValue;
   },
 
   /**
    * Gets the global browser.content.full-zoom content preference.
    *
-   * WARNING: callback may be called synchronously or asynchronously.  The
-   * reason is that it's usually desirable to avoid turns of the event loop
-   * where possible, since they can lead to visible, jarring jumps in zoom
-   * level.  It's not always possible to avoid them, though.  As a convenience,
-   * then, this method takes a callback and returns nothing.
-   *
    * @param browser   The browser pertaining to the zoom.
-   * @param callback  Synchronously or asynchronously called when done.  It's
-   *                  bound to this object (FullZoom) and called as:
-   *                    callback(prefValue)
+   * @returns Promise<prefValue>
+   *                  Resolves to the preference value when done.
    */
-  _getGlobalValue: function FullZoom__getGlobalValue(browser, callback) {
+  _getGlobalValue: function FullZoom__getGlobalValue(browser) {
     // * !("_globalValue" in this) => global value not yet cached.
     // * this._globalValue === undefined => global value known not to exist.
     // * Otherwise, this._globalValue is a number, the global value.
-    if ("_globalValue" in this) {
-      callback.call(this, this._globalValue, true);
-      return;
-    }
-    let value = undefined;
-    this._cps2.getGlobal(this.name, this._loadContextFromBrowser(browser), {
-      handleResult: function (pref) { value = pref.value; },
-      handleCompletion: function (reason) {
-        this._globalValue = this._ensureValid(value);
-        callback.call(this, this._globalValue);
-      }.bind(this)
+    return new Promise(resolve => {
+      if ("_globalValue" in this) {
+        resolve(this._globalValue);
+        return;
+      }
+      let value = undefined;
+      this._cps2.getGlobal(this.name, this._loadContextFromBrowser(browser), {
+        handleResult: function (pref) { value = pref.value; },
+        handleCompletion: (reason) => {
+          this._globalValue = this._ensureValid(value);
+          resolve(this._globalValue);
+        }
+      });
     });
   },
 
   /**
    * Gets the load context from the given Browser.
    *
    * @param Browser  The Browser whose load context will be returned.
    * @return        The nsILoadContext of the given Browser.