refactor: use ipcRendererUtils.invoke / ipcMainUtils.handle for desktopCapturer.getSources() (#16619)

This commit is contained in:
Milan Burda 2019-03-08 00:31:25 +01:00 committed by Samuel Attard
parent ac88b3ead5
commit 5791a2a9ec
2 changed files with 31 additions and 50 deletions

View file

@ -1,6 +1,6 @@
'use strict' 'use strict'
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal') const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
const { desktopCapturer } = process.atomBinding('desktop_capturer') const { desktopCapturer } = process.atomBinding('desktop_capturer')
const eventBinding = process.atomBinding('event') const eventBinding = process.atomBinding('event')
@ -10,27 +10,23 @@ const deepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b)
// A queue for holding all requests from renderer process. // A queue for holding all requests from renderer process.
let requestsQueue = [] let requestsQueue = []
const electronSources = 'ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES' ipcMainUtils.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', (event, captureWindow, captureScreen, thumbnailSize, fetchWindowIcons) => {
const capturerResult = (id) => `ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_${id}`
ipcMainInternal.on(electronSources, (event, captureWindow, captureScreen, thumbnailSize, fetchWindowIcons, id) => {
const customEvent = eventBinding.createWithSender(event.sender) const customEvent = eventBinding.createWithSender(event.sender)
event.sender.emit('desktop-capturer-get-sources', customEvent) event.sender.emit('desktop-capturer-get-sources', customEvent)
if (customEvent.defaultPrevented) { if (customEvent.defaultPrevented) {
event._replyInternal(capturerResult(id), []) return []
return
} }
return new Promise((resolve, reject) => {
const request = { const request = {
id,
options: { options: {
captureWindow, captureWindow,
captureScreen, captureScreen,
thumbnailSize, thumbnailSize,
fetchWindowIcons fetchWindowIcons
}, },
event resolve
} }
requestsQueue.push(request) requestsQueue.push(request)
if (requestsQueue.length === 1) { if (requestsQueue.length === 1) {
@ -40,7 +36,8 @@ ipcMainInternal.on(electronSources, (event, captureWindow, captureScreen, thumbn
// If the WebContents is destroyed before receiving result, just remove the // If the WebContents is destroyed before receiving result, just remove the
// reference from requestsQueue to make the module not send the result to it. // reference from requestsQueue to make the module not send the result to it.
event.sender.once('destroyed', () => { event.sender.once('destroyed', () => {
request.event = null request.resolve = null
})
}) })
}) })
@ -59,16 +56,15 @@ desktopCapturer.emit = (event, name, sources, fetchWindowIcons) => {
} }
}) })
if (handledRequest.event) { if (handledRequest.resolve) {
handledRequest.event._replyInternal(capturerResult(handledRequest.id), result) handledRequest.resolve(result)
} }
// Check the queue to see whether there is another identical request & handle // Check the queue to see whether there is another identical request & handle
requestsQueue.forEach(request => { requestsQueue.forEach(request => {
const event = request.event
if (deepEqual(handledRequest.options, request.options)) { if (deepEqual(handledRequest.options, request.options)) {
if (event) { if (request.resolve) {
event._replyInternal(capturerResult(request.id), result) request.resolve(result)
} }
} else { } else {
unhandledRequestsQueue.push(request) unhandledRequestsQueue.push(request)

View file

@ -1,15 +1,7 @@
'use strict' 'use strict'
const { nativeImage, deprecate } = require('electron') const { nativeImage, deprecate } = require('electron')
const { ipcRendererInternal } = require('@electron/internal/renderer/ipc-renderer-internal') const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils')
const includes = [].includes
let currentId = 0
const incrementId = () => {
currentId += 1
return currentId
}
// |options.types| can't be empty and must be an array // |options.types| can't be empty and must be an array
function isValid (options) { function isValid (options) {
@ -31,8 +23,8 @@ const getSources = (options) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!isValid(options)) throw new Error('Invalid options') if (!isValid(options)) throw new Error('Invalid options')
const captureWindow = includes.call(options.types, 'window') const captureWindow = options.types.includes('window')
const captureScreen = includes.call(options.types, 'screen') const captureScreen = options.types.includes('screen')
if (options.thumbnailSize == null) { if (options.thumbnailSize == null) {
options.thumbnailSize = { options.thumbnailSize = {
@ -44,15 +36,8 @@ const getSources = (options) => {
options.fetchWindowIcons = false options.fetchWindowIcons = false
} }
const id = incrementId() ipcRendererUtils.invoke('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', captureWindow, captureScreen, options.thumbnailSize, options.fetchWindowIcons)
ipcRendererInternal.send('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', captureWindow, captureScreen, options.thumbnailSize, options.fetchWindowIcons, id) .then(sources => resolve(mapSources(sources)), reject)
return ipcRendererInternal.once(`ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_${id}`, (event, sources) => {
try {
resolve(mapSources(sources))
} catch (error) {
reject(error)
}
})
}) })
} }