2016-03-24 20:15:04 +00:00
|
|
|
'use strict'
|
2016-03-21 18:09:29 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
const ipcMain = require('electron').ipcMain
|
|
|
|
const desktopCapturer = process.atomBinding('desktop_capturer').desktopCapturer
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
var deepEqual = function (opt1, opt2) {
|
|
|
|
return JSON.stringify(opt1) === JSON.stringify(opt2)
|
|
|
|
}
|
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.
|
2016-03-24 20:15:04 +00:00
|
|
|
var requestsQueue = []
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-04-06 23:21:26 +00:00
|
|
|
ipcMain.on('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', function (event, captureWindow, captureScreen, thumbnailSize, id) {
|
2016-03-24 20:15:04 +00:00
|
|
|
var request
|
2016-01-12 02:40:23 +00:00
|
|
|
request = {
|
|
|
|
id: id,
|
|
|
|
options: {
|
|
|
|
captureWindow: captureWindow,
|
|
|
|
captureScreen: captureScreen,
|
|
|
|
thumbnailSize: thumbnailSize
|
|
|
|
},
|
|
|
|
webContents: event.sender
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|
|
|
|
requestsQueue.push(request)
|
2016-01-12 02:40:23 +00:00
|
|
|
if (requestsQueue.length === 1) {
|
2016-03-24 20:15:04 +00:00
|
|
|
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.
|
2016-03-24 20:15:04 +00:00
|
|
|
return event.sender.once('destroyed', function () {
|
2016-03-29 00:40:40 +00:00
|
|
|
request.webContents = null
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
|
2016-03-24 20:15:04 +00:00
|
|
|
desktopCapturer.emit = function (event, name, sources) {
|
2016-01-14 18:35:29 +00:00
|
|
|
// Receiving sources result from main process, now send them back to renderer.
|
2016-03-24 20:15:04 +00:00
|
|
|
var handledRequest, i, len, ref, ref1, request, result, source, unhandledRequestsQueue
|
|
|
|
handledRequest = requestsQueue.shift(0)
|
|
|
|
result = (function () {
|
|
|
|
var i, len, results
|
|
|
|
results = []
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = 0, len = sources.length; i < len; i++) {
|
2016-03-24 20:15:04 +00:00
|
|
|
source = sources[i]
|
2016-01-12 02:40:23 +00:00
|
|
|
results.push({
|
|
|
|
id: source.id,
|
|
|
|
name: source.name,
|
2016-05-03 00:16:39 +00:00
|
|
|
thumbnail: source.thumbnail.toDataURL()
|
2016-03-24 20:15:04 +00:00
|
|
|
})
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
return results
|
|
|
|
})()
|
2016-01-12 02:40:23 +00:00
|
|
|
if ((ref = handledRequest.webContents) != null) {
|
2016-04-06 23:21:26 +00:00
|
|
|
ref.send('ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_' + 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.
|
2016-03-24 20:15:04 +00:00
|
|
|
unhandledRequestsQueue = []
|
2016-01-12 02:40:23 +00:00
|
|
|
for (i = 0, len = requestsQueue.length; i < len; i++) {
|
2016-03-24 20:15:04 +00:00
|
|
|
request = requestsQueue[i]
|
2016-01-12 02:40:23 +00:00
|
|
|
if (deepEqual(handledRequest.options, request.options)) {
|
|
|
|
if ((ref1 = request.webContents) != null) {
|
2016-04-06 23:21:26 +00:00
|
|
|
ref1.send('ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_' + request.id, result)
|
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
|
|
|
}
|
|
|
|
}
|
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) {
|
2016-03-24 20:15:04 +00:00
|
|
|
const {captureWindow, captureScreen, thumbnailSize} = requestsQueue[0].options
|
|
|
|
return desktopCapturer.startHandling(captureWindow, captureScreen, thumbnailSize)
|
2016-01-12 02:40:23 +00:00
|
|
|
}
|
2016-03-24 20:15:04 +00:00
|
|
|
}
|