Bug 1276072 - wait for 'connect' event and replace 'statechange' event with 'terminate' event. r=kershaw.
MozReview-Commit-ID: KRKTVUtbpNo
--- 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
};