From fbfcbbdf2dcb68f596357b9bbc011c3a11f58547 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 27 Feb 2016 16:28:25 -0500 Subject: [PATCH] Move check update timer into the user driver In an XPC environment, XPC processes may be killed if they are considered idle. Thus, we must do the update checking in the user driver. --- Sparkle/SUSparkleUserUpdaterDriver.m | 45 ++++++++++++++++++++++++++++ Sparkle/SUUpdater.m | 35 ++++++++++++---------- Sparkle/SUUserUpdaterDriver.h | 8 +++++ 3 files changed, 73 insertions(+), 15 deletions(-) diff --git a/Sparkle/SUSparkleUserUpdaterDriver.m b/Sparkle/SUSparkleUserUpdaterDriver.m index 87351c69..5a8e27a3 100644 --- a/Sparkle/SUSparkleUserUpdaterDriver.m +++ b/Sparkle/SUSparkleUserUpdaterDriver.m @@ -36,6 +36,9 @@ @property (nonatomic, readonly) SUHost *host; @property (nonatomic, readonly) BOOL handlesTermination; +@property (nonatomic) NSTimer *checkUpdateTimer; +@property (nonatomic, copy) void (^checkForUpdatesReply)(SUUpdateCheckTimerStatus); + @property (nonatomic) SUStatusController *checkingController; @property (nonatomic, copy) void (^updateCheckStatusCompletion)(SUUserInitiatedCheckStatus); @@ -57,6 +60,8 @@ @synthesize host = _host; @synthesize handlesTermination = _handlesTermination; @synthesize delegate = _delegate; +@synthesize checkUpdateTimer = _checkUpdateTimer; +@synthesize checkForUpdatesReply = _checkForUpdatesReply; @synthesize checkingController = _checkingController; @synthesize updateCheckStatusCompletion = _updateCheckStatusCompletion; @synthesize activeUpdateAlert = _activeUpdateAlert; @@ -80,6 +85,44 @@ return self; } +#pragma mark Check Updates Timer + +- (void)checkForUpdates:(NSTimer *)__unused timer +{ + if (self.checkForUpdatesReply != nil) { + self.checkForUpdatesReply(SUCheckForUpdateNow); + self.checkForUpdatesReply = nil; + } +} + +- (void)startUpdateCheckTimerWithNextTimeInterval:(NSTimeInterval)timeInterval reply:(void (^)(SUUpdateCheckTimerStatus))reply +{ + dispatch_async(dispatch_get_main_queue(), ^{ + self.checkForUpdatesReply = reply; + self.checkUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(checkForUpdates:) userInfo:nil repeats:NO]; + }); +} + +- (void)_invalidateUpdateCheckTimer +{ + if (self.checkUpdateTimer != nil) { + [self.checkUpdateTimer invalidate]; + self.checkUpdateTimer = nil; + + if (self.checkForUpdatesReply != nil) { + self.checkForUpdatesReply(SUCheckForUpdateWillOccurLater); + self.checkForUpdatesReply = nil; + } + } +} + +- (void)invalidateUpdateCheckTimer +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self _invalidateUpdateCheckTimer]; + }); +} + #pragma mark Update Permission - (void)requestUpdatePermissionWithSystemProfile:(NSArray *)systemProfile reply:(void (^)(SUUpdatePermissionPromptResult *))reply @@ -464,6 +507,8 @@ - (void)dismissUpdateInstallation { dispatch_async(dispatch_get_main_queue(), ^{ + [self _invalidateUpdateCheckTimer]; + [self cancelCheckForUpdates]; [self cancelDownload]; diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index 46a900e2..0f377157 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -31,7 +31,6 @@ NSString *const SUUpdaterAppcastItemNotificationKey = @"SUUpdaterAppcastItemNoti NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotificationKey"; @interface SUUpdater () -@property (strong) NSTimer *checkTimer; @property (strong) NSBundle *sparkleBundle; - (instancetype)initForBundle:(NSBundle *)bundle; @@ -52,7 +51,6 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification @synthesize delegate; @synthesize userUpdaterDriver = _userUpdaterDriver; -@synthesize checkTimer; @synthesize userAgentString = customUserAgentString; @synthesize httpHeaders; @synthesize driver; @@ -220,11 +218,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults - (void)scheduleNextUpdateCheck { - if (self.checkTimer) - { - [self.checkTimer invalidate]; - self.checkTimer = nil; // Timer is non-repeating, may have invalidated itself, so we had to retain it. - } + [self.userUpdaterDriver invalidateUpdateCheckTimer]; if (![self automaticallyChecksForUpdates]) return; // How long has it been since last we checked for an update? @@ -233,14 +227,24 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults NSTimeInterval intervalSinceCheck = [[NSDate date] timeIntervalSinceDate:lastCheckDate]; // Now we want to figure out how long until we check again. - NSTimeInterval delayUntilCheck, updateCheckInterval = [self updateCheckInterval]; + NSTimeInterval updateCheckInterval = [self updateCheckInterval]; if (updateCheckInterval < SUMinimumUpdateCheckInterval) updateCheckInterval = SUMinimumUpdateCheckInterval; - if (intervalSinceCheck < updateCheckInterval) - delayUntilCheck = (updateCheckInterval - intervalSinceCheck); // It hasn't been long enough. - else - delayUntilCheck = 0; // We're overdue! Run one now. - self.checkTimer = [NSTimer scheduledTimerWithTimeInterval:delayUntilCheck target:self selector:@selector(checkForUpdatesInBackground) userInfo:nil repeats:NO]; // Timer is non-repeating, may have invalidated itself, so we had to retain it. + if (intervalSinceCheck < updateCheckInterval) { + NSTimeInterval delayUntilCheck = (updateCheckInterval - intervalSinceCheck); // It hasn't been long enough. + [self.userUpdaterDriver startUpdateCheckTimerWithNextTimeInterval:delayUntilCheck reply:^(SUUpdateCheckTimerStatus checkTimerStatus) { + switch (checkTimerStatus) { + case SUCheckForUpdateNow: + [self checkForUpdatesInBackground]; + break; + case SUCheckForUpdateWillOccurLater: + break; + } + }]; + } else { + // We're overdue! Run one now. + [self checkForUpdatesInBackground]; + } } @@ -346,7 +350,8 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults - (void)checkForUpdatesWithDriver:(SUUpdateDriver *)d { if ([self updateInProgress]) { return; } - if (self.checkTimer) { [self.checkTimer invalidate]; self.checkTimer = nil; } // Timer is non-repeating, may have invalidated itself, so we had to retain it. + + [self.userUpdaterDriver invalidateUpdateCheckTimer]; [self willChangeValueForKey:@"lastUpdateCheckDate"]; [self.host setObject:[NSDate date] forUserDefaultsKey:SULastCheckTimeKey]; @@ -572,7 +577,7 @@ static NSString *const SUUpdaterDefaultsObservationContext = @"SUUpdaterDefaults - (void)dealloc { [self unregisterAsObserver]; - if (checkTimer) { [checkTimer invalidate]; } // Timer is non-repeating, may have invalidated itself, so we had to retain it. + [self.userUpdaterDriver invalidateUpdateCheckTimer]; } - (BOOL)validateMenuItem:(NSMenuItem *)item diff --git a/Sparkle/SUUserUpdaterDriver.h b/Sparkle/SUUserUpdaterDriver.h index 3f950b41..0ef87279 100644 --- a/Sparkle/SUUserUpdaterDriver.h +++ b/Sparkle/SUUserUpdaterDriver.h @@ -14,6 +14,11 @@ @protocol SUVersionDisplay; +typedef NS_ENUM(NSUInteger, SUUpdateCheckTimerStatus) { + SUCheckForUpdateNow, + SUCheckForUpdateWillOccurLater +}; + typedef NS_ENUM(NSUInteger, SUUserInitiatedCheckStatus) { SUUserInitiatedCheckDone, SUUserInitiatedCheckCancelled @@ -36,6 +41,9 @@ typedef NS_ENUM(NSUInteger, SUInstallUpdateStatus) { @protocol SUUserUpdaterDriver +- (void)startUpdateCheckTimerWithNextTimeInterval:(NSTimeInterval)timeInterval reply:(void (^)(SUUpdateCheckTimerStatus))reply; +- (void)invalidateUpdateCheckTimer; + #warning might need to whitelist class types for systemProfile.. need to test this - (void)requestUpdatePermissionWithSystemProfile:(NSArray *)systemProfile reply:(void (^)(SUUpdatePermissionPromptResult *))reply;