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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import Postis from 'postis';
  2. /**
  3. * The list of methods of incomming postis messages that we have to support for
  4. * backward compatability for the users that are directly sending messages to
  5. * Jitsi Meet (without using external_api.js)
  6. *
  7. * @type {string[]}
  8. */
  9. const legacyIncomingMethods = [ 'display-name', 'toggle-audio', 'toggle-video',
  10. 'toggle-film-strip', 'toggle-chat', 'toggle-contact-list',
  11. 'toggle-share-screen', 'video-hangup', 'email', 'avatar-url' ];
  12. /**
  13. * The list of methods of outgoing postis messages that we have to support for
  14. * backward compatability for the users that are directly listening to the
  15. * postis messages send by Jitsi Meet(without using external_api.js).
  16. *
  17. * @type {string[]}
  18. */
  19. const legacyOutgoingMethods = [ 'display-name-change', 'incoming-message',
  20. 'outgoing-message', 'participant-joined', 'participant-left',
  21. 'video-ready-to-close', 'video-conference-joined',
  22. 'video-conference-left' ];
  23. /**
  24. * The postis method used for all messages.
  25. *
  26. * @type {string}
  27. */
  28. const POSTIS_METHOD_NAME = 'data';
  29. /**
  30. * The default options for postis.
  31. *
  32. * @type {Object}
  33. */
  34. const defaultPostisOptions = {
  35. window: window.opener || window.parent
  36. };
  37. /**
  38. * Implements message transport using the postMessage API.
  39. */
  40. export default class PostMessageTransportBackend {
  41. /**
  42. * Creates new PostMessageTransportBackend instance.
  43. *
  44. * @param {Object} options - Optional parameters for configuration of the
  45. * transport.
  46. */
  47. constructor(options = {}) {
  48. const postisOptions = Object.assign({}, defaultPostisOptions, options);
  49. this.postis = Postis(postisOptions);
  50. // backward compatability
  51. legacyIncomingMethods.forEach(method =>
  52. this.postis.listen(method,
  53. params => this._onPostisDataReceived(method, params)));
  54. this.postis.listen(POSTIS_METHOD_NAME, data =>
  55. this._dataReceivedCallBack(data));
  56. this._dataReceivedCallBack = () => {
  57. // do nothing until real callback is set;
  58. };
  59. }
  60. /**
  61. * Handles incomming legacy postis data.
  62. *
  63. * @param {string} method - The method property from postis data object.
  64. * @param {Any} params - The params property from postis data object.
  65. * @returns {void}
  66. */
  67. _onPostisDataReceived(method, params = {}) {
  68. const newData = {
  69. data: {
  70. name: method,
  71. data: params
  72. }
  73. };
  74. this._dataReceivedCallBack(newData);
  75. }
  76. /**
  77. * Sends the passed data via postis using the old format.
  78. *
  79. * @param {Object} data - The data to be sent.
  80. * @returns {void}
  81. */
  82. _sendLegacyData(data) {
  83. const method = data.name;
  84. if (method && legacyOutgoingMethods.indexOf(method) !== -1) {
  85. this.postis.send({
  86. method,
  87. params: data.data
  88. });
  89. }
  90. }
  91. /**
  92. * Disposes the allocated resources.
  93. *
  94. * @returns {void}
  95. */
  96. dispose() {
  97. this.postis.destroy();
  98. }
  99. /**
  100. * Sends the passed data.
  101. *
  102. * @param {Object} data - The data to be sent.
  103. * @returns {void}
  104. */
  105. send(data) {
  106. this.postis.send({
  107. method: POSTIS_METHOD_NAME,
  108. params: data
  109. });
  110. // For the legacy use case we don't need any new fields defined in
  111. // Transport class. That's why we are passing only the original object
  112. // passed by the consumer of the Transport class which is data.data.
  113. this._sendLegacyData(data.data);
  114. }
  115. /**
  116. * Sets the callback for receiving data.
  117. *
  118. * @param {Function} callback - The new callback.
  119. * @returns {void}
  120. */
  121. setDataReceivedCallback(callback) {
  122. this._dataReceivedCallBack = callback;
  123. }
  124. }