window-setup.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // This file should have no requires since it is used by the isolated context
  2. // preload bundle. Instead arguments should be passed in for everything it
  3. // needs.
  4. // This file implements the following APIs:
  5. // - window.alert()
  6. // - window.confirm()
  7. // - window.history.back()
  8. // - window.history.forward()
  9. // - window.history.go()
  10. // - window.history.length
  11. // - window.open()
  12. // - window.opener.blur()
  13. // - window.opener.close()
  14. // - window.opener.eval()
  15. // - window.opener.focus()
  16. // - window.opener.location
  17. // - window.opener.print()
  18. // - window.opener.postMessage()
  19. // - window.prompt()
  20. // - document.hidden
  21. // - document.visibilityState
  22. 'use strict'
  23. const {defineProperty} = Object
  24. // Helper function to resolve relative url.
  25. const a = window.top.document.createElement('a')
  26. const resolveURL = function (url) {
  27. a.href = url
  28. return a.href
  29. }
  30. // Use this method to ensure values expected as strings in the main process
  31. // are convertible to strings in the renderer process. This ensures exceptions
  32. // converting values to strings are thrown in this process.
  33. const toString = (value) => {
  34. return value != null ? `${value}` : value
  35. }
  36. const windowProxies = {}
  37. const getOrCreateProxy = (ipcRenderer, guestId) => {
  38. let proxy = windowProxies[guestId]
  39. if (proxy == null) {
  40. proxy = new BrowserWindowProxy(ipcRenderer, guestId)
  41. windowProxies[guestId] = proxy
  42. }
  43. return proxy
  44. }
  45. const removeProxy = (guestId) => {
  46. delete windowProxies[guestId]
  47. }
  48. function BrowserWindowProxy (ipcRenderer, guestId) {
  49. this.closed = false
  50. defineProperty(this, 'location', {
  51. get: function () {
  52. return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'getURL')
  53. },
  54. set: function (url) {
  55. url = resolveURL(url)
  56. return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'loadURL', url)
  57. }
  58. })
  59. ipcRenderer.once(`ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_${guestId}`, () => {
  60. removeProxy(guestId)
  61. this.closed = true
  62. })
  63. this.close = () => {
  64. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', guestId)
  65. }
  66. this.focus = () => {
  67. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'focus')
  68. }
  69. this.blur = () => {
  70. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'blur')
  71. }
  72. this.print = () => {
  73. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'print')
  74. }
  75. this.postMessage = (message, targetOrigin) => {
  76. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', guestId, message, toString(targetOrigin), window.location.origin)
  77. }
  78. this.eval = (...args) => {
  79. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'executeJavaScript', ...args)
  80. }
  81. }
  82. // Forward history operations to browser.
  83. const sendHistoryOperation = function (ipcRenderer, ...args) {
  84. ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER', ...args)
  85. }
  86. const getHistoryOperation = function (ipcRenderer, ...args) {
  87. return ipcRenderer.sendSync('ELECTRON_SYNC_NAVIGATION_CONTROLLER', ...args)
  88. }
  89. module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => {
  90. if (guestInstanceId == null) {
  91. // Override default window.close.
  92. window.close = function () {
  93. ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CLOSE')
  94. }
  95. }
  96. // Make the browser window or guest view emit "new-window" event.
  97. window.open = function (url, frameName, features) {
  98. if (url != null && url !== '') {
  99. url = resolveURL(url)
  100. }
  101. const guestId = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, toString(frameName), toString(features))
  102. if (guestId != null) {
  103. return getOrCreateProxy(ipcRenderer, guestId)
  104. } else {
  105. return null
  106. }
  107. }
  108. window.alert = function (message, title) {
  109. ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_ALERT', toString(message), toString(title))
  110. }
  111. window.confirm = function (message, title) {
  112. return ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CONFIRM', toString(message), toString(title))
  113. }
  114. // But we do not support prompt().
  115. window.prompt = function () {
  116. throw new Error('prompt() is and will not be supported.')
  117. }
  118. if (openerId != null) {
  119. window.opener = getOrCreateProxy(ipcRenderer, openerId)
  120. }
  121. ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, message, sourceOrigin) {
  122. // Manually dispatch event instead of using postMessage because we also need to
  123. // set event.source.
  124. event = document.createEvent('Event')
  125. event.initEvent('message', false, false)
  126. event.data = message
  127. event.origin = sourceOrigin
  128. event.source = getOrCreateProxy(ipcRenderer, sourceId)
  129. window.dispatchEvent(event)
  130. })
  131. window.history.back = function () {
  132. sendHistoryOperation(ipcRenderer, 'goBack')
  133. }
  134. window.history.forward = function () {
  135. sendHistoryOperation(ipcRenderer, 'goForward')
  136. }
  137. window.history.go = function (offset) {
  138. sendHistoryOperation(ipcRenderer, 'goToOffset', +offset)
  139. }
  140. defineProperty(window.history, 'length', {
  141. get: function () {
  142. return getHistoryOperation(ipcRenderer, 'length')
  143. }
  144. })
  145. // The initial visibilityState.
  146. let cachedVisibilityState = hiddenPage ? 'hidden' : 'visible'
  147. // Subscribe to visibilityState changes.
  148. ipcRenderer.on('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', function (event, visibilityState) {
  149. if (cachedVisibilityState !== visibilityState) {
  150. cachedVisibilityState = visibilityState
  151. document.dispatchEvent(new Event('visibilitychange'))
  152. }
  153. })
  154. // Make document.hidden and document.visibilityState return the correct value.
  155. defineProperty(document, 'hidden', {
  156. get: function () {
  157. return cachedVisibilityState !== 'visible'
  158. }
  159. })
  160. defineProperty(document, 'visibilityState', {
  161. get: function () {
  162. return cachedVisibilityState
  163. }
  164. })
  165. }