Browse Source

fix: potential crash when setting vibrancy (#29677)

Shelley Vohr 3 years ago
parent
commit
2ae7e04598
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

@@ -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) {

+ 7 - 0
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', () => {