Use id from source window when building proxy for event source

This commit is contained in:
Kevin Sawicki 2015-12-14 16:47:33 -08:00
parent 0ef0ce7345
commit 184b11be4c
2 changed files with 19 additions and 17 deletions

View file

@ -74,23 +74,27 @@ 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, message, targetOrigin, sourceOrigin) ->
sourceId = BrowserWindow.fromWebContents(event.sender)?.id
return unless sourceId?
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', (event, guestId, message, targetOrigin, sourceOrigin) ->
embedder = v8Util.getHiddenValue event.sender, 'embedder' embedder = v8Util.getHiddenValue event.sender, 'embedder'
if embedder?.getURL().indexOf(targetOrigin) is 0 or targetOrigin is '*' if embedder?.getURL().indexOf(targetOrigin) is 0 or targetOrigin is '*'
embedder?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', guestId, message, sourceOrigin embedder?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', sourceId, 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

View file

@ -91,22 +91,20 @@ 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.')
guestId = ipcRenderer.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_GUEST_ID' openerId = ipcRenderer.sendSync 'ATOM_SHELL_GUEST_WINDOW_MANAGER_GET_OPENER_ID'
if guestId? if openerId?
window.opener = BrowserWindowProxy.getOrCreate(guestId) window.opener = BrowserWindowProxy.getOrCreate(openerId)
# Remove BrowserWindowProxy API and give it a custom postMessage method
Object.setPrototypeOf(window.opener, null)
window.opener.postMessage = (message, targetOrigin='*') -> window.opener.postMessage = (message, targetOrigin='*') ->
ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', guestId, message, targetOrigin, location.origin ipcRenderer.send 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPENER_POSTMESSAGE', 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.