From fdee19ec69ae330dae9b23033a791be9c61c2840 Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 29 Apr 2016 00:37:21 -0400 Subject: [PATCH] Add installation did finish event. This is not complete. For sparkle-cli and out-of-process updaters, I added a did finish installing event. I also have logic dealing with whether an update can be silently installed or not. I made closing status windows before app termination a reponsibility for the user driver now, not the installer drivers. I improved checking for updates before starting the updater, if Sparle didn't ask the user for permission yet. Currently, Autoupdate can persist in the background even when the app is terminated, if we don't have sufficient privilleges to install right away for example, or if an update isn't silent. After much thinking, I need to ditch this strategy because: 1. An app can be manually replaced and so the installer would have to monitor that 2. We leave a zombie process running --- Sparkle/Autoupdate/AppInstaller.m | 77 ++++++++++++--------- Sparkle/Autoupdate/SUMessageTypes.h | 3 +- Sparkle/Autoupdate/SUMessageTypes.m | 3 + Sparkle/Autoupdate/TerminationListener.h | 4 +- Sparkle/Autoupdate/TerminationListener.m | 71 +++++++++++-------- Sparkle/SUAutomaticUpdateDriver.m | 18 ++--- Sparkle/SUCoreBasedUpdateDriver.h | 6 +- Sparkle/SUCoreBasedUpdateDriver.m | 23 ++++-- Sparkle/SUInstallerDriver.h | 4 +- Sparkle/SUInstallerDriver.m | 35 ++++++++-- Sparkle/SUStandardUserDriver.m | 14 +++- Sparkle/SUUIBasedUpdateDriver.m | 28 ++++---- Sparkle/SUUpdater.m | 14 ++-- Sparkle/SUUserDriver.h | 23 ++++-- TestApplication/SUPopUpTitlebarUserDriver.m | 12 +++- Tests/SUUpdaterTest.m | 4 +- sparkle-cli/AppDelegate.m | 5 +- sparkle-cli/SUCommandLineUserDriver.m | 11 ++- 18 files changed, 238 insertions(+), 117 deletions(-) diff --git a/Sparkle/Autoupdate/AppInstaller.m b/Sparkle/Autoupdate/AppInstaller.m index d29b2a38..d38646e0 100644 --- a/Sparkle/Autoupdate/AppInstaller.m +++ b/Sparkle/Autoupdate/AppInstaller.m @@ -59,6 +59,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @property (nonatomic) BOOL performedStage1Installation; @property (nonatomic) BOOL performedStage2Installation; @property (nonatomic) BOOL performedStage3Installation; +@property (nonatomic) BOOL waitingForStage2Feedback; @end @@ -79,6 +80,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; @synthesize performedStage1Installation = _performedStage1Installation; @synthesize performedStage2Installation = _performedStage2Installation; @synthesize performedStage3Installation = _performedStage3Installation; +@synthesize waitingForStage2Feedback = _waitingForStage2Feedback; /* * hostPath - path to host (original) application @@ -379,7 +381,9 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; dispatch_async(dispatch_get_main_queue(), ^{ if (self.performedStage1Installation) { // Resume the installation if we aren't done with stage 2 yet, and remind the client we are prepared to relaunch - [self resumeInstallation]; + dispatch_async(self.installerQueue, ^{ + [self performStage2InstallationIfNeeded]; + }); } }); }]; @@ -424,18 +428,32 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; dispatch_async(dispatch_get_main_queue(), ^{ self.installer = installer; - NSData *silentData = [NSData dataWithBytes:&canPerformSilentInstall length:sizeof(canPerformSilentInstall)]; - [self.remotePort sendMessageWithIdentifier:SUInstallationFinishedStage1 data:silentData completion:^(BOOL success) { + uint8_t targetTerminated = (uint8_t)self.terminationListener.terminated; + + uint8_t sendInformation[] = {canPerformSilentInstall, targetTerminated}; + + NSData *sendData = [NSData dataWithBytes:sendInformation length:sizeof(sendInformation)]; + + self.waitingForStage2Feedback = (canPerformSilentInstall == 0); + + [self.remotePort sendMessageWithIdentifier:SUInstallationFinishedStage1 data:sendData completion:^(BOOL success) { if (!success) { SULog(@"Error sending stage 1 finish"); + + if (self.waitingForStage2Feedback) { + SULog(@"Exiting because sending Stage 1 Installation Failed and we need to wait for continuation response"); + [self cleanupAndExitWithStatus:EXIT_FAILURE]; + } } }]; self.performedStage1Installation = YES; - // Stage 2 can still be run before we finish installation - // if the updater requests for it before the app is terminated - [self finishInstallationAfterHostTermination]; + if (!self.waitingForStage2Feedback) { + // Stage 2 can still be run before we finish installation + // if the updater requests for it before the app is terminated + [self finishInstallationAfterHostTermination]; + } }); }); } @@ -447,6 +465,7 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; } if (self.shouldShowUI && [self.installer mayNeedToRequestAuthorization]) { +#warning should probably do this on main thread // We should activate our app so that the auth prompt will be active [[NSApplication sharedApplication] activateIgnoringOtherApps:YES]; } @@ -462,43 +481,29 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; }); } else { self.performedStage2Installation = YES; - } -} - -// Can be called multiple times without harm -- (void)resumeInstallation -{ - dispatch_async(self.installerQueue, ^{ - [self performStage2InstallationIfNeeded]; dispatch_async(dispatch_get_main_queue(), ^{ - [self.remotePort sendMessageWithIdentifier:SUInstallationFinishedStage2 data:[NSData data] completion:^(BOOL success) { + uint8_t targetTerminated = (uint8_t)self.terminationListener.terminated; + + NSData *sendData = [NSData dataWithBytes:&targetTerminated length:sizeof(targetTerminated)]; + [self.remotePort sendMessageWithIdentifier:SUInstallationFinishedStage2 data:sendData completion:^(BOOL success) { if (!success) { SULog(@"Error sending stage 2 finish"); } }]; - // Stage 3 will perform when the target terminates + if (self.waitingForStage2Feedback) { + self.waitingForStage2Feedback = NO; + + [self finishInstallationAfterHostTermination]; + } // otherwise stage 3 will perform when the target terminates }); - }); + } } - (void)finishInstallationAfterHostTermination { - [self.terminationListener startListeningWithCompletion:^(BOOL success) { - self.terminationListener = nil; - - if (!success) { - SULog(@"Timed out waiting for target to terminate. Target path is %@", self.host.bundlePath); - [self.installer cleanup]; - self.installer = nil; - - dispatch_async(dispatch_get_main_queue(), ^{ - [self cleanupAndExitWithStatus:EXIT_FAILURE]; - }); - return; - } - + [self.terminationListener startListeningWithCompletion:^{ // Launch our installer progress UI tool if only after a certain amount of time passes __block NSRunningApplication *installerProgressRunningApplication = nil; __block BOOL shouldLaunchInstallerProgress = YES; @@ -513,7 +518,9 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(SUDisplayProgressTimeDelay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - if (shouldLaunchInstallerProgress) { + // 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 (shouldLaunchInstallerProgress && self.remotePort == nil) { NSError *launchError = nil; NSArray *arguments = @[self.host.bundlePath]; @@ -552,6 +559,12 @@ static const NSTimeInterval SUDisplayProgressTimeDelay = 0.7; [installerProgressRunningApplication terminate]; shouldLaunchInstallerProgress = NO; + [self.remotePort sendMessageWithIdentifier:SUInstallationFinishedStage3 data:[NSData data] completion:^(BOOL success) { + if (!success) { + SULog(@"Error sending stage 3 finish"); + } + }]; + // Invalidate the local port before re-launching the updated app // If we don't do this, the updated app could think the installation can be resumable, // if it checks for updates immediately on launch diff --git a/Sparkle/Autoupdate/SUMessageTypes.h b/Sparkle/Autoupdate/SUMessageTypes.h index 6df6f45a..dfa1a9f7 100644 --- a/Sparkle/Autoupdate/SUMessageTypes.h +++ b/Sparkle/Autoupdate/SUMessageTypes.h @@ -24,7 +24,8 @@ typedef NS_ENUM(int32_t, SUInstallerMessageType) SUValidationStarted = 4, SUInstallationStartedStage1 = 5, SUInstallationFinishedStage1 = 6, - SUInstallationFinishedStage2 = 7 + SUInstallationFinishedStage2 = 7, + SUInstallationFinishedStage3 = 8 }; typedef NS_ENUM(int32_t, SUUpdaterMessageType) diff --git a/Sparkle/Autoupdate/SUMessageTypes.m b/Sparkle/Autoupdate/SUMessageTypes.m index 0fcd39da..84a0fc8e 100644 --- a/Sparkle/Autoupdate/SUMessageTypes.m +++ b/Sparkle/Autoupdate/SUMessageTypes.m @@ -37,6 +37,9 @@ BOOL SUInstallerMessageTypeIsLegal(SUInstallerMessageType oldMessageType, SUInst case SUInstallationFinishedStage2: legal = (oldMessageType == SUInstallationFinishedStage1); break; + case SUInstallationFinishedStage3: + legal = (oldMessageType == SUInstallationFinishedStage2); + break; } return legal; } diff --git a/Sparkle/Autoupdate/TerminationListener.h b/Sparkle/Autoupdate/TerminationListener.h index c939b870..977ccce6 100644 --- a/Sparkle/Autoupdate/TerminationListener.h +++ b/Sparkle/Autoupdate/TerminationListener.h @@ -14,7 +14,9 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)initWithBundle:(NSBundle *)bundle; -- (void)startListeningWithCompletion:(void (^)(BOOL))completionBlock; +@property (nonatomic, readonly) BOOL terminated; + +- (void)startListeningWithCompletion:(void (^)(void))completionBlock; @end diff --git a/Sparkle/Autoupdate/TerminationListener.m b/Sparkle/Autoupdate/TerminationListener.m index 03e22798..4369e62d 100644 --- a/Sparkle/Autoupdate/TerminationListener.m +++ b/Sparkle/Autoupdate/TerminationListener.m @@ -10,14 +10,16 @@ @interface TerminationListener () -@property (nonatomic, readonly) NSRunningApplication *runningApplication; -@property (nonatomic, copy) void (^completionBlock)(BOOL); +@property (nonatomic, readonly) NSBundle *bundle; +@property (nonatomic) NSRunningApplication *listeningApplication; +@property (nonatomic, copy) void (^completionBlock)(void); @end @implementation TerminationListener -@synthesize runningApplication = _runningApplication; +@synthesize bundle = _bundle; +@synthesize listeningApplication = _listeningApplication; @synthesize completionBlock = _completionBlock; - (instancetype)initWithBundle:(NSBundle *)bundle @@ -26,47 +28,58 @@ return nil; } - NSString *bundlePath = bundle.bundlePath; - NSString *bundleIdentifier = bundle.bundleIdentifier; - - NSArray *runningApplications = - (bundleIdentifier != nil) ? - [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleIdentifier] : - [[NSWorkspace sharedWorkspace] runningApplications]; - - for (NSRunningApplication *runningApplication in runningApplications) { - // Comparing the URLs hasn't worked well for me in practice, so I'm comparing the file paths instead - if ([runningApplication.bundleURL.path isEqualToString:bundlePath]) { - _runningApplication = runningApplication; - break; - } - } + _bundle = bundle; return self; } -- (void)cleanupWithSuccess:(BOOL)success completion:(void (^)(BOOL))completionBlock +// Don't cache the running application instance because it can change later any number of times after initialization +// If for example, the user re-launches the application and we haven't started listening yet +- (NSRunningApplication *)runningApplication { - completionBlock(success); + NSString *bundlePath = self.bundle.bundlePath; + NSString *bundleIdentifier = self.bundle.bundleIdentifier; + + NSArray *runningApplications = + (bundleIdentifier != nil) ? + [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleIdentifier] : + [[NSWorkspace sharedWorkspace] runningApplications]; + + for (NSRunningApplication *runningApplication in runningApplications) { + // Comparing the URLs hasn't worked well for me in practice, so I'm comparing the file paths instead + if ([runningApplication.bundleURL.path isEqualToString:bundlePath]) { + return runningApplication; + } + } + return nil; } -- (void)startListeningWithCompletion:(void (^)(BOOL))completionBlock +- (BOOL)terminated { - BOOL alreadyTerminated = (self.runningApplication == nil || self.runningApplication.isTerminated); - if (alreadyTerminated) { - [self cleanupWithSuccess:YES completion:completionBlock]; + NSRunningApplication *runningApplication = [self runningApplication]; + return (runningApplication == nil || runningApplication.isTerminated); +} + +- (void)startListeningWithCompletion:(void (^)(void))completionBlock +{ + NSRunningApplication *runningApplication = [self runningApplication]; + if (runningApplication == nil || runningApplication.terminated) { + completionBlock(); } else { self.completionBlock = completionBlock; - [self.runningApplication addObserver:self forKeyPath:NSStringFromSelector(@selector(isTerminated)) options:NSKeyValueObservingOptionNew context:NULL]; + + // We must strongly reference the listening application, so it doesn't deallocate while we have KVO observer to it + self.listeningApplication = runningApplication; + [self.listeningApplication addObserver:self forKeyPath:NSStringFromSelector(@selector(isTerminated)) options:NSKeyValueObservingOptionNew context:NULL]; } } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)__unused change context:(void *)__unused context { - if (object == self.runningApplication && [keyPath isEqualToString:NSStringFromSelector(@selector(isTerminated))]) { - if (self.runningApplication.isTerminated) { - [self.runningApplication removeObserver:self forKeyPath:keyPath]; - [self cleanupWithSuccess:YES completion:self.completionBlock]; + if (object == self.listeningApplication && [keyPath isEqualToString:NSStringFromSelector(@selector(isTerminated))]) { + if (self.listeningApplication.terminated) { + [self.listeningApplication removeObserver:self forKeyPath:keyPath]; + self.completionBlock(); self.completionBlock = nil; } } diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index 00e3a660..796d906a 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -22,7 +22,7 @@ @property (nonatomic, readonly) SUCoreBasedUpdateDriver *coreDriver; @property (nonatomic) BOOL foundCriticalUpdate; -@property (nonatomic) BOOL canInstallSilently; +@property (nonatomic) BOOL willInstallSilently; @end @@ -30,7 +30,7 @@ @synthesize coreDriver = _coreDriver; @synthesize foundCriticalUpdate = _foundCriticalUpdate; -@synthesize canInstallSilently = _canInstallSilently; +@synthesize willInstallSilently = _willInstallSilently; - (instancetype)initWithHost:(SUHost *)host sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater updaterDelegate:(nullable id )updaterDelegate { @@ -60,18 +60,20 @@ [self.coreDriver downloadUpdateFromAppcastItem:updateItem]; } -- (void)installerDidFinishPreparationAndCanInstallSilently:(BOOL)canInstallSilently +- (void)installerDidFinishPreparationAndWillInstallImmediately:(BOOL)willInstallImmediately silently:(BOOL)willInstallSilently { - self.canInstallSilently = canInstallSilently; + self.willInstallSilently = willInstallSilently; - // We are done and can safely abort now - // The installer tool will keep the installation alive - [self abortUpdate]; + if (!willInstallImmediately) { + // We are done and can safely abort now + // The installer tool will keep the installation alive + [self abortUpdate]; + } } - (BOOL)basicDriverShouldSignalShowingUpdateImmediately { - return (!self.canInstallSilently || self.foundCriticalUpdate); + return (!self.willInstallSilently || self.foundCriticalUpdate); } - (void)basicDriverIsRequestingAbortUpdateWithError:(NSError *)error diff --git a/Sparkle/SUCoreBasedUpdateDriver.h b/Sparkle/SUCoreBasedUpdateDriver.h index 2de51ea0..83b9b115 100644 --- a/Sparkle/SUCoreBasedUpdateDriver.h +++ b/Sparkle/SUCoreBasedUpdateDriver.h @@ -19,7 +19,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem; -- (void)installerDidFinishPreparationAndCanInstallSilently:(BOOL)canInstallSilently; +- (void)installerDidFinishPreparationAndWillInstallImmediately:(BOOL)willInstallImmediately silently:(BOOL)willInstallSilently; - (void)coreDriverIsRequestingAbortUpdateWithError:(nullable NSError *)error; @@ -43,7 +43,9 @@ NS_ASSUME_NONNULL_BEGIN - (void)installerDidExtractUpdateWithProgress:(double)progress; -- (void)coreDriverIsRequestingAppTermination; +- (void)installerIsRequestingAppTermination; + +- (void)installerDidFinishInstallation; @end diff --git a/Sparkle/SUCoreBasedUpdateDriver.m b/Sparkle/SUCoreBasedUpdateDriver.m index f2d40f8f..1d2866de 100644 --- a/Sparkle/SUCoreBasedUpdateDriver.m +++ b/Sparkle/SUCoreBasedUpdateDriver.m @@ -193,9 +193,9 @@ } } -- (void)installerDidFinishPreparationAndCanInstallSilently:(BOOL)canInstallSilently +- (void)installerDidFinishPreparationAndWillInstallImmediately:(BOOL)willInstallImmediately silently:(BOOL)willInstallSilently { - [self.delegate installerDidFinishPreparationAndCanInstallSilently:canInstallSilently]; + [self.delegate installerDidFinishPreparationAndWillInstallImmediately:willInstallImmediately silently:willInstallSilently]; } - (void)finishInstallationWithResponse:(SUInstallUpdateStatus)installUpdateStatus @@ -213,20 +213,31 @@ } } -- (void)installerIsRequestingAppTermination +- (void)installerWillFinishInstallation { if ([self.updaterDelegate respondsToSelector:@selector(updater:willInstallUpdate:)]) { [self.updaterDelegate updater:self.updater willInstallUpdate:self.updateItem]; } +#warning should only notify if we are really about to restart the target... [[NSNotificationCenter defaultCenter] postNotificationName:SUUpdaterWillRestartNotification object:self]; if ([self.updaterDelegate respondsToSelector:@selector(updaterWillRelaunchApplication:)]) { [self.updaterDelegate updaterWillRelaunchApplication:self.updater]; } - +} + +- (void)installerDidFinishInstallation +{ + if ([self.delegate respondsToSelector:@selector(installerDidFinishInstallation)]) { + [self.delegate installerDidFinishInstallation]; + } +} + +- (void)installerIsRequestingAppTermination +{ // If they don't respond or do anything, we'll just install after the user terminates the app anyway - if ([self.delegate respondsToSelector:@selector(coreDriverIsRequestingAppTermination)]) { - [self.delegate coreDriverIsRequestingAppTermination]; + if ([self.delegate respondsToSelector:@selector(installerIsRequestingAppTermination)]) { + [self.delegate installerIsRequestingAppTermination]; } } diff --git a/Sparkle/SUInstallerDriver.h b/Sparkle/SUInstallerDriver.h index 9f6c46e1..1bd4dbf3 100644 --- a/Sparkle/SUInstallerDriver.h +++ b/Sparkle/SUInstallerDriver.h @@ -17,8 +17,10 @@ NS_ASSUME_NONNULL_BEGIN - (void)installerDidStartInstalling; - (void)installerDidExtractUpdateWithProgress:(double)progress; -- (void)installerDidFinishPreparationAndCanInstallSilently:(BOOL)canInstallSilently; +- (void)installerDidFinishPreparationAndWillInstallImmediately:(BOOL)willInstallImmediately silently:(BOOL)willInstallSilently; - (void)installerIsRequestingAppTermination; +- (void)installerWillFinishInstallation; +- (void)installerDidFinishInstallation; - (void)installerIsRequestingAbortInstallWithError:(nullable NSError *)error; - (void)installerDidFailToApplyDeltaUpdate; diff --git a/Sparkle/SUInstallerDriver.m b/Sparkle/SUInstallerDriver.m index 9bfa395c..089ac2f3 100644 --- a/Sparkle/SUInstallerDriver.m +++ b/Sparkle/SUInstallerDriver.m @@ -37,6 +37,7 @@ @property (nonatomic, readonly) NSBundle *sparkleBundle; @property (nonatomic, weak, readonly) id delegate; @property (nonatomic) SUInstallerMessageType currentStage; +@property (nonatomic) BOOL startedInstalling; @property (nonatomic) SULocalMessagePort *localPort; @property (nonatomic) id remotePort; @property (nonatomic) BOOL postponedOnce; @@ -56,6 +57,7 @@ @synthesize sparkleBundle = _sparkleBundle; @synthesize delegate = _delegate; @synthesize currentStage = _currentStage; +@synthesize startedInstalling = _startedInstalling; @synthesize localPort = _localPort; @synthesize remotePort = _remotePort; @synthesize postponedOnce = _postponedOnce; @@ -303,6 +305,7 @@ } else if (identifier == SUInstallationStartedStage1) { self.currentStage = identifier; [self.delegate installerDidStartInstalling]; + self.startedInstalling = YES; } else if (identifier == SUInstallationFinishedStage1) { self.currentStage = identifier; @@ -322,21 +325,45 @@ } BOOL canInstallSilently = NO; - if (data.length == sizeof(uint8_t)) { + if (data.length >= sizeof(uint8_t)) { canInstallSilently = (BOOL)*(const uint8_t *)data.bytes; } - [self.delegate installerDidFinishPreparationAndCanInstallSilently:canInstallSilently]; + BOOL hasTargetTerminated = NO; + if (data.length >= sizeof(uint8_t) * 2) { + hasTargetTerminated = (BOOL)*((const uint8_t *)data.bytes + 1); + } + + [self.delegate installerDidFinishPreparationAndWillInstallImmediately:(hasTargetTerminated && canInstallSilently) silently:canInstallSilently]; } else if (identifier == SUInstallationFinishedStage2) { - // Don't have to store current stage because we're severing our connection to the installer + self.currentStage = identifier; + if (!self.startedInstalling) { + // It's possible we can start from resuming to stage 2 rather than doing stage 1 again, so we should notify to start installing if we haven't done so already + self.startedInstalling = YES; + [self.delegate installerDidStartInstalling]; + } + + BOOL hasTargetTerminated = NO; + if (data.length >= sizeof(uint8_t)) { + hasTargetTerminated = (BOOL)*(const uint8_t *)data.bytes; + } [self.remotePort invalidate]; self.remotePort = nil; + [self.delegate installerWillFinishInstallation]; + + if (!hasTargetTerminated) { + [self.delegate installerIsRequestingAppTermination]; + } + } else if (identifier == SUInstallationFinishedStage3) { + self.currentStage = identifier; + [self.localPort invalidate]; self.localPort = nil; - [self.delegate installerIsRequestingAppTermination]; + [self.delegate installerDidFinishInstallation]; + [self.delegate installerIsRequestingAbortInstallWithError:nil]; } } diff --git a/Sparkle/SUStandardUserDriver.m b/Sparkle/SUStandardUserDriver.m index 073e53b6..0d00dd80 100644 --- a/Sparkle/SUStandardUserDriver.m +++ b/Sparkle/SUStandardUserDriver.m @@ -160,7 +160,7 @@ #pragma mark Install & Relaunch Update -- (void)showExtractionFinishedAndReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler +- (void)showReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler { dispatch_async(dispatch_get_main_queue(), ^{ [self.statusController beginActionWithTitle:SULocalizedString(@"Ready to Install", nil) maxProgressValue:1.0 statusText:nil]; @@ -349,11 +349,23 @@ }); } +- (void)showUpdateInstallationDidFinish +{ + //... +} + #pragma mark Aborting Everything - (void)terminateApplication { dispatch_async(dispatch_get_main_queue(), ^{ + // if a user chooses to NOT relaunch the app (as is the case with WebKit + // when it asks you if you are sure you want to close the app with multiple + // tabs open), the status window still stays on the screen and obscures + // other windows; with this fix, it doesn't + [self.statusController close]; + self.statusController = nil; + [[NSApplication sharedApplication] terminate:nil]; }); } diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index c34c4b15..a01df943 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -95,6 +95,7 @@ if (!self.resumingUpdate) { [self.coreDriver downloadUpdateFromAppcastItem:updateItem]; } else { +#warning why should we have to relaunch?? [self.coreDriver finishInstallationWithResponse:SUInstallAndRelaunchUpdateNow]; } break; @@ -156,24 +157,25 @@ [self.userDriver showExtractionReceivedProgress:progress]; } -- (void)installerDidFinishPreparationAndCanInstallSilently:(BOOL)__unused canInstallSilently +- (void)installerDidFinishPreparationAndWillInstallImmediately:(BOOL)willInstallImmediately silently:(BOOL)__unused willInstallSilently { - [self.userDriver showExtractionFinishedAndReadyToInstallAndRelaunch:^(SUInstallUpdateStatus installUpdateStatus) { - dispatch_async(dispatch_get_main_queue(), ^{ - [self.coreDriver finishInstallationWithResponse:installUpdateStatus]; - }); - }]; + if (!willInstallImmediately) { + [self.userDriver showReadyToInstallAndRelaunch:^(SUInstallUpdateStatus installUpdateStatus) { + dispatch_async(dispatch_get_main_queue(), ^{ + [self.coreDriver finishInstallationWithResponse:installUpdateStatus]; + }); + }]; + } } -- (void)coreDriverIsRequestingAppTermination +- (void)installerIsRequestingAppTermination { [self.userDriver terminateApplication]; - - // if a user chooses to NOT relaunch the app (as is the case with WebKit - // when it asks you if you are sure you want to close the app with multiple - // tabs open), the status window still stays on the screen and obscures - // other windows; with this fix, it doesn't - [self.userDriver dismissUpdateInstallation]; +} + +- (void)installerDidFinishInstallation +{ + [self.userDriver showUpdateInstallationDidFinish]; } - (BOOL)basicDriverShouldSignalShowingUpdateImmediately diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index 555f66de..9f0684e7 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -222,6 +222,10 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification if ([self.host objectForUserDefaultsKey:SUEnableAutomaticChecksKey]) { shouldPrompt = NO; } + // If the developer wants to check for updates, we shouldn't bug the user about a prompt yet + else if (self.preStartedScheduledUpdateBlock != nil) { + shouldPrompt = NO; + } // Does the delegate want to take care of the logic for when we should ask permission to update? else if ([self.delegate respondsToSelector:@selector(updaterShouldPromptForPermissionToCheckForUpdates:)]) { shouldPrompt = [self.delegate updaterShouldPromptForPermissionToCheckForUpdates:self]; @@ -253,14 +257,8 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification SUUpdater *strongSelf = weakSelf; if (strongSelf != nil) { [strongSelf updatePermissionPromptFinishedWithResult:result]; - - if (strongSelf.preStartedScheduledUpdateBlock != nil) { - strongSelf.preStartedScheduledUpdateBlock(); - strongSelf.preStartedScheduledUpdateBlock = nil; - } else { - // Schedule checks, but make sure we ignore the delayed call from KVO - [strongSelf resetUpdateCycle]; - } + // Schedule checks, but make sure we ignore the delayed call from KVO + [strongSelf resetUpdateCycle]; } }); }]; diff --git a/Sparkle/SUUserDriver.h b/Sparkle/SUUserDriver.h index ca232176..6cf06db8 100644 --- a/Sparkle/SUUserDriver.h +++ b/Sparkle/SUUserDriver.h @@ -216,9 +216,10 @@ NS_ASSUME_NONNULL_BEGIN - (void)showExtractionReceivedProgress:(double)progress; /*! - * Show the user that the update finished extracting and is ready to install + * Show the user that the update is ready to install * - * Let the user know that the update is ready and ask them whether they want to install or not + * Let the user know that the update is ready and ask them whether they want to install or not. + * Note if the target application is already terminated and an update can be performed silently, this method may not be invoked. * * @param installUpdateHandler A reply of SUInstallAndRelaunchUpdateNow installs the update immediately. * A reply of SUDismissUpdateInstallation dismisses the update installation. Note the update may still be installed after @@ -226,7 +227,7 @@ NS_ASSUME_NONNULL_BEGIN * * This can be called from any thread */ -- (void)showExtractionFinishedAndReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler; +- (void)showReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler; /*! * Show the user that the update is installing @@ -240,13 +241,25 @@ NS_ASSUME_NONNULL_BEGIN /*! * Terminate the application * - * Sparkle is signaling that it wants the application to be terminated immediately. - * If an implementor has already delayed application termination, now would be the appropriate time to stop. + * Sparkle is signaling that it wants the application to request for application termination. + * Care should be taken if the request for application termination fails or is delayed. + * Eg: may want to hide installation status windows in case termination doesn't happen right away * * This can be called from any thread */ - (void)terminateApplication; +/*! + * Show the user that the update installation finished + * + * Let the user know that the update finished installing. + * This will only be invoked if the updater process is still alive, which is typically not the case if + * the updater's lifetime is tied to the application it is updating. + * + * This can be called from any thread + */ +- (void)showUpdateInstallationDidFinish; + /*! * Dismiss the current update installation * diff --git a/TestApplication/SUPopUpTitlebarUserDriver.m b/TestApplication/SUPopUpTitlebarUserDriver.m index b9e85a19..d5249a2b 100644 --- a/TestApplication/SUPopUpTitlebarUserDriver.m +++ b/TestApplication/SUPopUpTitlebarUserDriver.m @@ -188,7 +188,7 @@ #pragma mark Install & Relaunch Update -- (void)showExtractionFinishedAndReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler +- (void)showReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler { dispatch_async(dispatch_get_main_queue(), ^{ [self.coreComponent registerInstallUpdateHandler:installUpdateHandler]; @@ -303,11 +303,21 @@ }); } +- (void)showUpdateInstallationDidFinish +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self addUpdateButtonWithTitle:@"Installation Finished!"]; + }); +} + #pragma mark Aborting Everything - (void)terminateApplication { dispatch_async(dispatch_get_main_queue(), ^{ + // In case our termination request fails or is delayed + [self removeUpdateButton]; + [[NSApplication sharedApplication] terminate:nil]; }); } diff --git a/Tests/SUUpdaterTest.m b/Tests/SUUpdaterTest.m index 55c24784..011999d6 100644 --- a/Tests/SUUpdaterTest.m +++ b/Tests/SUUpdaterTest.m @@ -49,10 +49,12 @@ - (void)showExtractionReceivedProgress:(double)__unused progress {} -- (void)showExtractionFinishedAndReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))__unused installUpdateHandler {} +- (void)showReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))__unused installUpdateHandler {} - (void)showInstallingUpdate {} +- (void)showUpdateInstallationDidFinish {} + - (void)dismissUpdateInstallation {} - (void)terminateApplication {} diff --git a/sparkle-cli/AppDelegate.m b/sparkle-cli/AppDelegate.m index 130fbcc2..1455d287 100644 --- a/sparkle-cli/AppDelegate.m +++ b/sparkle-cli/AppDelegate.m @@ -42,13 +42,14 @@ - (void)applicationDidFinishLaunching:(NSNotification *)__unused aNotification { + // Kick off a scheduled update check once we start the updater later + [self.updater checkForUpdates]; + NSError *updaterError = nil; if (![self.updater startUpdater:&updaterError]) { printf("Error: Failed to initialize updater with error (%ld): %s\n", updaterError.code, updaterError.localizedDescription.UTF8String); exit(EXIT_FAILURE); } - - [self.updater checkForUpdates]; } @end diff --git a/sparkle-cli/SUCommandLineUserDriver.m b/sparkle-cli/SUCommandLineUserDriver.m index 65ce4f99..c3f6865d 100644 --- a/sparkle-cli/SUCommandLineUserDriver.m +++ b/sparkle-cli/SUCommandLineUserDriver.m @@ -66,7 +66,7 @@ - (void)requestUpdatePermissionWithSystemProfile:(NSArray *)__unused systemProfile reply:(void (^)(SUUpdatePermissionPromptResult *))__unused reply { dispatch_async(dispatch_get_main_queue(), ^{ - printf("Asked about making update permission decision.. Aborting because I do not want to make this decision."); + printf("Asked about making update permission decision.. Aborting because I do not want to make this decision.\n"); exit(EXIT_FAILURE); }); } @@ -154,7 +154,7 @@ }); } -- (void)showExtractionFinishedAndReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler +- (void)showReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler { dispatch_async(dispatch_get_main_queue(), ^{ [self.coreComponent registerInstallUpdateHandler:installUpdateHandler]; @@ -169,6 +169,13 @@ }); } +- (void)showUpdateInstallationDidFinish +{ + dispatch_async(dispatch_get_main_queue(), ^{ + printf("Installation Finished...\n"); + }); +} + - (void)dismissUpdateInstallation { dispatch_async(dispatch_get_main_queue(), ^{