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 cb1f2dc
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 45 deletions.
12 changes: 9 additions & 3 deletions docs/api/web-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -1321,9 +1321,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. 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 +1336,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 +1347,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()`.

#### `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 +1358,8 @@ 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()`.

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

Get the system printer list.
Expand Down
46 changes: 45 additions & 1 deletion docs/breaking-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,58 @@ 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 internally 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 internally 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)

### 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
1 change: 0 additions & 1 deletion patches/chromium/.patches
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ fix_media_key_usage_with_globalshortcuts.patch
feat_expose_raw_response_headers_from_urlloader.patch
chore_do_not_use_chrome_windows_in_cryptotoken_webrequestsender.patch
process_singleton.patch
fix_expose_decrementcapturercount_in_web_contents_impl.patch
add_ui_scopedcliboardwriter_writeunsaferawdata.patch
feat_add_data_parameter_to_processsingleton.patch
load_v8_snapshot_in_browser_process.patch
Expand Down

This file was deleted.

18 changes: 15 additions & 3 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 @@ -3152,11 +3154,15 @@ void WebContents::StartDrag(const gin_helper::Dictionary& item,

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 @@ -3176,6 +3182,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,10 +3201,12 @@ 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) {
gfx::Size size;
bool stay_hidden = false;
Expand All @@ -3213,6 +3224,7 @@ void WebContents::IncrementCapturerCount(gin::Arguments* args) {
.Release();
}

// TODO(codebytere): remove in Electron v23.
void WebContents::DecrementCapturerCount(gin::Arguments* args) {
bool stay_hidden = false;
bool stay_awake = false;
Expand Down

0 comments on commit cb1f2dc

Please sign in to comment.