diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 9c45d6a49..d3b0fd767 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -1225,6 +1225,29 @@ Returns `Promise` - Resolves with a [NativeImage](native-image.md) Captures a snapshot of the page within `rect`. Omitting `rect` will capture the whole visible page. +#### `contents.isBeingCaptured()` + +Returns `Boolean` - Whether this page is being captured. It returns true when the capturer count +is large then 0. + +#### `contents.incrementCapturerCount([size, stayHidden])` + +* `size` [Size](structures/size.md) (optional) - The perferred size for the capturer. +* `stayHidden` Boolean (optional) - Keep the page hidden instead of visible. + +Increase the capturer count by one. The page is considered visible when its browser window is +hidden and the capturer count is non-zero. If you would like the page to stay hidden, you should ensure that `stayHidden` is set to true. + +This also affects the Page Visibility API. + +#### `contents.decrementCapturerCount([stayHidden])` + +* `stayHidden` Boolean (optional) - Keep the page in hidden state instead of visible. + +Decrease the capturer count by one. The page will be set to hidden or occluded state when its +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. + #### `contents.getPrinters()` Get the system printer list. diff --git a/shell/browser/api/atom_api_web_contents.cc b/shell/browser/api/atom_api_web_contents.cc index 089127778..7deff0a71 100644 --- a/shell/browser/api/atom_api_web_contents.cc +++ b/shell/browser/api/atom_api_web_contents.cc @@ -2222,6 +2222,31 @@ v8::Local WebContents::CapturePage(gin_helper::Arguments* args) { return handle; } +void WebContents::IncrementCapturerCount(gin_helper::Arguments* args) { + gfx::Size size; + bool stay_hidden = false; + + // get size arguments if they exist + args->GetNext(&size); + // get stayHidden arguments if they exist + args->GetNext(&stay_hidden); + + web_contents()->IncrementCapturerCount(size, stay_hidden); +} + +void WebContents::DecrementCapturerCount(gin_helper::Arguments* args) { + bool stay_hidden = false; + + // get stayHidden arguments if they exist + args->GetNext(&stay_hidden); + + web_contents()->DecrementCapturerCount(stay_hidden); +} + +bool WebContents::IsBeingCaptured() { + return web_contents()->IsBeingCaptured(); +} + void WebContents::OnCursorChange(const content::WebCursor& cursor) { const content::CursorInfo& info = cursor.info(); @@ -2598,6 +2623,9 @@ void WebContents::BuildPrototype(v8::Isolate* isolate, .SetMethod("setEmbedder", &WebContents::SetEmbedder) .SetMethod("setDevToolsWebContents", &WebContents::SetDevToolsWebContents) .SetMethod("getNativeView", &WebContents::GetNativeView) + .SetMethod("incrementCapturerCount", &WebContents::IncrementCapturerCount) + .SetMethod("decrementCapturerCount", &WebContents::DecrementCapturerCount) + .SetMethod("isBeingCaptured", &WebContents::IsBeingCaptured) .SetMethod("setWebRTCIPHandlingPolicy", &WebContents::SetWebRTCIPHandlingPolicy) .SetMethod("getWebRTCIPHandlingPolicy", diff --git a/shell/browser/api/atom_api_web_contents.h b/shell/browser/api/atom_api_web_contents.h index d27fc2ac8..51cdf9333 100644 --- a/shell/browser/api/atom_api_web_contents.h +++ b/shell/browser/api/atom_api_web_contents.h @@ -185,6 +185,9 @@ class WebContents : public gin_helper::TrackableObject, void SetEmbedder(const WebContents* embedder); void SetDevToolsWebContents(const WebContents* devtools); v8::Local GetNativeView() const; + void IncrementCapturerCount(gin_helper::Arguments* args); + void DecrementCapturerCount(gin_helper::Arguments* args); + bool IsBeingCaptured(); #if BUILDFLAG(ENABLE_PRINTING) void Print(gin_helper::Arguments* args);