2016-04-11 06:20:55 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
|
|
|
// Bug 1228209 - plan to remove this eventually
|
|
|
|
function log(aMsg) {
|
|
|
|
//dump("-*- PresentationDataChannelSessionTransport.js : " + aMsg + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
const PRESENTATIONTRANSPORT_CID = Components.ID("{dd2bbf2f-3399-4389-8f5f-d382afb8b2d6}");
|
|
|
|
const PRESENTATIONTRANSPORT_CONTRACTID = "mozilla.org/presentation/datachanneltransport;1";
|
|
|
|
|
|
|
|
const PRESENTATIONTRANSPORTBUILDER_CID = Components.ID("{215b2f62-46e2-4004-a3d1-6858e56c20f3}");
|
|
|
|
const PRESENTATIONTRANSPORTBUILDER_CONTRACTID = "mozilla.org/presentation/datachanneltransportbuilder;1";
|
|
|
|
|
|
|
|
function PresentationDataChannelDescription(aDataChannelSDP) {
|
|
|
|
this._dataChannelSDP = JSON.stringify(aDataChannelSDP);
|
|
|
|
}
|
|
|
|
|
|
|
|
PresentationDataChannelDescription.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationChannelDescription]),
|
|
|
|
get type() {
|
2016-09-18 02:42:00 +03:00
|
|
|
return Ci.nsIPresentationChannelDescription.TYPE_DATACHANNEL;
|
2016-04-11 06:20:55 +03:00
|
|
|
},
|
|
|
|
get tcpAddress() {
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
get tcpPort() {
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
get dataChannelSDP() {
|
|
|
|
return this._dataChannelSDP;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function PresentationTransportBuilder() {
|
|
|
|
log("PresentationTransportBuilder construct");
|
|
|
|
this._isControlChannelNeeded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PresentationTransportBuilder.prototype = {
|
|
|
|
classID: PRESENTATIONTRANSPORTBUILDER_CID,
|
|
|
|
contractID: PRESENTATIONTRANSPORTBUILDER_CONTRACTID,
|
2016-09-16 15:59:00 +03:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationSessionTransportBuilder,
|
|
|
|
Ci.nsIPresentationDataChannelSessionTransportBuilder,
|
2016-04-11 06:20:55 +03:00
|
|
|
Ci.nsITimerCallback]),
|
|
|
|
|
2016-06-03 06:03:15 +03:00
|
|
|
buildDataChannelTransport: function(aRole, aWindow, aListener) {
|
|
|
|
if (!aRole || !aWindow || !aListener) {
|
2016-04-11 06:20:55 +03:00
|
|
|
log("buildDataChannelTransport with illegal parameters");
|
|
|
|
throw Cr.NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._window) {
|
|
|
|
log("buildDataChannelTransport has started.");
|
|
|
|
throw Cr.NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2016-04-18 03:23:00 +03:00
|
|
|
log("buildDataChannelTransport with role " + aRole);
|
|
|
|
this._role = aRole;
|
2016-04-11 06:20:55 +03:00
|
|
|
this._window = aWindow;
|
|
|
|
this._listener = aListener.QueryInterface(Ci.nsIPresentationSessionTransportBuilderListener);
|
|
|
|
|
|
|
|
// TODO bug 1227053 set iceServers from |nsIPresentationDevice|
|
|
|
|
this._peerConnection = new this._window.RTCPeerConnection();
|
|
|
|
|
2016-06-03 06:03:15 +03:00
|
|
|
// |this._listener == null| will throw since the control channel is
|
2016-04-11 06:20:55 +03:00
|
|
|
// abnormally closed.
|
|
|
|
this._peerConnection.onicecandidate = aEvent => aEvent.candidate &&
|
2016-06-03 06:03:15 +03:00
|
|
|
this._listener.sendIceCandidate(JSON.stringify(aEvent.candidate));
|
2016-04-11 06:20:55 +03:00
|
|
|
|
|
|
|
this._peerConnection.onnegotiationneeded = () => {
|
2016-04-18 03:23:00 +03:00
|
|
|
log("onnegotiationneeded with role " + this._role);
|
2016-04-11 06:20:55 +03:00
|
|
|
this._peerConnection.createOffer()
|
|
|
|
.then(aOffer => this._peerConnection.setLocalDescription(aOffer))
|
2016-06-03 06:03:15 +03:00
|
|
|
.then(() => this._listener
|
2016-04-11 06:20:55 +03:00
|
|
|
.sendOffer(new PresentationDataChannelDescription(this._peerConnection.localDescription)))
|
|
|
|
.catch(e => this._reportError(e));
|
|
|
|
}
|
|
|
|
|
2016-04-18 03:23:00 +03:00
|
|
|
switch (this._role) {
|
|
|
|
case Ci.nsIPresentationService.ROLE_CONTROLLER:
|
2016-04-11 06:20:55 +03:00
|
|
|
this._dataChannel = this._peerConnection.createDataChannel("presentationAPI");
|
|
|
|
this._setDataChannel();
|
|
|
|
break;
|
|
|
|
|
2016-04-18 03:23:00 +03:00
|
|
|
case Ci.nsIPresentationService.ROLE_RECEIVER:
|
2016-04-11 06:20:55 +03:00
|
|
|
this._peerConnection.ondatachannel = aEvent => {
|
|
|
|
this._dataChannel = aEvent.channel;
|
2016-09-18 02:42:00 +03:00
|
|
|
// Ensure the binaryType of dataChannel is blob.
|
|
|
|
this._dataChannel.binaryType = "blob";
|
2016-04-11 06:20:55 +03:00
|
|
|
this._setDataChannel();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw Cr.NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO bug 1228235 we should have a way to let device providers customize
|
|
|
|
// the time-out duration.
|
|
|
|
let timeout;
|
|
|
|
try {
|
|
|
|
timeout = Services.prefs.getIntPref("presentation.receiver.loading.timeout");
|
|
|
|
} catch (e) {
|
|
|
|
// This happens if the pref doesn't exist, so we have a default value.
|
|
|
|
timeout = 10000;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The timer is to check if the negotiation finishes on time.
|
|
|
|
this._timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
this._timer.initWithCallback(this, timeout, this._timer.TYPE_ONE_SHOT);
|
|
|
|
},
|
|
|
|
|
|
|
|
notify: function() {
|
|
|
|
if (!this._sessionTransport) {
|
|
|
|
this._cleanup(Cr.NS_ERROR_NET_TIMEOUT);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_reportError: function(aError) {
|
|
|
|
log("report Error " + aError.name + ":" + aError.message);
|
|
|
|
this._cleanup(Cr.NS_ERROR_FAILURE);
|
|
|
|
},
|
|
|
|
|
|
|
|
_setDataChannel: function() {
|
|
|
|
this._dataChannel.onopen = () => {
|
2016-04-18 03:23:00 +03:00
|
|
|
log("data channel is open, notify the listener, role " + this._role);
|
2016-04-11 06:20:55 +03:00
|
|
|
|
|
|
|
// Handoff the ownership of _peerConnection and _dataChannel to
|
|
|
|
// _sessionTransport
|
|
|
|
this._sessionTransport = new PresentationTransport();
|
2016-09-18 02:42:00 +03:00
|
|
|
this._sessionTransport.init(this._peerConnection, this._dataChannel, this._window);
|
2016-04-11 06:20:55 +03:00
|
|
|
this._peerConnection = this._dataChannel = null;
|
|
|
|
|
|
|
|
this._listener.onSessionTransport(this._sessionTransport);
|
|
|
|
this._sessionTransport.callback.notifyTransportReady();
|
|
|
|
|
|
|
|
this._cleanup(Cr.NS_OK);
|
|
|
|
};
|
|
|
|
|
|
|
|
this._dataChannel.onerror = aError => {
|
|
|
|
log("data channel onerror " + aError.name + ":" + aError.message);
|
|
|
|
this._cleanup(Cr.NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_cleanup: function(aReason) {
|
|
|
|
if (aReason != Cr.NS_OK) {
|
|
|
|
this._listener.onError(aReason);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._dataChannel) {
|
|
|
|
this._dataChannel.close();
|
|
|
|
this._dataChannel = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._peerConnection) {
|
|
|
|
this._peerConnection.close();
|
|
|
|
this._peerConnection = null;
|
|
|
|
}
|
|
|
|
|
2016-04-18 03:23:00 +03:00
|
|
|
this._role = null;
|
2016-04-11 06:20:55 +03:00
|
|
|
this._window = null;
|
|
|
|
|
|
|
|
this._listener = null;
|
|
|
|
this._sessionTransport = null;
|
|
|
|
|
|
|
|
if (this._timer) {
|
|
|
|
this._timer.cancel();
|
|
|
|
this._timer = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIPresentationControlChannelListener
|
|
|
|
onOffer: function(aOffer) {
|
2016-04-18 03:23:00 +03:00
|
|
|
if (this._role !== Ci.nsIPresentationService.ROLE_RECEIVER ||
|
2016-04-11 06:20:55 +03:00
|
|
|
this._sessionTransport) {
|
|
|
|
log("onOffer status error");
|
|
|
|
this._cleanup(Cr.NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-04-18 03:23:00 +03:00
|
|
|
log("onOffer: " + aOffer.dataChannelSDP + " with role " + this._role);
|
2016-04-11 06:20:55 +03:00
|
|
|
|
|
|
|
let offer = new this._window
|
|
|
|
.RTCSessionDescription(JSON.parse(aOffer.dataChannelSDP));
|
|
|
|
|
|
|
|
this._peerConnection.setRemoteDescription(offer)
|
|
|
|
.then(() => this._peerConnection.signalingState == "stable" ||
|
|
|
|
this._peerConnection.createAnswer())
|
|
|
|
.then(aAnswer => this._peerConnection.setLocalDescription(aAnswer))
|
|
|
|
.then(() => {
|
|
|
|
this._isControlChannelNeeded = false;
|
2016-06-03 06:03:15 +03:00
|
|
|
this._listener
|
2016-04-11 06:20:55 +03:00
|
|
|
.sendAnswer(new PresentationDataChannelDescription(this._peerConnection.localDescription))
|
|
|
|
}).catch(e => this._reportError(e));
|
|
|
|
},
|
|
|
|
|
|
|
|
onAnswer: function(aAnswer) {
|
2016-04-18 03:23:00 +03:00
|
|
|
if (this._role !== Ci.nsIPresentationService.ROLE_CONTROLLER ||
|
2016-04-11 06:20:55 +03:00
|
|
|
this._sessionTransport) {
|
|
|
|
log("onAnswer status error");
|
|
|
|
this._cleanup(Cr.NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
2016-04-18 03:23:00 +03:00
|
|
|
log("onAnswer: " + aAnswer.dataChannelSDP + " with role " + this._role);
|
2016-04-11 06:20:55 +03:00
|
|
|
|
|
|
|
let answer = new this._window
|
|
|
|
.RTCSessionDescription(JSON.parse(aAnswer.dataChannelSDP));
|
|
|
|
|
|
|
|
this._peerConnection.setRemoteDescription(answer).catch(e => this._reportError(e));
|
|
|
|
this._isControlChannelNeeded = false;
|
|
|
|
},
|
|
|
|
|
|
|
|
onIceCandidate: function(aCandidate) {
|
2016-04-18 03:23:00 +03:00
|
|
|
log("onIceCandidate: " + aCandidate + " with role " + this._role);
|
2016-04-11 06:20:55 +03:00
|
|
|
let candidate = new this._window.RTCIceCandidate(JSON.parse(aCandidate));
|
|
|
|
this._peerConnection.addIceCandidate(candidate).catch(e => this._reportError(e));
|
|
|
|
},
|
|
|
|
|
2016-07-12 05:16:46 +03:00
|
|
|
notifyDisconnected: function(aReason) {
|
|
|
|
log("notifyDisconnected reason: " + aReason);
|
2016-04-11 06:20:55 +03:00
|
|
|
|
|
|
|
if (aReason != Cr.NS_OK) {
|
|
|
|
this._cleanup(aReason);
|
|
|
|
} else if (this._isControlChannelNeeded) {
|
|
|
|
this._cleanup(Cr.NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
function PresentationTransport() {
|
|
|
|
this._messageQueue = [];
|
|
|
|
this._closeReason = Cr.NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PresentationTransport.prototype = {
|
|
|
|
classID: PRESENTATIONTRANSPORT_CID,
|
|
|
|
contractID: PRESENTATIONTRANSPORT_CONTRACTID,
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIPresentationSessionTransport]),
|
|
|
|
|
2016-09-18 02:42:00 +03:00
|
|
|
init: function(aPeerConnection, aDataChannel, aWindow) {
|
2016-04-11 06:20:55 +03:00
|
|
|
log("initWithDataChannel");
|
|
|
|
this._enableDataNotification = false;
|
|
|
|
this._dataChannel = aDataChannel;
|
|
|
|
this._peerConnection = aPeerConnection;
|
2016-09-18 02:42:00 +03:00
|
|
|
this._window = aWindow;
|
2016-04-11 06:20:55 +03:00
|
|
|
|
|
|
|
this._dataChannel.onopen = () => {
|
|
|
|
log("data channel reopen. Should never touch here");
|
|
|
|
};
|
|
|
|
|
|
|
|
this._dataChannel.onclose = () => {
|
|
|
|
log("data channel onclose");
|
|
|
|
if (this._callback) {
|
|
|
|
this._callback.notifyTransportClosed(this._closeReason);
|
|
|
|
}
|
|
|
|
this._cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
this._dataChannel.onmessage = aEvent => {
|
|
|
|
log("data channel onmessage " + aEvent.data);
|
|
|
|
|
|
|
|
if (!this._enableDataNotification || !this._callback) {
|
|
|
|
log("queue message");
|
|
|
|
this._messageQueue.push(aEvent.data);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-18 02:42:00 +03:00
|
|
|
this._doNotifyData(aEvent.data);
|
2016-04-11 06:20:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
this._dataChannel.onerror = aError => {
|
|
|
|
log("data channel onerror " + aError.name + ":" + aError.message);
|
|
|
|
if (this._callback) {
|
|
|
|
this._callback.notifyTransportClosed(Cr.NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
this._cleanup();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIPresentationTransport
|
|
|
|
get selfAddress() {
|
|
|
|
throw NS_ERROR_NOT_AVAILABLE;
|
|
|
|
},
|
|
|
|
|
|
|
|
get callback() {
|
|
|
|
return this._callback;
|
|
|
|
},
|
|
|
|
|
|
|
|
set callback(aCallback) {
|
|
|
|
this._callback = aCallback;
|
|
|
|
},
|
|
|
|
|
|
|
|
send: function(aData) {
|
|
|
|
log("send " + aData);
|
|
|
|
this._dataChannel.send(aData);
|
|
|
|
},
|
|
|
|
|
2016-09-18 02:42:00 +03:00
|
|
|
sendBinaryMsg: function(aData) {
|
|
|
|
log("sendBinaryMsg");
|
|
|
|
|
|
|
|
let array = new Uint8Array(aData.length);
|
|
|
|
for (let i = 0; i < aData.length; i++) {
|
|
|
|
array[i] = aData.charCodeAt(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
this._dataChannel.send(array);
|
|
|
|
},
|
|
|
|
|
|
|
|
sendBlob: function(aBlob) {
|
|
|
|
log("sendBlob");
|
|
|
|
|
|
|
|
this._dataChannel.send(aBlob);
|
|
|
|
},
|
|
|
|
|
2016-04-11 06:20:55 +03:00
|
|
|
enableDataNotification: function() {
|
|
|
|
log("enableDataNotification");
|
|
|
|
if (this._enableDataNotification) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this._callback) {
|
|
|
|
throw NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._enableDataNotification = true;
|
|
|
|
|
2016-09-18 02:42:00 +03:00
|
|
|
this._messageQueue.forEach(aData => this._doNotifyData(aData));
|
2016-04-11 06:20:55 +03:00
|
|
|
this._messageQueue = [];
|
|
|
|
},
|
|
|
|
|
|
|
|
close: function(aReason) {
|
|
|
|
this._closeReason = aReason;
|
|
|
|
|
|
|
|
this._dataChannel.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
_cleanup: function() {
|
|
|
|
this._dataChannel = null;
|
|
|
|
|
|
|
|
if (this._peerConnection) {
|
|
|
|
this._peerConnection.close();
|
|
|
|
this._peerConnection = null;
|
|
|
|
}
|
|
|
|
this._callback = null;
|
|
|
|
this._messageQueue = [];
|
2016-09-18 02:42:00 +03:00
|
|
|
this._window = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
_doNotifyData: function(aData) {
|
|
|
|
if (!this._callback) {
|
|
|
|
throw NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aData instanceof this._window.Blob) {
|
|
|
|
let reader = new this._window.FileReader();
|
|
|
|
reader.addEventListener("load", (aEvent) => {
|
|
|
|
this._callback.notifyData(aEvent.target.result, true);
|
|
|
|
});
|
|
|
|
reader.readAsBinaryString(aData);
|
|
|
|
} else {
|
|
|
|
this._callback.notifyData(aData, false);
|
|
|
|
}
|
2016-04-11 06:20:55 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([PresentationTransportBuilder,
|
|
|
|
PresentationTransport]);
|