ipc.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. // `postMessage` uses `structuredClone` to serialize the data before sending it
  83. // unlike `JSON.stringify`, we can't extend a type with a method similar to `toJSON`
  84. // so that `structuredClone` would use, so until https://github.com/whatwg/html/issues/7428
  85. // we manually call `toIPC`
  86. function recursiveCallToIPC(data) {
  87. if (
  88. typeof data === 'object' &&
  89. 'constructor' in data &&
  90. data.constructor === Array
  91. ) {
  92. return data.map((v) => recursiveCallToIPC(v))
  93. }
  94. if (typeof data === 'object' && 'toIPC' in data) {
  95. return data.toIPC()
  96. }
  97. if (
  98. typeof data === 'object' &&
  99. 'constructor' in data &&
  100. data.constructor === Object
  101. ) {
  102. const acc = {}
  103. Object.entries(data).forEach(([k, v]) => {
  104. acc[k] = recursiveCallToIPC(v)
  105. })
  106. return acc
  107. }
  108. return data
  109. }
  110. data.payload = recursiveCallToIPC(data.payload)
  111. isolation.frame.contentWindow.postMessage(
  112. data,
  113. '*' /* todo: set this to the secure origin */
  114. )
  115. }
  116. Object.defineProperty(window.__TAURI_INTERNALS__, 'ipc', {
  117. // todo: JSDoc this function
  118. value: Object.freeze((message) => {
  119. switch (pattern) {
  120. case 'brownfield':
  121. window.__TAURI_INTERNALS__.postMessage(message)
  122. break
  123. case 'isolation':
  124. if (!isIsolationPayload(message)) {
  125. console.error(
  126. 'Tauri "Isolation" Pattern found an invalid isolation message payload',
  127. message
  128. )
  129. break
  130. }
  131. if (isolation.ready) {
  132. sendIsolationMessage(message)
  133. } else {
  134. isolation.queue.push(message)
  135. }
  136. break
  137. case 'error':
  138. console.error(
  139. 'Tauri IPC found a Tauri Pattern, but it was an error. Check for other log messages to find the cause.'
  140. )
  141. break
  142. default:
  143. console.error(
  144. 'Tauri IPC did not find a Tauri Pattern that it understood.'
  145. )
  146. break
  147. }
  148. })
  149. })
  150. /**
  151. * IMPORTANT: See isolation_secure.js for the isolation frame implementation.
  152. * main frame -> isolation frame = isolation payload
  153. * isolation frame -> main frame = isolation message
  154. */
  155. if (pattern === 'isolation') {
  156. window.addEventListener(
  157. 'message',
  158. (event) => {
  159. // watch for the isolation frame being ready and flush any queued messages
  160. if (event.data === '__TAURI_ISOLATION_READY__') {
  161. isolation.ready = true
  162. for (const message of isolation.queue) {
  163. sendIsolationMessage(message)
  164. }
  165. isolation.queue = []
  166. return
  167. }
  168. if (isIsolationMessage(event)) {
  169. window.__TAURI_INTERNALS__.postMessage(event.data)
  170. }
  171. },
  172. false
  173. )
  174. }
  175. })()