From c243cf0c0ff641f930b5dfb7918e5c3f20b24ec1 Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Wed, 22 Jun 2016 10:40:01 +0200 Subject: [PATCH] Add BrowserWindow.prototype.setContentProtection(enable) --- atom/browser/api/atom_api_window.cc | 5 +++++ atom/browser/api/atom_api_window.h | 1 + atom/browser/native_window.h | 1 + atom/browser/native_window_mac.h | 1 + atom/browser/native_window_mac.mm | 5 +++++ atom/browser/native_window_views.cc | 7 +++++++ atom/browser/native_window_views.h | 1 + docs/api/browser-window.md | 7 +++++++ 8 files changed, 28 insertions(+) diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index cc3749bd0143..ae6bb77cf9bc 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -572,6 +572,10 @@ void Window::SetIgnoreMouseEvents(bool ignore) { return window_->SetIgnoreMouseEvents(ignore); } +void Window::SetContentProtection(bool enable) { + return window_->SetContentProtection(enable); +} + void Window::SetFocusable(bool focusable) { return window_->SetFocusable(focusable); } @@ -833,6 +837,7 @@ void Window::BuildPrototype(v8::Isolate* isolate, .SetMethod("setDocumentEdited", &Window::SetDocumentEdited) .SetMethod("isDocumentEdited", &Window::IsDocumentEdited) .SetMethod("setIgnoreMouseEvents", &Window::SetIgnoreMouseEvents) + .SetMethod("setContentProtection", &Window::SetContentProtection) .SetMethod("setFocusable", &Window::SetFocusable) .SetMethod("focusOnWebView", &Window::FocusOnWebView) .SetMethod("blurWebView", &Window::BlurWebView) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 21ecca4c7292..a204b70e296c 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -153,6 +153,7 @@ class Window : public mate::TrackableObject, void SetDocumentEdited(bool edited); bool IsDocumentEdited(); void SetIgnoreMouseEvents(bool ignore); + void SetContentProtection(bool enable); void SetFocusable(bool focusable); void CapturePage(mate::Arguments* args); void SetProgressBar(double progress); diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index d3b6f8a2b0be..8656c02ca939 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -157,6 +157,7 @@ class NativeWindow : public base::SupportsUserData, virtual void SetDocumentEdited(bool edited); virtual bool IsDocumentEdited(); virtual void SetIgnoreMouseEvents(bool ignore) = 0; + virtual void SetContentProtection(bool enable) = 0; virtual void SetFocusable(bool focusable); virtual void SetMenu(ui::MenuModel* menu); virtual bool HasModalDialog(); diff --git a/atom/browser/native_window_mac.h b/atom/browser/native_window_mac.h index af99b3912e16..d07d586a8439 100644 --- a/atom/browser/native_window_mac.h +++ b/atom/browser/native_window_mac.h @@ -79,6 +79,7 @@ class NativeWindowMac : public NativeWindow { void SetDocumentEdited(bool edited) override; bool IsDocumentEdited() override; void SetIgnoreMouseEvents(bool ignore) override; + void SetContentProtection(bool enable) override; bool HasModalDialog() override; void SetParentWindow(NativeWindow* parent) override; gfx::NativeWindow GetNativeWindow() override; diff --git a/atom/browser/native_window_mac.mm b/atom/browser/native_window_mac.mm index 3330eb8e9f3e..004d2cad23de 100644 --- a/atom/browser/native_window_mac.mm +++ b/atom/browser/native_window_mac.mm @@ -941,6 +941,11 @@ void NativeWindowMac::SetIgnoreMouseEvents(bool ignore) { [window_ setIgnoresMouseEvents:ignore]; } +void NativeWindowMac::SetContentProtection(bool enable) { + [window_ setSharingType:enable ? NSWindowSharingNone + : NSWindowSharingReadOnly]; +} + bool NativeWindowMac::HasModalDialog() { return [window_ attachedSheet] != nil; } diff --git a/atom/browser/native_window_views.cc b/atom/browser/native_window_views.cc index 1feb9618ac66..e9ff6952816f 100644 --- a/atom/browser/native_window_views.cc +++ b/atom/browser/native_window_views.cc @@ -728,6 +728,13 @@ void NativeWindowViews::SetIgnoreMouseEvents(bool ignore) { #endif } +void NativeWindowViews::SetContentProtection(bool enable) { +#if defined(OS_WIN) + DWORD affinity = enable ? WDA_MONITOR : WDA_NONE; + ::SetWindowDisplayAffinity(GetAcceleratedWidget(), affinity); +#endif +} + void NativeWindowViews::SetFocusable(bool focusable) { #if defined(OS_WIN) LONG ex_style = ::GetWindowLong(GetAcceleratedWidget(), GWL_EXSTYLE); diff --git a/atom/browser/native_window_views.h b/atom/browser/native_window_views.h index 69d3d27a35da..71f3741f2fad 100644 --- a/atom/browser/native_window_views.h +++ b/atom/browser/native_window_views.h @@ -96,6 +96,7 @@ class NativeWindowViews : public NativeWindow, void SetHasShadow(bool has_shadow) override; bool HasShadow() override; void SetIgnoreMouseEvents(bool ignore) override; + void SetContentProtection(bool enable) override; void SetFocusable(bool focusable) override; void SetMenu(ui::MenuModel* menu_model) override; void SetParentWindow(NativeWindow* parent) override; diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index e95570450b62..e5a9732e2269 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -1050,6 +1050,13 @@ All mouse events happened in this window will be passed to the window below this window, but if this window has focus, it will still receive keyboard events. +### `win.setContentProtection(enable)` _macOS_ _Windows_ + +Prevents the window contents from being captured by other apps. + +On macOS it sets the NSWindow's sharingType to NSWindowSharingNone. +On Windows it calls SetWindowDisplayAffinity with WDA_MONITOR. + ### `win.setFocusable(focusable)` _Windows_ * `focusable` Boolean