From be0212582613cdd468d608ff81f388232f6815b2 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Sat, 8 Dec 2018 10:41:27 +0100 Subject: [PATCH] return empty instead of error --- docs/api/app.md | 2 +- docs/api/web-contents.md | 2 +- lib/browser/desktop-capturer.js | 8 +++----- lib/renderer/api/desktop-capturer.js | 10 ++++------ spec/api-desktop-capturer-spec.js | 18 +++++++++--------- 5 files changed, 18 insertions(+), 22 deletions(-) diff --git a/docs/api/app.md b/docs/api/app.md index 5a6c8604ed70d..5a6a1c45c5a71 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -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' diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 5bca2bf0d0077..679e61b6b54f8 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -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' diff --git a/lib/browser/desktop-capturer.js b/lib/browser/desktop-capturer.js index 9bcb005f321d3..fa10803079d21 100644 --- a/lib/browser/desktop-capturer.js +++ b/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') @@ -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 } @@ -63,7 +61,7 @@ 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 @@ -71,7 +69,7 @@ desktopCapturer.emit = (event, name, sources, fetchWindowIcons) => { 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) diff --git a/lib/renderer/api/desktop-capturer.js b/lib/renderer/api/desktop-capturer.js index dcd18f798a02c..da231d45722e0 100644 --- a/lib/renderer/api/desktop-capturer.js +++ b/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 @@ -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) } }) } diff --git a/spec/api-desktop-capturer-spec.js b/spec/api-desktop-capturer-spec.js index a66c972499661..b0137c3b45ef4 100644 --- a/spec/api-desktop-capturer-spec.js +++ b/spec/api-desktop-capturer-spec.js @@ -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) => { @@ -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() + }) + }) }) })