window-setup.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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, usesNativeWindowOpen) => {
  90. if (guestInstanceId == null) {
  91. // Override default window.close.
  92. window.close = function () {
  93. ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CLOSE')
  94. }
  95. }
  96. if (!usesNativeWindowOpen) {
  97. // Make the browser window or guest view emit "new-window" event.
  98. window.open = function (url, frameName, features) {
  99. if (url != null && url !== '') {
  100. url = resolveURL(url)
  101. }
  102. const guestId = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, toString(frameName), toString(features))
  103. if (guestId != null) {
  104. return getOrCreateProxy(ipcRenderer, guestId)
  105. } else {
  106. return null
  107. }
  108. }
  109. if (openerId != null) {
  110. window.opener = getOrCreateProxy(ipcRenderer, openerId)
  111. }
  112. }
  113. // But we do not support prompt().
  114. window.prompt = function () {
  115. throw new Error('prompt() is and will not be supported.')
  116. }
  117. ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, message, sourceOrigin) {
  118. // Manually dispatch event instead of using postMessage because we also need to
  119. // set event.source.
  120. event = document.createEvent('Event')
  121. event.initEvent('message', false, false)
  122. event.data = message
  123. event.origin = sourceOrigin
  124. event.source = getOrCreateProxy(ipcRenderer, sourceId)
  125. window.dispatchEvent(event)
  126. })
  127. window.history.back = function () {
  128. sendHistoryOperation(ipcRenderer, 'goBack')
  129. }
  130. window.history.forward = function () {
  131. sendHistoryOperation(ipcRenderer, 'goForward')
  132. }
  133. window.history.go = function (offset) {
  134. sendHistoryOperation(ipcRenderer, 'goToOffset', +offset)
  135. }
  136. defineProperty(window.history, 'length', {
  137. get: function () {
  138. return getHistoryOperation(ipcRenderer, 'length')
  139. }
  140. })
  141. if (guestInstanceId != null) {
  142. // Webview `document.visibilityState` tracks window visibility (and ignores
  143. // the actual <webview> element visibility) for backwards compatibility.
  144. // See discussion in #9178.
  145. //
  146. // Note that this results in duplicate visibilitychange events (since
  147. // Chromium also fires them) and potentially incorrect visibility change.
  148. // We should reconsider this decision for Electron 2.0.
  149. let cachedVisibilityState = hiddenPage ? 'hidden' : 'visible'
  150. // Subscribe to visibilityState changes.
  151. ipcRenderer.on('ELECTRON_GUEST_INSTANCE_VISIBILITY_CHANGE', function (event, visibilityState) {
  152. if (cachedVisibilityState !== visibilityState) {
  153. cachedVisibilityState = visibilityState
  154. document.dispatchEvent(new Event('visibilitychange'))
  155. }
  156. })
  157. // Make document.hidden and document.visibilityState return the correct value.
  158. defineProperty(document, 'hidden', {
  159. get: function () {
  160. return cachedVisibilityState !== 'visible'
  161. }
  162. })
  163. defineProperty(document, 'visibilityState', {
  164. get: function () {
  165. return cachedVisibilityState
  166. }
  167. })
  168. }
  169. }