Skip to content

Commit 8e30e21

Browse files
authored
feat: Exposing methods required by capturing a hidden webContents (electron#21894)
1 parent a80f7fc commit 8e30e21

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

docs/api/web-contents.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,25 @@ Returns `Promise<NativeImage>` - Resolves with a [NativeImage](native-image.md)
12321232

12331233
Captures a snapshot of the page within `rect`. Omitting `rect` will capture the whole visible page.
12341234

1235+
#### `contents.isBeingCaptured()`
1236+
1237+
Returns `Boolean` - Whether this page is being captured. It returns true when the capturer count
1238+
is large then 0.
1239+
1240+
#### `contents.incrementCapturerCount([size])`
1241+
1242+
* `size` [Size](structures/size.md) (optional) - The perferred size for the capturer.
1243+
1244+
Increase the capturer count by one. The page is considered visible when its browser window is
1245+
hidden and the capturer count is non-zero.
1246+
1247+
This also affects the Page Visibility API.
1248+
1249+
#### `contents.decrementCapturerCount()`
1250+
1251+
Decrease the capturer count by one. The page will be set to hidden or occluded state when its
1252+
browser window is hidden or occluded and the capturer count reaches zero.
1253+
12351254
#### `contents.getPrinters()`
12361255

12371256
Get the system printer list.

shell/browser/api/atom_api_web_contents.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,6 +2204,23 @@ v8::Local<v8::Promise> WebContents::CapturePage(mate::Arguments* args) {
22042204
return handle;
22052205
}
22062206

2207+
void WebContents::IncrementCapturerCount(mate::Arguments* args) {
2208+
gfx::Size size;
2209+
2210+
// get size arguments if they exist
2211+
args->GetNext(&size);
2212+
2213+
web_contents()->IncrementCapturerCount(size);
2214+
}
2215+
2216+
void WebContents::DecrementCapturerCount(mate::Arguments* args) {
2217+
web_contents()->DecrementCapturerCount();
2218+
}
2219+
2220+
bool WebContents::IsBeingCaptured() {
2221+
return web_contents()->IsBeingCaptured();
2222+
}
2223+
22072224
void WebContents::OnCursorChange(const content::WebCursor& cursor) {
22082225
const content::CursorInfo& info = cursor.info();
22092226

@@ -2598,6 +2615,9 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
25982615
.SetMethod("setEmbedder", &WebContents::SetEmbedder)
25992616
.SetMethod("setDevToolsWebContents", &WebContents::SetDevToolsWebContents)
26002617
.SetMethod("getNativeView", &WebContents::GetNativeView)
2618+
.SetMethod("incrementCapturerCount", &WebContents::IncrementCapturerCount)
2619+
.SetMethod("decrementCapturerCount", &WebContents::DecrementCapturerCount)
2620+
.SetMethod("isBeingCaptured", &WebContents::IsBeingCaptured)
26012621
.SetMethod("setWebRTCIPHandlingPolicy",
26022622
&WebContents::SetWebRTCIPHandlingPolicy)
26032623
.SetMethod("getWebRTCIPHandlingPolicy",

shell/browser/api/atom_api_web_contents.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ class WebContents : public mate::TrackableObject<WebContents>,
185185
void SetEmbedder(const WebContents* embedder);
186186
void SetDevToolsWebContents(const WebContents* devtools);
187187
v8::Local<v8::Value> GetNativeView() const;
188+
void IncrementCapturerCount(mate::Arguments* args);
189+
void DecrementCapturerCount(mate::Arguments* args);
190+
bool IsBeingCaptured();
188191

189192
#if BUILDFLAG(ENABLE_PRINTING)
190193
void OnGetDefaultPrinter(base::DictionaryValue print_settings,

0 commit comments

Comments
 (0)