From 6f8e8e4789724e5b0c140bfbd40e570ef3d14e1d Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 3 Oct 2021 12:58:00 -0700 Subject: [PATCH] Make canCheckForUpdates property KVO compliant/observable (#1968) * Make canCheckForUpdates property KVO compliant/observable Also set updater session in progress when probing for update check interval to use. * Add assert that driver must be nil in checkForUpdatesWithDriver:.. --- Sparkle/SPUAutomaticUpdateDriver.m | 4 ++ Sparkle/SPUProbingUpdateDriver.m | 4 ++ Sparkle/SPUScheduledUpdateDriver.m | 11 +++ Sparkle/SPUUpdateDriver.h | 2 + Sparkle/SPUUpdater.h | 2 + Sparkle/SPUUpdater.m | 95 +++++++++++++++----------- Sparkle/SPUUserInitiatedUpdateDriver.m | 18 ++++- 7 files changed, 94 insertions(+), 42 deletions(-) diff --git a/Sparkle/SPUAutomaticUpdateDriver.m b/Sparkle/SPUAutomaticUpdateDriver.m index 8fe1ce30..0abf11b9 100644 --- a/Sparkle/SPUAutomaticUpdateDriver.m +++ b/Sparkle/SPUAutomaticUpdateDriver.m @@ -57,6 +57,10 @@ [self.coreDriver setCompletionHandler:completionBlock]; } +- (void)setUpdateShownHandler:(void (^)(void))updateShownHandler +{ +} + - (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders { [self.coreDriver checkForUpdatesAtAppcastURL:appcastURL withUserAgent:userAgent httpHeaders:httpHeaders inBackground:YES requiresSilentInstall:YES]; diff --git a/Sparkle/SPUProbingUpdateDriver.m b/Sparkle/SPUProbingUpdateDriver.m index 614e0231..0edac192 100644 --- a/Sparkle/SPUProbingUpdateDriver.m +++ b/Sparkle/SPUProbingUpdateDriver.m @@ -38,6 +38,10 @@ [self.basicDriver setCompletionHandler:completionBlock]; } +- (void)setUpdateShownHandler:(void (^)(void))updateShownHandler +{ +} + - (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders { [self.basicDriver checkForUpdatesAtAppcastURL:appcastURL withUserAgent:userAgent httpHeaders:httpHeaders inBackground:YES]; diff --git a/Sparkle/SPUScheduledUpdateDriver.m b/Sparkle/SPUScheduledUpdateDriver.m index 99c86ff4..99b5b2f3 100644 --- a/Sparkle/SPUScheduledUpdateDriver.m +++ b/Sparkle/SPUScheduledUpdateDriver.m @@ -19,6 +19,7 @@ @property (nonatomic, readonly) SPUUIBasedUpdateDriver *uiDriver; @property (nonatomic) BOOL showedUpdate; +@property (nonatomic) void (^updateDidShowHandler)(void); @end @@ -26,6 +27,7 @@ @synthesize uiDriver = _uiDriver; @synthesize showedUpdate = _showedUpdate; +@synthesize updateDidShowHandler = _updateDidShowHandler; - (instancetype)initWithHost:(SUHost *)host applicationBundle:(NSBundle *)applicationBundle updater:(id)updater userDriver:(id )userDriver updaterDelegate:(nullable id )updaterDelegate { @@ -41,6 +43,11 @@ [self.uiDriver setCompletionHandler:completionBlock]; } +- (void)setUpdateShownHandler:(void (^)(void))handler +{ + self.updateDidShowHandler = handler; +} + - (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders { [self.uiDriver checkForUpdatesAtAppcastURL:appcastURL withUserAgent:userAgent httpHeaders:httpHeaders inBackground:YES]; @@ -59,6 +66,10 @@ - (void)uiDriverDidShowUpdate { self.showedUpdate = YES; + + if (self.updateDidShowHandler != nil) { + self.updateDidShowHandler(); + } } - (BOOL)showingUpdate diff --git a/Sparkle/SPUUpdateDriver.h b/Sparkle/SPUUpdateDriver.h index a3fab70d..1aca6bcd 100644 --- a/Sparkle/SPUUpdateDriver.h +++ b/Sparkle/SPUUpdateDriver.h @@ -21,6 +21,8 @@ typedef void (^SPUUpdateDriverCompletion)(BOOL shouldShowUpdateImmediately, id resumableUpdate; @property (nonatomic) BOOL sessionInProgress; +@property (nonatomic) BOOL canCheckForUpdates; @property (nonatomic) BOOL showingPermissionRequest; @property (nonatomic, copy) NSDate *updateLastCheckedDate; @@ -83,6 +84,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @synthesize startedUpdater = _startedUpdater; @synthesize resumableUpdate = _resumableUpdate; @synthesize sessionInProgress = _sessionInProgress; +@synthesize canCheckForUpdates = _canCheckForUpdates; @synthesize showingPermissionRequest = _showingPermissionRequest; @synthesize updateLastCheckedDate = _updateLastCheckedDate; @synthesize loggedATSWarning = _loggedATSWarning; @@ -155,6 +157,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } self.startedUpdater = YES; + self.canCheckForUpdates = YES; // Start updater on next update cycle so we make sure the application invoking the updater is ready // This also gives the developer a cycle to check for updates before Sparkle's update cycle scheduler kicks in @@ -341,6 +344,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification self.showingPermissionRequest = YES; self.sessionInProgress = YES; + self.canCheckForUpdates = YES; __weak SPUUpdater *weakSelf = self; [self.userDriver showUpdatePermissionRequest:updatePermissionRequest reply:^(SUUpdatePermissionResponse *response) { @@ -360,7 +364,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification // We start the update checks and register as observer for changes after the prompt finishes } else { // We check if the user's said they want updates, or they haven't said anything, and the default is set to checking. - [self scheduleNextUpdateCheck]; + [self scheduleNextUpdateCheckFiringImmediately:NO]; } } @@ -389,16 +393,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification [self didChangeValueForKey:NSStringFromSelector(@selector((lastUpdateCheckDate)))]; } -- (void)scheduleNextUpdateCheck -{ - [self scheduleNextUpdateCheckFiringImmediately:NO]; -} - -- (BOOL)canCheckForUpdates -{ - return self.startedUpdater && (self.showingPermissionRequest || self.driver.showingUpdate || !self.sessionInProgress); -} - +// Note this method is never called when sessionInProgress is YES - (void)scheduleNextUpdateCheckFiringImmediately:(BOOL)firingImmediately { [self.updaterTimer invalidate]; @@ -413,7 +408,36 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification if (firingImmediately) { [self checkForUpdatesInBackground]; } else { - [self retrieveNextUpdateCheckInterval:^(NSTimeInterval updateCheckInterval) { + // This may not return the same update check interval as the developer has configured + // Notably it may differ when we have an update that has been already downloaded and needs to resume, + // as well as if that update is marked critical or not + void (^retrieveNextUpdateCheckInterval)(void (^)(NSTimeInterval)) = ^(void (^completionHandler)(NSTimeInterval)) { + NSString *hostBundleIdentifier = self.host.bundle.bundleIdentifier; + assert(hostBundleIdentifier != nil); + [SPUProbeInstallStatus probeInstallerUpdateItemForHostBundleIdentifier:hostBundleIdentifier completion:^(SPUInstallationInfo * _Nullable installationInfo) { + dispatch_async(dispatch_get_main_queue(), ^{ + NSTimeInterval regularCheckInterval = [self updateCheckInterval]; + if (installationInfo == nil) { + // Proceed as normal if there's no resumable updates + completionHandler(regularCheckInterval); + } else { + if (!installationInfo.canSilentlyInstall || [installationInfo.appcastItem isCriticalUpdate] || [installationInfo.appcastItem isInformationOnlyUpdate]) { + completionHandler(MIN(regularCheckInterval, SUImpatientUpdateCheckInterval)); + } else { + completionHandler(MAX(regularCheckInterval, SUImpatientUpdateCheckInterval)); + } + } + }); + }]; + }; + + self.canCheckForUpdates = NO; + self.sessionInProgress = YES; + + retrieveNextUpdateCheckInterval(^(NSTimeInterval updateCheckInterval) { + self.canCheckForUpdates = YES; + self.sessionInProgress = NO; + // This callback is asynchronous, so the timer may be set. Invalidate to make sure it isn't. [self.updaterTimer invalidate]; @@ -435,7 +459,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification // We're overdue! Run one now. [self checkForUpdatesInBackground]; } - }]; + }); } } @@ -457,6 +481,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } self.sessionInProgress = YES; + self.canCheckForUpdates = NO; // We don't want the probe check to act on the driver if the updater is going near death __weak SPUUpdater *weakSelf = self; @@ -518,6 +543,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } self.sessionInProgress = YES; + self.canCheckForUpdates = NO; id theUpdateDriver = [[SPUUserInitiatedUpdateDriver alloc] initWithHost:self.host applicationBundle:self.applicationBundle updater:self userDriver:self.userDriver updaterDelegate:self.delegate]; @@ -549,6 +575,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } self.sessionInProgress = YES; + self.canCheckForUpdates = NO; NSString *bundleIdentifier = self.host.bundle.bundleIdentifier; assert(bundleIdentifier != nil); @@ -564,6 +591,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)checkForUpdatesWithDriver:(id )d updateCheck:(SPUUpdateCheck)updateCheck installerInProgress:(BOOL)installerInProgress { + assert(self.driver == nil); if (self.driver != nil) { return; } @@ -600,16 +628,18 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification SPUUpdater *strongSelf = weakSelf; if (strongSelf != nil) { strongSelf.driver = nil; - strongSelf.sessionInProgress = NO; [strongSelf updateLastUpdateCheckDate]; + strongSelf.sessionInProgress = NO; + strongSelf.canCheckForUpdates = YES; + notifyDelegateOfDriverCompletion(error, NO); // Ensure the delegate doesn't start a new session when being notified of the previous one ending if (!strongSelf.sessionInProgress) { if (shouldScheduleNextUpdateCheck) { - [strongSelf scheduleNextUpdateCheck]; + [strongSelf scheduleNextUpdateCheckFiringImmediately:NO]; } else { SULog(SULogLevelDefault, @"Disabling scheduled updates.."); } @@ -650,9 +680,12 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification if (strongSelf != nil) { strongSelf.resumableUpdate = resumableUpdate; strongSelf.driver = nil; - strongSelf.sessionInProgress = NO; + [strongSelf updateLastUpdateCheckDate]; + strongSelf.sessionInProgress = NO; + strongSelf.canCheckForUpdates = YES; + notifyDelegateOfDriverCompletion(error, shouldShowUpdateImmediately); // Ensure the delegate doesn't start a new session when being notified of the previous one ending @@ -662,6 +695,10 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification } }]; + [self.driver setUpdateShownHandler:^{ + weakSelf.canCheckForUpdates = YES; + }]; + if (installerInProgress) { // Resume an update that has already begun installing in the background [self.driver resumeInstallingUpdate]; @@ -698,7 +735,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification if (!self.sessionInProgress) { [self cancelNextUpdateCycle]; - [self scheduleNextUpdateCheck]; + [self scheduleNextUpdateCheckFiringImmediately:NO]; } } @@ -935,30 +972,6 @@ static NSString *escapeURLComponent(NSString *str) { return [self.updaterSettings updateCheckInterval]; } -// This may not return the same update check interval as the developer has configured -// Notably it may differ when we have an update that has been already downloaded and needs to resume, -// as well as if that update is marked critical or not -- (void)retrieveNextUpdateCheckInterval:(void (^)(NSTimeInterval))completionHandler -{ - NSString *hostBundleIdentifier = self.host.bundle.bundleIdentifier; - assert(hostBundleIdentifier != nil); - [SPUProbeInstallStatus probeInstallerUpdateItemForHostBundleIdentifier:hostBundleIdentifier completion:^(SPUInstallationInfo * _Nullable installationInfo) { - dispatch_async(dispatch_get_main_queue(), ^{ - NSTimeInterval regularCheckInterval = [self updateCheckInterval]; - if (installationInfo == nil) { - // Proceed as normal if there's no resumable updates - completionHandler(regularCheckInterval); - } else { - if (!installationInfo.canSilentlyInstall || [installationInfo.appcastItem isCriticalUpdate] || [installationInfo.appcastItem isInformationOnlyUpdate]) { - completionHandler(MIN(regularCheckInterval, SUImpatientUpdateCheckInterval)); - } else { - completionHandler(MAX(regularCheckInterval, SUImpatientUpdateCheckInterval)); - } - } - }); - }]; -} - - (void)dealloc { // Stop checking for updates diff --git a/Sparkle/SPUUserInitiatedUpdateDriver.m b/Sparkle/SPUUserInitiatedUpdateDriver.m index b62bbe54..6d64bb76 100644 --- a/Sparkle/SPUUserInitiatedUpdateDriver.m +++ b/Sparkle/SPUUserInitiatedUpdateDriver.m @@ -20,6 +20,7 @@ @property (nonatomic) BOOL showingUserInitiatedProgress; @property (nonatomic) BOOL showingUpdate; @property (nonatomic) BOOL aborted; +@property (nonatomic) void (^updateDidShowHandler)(void); @end @@ -30,6 +31,7 @@ @synthesize showingUserInitiatedProgress = _showingUserInitiatedProgress; @synthesize showingUpdate = _showingUpdate; @synthesize aborted = _aborted; +@synthesize updateDidShowHandler = _updateDidShowHandler; - (instancetype)initWithHost:(SUHost *)host applicationBundle:(NSBundle *)applicationBundle updater:(id)updater userDriver:(id )userDriver updaterDelegate:(nullable id )updaterDelegate { @@ -46,10 +48,19 @@ [self.uiDriver setCompletionHandler:completionBlock]; } +- (void)setUpdateShownHandler:(void (^)(void))handler +{ + self.updateDidShowHandler = handler; +} + - (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders { self.showingUserInitiatedProgress = YES; - self.showingUpdate = YES; + + if (self.updateDidShowHandler != nil) { + self.updateDidShowHandler(); + self.updateDidShowHandler = nil; + } [self.userDriver showUserInitiatedUpdateCheckWithCancellation:^{ dispatch_async(dispatch_get_main_queue(), ^{ @@ -77,6 +88,11 @@ // When a new update check has not been initiated and an update has been resumed, // update the driver to indicate we are showing an update to the user self.showingUpdate = YES; + + if (self.updateDidShowHandler != nil) { + self.updateDidShowHandler(); + self.updateDidShowHandler = nil; + } } - (void)basicDriverIsRequestingAbortUpdateWithError:(nullable NSError *)error