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 26, 2022
1 parent bc6196a commit 154975b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 112 deletions.
10 changes: 7 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.
* `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 @@ -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,6 +1347,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 @@ -1354,7 +1358,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
42 changes: 32 additions & 10 deletions docs/breaking-changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,26 @@ 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 (22.0)
## Planned Breaking API Changes (23.0)

### Removed: WebContents `new-window` event
### Removed: `webContents.incrementCapturerCount(stayHidden, stayAwake)`

The `new-window` event of WebContents has been removed. It is replaced by [`webContents.setWindowOpenHandler()`](api/web-contents.md#contentssetwindowopenhandlerhandler).
The `webContents.incrementCapturerCount(stayHidden, stayAwake)` function has been removed.
It is now automatically handled internally when a page capture completes.

```js
// Removed in Electron 22
webContents.on('new-window', (event) => {
event.preventDefault()
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
webContents.setWindowOpenHandler((details) => {
return { action: 'deny' }
w.capturePage().then(image => {
console.log(image.toDataURL())
})
```

Expand All @@ -38,20 +43,37 @@ It is now automatically handled internally when a page capture completes.
```js
const w = new BrowserWindow({ show: false })

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

// Replace with
w.webContents.incrementCapturerCount()
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 22
webContents.on('new-window', (event) => {
event.preventDefault()
})

// Replace with
webContents.setWindowOpenHandler((details) => {
return { action: 'deny' }
})
```

## Planned Breaking API Changes (20.0)

### Behavior Changed: V8 Memory Cage enabled
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 @@ -3143,20 +3151,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 @@ -3176,6 +3181,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 @@ -3191,36 +3199,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 @@ -267,9 +267,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 @@ -807,8 +804,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 154975b

Please sign in to comment.