Merge pull request #3812 from atom/make-opener-a-proxy
Make window.opener a BrowserWindowProxy
This commit is contained in:
commit
e7090887d9
4 changed files with 25 additions and 23 deletions
|
@ -74,23 +74,19 @@ ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', (event, guestId, met
|
||||||
BrowserWindow.fromId(guestId)?[method] args...
|
BrowserWindow.fromId(guestId)?[method] args...
|
||||||
|
|
||||||
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', (event, guestId, message, targetOrigin, sourceOrigin) ->
|
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', (event, guestId, message, targetOrigin, sourceOrigin) ->
|
||||||
|
sourceId = BrowserWindow.fromWebContents(event.sender)?.id
|
||||||
|
return unless sourceId?
|
||||||
|
|
||||||
guestContents = BrowserWindow.fromId(guestId)?.webContents
|
guestContents = BrowserWindow.fromId(guestId)?.webContents
|
||||||
if guestContents?.getURL().indexOf(targetOrigin) is 0 or targetOrigin is '*'
|
if guestContents?.getURL().indexOf(targetOrigin) is 0 or targetOrigin is '*'
|
||||||
guestContents?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', guestId, message, sourceOrigin
|
guestContents?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin
|
||||||
|
|
||||||
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', (event, guestId, message, targetOrigin, sourceOrigin) ->
|
|
||||||
embedder = v8Util.getHiddenValue event.sender, 'embedder'
|
|
||||||
if embedder?.getURL().indexOf(targetOrigin) is 0 or targetOrigin is '*'
|
|
||||||
embedder?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', guestId, message, sourceOrigin
|
|
||||||
|
|
||||||
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', (event, guestId, method, args...) ->
|
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', (event, guestId, method, args...) ->
|
||||||
BrowserWindow.fromId(guestId)?.webContents?[method] args...
|
BrowserWindow.fromId(guestId)?.webContents?[method] args...
|
||||||
|
|
||||||
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_GUEST_ID', (event) ->
|
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_OPENER_ID', (event) ->
|
||||||
embedder = v8Util.getHiddenValue event.sender, 'embedder'
|
embedder = v8Util.getHiddenValue event.sender, 'embedder'
|
||||||
|
openerId = null
|
||||||
if embedder?
|
if embedder?
|
||||||
guest = BrowserWindow.fromWebContents event.sender
|
openerId = BrowserWindow.fromWebContents(embedder)?.id
|
||||||
if guest?
|
event.returnValue = openerId
|
||||||
event.returnValue = guest.id
|
|
||||||
return
|
|
||||||
event.returnValue = null
|
|
||||||
|
|
|
@ -91,21 +91,17 @@ window.confirm = (message, title='') ->
|
||||||
window.prompt = ->
|
window.prompt = ->
|
||||||
throw new Error('prompt() is and will not be supported.')
|
throw new Error('prompt() is and will not be supported.')
|
||||||
|
|
||||||
# Implement window.postMessage if current window is a guest window.
|
openerId = ipcRenderer.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_OPENER_ID'
|
||||||
guestId = ipcRenderer.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_GUEST_ID'
|
window.opener = BrowserWindowProxy.getOrCreate(openerId) if openerId?
|
||||||
if guestId?
|
|
||||||
window.opener =
|
|
||||||
postMessage: (message, targetOrigin='*') ->
|
|
||||||
ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', guestId, message, targetOrigin, location.origin
|
|
||||||
|
|
||||||
ipcRenderer.on 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', (event, guestId, message, sourceOrigin) ->
|
ipcRenderer.on 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', (event, sourceId, message, sourceOrigin) ->
|
||||||
# Manually dispatch event instead of using postMessage because we also need to
|
# Manually dispatch event instead of using postMessage because we also need to
|
||||||
# set event.source.
|
# set event.source.
|
||||||
event = document.createEvent 'Event'
|
event = document.createEvent 'Event'
|
||||||
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(guestId)
|
event.source = BrowserWindowProxy.getOrCreate(sourceId)
|
||||||
window.dispatchEvent event
|
window.dispatchEvent event
|
||||||
|
|
||||||
# Forward history operations to browser.
|
# Forward history operations to browser.
|
||||||
|
|
|
@ -116,11 +116,16 @@ describe 'chromium feature', ->
|
||||||
b = window.open url, '', 'show=no'
|
b = window.open url, '', 'show=no'
|
||||||
|
|
||||||
describe 'window.postMessage', ->
|
describe 'window.postMessage', ->
|
||||||
it 'sets the origin correctly', (done) ->
|
it 'sets the source and origin correctly', (done) ->
|
||||||
|
sourceId = remote.getCurrentWindow().id
|
||||||
listener = (event) ->
|
listener = (event) ->
|
||||||
window.removeEventListener 'message', listener
|
window.removeEventListener 'message', listener
|
||||||
b.close()
|
b.close()
|
||||||
assert.equal event.data, 'file://testing'
|
message = JSON.parse(event.data)
|
||||||
|
assert.equal message.data, 'testing'
|
||||||
|
assert.equal message.origin, 'file://'
|
||||||
|
assert.equal message.sourceEqualsOpener, true
|
||||||
|
assert.equal message.sourceId, sourceId
|
||||||
assert.equal event.origin, 'file://'
|
assert.equal event.origin, 'file://'
|
||||||
done()
|
done()
|
||||||
window.addEventListener 'message', listener
|
window.addEventListener 'message', listener
|
||||||
|
|
|
@ -2,7 +2,12 @@
|
||||||
<body>
|
<body>
|
||||||
<script type="text/javascript" charset="utf-8">
|
<script type="text/javascript" charset="utf-8">
|
||||||
window.addEventListener('message', function (e) {
|
window.addEventListener('message', function (e) {
|
||||||
window.opener.postMessage(e.origin + e.data, '*');
|
window.opener.postMessage(JSON.stringify({
|
||||||
|
origin: e.origin,
|
||||||
|
data: e.data,
|
||||||
|
sourceEqualsOpener: e.source === window.opener,
|
||||||
|
sourceId: e.source.guestId
|
||||||
|
}), '*');
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
Loading…
Reference in a new issue