Browse Source

fix: potential crash when setting vibrancy (#29722)

Co-authored-by: Shelley Vohr <[email protected]>
trop[bot] 3 years ago
parent
commit
10c238c0ea
2 changed files with 11 additions and 4 deletions
  1. 4 4
      shell/browser/native_window_mac.mm
  2. 7 0
      spec-main/api-browser-window-spec.ts

+ 4 - 4
shell/browser/native_window_mac.mm

@@ -1353,8 +1353,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]
@@ -1383,7 +1381,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");
@@ -1440,8 +1438,10 @@ void NativeWindowMac::SetVibrancy(const std::string& type) {
     }
   }
 
-  if (vibrancyType)
+  if (vibrancyType) {
+    vibrancy_type_ = type;
     [effect_view setMaterial:vibrancyType];
+  }
 }
 
 void NativeWindowMac::SetWindowButtonVisibility(bool visible) {

+ 7 - 0
spec-main/api-browser-window-spec.ts

@@ -1597,6 +1597,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', () => {