From e60baaeec8eeaba41d719c82a5aa9591b5586dec Mon Sep 17 00:00:00 2001 From: Zorg Date: Mon, 21 Sep 2015 23:26:41 -0400 Subject: [PATCH 1/2] Keep Auto Updater alive for small delay before launching app This increases the likelihood that the updated launched app will be brought up front and active. --- Sparkle/Autoupdate/Autoupdate.m | 66 +++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/Sparkle/Autoupdate/Autoupdate.m b/Sparkle/Autoupdate/Autoupdate.m index 0c7db37c..d06a8653 100644 --- a/Sparkle/Autoupdate/Autoupdate.m +++ b/Sparkle/Autoupdate/Autoupdate.m @@ -72,6 +72,13 @@ static const NSTimeInterval SUParentQuitCheckInterval = .25; */ static const NSTimeInterval SUInstallationTimeLimit = 5; +/*! + * Terminate the application after a delay from launching the new update to avoid OS activation issues + * This delay should be be high enough to increase the likelihood that our updated app will be launched up front, + * but should be low enough so that the user doesn't ponder why the updater hasn't finished terminating yet + */ +static const NSTimeInterval SUTerminationTimeDelay = 0.5; + @interface AppInstaller : NSObject /* @@ -97,6 +104,8 @@ static const NSTimeInterval SUInstallationTimeLimit = 5; @property (nonatomic, assign) BOOL shouldRelaunch; @property (nonatomic, assign) BOOL shouldShowUI; +@property (nonatomic, assign) BOOL isTerminating; + @end @implementation AppInstaller @@ -108,6 +117,7 @@ static const NSTimeInterval SUInstallationTimeLimit = 5; @synthesize relaunchPath = _relaunchPath; @synthesize shouldRelaunch = _shouldRelaunch; @synthesize shouldShowUI = _shouldShowUI; +@synthesize isTerminating = _isTerminating; - (instancetype)initWithHostPath:(NSString *)hostPath relaunchPath:(NSString *)relaunchPath parentProcessId:(pid_t)parentProcessId updateFolderPath:(NSString *)updateFolderPath shouldRelaunch:(BOOL)shouldRelaunch shouldShowUI:(BOOL)shouldShowUI { @@ -131,12 +141,14 @@ static const NSTimeInterval SUInstallationTimeLimit = 5; self.terminationListener = nil; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(SUInstallationTimeLimit * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - // Show app icon in the dock - ProcessSerialNumber psn = { 0, kCurrentProcess }; - TransformProcessType(&psn, kProcessTransformToForegroundApplication); + if (!self.isTerminating) { + // Show app icon in the dock + ProcessSerialNumber psn = { 0, kCurrentProcess }; + TransformProcessType(&psn, kProcessTransformToForegroundApplication); + } }); - [self install]; + [self install]; }]; } @@ -150,7 +162,7 @@ static const NSTimeInterval SUInstallationTimeLimit = 5; self.statusController = [[SUStatusController alloc] initWithHost:host]; [self.statusController setButtonTitle:SULocalizedString(@"Cancel Update", @"") target:nil action:Nil isDefault:NO]; [self.statusController beginActionWithTitle:SULocalizedString(@"Installing update...", @"") - maxProgressValue: 0 statusText: @""]; + maxProgressValue: 0 statusText: @""]; [self.statusController showWindow:self]; } @@ -181,21 +193,43 @@ static const NSTimeInterval SUInstallationTimeLimit = 5; }]; } -- (void)cleanupAndTerminateWithPathToRelaunch:(NSString *)relaunchPath __attribute__((noreturn)) +- (void)cleanupAndTerminateWithPathToRelaunch:(NSString *)relaunchPath { + self.isTerminating = YES; + + dispatch_block_t cleanupAndExit = ^{ + NSError *theError = nil; + if (![SUPlainInstaller _removeFileAtPath:self.updateFolderPath error:&theError]) { + SULog(@"Couldn't remove update folder: %@.", theError); + } + + [[NSFileManager defaultManager] removeItemAtPath:[[NSBundle mainBundle] bundlePath] error:NULL]; + + exit(EXIT_SUCCESS); + }; + if (self.shouldRelaunch) { - if (![[NSWorkspace sharedWorkspace] openFile:relaunchPath]) { + // The auto updater can terminate before the newly updated app is finished launching + // If that happens, the OS may not make the updated app active and frontmost + // (Or it does become frontmost, but the OS backgrounds it afterwards.. It's some kind of timing/activation issue that doesn't occur all the time) + // The only remedy I've been able to find is waiting an arbitrary delay before exiting our application + + if (![[NSWorkspace sharedWorkspace] launchApplication:relaunchPath]) { SULog(@"Failed to launch %@", relaunchPath); } + + [self.statusController close]; + + // Don't even think about hiding the app icon from the dock if we've already shown it + // Transforming the app back to a background one has a backfiring effect, decreasing the likelihood + // that the updated app will be brought up front + + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(SUTerminationTimeDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + cleanupAndExit(); + }); + } else { + cleanupAndExit(); } - - NSError *theError = nil; - if (![SUPlainInstaller _removeFileAtPath:self.updateFolderPath error:&theError]) - SULog(@"Couldn't remove update folder: %@.", theError); - - [[NSFileManager defaultManager] removeItemAtPath:[[NSBundle mainBundle] bundlePath] error:NULL]; - - exit(EXIT_SUCCESS); } @end @@ -219,7 +253,7 @@ int main(int __unused argc, const char __unused *argv[]) AppInstaller *appInstaller = [[AppInstaller alloc] initWithHostPath:args[1] relaunchPath:args[2] parentProcessId:[args[3] intValue] - updateFolderPath:args[4] + updateFolderPath:args[4] shouldRelaunch:(args.count > 5) ? [args[5] boolValue] : YES shouldShowUI:shouldShowUI]; [application setDelegate:appInstaller]; From e1002f066249db59fef3b6ec532086351b389a26 Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 25 Sep 2015 18:51:55 -0400 Subject: [PATCH 2/2] Revert using -[NSWorkspace openFile:] --- Sparkle/Autoupdate/Autoupdate.m | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sparkle/Autoupdate/Autoupdate.m b/Sparkle/Autoupdate/Autoupdate.m index d06a8653..3fa016d3 100644 --- a/Sparkle/Autoupdate/Autoupdate.m +++ b/Sparkle/Autoupdate/Autoupdate.m @@ -214,7 +214,8 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.5; // (Or it does become frontmost, but the OS backgrounds it afterwards.. It's some kind of timing/activation issue that doesn't occur all the time) // The only remedy I've been able to find is waiting an arbitrary delay before exiting our application - if (![[NSWorkspace sharedWorkspace] launchApplication:relaunchPath]) { + // Don't use -launchApplication: because we may not be launching an application. Eg: it could be a system prefpane + if (![[NSWorkspace sharedWorkspace] openFile:relaunchPath]) { SULog(@"Failed to launch %@", relaunchPath); }