2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-03-21 18:09:29 +00:00
|
|
|
|
2019-03-07 23:31:25 +00:00
|
|
|
const ipcMainUtils = require('@electron/internal/browser/ipc-main-internal-utils')
|
2018-12-20 02:44:30 +00:00
|
|
|
|
2019-03-18 19:37:06 +00:00
|
|
|
const { desktopCapturer } = process.electronBinding('desktop_capturer')
|
|
|
|
const eventBinding = process.electronBinding('event')
|
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 = []
|
|
|
|
|
2019-03-07 23:31:25 +00:00
|
|
|
ipcMainUtils.handle('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', (event, captureWindow, captureScreen, thumbnailSize, fetchWindowIcons) => {
|
2018-12-20 02:44:30 +00:00
|
|
|
const customEvent = eventBinding.createWithSender(event.sender)
|
|
|
|
event.sender.emit('desktop-capturer-get-sources', customEvent)
|
|
|
|
|
|
|
|
if (customEvent.defaultPrevented) {
|
2019-03-07 23:31:25 +00:00
|
|
|
return []
|
2018-12-20 02:44:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-07 23:31:25 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const request = {
|
|
|
|
options: {
|
|
|
|
captureWindow,
|
|
|
|
captureScreen,
|
|
|
|
thumbnailSize,
|
|
|
|
fetchWindowIcons
|
|
|
|
},
|
2019-04-25 22:12:38 +00:00
|
|
|
resolve,
|
|
|
|
reject
|
2019-03-07 23:31:25 +00:00
|
|
|
}
|
|
|
|
requestsQueue.push(request)
|
|
|
|
if (requestsQueue.length === 1) {
|
|
|
|
desktopCapturer.startHandling(captureWindow, captureScreen, thumbnailSize, fetchWindowIcons)
|
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2019-03-07 23:31:25 +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.
|
|
|
|
event.sender.once('destroyed', () => {
|
|
|
|
request.resolve = null
|
|
|
|
})
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2018-12-04 06:42:49 +00:00
|
|
|
desktopCapturer.emit = (event, name, sources, fetchWindowIcons) => {
|
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 unhandledRequestsQueue = []
|
|
|
|
|
2019-04-25 22:12:38 +00:00
|
|
|
if (name === 'error') {
|
|
|
|
const error = sources
|
|
|
|
handledRequest.reject(error)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-24 19:47:09 +00:00
|
|
|
const result = sources.map(source => {
|
|
|
|
return {
|
|
|
|
id: source.id,
|
|
|
|
name: source.name,
|
2018-04-09 05:43:35 +00:00
|
|
|
thumbnail: source.thumbnail.toDataURL(),
|
2018-12-04 06:42:49 +00:00
|
|
|
display_id: source.display_id,
|
|
|
|
appIcon: (fetchWindowIcons && source.appIcon) ? source.appIcon.toDataURL() : null
|
2017-10-24 19:47:09 +00:00
|
|
|
}
|
|
|
|
})
|
2017-10-24 16:49:37 +00:00
|
|
|
|
2019-03-07 23:31:25 +00:00
|
|
|
if (handledRequest.resolve) {
|
|
|
|
handledRequest.resolve(result)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 23:36:06 +00:00
|
|
|
// Check the queue to see whether there is another identical request & handle
|
2017-10-24 16:49:37 +00:00
|
|
|
requestsQueue.forEach(request => {
|
2016-01-12 02:40:23 +00:00
|
|
|
if (deepEqual(handledRequest.options, request.options)) {
|
2019-03-07 23:31:25 +00:00
|
|
|
if (request.resolve) {
|
|
|
|
request.resolve(result)
|
2018-10-06 11:48:00 +00:00
|
|
|
}
|
2016-01-12 02:40:23 +00:00
|
|
|
} else {
|
2016-03-24 20:15:04 +00:00
|
|
|
unhandledRequestsQueue.push(request)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2017-10-24 16:49:37 +00:00
|
|
|
})
|
2016-03-24 20:15:04 +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) {
|
2018-12-04 06:42:49 +00:00
|
|
|
const { captureWindow, captureScreen, thumbnailSize, fetchWindowIcons } = requestsQueue[0].options
|
|
|
|
return desktopCapturer.startHandling(captureWindow, captureScreen, thumbnailSize, fetchWindowIcons)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|