2016-07-04 13:12:04 +03:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
/* jshint esnext:true, globalstrict:true, moz:true, undef:true, unused:true */
|
|
|
|
/* globals Components, dump */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2018-02-23 22:50:01 +03:00
|
|
|
var EXPORTED_SYMBOLS = ["ReceiverStateMachine"]; // jshint ignore:line
|
2016-07-04 13:12:04 +03:00
|
|
|
|
|
|
|
/* globals State, CommandType */
|
2019-01-17 21:18:31 +03:00
|
|
|
const {CommandType, State} = ChromeUtils.import("resource://gre/modules/presentation/StateMachineHelper.jsm");
|
2016-07-04 13:12:04 +03:00
|
|
|
|
|
|
|
const DEBUG = false;
|
|
|
|
function debug(str) {
|
|
|
|
dump("-*- ReceiverStateMachine: " + str + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
var handlers = [
|
|
|
|
function _initHandler(stateMachine, command) {
|
|
|
|
// shouldn't receive any command at init state
|
|
|
|
DEBUG && debug("unexpected command: " + JSON.stringify(command)); // jshint ignore:line
|
|
|
|
},
|
|
|
|
function _connectingHandler(stateMachine, command) {
|
|
|
|
switch (command.type) {
|
|
|
|
case CommandType.CONNECT:
|
|
|
|
stateMachine._sendCommand({
|
2018-10-19 15:55:39 +03:00
|
|
|
type: CommandType.CONNECT_ACK,
|
2016-07-04 13:12:04 +03:00
|
|
|
});
|
|
|
|
stateMachine.state = State.CONNECTED;
|
|
|
|
stateMachine._notifyDeviceConnected(command.deviceId);
|
|
|
|
break;
|
|
|
|
case CommandType.DISCONNECT:
|
|
|
|
stateMachine.state = State.CLOSED;
|
2016-07-12 05:16:46 +03:00
|
|
|
stateMachine._notifyDisconnected(command.reason);
|
2016-07-04 13:12:04 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug("unexpected command: " + JSON.stringify(command));
|
|
|
|
// ignore unexpected command
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function _connectedHandler(stateMachine, command) {
|
|
|
|
switch (command.type) {
|
|
|
|
case CommandType.DISCONNECT:
|
|
|
|
stateMachine.state = State.CLOSED;
|
2016-07-12 05:16:46 +03:00
|
|
|
stateMachine._notifyDisconnected(command.reason);
|
2016-07-04 13:12:04 +03:00
|
|
|
break;
|
|
|
|
case CommandType.LAUNCH:
|
|
|
|
stateMachine._notifyLaunch(command.presentationId,
|
|
|
|
command.url);
|
|
|
|
stateMachine._sendCommand({
|
|
|
|
type: CommandType.LAUNCH_ACK,
|
2018-10-19 15:55:39 +03:00
|
|
|
presentationId: command.presentationId,
|
2016-07-04 13:12:04 +03:00
|
|
|
});
|
|
|
|
break;
|
2016-05-27 02:22:27 +03:00
|
|
|
case CommandType.TERMINATE:
|
|
|
|
stateMachine._notifyTerminate(command.presentationId);
|
|
|
|
break;
|
|
|
|
case CommandType.TERMINATE_ACK:
|
|
|
|
stateMachine._notifyTerminate(command.presentationId);
|
|
|
|
break;
|
2016-07-04 13:12:04 +03:00
|
|
|
case CommandType.OFFER:
|
|
|
|
case CommandType.ICE_CANDIDATE:
|
|
|
|
stateMachine._notifyChannelDescriptor(command);
|
|
|
|
break;
|
2016-08-02 20:10:00 +03:00
|
|
|
case CommandType.RECONNECT:
|
|
|
|
stateMachine._notifyReconnect(command.presentationId,
|
|
|
|
command.url);
|
|
|
|
stateMachine._sendCommand({
|
|
|
|
type: CommandType.RECONNECT_ACK,
|
2018-10-19 15:55:39 +03:00
|
|
|
presentationId: command.presentationId,
|
2016-08-02 20:10:00 +03:00
|
|
|
});
|
|
|
|
break;
|
2016-07-04 13:12:04 +03:00
|
|
|
default:
|
|
|
|
debug("unexpected command: " + JSON.stringify(command));
|
|
|
|
// ignore unexpected command
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function _closingHandler(stateMachine, command) {
|
2016-07-26 05:59:52 +03:00
|
|
|
switch (command.type) {
|
|
|
|
case CommandType.DISCONNECT:
|
|
|
|
stateMachine.state = State.CLOSED;
|
|
|
|
stateMachine._notifyDisconnected(command.reason);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
debug("unexpected command: " + JSON.stringify(command));
|
|
|
|
// ignore unexpected command
|
|
|
|
break;
|
|
|
|
}
|
2016-07-04 13:12:04 +03:00
|
|
|
},
|
|
|
|
function _closedHandler(stateMachine, command) {
|
|
|
|
// ignore every command in closed state.
|
|
|
|
DEBUG && debug("unexpected command: " + JSON.stringify(command)); // jshint ignore:line
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
function ReceiverStateMachine(channel) {
|
|
|
|
this.state = State.INIT;
|
|
|
|
this._channel = channel;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReceiverStateMachine.prototype = {
|
|
|
|
launch: function _launch() {
|
|
|
|
// presentation session can only be launched by controlling UA.
|
|
|
|
debug("receiver shouldn't trigger launch");
|
|
|
|
},
|
|
|
|
|
2016-05-27 02:22:27 +03:00
|
|
|
terminate: function _terminate(presentationId) {
|
|
|
|
if (this.state === State.CONNECTED) {
|
|
|
|
this._sendCommand({
|
|
|
|
type: CommandType.TERMINATE,
|
2018-07-03 18:19:05 +03:00
|
|
|
presentationId,
|
2016-05-27 02:22:27 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
terminateAck: function _terminateAck(presentationId) {
|
|
|
|
if (this.state === State.CONNECTED) {
|
|
|
|
this._sendCommand({
|
|
|
|
type: CommandType.TERMINATE_ACK,
|
2018-07-03 18:19:05 +03:00
|
|
|
presentationId,
|
2016-05-27 02:22:27 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-08-02 20:10:00 +03:00
|
|
|
reconnect: function _reconnect() {
|
|
|
|
debug("receiver shouldn't trigger reconnect");
|
|
|
|
},
|
|
|
|
|
2016-07-04 13:12:04 +03:00
|
|
|
sendOffer: function _sendOffer() {
|
|
|
|
// offer can only be sent by controlling UA.
|
|
|
|
debug("receiver shouldn't generate offer");
|
|
|
|
},
|
|
|
|
|
|
|
|
sendAnswer: function _sendAnswer(answer) {
|
|
|
|
if (this.state === State.CONNECTED) {
|
|
|
|
this._sendCommand({
|
|
|
|
type: CommandType.ANSWER,
|
2018-07-03 18:19:05 +03:00
|
|
|
answer,
|
2016-07-04 13:12:04 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
updateIceCandidate: function _updateIceCandidate(candidate) {
|
|
|
|
if (this.state === State.CONNECTED) {
|
|
|
|
this._sendCommand({
|
|
|
|
type: CommandType.ICE_CANDIDATE,
|
2018-07-03 18:19:05 +03:00
|
|
|
candidate,
|
2016-07-04 13:12:04 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onCommand: function _onCommand(command) {
|
|
|
|
handlers[this.state](this, command);
|
|
|
|
},
|
|
|
|
|
|
|
|
onChannelReady: function _onChannelReady() {
|
|
|
|
if (this.state === State.INIT) {
|
|
|
|
this.state = State.CONNECTING;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onChannelClosed: function _onChannelClose(reason, isByRemote) {
|
|
|
|
switch (this.state) {
|
|
|
|
case State.CONNECTED:
|
|
|
|
if (isByRemote) {
|
|
|
|
this.state = State.CLOSED;
|
2016-07-12 05:16:46 +03:00
|
|
|
this._notifyDisconnected(reason);
|
2016-07-04 13:12:04 +03:00
|
|
|
} else {
|
|
|
|
this._sendCommand({
|
|
|
|
type: CommandType.DISCONNECT,
|
2018-10-19 15:55:39 +03:00
|
|
|
reason,
|
2016-07-04 13:12:04 +03:00
|
|
|
});
|
|
|
|
this.state = State.CLOSING;
|
|
|
|
this._closeReason = reason;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case State.CLOSING:
|
|
|
|
if (isByRemote) {
|
|
|
|
this.state = State.CLOSED;
|
|
|
|
if (this._closeReason) {
|
|
|
|
reason = this._closeReason;
|
|
|
|
delete this._closeReason;
|
|
|
|
}
|
2016-07-12 05:16:46 +03:00
|
|
|
this._notifyDisconnected(reason);
|
2016-07-04 13:12:04 +03:00
|
|
|
} else {
|
|
|
|
// do nothing and wait for remote channel closed.
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DEBUG && debug("unexpected channel close: " + reason + ", " + isByRemote); // jshint ignore:line
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_sendCommand: function _sendCommand(command) {
|
|
|
|
this._channel.sendCommand(command);
|
|
|
|
},
|
|
|
|
|
|
|
|
_notifyDeviceConnected: function _notifyDeviceConnected(deviceName) {
|
|
|
|
this._channel.notifyDeviceConnected(deviceName);
|
|
|
|
},
|
|
|
|
|
2016-07-12 05:16:46 +03:00
|
|
|
_notifyDisconnected: function _notifyDisconnected(reason) {
|
|
|
|
this._channel.notifyDisconnected(reason);
|
2016-07-04 13:12:04 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_notifyLaunch: function _notifyLaunch(presentationId, url) {
|
|
|
|
this._channel.notifyLaunch(presentationId, url);
|
|
|
|
},
|
|
|
|
|
2016-05-27 02:22:27 +03:00
|
|
|
_notifyTerminate: function _notifyTerminate(presentationId) {
|
|
|
|
this._channel.notifyTerminate(presentationId);
|
|
|
|
},
|
|
|
|
|
2016-08-02 20:10:00 +03:00
|
|
|
_notifyReconnect: function _notifyReconnect(presentationId, url) {
|
|
|
|
this._channel.notifyReconnect(presentationId, url);
|
|
|
|
},
|
|
|
|
|
2016-07-04 13:12:04 +03:00
|
|
|
_notifyChannelDescriptor: function _notifyChannelDescriptor(command) {
|
|
|
|
switch (command.type) {
|
|
|
|
case CommandType.OFFER:
|
|
|
|
this._channel.notifyOffer(command.offer);
|
|
|
|
break;
|
|
|
|
case CommandType.ICE_CANDIDATE:
|
|
|
|
this._channel.notifyIceCandidate(command.candidate);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|