refactor: add emitCustomEvent() helper (#17960)

This commit is contained in:
Milan Burda 2019-05-01 15:07:57 +02:00 committed by Alexey Kuzmin
parent aebad6fd21
commit 6f5c850d60
5 changed files with 35 additions and 48 deletions

View file

@ -8,6 +8,7 @@ const path = require('path')
const v8Util = process.electronBinding('v8_util')
const eventBinding = process.electronBinding('event')
const clipboard = process.electronBinding('clipboard')
const features = process.electronBinding('features')
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) {
const objectId = [passedContextId, id]
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) {
const customEvent = eventBinding.createWithSender(event.sender)
event.sender.emit('remote-require', customEvent, moduleName)
const customEvent = emitCustomEvent(event.sender, 'remote-require', moduleName)
if (customEvent.returnValue === undefined) {
if (customEvent.defaultPrevented) {
@ -298,8 +307,7 @@ handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, modu
})
handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId, moduleName) {
const customEvent = eventBinding.createWithSender(event.sender)
event.sender.emit('remote-get-builtin', customEvent, moduleName)
const customEvent = emitCustomEvent(event.sender, 'remote-get-builtin', moduleName)
if (customEvent.returnValue === undefined) {
if (customEvent.defaultPrevented) {
@ -313,8 +321,7 @@ handleRemoteCommand('ELECTRON_BROWSER_GET_BUILTIN', function (event, contextId,
})
handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globalName) {
const customEvent = eventBinding.createWithSender(event.sender)
event.sender.emit('remote-get-global', customEvent, globalName)
const customEvent = emitCustomEvent(event.sender, 'remote-get-global', globalName)
if (customEvent.returnValue === undefined) {
if (customEvent.defaultPrevented) {
@ -328,8 +335,7 @@ handleRemoteCommand('ELECTRON_BROWSER_GLOBAL', function (event, contextId, globa
})
handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextId) {
const customEvent = eventBinding.createWithSender(event.sender)
event.sender.emit('remote-get-current-window', customEvent)
const customEvent = emitCustomEvent(event.sender, 'remote-get-current-window')
if (customEvent.returnValue === undefined) {
if (customEvent.defaultPrevented) {
@ -343,8 +349,7 @@ handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WINDOW', function (event, contextI
})
handleRemoteCommand('ELECTRON_BROWSER_CURRENT_WEB_CONTENTS', function (event, contextId) {
const customEvent = eventBinding.createWithSender(event.sender)
event.sender.emit('remote-get-current-web-contents', customEvent)
const customEvent = emitCustomEvent(event.sender, 'remote-get-current-web-contents')
if (customEvent.returnValue === undefined) {
if (customEvent.defaultPrevented) {
@ -447,8 +452,7 @@ handleRemoteCommand('ELECTRON_BROWSER_CONTEXT_RELEASE', (event, contextId) => {
handleRemoteCommand('ELECTRON_BROWSER_GUEST_WEB_CONTENTS', function (event, contextId, guestInstanceId) {
const guest = guestViewManager.getGuestForWebContents(guestInstanceId, event.sender)
const customEvent = eventBinding.createWithSender(event.sender)
event.sender.emit('remote-get-guest-web-contents', customEvent, guest)
const customEvent = emitCustomEvent(event.sender, 'remote-get-guest-web-contents', guest)
if (customEvent.returnValue === undefined) {
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)))
})
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
const getAppPath = async function () {
if (absoluteAppPath === undefined) {