Bug 1238310: Part 2 - Return a promise from FullZoom.reset that resolves on completion. r?adw draft
authorKris Maglione <maglione.k@gmail.com>
Fri, 01 Apr 2016 15:37:29 -0700
changeset 353954 5da4e631789e8da63e45b0385131dbf6ae3e72f7
parent 353953 4c643683c24b984676e0823fa607949dff7e9111
child 353955 b43761bbeccfa2da0fad969676c2e7ac9b434f7d
push id15974
push usermaglione.k@gmail.com
push dateWed, 20 Apr 2016 03:00:13 +0000
reviewersadw
bugs1238310
milestone48.0a1
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
@@ -280,28 +280,25 @@ var FullZoom = {
   },
 
   /**
    * Sets the zoom level of the page in the given browser to the global zoom
    * level.
    */
   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 +336,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 +470,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.