fix: setContentProtection(true) after hide on Windows (#45889)

fix: content protection after hide on Windows

5789117

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot] 2025-03-05 06:13:36 +01:00 committed by GitHub
parent 179fde9278
commit 6eb4932c68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 60 additions and 11 deletions

View file

@ -724,6 +724,10 @@ void BaseWindow::SetContentProtection(bool enable) {
return window_->SetContentProtection(enable);
}
bool BaseWindow::IsContentProtected() const {
return window_->IsContentProtected();
}
void BaseWindow::SetFocusable(bool focusable) {
return window_->SetFocusable(focusable);
}
@ -1261,6 +1265,7 @@ void BaseWindow::BuildPrototype(v8::Isolate* isolate,
.SetMethod("isDocumentEdited", &BaseWindow::IsDocumentEdited)
.SetMethod("setIgnoreMouseEvents", &BaseWindow::SetIgnoreMouseEvents)
.SetMethod("setContentProtection", &BaseWindow::SetContentProtection)
.SetMethod("_isContentProtected", &BaseWindow::IsContentProtected)
.SetMethod("setFocusable", &BaseWindow::SetFocusable)
.SetMethod("isFocusable", &BaseWindow::IsFocusable)
.SetMethod("setMenu", &BaseWindow::SetMenu)

View file

@ -177,6 +177,7 @@ class BaseWindow : public gin_helper::TrackableObject<BaseWindow>,
bool IsDocumentEdited() const;
void SetIgnoreMouseEvents(bool ignore, gin_helper::Arguments* args);
void SetContentProtection(bool enable);
bool IsContentProtected() const;
void SetFocusable(bool focusable);
bool IsFocusable() const;
void SetMenu(v8::Isolate* isolate, v8::Local<v8::Value> menu);

View file

@ -186,6 +186,7 @@ class NativeWindow : public base::SupportsUserData,
virtual bool IsDocumentEdited() const;
virtual void SetIgnoreMouseEvents(bool ignore, bool forward) = 0;
virtual void SetContentProtection(bool enable) = 0;
virtual bool IsContentProtected() const = 0;
virtual void SetFocusable(bool focusable) {}
virtual bool IsFocusable() const;
virtual void SetMenu(ElectronMenuModel* menu) {}

View file

@ -110,6 +110,7 @@ class NativeWindowMac : public NativeWindow,
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;

View file

@ -1170,6 +1170,10 @@ void NativeWindowMac::SetContentProtection(bool enable) {
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.

View file

@ -1317,17 +1317,15 @@ void NativeWindowViews::SetIgnoreMouseEvents(bool ignore, bool forward) {
void NativeWindowViews::SetContentProtection(bool enable) {
#if BUILDFLAG(IS_WIN)
HWND hwnd = GetAcceleratedWidget();
DWORD affinity = enable ? WDA_EXCLUDEFROMCAPTURE : WDA_NONE;
::SetWindowDisplayAffinity(hwnd, affinity);
if (!layered_) {
// Workaround to prevent black window on screen capture after hiding and
// showing the BrowserWindow.
LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
ex_style |= WS_EX_LAYERED;
::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
layered_ = true;
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
}

View file

@ -116,6 +116,7 @@ class NativeWindowViews : public NativeWindow,
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;

View file

@ -296,6 +296,44 @@ describe('BrowserWindow module', () => {
});
});
ifdescribe(process.platform !== 'linux')('BrowserWindow.getContentProtection', () => {
afterEach(closeAllWindows);
it('can set content protection', async () => {
const w = new BrowserWindow({ show: false });
// @ts-expect-error This is a private API
expect(w._isContentProtected()).to.equal(false);
const shown = once(w, 'show');
w.show();
await shown;
w.setContentProtection(true);
// @ts-expect-error This is a private API
expect(w._isContentProtected()).to.equal(true);
});
it('does not remove content protection after the window is hidden and shown', async () => {
const w = new BrowserWindow({ show: false });
const hidden = once(w, 'hide');
const shown = once(w, 'show');
w.show();
await shown;
w.setContentProtection(true);
w.hide();
await hidden;
w.show();
await shown;
// @ts-expect-error This is a private API
expect(w._isContentProtected()).to.equal(true);
});
});
describe('BrowserWindow.loadURL(url)', () => {
let w: BrowserWindow;
const scheme = 'other';