2016-03-24 13:15:04 -07:00
|
|
|
'use strict'
|
2016-03-21 11:09:29 -07:00
|
|
|
|
2019-02-04 14:49:53 -08:00
|
|
|
const { ipcMainInternal } = require('@electron/internal/browser/ipc-main-internal')
|
2018-12-20 03:44:30 +01:00
|
|
|
|
2018-09-14 02:10:51 +10:00
|
|
|
const { desktopCapturer } = process.atomBinding('desktop_capturer')
|
2018-12-20 03:44:30 +01:00
|
|
|
const eventBinding = process.atomBinding('event')
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2017-10-24 12:49:37 -04:00
|
|
|
const deepEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b)
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// A queue for holding all requests from renderer process.
|
2017-10-24 12:49:37 -04:00
|
|
|
let requestsQueue = []
|
|
|
|
|
|
|
|
const electronSources = 'ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES'
|
|
|
|
const capturerResult = (id) => `ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_${id}`
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2019-02-04 14:49:53 -08:00
|
|
|
ipcMainInternal.on(electronSources, (event, captureWindow, captureScreen, thumbnailSize, fetchWindowIcons, id) => {
|
2018-12-20 03:44:30 +01:00
|
|
|
const customEvent = eventBinding.createWithSender(event.sender)
|
|
|
|
event.sender.emit('desktop-capturer-get-sources', customEvent)
|
|
|
|
|
|
|
|
if (customEvent.defaultPrevented) {
|
2019-01-22 11:24:46 -08:00
|
|
|
event._replyInternal(capturerResult(id), [])
|
2018-12-20 03:44:30 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-24 12:49:37 -04:00
|
|
|
const request = {
|
|
|
|
id,
|
2016-01-11 18:40:23 -08:00
|
|
|
options: {
|
2017-10-24 12:49:37 -04:00
|
|
|
captureWindow,
|
|
|
|
captureScreen,
|
2018-12-03 22:42:49 -08:00
|
|
|
thumbnailSize,
|
|
|
|
fetchWindowIcons
|
2016-01-11 18:40:23 -08:00
|
|
|
},
|
2019-01-22 11:24:46 -08:00
|
|
|
event
|
2016-03-24 13:15:04 -07:00
|
|
|
}
|
|
|
|
requestsQueue.push(request)
|
2016-01-11 18:40:23 -08:00
|
|
|
if (requestsQueue.length === 1) {
|
2018-12-03 22:42:49 -08:00
|
|
|
desktopCapturer.startHandling(captureWindow, captureScreen, thumbnailSize, fetchWindowIcons)
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
|
|
|
|
2016-01-14 10:44:21 -08: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 12:49:37 -04:00
|
|
|
event.sender.once('destroyed', () => {
|
2019-01-22 11:24:46 -08:00
|
|
|
request.event = null
|
2016-03-24 13:15:04 -07:00
|
|
|
})
|
|
|
|
})
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2018-12-03 22:42:49 -08:00
|
|
|
desktopCapturer.emit = (event, name, sources, fetchWindowIcons) => {
|
2016-01-14 10:35:29 -08:00
|
|
|
// Receiving sources result from main process, now send them back to renderer.
|
2017-10-24 15:47:09 -04:00
|
|
|
const handledRequest = requestsQueue.shift()
|
2017-10-24 12:49:37 -04:00
|
|
|
const unhandledRequestsQueue = []
|
|
|
|
|
2017-10-24 15:47:09 -04:00
|
|
|
const result = sources.map(source => {
|
|
|
|
return {
|
|
|
|
id: source.id,
|
|
|
|
name: source.name,
|
2018-04-08 22:43:35 -07:00
|
|
|
thumbnail: source.thumbnail.toDataURL(),
|
2018-12-03 22:42:49 -08:00
|
|
|
display_id: source.display_id,
|
|
|
|
appIcon: (fetchWindowIcons && source.appIcon) ? source.appIcon.toDataURL() : null
|
2017-10-24 15:47:09 -04:00
|
|
|
}
|
|
|
|
})
|
2017-10-24 12:49:37 -04:00
|
|
|
|
2019-01-22 11:24:46 -08:00
|
|
|
if (handledRequest.event) {
|
|
|
|
handledRequest.event._replyInternal(capturerResult(handledRequest.id), result)
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
|
|
|
|
2017-10-24 19:36:06 -04:00
|
|
|
// Check the queue to see whether there is another identical request & handle
|
2017-10-24 12:49:37 -04:00
|
|
|
requestsQueue.forEach(request => {
|
2019-01-22 11:24:46 -08:00
|
|
|
const event = request.event
|
2016-01-11 18:40:23 -08:00
|
|
|
if (deepEqual(handledRequest.options, request.options)) {
|
2019-01-22 11:24:46 -08:00
|
|
|
if (event) {
|
|
|
|
event._replyInternal(capturerResult(request.id), result)
|
2018-10-06 13:48:00 +02:00
|
|
|
}
|
2016-01-11 18:40:23 -08:00
|
|
|
} else {
|
2016-03-24 13:15:04 -07:00
|
|
|
unhandledRequestsQueue.push(request)
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2017-10-24 12:49:37 -04:00
|
|
|
})
|
2016-03-24 13:15:04 -07:00
|
|
|
requestsQueue = unhandledRequestsQueue
|
2016-01-11 18:40:23 -08:00
|
|
|
|
2016-01-14 10:35:29 -08:00
|
|
|
// If the requestsQueue is not empty, start a new request handling.
|
2016-01-11 18:40:23 -08:00
|
|
|
if (requestsQueue.length > 0) {
|
2018-12-03 22:42:49 -08:00
|
|
|
const { captureWindow, captureScreen, thumbnailSize, fetchWindowIcons } = requestsQueue[0].options
|
|
|
|
return desktopCapturer.startHandling(captureWindow, captureScreen, thumbnailSize, fetchWindowIcons)
|
2016-01-11 18:40:23 -08:00
|
|
|
}
|
2016-03-24 13:15:04 -07:00
|
|
|
}
|