From 3ed77f975a4402bce3c1b92ce7e1bfbcad58c4de Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 30 Oct 2021 11:03:37 -0700 Subject: [PATCH] Don't use current date unless necessary when scheduling next update (1.x) (#1991) --- Sparkle/SUUpdater.m | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index f2bf066a..904d416a 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -45,7 +45,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (instancetype)initForBundle:(NSBundle *)bundle; - (void)startUpdateCycle; - (void)checkForUpdatesWithDriver:(SUUpdateDriver *)updateDriver; -- (void)scheduleNextUpdateCheck; +- (void)scheduleNextUpdateCheckUsingCurrentTime:(BOOL)usingCurrentTime; - (void)registerAsObserver; - (void)unregisterAsObserver; - (void)updateDriverDidFinish:(NSNotification *)note; @@ -255,7 +255,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults // 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 scheduleNextUpdateCheckUsingCurrentTime:YES]; } } @@ -271,7 +271,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults { self.driver = nil; [self updateLastUpdateCheckDate]; - [self scheduleNextUpdateCheck]; + [self scheduleNextUpdateCheckUsingCurrentTime:NO]; } } @@ -293,7 +293,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults [self didChangeValueForKey:@"lastUpdateCheckDate"]; } -- (void)scheduleNextUpdateCheck +- (void)scheduleNextUpdateCheckUsingCurrentTime:(BOOL)usingCurrentTime { if (self.checkTimer) { @@ -303,9 +303,20 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults if (![self automaticallyChecksForUpdates]) return; // How long has it been since last we checked for an update? - NSDate *lastCheckDate = [self lastUpdateCheckDate]; - if (!lastCheckDate) { lastCheckDate = [NSDate distantPast]; } - NSTimeInterval intervalSinceCheck = [[NSDate date] timeIntervalSinceDate:lastCheckDate]; + NSTimeInterval intervalSinceCheck; + if (usingCurrentTime) { + NSDate *lastCheckDate = [self lastUpdateCheckDate]; + if (!lastCheckDate) { lastCheckDate = [NSDate distantPast]; } + intervalSinceCheck = [[NSDate date] timeIntervalSinceDate:lastCheckDate]; + if (intervalSinceCheck < 0) { + // Last update check date is in the future and bogus, so reset it to current date + [self updateLastUpdateCheckDate]; + + intervalSinceCheck = 0; + } + } else { + intervalSinceCheck = 0; + } // Now we want to figure out how long until we check again. NSTimeInterval delayUntilCheck, updateCheckInterval = [self updateCheckInterval]; @@ -333,23 +344,13 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults - (void)receiveWakeNote { if (self.shouldRescheduleOnWake) // the reason for rescheduling the update-check timer is that NSTimer does behave as if the time the Mac spends asleep did not exist at all, which can significantly prolong the time between update checks - [self scheduleNextUpdateCheck]; + [self scheduleNextUpdateCheckUsingCurrentTime:YES]; } - (void)checkForUpdatesInBackground { BOOL automatic = [self automaticallyDownloadsUpdates]; - if (!automatic) { - NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.apple.notificationcenterui"]; - BOOL dnd = [defaults boolForKey:@"doNotDisturb"]; - if (dnd) { - SULog(SULogLevelDefault, @"Delayed update, because Do Not Disturb is on"); - [self updateLastUpdateCheckDate]; - [self scheduleNextUpdateCheck]; - return; - } - } - + // Do not use reachability for a preflight check. This can be deceptive and a bad idea. Apple does not recommend doing it. SUUpdateDriver *theUpdateDriver = [(SUBasicUpdateDriver *)[(automatic ? [SUAutomaticUpdateDriver class] : [SUScheduledUpdateDriver class])alloc] initWithUpdater:self]; @@ -396,7 +397,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults if( [self.delegate respondsToSelector: @selector(updaterMayCheckForUpdates:)] && ![self.delegate updaterMayCheckForUpdates: self] ) { - [self scheduleNextUpdateCheck]; + [self scheduleNextUpdateCheckUsingCurrentTime:NO]; return; } @@ -405,7 +406,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults // If we're not given a driver at all, just schedule the next update check and bail. if (!self.driver) { - [self scheduleNextUpdateCheck]; + [self scheduleNextUpdateCheckUsingCurrentTime:NO]; return; } @@ -457,7 +458,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults - (void)resetUpdateCycle { [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(resetUpdateCycle) object:nil]; - [self scheduleNextUpdateCheck]; + [self scheduleNextUpdateCheckUsingCurrentTime:YES]; } - (void)setAutomaticallyChecksForUpdates:(BOOL)automaticallyCheckForUpdates