From 3c6c77efe3cdddcf6a657c94b1506d640d4b1d4d Mon Sep 17 00:00:00 2001 From: LuoJinghua Date: Sat, 25 Jan 2020 08:43:42 +0800 Subject: [PATCH] feat: Exposing methods required by capturing a hidden webContents --- docs/api/web-contents.md | 23 ++++++++++++++++++ shell/browser/api/atom_api_web_contents.cc | 28 ++++++++++++++++++++++ shell/browser/api/atom_api_web_contents.h | 3 +++ 3 files changed, 54 insertions(+) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 4f2f53cc7d5c0..b22e3c4d80bf1 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -1247,6 +1247,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 6cf1aa009b3f3..7f971c334788b 100644 --- a/shell/browser/api/atom_api_web_contents.cc +++ b/shell/browser/api/atom_api_web_contents.cc @@ -2220,6 +2220,31 @@ v8::Local WebContents::CapturePage(mate::Arguments* mate_args) { return handle; } +void WebContents::IncrementCapturerCount(mate::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(mate::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(); @@ -2599,6 +2624,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 809d56dc9f7f9..92a8c62ae9673 100644 --- a/shell/browser/api/atom_api_web_contents.h +++ b/shell/browser/api/atom_api_web_contents.h @@ -182,6 +182,9 @@ class WebContents : public mate::TrackableObject, void SetEmbedder(const WebContents* embedder); void SetDevToolsWebContents(const WebContents* devtools); v8::Local GetNativeView() const; + void IncrementCapturerCount(mate::Arguments* args); + void DecrementCapturerCount(mate::Arguments* args); + bool IsBeingCaptured(); #if BUILDFLAG(ENABLE_PRINTING) void Print(mate::Arguments* args);