ipc.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2019-2024 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. /**
  5. * @typedef {{callback: string, error: string, data: *}} IsolationPayload - a valid isolation payload
  6. */
  7. ;(function () {
  8. /**
  9. * @type {string}
  10. */
  11. const pattern = window.__TAURI_INTERNALS__.__TAURI_PATTERN__.pattern
  12. /**
  13. * @type {string}
  14. */
  15. const isolationOrigin = __TEMPLATE_isolation_origin__
  16. /**
  17. * @type {{queue: object[], ready: boolean, frame: HTMLElement | null}}
  18. */
  19. const isolation = Object.create(null)
  20. isolation.queue = []
  21. isolation.ready = false
  22. isolation.frame = null
  23. /**
  24. * Detects if a message event is a valid isolation message.
  25. *
  26. * @param {MessageEvent<object>} event - a message event that is expected to be an isolation message
  27. * @return {boolean} - if the event was a valid isolation message
  28. */
  29. function isIsolationMessage(event) {
  30. if (
  31. typeof event.data === 'object' &&
  32. typeof event.data.payload === 'object'
  33. ) {
  34. const keys = Object.keys(event.data.payload || {})
  35. return (
  36. keys.length > 0 &&
  37. keys.every(
  38. (key) => key === 'contentType' || key === 'nonce' || key === 'payload'
  39. )
  40. )
  41. }
  42. return false
  43. }
  44. /**
  45. * Detects if data is able to transform into an isolation payload.
  46. *
  47. * @param {object} data - object that is expected to contain at least a callback and error identifier
  48. * @return {boolean} - if the data is able to transform into an isolation payload
  49. */
  50. function isIsolationPayload(data) {
  51. return (
  52. typeof data === 'object' &&
  53. 'callback' in data &&
  54. 'error' in data &&
  55. !isIsolationMessage(data)
  56. )
  57. }
  58. /**
  59. * Sends a properly formatted message to the isolation frame.
  60. *
  61. * @param {IsolationPayload} data - data that has been validated to be an isolation payload
  62. */
  63. function sendIsolationMessage(data) {
  64. // set the frame dom element if it's not been set before
  65. if (!isolation.frame) {
  66. const frame = document.querySelector('iframe#__tauri_isolation__')
  67. if (frame.src.startsWith(isolationOrigin)) {
  68. isolation.frame = frame
  69. } else {
  70. console.error(
  71. 'Tauri IPC found an isolation iframe, but it had the wrong origin'
  72. )
  73. }
  74. }
  75. // ensure we have the target to send the message to
  76. if (!isolation.frame || !isolation.frame.contentWindow) {
  77. console.error(
  78. 'Tauri "Isolation" Pattern could not find the Isolation iframe window'
  79. )
  80. return
  81. }
  82. isolation.frame.contentWindow.postMessage(
  83. data,
  84. '*' /* todo: set this to the secure origin */
  85. )
  86. }
  87. Object.defineProperty(window.__TAURI_INTERNALS__, 'ipc', {
  88. // todo: JSDoc this function
  89. value: Object.freeze((message) => {
  90. switch (pattern) {
  91. case 'brownfield':
  92. window.__TAURI_INTERNALS__.postMessage(message)
  93. break
  94. case 'isolation':
  95. if (!isIsolationPayload(message)) {
  96. console.error(
  97. 'Tauri "Isolation" Pattern found an invalid isolation message payload',
  98. message
  99. )
  100. break
  101. }
  102. if (isolation.ready) {
  103. sendIsolationMessage(message)
  104. } else {
  105. isolation.queue.push(message)
  106. }
  107. break
  108. case 'error':
  109. console.error(
  110. 'Tauri IPC found a Tauri Pattern, but it was an error. Check for other log messages to find the cause.'
  111. )
  112. break
  113. default:
  114. console.error(
  115. 'Tauri IPC did not find a Tauri Pattern that it understood.'
  116. )
  117. break
  118. }
  119. })
  120. })
  121. /**
  122. * IMPORTANT: See isolation_secure.js for the isolation frame implementation.
  123. * main frame -> isolation frame = isolation payload
  124. * isolation frame -> main frame = isolation message
  125. */
  126. if (pattern === 'isolation') {
  127. window.addEventListener(
  128. 'message',
  129. (event) => {
  130. // watch for the isolation frame being ready and flush any queued messages
  131. if (event.data === '__TAURI_ISOLATION_READY__') {
  132. isolation.ready = true
  133. for (const message of isolation.queue) {
  134. sendIsolationMessage(message)
  135. }
  136. isolation.queue = []
  137. return
  138. }
  139. if (isIsolationMessage(event)) {
  140. window.__TAURI_INTERNALS__.postMessage(event.data)
  141. }
  142. },
  143. false
  144. )
  145. }
  146. })()