You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PostMessageTransportBackend.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import Postis from 'postis';
  2. /**
  3. * The default options for postis.
  4. *
  5. * @type {Object}
  6. */
  7. const DEFAULT_POSTIS_OPTIONS = {
  8. window: window.opener || window.parent
  9. };
  10. /**
  11. * The list of methods of incoming postis messages that we have to support for
  12. * backward compatibility for the users that are directly sending messages to
  13. * Jitsi Meet (without using external_api.js)
  14. *
  15. * @type {string[]}
  16. */
  17. const LEGACY_INCOMING_METHODS = [
  18. 'avatar-url',
  19. 'display-name',
  20. 'email',
  21. 'toggle-audio',
  22. 'toggle-chat',
  23. 'toggle-film-strip',
  24. 'toggle-share-screen',
  25. 'toggle-video',
  26. 'video-hangup'
  27. ];
  28. /**
  29. * The list of methods of outgoing postis messages that we have to support for
  30. * backward compatibility for the users that are directly listening to the
  31. * postis messages send by Jitsi Meet(without using external_api.js).
  32. *
  33. * @type {string[]}
  34. */
  35. const LEGACY_OUTGOING_METHODS = [
  36. 'display-name-change',
  37. 'incoming-message',
  38. 'outgoing-message',
  39. 'participant-joined',
  40. 'participant-left',
  41. 'video-conference-joined',
  42. 'video-conference-left',
  43. 'video-ready-to-close'
  44. ];
  45. /**
  46. * The postis method used for all messages.
  47. *
  48. * @type {string}
  49. */
  50. const POSTIS_METHOD_NAME = 'message';
  51. /**
  52. * Implements message transport using the postMessage API.
  53. */
  54. export default class PostMessageTransportBackend {
  55. /**
  56. * Creates new PostMessageTransportBackend instance.
  57. *
  58. * @param {Object} options - Optional parameters for configuration of the
  59. * transport.
  60. */
  61. constructor({ enableLegacyFormat, postisOptions } = {}) {
  62. // eslint-disable-next-line new-cap
  63. this.postis = Postis({
  64. ...DEFAULT_POSTIS_OPTIONS,
  65. ...postisOptions
  66. });
  67. /**
  68. * If true PostMessageTransportBackend will process and send messages
  69. * using the legacy format and in the same time the current format.
  70. * Otherwise all messages (outgoing and incoming) that are using the
  71. * legacy format will be ignored.
  72. *
  73. * @type {boolean}
  74. */
  75. this._enableLegacyFormat = enableLegacyFormat;
  76. if (this._enableLegacyFormat) {
  77. // backward compatibility
  78. LEGACY_INCOMING_METHODS.forEach(method =>
  79. this.postis.listen(
  80. method,
  81. params =>
  82. this._legacyMessageReceivedCallback(method, params)
  83. )
  84. );
  85. }
  86. this._receiveCallback = () => {
  87. // Do nothing until a callback is set by the consumer of
  88. // PostMessageTransportBackend via setReceiveCallback.
  89. };
  90. this.postis.listen(
  91. POSTIS_METHOD_NAME,
  92. message => this._receiveCallback(message));
  93. }
  94. /**
  95. * Handles incoming legacy postis messages.
  96. *
  97. * @param {string} method - The method property from the postis message.
  98. * @param {Any} params - The params property from the postis message.
  99. * @returns {void}
  100. */
  101. _legacyMessageReceivedCallback(method, params = {}) {
  102. this._receiveCallback({
  103. data: {
  104. name: method,
  105. data: params
  106. }
  107. });
  108. }
  109. /**
  110. * Sends the passed message via postis using the old format.
  111. *
  112. * @param {Object} legacyMessage - The message to be sent.
  113. * @returns {void}
  114. */
  115. _sendLegacyMessage({ name, ...data }) {
  116. if (name && LEGACY_OUTGOING_METHODS.indexOf(name) !== -1) {
  117. this.postis.send({
  118. method: name,
  119. params: data
  120. });
  121. }
  122. }
  123. /**
  124. * Disposes the allocated resources.
  125. *
  126. * @returns {void}
  127. */
  128. dispose() {
  129. this.postis.destroy();
  130. }
  131. /**
  132. * Sends the passed message.
  133. *
  134. * @param {Object} message - The message to be sent.
  135. * @returns {void}
  136. */
  137. send(message) {
  138. this.postis.send({
  139. method: POSTIS_METHOD_NAME,
  140. params: message
  141. });
  142. if (this._enableLegacyFormat) {
  143. // For the legacy use case we don't need any new fields defined in
  144. // Transport class. That's why we are passing only the original
  145. // object passed by the consumer of the Transport class which is
  146. // message.data.
  147. this._sendLegacyMessage(message.data || {});
  148. }
  149. }
  150. /**
  151. * Sets the callback for receiving data.
  152. *
  153. * @param {Function} callback - The new callback.
  154. * @returns {void}
  155. */
  156. setReceiveCallback(callback) {
  157. this._receiveCallback = callback;
  158. }
  159. }