From 345ed3b60ec97e87fe332d9154f8290dd01100e7 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 23 Jul 2016 09:08:29 -0400 Subject: [PATCH] Add resuming downloaded updates without starting the installer automatically This allows automatically downloading updates, and having them prompt up later, so the user can perform authorization later for starting up the installer --- InstallerLauncher/SUAuthorizationReply.h | 3 +- InstallerLauncher/SUInstallerLauncher.m | 18 +++++---- Sparkle/SUAutomaticUpdateDriver.m | 17 ++++---- Sparkle/SUBasicUpdateDriver.h | 6 +-- Sparkle/SUBasicUpdateDriver.m | 36 +++++++++++------ Sparkle/SUCoreBasedUpdateDriver.h | 12 ++++-- Sparkle/SUCoreBasedUpdateDriver.m | 50 +++++++++++++++++++----- Sparkle/SUErrors.h | 3 +- Sparkle/SUInstallerDriver.h | 2 +- Sparkle/SUInstallerDriver.m | 35 +++++++++-------- Sparkle/SUProbingUpdateDriver.m | 18 +++++---- Sparkle/SUScheduledUpdateDriver.m | 5 +++ Sparkle/SUStandardUserDriver.m | 13 +++++- Sparkle/SUUIBasedUpdateDriver.h | 4 +- Sparkle/SUUIBasedUpdateDriver.m | 26 +++++++----- Sparkle/SUUpdateDriver.h | 6 ++- Sparkle/SUUpdater.m | 16 +++++--- Sparkle/SUUserInitiatedUpdateDriver.m | 5 +++ 18 files changed, 187 insertions(+), 88 deletions(-) diff --git a/InstallerLauncher/SUAuthorizationReply.h b/InstallerLauncher/SUAuthorizationReply.h index 1b8d0c97..3b5ef276 100644 --- a/InstallerLauncher/SUAuthorizationReply.h +++ b/InstallerLauncher/SUAuthorizationReply.h @@ -12,5 +12,6 @@ typedef NS_ENUM(NSUInteger, SUAuthorizationReply) { SUAuthorizationReplySuccess = 0, SUAuthorizationReplyCancelled = 1, - SUAuthorizationReplyFailure = 2 + SUAuthorizationReplyFailure = 2, + SUAuthorizationReplyTryAgainLater = 3 }; diff --git a/InstallerLauncher/SUInstallerLauncher.m b/InstallerLauncher/SUInstallerLauncher.m index 423d0f46..405a8fd3 100644 --- a/InstallerLauncher/SUInstallerLauncher.m +++ b/InstallerLauncher/SUInstallerLauncher.m @@ -175,14 +175,18 @@ submittedInstaller = ([SUSubmitInstaller submitInstallerAtPath:installerPath withHostBundle:hostBundle allowingInteraction:allowingInteraction inSystemDomain:needsSystemAuthorization] == SUAuthorizationReplySuccess); } - BOOL submittedProgressTool = [self submitProgressToolAtPath:progressToolPath withHostBundle:hostBundle allowingInteraction:allowingInteraction installerPath:installerPath shouldSubmitInstaller:!shouldSubmitInstallerImmediately]; - - if (!submittedProgressTool) { - completionHandler(SUAuthorizationReplyFailure); - } else if (shouldSubmitInstallerImmediately) { - completionHandler(submittedInstaller ? SUAuthorizationReplySuccess : SUAuthorizationReplyFailure); + if (needsSystemAuthorization && !preflighted && !allowingInteraction) { + completionHandler(SUAuthorizationReplyTryAgainLater); } else { - [self waitForProgressAgentWithHostBundle:hostBundle completion:completionHandler]; + BOOL submittedProgressTool = [self submitProgressToolAtPath:progressToolPath withHostBundle:hostBundle allowingInteraction:allowingInteraction installerPath:installerPath shouldSubmitInstaller:!shouldSubmitInstallerImmediately]; + + if (!submittedProgressTool) { + completionHandler(SUAuthorizationReplyFailure); + } else if (shouldSubmitInstallerImmediately) { + completionHandler(submittedInstaller ? SUAuthorizationReplySuccess : SUAuthorizationReplyFailure); + } else { + [self waitForProgressAgentWithHostBundle:hostBundle completion:completionHandler]; + } } }); } diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index 03840c4b..caf1518e 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -49,7 +49,7 @@ - (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders completion:(SUUpdateDriverCompletion)completionBlock { - [self.coreDriver checkForUpdatesAtAppcastURL:appcastURL withUserAgent:userAgent httpHeaders:httpHeaders includesSkippedUpdates:NO completion:completionBlock]; + [self.coreDriver checkForUpdatesAtAppcastURL:appcastURL withUserAgent:userAgent httpHeaders:httpHeaders includesSkippedUpdates:NO requiresSilentInstall:YES completion:completionBlock]; } - (void)resumeUpdateWithCompletion:(SUUpdateDriverCompletion)__unused completionBlock __attribute__((noreturn)) @@ -59,6 +59,13 @@ abort(); } +- (void)resumeDownloadedUpdate:(SUDownloadedUpdate *)__unused downloadedUpdate completion:(SUUpdateDriverCompletion)__unused completionBlock __attribute__((noreturn)) +{ + // Nothing really to do here.. this shouldn't be called. + SULog(@"Error: resumeDownloadedUpdate:completion: called on SUAutomaticUpdateDriver"); + abort(); +} + - (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem { self.updateItem = updateItem; @@ -99,11 +106,6 @@ } } -- (BOOL)basicDriverShouldSignalShowingUpdateImmediately -{ - return (!self.willInstallSilently || self.updateItem.isCriticalUpdate); -} - - (void)basicDriverIsRequestingAbortUpdateWithError:(NSError *)error { [self abortUpdateWithError:error]; @@ -121,7 +123,8 @@ - (void)abortUpdateWithError:(NSError *)error { - [self.coreDriver abortUpdateWithError:error]; + BOOL startNextUpdateImmediately = (error == nil) && (!self.willInstallSilently || self.updateItem.isCriticalUpdate); + [self.coreDriver abortUpdateAndSignalShowingNextUpdateImmediately:startNextUpdateImmediately error:error]; } @end diff --git a/Sparkle/SUBasicUpdateDriver.h b/Sparkle/SUBasicUpdateDriver.h index 6939c0a8..ad62d2c7 100644 --- a/Sparkle/SUBasicUpdateDriver.h +++ b/Sparkle/SUBasicUpdateDriver.h @@ -20,8 +20,6 @@ NS_ASSUME_NONNULL_BEGIN - (void)basicDriverIsRequestingAbortUpdateWithError:(nullable NSError *)error; -- (BOOL)basicDriverShouldSignalShowingUpdateImmediately; - @optional - (void)basicDriverDidFinishLoadingAppcast; @@ -36,9 +34,11 @@ NS_ASSUME_NONNULL_BEGIN - (void)resumeUpdateWithCompletion:(SUUpdateDriverCompletion)completionBlock; +- (void)resumeDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate completion:(SUUpdateDriverCompletion)completionBlock; + @property (nullable, nonatomic, readonly) SUAppcastItem *nonDeltaUpdateItem; -- (void)abortUpdateWithError:(nullable NSError *)error; +- (void)abortUpdateAndSignalShowingNextUpdateImmediately:(BOOL)shouldSignalShowingUpdate downloadedUpdate:(SUDownloadedUpdate * _Nullable)downloadedUpdate error:(nullable NSError *)error; @end diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index acedf2e8..75c6796b 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -16,6 +16,7 @@ #import "SUAppcastItem.h" #import "SUProbeInstallStatus.h" #import "SUInstallationInfo.h" +#import "SUDownloadedUpdate.h" @interface SUBasicUpdateDriver () @@ -64,26 +65,37 @@ } } +- (void)notifyResumableUpdateItem:(SUAppcastItem *)updateItem +{ + if (updateItem == nil) { + [self.delegate basicDriverIsRequestingAbortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUResumeAppcastError userInfo:@{ NSLocalizedDescriptionKey: SULocalizedString(@"Failed to resume installing update.", nil) }]]; + } else { + // Kind of lying, but triggering the notification so drivers can know when to stop showing initial fetching progress + [self notifyFinishLoadingAppcast]; + + SUAppcastItem *nonNullUpdateItem = updateItem; + [self didFindValidUpdateWithAppcastItem:nonNullUpdateItem]; + } +} + - (void)resumeUpdateWithCompletion:(SUUpdateDriverCompletion)completionBlock { self.completionBlock = completionBlock; [SUProbeInstallStatus probeInstallerUpdateItemForHost:self.host completion:^(SUInstallationInfo * _Nullable installationInfo) { dispatch_async(dispatch_get_main_queue(), ^{ - SUAppcastItem *updateItem = installationInfo.appcastItem; - if (updateItem == nil) { - [self.delegate basicDriverIsRequestingAbortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUResumeAppcastError userInfo:@{ NSLocalizedDescriptionKey: SULocalizedString(@"Failed to resume installing update.", nil) }]]; - } else { - // Kind of lying, but triggering the notification so drivers can know when to stop showing initial fetching progress - [self notifyFinishLoadingAppcast]; - - SUAppcastItem *nonNullUpdateItem = updateItem; - [self didFindValidUpdateWithAppcastItem:nonNullUpdateItem]; - } + [self notifyResumableUpdateItem:installationInfo.appcastItem]; }); }]; } +- (void)resumeDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate completion:(SUUpdateDriverCompletion)completionBlock +{ + self.completionBlock = completionBlock; + + [self notifyResumableUpdateItem:downloadedUpdate.updateItem]; +} + - (SUAppcastItem *)nonDeltaUpdateItem { return self.appcastDriver.nonDeltaUpdateItem; @@ -141,7 +153,7 @@ [self.delegate basicDriverIsRequestingAbortUpdateWithError:notFoundError]; } -- (void)abortUpdateWithError:(nullable NSError *)error +- (void)abortUpdateAndSignalShowingNextUpdateImmediately:(BOOL)shouldSignalShowingUpdate downloadedUpdate:(SUDownloadedUpdate * _Nullable)downloadedUpdate error:(nullable NSError *)error { if (error != nil) { if ([error code] != SUNoUpdateError) { // Let's not bother logging this. @@ -160,7 +172,7 @@ } if (self.completionBlock != nil) { - self.completionBlock([self.delegate basicDriverShouldSignalShowingUpdateImmediately]); + self.completionBlock(shouldSignalShowingUpdate, downloadedUpdate); self.completionBlock = nil; } } diff --git a/Sparkle/SUCoreBasedUpdateDriver.h b/Sparkle/SUCoreBasedUpdateDriver.h index 97d4d94e..3fb0e8fd 100644 --- a/Sparkle/SUCoreBasedUpdateDriver.h +++ b/Sparkle/SUCoreBasedUpdateDriver.h @@ -25,8 +25,6 @@ NS_ASSUME_NONNULL_BEGIN - (void)basicDriverIsRequestingAbortUpdateWithError:(nullable NSError *)error; -- (BOOL)basicDriverShouldSignalShowingUpdateImmediately; - @optional - (void)basicDriverDidFinishLoadingAppcast; @@ -53,15 +51,21 @@ NS_ASSUME_NONNULL_BEGIN - (instancetype)initWithHost:(SUHost *)host sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater updaterDelegate:(nullable id )updaterDelegate delegate:(id)delegate; -- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates completion:(SUUpdateDriverCompletion)completionBlock; +- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates requiresSilentInstall:(BOOL)silentInstall completion:(SUUpdateDriverCompletion)completionBlock; - (void)resumeUpdateWithCompletion:(SUUpdateDriverCompletion)completionBlock; +- (void)resumeDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate completion:(SUUpdateDriverCompletion)completionBlock; + - (void)downloadUpdateFromAppcastItem:(SUAppcastItem *)updateItem; +- (void)extractDownloadedUpdate; + +- (void)clearDownloadedUpdate; + - (void)finishInstallationWithResponse:(SUInstallUpdateStatus)installUpdateStatus displayingUserInterface:(BOOL)displayingUserInterface; -- (void)abortUpdateWithError:(nullable NSError *)error; +- (void)abortUpdateAndSignalShowingNextUpdateImmediately:(BOOL)shouldSignalShowingUpdate error:(nullable NSError *)error; @end diff --git a/Sparkle/SUCoreBasedUpdateDriver.m b/Sparkle/SUCoreBasedUpdateDriver.m index 0a7ee5de..fa3845a4 100644 --- a/Sparkle/SUCoreBasedUpdateDriver.m +++ b/Sparkle/SUCoreBasedUpdateDriver.m @@ -28,9 +28,11 @@ @property (nonatomic, readonly) SUInstallerDriver *installerDriver; @property (nonatomic, weak, readonly) id delegate; @property (nonatomic) SUAppcastItem *updateItem; +@property (nonatomic) SUDownloadedUpdate *downloadedUpdate; @property (nonatomic, readonly) SUHost *host; @property (nonatomic) BOOL resumingUpdate; +@property (nonatomic) BOOL silentInstall; @property (nonatomic, readonly, weak) id updater; // if we didn't have legacy support, I'd remove this.. @property (nullable, nonatomic, readonly, weak) id updaterDelegate; @property (nonatomic) NSString *userAgent; @@ -46,9 +48,11 @@ @synthesize updateItem = _updateItem; @synthesize host = _host; @synthesize resumingUpdate = _resumingUpdate; +@synthesize silentInstall = _silentInstall; @synthesize updater = _updater; @synthesize updaterDelegate = _updaterDelegate; @synthesize userAgent = _userAgent; +@synthesize downloadedUpdate = _downloadedUpdate; - (instancetype)initWithHost:(SUHost *)host sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater updaterDelegate:(nullable id )updaterDelegate delegate:(id)delegate { @@ -70,9 +74,10 @@ return self; } -- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates completion:(SUUpdateDriverCompletion)completionBlock +- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates requiresSilentInstall:(BOOL)silentInstall completion:(SUUpdateDriverCompletion)completionBlock { self.userAgent = userAgent; + self.silentInstall = silentInstall; [self.basicDriver checkForUpdatesAtAppcastURL:appcastURL withUserAgent:userAgent httpHeaders:httpHeaders includesSkippedUpdates:includesSkippedUpdates completion:completionBlock]; } @@ -80,9 +85,19 @@ - (void)resumeUpdateWithCompletion:(SUUpdateDriverCompletion)completionBlock { self.resumingUpdate = YES; + self.silentInstall = NO; + [self.basicDriver resumeUpdateWithCompletion:completionBlock]; } +- (void)resumeDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate completion:(SUUpdateDriverCompletion)completionBlock +{ + self.downloadedUpdate = downloadedUpdate; + self.silentInstall = NO; + + [self.basicDriver resumeDownloadedUpdate:downloadedUpdate completion:completionBlock]; +} + - (void)basicDriverDidFinishLoadingAppcast { if ([self.delegate respondsToSelector:@selector(basicDriverDidFinishLoadingAppcast)]) { @@ -136,15 +151,33 @@ } - (void)downloadDriverDidDownloadUpdate:(SUDownloadedUpdate *)downloadedUpdate +{ + self.downloadedUpdate = downloadedUpdate; + [self extractUpdate:downloadedUpdate]; +} + +- (void)extractDownloadedUpdate +{ + assert(self.downloadedUpdate != nil); + [self extractUpdate:self.downloadedUpdate]; +} + +- (void)clearDownloadedUpdate +{ + self.downloadedUpdate = nil; +} + +- (void)extractUpdate:(SUDownloadedUpdate *)downloadedUpdate { // Now we have to extract the downloaded archive. if ([self.delegate respondsToSelector:@selector(coreDriverDidFinishDownloadingUpdate)]) { [self.delegate coreDriverDidFinishDownloadingUpdate]; } - [self.installerDriver extractDownloadedUpdate:downloadedUpdate completion:^(NSError * _Nullable error) { + [self.installerDriver extractDownloadedUpdate:downloadedUpdate silently:self.silentInstall completion:^(NSError * _Nullable error) { if (error != nil) { - [self.delegate coreDriverIsRequestingAbortUpdateWithError:(error.code == SUInstallationCancelledError) ? nil : error]; + BOOL silentError = (error.code == SUInstallationCancelledError || error.code == SUInstallationTryAgainLaterError); + [self.delegate coreDriverIsRequestingAbortUpdateWithError:silentError ? nil : error]; } }]; } @@ -244,16 +277,13 @@ [self downloadUpdateFromAppcastItem:nonDeltaUpdateItem]; } -- (BOOL)basicDriverShouldSignalShowingUpdateImmediately -{ - return [self.delegate basicDriverShouldSignalShowingUpdateImmediately]; -} - -- (void)abortUpdateWithError:(nullable NSError *)error +- (void)abortUpdateAndSignalShowingNextUpdateImmediately:(BOOL)shouldSignalShowingUpdate error:(nullable NSError *)error { [self.installerDriver abortInstall]; [self.downloadDriver cleanup]; - [self.basicDriver abortUpdateWithError:error]; + + SUDownloadedUpdate *downloadedUpdate = (error == nil) ? self.downloadedUpdate : nil; + [self.basicDriver abortUpdateAndSignalShowingNextUpdateImmediately:shouldSignalShowingUpdate downloadedUpdate:downloadedUpdate error:error]; } @end diff --git a/Sparkle/SUErrors.h b/Sparkle/SUErrors.h index cbcb70f5..a5dc353d 100644 --- a/Sparkle/SUErrors.h +++ b/Sparkle/SUErrors.h @@ -54,7 +54,8 @@ typedef NS_ENUM(OSStatus, SUError) { SURelaunchError = 4004, SUInstallationError = 4005, SUDowngradeError = 4006, - SUInstallationCancelledError = 4007 + SUInstallationCancelledError = 4007, + SUInstallationTryAgainLaterError = 4008 }; #endif diff --git a/Sparkle/SUInstallerDriver.h b/Sparkle/SUInstallerDriver.h index f804cbef..6fc40490 100644 --- a/Sparkle/SUInstallerDriver.h +++ b/Sparkle/SUInstallerDriver.h @@ -33,7 +33,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)resumeUpdateWithUpdateItem:(SUAppcastItem *)updateItem; -- (void)extractDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate completion:(void (^)(NSError * _Nullable))completionHandler; +- (void)extractDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate silently:(BOOL)silently completion:(void (^)(NSError * _Nullable))completionHandler; - (void)installWithToolAndRelaunch:(BOOL)relaunch displayingUserInterface:(BOOL)showUI; diff --git a/Sparkle/SUInstallerDriver.m b/Sparkle/SUInstallerDriver.m index 864893cd..8e079045 100644 --- a/Sparkle/SUInstallerDriver.m +++ b/Sparkle/SUInstallerDriver.m @@ -130,7 +130,7 @@ } // This can be called multiple times (eg: if a delta update fails, this may be called again with a regular update item) -- (void)extractDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate completion:(void (^)(NSError * _Nullable))completionHandler +- (void)extractDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate silently:(BOOL)silently completion:(void (^)(NSError * _Nullable))completionHandler { self.updateItem = downloadedUpdate.updateItem; self.temporaryDirectory = downloadedUpdate.temporaryDirectory; @@ -139,7 +139,7 @@ self.currentStage = SUInstallerNotStarted; if (self.installerConnection == nil) { - [self launchAutoUpdateWithCompletion:completionHandler]; + [self launchAutoUpdateSilently:silently completion:completionHandler]; } else { // The Install tool is already alive; just send out installation input data again [self sendInstallationData]; @@ -372,7 +372,7 @@ return cachePath; } -- (void)launchAutoUpdateWithCompletion:(void (^)(NSError *_Nullable))completionHandler +- (void)launchAutoUpdateSilently:(BOOL)silently completion:(void (^)(NSError *_Nullable))completionHandler { // Copy the relauncher into a temporary directory so we can get to it after the new version's installed. // Only the paranoid survive: if there's already a stray copy of relaunch there, we would have problems. @@ -426,7 +426,8 @@ installerLauncher = launcherConnection.remoteObjectProxy; } - BOOL shouldAllowInstallerInteraction = NO; +#warning review this... + BOOL shouldAllowInstallerInteraction = !silently; if ([self.updaterDelegate respondsToSelector:@selector(updaterShouldAllowInstallerInteraction:)]) { shouldAllowInstallerInteraction = [self.updaterDelegate updaterShouldAllowInstallerInteraction:self.updater]; } @@ -440,18 +441,20 @@ retrievedLaunchStatus = YES; [launcherConnection invalidate]; - if (result == SUAuthorizationReplyFailure) { - NSError *error = - [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey:SULocalizedString(@"An error occurred while launching the installer. Please try again later.", nil) }]; - - completionHandler(error); - } else if (result == SUAuthorizationReplyCancelled) { - NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationCancelledError userInfo:nil]; - completionHandler(error); - } else { - [self setUpConnection]; - - completionHandler(nil); + switch (result) { + case SUAuthorizationReplyFailure: + completionHandler([NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationError userInfo:@{ NSLocalizedDescriptionKey:SULocalizedString(@"An error occurred while launching the installer. Please try again later.", nil) }]); + break; + case SUAuthorizationReplyCancelled: + completionHandler([NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationCancelledError userInfo:nil]); + break; + case SUAuthorizationReplyTryAgainLater: + completionHandler([NSError errorWithDomain:SUSparkleErrorDomain code:SUInstallationTryAgainLaterError userInfo:nil]); + break; + case SUAuthorizationReplySuccess: + [self setUpConnection]; + completionHandler(nil); + break; } }); }]; diff --git a/Sparkle/SUProbingUpdateDriver.m b/Sparkle/SUProbingUpdateDriver.m index 422fccb8..21cdfb1d 100644 --- a/Sparkle/SUProbingUpdateDriver.m +++ b/Sparkle/SUProbingUpdateDriver.m @@ -16,12 +16,14 @@ @interface SUProbingUpdateDriver () @property (nonatomic, readonly) SUBasicUpdateDriver *basicDriver; +@property (nonatomic) SUDownloadedUpdate *downloadedUpdate; @end @implementation SUProbingUpdateDriver @synthesize basicDriver = _basicDriver; +@synthesize downloadedUpdate = _downloadedUpdate; - (instancetype)initWithHost:(SUHost *)host updater:(id)updater updaterDelegate:(id )updaterDelegate { @@ -37,16 +39,18 @@ [self.basicDriver checkForUpdatesAtAppcastURL:appcastURL withUserAgent:userAgent httpHeaders:httpHeaders includesSkippedUpdates:NO completion:completionBlock]; } -- (BOOL)basicDriverShouldSignalShowingUpdateImmediately -{ - return NO; -} - - (void)resumeUpdateWithCompletion:(SUUpdateDriverCompletion)completionBlock { [self.basicDriver resumeUpdateWithCompletion:completionBlock]; } +- (void)resumeDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate completion:(SUUpdateDriverCompletion)completionBlock +{ + self.downloadedUpdate = downloadedUpdate; + + [self.basicDriver resumeDownloadedUpdate:downloadedUpdate completion:completionBlock]; +} + - (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)__unused appcastItem { // Stop as soon as we have an answer @@ -55,9 +59,7 @@ - (void)basicDriverIsRequestingAbortUpdateWithError:(nullable NSError *)error { - [self abortUpdateWithError:error]; - } - (void)abortUpdate @@ -67,7 +69,7 @@ - (void)abortUpdateWithError:(nullable NSError *)error { - [self.basicDriver abortUpdateWithError:error]; + [self.basicDriver abortUpdateAndSignalShowingNextUpdateImmediately:NO downloadedUpdate:self.downloadedUpdate error:error]; } @end diff --git a/Sparkle/SUScheduledUpdateDriver.m b/Sparkle/SUScheduledUpdateDriver.m index 6bc7483d..dd31f8e7 100644 --- a/Sparkle/SUScheduledUpdateDriver.m +++ b/Sparkle/SUScheduledUpdateDriver.m @@ -45,6 +45,11 @@ [self.uiDriver resumeUpdateWithCompletion:completionBlock]; } +- (void)resumeDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate completion:(SUUpdateDriverCompletion)completionBlock +{ + [self.uiDriver resumeDownloadedUpdate:downloadedUpdate completion:completionBlock]; +} + - (void)basicDriverIsRequestingAbortUpdateWithError:(nullable NSError *)__unused error { // Don't tell the user that no update was found or some appcast fetch error occurred for scheduled update checks diff --git a/Sparkle/SUStandardUserDriver.m b/Sparkle/SUStandardUserDriver.m index b0f250fe..be0741e4 100644 --- a/Sparkle/SUStandardUserDriver.m +++ b/Sparkle/SUStandardUserDriver.m @@ -318,15 +318,22 @@ #pragma mark Download & Install Updates +- (void)showStatusController +{ + if (self.statusController == nil) { + self.statusController = [[SUStatusController alloc] initWithHost:self.host]; + [self.statusController showWindow:self]; + } +} + - (void)showDownloadInitiatedWithCompletion:(void (^)(SUDownloadUpdateStatus))downloadUpdateStatusCompletion { dispatch_async(dispatch_get_main_queue(), ^{ [self.coreComponent registerDownloadStatusHandler:downloadUpdateStatusCompletion]; - self.statusController = [[SUStatusController alloc] initWithHost:self.host]; + [self showStatusController]; [self.statusController beginActionWithTitle:SULocalizedString(@"Downloading update...", @"Take care not to overflow the status window.") maxProgressValue:0.0 statusText:nil]; [self.statusController setButtonTitle:SULocalizedString(@"Cancel", nil) target:self action:@selector(cancelDownload:) isDefault:NO]; - [self.statusController showWindow:self]; }); } @@ -364,7 +371,9 @@ dispatch_async(dispatch_get_main_queue(), ^{ [self.coreComponent completeDownloadStatus]; + [self showStatusController]; [self.statusController beginActionWithTitle:SULocalizedString(@"Extracting update...", @"Take care not to overflow the status window.") maxProgressValue:0.0 statusText:nil]; + [self.statusController setButtonTitle:SULocalizedString(@"Cancel", nil) target:nil action:nil isDefault:NO]; [self.statusController setButtonEnabled:NO]; }); } diff --git a/Sparkle/SUUIBasedUpdateDriver.h b/Sparkle/SUUIBasedUpdateDriver.h index 443352df..37161ac7 100644 --- a/Sparkle/SUUIBasedUpdateDriver.h +++ b/Sparkle/SUUIBasedUpdateDriver.h @@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN @end -@class SUHost; +@class SUHost, SUDownloadedUpdate; @protocol SUUserDriver, SUUpdaterDelegate; @interface SUUIBasedUpdateDriver : NSObject @@ -33,6 +33,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)resumeUpdateWithCompletion:(SUUpdateDriverCompletion)completionBlock; +- (void)resumeDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate completion:(SUUpdateDriverCompletion)completionBlock; + - (void)abortUpdateWithError:(nullable NSError *)error; @end diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 8a4d61f7..ca68b8b2 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -29,6 +29,7 @@ @property (nonatomic, weak, readonly) id delegate; @property (nonatomic, readonly) id userDriver; @property (nonatomic) BOOL resumingUpdate; +@property (nonatomic) BOOL resumingDownloadedUpdate; @end @@ -41,6 +42,7 @@ @synthesize userDriver = _userDriver; @synthesize delegate = _delegate; @synthesize resumingUpdate = _resumingUpdate; +@synthesize resumingDownloadedUpdate = _resumingDownloadedUpdate; - (instancetype)initWithHost:(SUHost *)host sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater userDriver:(id )userDriver updaterDelegate:(nullable id )updaterDelegate delegate:(id)delegate { @@ -59,7 +61,7 @@ - (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates completion:(SUUpdateDriverCompletion)completionBlock { - [self.coreDriver checkForUpdatesAtAppcastURL:appcastURL withUserAgent:userAgent httpHeaders:httpHeaders includesSkippedUpdates:includesSkippedUpdates completion:completionBlock]; + [self.coreDriver checkForUpdatesAtAppcastURL:appcastURL withUserAgent:userAgent httpHeaders:httpHeaders includesSkippedUpdates:includesSkippedUpdates requiresSilentInstall:NO completion:completionBlock]; } - (void)resumeUpdateWithCompletion:(SUUpdateDriverCompletion)completionBlock @@ -68,6 +70,12 @@ [self.coreDriver resumeUpdateWithCompletion:completionBlock]; } +- (void)resumeDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate completion:(SUUpdateDriverCompletion)completionBlock +{ + self.resumingDownloadedUpdate = YES; + [self.coreDriver resumeDownloadedUpdate:downloadedUpdate completion:completionBlock]; +} + - (void)basicDriverDidFinishLoadingAppcast { if ([self.delegate respondsToSelector:@selector(basicDriverDidFinishLoadingAppcast)]) { @@ -77,15 +85,20 @@ - (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem { - if (!self.resumingUpdate) { + if (!self.resumingUpdate || self.resumingDownloadedUpdate) { [self.userDriver showUpdateFoundWithAppcastItem:updateItem reply:^(SUUpdateAlertChoice choice) { dispatch_async(dispatch_get_main_queue(), ^{ [self.host setObject:nil forUserDefaultsKey:SUSkippedVersionKey]; switch (choice) { case SUInstallUpdateChoice: - [self.coreDriver downloadUpdateFromAppcastItem:updateItem]; + if (!self.resumingDownloadedUpdate) { + [self.coreDriver downloadUpdateFromAppcastItem:updateItem]; + } else { + [self.coreDriver extractDownloadedUpdate]; + } break; case SUSkipThisVersionChoice: + [self.coreDriver clearDownloadedUpdate]; [self.host setObject:[updateItem versionString] forUserDefaultsKey:SUSkippedVersionKey]; // Fall through case SUInstallLaterChoice: @@ -183,11 +196,6 @@ [self.userDriver showUpdateInstallationDidFinish]; } -- (BOOL)basicDriverShouldSignalShowingUpdateImmediately -{ - return NO; -} - - (void)basicDriverIsRequestingAbortUpdateWithError:(nullable NSError *)error { // A delegate may want to handle this type of error specially @@ -203,7 +211,7 @@ { void (^abortUpdate)(void) = ^{ [self.userDriver dismissUpdateInstallation]; - [self.coreDriver abortUpdateWithError:error]; + [self.coreDriver abortUpdateAndSignalShowingNextUpdateImmediately:NO error:error]; }; if (error != nil) { diff --git a/Sparkle/SUUpdateDriver.h b/Sparkle/SUUpdateDriver.h index 8c925404..08d8a633 100644 --- a/Sparkle/SUUpdateDriver.h +++ b/Sparkle/SUUpdateDriver.h @@ -8,7 +8,9 @@ NS_ASSUME_NONNULL_BEGIN -typedef void (^SUUpdateDriverCompletion)(BOOL shouldShowUpdateImmediately); +@class SUDownloadedUpdate; + +typedef void (^SUUpdateDriverCompletion)(BOOL shouldShowUpdateImmediately, SUDownloadedUpdate * _Nullable resumableUpdate); @protocol SUUpdateDriver @@ -16,6 +18,8 @@ typedef void (^SUUpdateDriverCompletion)(BOOL shouldShowUpdateImmediately); - (void)resumeUpdateWithCompletion:(SUUpdateDriverCompletion)completionBlock; +- (void)resumeDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate completion:(SUUpdateDriverCompletion)completionBlock; + - (void)abortUpdate; @end diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index f614acad..8deeb9ac 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -27,6 +27,7 @@ #import "SUErrors.h" #import "SUXPCServiceInfo.h" #import "SUUpdaterCycle.h" +#import "SUDownloadedUpdate.h" #ifdef _APPKITDEFINES_H #error This is a "core" class and should NOT import AppKit @@ -49,6 +50,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @property (nonatomic, readonly) SUUpdaterCycle *updaterCycle; @property (nonatomic) BOOL startedUpdater; @property (nonatomic, copy) void (^preStartedScheduledUpdateBlock)(void); +@property (nonatomic, nullable) SUDownloadedUpdate *resumableUpdate; @end @@ -65,6 +67,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @synthesize sparkleBundle = _sparkleBundle; @synthesize startedUpdater = _startedUpdater; @synthesize preStartedScheduledUpdateBlock = _preStartedScheduledUpdateBlock; +@synthesize resumableUpdate = _resumableUpdate; #ifdef DEBUG + (void)load @@ -376,7 +379,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } id updateDriver; - if (!installerIsRunning && [strongSelf automaticallyDownloadsUpdates] && [strongSelf allowsAutomaticUpdates]) { + if (!installerIsRunning && [strongSelf automaticallyDownloadsUpdates] && [strongSelf allowsAutomaticUpdates] && strongSelf.resumableUpdate == nil) { updateDriver = [[SUAutomaticUpdateDriver alloc] initWithHost:strongSelf.host @@ -479,9 +482,10 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification // Use a NIL URL to cancel quietly. if (theFeedURL) { __weak SUUpdater *weakSelf = self; - SUUpdateDriverCompletion completionBlock = ^(BOOL shouldShowUpdateImmediately) { + SUUpdateDriverCompletion completionBlock = ^(BOOL shouldShowUpdateImmediately, SUDownloadedUpdate * _Nullable resumableUpdate) { SUUpdater *strongSelf = weakSelf; if (strongSelf != nil) { + strongSelf.resumableUpdate = resumableUpdate; strongSelf.driver = nil; [strongSelf updateLastUpdateCheckDate]; [strongSelf scheduleNextUpdateCheckFiringImmediately:shouldShowUpdateImmediately]; @@ -490,10 +494,12 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification [self.userDriver showCanCheckForUpdates:NO]; - if (!installerInProgress) { - [self.driver checkForUpdatesAtAppcastURL:theFeedURL withUserAgent:[self userAgentString] httpHeaders:[self httpHeaders] completion:completionBlock]; - } else { + if (installerInProgress) { [self.driver resumeUpdateWithCompletion:completionBlock]; + } else if (self.resumableUpdate != nil) { + [self.driver resumeDownloadedUpdate:(SUDownloadedUpdate * _Nonnull)self.resumableUpdate completion:completionBlock]; + } else { + [self.driver checkForUpdatesAtAppcastURL:theFeedURL withUserAgent:[self userAgentString] httpHeaders:[self httpHeaders] completion:completionBlock]; } } else { [self.driver abortUpdate]; diff --git a/Sparkle/SUUserInitiatedUpdateDriver.m b/Sparkle/SUUserInitiatedUpdateDriver.m index 21b9b586..c6e62fa3 100644 --- a/Sparkle/SUUserInitiatedUpdateDriver.m +++ b/Sparkle/SUUserInitiatedUpdateDriver.m @@ -65,6 +65,11 @@ [self.uiDriver resumeUpdateWithCompletion:completionBlock]; } +- (void)resumeDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate completion:(SUUpdateDriverCompletion)completionBlock +{ + [self.uiDriver resumeDownloadedUpdate:downloadedUpdate completion:completionBlock]; +} + - (void)basicDriverIsRequestingAbortUpdateWithError:(nullable NSError *)error { [self abortUpdateWithError:error];