diff --git a/Sparkle/SUCoreBasedUpdateDriver.m b/Sparkle/SUCoreBasedUpdateDriver.m index 2df12e1b..a9604df2 100644 --- a/Sparkle/SUCoreBasedUpdateDriver.m +++ b/Sparkle/SUCoreBasedUpdateDriver.m @@ -156,8 +156,6 @@ [self.installerDriver extractDownloadPath:downloadPath withUpdateItem:self.updateItem temporaryDirectory:temporaryDirectory completion:^(NSError * _Nullable error) { if (error != nil) { [self.delegate coreDriverIsRequestingAbortUpdateWithError:error]; - } else { - [self.downloadDriver cancelTrashCleanup]; } }]; } diff --git a/Sparkle/SUDownloadDriver.h b/Sparkle/SUDownloadDriver.h index a0a7bd1f..76bba952 100644 --- a/Sparkle/SUDownloadDriver.h +++ b/Sparkle/SUDownloadDriver.h @@ -35,7 +35,6 @@ NS_ASSUME_NONNULL_BEGIN @property (nullable, nonatomic, readonly, copy) NSString *downloadPath; - (void)cleanup; -- (void)cancelTrashCleanup; @end diff --git a/Sparkle/SUDownloadDriver.m b/Sparkle/SUDownloadDriver.m index e899237d..14b02b13 100644 --- a/Sparkle/SUDownloadDriver.m +++ b/Sparkle/SUDownloadDriver.m @@ -13,6 +13,7 @@ #import "SUFileManager.h" #import "SULocalizations.h" #import "SUHost.h" +#import "SULog.h" #import "SUErrors.h" #ifdef _APPKITDEFINES_H @@ -66,7 +67,67 @@ NSString *bundleIdentifier = self.host.bundle.bundleIdentifier; assert(bundleIdentifier != nil); - [self.connection.remoteObjectProxy startDownloadWithRequest:self.request bundleIdentifier:bundleIdentifier desiredFilename:[NSString stringWithFormat:@"%@ %@", [self.host name], [self.updateItem versionString]]]; + __weak SUDownloadDriver *weakSelf = self; + __block BOOL retrievedDownloadResult = NO; + + self.connection.interruptionHandler = ^{ + dispatch_async(dispatch_get_main_queue(), ^{ + if (!retrievedDownloadResult) { + [weakSelf.connection invalidate]; + } + }); + }; + + self.connection.invalidationHandler = ^{ + dispatch_async(dispatch_get_main_queue(), ^{ + if (!retrievedDownloadResult) { + SULog(@"Connection to update downloader was invalidated"); + + NSDictionary *userInfo = + @{ + NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while downloading the update. Please try again later.", nil), + }; + + NSError *downloadError = [NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:userInfo]; + + [weakSelf.delegate downloadDriverDidFailToDownloadUpdateWithError:downloadError]; + } + }); + }; + + NSString *desiredFilename = [NSString stringWithFormat:@"%@ %@", [self.host name], [self.updateItem versionString]]; + [self.connection.remoteObjectProxy startDownloadWithRequest:self.request bundleIdentifier:bundleIdentifier desiredFilename:desiredFilename completion:^(BOOL success, NSError * _Nullable error) { + dispatch_async(dispatch_get_main_queue(), ^{ + retrievedDownloadResult = YES; + + if (!success) { + NSURL *failingUrl = error.userInfo[NSURLErrorFailingURLErrorKey]; + if (!failingUrl) { + failingUrl = [self.updateItem fileURL]; + } + + NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ + NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while downloading the update. Please try again later.", nil), + NSUnderlyingErrorKey: error, + }]; + if (failingUrl) { + userInfo[NSURLErrorFailingURLErrorKey] = failingUrl; + } + + NSError *downloadError = [NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:userInfo]; + [self.delegate downloadDriverDidFailToDownloadUpdateWithError:downloadError]; + } else { + // If this fails, then we must have already cleaned up / cancelled + SUDownloadDriver *strongSelf = weakSelf; + if (strongSelf.connection != nil) { + [strongSelf.connection invalidate]; + strongSelf.connection = nil; + + [strongSelf.delegate downloadDriverDidDownloadUpdate]; + } + } + }); + }]; } - (void)dealloc @@ -74,11 +135,6 @@ [self cleanup]; } -- (void)cancelTrashCleanup -{ - self.temporaryDirectory = nil; -} - - (void)cleanup { if (self.connection != nil) { @@ -118,38 +174,4 @@ }); } -- (void)downloaderDidFinish -{ - dispatch_async(dispatch_get_main_queue(), ^{ - // If this fails, then we must have already cleaned up / cancelled - if (self.connection != nil) { - [self.connection invalidate]; - self.connection = nil; - - [self.delegate downloadDriverDidDownloadUpdate]; - } - }); -} - -- (void)downloaderDidFailWithError:(NSError *)error -{ - dispatch_async(dispatch_get_main_queue(), ^{ - NSURL *failingUrl = error.userInfo[NSURLErrorFailingURLErrorKey]; - if (!failingUrl) { - failingUrl = [self.updateItem fileURL]; - } - - NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithDictionary:@{ - NSLocalizedDescriptionKey: SULocalizedString(@"An error occurred while downloading the update. Please try again later.", nil), - NSUnderlyingErrorKey: error, - }]; - if (failingUrl) { - userInfo[NSURLErrorFailingURLErrorKey] = failingUrl; - } - - NSError *downloadError = [NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:userInfo]; - [self.delegate downloadDriverDidFailToDownloadUpdateWithError:downloadError]; - }); -} - @end diff --git a/Sparkle/SUDownloaderDelegate.h b/Sparkle/SUDownloaderDelegate.h index 5ac57c01..8187e93c 100644 --- a/Sparkle/SUDownloaderDelegate.h +++ b/Sparkle/SUDownloaderDelegate.h @@ -20,10 +20,6 @@ NS_ASSUME_NONNULL_BEGIN - (void)downloaderDidReceiveDataOfLength:(NSUInteger)length; -- (void)downloaderDidFinish; - -- (void)downloaderDidFailWithError:(NSError *)error; - @end NS_ASSUME_NONNULL_END diff --git a/UpdateDownloader/SUUpdateDownloader.m b/UpdateDownloader/SUUpdateDownloader.m index 9a3212ed..ffda02e4 100644 --- a/UpdateDownloader/SUUpdateDownloader.m +++ b/UpdateDownloader/SUUpdateDownloader.m @@ -16,6 +16,7 @@ @property (nonatomic) NSURLDownload *download; @property (nonatomic, copy) NSString *bundleIdentifier; @property (nonatomic, copy) NSString *desiredFilename; +@property (nonatomic, copy) void (^completionBlock)(BOOL, NSError * _Nullable); @end @@ -25,6 +26,7 @@ @synthesize download = _download; @synthesize bundleIdentifier = _bundleIdentifier; @synthesize desiredFilename = _desiredFilename; +@synthesize completionBlock = _completionBlock; - (instancetype)initWithDelegate:(id )delegate { @@ -41,12 +43,13 @@ self.download = nil; } -- (void)startDownloadWithRequest:(NSURLRequest *)request bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename +- (void)startDownloadWithRequest:(NSURLRequest *)request bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename completion:(void (^)(BOOL success, NSError * _Nullable error))completionBlock { dispatch_async(dispatch_get_main_queue(), ^{ self.download = [[NSURLDownload alloc] initWithRequest:request delegate:self]; self.desiredFilename = desiredFilename; self.bundleIdentifier = bundleIdentifier; + self.completionBlock = completionBlock; // Remove our old caches path so we don't start accumulating files in there NSString *cachePath = [[self class] sparkleCachePathForBundleIdentifier:self.bundleIdentifier]; @@ -110,12 +113,14 @@ - (void)downloadDidFinish:(NSURLDownload *)__unused d { - [self.delegate downloaderDidFinish]; + self.completionBlock(YES, nil); + self.completionBlock = nil; } - (void)download:(NSURLDownload *)__unused download didFailWithError:(NSError *)error { - [self.delegate downloaderDidFailWithError:error]; + self.completionBlock(NO, error); + self.completionBlock = nil; } - (BOOL)download:(NSURLDownload *)__unused download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType diff --git a/UpdateDownloader/SUUpdateDownloaderProtocol.h b/UpdateDownloader/SUUpdateDownloaderProtocol.h index 04596f04..dfb8b7ff 100644 --- a/UpdateDownloader/SUUpdateDownloaderProtocol.h +++ b/UpdateDownloader/SUUpdateDownloaderProtocol.h @@ -13,7 +13,7 @@ NS_ASSUME_NONNULL_BEGIN // The protocol that this service will vend as its API. This header file will also need to be visible to the process hosting the service. @protocol SUUpdateDownloaderProtocol -- (void)startDownloadWithRequest:(NSURLRequest *)request bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename; +- (void)startDownloadWithRequest:(NSURLRequest *)request bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename completion:(void (^)(BOOL success, NSError * _Nullable error))completionBlock; @end