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

109 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
const { webFrame } = require('electron')
const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
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', 'frameProcessId', 'frameRoutingId'],
'did-frame-finish-load': ['isMainFrame', 'frameProcessId', 'frameRoutingId'],
2016-01-12 02:40:23 +00:00
'did-start-loading': [],
'did-stop-loading': [],
'dom-ready': [],
'console-message': ['level', 'message', 'line', 'sourceId'],
'context-menu': ['params'],
2016-01-12 02:40:23 +00:00
'devtools-opened': [],
'devtools-closed': [],
'devtools-focused': [],
'new-window': ['url', 'frameName', 'disposition', 'options'],
'will-navigate': ['url'],
'did-start-navigation': ['url', 'isInPlace', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
'did-navigate': ['url', 'httpResponseCode', 'httpStatusText'],
'did-frame-navigate': ['url', 'httpResponseCode', 'httpStatusText', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
'did-navigate-in-page': ['url', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
'focus-change': ['focus', 'guestInstanceId'],
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)
} else if (eventName === 'focus-change') {
webView.onFocusChange(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 () {
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-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}`)
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
},
createGuestSync: function (params) {
return ipcRenderer.sendSync('ELECTRON_GUEST_VIEW_MANAGER_CREATE_GUEST_SYNC', params)
},
destroyGuest: function (guestInstanceId) {
ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_DESTROY_GUEST', guestInstanceId)
},
attachGuest: function (elementInstanceId, guestInstanceId, params, contentWindow) {
const embedderFrameId = webFrame.getWebFrameId(contentWindow)
2018-09-13 16:10:51 +00:00
if (embedderFrameId < 0) { // this error should not happen.
throw new Error('Invalid embedder frame')
}
ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_ATTACH_GUEST', embedderFrameId, elementInstanceId, guestInstanceId, params)
2016-03-28 23:36:08 +00:00
}
2016-03-25 19:57:17 +00:00
}