From 23e027d02f0a54f7479a2a2d10863df232dbd1fc Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 30 Jul 2016 14:18:39 -0400 Subject: [PATCH] Have progress agent terminate after relaunching, not after stopping progress --- Autoupdate/AppInstaller.m | 23 ++--- .../InstallerProgressAppController.m | 90 +++++++++---------- 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/Autoupdate/AppInstaller.m b/Autoupdate/AppInstaller.m index 5d230ba2..3f0c66d2 100644 --- a/Autoupdate/AppInstaller.m +++ b/Autoupdate/AppInstaller.m @@ -563,22 +563,22 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; return; } - // Ask the updater if it is still alive - // If they are, we will receive a pong response back - // Reset if we received a pong just to be on the safe side - self.receivedUpdaterPong = NO; - [self.communicator handleMessageWithIdentifier:SUUpdaterAlivePing data:[NSData data]]; - - // Launch our installer progress UI tool if only after a certain amount of time passes, + // Show our installer progress UI tool if only after a certain amount of time passes, // and if our installer is silent (i.e, doesn't show progress on its own) - __block BOOL shouldLaunchInstallerProgress = YES; + __block BOOL shouldShowUIProgress = YES; if (self.shouldShowUI && [self.installer canInstallSilently]) { + // Ask the updater if it is still alive + // If they are, we will receive a pong response back + // Reset if we received a pong just to be on the safe side + self.receivedUpdaterPong = NO; + [self.communicator handleMessageWithIdentifier:SUUpdaterAlivePing data:[NSData data]]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(SUDisplayProgressTimeDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // Make sure we're still eligible for showing the installer progress // Also if the updater process is still alive, showing the progress should not be our duty // if the communicator object is nil, the updater definitely isn't alive. However, if it is not nil, // this does not necessarily mean the updater is alive, so we should also check if we got a recent response back from the updater - if (shouldLaunchInstallerProgress && (!self.receivedUpdaterPong || self.communicator == nil)) { + if (shouldShowUIProgress && (!self.receivedUpdaterPong || self.communicator == nil)) { [self.agentConnection.agent showProgress]; } }); @@ -608,11 +608,11 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; self.performedStage3Installation = YES; dispatch_async(dispatch_get_main_queue(), ^{ - // Make sure to terminate our displayed progress before we move onto cleanup & relaunch + // Make sure to stop our displayed progress before we move onto cleanup & relaunch // This will also stop the agent from broadcasting the status info service, which we want to do before // we relaunch the app because the relaunched app could check the service upon launch.. [self.agentConnection.agent stopProgress]; - shouldLaunchInstallerProgress = NO; + shouldShowUIProgress = NO; [self.communicator handleMessageWithIdentifier:SUInstallationFinishedStage3 data:[NSData data]]; @@ -628,6 +628,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; pathToRelaunch = self.relaunchPath; } + // This will also signal to the agent that it will terminate soon [self.agentConnection.agent relaunchPath:pathToRelaunch]; } diff --git a/Sparkle/InstallerProgress/InstallerProgressAppController.m b/Sparkle/InstallerProgress/InstallerProgressAppController.m index dc531fc6..f9d8813b 100644 --- a/Sparkle/InstallerProgress/InstallerProgressAppController.m +++ b/Sparkle/InstallerProgress/InstallerProgressAppController.m @@ -162,18 +162,19 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; - (void)registerRelaunchBundlePath:(NSString *)relaunchBundlePath reply:(void (^)(NSNumber *))reply { dispatch_async(dispatch_get_main_queue(), ^{ - assert(relaunchBundlePath != nil); - NSBundle *relaunchBundle = [NSBundle bundleWithPath:relaunchBundlePath]; - if (relaunchBundle == nil) { - [self cleanupAndExitWithStatus:EXIT_FAILURE]; + if (relaunchBundlePath != nil && !self.willTerminate) { + NSBundle *relaunchBundle = [NSBundle bundleWithPath:relaunchBundlePath]; + if (relaunchBundle == nil) { + [self cleanupAndExitWithStatus:EXIT_FAILURE]; + } + + NSRunningApplication *runningApplication = [SUApplicationInfo runningApplicationWithBundle:relaunchBundle]; + NSNumber *processIdentifier = (runningApplication == nil || runningApplication.terminated) ? nil : @(runningApplication.processIdentifier); + + reply(processIdentifier); + + self.repliedToRegistration = YES; } - - NSRunningApplication *runningApplication = [SUApplicationInfo runningApplicationWithBundle:relaunchBundle]; - NSNumber *processIdentifier = (runningApplication == nil || runningApplication.terminated) ? nil : @(runningApplication.processIdentifier); - - reply(processIdentifier); - - self.repliedToRegistration = YES; }); } @@ -189,8 +190,18 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; - (void)relaunchPath:(NSString *)pathToRelaunch { dispatch_async(dispatch_get_main_queue(), ^{ - if (![[NSWorkspace sharedWorkspace] openFile:pathToRelaunch]) { - SULog(@"Error: Failed to relaunch bundle at %@", pathToRelaunch); + if (!self.willTerminate) { + if (![[NSWorkspace sharedWorkspace] openFile:pathToRelaunch]) { + SULog(@"Error: Failed to relaunch bundle at %@", pathToRelaunch); + } + + // Delay termination for a little bit to better increase the chance the updated application when relaunched will be the frontmost application + // This is related to macOS activation issues when terminating a frontmost application happens right before launching another app + + self.willTerminate = YES; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(SUTerminationTimeDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [self cleanupAndExitWithStatus:EXIT_SUCCESS]; + }); } }); } @@ -198,44 +209,33 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.3; - (void)showProgress { dispatch_async(dispatch_get_main_queue(), ^{ - // Show app icon in the dock - ProcessSerialNumber psn = { 0, kCurrentProcess }; - TransformProcessType(&psn, kProcessTransformToForegroundApplication); - - // Note: the application icon needs to be set after showing the icon in the dock - self.application.applicationIconImage = [SUApplicationInfo bestIconForBundle:self.hostBundle]; - - // Activate ourselves otherwise we will probably be in the background - [self.application activateIgnoringOtherApps:YES]; - - [self.delegate installerProgressShouldDisplayWithBundle:self.hostBundle]; + if (!self.willTerminate) { + // Show app icon in the dock + ProcessSerialNumber psn = { 0, kCurrentProcess }; + TransformProcessType(&psn, kProcessTransformToForegroundApplication); + + // Note: the application icon needs to be set after showing the icon in the dock + self.application.applicationIconImage = [SUApplicationInfo bestIconForBundle:self.hostBundle]; + + // Activate ourselves otherwise we will probably be in the background + [self.application activateIgnoringOtherApps:YES]; + + [self.delegate installerProgressShouldDisplayWithBundle:self.hostBundle]; + } }); } - (void)stopProgress { dispatch_async(dispatch_get_main_queue(), ^{ - [self _stopProgress]; - }); -} - -- (void)_stopProgress -{ - [self.statusInfo invalidate]; - self.statusInfo = nil; - - // Dismiss any UI immediately, but delay termination for a little bit to better increase the chance - // the updated application when relaunched will be the frontmost application - // This is related to macOS activation issues when terminating a frontmost application happens right before - // launching another app - - [self.delegate installerProgressShouldStop]; - self.delegate = nil; - - self.willTerminate = YES; - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(SUTerminationTimeDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - // Exit right away, don't go through the apple event process again - [self cleanupAndExitWithStatus:EXIT_SUCCESS]; + // Dismiss any UI immediately + [self.delegate installerProgressShouldStop]; + self.delegate = nil; + + // No need to broadcast status service anymore + // In fact we shouldn't when we decide to relaunch the update + [self.statusInfo invalidate]; + self.statusInfo = nil; }); }