window-setup.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. const windowProxies = {}
  31. const getOrCreateProxy = (ipcRenderer, guestId) => {
  32. let proxy = windowProxies[guestId]
  33. if (proxy == null) {
  34. proxy = new BrowserWindowProxy(ipcRenderer, guestId)
  35. windowProxies[guestId] = proxy
  36. }
  37. return proxy
  38. }
  39. const removeProxy = (guestId) => {
  40. delete windowProxies[guestId]
  41. }
  42. function BrowserWindowProxy (ipcRenderer, guestId) {
  43. this.closed = false
  44. defineProperty(this, 'location', {
  45. get: function () {
  46. return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'getURL')
  47. },
  48. set: function (url) {
  49. url = resolveURL(url)
  50. return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', guestId, 'loadURL', url)
  51. }
  52. })
  53. ipcRenderer.once(`ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_${guestId}`, () => {
  54. removeProxy(guestId)
  55. this.closed = true
  56. })
  57. this.close = () => {
  58. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', guestId)
  59. }
  60. this.focus = () => {
  61. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'focus')
  62. }
  63. this.blur = () => {
  64. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'blur')
  65. }
  66. this.print = () => {
  67. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'print')
  68. }
  69. this.postMessage = (message, targetOrigin) => {
  70. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', guestId, message, targetOrigin, window.location.origin)
  71. }
  72. this.eval = (...args) => {
  73. ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'executeJavaScript', ...args)
  74. }
  75. }
  76. // Forward history operations to browser.
  77. const sendHistoryOperation = function (ipcRenderer, ...args) {
  78. ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER', ...args)
  79. }
  80. const getHistoryOperation = function (ipcRenderer, ...args) {
  81. return ipcRenderer.sendSync('ELECTRON_SYNC_NAVIGATION_CONTROLLER', ...args)
  82. }
  83. module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => {
  84. if (guestInstanceId == null) {
  85. // Override default window.close.
  86. window.close = function () {
  87. ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CLOSE')
  88. }
  89. }
  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, frameName, features)
  96. if (guestId != null) {
  97. return getOrCreateProxy(ipcRenderer, guestId)
  98. } else {
  99. return null
  100. }
  101. }
  102. window.alert = function (message, title) {
  103. ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_ALERT', message, title)
  104. }
  105. window.confirm = function (message, title) {
  106. return ipcRenderer.sendSync('ELECTRON_BROWSER_WINDOW_CONFIRM', message, title)
  107. }
  108. // But we do not support prompt().
  109. window.prompt = function () {
  110. throw new Error('prompt() is and will not be supported.')
  111. }
  112. if (openerId != null) {
  113. window.opener = getOrCreateProxy(ipcRenderer, openerId)
  114. }
  115. ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, message, sourceOrigin) {
  116. // Manually dispatch event instead of using postMessage because we also need to
  117. // set event.source.
  118. event = document.createEvent('Event')
  119. event.initEvent('message', false, false)
  120. event.data = message
  121. event.origin = sourceOrigin
  122. event.source = getOrCreateProxy(ipcRenderer, sourceId)
  123. window.dispatchEvent(event)
  124. })
  125. window.history.back = function () {
  126. sendHistoryOperation(ipcRenderer, 'goBack')
  127. }
  128. window.history.forward = function () {
  129. sendHistoryOperation(ipcRenderer, 'goForward')
  130. }
  131. window.history.go = function (offset) {
  132. sendHistoryOperation(ipcRenderer, 'goToOffset', offset)
  133. }
  134. defineProperty(window.history, 'length', {
  135. get: function () {
  136. return getHistoryOperation(ipcRenderer, 'length')
  137. }
  138. })
  139. // The initial visibilityState.
  140. let cachedVisibilityState = hiddenPage ? 'hidden' : 'visible'
  141. // Subscribe to visibilityState changes.
  142. ipcRenderer.on('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', function (event, visibilityState) {
  143. if (cachedVisibilityState !== visibilityState) {
  144. cachedVisibilityState = visibilityState
  145. document.dispatchEvent(new Event('visibilitychange'))
  146. }
  147. })
  148. // Make document.hidden and document.visibilityState return the correct value.
  149. defineProperty(document, 'hidden', {
  150. get: function () {
  151. return cachedVisibilityState !== 'visible'
  152. }
  153. })
  154. defineProperty(document, 'visibilityState', {
  155. get: function () {
  156. return cachedVisibilityState
  157. }
  158. })
  159. }