From 40624199ebf02aeca1bfbb34180863e725d9ac97 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Sat, 26 Apr 2025 19:40:39 -0500 Subject: [PATCH] refactor: use upstream content protection logic on macOS (#46814) * refactor: use upstream content protection logic on macOS Co-authored-by: Shelley Vohr * Update shell/browser/native_window.h Co-authored-by: Charles Kerr Co-authored-by: Shelley Vohr --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr --- shell/browser/native_window.cc | 15 +++++++++++++++ shell/browser/native_window.h | 4 ++-- shell/browser/native_window_mac.h | 2 -- shell/browser/native_window_mac.mm | 9 --------- shell/browser/native_window_views.cc | 15 --------------- shell/browser/native_window_views.h | 2 -- 6 files changed, 17 insertions(+), 30 deletions(-) diff --git a/shell/browser/native_window.cc b/shell/browser/native_window.cc index 38386ab0e702..21437c428d9d 100644 --- a/shell/browser/native_window.cc +++ b/shell/browser/native_window.cc @@ -26,6 +26,7 @@ #include "shell/common/options_switches.h" #include "ui/base/hit_test.h" #include "ui/compositor/compositor.h" +#include "ui/views/widget/native_widget_private.h" #include "ui/views/widget/widget.h" #if !BUILDFLAG(IS_MAC) @@ -814,6 +815,20 @@ void NativeWindow::HandlePendingFullscreenTransitions() { SetFullScreen(next_transition); } +void NativeWindow::SetContentProtection(bool enable) { +#if !BUILDFLAG(IS_LINUX) + widget()->native_widget_private()->SetAllowScreenshots(!enable); +#endif +} + +bool NativeWindow::IsContentProtected() const { +#if !BUILDFLAG(IS_LINUX) + return !widget()->native_widget_private()->AreScreenshotsAllowed(); +#else // Not implemented on Linux + return false; +#endif +} + bool NativeWindow::IsTranslucent() const { // Transparent windows are translucent if (transparent()) { diff --git a/shell/browser/native_window.h b/shell/browser/native_window.h index 8eee441a051f..a0d31dcee4bc 100644 --- a/shell/browser/native_window.h +++ b/shell/browser/native_window.h @@ -186,8 +186,8 @@ class NativeWindow : public base::SupportsUserData, virtual void SetDocumentEdited(bool edited) {} virtual bool IsDocumentEdited() const; virtual void SetIgnoreMouseEvents(bool ignore, bool forward) = 0; - virtual void SetContentProtection(bool enable) = 0; - virtual bool IsContentProtected() const = 0; + void SetContentProtection(bool enable); + bool IsContentProtected() const; virtual void SetFocusable(bool focusable) {} virtual bool IsFocusable() const; virtual void SetMenu(ElectronMenuModel* menu) {} diff --git a/shell/browser/native_window_mac.h b/shell/browser/native_window_mac.h index 5420e14aecd7..fa51e9ee447d 100644 --- a/shell/browser/native_window_mac.h +++ b/shell/browser/native_window_mac.h @@ -109,8 +109,6 @@ class NativeWindowMac : public NativeWindow, void SetIgnoreMouseEvents(bool ignore, bool forward) override; bool IsHiddenInMissionControl() const override; void SetHiddenInMissionControl(bool hidden) override; - void SetContentProtection(bool enable) override; - bool IsContentProtected() const override; void SetFocusable(bool focusable) override; bool IsFocusable() const override; void SetParentWindow(NativeWindow* parent) override; diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index 39c56e0ec441..131e420cfcc5 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -1171,15 +1171,6 @@ void NativeWindowMac::SetIgnoreMouseEvents(bool ignore, bool forward) { } } -void NativeWindowMac::SetContentProtection(bool enable) { - [window_ - setSharingType:enable ? NSWindowSharingNone : NSWindowSharingReadOnly]; -} - -bool NativeWindowMac::IsContentProtected() const { - return [window_ sharingType] == NSWindowSharingNone; -} - void NativeWindowMac::SetFocusable(bool focusable) { // No known way to unfocus the window if it had the focus. Here we do not // want to call Focus(false) because it moves the window to the back, i.e. diff --git a/shell/browser/native_window_views.cc b/shell/browser/native_window_views.cc index ea557ffb44ec..6a19a0768853 100644 --- a/shell/browser/native_window_views.cc +++ b/shell/browser/native_window_views.cc @@ -44,7 +44,6 @@ #include "ui/ozone/public/ozone_platform.h" #include "ui/views/background.h" #include "ui/views/controls/webview/webview.h" -#include "ui/views/widget/native_widget_private.h" #include "ui/views/widget/widget.h" #include "ui/views/window/client_view.h" #include "ui/wm/core/shadow_types.h" @@ -1312,20 +1311,6 @@ void NativeWindowViews::SetIgnoreMouseEvents(bool ignore, bool forward) { #endif } -void NativeWindowViews::SetContentProtection(bool enable) { -#if BUILDFLAG(IS_WIN) - widget()->native_widget_private()->SetAllowScreenshots(!enable); -#endif -} - -bool NativeWindowViews::IsContentProtected() const { -#if BUILDFLAG(IS_WIN) - return !widget()->native_widget_private()->AreScreenshotsAllowed(); -#else // Not implemented on Linux - return false; -#endif -} - void NativeWindowViews::SetFocusable(bool focusable) { widget()->widget_delegate()->SetCanActivate(focusable); #if BUILDFLAG(IS_WIN) diff --git a/shell/browser/native_window_views.h b/shell/browser/native_window_views.h index cdb6fb9c1435..2d23a020f115 100644 --- a/shell/browser/native_window_views.h +++ b/shell/browser/native_window_views.h @@ -115,8 +115,6 @@ class NativeWindowViews : public NativeWindow, void SetOpacity(const double opacity) override; double GetOpacity() const override; void SetIgnoreMouseEvents(bool ignore, bool forward) override; - void SetContentProtection(bool enable) override; - bool IsContentProtected() const override; void SetFocusable(bool focusable) override; bool IsFocusable() const override; void SetMenu(ElectronMenuModel* menu_model) override;