diff --git a/lib/renderer/window-setup.js b/lib/renderer/window-setup.js index 3aa451d16468..f7328c9b0c58 100644 --- a/lib/renderer/window-setup.js +++ b/lib/renderer/window-setup.js @@ -15,53 +15,62 @@ const resolveURL = function (url) { const windowProxies = {} +const getOrCreateProxy = (ipcRenderer, guestId) => { + let proxy = windowProxies[guestId] + if (proxy == null) { + proxy = new BrowserWindowProxy(ipcRenderer, guestId) + windowProxies[guestId] = proxy + } + return proxy +} + +const removeProxy = (guestId) => { + delete windowProxies[guestId] +} + +function BrowserWindowProxy (ipcRenderer, guestId) { + this.closed = false + + ipcRenderer.once(`ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_${guestId}`, () => { + removeProxy(this.guestId) + this.closed = true + }) + + this.close = () => { + ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', guestId) + } + + this.focus = () => { + ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'focus') + } + + this.blur = () => { + ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'blur') + } + + this.print = () => { + ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'print') + } + + this.postMessage = (message, targetOrigin) => { + ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', guestId, message, targetOrigin, window.location.origin) + } + + this.eval = (...args) => { + ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'executeJavaScript', ...args) + } +} + +// Forward history operations to browser. +const sendHistoryOperation = function (ipcRenderer, ...args) { + ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER', ...args) +} + +const getHistoryOperation = function (ipcRenderer, ...args) { + return ipcRenderer.sendSync('ELECTRON_SYNC_NAVIGATION_CONTROLLER', ...args) +} + module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => { - const getOrCreateProxy = (guestId) => { - let proxy = windowProxies[guestId] - if (proxy == null) { - proxy = new BrowserWindowProxy(guestId) - windowProxies[guestId] = proxy - } - return proxy - } - - const removeProxy = (guestId) => { - delete windowProxies[guestId] - } - - function BrowserWindowProxy (guestId) { - this.closed = false - - ipcRenderer.once(`ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_${guestId}`, () => { - removeProxy(this.guestId) - this.closed = true - }) - - this.close = () => { - ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', guestId) - } - - this.focus = () => { - ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'focus') - } - - this.blur = () => { - ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', guestId, 'blur') - } - - this.print = () => { - ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'print') - } - - this.postMessage = (message, targetOrigin) => { - ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', guestId, message, targetOrigin, window.location.origin) - } - - this.eval = (...args) => { - ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'executeJavaScript', ...args) - } - } - if (guestInstanceId == null) { // Override default window.close. window.close = function () { @@ -76,7 +85,7 @@ module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => { } const guestId = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, frameName, features) if (guestId != null) { - return getOrCreateProxy(guestId) + return getOrCreateProxy(ipcRenderer, guestId) } else { return null } @@ -96,7 +105,7 @@ module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => { } if (openerId != null) { - window.opener = getOrCreateProxy(openerId) + window.opener = getOrCreateProxy(ipcRenderer, openerId) } ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, message, sourceOrigin) { @@ -106,34 +115,25 @@ module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => { event.initEvent('message', false, false) event.data = message event.origin = sourceOrigin - event.source = BrowserWindowProxy.getOrCreate(sourceId) + event.source = getOrCreateProxy(ipcRenderer, sourceId) window.dispatchEvent(event) }) - // Forward history operations to browser. - const sendHistoryOperation = function (...args) { - ipcRenderer.send('ELECTRON_NAVIGATION_CONTROLLER', ...args) - } - - const getHistoryOperation = function (...args) { - return ipcRenderer.sendSync('ELECTRON_SYNC_NAVIGATION_CONTROLLER', ...args) - } - window.history.back = function () { - sendHistoryOperation('goBack') + sendHistoryOperation(ipcRenderer, 'goBack') } window.history.forward = function () { - sendHistoryOperation('goForward') + sendHistoryOperation(ipcRenderer, 'goForward') } window.history.go = function (offset) { - sendHistoryOperation('goToOffset', offset) + sendHistoryOperation(ipcRenderer, 'goToOffset', offset) } defineProperty(window.history, 'length', { get: function () { - return getHistoryOperation('length') + return getHistoryOperation(ipcRenderer, 'length') } })