8000 fix: `setContentProtection(true)` after hide on Windows by trop[bot] · Pull Request #45889 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions shell/browser/api/electron_api_base_window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions shell/browser/api/electron_api_base_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions shell/browser/native_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
1 change: 1 addition & 0 deletions shell/browser/native_window_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions shell/browser/native_window_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1170,6 +1170,10 @@ static bool FromV8(v8::Isolate* isolate,
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.
Expand Down
20 changes: 9 additions & 11 deletions shell/browser/native_window_views.cc
ED4F
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions shell/browser/native_window_views.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions spec/api-browser-window-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
0