diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index c071001f..d36d7bb6 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -333,7 +333,6 @@ 72A5D5B71D6929F80009E5AC /* SPUInstallationInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = 7267E5B51D3D8AEE00D1BF90 /* SPUInstallationInfo.m */; }; 72A5D5B91D6929F80009E5AC /* SUAppcast.m in Sources */ = {isa = PBXBuildFile; fileRef = 61B5FB9509C4F04600B25A18 /* SUAppcast.m */; }; 72A5D5BA1D6929F80009E5AC /* SUAppcastItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 61B5FC5409C5182000B25A18 /* SUAppcastItem.m */; }; - 72A5D5BB1D6929F80009E5AC /* SPUURLDownload.m in Sources */ = {isa = PBXBuildFile; fileRef = 72B09CE61CE6BCBD0052EF9E /* SPUURLDownload.m */; }; 72A5D5BC1D6929F80009E5AC /* SPUURLRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = 7293A1AD1CEE933800B957A7 /* SPUURLRequest.m */; }; 72A5D5BD1D6929F80009E5AC /* SPUDownloadData.m in Sources */ = {isa = PBXBuildFile; fileRef = 72F9EC431D5E9ED8004AC8B6 /* SPUDownloadData.m */; }; 72A5D5BE1D6929F80009E5AC /* SUStandardVersionComparator.m in Sources */ = {isa = PBXBuildFile; fileRef = 61A225A30D1C4AC000430CCD /* SUStandardVersionComparator.m */; }; @@ -388,8 +387,6 @@ 72AC6B2A1B9AAF3A00F62325 /* SparkleTestCodeSignApp.tar.bz2 in Resources */ = {isa = PBXBuildFile; fileRef = 72AC6B291B9AAF3A00F62325 /* SparkleTestCodeSignApp.tar.bz2 */; }; 72AC6B2C1B9AB0EE00F62325 /* SparkleTestCodeSignApp.tar.xz in Resources */ = {isa = PBXBuildFile; fileRef = 72AC6B2B1B9AB0EE00F62325 /* SparkleTestCodeSignApp.tar.xz */; }; 72AC6B2E1B9B218C00F62325 /* SparkleTestCodeSignApp.dmg in Resources */ = {isa = PBXBuildFile; fileRef = 72AC6B2D1B9B218C00F62325 /* SparkleTestCodeSignApp.dmg */; }; - 72B09CE71CE6BCBD0052EF9E /* SPUURLDownload.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B09CE51CE6BCBD0052EF9E /* SPUURLDownload.h */; }; - 72B09CE81CE6BCBD0052EF9E /* SPUURLDownload.m in Sources */ = {isa = PBXBuildFile; fileRef = 72B09CE61CE6BCBD0052EF9E /* SPUURLDownload.m */; }; 72B3DEC91E23472200457642 /* SPUDownloadedUpdate.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B3DEC71E23472200457642 /* SPUDownloadedUpdate.h */; }; 72B3DECA1E23472200457642 /* SPUDownloadedUpdate.m in Sources */ = {isa = PBXBuildFile; fileRef = 72B3DEC81E23472200457642 /* SPUDownloadedUpdate.m */; }; 72B3DECD1E23479000457642 /* SPUInformationalUpdate.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B3DECB1E23479000457642 /* SPUInformationalUpdate.h */; }; @@ -1296,8 +1293,6 @@ 72AC6B2B1B9AB0EE00F62325 /* SparkleTestCodeSignApp.tar.xz */ = {isa = PBXFileReference; lastKnownFileType = file; path = SparkleTestCodeSignApp.tar.xz; sourceTree = ""; }; 72AC6B2D1B9B218C00F62325 /* SparkleTestCodeSignApp.dmg */ = {isa = PBXFileReference; lastKnownFileType = file; path = SparkleTestCodeSignApp.dmg; sourceTree = ""; }; 72AFC6121B9A944200F6B565 /* Sparkle Unit Tests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Sparkle Unit Tests-Bridging-Header.h"; sourceTree = ""; }; - 72B09CE51CE6BCBD0052EF9E /* SPUURLDownload.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPUURLDownload.h; sourceTree = ""; }; - 72B09CE61CE6BCBD0052EF9E /* SPUURLDownload.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPUURLDownload.m; sourceTree = ""; }; 72B09CE91CEA18900052EF9E /* bscommon.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = bscommon.c; sourceTree = ""; }; 72B09CEA1CEA18900052EF9E /* bscommon.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = bscommon.h; sourceTree = ""; }; 72B398D21D3D879300EE297F /* Autoupdate */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Autoupdate; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -1858,8 +1853,6 @@ 72F9EC421D5E9ED8004AC8B6 /* SPUDownloadData.h */, 72F9EC431D5E9ED8004AC8B6 /* SPUDownloadData.m */, 7214B8851D45AD9A00CB5CED /* SPUInstallationType.h */, - 72B09CE51CE6BCBD0052EF9E /* SPUURLDownload.h */, - 72B09CE61CE6BCBD0052EF9E /* SPUURLDownload.m */, 7293A1AC1CEE933800B957A7 /* SPUURLRequest.h */, 7293A1AD1CEE933800B957A7 /* SPUURLRequest.m */, 61B5FB9409C4F04600B25A18 /* SUAppcast.h */, @@ -2438,7 +2431,6 @@ 72F9EC481D5EA904004AC8B6 /* SPUUpdaterDelegate.h in Headers */, 726E078D1CA891E9001A286B /* SPUUpdaterSettings.h in Headers */, 72F9EC3F1D5E823F004AC8B6 /* SPUUpdaterTimer.h in Headers */, - 72B09CE71CE6BCBD0052EF9E /* SPUURLDownload.h in Headers */, 723ABFC4259D4CB300BDB4FA /* SUWKWebView.h in Headers */, 7293A1AE1CEE933800B957A7 /* SPUURLRequest.h in Headers */, 725CB9571C7120410064365A /* SPUUserDriver.h in Headers */, @@ -3459,7 +3451,6 @@ 72A5D5D81D6929F80009E5AC /* SPUUpdaterCycle.m in Sources */, 72A5D5D51D6929F80009E5AC /* SPUUpdaterSettings.m in Sources */, 72A5D5D91D6929F90009E5AC /* SPUUpdaterTimer.m in Sources */, - 72A5D5BB1D6929F80009E5AC /* SPUURLDownload.m in Sources */, 72A5D5BC1D6929F80009E5AC /* SPUURLRequest.m in Sources */, 72A5D5C81D6929F80009E5AC /* SPUUserInitiatedUpdateDriver.m in Sources */, 72A5D5AD1D6929F80009E5AC /* SPUXPCServiceInfo.m in Sources */, @@ -3570,7 +3561,6 @@ 726E078E1CA891E9001A286B /* SPUUpdaterSettings.m in Sources */, 721D588F25BE59F900D23BEA /* SUPhasedUpdateGroupInfo.m in Sources */, 72F9EC401D5E823F004AC8B6 /* SPUUpdaterTimer.m in Sources */, - 72B09CE81CE6BCBD0052EF9E /* SPUURLDownload.m in Sources */, 7293A1AF1CEE933800B957A7 /* SPUURLRequest.m in Sources */, 72B767DF1C9CDB9700A07552 /* SPUUserInitiatedUpdateDriver.m in Sources */, 72F94F591CC44DE1002DEE68 /* SPUXPCServiceInfo.m in Sources */, diff --git a/Sparkle/SPUBasicUpdateDriver.m b/Sparkle/SPUBasicUpdateDriver.m index c6427f1c..bf4ec6cc 100644 --- a/Sparkle/SPUBasicUpdateDriver.m +++ b/Sparkle/SPUBasicUpdateDriver.m @@ -203,21 +203,23 @@ { self.aborted = YES; - if (error != nil) { - if (error.code != SUNoUpdateError && error.code != SUInstallationCanceledError && error.code != SUInstallationAuthorizeLaterError) { // Let's not bother logging this. - SULogError(error); + [self.appcastDriver cleanup:^{ + if (error != nil) { + if (error.code != SUNoUpdateError && error.code != SUInstallationCanceledError && error.code != SUInstallationAuthorizeLaterError) { // Let's not bother logging this. + SULogError(error); + } + + // Notify host app that updater has aborted + if ([self.updaterDelegate respondsToSelector:@selector((updater:didAbortWithError:))]) { + [self.updaterDelegate updater:self.updater didAbortWithError:(NSError * _Nonnull)error]; + } } - // Notify host app that updater has aborted - if ([self.updaterDelegate respondsToSelector:@selector((updater:didAbortWithError:))]) { - [self.updaterDelegate updater:self.updater didAbortWithError:(NSError * _Nonnull)error]; + if (self.completionBlock != nil) { + self.completionBlock(shouldShowUpdateImmediately, resumableUpdate); + self.completionBlock = nil; } - } - - if (self.completionBlock != nil) { - self.completionBlock(shouldShowUpdateImmediately, resumableUpdate); - self.completionBlock = nil; - } + }]; } @end diff --git a/Sparkle/SPUCoreBasedUpdateDriver.m b/Sparkle/SPUCoreBasedUpdateDriver.m index b095524f..973f4245 100644 --- a/Sparkle/SPUCoreBasedUpdateDriver.m +++ b/Sparkle/SPUCoreBasedUpdateDriver.m @@ -184,7 +184,7 @@ withRequest:self.downloadDriver.request]; } - [self.downloadDriver downloadUpdate]; + [self.downloadDriver downloadFile]; } - (void)downloadDriverWillBeginDownload @@ -286,7 +286,7 @@ }]; } -- (void)downloadDriverDidFailToDownloadUpdateWithError:(NSError *)error +- (void)downloadDriverDidFailToDownloadFileWithError:(NSError *)error { if ([self.updaterDelegate respondsToSelector:@selector((updater:failedToDownloadUpdate:error:))]) { NSError *errorToReport = [error.userInfo objectForKey:NSUnderlyingErrorKey]; diff --git a/Sparkle/SPUDownloadDriver.h b/Sparkle/SPUDownloadDriver.h index 14934806..28c62c34 100644 --- a/Sparkle/SPUDownloadDriver.h +++ b/Sparkle/SPUDownloadDriver.h @@ -10,29 +10,39 @@ NS_ASSUME_NONNULL_BEGIN -@class SUAppcastItem, SUHost, SPUDownloadedUpdate; +@class SUAppcastItem, SUHost, SPUDownloadedUpdate, SPUDownloadData; @protocol SPUDownloadDriverDelegate +- (void)downloadDriverDidFailToDownloadFileWithError:(NSError *)error; + +@optional + - (void)downloadDriverWillBeginDownload; -- (void)downloadDriverDidReceiveExpectedContentLength:(uint64_t)expectedContentLength; - -- (void)downloadDriverDidReceiveDataOfLength:(uint64_t)length; - +// For persitent update downloads - (void)downloadDriverDidDownloadUpdate:(SPUDownloadedUpdate *)downloadedUpdate; -- (void)downloadDriverDidFailToDownloadUpdateWithError:(NSError *)error; +// For temporary downloads +- (void)downloadDriverDidDownloadData:(SPUDownloadData *)downloadData; + +// Only for persistent downloads +- (void)downloadDriverDidReceiveExpectedContentLength:(uint64_t)expectedContentLength; + +// Only for persistent downloads +- (void)downloadDriverDidReceiveDataOfLength:(uint64_t)length; @end @interface SPUDownloadDriver : NSObject +- (instancetype)initWithRequestURL:(NSURL *)requestURL 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 *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate; - (instancetype)initWithHost:(SUHost *)host; -- (void)downloadUpdate; +- (void)downloadFile; - (void)removeDownloadedUpdate:(SPUDownloadedUpdate *)downloadedUpdate; diff --git a/Sparkle/SPUDownloadDriver.m b/Sparkle/SPUDownloadDriver.m index 7b7c3c03..dc8cde8f 100644 --- a/Sparkle/SPUDownloadDriver.m +++ b/Sparkle/SPUDownloadDriver.m @@ -97,7 +97,7 @@ NSError *downloadError = [NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:userInfo]; - [strongSelf.delegate downloadDriverDidFailToDownloadUpdateWithError:downloadError]; + [strongSelf.delegate downloadDriverDidFailToDownloadFileWithError:downloadError]; } }); }; @@ -108,20 +108,18 @@ 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)initWithRequestURL:(NSURL *)requestURL host:(SUHost *)host userAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate { self = [self initWithHost:host]; if (self != nil) { - _updateItem = updateItem; - _secondaryUpdateItem = secondaryUpdateItem; _delegate = delegate; _inBackground = background; - _request = [NSMutableURLRequest requestWithURL:updateItem.fileURL]; + _request = [NSMutableURLRequest requestWithURL:requestURL]; [_request setValue:userAgent forHTTPHeaderField:@"User-Agent"]; _request.networkServiceType = background ? NSURLNetworkServiceTypeBackground : NSURLNetworkServiceTypeDefault; - if (httpHeaders) { + if (httpHeaders != nil) { for (NSString *key in httpHeaders) { NSString *value = [httpHeaders objectForKey:key]; [_request setValue:value forHTTPHeaderField:key]; @@ -131,16 +129,32 @@ return self; } -- (void)downloadUpdate +- (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem secondaryUpdateItem:(SUAppcastItem * _Nullable)secondaryUpdateItem host:(SUHost *)host userAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background delegate:(id)delegate { - [self.delegate downloadDriverWillBeginDownload]; + self = [self initWithRequestURL:updateItem.fileURL host:host userAgent:userAgent httpHeaders:httpHeaders inBackground:background delegate:delegate]; + if (self != nil) { + _updateItem = updateItem; + _secondaryUpdateItem = secondaryUpdateItem; + } + return self; +} + +- (void)downloadFile +{ + if ([self.delegate respondsToSelector:@selector(downloadDriverWillBeginDownload)]) { + [self.delegate downloadDriverWillBeginDownload]; + } - NSString *desiredFilename = [NSString stringWithFormat:@"%@ %@", [self.host name], [self.updateItem versionString]]; - - NSString *bundleIdentifier = self.host.bundle.bundleIdentifier; - assert(bundleIdentifier != nil); - - [self.downloader startPersistentDownloadWithRequest:[SPUURLRequest URLRequestWithRequest:self.request] bundleIdentifier:bundleIdentifier desiredFilename:desiredFilename]; + if (self.updateItem != nil) { + NSString *desiredFilename = [NSString stringWithFormat:@"%@ %@", [self.host name], [self.updateItem versionString]]; + + NSString *bundleIdentifier = self.host.bundle.bundleIdentifier; + assert(bundleIdentifier != nil); + + [self.downloader startPersistentDownloadWithRequest:[SPUURLRequest URLRequestWithRequest:self.request] bundleIdentifier:bundleIdentifier desiredFilename:desiredFilename]; + } else { + [self.downloader startTemporaryDownloadWithRequest:[SPUURLRequest URLRequestWithRequest:self.request]]; + } } - (void)removeDownloadedUpdate:(SPUDownloadedUpdate *)downloadedUpdate @@ -182,18 +196,28 @@ } } -- (void)downloaderDidFinishWithTemporaryDownloadData:(SPUDownloadData * _Nullable)__unused downloadData +- (void)downloaderDidFinishWithTemporaryDownloadData:(SPUDownloadData * _Nullable)downloadData { dispatch_async(dispatch_get_main_queue(), ^{ self.retrievedDownloadResult = YES; - if (self.expectedContentLength > 0 && self.updateItem.contentLength > 0 && self.expectedContentLength != self.updateItem.contentLength) { - SULog(SULogLevelError, @"Warning: Downloader's expected content length (%llu) != Appcast item's length (%llu)", self.expectedContentLength, self.updateItem.contentLength); + if (self.updateItem != nil) { + if (self.expectedContentLength > 0 && self.updateItem.contentLength > 0 && self.expectedContentLength != self.updateItem.contentLength) { + SULog(SULogLevelError, @"Warning: Downloader's expected content length (%llu) != Appcast item's length (%llu)", self.expectedContentLength, self.updateItem.contentLength); + } + + SPUDownloadedUpdate *downloadedUpdate = [[SPUDownloadedUpdate alloc] initWithAppcastItem:self.updateItem secondaryAppcastItem:self.secondaryUpdateItem downloadName:self.downloadName temporaryDirectory:self.temporaryDirectory]; + + if ([self.delegate respondsToSelector:@selector(downloadDriverDidDownloadUpdate:)]) { + [self.delegate downloadDriverDidDownloadUpdate:downloadedUpdate]; + } + } else { + assert(downloadData != nil); + SPUDownloadData *nonNullDownloadData = downloadData; + if ([self.delegate respondsToSelector:@selector(downloadDriverDidDownloadData:)]) { + [self.delegate downloadDriverDidDownloadData:nonNullDownloadData]; + } } - - SPUDownloadedUpdate *downloadedUpdate = [[SPUDownloadedUpdate alloc] initWithAppcastItem:self.updateItem secondaryAppcastItem:self.secondaryUpdateItem downloadName:self.downloadName temporaryDirectory:self.temporaryDirectory]; - - [self.delegate downloadDriverDidDownloadUpdate:downloadedUpdate]; }); } @@ -216,7 +240,7 @@ } NSError *downloadError = [NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:userInfo]; - [self.delegate downloadDriverDidFailToDownloadUpdateWithError:downloadError]; + [self.delegate downloadDriverDidFailToDownloadFileWithError:downloadError]; }); } @@ -232,7 +256,9 @@ { dispatch_async(dispatch_get_main_queue(), ^{ // Fallback to appcast item's content length if we don't get the length from HTTP header - [self.delegate downloadDriverDidReceiveExpectedContentLength:expectedContentLength > 0 ? (uint64_t)expectedContentLength : self.updateItem.contentLength]; + if ([self.delegate respondsToSelector:@selector(downloadDriverDidReceiveExpectedContentLength:)]) { + [self.delegate downloadDriverDidReceiveExpectedContentLength:expectedContentLength > 0 ? (uint64_t)expectedContentLength : self.updateItem.contentLength]; + } // Reset expected content length from downloader // Later we verify if the total length matches with the content length from the appcast @@ -245,7 +271,9 @@ - (void)downloaderDidReceiveDataOfLength:(uint64_t)length { dispatch_async(dispatch_get_main_queue(), ^{ - [self.delegate downloadDriverDidReceiveDataOfLength:length]; + if ([self.delegate respondsToSelector:@selector(downloadDriverDidReceiveDataOfLength:)]) { + [self.delegate downloadDriverDidReceiveDataOfLength:length]; + } }); } diff --git a/Sparkle/SPUUIBasedUpdateDriver.m b/Sparkle/SPUUIBasedUpdateDriver.m index 54ae8de4..78985580 100644 --- a/Sparkle/SPUUIBasedUpdateDriver.m +++ b/Sparkle/SPUUIBasedUpdateDriver.m @@ -14,7 +14,6 @@ #import "SPUUpdaterDelegate.h" #import "SUAppcastItem.h" #import "SUErrors.h" -#import "SPUURLDownload.h" #import "SPUDownloadData.h" #import "SPUResumableUpdate.h" @@ -268,14 +267,14 @@ 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]; - } - }); +// 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]; +// } +// }); } } diff --git a/Sparkle/SPUURLDownload.h b/Sparkle/SPUURLDownload.h deleted file mode 100644 index bfc7565d..00000000 --- a/Sparkle/SPUURLDownload.h +++ /dev/null @@ -1,19 +0,0 @@ -// -// SPUURLDownload.h -// Sparkle -// -// Created by Mayur Pawashe on 5/13/16. -// Copyright © 2016 Sparkle Project. All rights reserved. -// - -#import - -NS_ASSUME_NONNULL_BEGIN - -@class SPUDownloadData; - -// From the completion block, either data or error can be nil, but not both -// The completion block returns on the main queue -void SPUDownloadURLWithRequest(NSURLRequest * request, void (^completionBlock)(SPUDownloadData * _Nullable download, NSError * _Nullable error)); - -NS_ASSUME_NONNULL_END diff --git a/Sparkle/SPUURLDownload.m b/Sparkle/SPUURLDownload.m deleted file mode 100644 index 5c67de29..00000000 --- a/Sparkle/SPUURLDownload.m +++ /dev/null @@ -1,134 +0,0 @@ -// -// SUURLDownload.m -// Sparkle -// -// Created by Mayur Pawashe on 5/13/16. -// Copyright © 2016 Sparkle Project. All rights reserved. -// - -#import "SPUURLDownload.h" -#import "SPUXPCServiceInfo.h" -#import "SPUURLRequest.h" -#import "SPUDownloadData.h" -#import "SPUDownloaderProtocol.h" -#import "SPUDownloaderDelegate.h" -#import "SPUDownloader.h" -#import "SUErrors.h" - - -#include "AppKitPrevention.h" - -@interface SPUTemporaryDownloaderDelegate : NSObject - -@property (nonatomic, copy) void (^completionBlock)(SPUDownloadData * _Nullable, NSError * _Nullable); - -@end - -@implementation SPUTemporaryDownloaderDelegate - -@synthesize completionBlock = _completionBlock; - -- (instancetype)initWithCompletion:(void (^)(SPUDownloadData * _Nullable, NSError * _Nullable))completionBlock -{ - self = [super init]; - if (self != nil) { - _completionBlock = [completionBlock copy]; - } - return self; -} - -- (void)downloaderDidSetDestinationName:(NSString *)__unused destinationName temporaryDirectory:(NSString *)__unused temporaryDirectory -{ -} - -- (void)downloaderDidReceiveExpectedContentLength:(int64_t)__unused expectedContentLength -{ -} - -- (void)downloaderDidReceiveDataOfLength:(uint64_t)__unused length -{ -} - -- (void)downloaderDidFinishWithTemporaryDownloadData:(SPUDownloadData * _Nullable)downloadData -{ - assert(self.completionBlock != nil); - if (self.completionBlock != nil) { - self.completionBlock(downloadData, nil); - self.completionBlock = nil; - } -} - -- (void)downloaderDidFailWithError:(NSError *)error -{ - assert(self.completionBlock != nil); - if (self.completionBlock != nil) { - self.completionBlock(nil, error); - self.completionBlock = nil; - } -} - -@end - -void SPUDownloadURLWithRequest(NSURLRequest * request, void (^completionBlock)(SPUDownloadData * _Nullable, NSError * _Nullable)) -{ - id downloader = nil; - NSXPCConnection *connection = nil; - __block BOOL retrievedDownloadResult = NO; - - SPUTemporaryDownloaderDelegate *temporaryDownloaderDelegate = [[SPUTemporaryDownloaderDelegate alloc] initWithCompletion:^(SPUDownloadData * _Nullable downloadData, NSError * _Nullable error) { - dispatch_async(dispatch_get_main_queue(), ^{ - if (!retrievedDownloadResult) { - retrievedDownloadResult = YES; - [connection invalidate]; - - if (downloadData == nil || downloadData.data == nil) { - completionBlock(nil, error); - } else { - completionBlock(downloadData, nil); - } - } - }); - }]; - - if (!SPUXPCServiceExists(@DOWNLOADER_BUNDLE_ID)) { - downloader = [[SPUDownloader alloc] initWithDelegate:temporaryDownloaderDelegate]; - } else { - connection = [[NSXPCConnection alloc] initWithServiceName:@DOWNLOADER_BUNDLE_ID]; - connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SPUDownloaderProtocol)]; - connection.exportedInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SPUDownloaderDelegate)]; - connection.exportedObject = temporaryDownloaderDelegate; - - connection.interruptionHandler = ^{ - dispatch_async(dispatch_get_main_queue(), ^{ - if (!retrievedDownloadResult) { - // We'll break the retain cycle in the invalidation handler -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Warc-retain-cycles" - [connection invalidate]; -#pragma clang diagnostic pop - } - }); - }; - - connection.invalidationHandler = ^{ - dispatch_async(dispatch_get_main_queue(), ^{ - if (!retrievedDownloadResult) { - completionBlock(nil, [NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:nil]); - } - - // Break the retain cycle -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Warc-retain-cycles" - connection.interruptionHandler = nil; - connection.invalidationHandler = nil; -#pragma clang diagnostic pop - }); - }; - - [connection resume]; - - downloader = connection.remoteObjectProxy; - } - - [downloader startTemporaryDownloadWithRequest:[SPUURLRequest URLRequestWithRequest:request]]; -} diff --git a/Sparkle/SUAppcast.h b/Sparkle/SUAppcast.h index 7030dff3..7f09762e 100644 --- a/Sparkle/SUAppcast.h +++ b/Sparkle/SUAppcast.h @@ -22,15 +22,14 @@ NS_ASSUME_NONNULL_BEGIN @class SUAppcastItem; -SU_EXPORT @interface SUAppcast : NSObject +SU_EXPORT @interface SUAppcast : NSObject -@property (copy, nullable) NSString *userAgentString; -@property (copy, nullable) NSDictionary *httpHeaders; +- (nullable instancetype)initWithXMLData:(NSData *)xmlData relativeToURL:(NSURL *)relativeURL error:(NSError * __autoreleasing *)error; -- (void)fetchAppcastFromURL:(NSURL *)url inBackground:(BOOL)bg completionBlock:(void (^)(NSError *_Nullable))err; - (SUAppcast *)copyByFilteringItems:(BOOL (^)(SUAppcastItem *))filterBlock; @property (readonly, copy, nullable) NSArray *items; + @end NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index 467854a7..38000d60 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -14,111 +14,139 @@ #import "SULog.h" #import "SUErrors.h" #import "SULocalizations.h" -#import "SPUXPCServiceInfo.h" -#import "SPUURLDownload.h" -#import "SPUDownloadData.h" #include "AppKitPrevention.h" -@interface NSXMLElement (SUAppcastExtensions) -@property (readonly, copy) NSDictionary *attributesAsDictionary; -@end +//@interface NSXMLElement (SUAppcastExtensions) +//@property (readonly, copy) NSDictionary *attributesAsDictionary; +//@end +// +//@implementation NSXMLElement (SUAppcastExtensions) +//- (NSDictionary *)attributesAsDictionary +//{ +// NSEnumerator *attributeEnum = [[self attributes] objectEnumerator]; +// NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; +// +// for (NSXMLNode *attribute in attributeEnum) { +// NSString *attrName = [attribute name]; +// if (!attrName) { +// continue; +// } +// NSString *attributeStringValue = [attribute stringValue]; +// if (attributeStringValue != nil) { +// [dictionary setObject:attributeStringValue forKey:attrName]; +// } +// } +// return dictionary; +//} +//@end -@implementation NSXMLElement (SUAppcastExtensions) -- (NSDictionary *)attributesAsDictionary -{ - NSEnumerator *attributeEnum = [[self attributes] objectEnumerator]; - NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; +@interface SUAppcast () - for (NSXMLNode *attribute in attributeEnum) { - NSString *attrName = [attribute name]; - if (!attrName) { - continue; - } - NSString *attributeStringValue = [attribute stringValue]; - if (attributeStringValue != nil) { - [dictionary setObject:attributeStringValue forKey:attrName]; - } - } - return dictionary; -} -@end +@property (nonatomic, copy) NSArray *items; -@interface SUAppcast () -@property (strong) void (^completionBlock)(NSError *); -@property (copy) NSArray *items; -- (void)reportError:(NSError *)error; -- (NSXMLNode *)bestNodeInNodes:(NSArray *)nodes; @end @implementation SUAppcast -@synthesize completionBlock; -@synthesize userAgentString; -@synthesize httpHeaders; -@synthesize items; +@synthesize items = _items; -- (void)fetchAppcastFromURL:(NSURL *)url inBackground:(BOOL)background completionBlock:(void (^)(NSError *))block +//- (void)fetchAppcastFromURL:(NSURL *)url inBackground:(BOOL)background completionBlock:(void (^)(NSError *))block +//{ +// self.completionBlock = block; +// +// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0]; +// if (background) { +// request.networkServiceType = NSURLNetworkServiceTypeBackground; +// } +// +// if (self.userAgentString) { +// [request setValue:self.userAgentString forHTTPHeaderField:@"User-Agent"]; +// } +// request.networkServiceType = background ? NSURLNetworkServiceTypeBackground : NSURLNetworkServiceTypeDefault; +// +// if (self.httpHeaders) { +// for (NSString *key in self.httpHeaders) { +// NSString *value = [self.httpHeaders objectForKey:key]; +// [request setValue:value forHTTPHeaderField:key]; +// } +// } +// +// [request setValue:@"application/rss+xml,*/*;q=0.1" forHTTPHeaderField:@"Accept"]; +// +// SPUDownloadURLWithRequest(request, ^(SPUDownloadData * _Nullable downloadData, NSError * _Nullable error) { +// if (downloadData != nil) { +// NSError *parseError = nil; +// NSArray *appcastItems = [self parseAppcastItemsFromXMLData:downloadData.data relativeToURL:downloadData.URL error:&parseError]; +// +// if (appcastItems != nil) { +// self.items = appcastItems; +// self.completionBlock(nil); +// self.completionBlock = nil; +// } else { +// NSMutableDictionary *userInfo = [NSMutableDictionary +// dictionaryWithObject: SULocalizedString(@"An error occurred while parsing the update feed.", nil) +// forKey: NSLocalizedDescriptionKey]; +// if (parseError != nil) { +// [userInfo setObject:parseError forKey:NSUnderlyingErrorKey]; +// } +// [self reportError:[NSError errorWithDomain:SUSparkleErrorDomain +// code:SUAppcastParseError +// userInfo:userInfo]]; +// } +// } else { +// SULog(SULogLevelError, @"Encountered download feed error: %@", error); +// +// NSMutableDictionary *userInfo = [NSMutableDictionary +// dictionaryWithObject: SULocalizedString(@"An error occurred while downloading the update feed.", nil) +// forKey: NSLocalizedDescriptionKey]; +// +// if (error != nil) { +// NSError *assignedError = error; // Silences a Clang warning about implicit conversion from Nullable to Nonnull. +// [userInfo setObject:assignedError forKey:NSUnderlyingErrorKey]; +// } +// +// [self reportError:[NSError errorWithDomain:SUSparkleErrorDomain +// code:SUDownloadError +// userInfo:userInfo]]; +// } +// }); +//} + +//- (void)reportError:(NSError *)error +//{ +// NSMutableDictionary *userInfo = [error.userInfo mutableCopy]; +// +// if (!userInfo) +// { +// userInfo = [[NSMutableDictionary alloc] initWithDictionary:@{ +// NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred in retrieving update information. Please try again later.", nil), +// NSLocalizedFailureReasonErrorKey: [error localizedDescription], +// NSUnderlyingErrorKey: error, +// }]; +// } +// +// NSURL *failingUrl = [error.userInfo objectForKey:NSURLErrorFailingURLErrorKey]; +// if (failingUrl) { +// [userInfo setObject:failingUrl forKey:NSURLErrorFailingURLErrorKey]; +// } +// +// self.completionBlock([NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastError userInfo:userInfo]); +// self.completionBlock = nil; +//} + + +- (nullable instancetype)initWithXMLData:(NSData *)xmlData relativeToURL:(NSURL *)relativeURL error:(NSError * __autoreleasing *)error { - self.completionBlock = block; - - NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0]; - if (background) { - request.networkServiceType = NSURLNetworkServiceTypeBackground; - } - - if (self.userAgentString) { - [request setValue:self.userAgentString forHTTPHeaderField:@"User-Agent"]; - } - request.networkServiceType = background ? NSURLNetworkServiceTypeBackground : NSURLNetworkServiceTypeDefault; - - if (self.httpHeaders) { - for (NSString *key in self.httpHeaders) { - NSString *value = [self.httpHeaders objectForKey:key]; - [request setValue:value forHTTPHeaderField:key]; + self = [super init]; + if (self != nil) { + _items = [self parseAppcastItemsFromXMLData:xmlData relativeToURL:relativeURL error:error]; + if (_items == nil) { + return nil; } } - - [request setValue:@"application/rss+xml,*/*;q=0.1" forHTTPHeaderField:@"Accept"]; - - SPUDownloadURLWithRequest(request, ^(SPUDownloadData * _Nullable downloadData, NSError * _Nullable error) { - if (downloadData != nil) { - NSError *parseError = nil; - NSArray *appcastItems = [self parseAppcastItemsFromXMLData:downloadData.data relativeToURL:downloadData.URL error:&parseError]; - - if (appcastItems != nil) { - self.items = appcastItems; - self.completionBlock(nil); - self.completionBlock = nil; - } else { - NSMutableDictionary *userInfo = [NSMutableDictionary - dictionaryWithObject: SULocalizedString(@"An error occurred while parsing the update feed.", nil) - forKey: NSLocalizedDescriptionKey]; - if (parseError != nil) { - [userInfo setObject:parseError forKey:NSUnderlyingErrorKey]; - } - [self reportError:[NSError errorWithDomain:SUSparkleErrorDomain - code:SUAppcastParseError - userInfo:userInfo]]; - } - } else { - SULog(SULogLevelError, @"Encountered download feed error: %@", error); - - NSMutableDictionary *userInfo = [NSMutableDictionary - dictionaryWithObject: SULocalizedString(@"An error occurred while downloading the update feed.", nil) - forKey: NSLocalizedDescriptionKey]; - - if (error != nil) { - NSError *assignedError = error; // Silences a Clang warning about implicit conversion from Nullable to Nonnull. - [userInfo setObject:assignedError forKey:NSUnderlyingErrorKey]; - } - - [self reportError:[NSError errorWithDomain:SUSparkleErrorDomain - code:SUDownloadError - userInfo:userInfo]]; - } - }); + return self; } - (NSDictionary *)attributesOfNode:(NSXMLElement *)node @@ -255,34 +283,10 @@ return nil; } } - - self.items = appcastItems; return appcastItems; } -- (void)reportError:(NSError *)error -{ - NSMutableDictionary *userInfo = [error.userInfo mutableCopy]; - - if (!userInfo) - { - userInfo = [[NSMutableDictionary alloc] initWithDictionary:@{ - NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred in retrieving update information. Please try again later.", nil), - NSLocalizedFailureReasonErrorKey: [error localizedDescription], - NSUnderlyingErrorKey: error, - }]; - } - - NSURL *failingUrl = [error.userInfo objectForKey:NSURLErrorFailingURLErrorKey]; - if (failingUrl) { - [userInfo setObject:failingUrl forKey:NSURLErrorFailingURLErrorKey]; - } - - self.completionBlock([NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastError userInfo:userInfo]); - self.completionBlock = nil; -} - - (NSXMLNode *)bestNodeInNodes:(NSArray *)nodes { // We use this method to pick out the localized version of a node when one's available. diff --git a/Sparkle/SUAppcastDriver.h b/Sparkle/SUAppcastDriver.h index e9668678..85984d9b 100644 --- a/Sparkle/SUAppcastDriver.h +++ b/Sparkle/SUAppcastDriver.h @@ -28,6 +28,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)loadAppcastFromURL:(NSURL *)appcastURL userAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background includesSkippedUpdates:(BOOL)includesSkippedUpdates; +- (void)cleanup:(void (^)(void))completionHandler; + @end NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUAppcastDriver.m b/Sparkle/SUAppcastDriver.m index 64366002..61e71d6a 100644 --- a/Sparkle/SUAppcastDriver.m +++ b/Sparkle/SUAppcastDriver.m @@ -17,27 +17,33 @@ #import "SUConstants.h" #import "SUPhasedUpdateGroupInfo.h" #import "SULog.h" +#import "SPUDownloadDriver.h" +#import "SPUDownloadData.h" +#import "SULocalizations.h" +#import "SUErrors.h" #include "AppKitPrevention.h" -@interface SUAppcastDriver () +@interface SUAppcastDriver () @property (nonatomic, readonly) SUHost *host; -@property (nonatomic, copy) NSString *userAgent; @property (nullable, nonatomic, readonly, weak) id updater; @property (nullable, nonatomic, readonly, weak) id updaterDelegate; @property (nullable, nonatomic, readonly, weak) id delegate; +@property (nonatomic) SPUDownloadDriver *downloadDriver; +@property (nonatomic) BOOL includesSkippedUpdates; @end @implementation SUAppcastDriver @synthesize host = _host; -@synthesize userAgent = _userAgent; @synthesize updater = _updater; @synthesize updaterDelegate = _updaterDelegate; @synthesize delegate = _delegate; +@synthesize downloadDriver = _downloadDriver; +@synthesize includesSkippedUpdates = _includesSkippedUpdates; - (instancetype)initWithHost:(SUHost *)host updater:(id)updater updaterDelegate:(id )updaterDelegate delegate:(id )delegate { @@ -53,18 +59,48 @@ - (void)loadAppcastFromURL:(NSURL *)appcastURL userAgent:(NSString *)userAgent httpHeaders:(NSDictionary * _Nullable)httpHeaders inBackground:(BOOL)background includesSkippedUpdates:(BOOL)includesSkippedUpdates { - self.userAgent = userAgent; + NSMutableDictionary *requestHTTPHeaders = [NSMutableDictionary dictionary]; + if (httpHeaders != nil) { + [requestHTTPHeaders addEntriesFromDictionary:(NSDictionary * _Nonnull)httpHeaders]; + } + requestHTTPHeaders[@"Accept"] = @"application/rss+xml,*/*;q=0.1"; - SUAppcast *appcast = [[SUAppcast alloc] init]; - [appcast setUserAgentString:userAgent]; - [appcast setHttpHeaders:httpHeaders]; - [appcast fetchAppcastFromURL:appcastURL inBackground:background completionBlock:^(NSError *error) { - if (error != nil) { - [self.delegate didFailToFetchAppcastWithError:error]; - } else { - [self appcastDidFinishLoading:appcast inBackground:background includesSkippedUpdates:includesSkippedUpdates]; + self.downloadDriver = [[SPUDownloadDriver alloc] initWithRequestURL:appcastURL host:self.host userAgent:userAgent httpHeaders:requestHTTPHeaders inBackground:background delegate:self]; + + self.includesSkippedUpdates = includesSkippedUpdates; + + [self.downloadDriver downloadFile]; +} + +- (void)downloadDriverDidDownloadData:(SPUDownloadData *)downloadData +{ + NSError *appcastError = nil; + SUAppcast *appcast = [[SUAppcast alloc] initWithXMLData:downloadData.data relativeToURL:downloadData.URL error:&appcastError]; + + if (appcast == nil) { + NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObject:SULocalizedString(@"An error occurred while parsing the update feed.", nil) forKey:NSLocalizedDescriptionKey]; + + if (appcastError != nil) { + [userInfo setObject:appcastError forKey:NSUnderlyingErrorKey]; } - }]; + + [self.delegate didFailToFetchAppcastWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUAppcastParseError userInfo:userInfo]]; + } else { + [self appcastDidFinishLoading:appcast inBackground:self.downloadDriver.inBackground includesSkippedUpdates:self.includesSkippedUpdates]; + } +} + +- (void)downloadDriverDidFailToDownloadFileWithError:(nonnull NSError *)error +{ + SULog(SULogLevelError, @"Encountered download feed error: %@", error); + + NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{NSLocalizedDescriptionKey:SULocalizedString(@"An error occurred in retrieving update information. Please try again later.", nil)}]; + + if (error != nil) { + [userInfo setObject:error forKey:NSUnderlyingErrorKey]; + } + + [self.delegate didFailToFetchAppcastWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:userInfo]]; } - (SUAppcastItem * _Nullable)preferredUpdateForRegularAppcastItem:(SUAppcastItem * _Nullable)regularItem secondaryUpdate:(SUAppcastItem * __autoreleasing _Nullable *)secondaryUpdate @@ -277,4 +313,13 @@ return YES; } +- (void)cleanup:(void (^)(void))completionHandler +{ + if (self.downloadDriver == nil) { + completionHandler(); + } else { + [self.downloadDriver cleanup:completionHandler]; + } +} + @end