diff --git a/Sparkle/SPUBasicUpdateDriver.h b/Sparkle/SPUBasicUpdateDriver.h index 8598024f..107dd020 100644 --- a/Sparkle/SPUBasicUpdateDriver.h +++ b/Sparkle/SPUBasicUpdateDriver.h @@ -38,7 +38,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)resumeUpdate:(id)resumableUpdate completion:(SPUUpdateDriverCompletion)completionBlock; -@property (nullable, nonatomic, readonly) SUAppcastItem *nonDeltaUpdateItem; +@property (nullable, nonatomic, readonly) SUAppcastItem *secondaryUpdateItem; - (void)abortUpdateAndShowNextUpdateImmediately:(BOOL)shouldSignalShowingUpdate resumableUpdate:(id _Nullable)resumableUpdate error:(nullable NSError *)error; diff --git a/Sparkle/SPUBasicUpdateDriver.m b/Sparkle/SPUBasicUpdateDriver.m index 49929c1a..2a3be1d4 100644 --- a/Sparkle/SPUBasicUpdateDriver.m +++ b/Sparkle/SPUBasicUpdateDriver.m @@ -112,9 +112,9 @@ [self notifyResumableUpdateItem:resumableUpdate.updateItem systemDomain:nil]; } -- (SUAppcastItem *)nonDeltaUpdateItem +- (SUAppcastItem *)secondaryUpdateItem { - return self.appcastDriver.nonDeltaUpdateItem; + return self.appcastDriver.secondaryUpdateItem; } - (void)didFailToFetchAppcastWithError:(NSError *)error diff --git a/Sparkle/SPUCoreBasedUpdateDriver.m b/Sparkle/SPUCoreBasedUpdateDriver.m index a769fe08..4b5c435f 100644 --- a/Sparkle/SPUCoreBasedUpdateDriver.m +++ b/Sparkle/SPUCoreBasedUpdateDriver.m @@ -381,16 +381,16 @@ - (void)installerDidFailToApplyDeltaUpdate { - SUAppcastItem *nonDeltaUpdateItem = self.basicDriver.nonDeltaUpdateItem; - assert(nonDeltaUpdateItem != nil); + SUAppcastItem *secondaryUpdateItem = self.basicDriver.secondaryUpdateItem; + assert(secondaryUpdateItem != nil); BOOL backgroundDownload = self.downloadDriver.inBackground; [self clearDownloadedUpdate]; // Fall back to the non-delta update. Note that we don't want to trigger another update was found event. - self.updateItem = nonDeltaUpdateItem; - [self downloadUpdateFromAppcastItem:nonDeltaUpdateItem inBackground:backgroundDownload]; + self.updateItem = secondaryUpdateItem; + [self downloadUpdateFromAppcastItem:secondaryUpdateItem inBackground:backgroundDownload]; } - (void)abortUpdateAndShowNextUpdateImmediately:(BOOL)shouldShowUpdateImmediately error:(nullable NSError *)error diff --git a/Sparkle/SUAppcast.h b/Sparkle/SUAppcast.h index 5ae0f0fa..7030dff3 100644 --- a/Sparkle/SUAppcast.h +++ b/Sparkle/SUAppcast.h @@ -28,10 +28,9 @@ SU_EXPORT @interface SUAppcast : NSObject @property (copy, nullable) NSDictionary *httpHeaders; - (void)fetchAppcastFromURL:(NSURL *)url inBackground:(BOOL)bg completionBlock:(void (^)(NSError *_Nullable))err; -- (SUAppcast *)copyWithoutDeltaUpdates; - (SUAppcast *)copyByFilteringItems:(BOOL (^)(SUAppcastItem *))filterBlock; -@property (readonly, copy, nullable) NSArray *items; +@property (readonly, copy, nullable) NSArray *items; @end NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index 640960e8..467854a7 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -47,7 +47,7 @@ @interface SUAppcast () @property (strong) void (^completionBlock)(NSError *); -@property (copy) NSArray *items; +@property (copy) NSArray *items; - (void)reportError:(NSError *)error; - (NSXMLNode *)bestNodeInNodes:(NSArray *)nodes; @end @@ -306,12 +306,6 @@ return [nodes objectAtIndex:i]; } -- (SUAppcast *)copyWithoutDeltaUpdates { - return [self copyByFilteringItems:^(SUAppcastItem *item) { - return (BOOL)![item isDeltaUpdate]; - }]; -} - - (SUAppcast *)copyByFilteringItems:(BOOL (^)(SUAppcastItem *))filterBlock { SUAppcast *other = [SUAppcast new]; diff --git a/Sparkle/SUAppcastDriver.h b/Sparkle/SUAppcastDriver.h index b5b2dbf2..d9a6a3cf 100644 --- a/Sparkle/SUAppcastDriver.h +++ b/Sparkle/SUAppcastDriver.h @@ -28,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)loadAppcastFromURL:(NSURL *)appcastURL userAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background includesSkippedUpdates:(BOOL)includesSkippedUpdates; -@property (nullable, nonatomic, readonly) SUAppcastItem *nonDeltaUpdateItem; +@property (nullable, nonatomic, readonly) SUAppcastItem *secondaryUpdateItem; @end diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index 24a0e692..9dfb4a01 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -16,6 +16,7 @@ #import "SUHost.h" #import "SUConstants.h" #import "SUPhasedUpdateGroupInfo.h" +#import "SULog.h" #include "AppKitPrevention.h" @@ -26,7 +27,7 @@ @property (nonatomic, copy) NSString *userAgent; @property (nullable, nonatomic, readonly, weak) id updater; @property (nullable, nonatomic, readonly, weak) id updaterDelegate; -@property (nullable, nonatomic) SUAppcastItem *nonDeltaUpdateItem; +@property (nullable, nonatomic) SUAppcastItem *secondaryUpdateItem; @property (nullable, nonatomic, readonly, weak) id delegate; @end @@ -37,7 +38,7 @@ @synthesize userAgent = _userAgent; @synthesize updater = _updater; @synthesize updaterDelegate = _updaterDelegate; -@synthesize nonDeltaUpdateItem = _nonDeltaUpdateItem; +@synthesize secondaryUpdateItem = _secondaryUpdateItem; @synthesize delegate = _delegate; - (instancetype)initWithHost:(SUHost *)host updater:(id)updater updaterDelegate:(id )updaterDelegate delegate:(id )delegate @@ -68,6 +69,24 @@ }]; } +- (SUAppcastItem * _Nullable)preferredUpdateForRegularAppcastItem:(SUAppcastItem * _Nullable)regularItem secondaryUpdate:(SUAppcastItem * __autoreleasing _Nullable *)secondaryUpdate +{ + if (regularItem == nil) { + return nil; + } + + SUAppcastItem *deltaItem = [[self class] deltaUpdateFromAppcastItem:regularItem hostVersion:self.host.version]; + + if (deltaItem != nil) { + if (secondaryUpdate != NULL) { + *secondaryUpdate = regularItem; + } + return deltaItem; + } else { + return regularItem; + } +} + - (void)appcastDidFinishLoading:(SUAppcast *)loadedAppcast inBackground:(BOOL)background includesSkippedUpdates:(BOOL)includesSkippedUpdates { [self.delegate didFinishLoadingAppcast:loadedAppcast]; @@ -79,35 +98,33 @@ SUAppcast *supportedAppcast = [[self class] filterSupportedAppcast:loadedAppcast phasedUpdateGroup:phasedUpdateGroup]; SUAppcastItem *item = nil; - SUAppcastItem *nonDeltaUpdateItem = nil; + SUAppcastItem *secondaryItem = nil; - // Now we have to find the best valid update in the appcast. + // Find the best valid update in the appcast by asking the delegate if ([self.updaterDelegate respondsToSelector:@selector((bestValidUpdateInAppcast:forUpdater:))]) { - item = [self.updaterDelegate bestValidUpdateInAppcast:supportedAppcast forUpdater:(id _Nonnull)self.updater]; + SUAppcastItem *regularItem = [self.updaterDelegate bestValidUpdateInAppcast:supportedAppcast forUpdater:(id _Nonnull)self.updater]; + + assert(!regularItem.deltaUpdate); + if (regularItem.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"); + } else { + item = [self preferredUpdateForRegularAppcastItem:regularItem secondaryUpdate:&secondaryItem]; + } } - if (item != nil) + // Take care of finding best appcast item ourselves if delegate does not + if (item == nil) { - // Does the delegate want to handle it? - if ([item isDeltaUpdate]) { - nonDeltaUpdateItem = [self.updaterDelegate bestValidUpdateInAppcast:[supportedAppcast copyWithoutDeltaUpdates] forUpdater:(id _Nonnull)self.updater]; - } - } - else // If not, we'll take care of it ourselves. - { - // Find the best supported update - SUAppcastItem *deltaUpdateItem = nil; - item = [[self class] bestItemFromAppcastItems:supportedAppcast.items getDeltaItem:&deltaUpdateItem withHostVersion:self.host.version comparator:[self versionComparator]]; + SUAppcastItem *regularItem = [[self class] bestItemFromAppcastItems:supportedAppcast.items comparator:[self versionComparator]]; - if (item && deltaUpdateItem) { - nonDeltaUpdateItem = item; - item = deltaUpdateItem; - } + item = [self preferredUpdateForRegularAppcastItem:regularItem secondaryUpdate:&secondaryItem]; } if ([self itemContainsValidUpdate:item inBackground:background includesSkippedUpdates:includesSkippedUpdates]) { - self.nonDeltaUpdateItem = nonDeltaUpdateItem; + self.secondaryUpdateItem = secondaryItem; [self.delegate didFindValidUpdateWithAppcastItem:item preventsAutoupdate:[self itemPreventsAutoupdate:item]]; } else { NSComparisonResult hostToLatestAppcastItemComparisonResult = (item != nil) ? [[self versionComparator] compareVersion:[self.host version] toVersion:[item versionString]] : 0; @@ -125,9 +142,12 @@ }]; } -// This method is used by unit tests -// This method should not do *any* filtering, only version comparing -+ (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem * __autoreleasing *)deltaItem withHostVersion:(NSString *)hostVersion comparator:(id)comparator ++ (SUAppcastItem * _Nullable)deltaUpdateFromAppcastItem:(SUAppcastItem *)appcastItem hostVersion:(NSString *)hostVersion +{ + return appcastItem.deltaUpdates[hostVersion]; +} + ++ (SUAppcastItem * _Nullable)bestItemFromAppcastItems:(NSArray *)appcastItems comparator:(id)comparator { SUAppcastItem *item = nil; for(SUAppcastItem *candidate in appcastItems) { @@ -135,14 +155,17 @@ item = candidate; } } - - if (item && deltaItem) { - SUAppcastItem *deltaUpdateItem = [item deltaUpdates][hostVersion]; - if (deltaUpdateItem) { - *deltaItem = deltaUpdateItem; - } + return item; +} + +// This method is used by unit tests +// This method should not do *any* filtering, only version comparing ++ (SUAppcastItem *)bestItemFromAppcastItems:(NSArray *)appcastItems getDeltaItem:(SUAppcastItem * __autoreleasing *)deltaItem withHostVersion:(NSString *)hostVersion comparator:(id)comparator +{ + SUAppcastItem *item = [self bestItemFromAppcastItems:appcastItems comparator:comparator]; + if (item != nil && deltaItem != NULL) { + *deltaItem = [self deltaUpdateFromAppcastItem:item hostVersion:hostVersion]; } - return item; }