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

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