From 6adb0c3017ddc55eac26667a490e8d0d8fc74929 Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 30 Apr 2021 21:10:41 -0700 Subject: [PATCH] Use download driver for downloading release notes --- Sparkle/SPUDownloadDriver.h | 4 +- Sparkle/SPUDownloadDriver.m | 20 ++++--- Sparkle/SPUUIBasedUpdateDriver.m | 89 ++++++++++++++++++++++++++++---- 3 files changed, 94 insertions(+), 19 deletions(-) diff --git a/Sparkle/SPUDownloadDriver.h b/Sparkle/SPUDownloadDriver.h index 28c62c34..449f4151 100644 --- a/Sparkle/SPUDownloadDriver.h +++ b/Sparkle/SPUDownloadDriver.h @@ -36,9 +36,9 @@ NS_ASSUME_NONNULL_BEGIN @interface SPUDownloadDriver : NSObject -- (instancetype)initWithRequestURL:(NSURL *)requestURL host:(SUHost *)host userAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate; +- (instancetype)initWithRequestURL:(NSURL *)requestURL host:(SUHost *)host userAgent:(NSString * _Nullable)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate; -- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem secondaryUpdateItem:(SUAppcastItem * _Nullable)secondaryUpdateItem host:(SUHost *)host userAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate; +- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem secondaryUpdateItem:(SUAppcastItem * _Nullable)secondaryUpdateItem host:(SUHost *)host userAgent:(NSString * _Nullable)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate; - (instancetype)initWithHost:(SUHost *)host; diff --git a/Sparkle/SPUDownloadDriver.m b/Sparkle/SPUDownloadDriver.m index dc8cde8f..fb3ea22d 100644 --- a/Sparkle/SPUDownloadDriver.m +++ b/Sparkle/SPUDownloadDriver.m @@ -108,28 +108,34 @@ return self; } -- (instancetype)initWithRequestURL:(NSURL *)requestURL host:(SUHost *)host userAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate +- (instancetype)initWithRequestURL:(NSURL *)requestURL host:(SUHost *)host userAgent:(NSString * _Nullable)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate { self = [self initWithHost:host]; if (self != nil) { _delegate = delegate; - _inBackground = background; - _request = [NSMutableURLRequest requestWithURL:requestURL]; - [_request setValue:userAgent forHTTPHeaderField:@"User-Agent"]; - _request.networkServiceType = background ? NSURLNetworkServiceTypeBackground : NSURLNetworkServiceTypeDefault; + + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0]; + + if (userAgent != nil) { + [request setValue:(NSString * _Nonnull)userAgent forHTTPHeaderField:@"User-Agent"]; + } + + request.networkServiceType = background ? NSURLNetworkServiceTypeBackground : NSURLNetworkServiceTypeDefault; if (httpHeaders != nil) { for (NSString *key in httpHeaders) { NSString *value = [httpHeaders objectForKey:key]; - [_request setValue:value forHTTPHeaderField:key]; + [request setValue:value forHTTPHeaderField:key]; } } + + _request = request; } return self; } -- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem secondaryUpdateItem:(SUAppcastItem * _Nullable)secondaryUpdateItem host:(SUHost *)host userAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate +- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem secondaryUpdateItem:(SUAppcastItem * _Nullable)secondaryUpdateItem host:(SUHost *)host userAgent:(NSString * _Nullable)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate { self = [self initWithRequestURL:updateItem.fileURL host:host userAgent:userAgent httpHeaders:httpHeaders inBackground:background delegate:delegate]; if (self != nil) { diff --git a/Sparkle/SPUUIBasedUpdateDriver.m b/Sparkle/SPUUIBasedUpdateDriver.m index 78985580..bad7d510 100644 --- a/Sparkle/SPUUIBasedUpdateDriver.m +++ b/Sparkle/SPUUIBasedUpdateDriver.m @@ -16,10 +16,63 @@ #import "SUErrors.h" #import "SPUDownloadData.h" #import "SPUResumableUpdate.h" +#import "SPUDownloadDriver.h" #include "AppKitPrevention.h" +// Private class for downloading release notes +@interface SPUReleaseNotesDriver: NSObject + +@property (nonatomic, readonly) SPUDownloadDriver *downloadDriver; +@property (nonatomic) void (^completionHandler)(SPUDownloadData * _Nullable, NSError * _Nullable ); + +@end + +@implementation SPUReleaseNotesDriver + +@synthesize downloadDriver = _downloadDriver; +@synthesize completionHandler = _completionHandler; + +- (instancetype)initWithReleaseNotesURL:(NSURL *)releaseNotesURL host:(SUHost *)host completionHandler:(void (^)(SPUDownloadData * _Nullable, NSError * _Nullable))completionHandler +{ + self = [super init]; + if (self != nil) { + _downloadDriver = [[SPUDownloadDriver alloc] initWithRequestURL:releaseNotesURL host:host userAgent:nil httpHeaders:nil inBackground:NO delegate:self]; + _completionHandler = [completionHandler copy]; + } + return self; +} + +- (void)startDownload +{ + [self.downloadDriver downloadFile]; +} + +- (void)downloadDriverDidDownloadData:(SPUDownloadData *)downloadData +{ + if (self.completionHandler != nil) { + self.completionHandler(downloadData, nil); + self.completionHandler = nil; + } +} + +- (void)downloadDriverDidFailToDownloadFileWithError:(nonnull NSError *)error +{ + if (self.completionHandler != nil) { + self.completionHandler(nil, error); + self.completionHandler = nil; + } +} + +- (void)cleanup:(void (^)(void))cleanupHandler +{ + self.completionHandler = nil; + [self.downloadDriver cleanup:cleanupHandler]; +} + +@end + @interface SPUUIBasedUpdateDriver() @property (nonatomic, readonly) SPUCoreBasedUpdateDriver *coreDriver; @@ -29,6 +82,7 @@ @property (weak, nonatomic, readonly) id updaterDelegate; @property (nonatomic, weak, readonly) id delegate; @property (nonatomic, readonly) id userDriver; +@property (nonatomic) SPUReleaseNotesDriver *releaseNotesDriver; @property (nonatomic) BOOL resumingInstallingUpdate; @property (nonatomic) BOOL resumingDownloadedUpdate; @property (nonatomic) BOOL preventsInstallerInteraction; @@ -43,6 +97,7 @@ @synthesize userInitiated = _userInitiated; @synthesize updaterDelegate = _updaterDelegate; @synthesize userDriver = _userDriver; +@synthesize releaseNotesDriver = _releaseNotesDriver; @synthesize delegate = _delegate; @synthesize resumingInstallingUpdate = _resumingInstallingUpdate; @synthesize resumingDownloadedUpdate = _resumingDownloadedUpdate; @@ -265,16 +320,19 @@ } if (updateItem.releaseNotesURL != nil && (![updaterDelegate respondsToSelector:@selector(updaterShouldDownloadReleaseNotes:)] || [updaterDelegate updaterShouldDownloadReleaseNotes:self.updater])) { - NSURLRequest *request = [NSURLRequest requestWithURL:updateItem.releaseNotesURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]; -// id userDriver = self.userDriver; -// SPUDownloadURLWithRequest(request, ^(SPUDownloadData * _Nullable downloadData, NSError * _Nullable error) { -// if (downloadData != nil) { -// [userDriver showUpdateReleaseNotesWithDownloadData:(SPUDownloadData * _Nonnull)downloadData]; -// } else { -// [userDriver showUpdateReleaseNotesFailedToDownloadWithError:(NSError * _Nonnull)error]; -// } -// }); + __weak __typeof__(self) weakSelf = self; + self.releaseNotesDriver = [[SPUReleaseNotesDriver alloc] initWithReleaseNotesURL:updateItem.releaseNotesURL host:self.host completionHandler:^(SPUDownloadData * _Nullable downloadData, NSError * _Nullable error) { + __typeof__(self) strongSelf = weakSelf; + id userDriver = strongSelf.userDriver; + if (downloadData != nil) { + [userDriver showUpdateReleaseNotesWithDownloadData:(SPUDownloadData * _Nonnull)downloadData]; + } else { + [userDriver showUpdateReleaseNotesFailedToDownloadWithError:(NSError * _Nonnull)error]; + } + }]; + + [self.releaseNotesDriver startDownload]; } } @@ -373,7 +431,7 @@ [self.delegate coreDriverIsRequestingAbortUpdateWithError:error]; } -- (void)abortUpdateWithError:(nullable NSError *)error +- (void)_abortUpdateWithError:(nullable NSError *)error { void (^abortUpdate)(void) = ^{ [self.userDriver dismissUpdateInstallation]; @@ -415,4 +473,15 @@ } } +- (void)abortUpdateWithError:(nullable NSError *)error +{ + if (self.releaseNotesDriver != nil) { + [self.releaseNotesDriver cleanup:^{ + [self _abortUpdateWithError:error]; + }]; + } else { + [self _abortUpdateWithError:error]; + } +} + @end