refactor: add emitCustomEvent() helper (#17960)
This commit is contained in:
parent
aebad6fd21
commit
6f5c850d60
5 changed files with 35 additions and 48 deletions
|
@ -350,22 +350,6 @@ WebContents.prototype._init = function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
const forwardedEvents = [
|
|
||||||
'desktop-capturer-get-sources',
|
|
||||||
'remote-require',
|
|
||||||
'remote-get-global',
|
|
||||||
'remote-get-builtin',
|
|
||||||
'remote-get-current-window',
|
|
||||||
'remote-get-current-web-contents',
|
|
||||||
'remote-get-guest-web-contents'
|
|
||||||
]
|
|
||||||
|
|
||||||
for (const eventName of forwardedEvents) {
|
|
||||||
this.on(eventName, (event, ...args) => {
|
|
||||||
app.emit(eventName, event, this, ...args)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
this.on('crashed', (event, ...args) => {
|
this.on('crashed', (event, ...args) => {
|
||||||
app.emit('renderer-process-crashed', event, this, ...args)
|
app.emit('renderer-process-crashed', event, this, ...args)
|
||||||
})
|
})
|
||||||
|
|
|
@ -1,22 +1,12 @@
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
|
|
||||||
|
|
||||||
const { createDesktopCapturer } = process.electronBinding('desktop_capturer')
|
const { createDesktopCapturer } = process.electronBinding('desktop_capturer')
|
||||||
const eventBinding = process.electronBinding('event')
|
|
||||||
|
|
||||||
const deepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b)
|
const deepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b)
|
||||||
|
|
||||||
let currentlyRunning = []
|
let currentlyRunning = []
|
||||||
|
|
||||||
ipcMainUtils.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', (event, captureWindow, captureScreen, thumbnailSize, fetchWindowIcons) => {
|
exports.getSources = (event, captureWindow, captureScreen, thumbnailSize, fetchWindowIcons) => {
|
||||||
const customEvent = eventBinding.createWithSender(event.sender)
|
|
||||||
event.sender.emit('desktop-capturer-get-sources', customEvent)
|
|
||||||
|
|
||||||
if (customEvent.defaultPrevented) {
|
|
||||||
return []
|
|
||||||
}
|
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
captureWindow,
|
captureWindow,
|
||||||
captureScreen,
|
captureScreen,
|
||||||
|
@ -69,7 +59,7 @@ ipcMainUtils.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', (event, cap
|
||||||
})
|
})
|
||||||
|
|
||||||
return getSources
|
return getSources
|
||||||
})
|
}
|
||||||
|
|
||||||
const createCapturerEmitHandler = (capturer, request) => {
|
const createCapturerEmitHandler = (capturer, request) => {
|
||||||
return function handlEmitOnCapturer (event, name, sources, fetchWindowIcons) {
|
return function handlEmitOnCapturer (event, name, sources, fetchWindowIcons) {
|
||||||
|
|
|
@ -44,7 +44,7 @@ if (process.platform === 'win32') {
|
||||||
// Don't quit on fatal error.
|
// Don't quit on fatal error.
|
||||||
process.on('uncaughtException', function (error) {
|
process.on('uncaughtException', function (error) {
|
||||||
// Do nothing if the user has a custom uncaught exception handler.
|
// Do nothing if the user has a custom uncaught exception handler.
|
||||||
if (process.listeners('uncaughtException').length > 1) {
|
if (process.listenerCount('uncaughtException') > 1) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -159,12 +159,6 @@ require('@electron/internal/browser/chrome-devtools')
|
||||||
// Load the chrome extension support.
|
// Load the chrome extension support.
|
||||||
require('@electron/internal/browser/chrome-extension')
|
require('@electron/internal/browser/chrome-extension')
|
||||||
|
|
||||||
const features = process.electronBinding('features')
|
|
||||||
if (features.isDesktopCapturerEnabled()) {
|
|
||||||
// Load internal desktop-capturer module.
|
|
||||||
require('@electron/internal/browser/desktop-capturer')
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load protocol module to ensure it is populated on app ready
|
// Load protocol module to ensure it is populated on app ready
|
||||||
require('@electron/internal/browser/api/protocol')
|
require('@electron/internal/browser/api/protocol')
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ const path = require('path')
|
||||||
const v8Util = process.electronBinding('v8_util')
|
const v8Util = process.electronBinding('v8_util')
|
||||||
const eventBinding = process.electronBinding('event')
|
const eventBinding = process.electronBinding('event')
|
||||||
const clipboard = process.electronBinding('clipboard')
|
const clipboard = process.electronBinding('clipboard')
|
||||||
|
const features = process.electronBinding('features')
|
||||||
|
|
||||||
const { isPromise } = electron
|
const { isPromise } = electron
|
||||||
|
|
||||||
|
@ -273,6 +274,15 @@ const handleRemoteCommand = function (channel, handler) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const emitCustomEvent = function (contents, eventName, ...args) {
|
||||||
|
const event = eventBinding.createWithSender(contents)
|
||||||
|
|
||||||
|
electron.app.emit(eventName, event, contents, ...args)
|
||||||
|
contents.emit(eventName, event, ...args)
|
||||||
|
|
||||||
|
return event
|
||||||
|
}
|
||||||
|
|
||||||
handleRemoteCommand('ELECTRON_BROWSER_WRONG_CONTEXT_ERROR', function (event, contextId, passedContextId, id) {
|
handleRemoteCommand('ELECTRON_BROWSER_WRONG_CONTEXT_ERROR', function (event, contextId, passedContextId, id) {
|
||||||
const objectId = [passedContextId, id]
|
const objectId = [passedContextId, id]
|
||||||
if (!rendererFunctions.has(objectId)) {
|
if (!rendererFunctions.has(objectId)) {
|
||||||
|
@ -283,8 +293,7 @@ handleRemoteCommand('ELECTRON_BROWSER_WRONG_CONTEXT_ERROR', function (event, con
|
||||||
})
|
})
|
||||||
|
|
||||||
handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, moduleName) {
|
handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, moduleName) {
|
||||||
const customEvent = eventBinding.createWithSender(event.sender)
|
const customEvent = emitCustomEvent(event.sender, 'remote-require', moduleName)
|
||||||
event.sender.emit('remote-require', customEvent, moduleName)
|
|
||||||
|
|
||||||
if (customEvent.returnValue === undefined) {
|
if (customEvent.returnValue === undefined) {
|
||||||
if (customEvent.defaultPrevented) {
|
if (customEvent.defaultPrevented) {
|
||||||
|
@ -298,8 +307,7 @@ handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, modu
|
||||||
})
|
})
|
||||||
|
|
||||||
handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId, moduleName) {
|
handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId, moduleName) {
|
||||||
const customEvent = eventBinding.createWithSender(event.sender)
|
const customEvent = emitCustomEvent(event.sender, 'remote-get-builtin', moduleName)
|
||||||
event.sender.emit('remote-get-builtin', customEvent, moduleName)
|
|
||||||
|
|
||||||
if (customEvent.returnValue === undefined) {
|
if (customEvent.returnValue === undefined) {
|
||||||
if (customEvent.defaultPrevented) {
|
if (customEvent.defaultPrevented) {
|
||||||
|
@ -313,8 +321,7 @@ handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId,
|
||||||
})
|
})
|
||||||
|
|
||||||
handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globalName) {
|
handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globalName) {
|
||||||
const customEvent = eventBinding.createWithSender(event.sender)
|
const customEvent = emitCustomEvent(event.sender, 'remote-get-global', globalName)
|
||||||
event.sender.emit('remote-get-global', customEvent, globalName)
|
|
||||||
|
|
||||||
if (customEvent.returnValue === undefined) {
|
if (customEvent.returnValue === undefined) {
|
||||||
if (customEvent.defaultPrevented) {
|
if (customEvent.defaultPrevented) {
|
||||||
|
@ -328,8 +335,7 @@ handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globa
|
||||||
})
|
})
|
||||||
|
|
||||||
handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextId) {
|
handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextId) {
|
||||||
const customEvent = eventBinding.createWithSender(event.sender)
|
const customEvent = emitCustomEvent(event.sender, 'remote-get-current-window')
|
||||||
event.sender.emit('remote-get-current-window', customEvent)
|
|
||||||
|
|
||||||
if (customEvent.returnValue === undefined) {
|
if (customEvent.returnValue === undefined) {
|
||||||
if (customEvent.defaultPrevented) {
|
if (customEvent.defaultPrevented) {
|
||||||
|
@ -343,8 +349,7 @@ handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextI
|
||||||
})
|
})
|
||||||
|
|
||||||
handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS', function (event, contextId) {
|
handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS', function (event, contextId) {
|
||||||
const customEvent = eventBinding.createWithSender(event.sender)
|
const customEvent = emitCustomEvent(event.sender, 'remote-get-current-web-contents')
|
||||||
event.sender.emit('remote-get-current-web-contents', customEvent)
|
|
||||||
|
|
||||||
if (customEvent.returnValue === undefined) {
|
if (customEvent.returnValue === undefined) {
|
||||||
if (customEvent.defaultPrevented) {
|
if (customEvent.defaultPrevented) {
|
||||||
|
@ -447,8 +452,7 @@ handleRemoteCommand('ELECTRON_BROWSER_CONTEXT_RELEASE', (event, contextId) => {
|
||||||
handleRemoteCommand('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId) {
|
handleRemoteCommand('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId) {
|
||||||
const guest = guestViewManager.getGuestForWebContents(guestInstanceId, event.sender)
|
const guest = guestViewManager.getGuestForWebContents(guestInstanceId, event.sender)
|
||||||
|
|
||||||
const customEvent = eventBinding.createWithSender(event.sender)
|
const customEvent = emitCustomEvent(event.sender, 'remote-get-guest-web-contents', guest)
|
||||||
event.sender.emit('remote-get-guest-web-contents', customEvent, guest)
|
|
||||||
|
|
||||||
if (customEvent.returnValue === undefined) {
|
if (customEvent.returnValue === undefined) {
|
||||||
if (customEvent.defaultPrevented) {
|
if (customEvent.defaultPrevented) {
|
||||||
|
@ -498,6 +502,21 @@ ipcMainUtils.handle('ELECTRON_BROWSER_CLIPBOARD', function (event, method, ...ar
|
||||||
return clipboardUtils.serialize(electron.clipboard[method](...clipboardUtils.deserialize(args)))
|
return clipboardUtils.serialize(electron.clipboard[method](...clipboardUtils.deserialize(args)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (features.isDesktopCapturerEnabled()) {
|
||||||
|
const desktopCapturer = require('@electron/internal/browser/desktop-capturer')
|
||||||
|
|
||||||
|
ipcMainUtils.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', function (event, ...args) {
|
||||||
|
const customEvent = emitCustomEvent(event.sender, 'desktop-capturer-get-sources')
|
||||||
|
|
||||||
|
if (customEvent.defaultPrevented) {
|
||||||
|
console.error('Blocked desktopCapturer.getSources()')
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
|
||||||
|
return desktopCapturer.getSources(event, ...args)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let absoluteAppPath
|
let absoluteAppPath
|
||||||
const getAppPath = async function () {
|
const getAppPath = async function () {
|
||||||
if (absoluteAppPath === undefined) {
|
if (absoluteAppPath === undefined) {
|
||||||
|
|
|
@ -166,7 +166,7 @@ if (nodeIntegration) {
|
||||||
|
|
||||||
// Redirect window.onerror to uncaughtException.
|
// Redirect window.onerror to uncaughtException.
|
||||||
window.onerror = function (_message, _filename, _lineno, _colno, error) {
|
window.onerror = function (_message, _filename, _lineno, _colno, error) {
|
||||||
if (global.process.listeners('uncaughtException').length > 0) {
|
if (global.process.listenerCount('uncaughtException') > 0) {
|
||||||
// We do not want to add `uncaughtException` to our definitions
|
// We do not want to add `uncaughtException` to our definitions
|
||||||
// because we don't want anyone else (anywhere) to throw that kind
|
// because we don't want anyone else (anywhere) to throw that kind
|
||||||
// of error.
|
// of error.
|
||||||
|
|
Loading…
Reference in a new issue