From aee0b6f7d85cc06302610eb00cfc7fce0e47e2f1 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 27 Feb 2016 17:54:19 -0500 Subject: [PATCH] Allow user driver's delegate to handle initiating update checks In a XPC environment, a delegate may handle update checks by sending a message to the XPC process that tells it to reset the sparkle updater's check cycle. If the user driver delegate can do this, the XPC process will not need to wait for a reply, allowing the system to kill the XPC process when it becomes idle. --- Sparkle/SUSparkleUserUpdaterDriver.h | 10 ++++---- Sparkle/SUSparkleUserUpdaterDriver.m | 34 +++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/Sparkle/SUSparkleUserUpdaterDriver.h b/Sparkle/SUSparkleUserUpdaterDriver.h index 76578c4a..b2434123 100644 --- a/Sparkle/SUSparkleUserUpdaterDriver.h +++ b/Sparkle/SUSparkleUserUpdaterDriver.h @@ -11,21 +11,23 @@ @class SUHost; -@protocol SUModalAlertDelegate +@protocol SUSparkleUserUpdaterDriverDelegate @optional -- (void)userUpdaterDriverWillShowModalAlert:(id )userUpdaterDriver; +- (BOOL)responsibleForInitiatingUpdateCheckForUserDriver:(id )userUpdaterDriver; +- (void)initiateUpdateCheckForUserDriver:(id )userUpdaterDriver; +- (void)userUpdaterDriverWillShowModalAlert:(id )userUpdaterDriver; - (void)userUpdaterDriverDidShowModalAlert:(id )userUpdaterDriver; @end @interface SUSparkleUserUpdaterDriver : NSObject -- (instancetype)initWithHost:(SUHost *)host handlesTermination:(BOOL)handlesTermination delegate:(id )delegate; +- (instancetype)initWithHost:(SUHost *)host handlesTermination:(BOOL)handlesTermination delegate:(id )delegate; -@property (nonatomic, readonly, weak) id delegate; +@property (nonatomic, readonly, weak) id delegate; @property (nonatomic, readonly, getter = isInstallingUpdateOnTermination) BOOL installingUpdateOnTermination; - (void)sendTerminationSignalWithCompletion:(void (^)(void))finishTermination; diff --git a/Sparkle/SUSparkleUserUpdaterDriver.m b/Sparkle/SUSparkleUserUpdaterDriver.m index 5a8e27a3..b916c770 100644 --- a/Sparkle/SUSparkleUserUpdaterDriver.m +++ b/Sparkle/SUSparkleUserUpdaterDriver.m @@ -74,7 +74,7 @@ #pragma mark Birth -- (instancetype)initWithHost:(SUHost *)host handlesTermination:(BOOL)handlesTermination delegate:(id)delegate +- (instancetype)initWithHost:(SUHost *)host handlesTermination:(BOOL)handlesTermination delegate:(id)delegate { self = [super init]; if (self != nil) { @@ -87,18 +87,40 @@ #pragma mark Check Updates Timer +- (BOOL)isDelegateResponsibleForUpdateChecking +{ + BOOL result = NO; + if ([self.delegate respondsToSelector:@selector(responsibleForInitiatingUpdateCheckForUserDriver:)]) { + result = [self.delegate responsibleForInitiatingUpdateCheckForUserDriver:self]; + } + return result; +} + - (void)checkForUpdates:(NSTimer *)__unused timer { - if (self.checkForUpdatesReply != nil) { - self.checkForUpdatesReply(SUCheckForUpdateNow); - self.checkForUpdatesReply = nil; + if ([self isDelegateResponsibleForUpdateChecking]) { + if ([self.delegate respondsToSelector:@selector(initiateUpdateCheckForUserDriver:)]) { + [self.delegate initiateUpdateCheckForUserDriver:self]; + } else { + NSLog(@"Error: Delegate %@ for user driver %@ must implement initiateUpdateCheckForUserDriver: because it returned YES from responsibleForInitiatingUpdateCheckForUserDriver:", self.delegate, self); + } + } else { + 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; + if ([self isDelegateResponsibleForUpdateChecking]) { + reply(SUCheckForUpdateWillOccurLater); + } else { + self.checkForUpdatesReply = reply; + } + self.checkUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(checkForUpdates:) userInfo:nil repeats:NO]; }); } @@ -308,7 +330,7 @@ - (void)showAlert:(NSAlert *)alert { dispatch_async(dispatch_get_main_queue(), ^{ - id delegate = self.delegate; + id delegate = self.delegate; if ([delegate respondsToSelector:@selector(userUpdaterDriverWillShowModalAlert:)]) { [delegate userUpdaterDriverWillShowModalAlert:self];