Move more functions to outer scope

This commit is contained in:
Kevin Sawicki 2017-01-11 17:04:20 -08:00
parent 3f7b3c4bd7
commit bb260343de

View file

@ -15,21 +15,20 @@ const resolveURL = function (url) {
const windowProxies = {} const windowProxies = {}
module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => { const getOrCreateProxy = (ipcRenderer, guestId) => {
const getOrCreateProxy = (guestId) => {
let proxy = windowProxies[guestId] let proxy = windowProxies[guestId]
if (proxy == null) { if (proxy == null) {
proxy = new BrowserWindowProxy(guestId) proxy = new BrowserWindowProxy(ipcRenderer, guestId)
windowProxies[guestId] = proxy windowProxies[guestId] = proxy
} }
return proxy return proxy
} }
const removeProxy = (guestId) => { const removeProxy = (guestId) => {
delete windowProxies[guestId] delete windowProxies[guestId]
} }
function BrowserWindowProxy (guestId) { function BrowserWindowProxy (ipcRenderer, guestId) {
this.closed = false this.closed = false
ipcRenderer.once(`ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_${guestId}`, () => { ipcRenderer.once(`ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_${guestId}`, () => {
@ -60,8 +59,18 @@ module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => {
this.eval = (...args) => { this.eval = (...args) => {
ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', guestId, 'executeJavaScript', ...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) => {
if (guestInstanceId == null) { if (guestInstanceId == null) {
// Override default window.close. // Override default window.close.
window.close = function () { 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) const guestId = ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_OPEN', url, frameName, features)
if (guestId != null) { if (guestId != null) {
return getOrCreateProxy(guestId) return getOrCreateProxy(ipcRenderer, guestId)
} else { } else {
return null return null
} }
@ -96,7 +105,7 @@ module.exports = (ipcRenderer, guestInstanceId, openerId, hiddenPage) => {
} }
if (openerId != null) { if (openerId != null) {
window.opener = getOrCreateProxy(openerId) window.opener = getOrCreateProxy(ipcRenderer, openerId)
} }
ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, message, sourceOrigin) { 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.initEvent('message', false, false)
event.data = message event.data = message
event.origin = sourceOrigin event.origin = sourceOrigin
event.source = BrowserWindowProxy.getOrCreate(sourceId) event.source = getOrCreateProxy(ipcRenderer, sourceId)
window.dispatchEvent(event) 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 () { window.history.back = function () {
sendHistoryOperation('goBack') sendHistoryOperation(ipcRenderer, 'goBack')
} }
window.history.forward = function () { window.history.forward = function () {
sendHistoryOperation('goForward') sendHistoryOperation(ipcRenderer, 'goForward')
} }
window.history.go = function (offset) { window.history.go = function (offset) {
sendHistoryOperation('goToOffset', offset) sendHistoryOperation(ipcRenderer, 'goToOffset', offset)
} }
defineProperty(window.history, 'length', { defineProperty(window.history, 'length', {
get: function () { get: function () {
return getHistoryOperation('length') return getHistoryOperation(ipcRenderer, 'length')
} }
}) })