Skip to content

Commit

Permalink
chore: remove incrementCapturerCount
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Sep 22, 2022
1 parent db6b63d commit af8a66f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 102 deletions.
10 changes: 7 additions & 3 deletions docs/api/web-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -1306,9 +1306,11 @@ const requestId = webContents.findInPage('api')
console.log(requestId)
```

#### `contents.capturePage([rect])`
#### `contents.capturePage([rect, stayHidden, stayAwake])`

* `rect` [Rectangle](structures/rectangle.md) (optional) - The area of the page to be captured.
* `stayHidden` boolean (optional) - Keep the page hidden instead of visible.
* `stayAwake` boolean (optional) - Keep the system awake instead of allowing it to sleep.

Returns `Promise<NativeImage>` - Resolves with a [NativeImage](native-image.md)

Expand All @@ -1319,7 +1321,7 @@ Captures a snapshot of the page within `rect`. Omitting `rect` will capture the
Returns `boolean` - Whether this page is being captured. It returns true when the capturer count
is large then 0.

#### `contents.incrementCapturerCount([size, stayHidden, stayAwake])`
#### `contents.incrementCapturerCount([size, stayHidden, stayAwake])` _Deprecated_

* `size` [Size](structures/size.md) (optional) - The preferred size for the capturer.
* `stayHidden` boolean (optional) - Keep the page hidden instead of visible.
Expand All @@ -1330,6 +1332,8 @@ hidden and the capturer count is non-zero. If you would like the page to stay hi

This also affects the Page Visibility API.

**Deprecated:** This API's functionality is now handled automatically within `contents.capturePage()`.

#### `contents.decrementCapturerCount([stayHidden, stayAwake])` _Deprecated_

* `stayHidden` boolean (optional) - Keep the page in hidden state instead of visible.
Expand All @@ -1339,7 +1343,7 @@ Decrease the capturer count by one. The page will be set to hidden or occluded s
browser window is hidden or occluded and the capturer count reaches zero. If you want to
decrease the hidden capturer count instead you should set `stayHidden` to true.

**Deprecated:** This API's functionality is now handled automatically.
**Deprecated:** This API's functionality is now handled automatically within `contents.capturePage()`.

#### `contents.getPrinters()` _Deprecated_

Expand Down
55 changes: 22 additions & 33 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,14 @@ base::IDMap<WebContents*>& GetAllWebContents() {
return *s_all_web_contents;
}

void OnCapturePageDone(gin_helper::Promise<gfx::Image> promise,
base::ScopedClosureRunner capture_handle,
const SkBitmap& bitmap) {
// Hack to enable transparency in captured image
promise.Resolve(gfx::Image::CreateFrom1xBitmap(bitmap));
capture_handle.RunAndReset();
}

absl::optional<base::TimeDelta> GetCursorBlinkInterval() {
#if BUILDFLAG(IS_MAC)
absl::optional<base::TimeDelta> system_value(
Expand Down Expand Up @@ -3127,20 +3135,17 @@ void WebContents::StartDrag(const gin_helper::Dictionary& item,
}
}

void WebContents::OnCapturePageDone(gin_helper::Promise<gfx::Image> promise,
const SkBitmap& bitmap) {
// Hack to enable transparency in captured image
promise.Resolve(gfx::Image::CreateFrom1xBitmap(bitmap));
capture_handle_.RunAndReset();
}

v8::Local<v8::Promise> WebContents::CapturePage(gin::Arguments* args) {
gfx::Rect rect;
bool stay_hidden = false;
bool stay_awake = false;

gin_helper::Promise<gfx::Image> promise(args->isolate());
v8::Local<v8::Promise> handle = promise.GetHandle();

// get rect arguments if they exist
args->GetNext(&rect);
args->GetNext(&stay_hidden);
args->GetNext(&stay_awake);

auto* const view = web_contents()->GetRenderWidgetHostView();
if (!view) {
Expand All @@ -3160,6 +3165,9 @@ v8::Local<v8::Promise> WebContents::CapturePage(gin::Arguments* args) {
}
#endif // BUILDFLAG(IS_MAC)

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

// Capture full page if user doesn't specify a |rect|.
const gfx::Size view_size =
rect.IsEmpty() ? view->GetViewBounds().size() : rect.size();
Expand All @@ -3175,36 +3183,17 @@ v8::Local<v8::Promise> WebContents::CapturePage(gin::Arguments* args) {
if (scale > 1.0f)
bitmap_size = gfx::ScaleToCeiledSize(view_size, scale);

view->CopyFromSurface(
gfx::Rect(rect.origin(), view_size), bitmap_size,
base::BindOnce(&WebContents::OnCapturePageDone,
weak_factory_.GetWeakPtr(), std::move(promise)));
view->CopyFromSurface(gfx::Rect(rect.origin(), view_size), bitmap_size,
base::BindOnce(&OnCapturePageDone, std::move(promise),
std::move(capture_handle)));
return handle;
}

void WebContents::IncrementCapturerCount(gin::Arguments* args) {
gfx::Size size;
bool stay_hidden = false;
bool stay_awake = false;

args->GetNext(&size);
args->GetNext(&stay_hidden);
args->GetNext(&stay_awake);

if (IsBeingCaptured()) {
gin_helper::ErrorThrower(args->isolate())
.ThrowError("A capture session is already in progress.");
return;
}

capture_handle_ =
web_contents()->IncrementCapturerCount(size, stay_hidden, stay_awake);
}
// TODO(codebytere): remove in Electron v23.
void WebContents::IncrementCapturerCount(gin::Arguments* args) {}

// TODO(codebytere): remove in Electron v22.
void WebContents::DecrementCapturerCount(gin::Arguments* args) {
capture_handle_.RunAndReset();
}
void WebContents::DecrementCapturerCount(gin::Arguments* args) {}

bool WebContents::IsBeingCaptured() {
return web_contents()->IsBeingCaptured();
Expand Down
5 changes: 0 additions & 5 deletions shell/browser/api/electron_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,6 @@ class WebContents : public ExclusiveAccessContext,
// Dragging native items.
void StartDrag(const gin_helper::Dictionary& item, gin::Arguments* args);

void OnCapturePageDone(gin_helper::Promise<gfx::Image> promise,
const SkBitmap& bitmap);

// Captures the page with |rect|, |callback| would be called when capturing is
// done.
v8::Local<v8::Promise> CapturePage(gin::Arguments* args);
Expand Down Expand Up @@ -806,8 +803,6 @@ class WebContents : public ExclusiveAccessContext,

scoped_refptr<base::SequencedTaskRunner> file_task_runner_;

base::ScopedClosureRunner capture_handle_;

#if BUILDFLAG(ENABLE_PRINTING)
scoped_refptr<base::TaskRunner> print_task_runner_;
#endif
Expand Down
40 changes: 0 additions & 40 deletions spec/api-browser-view-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,45 +402,5 @@ describe('BrowserView module', () => {
const image = await view.webContents.capturePage();
expect(image.isEmpty()).to.equal(false);
});

it('throws an error when incrementing during a capture session', 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'));

expect(() => {
view.webContents.incrementCapturerCount();
view.webContents.capturePage();
view.webContents.incrementCapturerCount();
}).to.throw(/A capture session is already in progress./);
});

it('should increase the capturer count', () => {
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();
});
});
});
21 changes: 0 additions & 21 deletions spec/api-browser-window-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1764,19 +1764,6 @@ describe('BrowserWindow module', () => {
expect(image.isEmpty()).to.equal(false);
});

it('throws an error when incrementing during a capture session', 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');

expect(() => {
w.webContents.incrementCapturerCount();
w.capturePage();
w.webContents.incrementCapturerCount();
}).to.throw(/A capture session is already in progress./);
});

it('preserves transparency', async () => {
const w = new BrowserWindow({ show: false, transparent: true });
w.loadFile(path.join(fixtures, 'pages', 'theme-color.html'));
Expand All @@ -1790,14 +1777,6 @@ 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('should increase the capturer count', () => {
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)', () => {
Expand Down

0 comments on commit af8a66f

Please sign in to comment.