[vlc-commits] macosx: protect against VLC.app renaming or relocation

Felix Paul Kühne git at videolan.org
Tue Sep 10 18:49:10 CEST 2019


vlc | branch: master | Felix Paul Kühne <felix at feepk.net> | Tue Sep 10 18:47:40 2019 +0200| [d9313c7202547b8a70085e0bdb226df1c226fcf8] | committer: Felix Paul Kühne

macosx: protect against VLC.app renaming or relocation

Renaming or relocating VLC.app during runtime will lead to undefined behavior such as hard to reproduce crashes, panels not loading, etc. This guards against it and demands a restart, quit or allows the user to continue at their discreation (closes #22745)

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=d9313c7202547b8a70085e0bdb226df1c226fcf8
---

 modules/gui/macosx/main/VLCApplication.m | 64 +++++++++++++++++++++++++++++++-
 1 file changed, 63 insertions(+), 1 deletion(-)

diff --git a/modules/gui/macosx/main/VLCApplication.m b/modules/gui/macosx/main/VLCApplication.m
index 498c845549..d9d192d624 100644
--- a/modules/gui/macosx/main/VLCApplication.m
+++ b/modules/gui/macosx/main/VLCApplication.m
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * VLCApplication.m: MacOS X interface module
  *****************************************************************************
- * Copyright (C) 2002-2016 VLC authors and VideoLAN
+ * Copyright (C) 2002-2019 VLC authors and VideoLAN
  *
  * Authors: Derk-Jan Hartman <hartman at videolan.org>
  *          Felix Paul Kühne <fkuehne at videolan dot org>
@@ -28,12 +28,74 @@
  *****************************************************************************/
 
 #import "VLCApplication.h"
+#import "extensions/NSString+Helpers.h"
 
 /*****************************************************************************
  * VLCApplication implementation
  *****************************************************************************/
 
+ at interface VLCApplication ()
+{
+    NSURL *_appLocationURL;
+}
+
+ at end
+
 @implementation VLCApplication
+
+- (instancetype)init
+{
+    self = [super init];
+    if (self) {
+        [[NSNotificationCenter defaultCenter] addObserver:self
+                                                 selector:@selector(appBecameActive:)
+                                                     name:NSApplicationDidBecomeActiveNotification
+                                                   object:nil];
+        /* we need to keep a file reference to the app's current location so we can find out where
+         * it ends-up after being relocated or rename */
+        _appLocationURL = [[[NSBundle mainBundle] bundleURL] fileReferenceURL];
+
+    }
+    return self;
+}
+
+- (void)dealloc
+{
+    [[NSNotificationCenter defaultCenter] removeObserver:self];
+}
+
+- (void)appBecameActive:(NSNotification *)aNotification
+{
+    if ([[[NSBundle mainBundle] bundleURL] checkResourceIsReachableAndReturnError:nil]) {
+        return;
+    }
+
+    NSAlert *alert = [[NSAlert alloc] init];
+    [alert setAlertStyle:NSAlertStyleCritical];
+    [alert setMessageText:_NS("VLC has been moved or renamed")];
+    [alert setInformativeText:_NS("To prevent errors, VLC must be relaunched.\n\nIf you cannot quit immediately, click Continue, then quit and relaunch as soon as possible to avoid problems.")];
+    [alert addButtonWithTitle:_NS("Restart")];
+    [alert addButtonWithTitle:_NS("Quit")];
+    [alert addButtonWithTitle:_NS("Continue (Not Recommended)")];
+
+    NSInteger alertButton = [alert runModal];
+
+    if (alertButton == NSAlertThirdButtonReturn) {
+        return;
+    }
+
+    if (alertButton == NSAlertFirstButtonReturn) {
+        /* terminate and restart
+         * NOTE you may not use [VLCMain relaunchApplication] here as it depends on the app not having moved and WILL crash */
+        [[NSWorkspace sharedWorkspace] launchApplicationAtURL:_appLocationURL.absoluteURL
+                                                      options:NSWorkspaceLaunchNewInstance
+                                                configuration:@{}
+                                                        error:nil];
+    }
+
+    [self terminate:self];
+}
+
 // when user selects the quit menu from dock it sends a terminate:
 // but we need to send a stop: to properly exits libvlc.
 // However, we are not able to change the action-method sent by this standard menu item.



More information about the vlc-commits mailing list