electron/lib/browser/desktop-capturer.js

76 lines
2.3 KiB
JavaScript
Raw Normal View History

'use strict'
2016-03-21 18:09:29 +00:00
2017-10-24 16:49:37 +00:00
const {ipcMain} = require('electron')
const {desktopCapturer} = process.atomBinding('desktop_capturer')
2016-01-12 02:40:23 +00:00
2017-10-24 16:49:37 +00:00
const deepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b)
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// A queue for holding all requests from renderer process.
2017-10-24 16:49:37 +00:00
let requestsQueue = []
const electronSources = 'ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES'
const capturerResult = (id) => `ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_${id}`
2016-01-12 02:40:23 +00:00
2017-10-24 16:49:37 +00:00
ipcMain.on(electronSources, (event, captureWindow, captureScreen, thumbnailSize, id) => {
const request = {
id,
2016-01-12 02:40:23 +00:00
options: {
2017-10-24 16:49:37 +00:00
captureWindow,
captureScreen,
thumbnailSize
2016-01-12 02:40:23 +00:00
},
webContents: event.sender
}
requestsQueue.push(request)
2016-01-12 02:40:23 +00:00
if (requestsQueue.length === 1) {
desktopCapturer.startHandling(captureWindow, captureScreen, thumbnailSize)
2016-01-12 02:40:23 +00:00
}
2016-01-14 18:44:21 +00:00
// If the WebContents is destroyed before receiving result, just remove the
// reference from requestsQueue to make the module not send the result to it.
2017-10-24 16:49:37 +00:00
event.sender.once('destroyed', () => {
2016-03-29 00:40:40 +00:00
request.webContents = null
2017-10-24 16:49:37 +00:00
return
})
})
2016-01-12 02:40:23 +00:00
2017-10-24 16:49:37 +00:00
desktopCapturer.emit = (event, name, sources) => {
2016-01-14 18:35:29 +00:00
// Receiving sources result from main process, now send them back to renderer.
2017-10-24 19:47:09 +00:00
const handledRequest = requestsQueue.shift()
2017-10-24 16:49:37 +00:00
const handledWebContents = handledRequest.webContents
const unhandledRequestsQueue = []
2017-10-24 19:47:09 +00:00
const result = sources.map(source => {
return {
id: source.id,
name: source.name,
thumbnail: source.thumbnail.toDataURL()
}
})
2017-10-24 16:49:37 +00:00
if (handledWebContents != null) {
2017-10-24 19:47:09 +00:00
handledWebContents.send(capturerResult(handledRequest.id), result)
2016-01-12 02:40:23 +00:00
}
2016-01-14 18:44:21 +00:00
// Check the queue to see whether there is other same request. If has, handle
// it for reducing redunplicated `desktopCaptuer.startHandling` calls.
2017-10-24 16:49:37 +00:00
requestsQueue.forEach(request => {
const webContents = request.webContents
2016-01-12 02:40:23 +00:00
if (deepEqual(handledRequest.options, request.options)) {
2017-10-24 16:49:37 +00:00
if (webContents != null) {
2017-10-24 19:47:09 +00:00
webContents.send(capturerResult(request.id), result)
2016-01-12 02:40:23 +00:00
}
} else {
unhandledRequestsQueue.push(request)
2016-01-12 02:40:23 +00:00
}
2017-10-24 16:49:37 +00:00
})
requestsQueue = unhandledRequestsQueue
2016-01-12 02:40:23 +00:00
2016-01-14 18:35:29 +00:00
// If the requestsQueue is not empty, start a new request handling.
2016-01-12 02:40:23 +00:00
if (requestsQueue.length > 0) {
const {captureWindow, captureScreen, thumbnailSize} = requestsQueue[0].options
return desktopCapturer.startHandling(captureWindow, captureScreen, thumbnailSize)
2016-01-12 02:40:23 +00:00
}
}