From 2ae7e04598bc5ba36dcb1c1c06e7df8887cf71dd Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Wed, 16 Jun 2021 08:39:20 +0200 Subject: [PATCH] fix: potential crash when setting vibrancy (#29677) --- shell/browser/native_window_mac.mm | 8 ++++---- spec-main/api-browser-window-spec.ts | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index d72ef1b8bc1..7864ac48f48 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -1360,8 +1360,6 @@ void NativeWindowMac::SetVibrancy(const std::string& type) { return; } - vibrancy_type_ = type; - NSVisualEffectView* effect_view = (NSVisualEffectView*)vibrant_view; if (effect_view == nil) { effect_view = [[[NSVisualEffectView alloc] @@ -1390,7 +1388,7 @@ void NativeWindowMac::SetVibrancy(const std::string& type) { node::Environment* env = node::Environment::GetCurrent(JavascriptEnvironment::GetIsolate()); - NSVisualEffectMaterial vibrancyType; + NSVisualEffectMaterial vibrancyType{}; if (type == "appearance-based") { EmitWarning(env, "NSVisualEffectMaterialAppearanceBased" + dep_warn, "electron"); @@ -1447,8 +1445,10 @@ void NativeWindowMac::SetVibrancy(const std::string& type) { } } - if (vibrancyType) + if (vibrancyType) { + vibrancy_type_ = type; [effect_view setMaterial:vibrancyType]; + } } void NativeWindowMac::SetWindowButtonVisibility(bool visible) { diff --git a/spec-main/api-browser-window-spec.ts b/spec-main/api-browser-window-spec.ts index df764a5bb75..42da9aa4088 100644 --- a/spec-main/api-browser-window-spec.ts +++ b/spec-main/api-browser-window-spec.ts @@ -1609,6 +1609,13 @@ describe('BrowserWindow module', () => { w.setVibrancy('' as any); }).to.not.throw(); }); + + it('does not crash if vibrancy is set to an invalid value', () => { + const w = new BrowserWindow({ show: false }); + expect(() => { + w.setVibrancy('i-am-not-a-valid-vibrancy-type' as any); + }).to.not.throw(); + }); }); ifdescribe(process.platform === 'darwin')('trafficLightPosition', () => {