Skip to content

Commit

Permalink
return empty instead of error
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Dec 18, 2018
1 parent ff35307 commit be02125
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/api/app.md
Expand Up @@ -409,7 +409,7 @@ Returns:
* `webContents` [WebContents](web-contents.md)

Emitted when `desktopCapturer.getSources()` is called in the renderer process of `webContents`.
Calling `event.preventDefault()` will make it throw an error.
Calling `event.preventDefault()` will make it return empty sources.

### Event: 'remote-require'

Expand Down
2 changes: 1 addition & 1 deletion docs/api/web-contents.md
Expand Up @@ -670,7 +670,7 @@ Returns:
* `event` Event

Emitted when `desktopCapturer.getSources()` is called in the renderer process.
Calling `event.preventDefault()` will make it throw an error.
Calling `event.preventDefault()` will make it return empty sources.

#### Event: 'remote-require'

Expand Down
8 changes: 3 additions & 5 deletions lib/browser/desktop-capturer.js
@@ -1,7 +1,6 @@
'use strict'

const ipcMain = require('@electron/internal/browser/ipc-main-internal')
const errorUtils = require('@electron/internal/common/error-utils')

const { desktopCapturer } = process.atomBinding('desktop_capturer')
const eventBinding = process.atomBinding('event')
Expand All @@ -19,8 +18,7 @@ ipcMain.on(electronSources, (event, captureWindow, captureScreen, thumbnailSize,
event.sender.emit('desktop-capturer-get-sources', customEvent)

if (customEvent.defaultPrevented) {
const error = new Error('desktopCapturer.getSources() blocked')
event.sender._sendInternal(capturerResult(id), errorUtils.serialize(error))
event.sender._sendInternal(capturerResult(id), [])
return
}

Expand Down Expand Up @@ -63,15 +61,15 @@ desktopCapturer.emit = (event, name, sources, fetchWindowIcons) => {
})

if (handledWebContents) {
handledWebContents._sendInternal(capturerResult(handledRequest.id), null, result)
handledWebContents._sendInternal(capturerResult(handledRequest.id), result)
}

// Check the queue to see whether there is another identical request & handle
requestsQueue.forEach(request => {
const webContents = request.webContents
if (deepEqual(handledRequest.options, request.options)) {
if (webContents) {
webContents._sendInternal(capturerResult(request.id), null, result)
webContents._sendInternal(capturerResult(request.id), result)
}
} else {
unhandledRequestsQueue.push(request)
Expand Down
10 changes: 4 additions & 6 deletions lib/renderer/api/desktop-capturer.js
@@ -1,9 +1,7 @@
'use strict'

const { nativeImage } = require('electron')

const ipcRenderer = require('@electron/internal/renderer/ipc-renderer-internal')
const errorUtils = require('@electron/internal/common/error-utils')

const includes = [].includes
let currentId = 0
Expand Down Expand Up @@ -46,11 +44,11 @@ exports.getSources = function (options, callback) {

const id = incrementId()
ipcRenderer.send('ELECTRON_BROWSER_DESKTOP_CAPTURER_GET_SOURCES', captureWindow, captureScreen, options.thumbnailSize, options.fetchWindowIcons, id)
return ipcRenderer.once(`ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_${id}`, (event, error, sources) => {
if (error) {
callback(errorUtils.deserialize(error))
} else {
return ipcRenderer.once(`ELECTRON_RENDERER_DESKTOP_CAPTURER_RESULT_${id}`, (event, sources) => {
try {
callback(null, mapSources(sources))
} catch (error) {
callback(error)
}
})
}
18 changes: 9 additions & 9 deletions spec/api-desktop-capturer-spec.js
Expand Up @@ -39,15 +39,6 @@ describe('desktopCapturer', () => {
})
})

it('throws an error when blocked', done => {
ipcRenderer.send('handle-next-desktop-capturer-get-sources')
desktopCapturer.getSources({ types: ['screen'] }, (error, sources) => {
expect(error.message).to.equal('desktopCapturer.getSources() blocked')
expect(sources).to.be.undefined()
done()
})
})

it('does not throw an error when called more than once (regression)', (done) => {
let callCount = 0
const callback = (error, sources) => {
Expand Down Expand Up @@ -108,5 +99,14 @@ describe('desktopCapturer', () => {
}
done()
})

it('returns empty sources when blocked', done => {
ipcRenderer.send('handle-next-desktop-capturer-get-sources')
desktopCapturer.getSources({ types: ['screen'] }, (error, sources) => {
expect(error).to.be.null()
expect(sources).to.be.empty()
done()
})
})
})
})

0 comments on commit be02125

Please sign in to comment.