electron/atom/browser/lib/guest-view-manager.coffee

138 lines
4.5 KiB
CoffeeScript
Raw Normal View History

ipc = require 'ipc'
webContents = require 'web-contents'
2014-10-23 15:08:48 +00:00
webViewManager = null # Doesn't exist in early initialization.
supportedWebViewEvents = [
'did-finish-load'
'did-fail-load'
'did-frame-finish-load'
'did-start-loading'
'did-stop-loading'
'did-get-redirect-request'
'console-message'
2014-10-25 05:21:09 +00:00
'new-window'
'close'
'crashed'
'destroyed'
]
nextInstanceId = 0
guestInstances = {}
2014-12-08 16:05:34 +00:00
embedderElementsMap = {}
reverseEmbedderElementsMap = {}
# Generate guestInstanceId.
getNextInstanceId = (webContents) ->
++nextInstanceId
# Create a new guest instance.
createGuest = (embedder, params) ->
2014-10-23 15:08:48 +00:00
webViewManager ?= process.atomBinding 'web_view_manager'
id = getNextInstanceId embedder
guest = webContents.create
2014-10-23 15:08:48 +00:00
isGuest: true
guestInstanceId: id
storagePartitionId: params.storagePartitionId
guestInstances[id] = {guest, embedder}
# Destroy guest when the embedder is gone or navigated.
destroyEvents = ['destroyed', 'crashed', 'did-navigate-to-different-page']
destroy = ->
2014-10-25 14:01:24 +00:00
destroyGuest id if guestInstances[id]?
embedder.once event, destroy for event in destroyEvents
guest.once 'destroyed', ->
embedder.removeListener event, destroy for event in destroyEvents
2014-10-24 08:48:21 +00:00
# Init guest web view after attached.
2014-12-08 16:05:34 +00:00
guest.once 'did-attach', ->
params = @attachParams
delete @attachParams
@viewInstanceId = params.instanceId
2014-10-24 08:48:21 +00:00
min = width: params.minwidth, height: params.minheight
max = width: params.maxwidth, height: params.maxheight
@setAutoSize params.autosize, min, max
if params.src
2014-11-12 02:28:50 +00:00
if params.httpreferrer
@loadUrl params.src, {httpreferrer: params.httpreferrer}
else
@loadUrl params.src
2014-10-24 08:48:21 +00:00
if params.allowtransparency?
@setAllowTransparency params.allowtransparency
# Dispatch events to embedder.
for event in supportedWebViewEvents
do (event) ->
guest.on event, (_, args...) ->
embedder.send "ATOM_SHELL_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-#{guest.viewInstanceId}", event, args...
2014-12-17 19:09:11 +00:00
# Dispatch guest's IPC messages to embedder.
guest.on 'ipc-message-host', (_, channel, args...) ->
embedder.send "ATOM_SHELL_GUEST_VIEW_INTERNAL_IPC_MESSAGE-#{guest.viewInstanceId}", channel, args...
# Autosize.
guest.on 'size-changed', (_, args...) ->
embedder.send "ATOM_SHELL_GUEST_VIEW_INTERNAL_SIZE_CHANGED-#{guest.viewInstanceId}", args...
id
2014-12-08 16:05:34 +00:00
# Attach the guest to an element of embedder.
attachGuest = (embedder, elementInstanceId, guestInstanceId, params) ->
guest = guestInstances[guestInstanceId].guest
# Destroy the old guest when attaching.
key = "#{embedder.getId()}-#{elementInstanceId}"
oldGuestInstanceId = embedderElementsMap[key]
if oldGuestInstanceId?
# Reattachment to the same guest is not currently supported.
return unless oldGuestInstanceId != guestInstanceId
return unless guestInstances[oldGuestInstanceId]?
destroyGuest oldGuestInstanceId
webViewManager.addGuest guestInstanceId, elementInstanceId, embedder, guest,
nodeIntegration: params.nodeintegration
plugins: params.plugins
disableWebSecurity: params.disablewebsecurity
preloadUrl: params.preload ? ''
2014-12-08 16:05:34 +00:00
guest.attachParams = params
embedderElementsMap[key] = guestInstanceId
reverseEmbedderElementsMap[guestInstanceId] = key
# Destroy an existing guest instance.
destroyGuest = (id) ->
2014-10-23 15:08:48 +00:00
webViewManager.removeGuest id
guestInstances[id].guest.destroy()
delete guestInstances[id]
2014-12-08 16:05:34 +00:00
key = reverseEmbedderElementsMap[id]
if key?
delete reverseEmbedderElementsMap[id]
delete embedderElementsMap[key]
ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_CREATE_GUEST', (event, type, params, requestId) ->
2014-10-23 15:08:48 +00:00
event.sender.send "ATOM_SHELL_RESPONSE_#{requestId}", createGuest(event.sender, params)
2014-12-08 16:05:34 +00:00
ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_ATTACH_GUEST', (event, elementInstanceId, guestInstanceId, params, requestId) ->
attachGuest event.sender, elementInstanceId, guestInstanceId, params
event.sender.send "ATOM_SHELL_RESPONSE_#{requestId}"
2014-10-24 08:48:21 +00:00
ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_DESTROY_GUEST', (event, id) ->
destroyGuest id
2014-10-24 08:48:21 +00:00
ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_SET_AUTO_SIZE', (event, id, params) ->
guestInstances[id]?.guest.setAutoSize params.enableAutoSize, params.min, params.max
2014-10-24 08:48:21 +00:00
ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_SET_ALLOW_TRANSPARENCY', (event, id, allowtransparency) ->
guestInstances[id]?.guest.setAllowTransparency allowtransparency
2014-10-24 11:57:29 +00:00
# Returns WebContents from its guest id.
exports.getGuest = (id) ->
guestInstances[id]?.guest
# Returns the embedder of the guest.
exports.getEmbedder = (id) ->
guestInstances[id]?.embedder