From 6a8680464bf9926ae00f7b615056f275ffb2fa25 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 21 Aug 2016 15:14:47 -0400 Subject: [PATCH] Pass installer interaction flag to drivers and use delegate once --- Sparkle/SPUUpdater.m | 5 ++++- Sparkle/SUAutomaticUpdateDriver.m | 4 ++-- Sparkle/SUBasicUpdateDriver.m | 2 +- Sparkle/SUCoreBasedUpdateDriver.h | 4 ++-- Sparkle/SUCoreBasedUpdateDriver.m | 17 +++++++++++------ Sparkle/SUInstallerDriver.h | 2 +- Sparkle/SUInstallerDriver.m | 15 ++++----------- Sparkle/SUProbingUpdateDriver.m | 2 +- Sparkle/SUScheduledUpdateDriver.m | 4 ++-- Sparkle/SUUIBasedUpdateDriver.h | 4 ++-- Sparkle/SUUIBasedUpdateDriver.m | 24 +++++++++--------------- Sparkle/SUUpdateDriver.h | 2 +- Sparkle/SUUserInitiatedUpdateDriver.m | 4 ++-- 13 files changed, 42 insertions(+), 47 deletions(-) diff --git a/Sparkle/SPUUpdater.m b/Sparkle/SPUUpdater.m index a9a4bf4e..ecac1c73 100644 --- a/Sparkle/SPUUpdater.m +++ b/Sparkle/SPUUpdater.m @@ -551,7 +551,10 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } 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]; + BOOL allowsInstallerInteraction = + (![self.delegate respondsToSelector:@selector(updaterShouldAllowInstallerInteraction:)] || [self.delegate updaterShouldAllowInstallerInteraction:self]); + + [self.driver checkForUpdatesAtAppcastURL:theFeedURL withUserAgent:[self userAgentString] httpHeaders:[self httpHeaders] preventingInstallerInteraction:!allowsInstallerInteraction completion:completionBlock]; } } else { // I think this is really unlikely to occur but better be safe diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index 55895793..05304b1d 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -53,11 +53,11 @@ return self; } -- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders completion:(SUUpdateDriverCompletion)completionBlock +- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders preventingInstallerInteraction:(BOOL)preventsInstallerInteraction completion:(SUUpdateDriverCompletion)completionBlock { [self.coreDriver prepareCheckForUpdatesWithCompletion:completionBlock]; - [self.coreDriver preflightForUpdatePermissionWithReply:^(NSError * _Nullable error) { + [self.coreDriver preflightForUpdatePermissionPreventingInstallerInteraction:preventsInstallerInteraction reply:^(NSError * _Nullable error) { if (error != nil) { [self abortUpdateWithError:error]; } else { diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index df5f278c..8caae969 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -187,7 +187,7 @@ } while(--finiteRecursion && errorToDisplay); } - // Notify host app that update has aborted + // Notify host app that updater has aborted if ([self.updaterDelegate respondsToSelector:@selector(updater:didAbortWithError:)]) { [self.updaterDelegate updater:self.updater didAbortWithError:(NSError * _Nonnull)error]; } diff --git a/Sparkle/SUCoreBasedUpdateDriver.h b/Sparkle/SUCoreBasedUpdateDriver.h index ae4d7bb8..96a37c08 100644 --- a/Sparkle/SUCoreBasedUpdateDriver.h +++ b/Sparkle/SUCoreBasedUpdateDriver.h @@ -53,9 +53,9 @@ NS_ASSUME_NONNULL_BEGIN - (void)prepareCheckForUpdatesWithCompletion:(SUUpdateDriverCompletion)completionBlock; -- (void)preflightForUpdatePermissionWithReply:(void (^)(NSError * _Nullable))reply; +- (void)preflightForUpdatePermissionPreventingInstallerInteraction:(BOOL)preventsInstallerInteraction reply:(void (^)(NSError * _Nullable))reply; -- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates requiresSilentInstall:(BOOL)silentInstall; +- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates requiresSilentInstall:(BOOL)silentInstall; - (void)resumeInstallingUpdateWithCompletion:(SUUpdateDriverCompletion)completionBlock; diff --git a/Sparkle/SUCoreBasedUpdateDriver.m b/Sparkle/SUCoreBasedUpdateDriver.m index 5579f3ba..a2a5ecb8 100644 --- a/Sparkle/SUCoreBasedUpdateDriver.m +++ b/Sparkle/SUCoreBasedUpdateDriver.m @@ -35,6 +35,7 @@ @property (nonatomic, readonly) SUHost *host; @property (nonatomic) BOOL resumingInstallingUpdate; @property (nonatomic) BOOL silentInstall; +@property (nonatomic) BOOL preventsInstallerInteraction; @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; @@ -51,6 +52,7 @@ @synthesize host = _host; @synthesize resumingInstallingUpdate = _resumingInstallingUpdate; @synthesize silentInstall = _silentInstall; +@synthesize preventsInstallerInteraction = _preventsInstallerInteraction; @synthesize updater = _updater; @synthesize updaterDelegate = _updaterDelegate; @synthesize userAgent = _userAgent; @@ -80,14 +82,17 @@ [self.basicDriver prepareCheckForUpdatesWithCompletion:completionBlock]; } -- (void)preflightForUpdatePermissionWithReply:(void (^)(NSError * _Nullable))reply +- (void)preflightForUpdatePermissionPreventingInstallerInteraction:(BOOL)preventsInstallerInteraction reply:(void (^)(NSError * _Nullable))reply { + // Save for later + self.preventsInstallerInteraction = preventsInstallerInteraction; + // If we don't allow interaction, make sure we have sufficient privileges to update // If we don't, then we should abort early before trying to check for updates // Note if we don't have permission to update an application update without interaction, // then we won't have permission for package type of updates either (converse is not true) - if (![self.updaterDelegate respondsToSelector:@selector(updaterShouldAllowInstallerInteraction:)] || [self.updaterDelegate updaterShouldAllowInstallerInteraction:self.updater]) { - // Simple case where installer interaction is allowed + + if (!preventsInstallerInteraction) { reply(nil); } else { // Otherwise check if we have sufficient privileges to update without interaction @@ -101,7 +106,7 @@ } } -- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates requiresSilentInstall:(BOOL)silentInstall +- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates requiresSilentInstall:(BOOL)silentInstall { self.userAgent = userAgent; self.silentInstall = silentInstall; @@ -143,7 +148,7 @@ [self.installerDriver resumeInstallingUpdateWithUpdateItem:updateItem]; [self.delegate basicDriverDidFindUpdateWithAppcastItem:updateItem]; } else { - if (![self.updaterDelegate respondsToSelector:@selector(updaterShouldAllowInstallerInteraction:)] || [self.updaterDelegate updaterShouldAllowInstallerInteraction:self.updater]) { + if (!self.preventsInstallerInteraction) { // Simple case - delegate allows interaction, so we should continue along [self.delegate basicDriverDidFindUpdateWithAppcastItem:updateItem]; } else { @@ -216,7 +221,7 @@ [self.delegate coreDriverDidStartExtractingUpdate]; } - [self.installerDriver extractDownloadedUpdate:downloadedUpdate silently:self.silentInstall completion:^(NSError * _Nullable error) { + [self.installerDriver extractDownloadedUpdate:downloadedUpdate silently:self.silentInstall preventsInstallerInteraction:self.preventsInstallerInteraction completion:^(NSError * _Nullable error) { if (error != nil) { [self.delegate coreDriverIsRequestingAbortUpdateWithError:error]; } else { diff --git a/Sparkle/SUInstallerDriver.h b/Sparkle/SUInstallerDriver.h index 6bf69128..88150261 100644 --- a/Sparkle/SUInstallerDriver.h +++ b/Sparkle/SUInstallerDriver.h @@ -35,7 +35,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)checkIfApplicationInstallationRequiresAuthorizationWithReply:(void (^)(BOOL requiresAuthorization))reply; -- (void)extractDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate silently:(BOOL)silently completion:(void (^)(NSError * _Nullable))completionHandler; +- (void)extractDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate silently:(BOOL)silently preventsInstallerInteraction:(BOOL)preventsInstallerInteraction completion:(void (^)(NSError * _Nullable))completionHandler; - (void)installWithToolAndRelaunch:(BOOL)relaunch displayingUserInterface:(BOOL)showUI; diff --git a/Sparkle/SUInstallerDriver.m b/Sparkle/SUInstallerDriver.m index 31c52f64..21c19f67 100644 --- a/Sparkle/SUInstallerDriver.m +++ b/Sparkle/SUInstallerDriver.m @@ -140,7 +140,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 silently:(BOOL)silently completion:(void (^)(NSError * _Nullable))completionHandler +- (void)extractDownloadedUpdate:(SUDownloadedUpdate *)downloadedUpdate silently:(BOOL)silently preventsInstallerInteraction:(BOOL)preventsInstallerInteraction completion:(void (^)(NSError * _Nullable))completionHandler { self.updateItem = downloadedUpdate.updateItem; self.temporaryDirectory = downloadedUpdate.temporaryDirectory; @@ -149,7 +149,7 @@ self.currentStage = SPUInstallerNotStarted; if (self.installerConnection == nil) { - [self launchAutoUpdateSilently:silently completion:completionHandler]; + [self launchAutoUpdateSilently:silently preventsInstallerInteraction:preventsInstallerInteraction completion:completionHandler]; } else { // The Install tool is already alive; just send out installation input data again [self sendInstallationData]; @@ -363,7 +363,7 @@ }]; } -- (void)launchAutoUpdateSilently:(BOOL)silently completion:(void (^)(NSError *_Nullable))completionHandler +- (void)launchAutoUpdateSilently:(BOOL)silently preventsInstallerInteraction:(BOOL)preventsInstallerInteraction completion:(void (^)(NSError *_Nullable))completionHandler { id installerLauncher = nil; __block BOOL retrievedLaunchStatus = NO; @@ -410,13 +410,6 @@ installerLauncher = launcherConnection.remoteObjectProxy; } - // This determines if our updater delegate allows interaction - // If the delegate disallows interaction, then the update cannot be continued - BOOL updaterAllowsInteraction = YES; - if ([self.updaterDelegate respondsToSelector:@selector(updaterShouldAllowInstallerInteraction:)]) { - updaterAllowsInteraction = [self.updaterDelegate updaterShouldAllowInstallerInteraction:self.updater]; - } - // Our driver (automatic or UI based) has a say if interaction is allowed as well // An automatic driver may disallow interaction but the updater could try again later a UI based driver that does allow interaction BOOL driverAllowsInteraction = !silently; @@ -429,7 +422,7 @@ // The installer launcher could be in a XPC service, so we don't want to do localization in there NSString *authorizationPrompt = [NSString stringWithFormat:SULocalizedString(@"%1$@ wants to update.", nil), self.host.name]; - [installerLauncher launchInstallerWithHostBundlePath:hostBundlePath authorizationPrompt:authorizationPrompt installationType:installationType allowingDriverInteraction:driverAllowsInteraction allowingUpdaterInteraction:updaterAllowsInteraction completion:^(SUInstallerLauncherStatus result) { + [installerLauncher launchInstallerWithHostBundlePath:hostBundlePath authorizationPrompt:authorizationPrompt installationType:installationType allowingDriverInteraction:driverAllowsInteraction allowingUpdaterInteraction:!preventsInstallerInteraction completion:^(SUInstallerLauncherStatus result) { dispatch_async(dispatch_get_main_queue(), ^{ retrievedLaunchStatus = YES; [launcherConnection invalidate]; diff --git a/Sparkle/SUProbingUpdateDriver.m b/Sparkle/SUProbingUpdateDriver.m index b7d5d578..e211efc4 100644 --- a/Sparkle/SUProbingUpdateDriver.m +++ b/Sparkle/SUProbingUpdateDriver.m @@ -34,7 +34,7 @@ return self; } -- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders completion:(SUUpdateDriverCompletion)completionBlock +- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders preventingInstallerInteraction:(BOOL)__unused preventsInstallerInteraction completion:(SUUpdateDriverCompletion)completionBlock { // We don't preflight for update permission in this driver because we are just interested if an update is available diff --git a/Sparkle/SUScheduledUpdateDriver.m b/Sparkle/SUScheduledUpdateDriver.m index 7c43b116..4f73c296 100644 --- a/Sparkle/SUScheduledUpdateDriver.m +++ b/Sparkle/SUScheduledUpdateDriver.m @@ -35,11 +35,11 @@ return self; } -- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders completion:(SUUpdateDriverCompletion)completionBlock +- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders preventingInstallerInteraction:(BOOL)preventsInstallerInteraction completion:(SUUpdateDriverCompletion)completionBlock { [self.uiDriver prepareCheckForUpdatesWithCompletion:completionBlock]; - [self.uiDriver preflightForUpdatePermissionWithReply:^(NSError * _Nullable error) { + [self.uiDriver preflightForUpdatePermissionPreventingInstallerInteraction:preventsInstallerInteraction reply:^(NSError * _Nullable error) { if (error != nil) { // Don't tell the user about the permission error for scheduled update checks [self abortUpdateWithError:nil]; diff --git a/Sparkle/SUUIBasedUpdateDriver.h b/Sparkle/SUUIBasedUpdateDriver.h index 8ea8a435..46930654 100644 --- a/Sparkle/SUUIBasedUpdateDriver.h +++ b/Sparkle/SUUIBasedUpdateDriver.h @@ -32,9 +32,9 @@ NS_ASSUME_NONNULL_BEGIN - (void)prepareCheckForUpdatesWithCompletion:(SUUpdateDriverCompletion)completionBlock; -- (void)preflightForUpdatePermissionWithReply:(void (^)(NSError * _Nullable))reply; +- (void)preflightForUpdatePermissionPreventingInstallerInteraction:(BOOL)preventsInstallerInteraction reply:(void (^)(NSError * _Nullable))reply; -- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates; +- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates; - (void)resumeInstallingUpdateWithCompletion:(SUUpdateDriverCompletion)completionBlock; diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 4e7b6d54..60ce14cb 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -31,6 +31,7 @@ @property (nonatomic, readonly) id userDriver; @property (nonatomic) BOOL resumingInstallingUpdate; @property (nonatomic) BOOL resumingDownloadedUpdate; +@property (nonatomic) BOOL preventsInstallerInteraction; @end @@ -44,6 +45,7 @@ @synthesize delegate = _delegate; @synthesize resumingInstallingUpdate = _resumingInstallingUpdate; @synthesize resumingDownloadedUpdate = _resumingDownloadedUpdate; +@synthesize preventsInstallerInteraction = _preventsInstallerInteraction; - (instancetype)initWithHost:(SUHost *)host applicationBundle:(NSBundle *)applicationBundle sparkleBundle:(NSBundle *)sparkleBundle updater:(id)updater userDriver:(id )userDriver updaterDelegate:(nullable id )updaterDelegate delegate:(id)delegate { @@ -65,12 +67,14 @@ [self.coreDriver prepareCheckForUpdatesWithCompletion:completionBlock]; } -- (void)preflightForUpdatePermissionWithReply:(void (^)(NSError * _Nullable))reply +- (void)preflightForUpdatePermissionPreventingInstallerInteraction:(BOOL)preventsInstallerInteraction reply:(void (^)(NSError * _Nullable))reply { - [self.coreDriver preflightForUpdatePermissionWithReply:reply]; + self.preventsInstallerInteraction = preventsInstallerInteraction; + + [self.coreDriver preflightForUpdatePermissionPreventingInstallerInteraction:preventsInstallerInteraction reply:reply]; } -- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates +- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders includesSkippedUpdates:(BOOL)includesSkippedUpdates { [self.coreDriver checkForUpdatesAtAppcastURL:appcastURL withUserAgent:userAgent httpHeaders:httpHeaders includesSkippedUpdates:includesSkippedUpdates requiresSilentInstall:NO]; } @@ -94,16 +98,6 @@ } } -- (BOOL)canDisplayInstallerUserInterface -{ - // Are we allowed to perform updates that may require interaction? - BOOL updaterAllowsInteraction = YES; - if ([self.updaterDelegate respondsToSelector:@selector(updaterShouldAllowInstallerInteraction:)]) { - updaterAllowsInteraction = [self.updaterDelegate updaterShouldAllowInstallerInteraction:self.updater]; - } - return updaterAllowsInteraction; -} - - (void)basicDriverDidFindUpdateWithAppcastItem:(SUAppcastItem *)updateItem { if (self.resumingDownloadedUpdate) { @@ -145,7 +139,7 @@ [self.userDriver showResumableUpdateFoundWithAppcastItem:updateItem reply:^(SPUInstallUpdateStatus choice) { dispatch_async(dispatch_get_main_queue(), ^{ [self.host setObject:nil forUserDefaultsKey:SUSkippedVersionKey]; - [self.coreDriver finishInstallationWithResponse:choice displayingUserInterface:[self canDisplayInstallerUserInterface]]; + [self.coreDriver finishInstallationWithResponse:choice displayingUserInterface:!self.preventsInstallerInteraction]; }); }]; } @@ -214,7 +208,7 @@ if (!willInstallImmediately) { [self.userDriver showReadyToInstallAndRelaunch:^(SPUInstallUpdateStatus installUpdateStatus) { dispatch_async(dispatch_get_main_queue(), ^{ - [self.coreDriver finishInstallationWithResponse:installUpdateStatus displayingUserInterface:[self canDisplayInstallerUserInterface]]; + [self.coreDriver finishInstallationWithResponse:installUpdateStatus displayingUserInterface:!self.preventsInstallerInteraction]; }); }]; } diff --git a/Sparkle/SUUpdateDriver.h b/Sparkle/SUUpdateDriver.h index f5788c42..e3f4050a 100644 --- a/Sparkle/SUUpdateDriver.h +++ b/Sparkle/SUUpdateDriver.h @@ -14,7 +14,7 @@ typedef void (^SUUpdateDriverCompletion)(BOOL shouldShowUpdateImmediately, SUDow @protocol SUUpdateDriver -- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders completion:(SUUpdateDriverCompletion)completionBlock; +- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders preventingInstallerInteraction:(BOOL)preventsInstallerInteraction completion:(SUUpdateDriverCompletion)completionBlock; - (void)resumeInstallingUpdateWithCompletion:(SUUpdateDriverCompletion)completionBlock; diff --git a/Sparkle/SUUserInitiatedUpdateDriver.m b/Sparkle/SUUserInitiatedUpdateDriver.m index 105d4553..b2bebbce 100644 --- a/Sparkle/SUUserInitiatedUpdateDriver.m +++ b/Sparkle/SUUserInitiatedUpdateDriver.m @@ -40,11 +40,11 @@ return self; } -- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary *)httpHeaders completion:(SUUpdateDriverCompletion)completionBlock +- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders preventingInstallerInteraction:(BOOL)preventsInstallerInteraction completion:(SUUpdateDriverCompletion)completionBlock { [self.uiDriver prepareCheckForUpdatesWithCompletion:completionBlock]; - [self.uiDriver preflightForUpdatePermissionWithReply:^(NSError * _Nullable error) { + [self.uiDriver preflightForUpdatePermissionPreventingInstallerInteraction:preventsInstallerInteraction reply:^(NSError * _Nullable error) { if (!self.aborted) { if (error != nil) { [self abortUpdateWithError:error];