From 4765de406fa682ea05bdb49630505034b4d361bd Mon Sep 17 00:00:00 2001 From: Zeeker <13848632+zeeker999@users.noreply.github.com> Date: Fri, 18 Feb 2022 16:39:03 +0800 Subject: [PATCH] fix: IncrementCapturerCount doesn't increase the capturer count This regression was introduced by commit 22a70eb8. --- .../browser/api/electron_api_web_contents.cc | 5 +- spec-main/api-browser-view-spec.ts | 62 +++++++++++++++++++ spec-main/api-browser-window-spec.ts | 24 +++++++ 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 85d2e7207efc0..d09cad62e8fa3 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -3122,8 +3122,9 @@ void WebContents::IncrementCapturerCount(gin::Arguments* args) { // get stayAwake arguments if they exist args->GetNext(&stay_awake); - std::ignore = - web_contents()->IncrementCapturerCount(size, stay_hidden, stay_awake); + std::ignore = web_contents() + ->IncrementCapturerCount(size, stay_hidden, stay_awake) + .Release(); } void WebContents::DecrementCapturerCount(gin::Arguments* args) { diff --git a/spec-main/api-browser-view-spec.ts b/spec-main/api-browser-view-spec.ts index ac6a036db06b9..3a1c722048f00 100644 --- a/spec-main/api-browser-view-spec.ts +++ b/spec-main/api-browser-view-spec.ts @@ -302,4 +302,66 @@ describe('BrowserView module', () => { view.webContents.loadFile(path.join(fixtures, 'pages', 'window-open.html')); }); }); + + describe('BrowserView.capturePage(rect)', () => { + it('returns a Promise with a Buffer', async () => { + view = new BrowserView({ + webPreferences: { + backgroundThrottling: false + } + }); + w.addBrowserView(view); + view.setBounds({ + ...w.getBounds(), + x: 0, + y: 0 + }); + const image = await view.webContents.capturePage({ + x: 0, + y: 0, + width: 100, + height: 100 + }); + + expect(image.isEmpty()).to.equal(true); + }); + + xit('resolves after the window is hidden and capturer count is non-zero', async () => { + view = new BrowserView({ + webPreferences: { + backgroundThrottling: false + } + }); + w.setBrowserView(view); + view.setBounds({ + ...w.getBounds(), + x: 0, + y: 0 + }); + await view.webContents.loadFile(path.join(fixtures, 'pages', 'a.html')); + + view.webContents.incrementCapturerCount(); + const image = await view.webContents.capturePage(); + expect(image.isEmpty()).to.equal(false); + }); + + it('capturer count should be increased', () => { + view = new BrowserView({ + webPreferences: { + backgroundThrottling: false + } + }); + w.setBrowserView(view); + view.setBounds({ + ...w.getBounds(), + x: 0, + y: 0 + }); + + view.webContents.incrementCapturerCount(); + expect(view.webContents.isBeingCaptured()).to.be.true(); + view.webContents.decrementCapturerCount(); + expect(view.webContents.isBeingCaptured()).to.be.false(); + }); + }); }); diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index 5f3657001ab8c..e4b2d8a739ca9 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -1399,6 +1399,11 @@ describe('BrowserWindow module', () => { }); expect(image.isEmpty()).to.equal(true); + + w.webContents.loadFile(path.join(fixtures, 'pages', 'theme-color.html')); + await emittedOnce(w, 'ready-to-show'); + w.webContents.incrementCapturerCount(); + expect((await w.webContents.capturePage()).isEmpty()).to.equal(false); }); it('resolves after the window is hidden', async () => { @@ -1417,6 +1422,17 @@ describe('BrowserWindow module', () => { expect(hiddenImage.isEmpty()).to.equal(isEmpty); }); + it('resolves after the window is hidden and capturer count is non-zero', async () => { + const w = new BrowserWindow({ show: false }); + w.webContents.setBackgroundThrottling(false); + w.loadFile(path.join(fixtures, 'pages', 'a.html')); + await emittedOnce(w, 'ready-to-show'); + + w.webContents.incrementCapturerCount(); + const image = await w.capturePage(); + expect(image.isEmpty()).to.equal(false); + }); + it('preserves transparency', async () => { const w = new BrowserWindow({ show: false, transparent: true }); w.loadFile(path.join(fixtures, 'pages', 'theme-color.html')); @@ -1430,6 +1446,14 @@ describe('BrowserWindow module', () => { // Values can be 0,2,3,4, or 6. We want 6, which is RGB + Alpha expect(imgBuffer[25]).to.equal(6); }); + + it('capturer count should be increased', () => { + const w = new BrowserWindow({ show: false }); + w.webContents.incrementCapturerCount(); + expect(w.webContents.isBeingCaptured()).to.be.true(); + w.webContents.decrementCapturerCount(); + expect(w.webContents.isBeingCaptured()).to.be.false(); + }); }); describe('BrowserWindow.setProgressBar(progress)', () => {