Skip to content

Commit

Permalink
refactor: eliminate DecrementCapturerCount patch
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Sep 26, 2022
1 parent eb3262c commit df767c5
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 8 deletions.
14 changes: 11 additions & 3 deletions docs/api/web-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -1321,9 +1321,12 @@ const requestId = webContents.findInPage('api')
console.log(requestId)
```

#### `contents.capturePage([rect])`
#### `contents.capturePage([rect, opts])`

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

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

Expand All @@ -1334,7 +1337,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 @@ -1345,7 +1348,9 @@ hidden and the capturer count is non-zero. If you would like the page to stay hi

This also affects the Page Visibility API.

#### `contents.decrementCapturerCount([stayHidden, stayAwake])`
**Deprecated:** This API's functionality is now handled automatically within `contents.capturePage()`. See [breaking changes](../breaking-changes.md).

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

* `stayHidden` boolean (optional) - Keep the page in hidden state instead of visible.
* `stayAwake` boolean (optional) - Keep the system awake instead of allowing it to sleep.
Expand All @@ -1354,6 +1359,9 @@ 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 within `contents.capturePage()`.
See [breaking changes](../breaking-changes.md).

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

Get the system printer list.
Expand Down
88 changes: 87 additions & 1 deletion docs/breaking-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,100 @@ This document uses the following convention to categorize breaking changes:
* **Deprecated:** An API was marked as deprecated. The API will continue to function, but will emit a deprecation warning, and will be removed in a future release.
* **Removed:** An API or feature was removed, and is no longer supported by Electron.

## Planned Breaking API Changes (23.0)

### Removed: `webContents.incrementCapturerCount(stayHidden, stayAwake)`

The `webContents.incrementCapturerCount(stayHidden, stayAwake)` function has been removed.
It is now automatically handled by `webContents.capturePage` when a page capture completes.

```js
const w = new BrowserWindow({ show: false })

// Removed in Electron 23
w.webContents.incrementCapturerCount()
w.capturePage().then(image => {
console.log(image.toDataURL())
w.webContents.decrementCapturerCount()
})

// Replace with
w.capturePage().then(image => {
console.log(image.toDataURL())
})
```

### Removed: `webContents.decrementCapturerCount(stayHidden, stayAwake)`

The `webContents.decrementCapturerCount(stayHidden, stayAwake)` function has been removed.
It is now automatically handled by `webContents.capturePage` when a page capture completes.

```js
const w = new BrowserWindow({ show: false })

// Removed in Electron 23
w.webContents.incrementCapturerCount()
w.capturePage().then(image => {
console.log(image.toDataURL())
w.webContents.decrementCapturerCount()
})

// Replace with
w.capturePage().then(image => {
console.log(image.toDataURL())
})
```

## Planned Breaking API Changes (22.0)

### Deprecated: `webContents.incrementCapturerCount(stayHidden, stayAwake)`

`webContents.incrementCapturerCount(stayHidden, stayAwake)` has been deprecated.
It is now automatically handled by `webContents.capturePage` when a page capture completes.

```js
const w = new BrowserWindow({ show: false })

// Removed in Electron 23
w.webContents.incrementCapturerCount()
w.capturePage().then(image => {
console.log(image.toDataURL())
w.webContents.decrementCapturerCount()
})

// Replace with
w.capturePage().then(image => {
console.log(image.toDataURL())
})
```

### Removed: `webContents.decrementCapturerCount(stayHidden, stayAwake)`

`webContents.decrementCapturerCount(stayHidden, stayAwake)` has been deprecated.
It is now automatically handled by `webContents.capturePage` when a page capture completes.

```js
const w = new BrowserWindow({ show: false })

// Removed in Electron 23
w.webContents.incrementCapturerCount()
w.capturePage().then(image => {
console.log(image.toDataURL())
w.webContents.decrementCapturerCount()
})

// Replace with
w.capturePage().then(image => {
console.log(image.toDataURL())
})
```

### Removed: WebContents `new-window` event

The `new-window` event of WebContents has been removed. It is replaced by [`webContents.setWindowOpenHandler()`](api/web-contents.md#contentssetwindowopenhandlerhandler).

```js
// Removed in Electron 21
// Removed in Electron 22
webContents.on('new-window', (event) => {
event.preventDefault()
})
Expand Down
33 changes: 29 additions & 4 deletions shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -386,11 +386,13 @@ base::IDMap<WebContents*>& GetAllWebContents() {
return *s_all_web_contents;
}

// Called when CapturePage is done.
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() {
Expand Down Expand Up @@ -3151,13 +3153,22 @@ void WebContents::StartDrag(const gin_helper::Dictionary& item,
}

v8::Local<v8::Promise> WebContents::CapturePage(gin::Arguments* args) {
gfx::Rect rect;
gin_helper::Promise<gfx::Image> promise(args->isolate());
v8::Local<v8::Promise> handle = promise.GetHandle();

// get rect arguments if they exist
gfx::Rect rect;
args->GetNext(&rect);

bool stay_hidden = false;
bool stay_awake = false;
if (args && args->Length() == 2) {
gin_helper::Dictionary options;
if (args->GetNext(&options)) {
options.Get("stayHidden", &stay_hidden);
options.Get("stayAwake", &stay_awake);
}
}

auto* const view = web_contents()->GetRenderWidgetHostView();
if (!view) {
promise.Resolve(gfx::Image());
Expand All @@ -3176,6 +3187,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 @@ -3192,11 +3206,17 @@ v8::Local<v8::Promise> WebContents::CapturePage(gin::Arguments* args) {
bitmap_size = gfx::ScaleToCeiledSize(view_size, scale);

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

// TODO(codebytere): remove in Electron v23.
void WebContents::IncrementCapturerCount(gin::Arguments* args) {
EmitWarning(node::Environment::GetCurrent(args->isolate()),
"webContents.incrementCapturerCount() is deprecated and will be "
"removed in v23");

gfx::Size size;
bool stay_hidden = false;
bool stay_awake = false;
Expand All @@ -3213,7 +3233,12 @@ void WebContents::IncrementCapturerCount(gin::Arguments* args) {
.Release();
}

// TODO(codebytere): remove in Electron v23.
void WebContents::DecrementCapturerCount(gin::Arguments* args) {
EmitWarning(node::Environment::GetCurrent(args->isolate()),
"webContents.decrementCapturerCount() is deprecated and will be "
"removed in v23");

bool stay_hidden = false;
bool stay_awake = false;

Expand Down

0 comments on commit df767c5

Please sign in to comment.