From 10dd33c16eebe74a23b283e35e719e96e5b6e6e0 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Sun, 11 Aug 2024 21:30:27 +0200 Subject: [PATCH] fix: check screen capture permissions in `desktopCapturer` (#43271) fix: check screen capture permissions in desktopCapturer Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- .../api/electron_api_desktop_capturer.cc | 31 ++++++++++++++----- .../api/electron_api_desktop_capturer.h | 1 + 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/shell/browser/api/electron_api_desktop_capturer.cc b/shell/browser/api/electron_api_desktop_capturer.cc index ba762bb1515..9b73bb16b57 100644 --- a/shell/browser/api/electron_api_desktop_capturer.cc +++ b/shell/browser/api/electron_api_desktop_capturer.cc @@ -41,6 +41,10 @@ #include "ui/gfx/x/randr.h" #endif +#if BUILDFLAG(IS_MAC) +#include "ui/base/cocoa/permissions_utils.h" +#endif + #if BUILDFLAG(IS_LINUX) // Private function in ui/base/x/x11_display_util.cc base::flat_map GetMonitors( @@ -304,6 +308,13 @@ void DesktopCapturer::StartHandling(bool capture_window, capture_window_ = capture_window; capture_screen_ = capture_screen; +#if BUILDFLAG(IS_MAC) + if (!ui::TryPromptUserForScreenCapture()) { + HandleFailure(); + return; + } +#endif + { // Initialize the source list. // Apply the new thumbnail size and restart capture. @@ -455,21 +466,25 @@ void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) { std::back_inserter(captured_sources_)); } - if (!capture_window_ && !capture_screen_) { - v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); - v8::HandleScope scope(isolate); - gin_helper::CallMethod(this, "_onfinished", captured_sources_); + if (!capture_window_ && !capture_screen_) + HandleSuccess(); +} - screen_capturer_.reset(); - window_capturer_.reset(); +void DesktopCapturer::HandleSuccess() { + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + v8::HandleScope scope(isolate); + gin_helper::CallMethod(this, "_onfinished", captured_sources_); - Unpin(); - } + screen_capturer_.reset(); + window_capturer_.reset(); + + Unpin(); } void DesktopCapturer::HandleFailure() { v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); v8::HandleScope scope(isolate); + gin_helper::CallMethod(this, "_onerror", "Failed to get sources."); screen_capturer_.reset(); diff --git a/shell/browser/api/electron_api_desktop_capturer.h b/shell/browser/api/electron_api_desktop_capturer.h index 65285086a15..4f8bc7771f8 100644 --- a/shell/browser/api/electron_api_desktop_capturer.h +++ b/shell/browser/api/electron_api_desktop_capturer.h @@ -90,6 +90,7 @@ class DesktopCapturer : public gin::Wrappable, void UpdateSourcesList(DesktopMediaList* list); void HandleFailure(); + void HandleSuccess(); std::unique_ptr window_listener_; std::unique_ptr screen_listener_;