window-setup.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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, defineProperties } = Object
  24. // Helper function to resolve relative url.
  25. const a = window.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 LocationProxy (ipcRenderer, guestId) {
  49. const getGuestURL = function () {
  50. const urlString = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'getURL')
  51. try {
  52. return new URL(urlString)
  53. } catch (e) {
  54. console.error('LocationProxy: failed to parse string', urlString, e)
  55. }
  56. return null
  57. }
  58. const propertyProxyFor = function (property) {
  59. return {
  60. get: function () {
  61. const guestURL = getGuestURL()
  62. const value = guestURL ? guestURL[property] : ''
  63. return value === undefined ? '' : value
  64. },
  65. set: function (newVal) {
  66. const guestURL = getGuestURL()
  67. if (guestURL) {
  68. guestURL[property] = newVal
  69. return ipcRenderer.sendSync(
  70. 'ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC',
  71. guestId, 'loadURL', guestURL.toString())
  72. }
  73. }
  74. }
  75. }
  76. defineProperties(this, {
  77. hash: propertyProxyFor('hash'),
  78. href: propertyProxyFor('href'),
  79. host: propertyProxyFor('host'),
  80. hostname: propertyProxyFor('hostname'),
  81. origin: propertyProxyFor('origin'),
  82. pathname: propertyProxyFor('pathname'),
  83. port: propertyProxyFor('port'),
  84. protocol: propertyProxyFor('protocol'),
  85. search: propertyProxyFor('search')
  86. })
  87. this.toString = function () {
  88. return this.href
  89. }
  90. }
  91. function BrowserWindowProxy (ipcRenderer, guestId) {
  92. this.closed = false
  93. const location = new LocationProxy(ipcRenderer, guestId)
  94. defineProperty(this, 'location', {
  95. get: function () {
  96. return location
  97. },
  98. set: function (url) {
  99. url = resolveURL(url)
  100. return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'loadURL', url)
  101. }
  102. })
  103. ipcRenderer.once(`ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_${guestId}`, () => {
  104. removeProxy(guestId)
  105. this.closed = true
  106. })
  107. this.close = () => {
  108. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', guestId)
  109. }
  110. this.focus = () => {
  111. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'focus')
  112. }
  113. this.blur = () => {
  114. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'blur')
  115. }
  116. this.print = () => {
  117. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'print')
  118. }
  119. this.postMessage = (message, targetOrigin) => {
  120. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', guestId, message, toString(targetOrigin), window.location.origin)
  121. }
  122. this.eval = (...args) => {
  123. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'executeJavaScript', ...args)
  124. }
  125. }
  126. module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage, usesNativeWindowOpen) => {
  127. if (guestInstanceId == null) {
  128. // Override default window.close.
  129. window.close = function () {
  130. ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CLOSE')
  131. }
  132. }
  133. if (!usesNativeWindowOpen) {
  134. // Make the browser window or guest view emit "new-window" event.
  135. window.open = function (url, frameName, features) {
  136. if (url != null && url !== '') {
  137. url = resolveURL(url)
  138. }
  139. const guestId = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, toString(frameName), toString(features))
  140. if (guestId != null) {
  141. return getOrCreateProxy(ipcRenderer, guestId)
  142. } else {
  143. return null
  144. }
  145. }
  146. if (openerId != null) {
  147. window.opener = getOrCreateProxy(ipcRenderer, openerId)
  148. }
  149. }
  150. // But we do not support prompt().
  151. window.prompt = function () {
  152. throw new Error('prompt() is and will not be supported.')
  153. }
  154. ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, message, sourceOrigin) {
  155. // Manually dispatch event instead of using postMessage because we also need to
  156. // set event.source.
  157. event = document.createEvent('Event')
  158. event.initEvent('message', false, false)
  159. event.data = message
  160. event.origin = sourceOrigin
  161. event.source = getOrCreateProxy(ipcRenderer, sourceId)
  162. window.dispatchEvent(event)
  163. })
  164. window.history.back = function () {
  165. ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER_GO_BACK')
  166. }
  167. window.history.forward = function () {
  168. ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER_GO_FORWARD')
  169. }
  170. window.history.go = function (offset) {
  171. ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER_GO_TO_OFFSET', +offset)
  172. }
  173. defineProperty(window.history, 'length', {
  174. get: function () {
  175. return ipcRenderer.sendSync('ELECTRON_NAVIGATION_CONTROLLER_LENGTH')
  176. }
  177. })
  178. if (guestInstanceId != null) {
  179. // Webview `document.visibilityState` tracks window visibility (and ignores
  180. // the actual <webview> element visibility) for backwards compatibility.
  181. // See discussion in #9178.
  182. //
  183. // Note that this results in duplicate visibilitychange events (since
  184. // Chromium also fires them) and potentially incorrect visibility change.
  185. // We should reconsider this decision for Electron 2.0.
  186. let cachedVisibilityState = hiddenPage ? 'hidden' : 'visible'
  187. // Subscribe to visibilityState changes.
  188. ipcRenderer.on('ELECTRON_GUEST_INSTANCE_VISIBILITY_CHANGE', function (event, visibilityState) {
  189. if (cachedVisibilityState !== visibilityState) {
  190. cachedVisibilityState = visibilityState
  191. document.dispatchEvent(new Event('visibilitychange'))
  192. }
  193. })
  194. // Make document.hidden and document.visibilityState return the correct value.
  195. defineProperty(document, 'hidden', {
  196. get: function () {
  197. return cachedVisibilityState !== 'visible'
  198. }
  199. })
  200. defineProperty(document, 'visibilityState', {
  201. get: function () {
  202. return cachedVisibilityState
  203. }
  204. })
  205. }
  206. }