diff --git a/Sparkle/SPUDownloader.m b/Sparkle/SPUDownloader.m index e863a4e7..13b57ee6 100755 --- a/Sparkle/SPUDownloader.m +++ b/Sparkle/SPUDownloader.m @@ -16,7 +16,7 @@ #include "AppKitPrevention.h" -@interface SPUDownloader () +@interface SPUDownloader () @end @@ -41,11 +41,11 @@ // Don't implement dealloc - make the client call cleanup, which is the only way to remove the reference cycle from the delegate anyway -- (void)startPersistentDownloadWithRequest:(SPUURLRequest *)request bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename +- (void)startPersistentDownloadWithRequest:(SPUURLRequest *)__unused request bundleIdentifier:(NSString *)__unused bundleIdentifier desiredFilename:(NSString *)__unused desiredFilename { } -- (void)startTemporaryDownloadWithRequest:(SPUURLRequest *)request +- (void)startTemporaryDownloadWithRequest:(SPUURLRequest *)__unused request { } @@ -69,6 +69,10 @@ } } +- (void)cancel +{ + +} -(void)downloadDidFinishWithData:(SPUDownloadData*)data { diff --git a/Sparkle/SPUDownloaderDeprecated.m b/Sparkle/SPUDownloaderDeprecated.m index 8ae29dbb..4c4ee6dc 100644 --- a/Sparkle/SPUDownloaderDeprecated.m +++ b/Sparkle/SPUDownloaderDeprecated.m @@ -174,4 +174,9 @@ [super cleanup]; } +- (void)cancel +{ + [self cleanup]; +} + @end diff --git a/Sparkle/SPUDownloaderProtocol.h b/Sparkle/SPUDownloaderProtocol.h index d844eb84..36f41fd4 100755 --- a/Sparkle/SPUDownloaderProtocol.h +++ b/Sparkle/SPUDownloaderProtocol.h @@ -23,6 +23,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)cleanup; +- (void)cancel; + @end NS_ASSUME_NONNULL_END diff --git a/Sparkle/SPUDownloaderSession.m b/Sparkle/SPUDownloaderSession.m index aae8a76e..0c1c0885 100644 --- a/Sparkle/SPUDownloaderSession.m +++ b/Sparkle/SPUDownloaderSession.m @@ -166,6 +166,11 @@ [super cleanup]; } +- (void)cancel +{ + [self cleanup]; +} + // NSURLDownload has a [downlaod:shouldDecodeSourceDataOfMIMEType:] to determine if the data should be decoded. // This does not exist for NSURLSessionDownloadTask and appears unnecessary. Data tasks will decode data, but not download tasks. diff --git a/Sparkle/SUBasicUpdateDriver.h b/Sparkle/SUBasicUpdateDriver.h index bd83c4dd..937e0f9a 100644 --- a/Sparkle/SUBasicUpdateDriver.h +++ b/Sparkle/SUBasicUpdateDriver.h @@ -10,12 +10,14 @@ #define SUBASICUPDATEDRIVER_H #import "SUUpdateDriver.h" +#import "SPUDownloader.h" +#import "SPUDownloaderDelegate.h" -@class SUAppcast, SUAppcastItem, SUHost; -@interface SUBasicUpdateDriver : SUUpdateDriver +@class SUAppcast, SUAppcastItem, SUHost, SPUDownloadData; +@interface SUBasicUpdateDriver : SUUpdateDriver @property (strong, readonly) SUAppcastItem *updateItem; -@property (strong, readonly) NSURLDownload *download; +@property (strong, readonly) SPUDownloader *download; @property (copy, readonly) NSString *downloadPath; - (void)checkForUpdatesAtURL:(NSURL *)URL host:(SUHost *)host; @@ -29,9 +31,17 @@ - (void)didNotFindUpdate; - (void)downloadUpdate; +// SPUDownloaderDelegate +- (void)downloaderDidSetDestinationName:(NSString *)destinationName temporaryDirectory:(NSString *)temporaryDirectory; +- (void)downloaderDidReceiveExpectedContentLength:(int64_t)expectedContentLength; +- (void)downloaderDidReceiveDataOfLength:(uint64_t)length; +- (void)downloaderDidFinishWithTemporaryDownloadData:(SPUDownloadData * _Nullable)downloadData; +- (void)downloaderDidFailWithError:(NSError *)error; + +/* - (void)download:(NSURLDownload *)d decideDestinationWithSuggestedFilename:(NSString *)name; - (void)downloadDidFinish:(NSURLDownload *)d; -- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error; +- (void)download:(NSURLDownload *)download didFailWithError:(NSError *)error;*/ - (void)extractUpdate; - (void)failedToApplyDeltaUpdate; diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 927f3eae..1ef605ca 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -25,10 +25,14 @@ #import "SUAppcast.h" #import "SUAppcastItem.h" +#import "SPUURLRequest.h" +#import "SPUDownloaderDeprecated.h" +#import "SPUDownloaderSession.h" + @interface SUBasicUpdateDriver () @property (strong) SUAppcastItem *updateItem; -@property (strong) NSURLDownload *download; +@property (strong) SPUDownloader *download; @property (copy) NSString *downloadPath; @property (strong) SUAppcastItem *nonDeltaUpdateItem; @@ -256,6 +260,9 @@ - (void)downloadUpdate { + NSString *bundleIdentifier = self.host.bundle.bundleIdentifier; + assert(bundleIdentifier != nil); + // Clear cache directory so that downloads can't possibly accumulate inside NSString *appCachePath = [self appCachePath]; if ([[NSFileManager defaultManager] fileExistsAtPath:appCachePath]) { @@ -275,9 +282,64 @@ willDownloadUpdate:self.updateItem withRequest:request]; } - self.download = [[NSURLDownload alloc] initWithRequest:request delegate:self]; + // TODO: SPUDownloaderDeprecated vs SPUDownloaderSession + self.download = [[SPUDownloaderDeprecated alloc] initWithDelegate:self]; + SPUURLRequest *urlRequest = [SPUURLRequest URLRequestWithRequest:request]; + NSString *desiredFilename = [NSString stringWithFormat:@"%@ %@", [self.host name], [self.updateItem versionString]]; + [self.download startPersistentDownloadWithRequest:urlRequest bundleIdentifier:bundleIdentifier desiredFilename:desiredFilename]; } + +- (void)downloaderDidSetDestinationName:(NSString *)destinationName temporaryDirectory:(NSString *)temporaryDirectory +{ + self.tempDir = temporaryDirectory; + self.downloadPath = destinationName; // TODO: does this need to be a full path? +} + +- (void)downloaderDidReceiveExpectedContentLength:(int64_t)__unused expectedContentLength +{ + // don't need to do anything here as there's no GUI with this driver (there can be with child classes) +} + +- (void)downloaderDidReceiveDataOfLength:(uint64_t)__unused length +{ + // don't need do anything here as there's no GUI with this driver (there can be with child classes) +} + +- (void)downloaderDidFinishWithTemporaryDownloadData:(SPUDownloadData * _Nullable)__unused downloadData +{ + // finished. downloadData should be nil as this was a permanent download + assert(self.updateItem); + + [self extractUpdate]; +} + +- (void)downloaderDidFailWithError:(NSError *)error +{ + NSURL *failingUrl = [error.userInfo objectForKey:NSURLErrorFailingURLErrorKey]; + if (!failingUrl) { + failingUrl = [self.updateItem fileURL]; + } + + id updater = self.updater; + + if ([[updater delegate] respondsToSelector:@selector(updater:failedToDownloadUpdate:error:)]) { + [[updater delegate] updater:self.updater + failedToDownloadUpdate:self.updateItem + error:error]; + } + + NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ + NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while downloading the update. Please try again later.", nil), + NSUnderlyingErrorKey: error, + }]; + if (failingUrl) { + [userInfo setObject:failingUrl forKey:NSURLErrorFailingURLErrorKey]; + } + + [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:userInfo]]; +} +/* - (void)download:(NSURLDownload *)__unused d decideDestinationWithSuggestedFilename:(NSString *)name { NSString *downloadFileName = [NSString stringWithFormat:@"%@ %@", [self.host name], [self.updateItem versionString]]; @@ -303,47 +365,7 @@ self.downloadPath = [self.tempDir stringByAppendingPathComponent:name]; [self.download setDestination:self.downloadPath allowOverwrite:YES]; } - -- (void)downloadDidFinish:(NSURLDownload *)__unused d -{ - assert(self.updateItem); - - [self extractUpdate]; -} - -- (void)download:(NSURLDownload *)__unused download didFailWithError:(NSError *)error -{ - NSURL *failingUrl = [error.userInfo objectForKey:NSURLErrorFailingURLErrorKey]; - if (!failingUrl) { - failingUrl = [self.updateItem fileURL]; - } - - id updater = self.updater; - - if ([[updater delegate] respondsToSelector:@selector(updater:failedToDownloadUpdate:error:)]) { - [[updater delegate] updater:self.updater - failedToDownloadUpdate:self.updateItem - error:error]; - } - - NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ - NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while downloading the update. Please try again later.", nil), - NSUnderlyingErrorKey: error, - }]; - if (failingUrl) { - [userInfo setObject:failingUrl forKey:NSURLErrorFailingURLErrorKey]; - } - - [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:userInfo]]; -} - -- (BOOL)download:(NSURLDownload *)__unused download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType -{ - // We don't want the download system to extract our gzips. - // Note that we use a substring matching here instead of direct comparison because the docs say "application/gzip" but the system *uses* "application/x-gzip". This is a documentation bug. - return ([encodingType rangeOfString:@"gzip"].location == NSNotFound); -} - +*/ - (void)extractUpdate { id updater = self.updater; diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 0400e5aa..2aa592bd 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -169,9 +169,8 @@ [super downloadUpdate]; } -- (void)download:(NSURLDownload *)__unused download didReceiveResponse:(NSURLResponse *)response +- (void)downloaderDidReceiveExpectedContentLength:(int64_t) expectedContentLength { - long long expectedContentLength = [response expectedContentLength]; [self.statusController setMaxProgressValue:expectedContentLength > 0 ? expectedContentLength : self.updateItem.contentLength]; } @@ -205,7 +204,8 @@ #pragma clang diagnostic pop } -- (void)download:(NSURLDownload *)__unused download didReceiveDataOfLength:(NSUInteger)length + +- (void)downloaderDidReceiveDataOfLength:(uint64_t) length { double newProgressValue = [self.statusController progressValue] + (double)length;