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:..
This commit is contained in:
Zorg authored and GitHub committed 2021-10-03 12:58:00 -07:00
1 parent a9d5a42768
commit 6f8e8e4789
7 files changed
+94 -42

No files matched your search

+4
View File
@@ -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];
+4
View File
@@ -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];
+11
View File
@@ -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 <SPUUserDriver>)userDriver updaterDelegate:(nullable id <SPUUpdaterDelegate>)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
+2
View File
@@ -21,6 +21,8 @@ typedef void (^SPUUpdateDriverCompletion)(BOOL shouldShowUpdateImmediately, id<S
- (void)setCompletionHandler:(SPUUpdateDriverCompletion)completionBlock;
- (void)setUpdateShownHandler:(void (^)(void))updateShownHandler;
- (void)checkForUpdatesAtAppcastURL:(NSURL *)appcastURL withUserAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders;
- (void)resumeInstallingUpdate;
+2
View File
@@ -144,6 +144,8 @@ SU_EXPORT @interface SPUUpdater : NSObject
This property is suitable to use for menu item validation for seeing if `-checkForUpdates` can be invoked.
This property is also KVO-compliant.
Note this property does not reflect whether or not an update session is in progress. Please see `sessionInProgress` property instead.
*/
@property (nonatomic, readonly) BOOL canCheckForUpdates;
+54 -41
View File
@@ -58,6 +58,7 @@ NSString *const SUUpdaterAppcastNotificationKey = @"SUUpdaterAppCastNotification
@property (nonatomic) BOOL startedUpdater;
@property (nonatomic, nullable) id<SPUResumableUpdate> 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 <SPUUpdateDriver> 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 <SPUUpdateDriver> )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
+17 -1
View File
@@ -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 <SPUUserDriver>)userDriver updaterDelegate:(nullable id <SPUUpdaterDelegate>)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