Skip to content

Commit

Permalink
fix: add extra checks
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Oct 10, 2023
1 parent 78104f0 commit 4aff217
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
17 changes: 15 additions & 2 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,16 @@ base::IDMap<WebContents*>& GetAllWebContents() {
void OnCapturePageDone(gin_helper::Promise<gfx::Image> promise,
base::ScopedClosureRunner capture_handle,
const SkBitmap& bitmap) {
auto ui_task_runner = content::GetUIThreadTaskRunner({});
if (!ui_task_runner->RunsTasksInCurrentSequence()) {
ui_task_runner->PostTask(
FROM_HERE, base::BindOnce(&OnCapturePageDone, std::move(promise),
std::move(capture_handle), bitmap));
return;
}

// Hack to enable transparency in captured image
promise.Resolve(gfx::Image::CreateFrom1xBitmap(bitmap));

capture_handle.RunAndReset();
}

Expand Down Expand Up @@ -3452,11 +3459,17 @@ v8::Local<v8::Promise> WebContents::CapturePage(gin::Arguments* args) {
}

auto* const view = web_contents()->GetRenderWidgetHostView();
if (!view) {
if (!view || view->GetViewBounds().size().IsEmpty()) {
promise.Resolve(gfx::Image());
return handle;
}

if (!view->IsSurfaceAvailableForCopy()) {
promise.RejectWithErrorMessage(
"Current display surface not available for capture");
return handle;
}

auto capture_handle = web_contents()->IncrementCapturerCount(
rect.size(), stay_hidden, stay_awake);

Expand Down
19 changes: 17 additions & 2 deletions spec/api-browser-window-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2224,7 +2224,22 @@ describe('BrowserWindow module', () => {
expect(visible).to.equal('hidden');
});

it('resolves after the window is hidden', async () => {
it('resolves when the window is occluded', async () => {
const w1 = new BrowserWindow({ show: false });
w1.loadFile(path.join(fixtures, 'pages', 'a.html'));
await once(w1, 'ready-to-show');
w1.show();

const w2 = new BrowserWindow({ show: false });
w2.loadFile(path.join(fixtures, 'pages', 'a.html'));
await once(w2, 'ready-to-show');
w2.show();

const visibleImage = await w1.capturePage();
expect(visibleImage.isEmpty()).to.equal(false);
});

it('resolves when the window is not visible', async () => {
const w = new BrowserWindow({ show: false });
w.loadFile(path.join(fixtures, 'pages', 'a.html'));
await once(w, 'ready-to-show');
Expand All @@ -2233,7 +2248,7 @@ describe('BrowserWindow module', () => {
const visibleImage = await w.capturePage();
expect(visibleImage.isEmpty()).to.equal(false);

w.hide();
w.minimize();

const hiddenImage = await w.capturePage();
expect(hiddenImage.isEmpty()).to.equal(false);
Expand Down

0 comments on commit 4aff217

Please sign in to comment.