window-setup.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. 'use strict'
  2. // This file should have no requires since it is used by the isolated context
  3. // preload bundle. Instead arguments should be passed in for everything it
  4. // needs.
  5. // This file implements the following APIs:
  6. // - window.alert()
  7. // - window.confirm()
  8. // - window.history.back()
  9. // - window.history.forward()
  10. // - window.history.go()
  11. // - window.history.length
  12. // - window.open()
  13. // - window.opener.blur()
  14. // - window.opener.close()
  15. // - window.opener.eval()
  16. // - window.opener.focus()
  17. // - window.opener.location
  18. // - window.opener.print()
  19. // - window.opener.postMessage()
  20. // - window.prompt()
  21. // - document.hidden
  22. // - document.visibilityState
  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. module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage, usesNativeWindowOpen) => {
  83. if (guestInstanceId == null) {
  84. // Override default window.close.
  85. window.close = function () {
  86. ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CLOSE')
  87. }
  88. }
  89. if (!usesNativeWindowOpen) {
  90. // Make the browser window or guest view emit "new-window" event.
  91. window.open = function (url, frameName, features) {
  92. if (url != null && url !== '') {
  93. url = resolveURL(url)
  94. }
  95. const guestId = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, toString(frameName), toString(features))
  96. if (guestId != null) {
  97. return getOrCreateProxy(ipcRenderer, guestId)
  98. } else {
  99. return null
  100. }
  101. }
  102. if (openerId != null) {
  103. window.opener = getOrCreateProxy(ipcRenderer, openerId)
  104. }
  105. }
  106. // But we do not support prompt().
  107. window.prompt = function () {
  108. throw new Error('prompt() is and will not be supported.')
  109. }
  110. ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, message, sourceOrigin) {
  111. // Manually dispatch event instead of using postMessage because we also need to
  112. // set event.source.
  113. event = document.createEvent('Event')
  114. event.initEvent('message', false, false)
  115. event.data = message
  116. event.origin = sourceOrigin
  117. event.source = getOrCreateProxy(ipcRenderer, sourceId)
  118. window.dispatchEvent(event)
  119. })
  120. window.history.back = function () {
  121. ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER_GO_BACK')
  122. }
  123. window.history.forward = function () {
  124. ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER_GO_FORWARD')
  125. }
  126. window.history.go = function (offset) {
  127. ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER_GO_TO_OFFSET', +offset)
  128. }
  129. defineProperty(window.history, 'length', {
  130. get: function () {
  131. return ipcRenderer.sendSync('ELECTRON_NAVIGATION_CONTROLLER_LENGTH')
  132. }
  133. })
  134. if (guestInstanceId != null) {
  135. // Webview `document.visibilityState` tracks window visibility (and ignores
  136. // the actual <webview> element visibility) for backwards compatibility.
  137. // See discussion in #9178.
  138. //
  139. // Note that this results in duplicate visibilitychange events (since
  140. // Chromium also fires them) and potentially incorrect visibility change.
  141. // We should reconsider this decision for Electron 2.0.
  142. let cachedVisibilityState = hiddenPage ? 'hidden' : 'visible'
  143. // Subscribe to visibilityState changes.
  144. ipcRenderer.on('ELECTRON_GUEST_INSTANCE_VISIBILITY_CHANGE', function (event, visibilityState) {
  145. if (cachedVisibilityState !== visibilityState) {
  146. cachedVisibilityState = visibilityState
  147. document.dispatchEvent(new Event('visibilitychange'))
  148. }
  149. })
  150. // Make document.hidden and document.visibilityState return the correct value.
  151. defineProperty(document, 'hidden', {
  152. get: function () {
  153. return cachedVisibilityState !== 'visible'
  154. }
  155. })
  156. defineProperty(document, 'visibilityState', {
  157. get: function () {
  158. return cachedVisibilityState
  159. }
  160. })
  161. }
  162. }