2014-10-23 09:54:52 +00:00
|
|
|
ipc = require 'ipc'
|
|
|
|
webContents = require 'web-contents'
|
2014-10-23 15:08:48 +00:00
|
|
|
webViewManager = null # Doesn't exist in early initialization.
|
2014-10-23 09:54:52 +00:00
|
|
|
|
2014-10-25 01:05:50 +00:00
|
|
|
supportedWebViewEvents = [
|
|
|
|
'did-finish-load'
|
|
|
|
'did-fail-load'
|
|
|
|
'did-frame-finish-load'
|
|
|
|
'did-start-loading'
|
|
|
|
'did-stop-loading'
|
2015-04-08 13:44:47 +00:00
|
|
|
'did-get-response-details'
|
2014-10-25 01:05:50 +00:00
|
|
|
'did-get-redirect-request'
|
2015-04-29 13:49:31 +00:00
|
|
|
'dom-ready'
|
2014-10-25 02:01:19 +00:00
|
|
|
'console-message'
|
2014-10-25 05:21:09 +00:00
|
|
|
'new-window'
|
2014-10-25 02:01:19 +00:00
|
|
|
'close'
|
2014-10-25 01:05:50 +00:00
|
|
|
'crashed'
|
2015-05-12 19:35:56 +00:00
|
|
|
'gpu-crashed'
|
|
|
|
'plugin-crashed'
|
2014-10-25 01:05:50 +00:00
|
|
|
'destroyed'
|
2015-04-05 09:20:42 +00:00
|
|
|
'page-title-set'
|
2015-04-20 06:50:04 +00:00
|
|
|
'page-favicon-updated'
|
2015-05-19 08:36:19 +00:00
|
|
|
'enter-html-full-screen'
|
|
|
|
'leave-html-full-screen'
|
2014-10-25 01:05:50 +00:00
|
|
|
]
|
|
|
|
|
2014-10-23 09:54:52 +00:00
|
|
|
nextInstanceId = 0
|
|
|
|
guestInstances = {}
|
2014-12-08 16:05:34 +00:00
|
|
|
embedderElementsMap = {}
|
|
|
|
reverseEmbedderElementsMap = {}
|
2014-10-23 09:54:52 +00:00
|
|
|
|
|
|
|
# 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'
|
|
|
|
|
2014-10-23 09:54:52 +00:00
|
|
|
id = getNextInstanceId embedder
|
2014-10-24 08:19:00 +00:00
|
|
|
guest = webContents.create
|
2014-10-23 15:08:48 +00:00
|
|
|
isGuest: true
|
|
|
|
guestInstanceId: id
|
|
|
|
storagePartitionId: params.storagePartitionId
|
2014-10-26 11:30:53 +00:00
|
|
|
guestInstances[id] = {guest, embedder}
|
2014-10-24 08:19:00 +00:00
|
|
|
|
2014-12-09 22:38:43 +00:00
|
|
|
# Destroy guest when the embedder is gone or navigated.
|
|
|
|
destroyEvents = ['destroyed', 'crashed', 'did-navigate-to-different-page']
|
|
|
|
destroy = ->
|
2015-02-04 23:52:59 +00:00
|
|
|
destroyGuest embedder, id if guestInstances[id]?
|
2014-12-09 22:38:43 +00:00
|
|
|
embedder.once event, destroy for event in destroyEvents
|
2014-12-18 04:44:15 +00:00
|
|
|
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
|
|
|
|
|
2014-10-25 01:05:50 +00:00
|
|
|
@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
|
2015-05-11 17:00:41 +00:00
|
|
|
|
2014-10-24 08:48:21 +00:00
|
|
|
if params.src
|
2015-05-06 04:01:41 +00:00
|
|
|
opts = {}
|
|
|
|
opts.httpreferrer = params.httpreferrer if params.httpreferrer
|
|
|
|
opts.useragent = params.useragent if params.useragent
|
2015-05-11 17:00:41 +00:00
|
|
|
@loadUrl params.src, opts
|
|
|
|
|
2014-10-24 08:48:21 +00:00
|
|
|
if params.allowtransparency?
|
|
|
|
@setAllowTransparency params.allowtransparency
|
2014-10-24 08:19:00 +00:00
|
|
|
|
2014-10-25 01:05:50 +00:00
|
|
|
# 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.
|
2015-01-15 03:43:01 +00:00
|
|
|
guest.on 'ipc-message-host', (_, packed) ->
|
|
|
|
[channel, args...] = packed
|
2014-12-17 05:10:53 +00:00
|
|
|
embedder.send "ATOM_SHELL_GUEST_VIEW_INTERNAL_IPC_MESSAGE-#{guest.viewInstanceId}", channel, args...
|
|
|
|
|
2014-10-25 07:48:59 +00:00
|
|
|
# Autosize.
|
|
|
|
guest.on 'size-changed', (_, args...) ->
|
2014-12-13 01:16:12 +00:00
|
|
|
embedder.send "ATOM_SHELL_GUEST_VIEW_INTERNAL_SIZE_CHANGED-#{guest.viewInstanceId}", args...
|
2014-10-25 07:48:59 +00:00
|
|
|
|
2014-10-23 09:54:52 +00:00
|
|
|
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]?
|
2015-02-04 23:52:59 +00:00
|
|
|
destroyGuest embedder, oldGuestInstanceId
|
2014-12-08 16:05:34 +00:00
|
|
|
|
2014-12-09 22:38:43 +00:00
|
|
|
webViewManager.addGuest guestInstanceId, elementInstanceId, embedder, guest,
|
2014-12-09 22:51:35 +00:00
|
|
|
nodeIntegration: params.nodeintegration
|
2014-12-09 22:38:43 +00:00
|
|
|
plugins: params.plugins
|
2014-12-18 00:32:25 +00:00
|
|
|
disableWebSecurity: params.disablewebsecurity
|
2014-12-09 22:38:43 +00:00
|
|
|
preloadUrl: params.preload ? ''
|
|
|
|
|
2014-12-08 16:05:34 +00:00
|
|
|
guest.attachParams = params
|
|
|
|
embedderElementsMap[key] = guestInstanceId
|
|
|
|
reverseEmbedderElementsMap[guestInstanceId] = key
|
|
|
|
|
2014-10-23 09:54:52 +00:00
|
|
|
# Destroy an existing guest instance.
|
2015-02-04 23:52:59 +00:00
|
|
|
destroyGuest = (embedder, id) ->
|
|
|
|
webViewManager.removeGuest embedder, id
|
2014-10-26 11:30:53 +00:00
|
|
|
guestInstances[id].guest.destroy()
|
2014-10-23 09:54:52 +00:00
|
|
|
delete guestInstances[id]
|
|
|
|
|
2014-12-08 16:05:34 +00:00
|
|
|
key = reverseEmbedderElementsMap[id]
|
|
|
|
if key?
|
|
|
|
delete reverseEmbedderElementsMap[id]
|
|
|
|
delete embedderElementsMap[key]
|
|
|
|
|
2014-10-23 09:54:52 +00:00
|
|
|
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-10-23 09:54:52 +00:00
|
|
|
|
2015-01-12 22:17:49 +00:00
|
|
|
ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_ATTACH_GUEST', (event, elementInstanceId, guestInstanceId, params) ->
|
2014-12-08 16:05:34 +00:00
|
|
|
attachGuest event.sender, elementInstanceId, guestInstanceId, params
|
|
|
|
|
2014-10-24 08:48:21 +00:00
|
|
|
ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_DESTROY_GUEST', (event, id) ->
|
2015-02-04 23:52:59 +00:00
|
|
|
destroyGuest event.sender, id
|
2014-10-24 06:37:14 +00:00
|
|
|
|
2014-10-24 08:48:21 +00:00
|
|
|
ipc.on 'ATOM_SHELL_GUEST_VIEW_MANAGER_SET_AUTO_SIZE', (event, id, params) ->
|
2014-10-26 11:30:53 +00:00
|
|
|
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) ->
|
2014-10-26 11:30:53 +00:00
|
|
|
guestInstances[id]?.guest.setAllowTransparency allowtransparency
|
2014-10-24 11:57:29 +00:00
|
|
|
|
|
|
|
# Returns WebContents from its guest id.
|
|
|
|
exports.getGuest = (id) ->
|
2014-10-26 11:30:53 +00:00
|
|
|
guestInstances[id]?.guest
|
|
|
|
|
|
|
|
# Returns the embedder of the guest.
|
|
|
|
exports.getEmbedder = (id) ->
|
|
|
|
guestInstances[id]?.embedder
|