Merge pull request #3812 from atom/make-opener-a-proxy

Make window.opener a BrowserWindowProxy
This commit is contained in:
Cheng Zhao 2015-12-15 15:01:28 +08:00
commit e7090887d9
4 changed files with 25 additions and 23 deletions

View file

@ -74,23 +74,19 @@ ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', (event, guestId, met
BrowserWindow.fromId(guestId)?[method] args...
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
if guestContents?.getURL().indexOf(targetOrigin) is 0 or targetOrigin is '*'
guestContents?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', guestId, 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
guestContents?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', (event, guestId, 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'
openerId = null
if embedder?
guest = BrowserWindow.fromWebContents event.sender
if guest?
event.returnValue = guest.id
return
event.returnValue = null
openerId = BrowserWindow.fromWebContents(embedder)?.id
event.returnValue = openerId

View file

@ -91,21 +91,17 @@ window.confirm = (message, title='') ->
window.prompt = ->
throw new Error('prompt() is and will not be supported.')
# Implement window.postMessage if current window is a guest window.
guestId = ipcRenderer.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_GUEST_ID'
if guestId?
window.opener =
postMessage: (message, targetOrigin='*') ->
ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', guestId, message, targetOrigin, location.origin
openerId = ipcRenderer.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_OPENER_ID'
window.opener = BrowserWindowProxy.getOrCreate(openerId) if openerId?
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
# set event.source.
event = document.createEvent 'Event'
event.initEvent 'message', false, false
event.data = message
event.origin = sourceOrigin
event.source = BrowserWindowProxy.getOrCreate(guestId)
event.source = BrowserWindowProxy.getOrCreate(sourceId)
window.dispatchEvent event
# Forward history operations to browser.

View file

@ -116,11 +116,16 @@ describe 'chromium feature', ->
b = window.open url, '', 'show=no'
describe 'window.postMessage', ->
it 'sets the origin correctly', (done) ->
it 'sets the source and origin correctly', (done) ->
sourceId = remote.getCurrentWindow().id
listener = (event) ->
window.removeEventListener 'message', listener
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://'
done()
window.addEventListener 'message', listener

View file

@ -2,7 +2,12 @@
<body>
<script type="text/javascript" charset="utf-8">
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>
</body>