electron/lib/renderer/web-view/guest-view-internal.js

113 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-03-25 19:57:17 +00:00
'use strict'
2016-03-18 18:51:02 +00:00
2016-11-03 17:39:40 +00:00
const {ipcRenderer, webFrame} = require('electron')
2016-01-12 02:40:23 +00:00
2016-11-03 17:39:40 +00:00
let requestId = 0
2016-01-12 02:40:23 +00:00
2016-11-03 17:39:40 +00:00
const WEB_VIEW_EVENTS = {
2016-01-12 02:40:23 +00:00
'load-commit': ['url', 'isMainFrame'],
'did-attach': [],
2016-01-12 02:40:23 +00:00
'did-finish-load': [],
'did-fail-load': ['errorCode', 'errorDescription', 'validatedURL', 'isMainFrame'],
2016-01-12 02:40:23 +00:00
'did-frame-finish-load': ['isMainFrame'],
'did-start-loading': [],
'did-stop-loading': [],
'did-get-response-details': ['status', 'newURL', 'originalURL', 'httpResponseCode', 'requestMethod', 'referrer', 'headers', 'resourceType'],
2016-01-12 02:40:23 +00:00
'did-get-redirect-request': ['oldURL', 'newURL', 'isMainFrame'],
'dom-ready': [],
'console-message': ['level', 'message', 'line', 'sourceId'],
'devtools-opened': [],
'devtools-closed': [],
'devtools-focused': [],
'new-window': ['url', 'frameName', 'disposition', 'options'],
'will-navigate': ['url'],
'did-navigate': ['url'],
'did-navigate-in-page': ['url', 'isMainFrame'],
2016-01-12 02:40:23 +00:00
'close': [],
'crashed': [],
'gpu-crashed': [],
'plugin-crashed': ['name', 'version'],
'destroyed': [],
'page-title-updated': ['title', 'explicitSet'],
'page-favicon-updated': ['favicons'],
'enter-html-full-screen': [],
'leave-html-full-screen': [],
2016-06-07 06:56:19 +00:00
'media-started-playing': [],
'media-paused': [],
'found-in-page': ['result'],
'did-change-theme-color': ['themeColor'],
'update-target-url': ['url']
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-11-03 17:39:40 +00:00
const DEPRECATED_EVENTS = {
2016-01-12 02:40:23 +00:00
'page-title-updated': 'page-title-set'
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
2016-11-03 17:39:40 +00:00
const dispatchEvent = function (webView, eventName, eventKey, ...args) {
2016-01-12 02:40:23 +00:00
if (DEPRECATED_EVENTS[eventName] != null) {
2016-11-03 18:09:53 +00:00
dispatchEvent(webView, DEPRECATED_EVENTS[eventName], eventKey, ...args)
2016-01-12 02:40:23 +00:00
}
2016-11-03 17:39:40 +00:00
const domEvent = new Event(eventName)
2016-11-03 18:09:53 +00:00
WEB_VIEW_EVENTS[eventKey].forEach((prop, index) => {
domEvent[prop] = args[index]
})
2016-03-25 19:57:17 +00:00
webView.dispatchEvent(domEvent)
2016-01-12 02:40:23 +00:00
if (eventName === 'load-commit') {
2016-11-03 17:19:52 +00:00
webView.onLoadCommit(domEvent)
2016-01-12 02:40:23 +00:00
}
2016-03-25 19:57:17 +00:00
}
2016-01-12 02:40:23 +00:00
module.exports = {
2016-03-25 19:57:17 +00:00
registerEvents: function (webView, viewInstanceId) {
2016-11-03 17:39:40 +00:00
ipcRenderer.on(`ELECTRON_GUEST_VIEW_INTERNAL_DESTROY_GUEST-${viewInstanceId}`, function () {
webFrame.detachGuest(webView.internalInstanceId)
webView.guestInstanceId = undefined
webView.reset()
2016-11-03 17:39:40 +00:00
const domEvent = new Event('destroyed')
webView.dispatchEvent(domEvent)
})
2016-11-03 17:39:40 +00:00
ipcRenderer.on(`ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-${viewInstanceId}`, function (event, eventName, ...args) {
2016-11-03 18:37:18 +00:00
dispatchEvent(webView, eventName, eventName, ...args)
2016-03-25 19:57:17 +00:00
})
2016-03-18 18:51:02 +00:00
2016-11-03 17:39:40 +00:00
ipcRenderer.on(`ELECTRON_GUEST_VIEW_INTERNAL_IPC_MESSAGE-${viewInstanceId}`, function (event, channel, ...args) {
2016-11-03 17:39:40 +00:00
const domEvent = new Event('ipc-message')
2016-03-25 19:57:17 +00:00
domEvent.channel = channel
domEvent.args = args
2016-05-19 22:28:08 +00:00
webView.dispatchEvent(domEvent)
2016-03-25 19:57:17 +00:00
})
2016-03-18 18:51:02 +00:00
2016-11-03 17:39:40 +00:00
ipcRenderer.on(`ELECTRON_GUEST_VIEW_INTERNAL_SIZE_CHANGED-${viewInstanceId}`, function (event, ...args) {
2016-11-03 17:39:40 +00:00
const domEvent = new Event('size-changed')
const props = ['oldWidth', 'oldHeight', 'newWidth', 'newHeight']
2016-11-03 18:09:53 +00:00
for (let i = 0; i < props.length; i++) {
const prop = props[i]
domEvent[prop] = args[i]
2016-01-12 02:40:23 +00:00
}
2016-05-19 22:28:08 +00:00
webView.onSizeChanged(domEvent)
2016-03-25 19:57:17 +00:00
})
2016-01-12 02:40:23 +00:00
},
2016-03-25 19:57:17 +00:00
deregisterEvents: function (viewInstanceId) {
2016-11-03 17:39:40 +00:00
ipcRenderer.removeAllListeners(`ELECTRON_GUEST_VIEW_INTERNAL_DESTROY_GUEST-${viewInstanceId}`)
ipcRenderer.removeAllListeners(`ELECTRON_GUEST_VIEW_INTERNAL_DISPATCH_EVENT-${viewInstanceId}`)
ipcRenderer.removeAllListeners(`ELECTRON_GUEST_VIEW_INTERNAL_IPC_MESSAGE-${viewInstanceId}`)
ipcRenderer.removeAllListeners(`ELECTRON_GUEST_VIEW_INTERNAL_SIZE_CHANGED-${viewInstanceId}`)
2016-01-12 02:40:23 +00:00
},
2016-03-25 19:57:17 +00:00
createGuest: function (params, callback) {
requestId++
ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_CREATE_GUEST', params, requestId)
2016-11-03 17:39:40 +00:00
ipcRenderer.once(`ELECTRON_RESPONSE_${requestId}`, callback)
2016-01-12 02:40:23 +00:00
},
2016-03-25 19:57:17 +00:00
attachGuest: function (elementInstanceId, guestInstanceId, params) {
ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_ATTACH_GUEST', elementInstanceId, guestInstanceId, params)
2016-11-03 17:19:52 +00:00
webFrame.attachGuest(elementInstanceId)
2016-01-12 02:40:23 +00:00
},
2016-03-25 19:57:17 +00:00
destroyGuest: function (guestInstanceId) {
2016-11-03 17:19:52 +00:00
ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_DESTROY_GUEST', guestInstanceId)
2016-01-12 02:40:23 +00:00
},
2016-03-25 19:57:17 +00:00
setSize: function (guestInstanceId, params) {
2016-11-03 17:19:52 +00:00
ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_SET_SIZE', guestInstanceId, params)
2016-03-28 23:36:08 +00:00
}
2016-03-25 19:57:17 +00:00
}