From b21647bcef4ffc3417376c249cbc82a308581ce8 Mon Sep 17 00:00:00 2001 From: Mayur Pawashe Date: Mon, 5 Jul 2021 10:01:52 -0700 Subject: [PATCH] Allow delegate to pick no valid items (#1880) * Allow delegate to pick no valid items We now allow returning SUAppcastItem.emptyAppcastItem in -bestValidUpdateInAppcast:forUpdater: to indicate no update should be picked. We preserve returning nil to indicate Sparkle handles the best appcast selection instead. People have expressed needs for both of these decisions (and deprecated SUUpdater relies on the nil behavior). --- Sparkle/SPUUpdaterDelegate.h | 3 ++- Sparkle/SUAppcastDriver.m | 28 ++++++++++++++++++++-------- Sparkle/SUAppcastItem.h | 2 ++ Sparkle/SUAppcastItem.m | 10 ++++++++++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Sparkle/SPUUpdaterDelegate.h b/Sparkle/SPUUpdaterDelegate.h index 28aed683..9d456f40 100644 --- a/Sparkle/SPUUpdaterDelegate.h +++ b/Sparkle/SPUUpdaterDelegate.h @@ -209,7 +209,8 @@ typedef NS_ENUM(NSInteger, SPUUpdateCheck) \param appcast The appcast that was downloaded from the remote server. \param updater The updater instance. - \return The best valid appcast item, or nil if you don't want to be delegated this task. + \return The best valid appcast item. Return SUAppcastItem.emptyAppcastItem if no appcast item is valid. Return nil if you don't want to be delegated this task and + want to let Sparkle handle picking the best valid update. */ - (nullable SUAppcastItem *)bestValidUpdateInAppcast:(SUAppcast *)appcast forUpdater:(SPUUpdater *)updater; diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index d7f125e4..675a031d 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -129,25 +129,37 @@ // Find the best valid update in the appcast by asking the delegate // Don't ask the delegate if the appcast has no items though SUAppcastItem *regularItemFromDelegate; + BOOL delegateOptedOutOfSelection; if (appcast.items.count > 0 && [self.updaterDelegate respondsToSelector:@selector((bestValidUpdateInAppcast:forUpdater:))]) { SUAppcastItem *candidateItem = [self.updaterDelegate bestValidUpdateInAppcast:appcast forUpdater:(id _Nonnull)self.updater]; - assert(!candidateItem.deltaUpdate); - if (candidateItem.deltaUpdate) { - // Client would have to go out of their way to examine the .deltaUpdates to return one - // This is very unlikely, and we need them to give us a regular update item back - SULog(SULogLevelError, @"Error: -bestValidUpdateInAppcast:forUpdater: cannot return a delta update item"); + if (candidateItem == SUAppcastItem.emptyAppcastItem) { regularItemFromDelegate = nil; - } else { - regularItemFromDelegate = candidateItem; + delegateOptedOutOfSelection = YES; + } else if (candidateItem == nil) { + regularItemFromDelegate = nil; + delegateOptedOutOfSelection = NO; + } else if (candidateItem != nil) { + assert(!candidateItem.deltaUpdate); + if (candidateItem.deltaUpdate) { + // Client would have to go out of their way to examine the .deltaUpdates to return one + // We need them to give us a regular update item back instead.. + SULog(SULogLevelError, @"Error: -bestValidUpdateInAppcast:forUpdater: cannot return a delta update item"); + regularItemFromDelegate = nil; + } else { + regularItemFromDelegate = candidateItem; + } + + delegateOptedOutOfSelection = NO; } } else { regularItemFromDelegate = nil; + delegateOptedOutOfSelection = NO; } // Take care of finding best appcast item ourselves if delegate does not SUAppcastItem *regularItem; - if (regularItemFromDelegate == nil) { + if (regularItemFromDelegate == nil && !delegateOptedOutOfSelection) { regularItem = [[self class] bestItemFromAppcastItems:appcast.items comparator:versionComparator]; } else { regularItem = regularItemFromDelegate; diff --git a/Sparkle/SUAppcastItem.h b/Sparkle/SUAppcastItem.h index 95b2dda1..e0e3b205 100644 --- a/Sparkle/SUAppcastItem.h +++ b/Sparkle/SUAppcastItem.h @@ -68,6 +68,8 @@ SU_EXPORT @interface SUAppcastItem : NSObject - (instancetype)init NS_UNAVAILABLE; ++ (instancetype)emptyAppcastItem; + // Deprecated initializers - (nullable instancetype)initWithDictionary:(NSDictionary *)dict __deprecated_msg("Properties that depend on the system or application version are not supported when used with this initializer. The designated initializer is available in SUAppcastItem+Private.h. Please first explore other APIs or contact us to describe your use case."); - (nullable instancetype)initWithDictionary:(NSDictionary *)dict failureReason:(NSString * _Nullable __autoreleasing *_Nullable)error __deprecated_msg("Properties that depend on the system or application version are not supported when used with this initializer. The designated initializer is available in SUAppcastItem+Private.h. Please first explore other APIs or contact us to describe your use case."); diff --git a/Sparkle/SUAppcastItem.m b/Sparkle/SUAppcastItem.m index 3fa305a4..fdb427d6 100644 --- a/Sparkle/SUAppcastItem.m +++ b/Sparkle/SUAppcastItem.m @@ -285,6 +285,16 @@ static NSString *SUAppcastItemStateKey = @"SUAppcastItemState"; } } ++ (instancetype)emptyAppcastItem +{ + static SUAppcastItem *emptyAppcastItem; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + emptyAppcastItem = [[SUAppcastItem alloc] init]; + }); + return emptyAppcastItem; +} + // Initializer used for making delta items - (nullable instancetype)initWithDictionary:(NSDictionary *)dict relativeToURL:(NSURL * _Nullable)appcastURL state:(SPUAppcastItemState * _Nullable)state {