Merge branch 'access-url-through-web-contents'

This commit is contained in:
Kevin Sawicki 2016-11-28 10:56:35 -08:00
commit 5283b94bf9
3 changed files with 56 additions and 14 deletions

View file

@ -240,3 +240,18 @@ ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', function (event,
console.error(`Blocked ${event.sender.getURL()} from calling ${method} on its opener.`) console.error(`Blocked ${event.sender.getURL()} from calling ${method} on its opener.`)
} }
}) })
ipcMain.on('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', function (event, guestId, method, ...args) {
const guestContents = webContents.fromId(guestId)
if (guestContents == null) {
event.returnValue = null
return
}
if (canAccessWindow(event.sender, guestContents)) {
event.returnValue = guestContents[method].apply(guestContents, args)
} else {
console.error(`Blocked ${event.sender.getURL()} from calling ${method} on its opener.`)
event.returnValue = null
}
})

View file

@ -22,7 +22,7 @@ var BrowserWindowProxy = (function () {
} }
BrowserWindowProxy.remove = function (guestId) { BrowserWindowProxy.remove = function (guestId) {
return delete this.proxies[guestId] delete this.proxies[guestId]
} }
function BrowserWindowProxy (guestId1) { function BrowserWindowProxy (guestId1) {
@ -41,28 +41,28 @@ var BrowserWindowProxy = (function () {
} }
BrowserWindowProxy.prototype.close = function () { BrowserWindowProxy.prototype.close = function () {
return ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', this.guestId) ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', this.guestId)
} }
BrowserWindowProxy.prototype.focus = function () { BrowserWindowProxy.prototype.focus = function () {
return ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'focus') ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'focus')
} }
BrowserWindowProxy.prototype.blur = function () { BrowserWindowProxy.prototype.blur = function () {
return ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'blur') ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'blur')
} }
BrowserWindowProxy.prototype.print = function () { BrowserWindowProxy.prototype.print = function () {
return ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'print') ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'print')
} }
Object.defineProperty(BrowserWindowProxy.prototype, 'location', { Object.defineProperty(BrowserWindowProxy.prototype, 'location', {
get: function () { get: function () {
return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'getURL') return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', this.guestId, 'getURL')
}, },
set: function (url) { set: function (url) {
url = resolveURL(url) url = resolveURL(url)
return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_METHOD', this.guestId, 'loadURL', url) return ipcRenderer.sendSync('ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD_SYNC', this.guestId, 'loadURL', url)
} }
}) })
@ -70,11 +70,11 @@ var BrowserWindowProxy = (function () {
if (targetOrigin == null) { if (targetOrigin == null) {
targetOrigin = '*' targetOrigin = '*'
} }
return ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', this.guestId, message, targetOrigin, window.location.origin) ipcRenderer.send('ELECTRON_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', this.guestId, message, targetOrigin, window.location.origin)
} }
BrowserWindowProxy.prototype['eval'] = function (...args) { BrowserWindowProxy.prototype['eval'] = function (...args) {
return ipcRenderer.send.apply(ipcRenderer, ['ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'executeJavaScript'].concat(args)) ipcRenderer.send.apply(ipcRenderer, ['ELECTRON_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', this.guestId, 'executeJavaScript'].concat(args))
} }
return BrowserWindowProxy return BrowserWindowProxy
@ -207,7 +207,7 @@ ipcRenderer.on('ELECTRON_GUEST_WINDOW_POSTMESSAGE', function (event, sourceId, m
// Forward history operations to browser. // Forward history operations to browser.
var sendHistoryOperation = function (...args) { var sendHistoryOperation = function (...args) {
return ipcRenderer.send.apply(ipcRenderer, ['ELECTRON_NAVIGATION_CONTROLLER'].concat(args)) ipcRenderer.send.apply(ipcRenderer, ['ELECTRON_NAVIGATION_CONTROLLER'].concat(args))
} }
var getHistoryOperation = function (...args) { var getHistoryOperation = function (...args) {
@ -215,15 +215,15 @@ var getHistoryOperation = function (...args) {
} }
window.history.back = function () { window.history.back = function () {
return sendHistoryOperation('goBack') sendHistoryOperation('goBack')
} }
window.history.forward = function () { window.history.forward = function () {
return sendHistoryOperation('goForward') sendHistoryOperation('goForward')
} }
window.history.go = function (offset) { window.history.go = function (offset) {
return sendHistoryOperation('goToOffset', offset) sendHistoryOperation('goToOffset', offset)
} }
Object.defineProperty(window.history, 'length', { Object.defineProperty(window.history, 'length', {

View file

@ -345,6 +345,15 @@ describe('chromium feature', function () {
w = window.open(url, '', 'show=no') w = window.open(url, '', 'show=no')
}) })
it('works when origin matches', function (done) {
listener = function (event) {
assert.equal(event.data, location.href)
done()
}
window.addEventListener('message', listener)
w = window.open(`file://${fixtures}/pages/window-opener-location.html`, '', 'show=no')
})
it('works when origin does not match opener but has node integration', function (done) { it('works when origin does not match opener but has node integration', function (done) {
listener = function (event) { listener = function (event) {
assert.equal(event.data, location.href) assert.equal(event.data, location.href)
@ -397,11 +406,29 @@ describe('chromium feature', function () {
document.body.appendChild(webview) document.body.appendChild(webview)
}) })
it('works when origin matches', function (done) {
webview = new WebView()
webview.addEventListener('console-message', function (e) {
assert.equal(e.message, webview.src)
done()
})
webview.setAttribute('allowpopups', 'on')
webview.src = url.format({
pathname: srcPath,
protocol: 'file',
query: {
p: pageURL
},
slashes: true
})
document.body.appendChild(webview)
})
it('works when origin does not match opener but has node integration', function (done) { it('works when origin does not match opener but has node integration', function (done) {
webview = new WebView() webview = new WebView()
webview.addEventListener('console-message', function (e) { webview.addEventListener('console-message', function (e) {
webview.remove() webview.remove()
assert.equal(e.message, location.href) assert.equal(e.message, webview.src)
done() done()
}) })
webview.setAttribute('allowpopups', 'on') webview.setAttribute('allowpopups', 'on')