2015-11-12 10:28:04 +00:00
|
|
|
{ipcMain, BrowserWindow} = require 'electron'
|
2015-03-04 15:58:06 +00:00
|
|
|
v8Util = process.atomBinding 'v8_util'
|
2014-10-27 13:56:04 +00:00
|
|
|
|
2014-10-28 05:23:25 +00:00
|
|
|
frameToGuest = {}
|
2014-10-27 13:56:04 +00:00
|
|
|
|
2015-11-10 16:27:08 +00:00
|
|
|
# Copy attribute of |parent| to |child| if it is not defined in |child|.
|
|
|
|
mergeOptions = (child, parent) ->
|
2015-12-04 03:40:35 +00:00
|
|
|
for own key, value of parent when key not of child
|
2015-11-10 16:27:08 +00:00
|
|
|
if typeof value is 'object'
|
|
|
|
child[key] = mergeOptions {}, value
|
|
|
|
else
|
|
|
|
child[key] = value
|
|
|
|
child
|
|
|
|
|
2015-09-22 13:56:56 +00:00
|
|
|
# Merge |options| with the |embedder|'s window's options.
|
|
|
|
mergeBrowserWindowOptions = (embedder, options) ->
|
|
|
|
if embedder.browserWindowOptions?
|
|
|
|
# Inherit the original options if it is a BrowserWindow.
|
2015-11-10 16:27:08 +00:00
|
|
|
mergeOptions options, embedder.browserWindowOptions
|
2015-09-22 13:56:56 +00:00
|
|
|
else
|
|
|
|
# Or only inherit web-preferences if it is a webview.
|
2015-11-10 14:23:21 +00:00
|
|
|
options.webPreferences ?= {}
|
2015-11-10 16:27:08 +00:00
|
|
|
mergeOptions options.webPreferences, embedder.getWebPreferences()
|
2015-09-22 13:56:56 +00:00
|
|
|
options
|
|
|
|
|
2014-10-27 13:56:04 +00:00
|
|
|
# Create a new guest created by |embedder| with |options|.
|
2014-10-28 05:23:25 +00:00
|
|
|
createGuest = (embedder, url, frameName, options) ->
|
2014-10-28 10:51:28 +00:00
|
|
|
guest = frameToGuest[frameName]
|
|
|
|
if frameName and guest?
|
2015-11-13 08:03:40 +00:00
|
|
|
guest.loadURL url
|
2014-10-28 10:51:28 +00:00
|
|
|
return guest.id
|
2014-10-27 13:56:04 +00:00
|
|
|
|
2015-12-17 13:27:14 +00:00
|
|
|
# Remember the embedder window's id.
|
|
|
|
options.webPreferences ?= {}
|
|
|
|
options.webPreferences.openerId = BrowserWindow.fromWebContents(embedder)?.id
|
|
|
|
|
2015-09-22 15:39:31 +00:00
|
|
|
guest = new BrowserWindow(options)
|
2015-11-13 08:03:40 +00:00
|
|
|
guest.loadURL url
|
2014-10-27 13:56:04 +00:00
|
|
|
|
2014-10-28 10:51:28 +00:00
|
|
|
# When |embedder| is destroyed we should also destroy attached guest, and if
|
|
|
|
# guest is closed by user then we should prevent |embedder| from double
|
|
|
|
# closing guest.
|
2015-12-03 08:17:10 +00:00
|
|
|
guestId = guest.id
|
2014-10-28 10:51:28 +00:00
|
|
|
closedByEmbedder = ->
|
|
|
|
guest.removeListener 'closed', closedByUser
|
2015-07-06 13:26:50 +00:00
|
|
|
guest.destroy()
|
2014-10-28 10:51:28 +00:00
|
|
|
closedByUser = ->
|
2015-12-04 03:02:55 +00:00
|
|
|
embedder.send "ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSED_#{guestId}"
|
2014-10-28 10:51:28 +00:00
|
|
|
embedder.removeListener 'render-view-deleted', closedByEmbedder
|
|
|
|
embedder.once 'render-view-deleted', closedByEmbedder
|
|
|
|
guest.once 'closed', closedByUser
|
|
|
|
|
|
|
|
if frameName
|
|
|
|
frameToGuest[frameName] = guest
|
|
|
|
guest.frameName = frameName
|
|
|
|
guest.once 'closed', ->
|
|
|
|
delete frameToGuest[frameName]
|
2014-10-27 13:56:04 +00:00
|
|
|
|
2014-10-28 05:23:25 +00:00
|
|
|
guest.id
|
2014-10-27 13:56:04 +00:00
|
|
|
|
|
|
|
# Routed window.open messages.
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_OPEN', (event, args...) ->
|
2014-11-04 10:31:47 +00:00
|
|
|
[url, frameName, options] = args
|
2015-09-22 15:39:31 +00:00
|
|
|
options = mergeBrowserWindowOptions event.sender, options
|
|
|
|
event.sender.emit 'new-window', event, url, frameName, 'new-window', options
|
2015-09-22 12:11:05 +00:00
|
|
|
if (event.sender.isGuest() and not event.sender.allowPopups) or event.defaultPrevented
|
2014-10-27 13:56:04 +00:00
|
|
|
event.returnValue = null
|
|
|
|
else
|
2015-09-22 15:39:31 +00:00
|
|
|
event.returnValue = createGuest event.sender, url, frameName, options
|
2014-10-27 13:56:04 +00:00
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_CLOSE', (event, guestId) ->
|
2015-06-24 10:39:02 +00:00
|
|
|
BrowserWindow.fromId(guestId)?.destroy()
|
2014-10-27 15:03:05 +00:00
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_METHOD', (event, guestId, method, args...) ->
|
2015-06-24 10:39:02 +00:00
|
|
|
BrowserWindow.fromId(guestId)?[method] args...
|
2014-10-27 15:07:41 +00:00
|
|
|
|
2015-12-11 00:52:39 +00:00
|
|
|
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WINDOW_POSTMESSAGE', (event, guestId, message, targetOrigin, sourceOrigin) ->
|
2015-12-15 00:47:33 +00:00
|
|
|
sourceId = BrowserWindow.fromWebContents(event.sender)?.id
|
|
|
|
return unless sourceId?
|
|
|
|
|
2015-06-24 10:39:02 +00:00
|
|
|
guestContents = BrowserWindow.fromId(guestId)?.webContents
|
2015-11-13 08:03:40 +00:00
|
|
|
if guestContents?.getURL().indexOf(targetOrigin) is 0 or targetOrigin is '*'
|
2015-12-15 00:47:33 +00:00
|
|
|
guestContents?.send 'ATOM_SHELL_GUEST_WINDOW_POSTMESSAGE', sourceId, message, sourceOrigin
|
|
|
|
|
2015-11-12 10:28:04 +00:00
|
|
|
ipcMain.on 'ATOM_SHELL_GUEST_WINDOW_MANAGER_WEB_CONTENTS_METHOD', (event, guestId, method, args...) ->
|
2015-06-24 10:39:02 +00:00
|
|
|
BrowserWindow.fromId(guestId)?.webContents?[method] args...
|