# 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
+ window.opener = BrowserWindowProxy.getOrCreate(guestId)
+ Object.setPrototypeOf(window.opener, null)
+ 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) ->
# Manually dispatch event instead of using postMessage because we also need to
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 event.origin, 'file://'
done()
window.addEventListener 'message', listener
<body>
<script type="text/javascript" charset="utf-8">
window.addEventListener('message', function (e) {
- window.opener.postMessage(e.origin + e.data, '*');
+ var reply = JSON.stringify({
+ origin: e.origin,
+ data: e.data,
+ sourceEqualsOpener: e.source === window.opener
+ })
+ window.opener.postMessage(reply, '*');
});
</script>
</body>