Bug 1276072 - wait for 'connect' event and replace 'statechange' event with 'terminate' event. r=kershaw. draft
authorShih-Chiang Chien <schien@mozilla.com>
Wed, 01 Jun 2016 18:42:15 +0800
changeset 373862 4fc8be4b21966523cad78d2ea50e41937d2bea63
parent 373501 25321494921c824703a605127fb1f99b1faf5910
child 522486 cb3dac8d173aee50379b0c0288d40f35c85d2b59
push id19857
push userschien@mozilla.com
push dateWed, 01 Jun 2016 10:39:21 +0000
reviewerskershaw
bugs1276072
milestone49.0a1
Bug 1276072 - wait for 'connect' event and replace 'statechange' event with 'terminate' event. r=kershaw. MozReview-Commit-ID: KRKTVUtbpNo
toolkit/modules/secondscreen/PresentationApp.jsm
--- a/toolkit/modules/secondscreen/PresentationApp.jsm
+++ b/toolkit/modules/secondscreen/PresentationApp.jsm
@@ -34,17 +34,19 @@ function PresentationApp(service, reques
 }
 
 PresentationApp.prototype = {
   start: function start(callback) {
     this.request.startWithDevice(this.service.uuid)
     .then((session) => {
       this._session = session;
       if (callback) {
-        callback(true);
+        session.addEventListener('connect', () => {
+          callback(true);
+        });
       }
     }, () => {
       if (callback) {
         callback(false);
       }
     });
   },
 
@@ -77,35 +79,35 @@ PresentationApp.prototype = {
  * see https://github.com/mozilla-b2g/gaia/tree/master/tv_apps/fling-player
  */
 function RemoteMedia(session, listener) {
   this._session = session ;
   this._listener = listener;
   this._status = STATE_UNINIT;
 
   this._session.addEventListener("message", this);
-  this._session.addEventListener("statechange", this);
+  this._session.addEventListener("terminate", this);
 
   if (this._listener && "onRemoteMediaStart" in this._listener) {
     Services.tm.mainThread.dispatch((function() {
       this._listener.onRemoteMediaStart(this);
     }).bind(this), Ci.nsIThread.DISPATCH_NORMAL);
   }
 }
 
 RemoteMedia.prototype = {
   _seq: 0,
 
   handleEvent: function(e) {
     switch (e.type) {
       case "message":
         this._onmessage(e);
         break;
-      case "statechange":
-        this._onstatechange(e);
+      case "terminate":
+        this._onterminate(e);
         break;
     }
   },
 
   _onmessage: function(e) {
     DEBUG && debug("onmessage: " + e.data);
     if (this.status === STATE_SHUTDOWN) {
       return;
@@ -123,23 +125,21 @@ RemoteMedia.prototype = {
         this._status = STATE_STARTED;
         if (this._listener && "onRemoteMediaStatus" in this._listener) {
           this._listener.onRemoteMediaStatus(this);
         }
       }
     }
   },
 
-  _onstatechange: function(e) {
-    DEBUG && debug("onstatechange: " + this._session.state);
-    if (this._session.state !== "connected") {
-      this._status = STATE_SHUTDOWN;
-      if (this._listener && "onRemoteMediaStop" in this._listener) {
-        this._listener.onRemoteMediaStop(this);
-      }
+  _onterminate: function(e) {
+    DEBUG && debug("onterminate: " + this._session.state);
+    this._status = STATE_SHUTDOWN;
+    if (this._listener && "onRemoteMediaStop" in this._listener) {
+      this._listener.onRemoteMediaStop(this);
     }
   },
 
   _sendCommand: function(command, data) {
     let msg = {
       'type': command,
       'seq': ++this._seq
     };