2016-03-25 19:57:17 +00:00
|
|
|
'use strict'
|
2016-03-18 18:51:02 +00:00
|
|
|
|
2018-10-06 11:48:00 +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'],
|
2016-09-08 17:01:01 +00:00
|
|
|
'did-attach': [],
|
2016-01-12 02:40:23 +00:00
|
|
|
'did-finish-load': [],
|
2018-04-26 10:17:55 +00:00
|
|
|
'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'],
|
2017-01-13 22:04:09 +00:00
|
|
|
'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'],
|
2018-04-26 10:17:55 +00:00
|
|
|
'did-start-navigation': ['url', 'isInPlace', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
|
2018-05-01 04:34:41 +00:00
|
|
|
'did-navigate': ['url', 'httpResponseCode', 'httpStatusText'],
|
|
|
|
'did-frame-navigate': ['url', 'httpResponseCode', 'httpStatusText', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
|
2018-04-26 10:17:55 +00:00
|
|
|
'did-navigate-in-page': ['url', 'isMainFrame', 'frameProcessId', 'frameRoutingId'],
|
2018-08-28 18:35:44 +00:00
|
|
|
'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)
|
2018-08-28 18:35:44 +00:00
|
|
|
} 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 () {
|
2016-09-08 17:01:01 +00:00
|
|
|
webView.guestInstanceId = undefined
|
|
|
|
webView.reset()
|
2016-11-03 17:39:40 +00:00
|
|
|
const domEvent = new Event('destroyed')
|
2016-09-08 17:01:01 +00:00
|
|
|
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++
|
2016-04-06 23:21:26 +00:00
|
|
|
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
|
|
|
},
|
2018-07-21 02:11:28 +00:00
|
|
|
createGuestSync: function (params) {
|
|
|
|
return ipcRenderer.sendSync('ELECTRON_GUEST_VIEW_MANAGER_CREATE_GUEST_SYNC', params)
|
|
|
|
},
|
2018-09-07 06:41:48 +00:00
|
|
|
destroyGuest: function (guestInstanceId) {
|
|
|
|
ipcRenderer.send('ELECTRON_GUEST_VIEW_MANAGER_DESTROY_GUEST', guestInstanceId)
|
|
|
|
},
|
2018-08-16 22:57:40 +00:00
|
|
|
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.
|
2018-08-16 22:57:40 +00:00
|
|
|
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
|
|
|
}
|