From f2e485df87ad61cf67b2fc23677b1e5a5ce7fe61 Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 4 Mar 2016 19:00:47 -0500 Subject: [PATCH] Add proper remote updater idling support Meaning that we don't send a message or re-establish the XPC connection immediately when something goes wrong --- Sparkle/SUStandardUserDriver.h | 34 ++++++-- Sparkle/SUStandardUserDriver.m | 83 +++++++++++++------ Sparkle/SUUpdater.m | 8 +- Sparkle/SUUserDriver.h | 12 +++ .../SURemoteUpdateSettingsWindowController.m | 64 ++++++++------ 5 files changed, 143 insertions(+), 58 deletions(-) diff --git a/Sparkle/SUStandardUserDriver.h b/Sparkle/SUStandardUserDriver.h index 7b19abe0..393065b4 100644 --- a/Sparkle/SUStandardUserDriver.h +++ b/Sparkle/SUStandardUserDriver.h @@ -32,12 +32,29 @@ NS_ASSUME_NONNULL_BEGIN @property (nonatomic, weak, nullable) id delegate; /*! - Indicates whether or not an update is in progress as far as the user is concerned + Indicates whether or not an update is in progress as far as the user's perspective is concerned A typical application may rely on this property for its check for updates menu item validation */ @property (nonatomic, readonly, getter=isUpdateInProgress) BOOL updateInProgress; +/*! + Indicates whether or not a currently pending update check will be initiated in the future + + This may be useful for deciding if the user driver should be invalidated, when the connection to the updater + has been interrupted. + */ +@property (nonatomic, readonly) BOOL willInitiateNextUpdateCheck; + +/*! + Indicates whether or not Sparkle's updater has been idling on update checks; i.e, if automatic update checks + had been disabled the last time the updater checked. + + This may be useful for deciding if a connection needs to be immediately resumed or re-created if it has been interrupted. + See also -willInitiateNextUpdateCheck + */ +@property (nonatomic, readonly) BOOL idlesOnUpdateChecks; + /*! Send a signal to the user driver that the user application should terminate. @@ -66,16 +83,17 @@ NS_ASSUME_NONNULL_BEGIN - (void)terminateApplication; /*! - Dismiss the current update installation + Invalidate the current update installation - This is appropriate to call when the connection to the updater has been interrupted or invalidated - before trying to resume or establish a new connection. + This stops all behavior for the current update installation. It avoids making any calls or replies to Sparkle's updater. + Note this does not change the idle state of update checks (i.e, -idlesOnUpdateChecks), but it will invalidate any pending update checks. + One may want to check if there is a pending update check (i.e, -willInitiateNextUpdateCheck) before making this call to decide if the + user driver should be invalidated or not - This implementation will be dispatched on the main queue. - - Note this is also a part of the SUUserDriver protocol that this class implements + This is appropriate to call when the connection to the updater has been interrupted or invalidated. Note this class is still re-usable + in the case that the connection should be later resumed. */ -- (void)dismissUpdateInstallation; +- (void)invalidate; @end diff --git a/Sparkle/SUStandardUserDriver.m b/Sparkle/SUStandardUserDriver.m index 3faf08a1..cf51e849 100644 --- a/Sparkle/SUStandardUserDriver.m +++ b/Sparkle/SUStandardUserDriver.m @@ -28,6 +28,7 @@ @property (nonatomic) BOOL askedHandlingTermination; @property (nonatomic, readonly) BOOL handlesTermination; +@property (nonatomic) BOOL idlesOnUpdateChecks; @property (nonatomic) BOOL updateInProgress; @property (nonatomic) NSTimer *checkUpdateTimer; @@ -56,6 +57,7 @@ @synthesize handlesTermination = _handlesTermination; @synthesize askedHandlingTermination = _askedHandlingTermination; @synthesize delegate = _delegate; +@synthesize idlesOnUpdateChecks = _idlesOnUpdateChecks; @synthesize updateInProgress = _updateInProgress; @synthesize checkUpdateTimer = _checkUpdateTimer; @synthesize checkForUpdatesReply = _checkForUpdatesReply; @@ -83,6 +85,11 @@ #pragma mark Is Update Busy? +- (void)idleOnUpdateChecks:(BOOL)shouldIdleOnUpdateChecks +{ + self.idlesOnUpdateChecks = shouldIdleOnUpdateChecks; +} + - (void)showUpdateInProgress:(BOOL)isUpdateInProgress { dispatch_async(dispatch_get_main_queue(), ^{ @@ -114,6 +121,11 @@ return result; } +- (BOOL)willInitiateNextUpdateCheck +{ + return (self.checkUpdateTimer != nil); +} + - (void)checkForUpdates:(NSTimer *)__unused timer { if ([self isDelegateResponsibleForUpdateChecking]) { @@ -128,6 +140,8 @@ self.checkForUpdatesReply = nil; } } + + [self _invalidateUpdateCheckTimer]; } - (void)startUpdateCheckTimerWithNextTimeInterval:(NSTimeInterval)timeInterval reply:(void (^)(SUUpdateCheckTimerStatus))reply @@ -561,35 +575,56 @@ } } -#pragma mark Aborting Update +#pragma mark Aborting Everything + +- (void)_dismissUpdateInstallation +{ + // Make sure everything we call here does not dispatch async to main queue + // because we are already on the main queue (and I've been bitten in the past by this before) + + // Note: self.idlesOnUpdateChecks is intentionally not touched in case this instance is re-used + + self.updateInProgress = NO; + + [self _invalidateUpdateCheckTimer]; + + [self cancelCheckForUpdates]; + [self cancelDownload]; + + if (self.statusController) { + [self.statusController close]; + self.statusController = nil; + } + + if (self.activeUpdateAlert) { + [self.activeUpdateAlert close]; + self.activeUpdateAlert = nil; + } + + [self cancelObservingApplicationTermination]; + [self cancelObservingSystemPowerOff]; + [self cancelInstallAndRestart]; +} - (void)dismissUpdateInstallation { dispatch_async(dispatch_get_main_queue(), ^{ - // Make sure everything we call here does not dispatch async to main queue - // because we are already on the main queue (and I've been bitten in the past by this before) - - self.updateInProgress = NO; - - [self _invalidateUpdateCheckTimer]; - - [self cancelCheckForUpdates]; - [self cancelDownload]; - - if (self.statusController) { - [self.statusController close]; - self.statusController = nil; - } - - if (self.activeUpdateAlert) { - [self.activeUpdateAlert close]; - self.activeUpdateAlert = nil; - } - - [self cancelObservingApplicationTermination]; - [self cancelObservingSystemPowerOff]; - [self cancelInstallAndRestart]; + [self _dismissUpdateInstallation]; }); } +- (void)invalidate +{ + // Make sure any remote handlers will not be invoked + self.applicationTerminationHandler = nil; + self.systemPowerOffHandler = nil; + self.installUpdateHandler = nil; + self.checkForUpdatesReply = nil; + self.updateCheckStatusCompletion = nil; + self.downloadStatusCompletion = nil; + + // Dismiss the installation normally + [self _dismissUpdateInstallation]; +} + @end diff --git a/Sparkle/SUUpdater.m b/Sparkle/SUUpdater.m index c99164c4..26e08c53 100644 --- a/Sparkle/SUUpdater.m +++ b/Sparkle/SUUpdater.m @@ -203,7 +203,13 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification - (void)scheduleNextUpdateCheck { [self.userDriver invalidateUpdateCheckTimer]; - if (![self automaticallyChecksForUpdates]) return; + + if (![self automaticallyChecksForUpdates]) { + [self.userDriver idleOnUpdateChecks:YES]; + return; + } else { + [self.userDriver idleOnUpdateChecks:NO]; + } // How long has it been since last we checked for an update? NSDate *lastCheckDate = [self lastUpdateCheckDate]; diff --git a/Sparkle/SUUserDriver.h b/Sparkle/SUUserDriver.h index 29bfe914..ba69cd4a 100644 --- a/Sparkle/SUUserDriver.h +++ b/Sparkle/SUUserDriver.h @@ -41,6 +41,18 @@ NS_ASSUME_NONNULL_BEGIN */ - (void)showUpdateInProgress:(BOOL)isUpdateInProgress; +/*! + * Idle on timed updater checks. + * + * If the user driver should idle on update checks, then it shouldn't have to schedule any update checks. + * If we can idle on update checks, this means the updater just determined that automatic update checks were disabled + * + * @param shouldIdleOnUpdateChecks Indicates if the user driver should idle on updater checks + * + * This can be called from any thread. + */ +- (void)idleOnUpdateChecks:(BOOL)shouldIdleOnUpdateChecks; + /*! * Start the next scheduled update check timer. * diff --git a/TestApplication/SURemoteUpdateSettingsWindowController.m b/TestApplication/SURemoteUpdateSettingsWindowController.m index eab0e086..96f98655 100644 --- a/TestApplication/SURemoteUpdateSettingsWindowController.m +++ b/TestApplication/SURemoteUpdateSettingsWindowController.m @@ -63,48 +63,62 @@ // Try killing the TestAppHelper process to test this (might not want a debugger attached) self.connection.interruptionHandler = ^{ - NSLog(@"Connection is interrupted! Sending another message to get it back.."); + NSLog(@"Connection is interrupted..!"); dispatch_async(dispatch_get_main_queue(), ^{ if (weakSelf.isTerminating) { + NSLog(@"Terminating app.."); + // This will be dispatched on main queue again [weakSelf.userDriver terminateApplication]; } - }); - - // This method dispatches on the main queue - [weakSelf.userDriver dismissUpdateInstallation]; - - // I want this to be sent after dismissing update installation is done - dispatch_async(dispatch_get_main_queue(), ^{ - [weakSelf.connection.remoteObjectProxy startSparkle]; + + // Attempt to do work only if we haven't terminated yet + dispatch_async(dispatch_get_main_queue(), ^{ + if (!weakSelf.userDriver.willInitiateNextUpdateCheck) { + NSLog(@"Invalidating installation.."); + [weakSelf.userDriver invalidate]; + } + + // No need to do anything if we will be sending a message later anyway + // or if we won't have to send a message immediately anyway + if (!weakSelf.userDriver.willInitiateNextUpdateCheck && !weakSelf.userDriver.idlesOnUpdateChecks) { + NSLog(@"Starting up Sparkle again"); + [weakSelf.connection.remoteObjectProxy startSparkle]; + } else { + NSLog(@"Update checker is in progress or doesn't have to be; no need to panic"); + } + }); }); }; // Try testing this by using the Invalidate Connection menu item self.connection.invalidationHandler = ^{ - const uint64_t delay = 10; + const uint64_t delay = 60; NSLog(@"Connection is invalidated! Rebooting connection in %llu seconds..", delay); dispatch_async(dispatch_get_main_queue(), ^{ if (weakSelf.isTerminating) { + // This will dispatch on the main queue again [weakSelf.userDriver terminateApplication]; } - }); - - // This method dispatches on the main queue - [weakSelf.userDriver dismissUpdateInstallation]; - - dispatch_async(dispatch_get_main_queue(), ^{ - weakSelf.userDriver = nil; - weakSelf.connection = nil; - // This must be called on main queue - // Because the connection was invalidated, it might mean creating a new one will not be easy, so let's wait a bit in our example - // Note this is a "stupid" implementation. Perhaps a better one increases the time exponentially on each re-try until a certain limit is reached - // And that could be cleared out after a certain amount of idle time. Would require some thinking. - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - NSLog(@"Rebooting connection.."); - [weakSelf setUpConnection]; + // Attempt to do work only if we haven't terminated yet + dispatch_async(dispatch_get_main_queue(), ^{ + [weakSelf.userDriver invalidate]; + + weakSelf.userDriver = nil; + weakSelf.connection = nil; + + // Because the connection was invalidated, it might mean creating a new one will not be easy, so let's wait a bit in our example + // (Unless the user forces the connection to be created again in our test app, by eg, playing around with the checkboxes) + // Note this is probably a "dumb" implementation. Perhaps a better one increases the time exponentially on each re-try until a certain limit is reached + // And that could be cleared out after a certain amount of idle time. Plenty of ways to approach this.. + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + NSLog(@"Rebooting connection.."); + if (weakSelf != nil && weakSelf.connection == nil) { + [weakSelf setUpConnection]; + } + }); }); }); };