2017-04-17 15:52:31 +00:00
|
|
|
import Postis from 'postis';
|
|
|
|
|
2017-04-27 20:21:01 +00:00
|
|
|
/**
|
|
|
|
* The default options for postis.
|
|
|
|
*
|
|
|
|
* @type {Object}
|
|
|
|
*/
|
|
|
|
const DEFAULT_POSTIS_OPTIONS = {
|
|
|
|
window: window.opener || window.parent
|
|
|
|
};
|
|
|
|
|
2017-04-17 15:52:31 +00:00
|
|
|
/**
|
2017-04-28 20:24:20 +00:00
|
|
|
* The list of methods of incoming postis messages that we have to support for
|
|
|
|
* backward compatibility for the users that are directly sending messages to
|
2017-04-17 15:52:31 +00:00
|
|
|
* Jitsi Meet (without using external_api.js)
|
|
|
|
*
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
2017-04-27 20:21:01 +00:00
|
|
|
const LEGACY_INCOMING_METHODS = [
|
|
|
|
'avatar-url',
|
|
|
|
'display-name',
|
|
|
|
'email',
|
|
|
|
'toggle-audio',
|
|
|
|
'toggle-chat',
|
|
|
|
'toggle-contact-list',
|
|
|
|
'toggle-film-strip',
|
|
|
|
'toggle-share-screen',
|
|
|
|
'toggle-video',
|
|
|
|
'video-hangup'
|
|
|
|
];
|
2017-04-17 15:52:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The list of methods of outgoing postis messages that we have to support for
|
2017-04-28 20:24:20 +00:00
|
|
|
* backward compatibility for the users that are directly listening to the
|
2017-04-17 15:52:31 +00:00
|
|
|
* postis messages send by Jitsi Meet(without using external_api.js).
|
|
|
|
*
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
2017-04-27 20:21:01 +00:00
|
|
|
const LEGACY_OUTGOING_METHODS = [
|
|
|
|
'display-name-change',
|
|
|
|
'incoming-message',
|
|
|
|
'outgoing-message',
|
|
|
|
'participant-joined',
|
|
|
|
'participant-left',
|
|
|
|
'video-conference-joined',
|
|
|
|
'video-conference-left',
|
|
|
|
'video-ready-to-close'
|
|
|
|
];
|
2017-04-17 15:52:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The postis method used for all messages.
|
|
|
|
*
|
|
|
|
* @type {string}
|
|
|
|
*/
|
2017-04-28 21:03:36 +00:00
|
|
|
const POSTIS_METHOD_NAME = 'message';
|
2017-04-17 15:52:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements message transport using the postMessage API.
|
|
|
|
*/
|
|
|
|
export default class PostMessageTransportBackend {
|
|
|
|
/**
|
|
|
|
* Creates new PostMessageTransportBackend instance.
|
|
|
|
*
|
|
|
|
* @param {Object} options - Optional parameters for configuration of the
|
|
|
|
* transport.
|
|
|
|
*/
|
2017-04-27 20:21:01 +00:00
|
|
|
constructor({ enableLegacyFormat, postisOptions } = {}) {
|
|
|
|
this.postis = Postis({
|
|
|
|
...DEFAULT_POSTIS_OPTIONS,
|
|
|
|
...postisOptions
|
|
|
|
});
|
2017-04-17 15:52:31 +00:00
|
|
|
|
2017-04-28 20:24:20 +00:00
|
|
|
/**
|
2017-04-28 21:03:36 +00:00
|
|
|
* If true PostMessageTransportBackend will process and send messages
|
|
|
|
* using the legacy format and in the same time the current format.
|
|
|
|
* Otherwise all messages (outgoing and incoming) that are using the
|
|
|
|
* legacy format will be ignored.
|
2017-04-28 20:24:20 +00:00
|
|
|
*
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
2017-04-27 20:21:01 +00:00
|
|
|
this._enableLegacyFormat = enableLegacyFormat;
|
2017-04-18 03:17:23 +00:00
|
|
|
|
2017-04-28 20:24:20 +00:00
|
|
|
if (this._enableLegacyFormat) {
|
|
|
|
// backward compatibility
|
2017-04-27 20:21:01 +00:00
|
|
|
LEGACY_INCOMING_METHODS.forEach(method =>
|
|
|
|
this.postis.listen(
|
|
|
|
method,
|
2017-04-28 21:03:36 +00:00
|
|
|
params =>
|
|
|
|
this._legacyMessageReceivedCallback(method, params)
|
|
|
|
)
|
|
|
|
);
|
2017-04-18 03:17:23 +00:00
|
|
|
}
|
2017-04-17 15:52:31 +00:00
|
|
|
|
2017-04-27 20:21:01 +00:00
|
|
|
this._receiveCallback = () => {
|
|
|
|
// Do nothing until a callback is set by the consumer of
|
|
|
|
// PostMessageTransportBackend via setReceiveCallback.
|
2017-04-17 15:52:31 +00:00
|
|
|
};
|
2017-04-27 20:21:01 +00:00
|
|
|
|
|
|
|
this.postis.listen(
|
|
|
|
POSTIS_METHOD_NAME,
|
2017-04-28 21:03:36 +00:00
|
|
|
message => this._receiveCallback(message));
|
2017-04-17 15:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-28 21:03:36 +00:00
|
|
|
* Handles incoming legacy postis messages.
|
2017-04-17 15:52:31 +00:00
|
|
|
*
|
2017-04-28 21:03:36 +00:00
|
|
|
* @param {string} method - The method property from the postis message.
|
|
|
|
* @param {Any} params - The params property from the postis message.
|
2017-04-17 15:52:31 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-04-28 21:03:36 +00:00
|
|
|
_legacyMessageReceivedCallback(method, params = {}) {
|
2017-04-27 20:21:01 +00:00
|
|
|
this._receiveCallback({
|
2017-04-17 15:52:31 +00:00
|
|
|
data: {
|
|
|
|
name: method,
|
|
|
|
data: params
|
|
|
|
}
|
2017-04-27 20:21:01 +00:00
|
|
|
});
|
2017-04-17 15:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-28 21:03:36 +00:00
|
|
|
* Sends the passed message via postis using the old format.
|
2017-04-17 15:52:31 +00:00
|
|
|
*
|
2017-04-28 21:03:36 +00:00
|
|
|
* @param {Object} legacyMessage - The message to be sent.
|
2017-04-17 15:52:31 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-05-01 20:59:18 +00:00
|
|
|
_sendLegacyMessage({ name, ...data }) {
|
2017-04-27 20:21:01 +00:00
|
|
|
if (name && LEGACY_OUTGOING_METHODS.indexOf(name) !== -1) {
|
2017-04-17 15:52:31 +00:00
|
|
|
this.postis.send({
|
2017-04-27 20:21:01 +00:00
|
|
|
method: name,
|
|
|
|
params: data
|
2017-04-17 15:52:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disposes the allocated resources.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
dispose() {
|
|
|
|
this.postis.destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-28 21:03:36 +00:00
|
|
|
* Sends the passed message.
|
2017-04-17 15:52:31 +00:00
|
|
|
*
|
2017-04-28 21:03:36 +00:00
|
|
|
* @param {Object} message - The message to be sent.
|
2017-04-17 15:52:31 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-04-28 21:03:36 +00:00
|
|
|
send(message) {
|
2017-04-17 15:52:31 +00:00
|
|
|
this.postis.send({
|
|
|
|
method: POSTIS_METHOD_NAME,
|
2017-04-28 21:03:36 +00:00
|
|
|
params: message
|
2017-04-17 15:52:31 +00:00
|
|
|
});
|
|
|
|
|
2017-04-28 20:24:20 +00:00
|
|
|
if (this._enableLegacyFormat) {
|
2017-04-18 03:17:23 +00:00
|
|
|
// For the legacy use case we don't need any new fields defined in
|
|
|
|
// Transport class. That's why we are passing only the original
|
|
|
|
// object passed by the consumer of the Transport class which is
|
2017-04-28 21:03:36 +00:00
|
|
|
// message.data.
|
2017-08-04 08:04:46 +00:00
|
|
|
this._sendLegacyMessage(message.data || {});
|
2017-04-18 03:17:23 +00:00
|
|
|
}
|
2017-04-17 15:52:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the callback for receiving data.
|
|
|
|
*
|
|
|
|
* @param {Function} callback - The new callback.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2017-04-27 20:21:01 +00:00
|
|
|
setReceiveCallback(callback) {
|
|
|
|
this._receiveCallback = callback;
|
2017-04-17 15:52:31 +00:00
|
|
|
}
|
|
|
|
}
|