Bug 1322274: Make internal pc._legacyCatchAndCloseGuard responsible for returning content promise. draft
authorJan-Ivar Bruaroey <jib@mozilla.com>
Thu, 17 Nov 2016 00:22:43 -0500
changeset 448580 2f08f992e5018fc63cf080529f991b2630eab11a
parent 448570 c51e7406d7b2e2246a1ece0d8989282ca752039f
child 448581 7f620a3ed22fd7f65603ff2cacff069e24143512
push id38366
push userjbruaroey@mozilla.com
push dateSun, 11 Dec 2016 01:26:04 +0000
bugs1322274
milestone53.0a1
Bug 1322274: Make internal pc._legacyCatchAndCloseGuard responsible for returning content promise. MozReview-Commit-ID: WovvgG4Olp
dom/media/PeerConnection.js
--- a/dom/media/PeerConnection.js
+++ b/dom/media/PeerConnection.js
@@ -527,22 +527,24 @@ RTCPeerConnection.prototype = {
   // This wrapper helps implement legacy callbacks in a manner that produces
   // correct line-numbers in errors, provided that methods validate their inputs
   // before putting themselves on the pc's operations chain.
   //
   // It also serves as guard against settling promises past close().
 
   _legacyCatchAndCloseGuard: function(onSuccess, onError, func) {
     if (!onSuccess) {
-      return func().then(v => (this._closed ? new Promise(() => {}) : v),
-                         e => (this._closed ? new Promise(() => {}) : Promise.reject(e)));
+      return this._win.Promise.resolve(func())
+        .then(v => (this._closed ? new Promise(() => {}) : v),
+              e => (this._closed ? new Promise(() => {}) : Promise.reject(e)));
     }
     try {
-      return func().then(this._wrapLegacyCallback(onSuccess),
-                         this._wrapLegacyCallback(onError));
+      return this._win.Promise.resolve(func())
+        .then(this._wrapLegacyCallback(onSuccess),
+              this._wrapLegacyCallback(onError));
     } catch (e) {
       this._wrapLegacyCallback(onError)(e);
       return this._win.Promise.resolve(); // avoid webidl TypeError
     }
   },
 
   _wrapLegacyCallback: function(func) {
     return result => {
@@ -735,17 +737,17 @@ RTCPeerConnection.prototype = {
       onSuccess = optionsOrOnSuccess;
     } else {
       options = optionsOrOnSuccess;
     }
     return this._legacyCatchAndCloseGuard(onSuccess, onError, () => {
       let origin = Cu.getWebIDLCallerPrincipal().origin;
       return this._chain(() => {
         let p = Promise.all([this.getPermission(), this._certificateReady])
-          .then(() => new this._win.Promise((resolve, reject) => {
+          .then(() => new Promise((resolve, reject) => {
             this._onCreateOfferSuccess = resolve;
             this._onCreateOfferFailure = reject;
             this._impl.createOffer(options);
           }));
         p = this._addIdentityAssertion(p, origin);
         return p.then(sdp => Cu.cloneInto({ type: "offer", sdp: sdp }, this._win));
       });
     });
@@ -758,17 +760,17 @@ RTCPeerConnection.prototype = {
       onSuccess = optionsOrOnSuccess;
     } else {
       options = optionsOrOnSuccess;
     }
     return this._legacyCatchAndCloseGuard(onSuccess, onError, () => {
       let origin = Cu.getWebIDLCallerPrincipal().origin;
       return this._chain(() => {
         let p = Promise.all([this.getPermission(), this._certificateReady])
-          .then(() => new this._win.Promise((resolve, reject) => {
+          .then(() => new Promise((resolve, reject) => {
             // We give up line-numbers in errors by doing this here, but do all
             // state-checks inside the chain, to support the legacy feature that
             // callers don't have to wait for setRemoteDescription to finish.
             if (!this.remoteDescription) {
               throw new this._win.DOMException("setRemoteDescription not called",
                                                "InvalidStateError");
             }
             if (this.remoteDescription.type != "offer") {
@@ -832,17 +834,17 @@ RTCPeerConnection.prototype = {
 
       if (desc.type !== "rollback" && !desc.sdp) {
         throw new this._win.DOMException(
             "Empty or null SDP provided to setLocalDescription",
             "InvalidParameterError");
       }
 
       return this._chain(() => this.getPermission()
-          .then(() => new this._win.Promise((resolve, reject) => {
+          .then(() => new Promise((resolve, reject) => {
         this._onSetLocalDescriptionSuccess = resolve;
         this._onSetLocalDescriptionFailure = reject;
         this._impl.setLocalDescription(type, desc.sdp);
       })));
     });
   },
 
   _validateIdentity: function(sdp, origin) {
@@ -920,30 +922,29 @@ RTCPeerConnection.prototype = {
             "InvalidParameterError");
       }
 
       // Get caller's origin before hitting the promise chain
       let origin = Cu.getWebIDLCallerPrincipal().origin;
 
       return this._chain(() => {
         let setRem = this.getPermission()
-          .then(() => new this._win.Promise((resolve, reject) => {
+          .then(() => new Promise((resolve, reject) => {
             this._onSetRemoteDescriptionSuccess = resolve;
             this._onSetRemoteDescriptionFailure = reject;
             this._impl.setRemoteDescription(type, desc.sdp);
           })).then(() => { this._updateCanTrickle(); });
 
         if (desc.type === "rollback") {
           return setRem;
         }
 
         // Do setRemoteDescription and identity validation in parallel
         let validId = this._validateIdentity(desc.sdp, origin);
-        return this._win.Promise.all([setRem, validId])
-          .then(() => {}); // must return undefined
+        return Promise.all([setRem, validId]).then(() => {}); // return undefined
       });
     });
   },
 
   setIdentityProvider: function(provider, protocol, username) {
     this._checkClosed();
     this._localIdp.setIdentityProvider(provider, protocol, username);
   },
@@ -995,17 +996,17 @@ RTCPeerConnection.prototype = {
       if (!c) {
         // TODO: Implement processing for end-of-candidates (bug 1318167)
         return Promise.resolve();
       }
       if (c.sdpMid === null && c.sdpMLineIndex === null) {
         throw new this._win.DOMException("Invalid candidate (both sdpMid and sdpMLineIndex are null).",
                                          "TypeError");
       }
-      return this._chain(() => new this._win.Promise((resolve, reject) => {
+      return this._chain(() => new Promise((resolve, reject) => {
         this._onAddIceCandidateSuccess = resolve;
         this._onAddIceCandidateError = reject;
         this._impl.addIceCandidate(c.candidate, c.sdpMid || "", c.sdpMLineIndex);
       }));
     });
   },
 
   addStream: function(stream) {
@@ -1187,17 +1188,17 @@ RTCPeerConnection.prototype = {
   changeIceConnectionState: function(state) {
     this._iceConnectionState = state;
     _globalPCList.notifyLifecycleObservers(this, "iceconnectionstatechange");
     this.dispatchEvent(new this._win.Event("iceconnectionstatechange"));
   },
 
   getStats: function(selector, onSuccess, onError) {
     return this._legacyCatchAndCloseGuard(onSuccess, onError, () => {
-      return this._chain(() => new this._win.Promise((resolve, reject) => {
+      return this._chain(() => new Promise((resolve, reject) => {
         this._onGetStatsSuccess = resolve;
         this._onGetStatsFailure = reject;
         this._impl.getStats(selector);
       }));
     });
   },
 
   createDataChannel: function(label, {