From 70582639ee7b7dbaff04abea8cc902d0088fd028 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 8 May 2016 16:24:16 -0400 Subject: [PATCH] Remove completion block from SUUpdateDownloaderProtocol Only use the delegate now for communication via this protocol. This cleans up some inconsistency. --- Sparkle/SUDownloadDriver.m | 85 +++++++++---------- Sparkle/SUDownloaderDelegate.h | 6 +- UpdateDownloader/SUUpdateDownloader.m | 36 +++++--- UpdateDownloader/SUUpdateDownloaderProtocol.h | 2 +- 4 files changed, 70 insertions(+), 59 deletions(-) diff --git a/Sparkle/SUDownloadDriver.m b/Sparkle/SUDownloadDriver.m index 2bfec485..143d7903 100644 --- a/Sparkle/SUDownloadDriver.m +++ b/Sparkle/SUDownloadDriver.m @@ -30,6 +30,7 @@ @property (nonatomic, copy) NSString *temporaryDirectory; @property (nonatomic, copy) NSString *downloadName; @property (nonatomic, weak) id delegate; +@property (nonatomic) BOOL retrievedDownloadResult; @end @@ -43,6 +44,7 @@ @synthesize temporaryDirectory = _temporaryDirectory; @synthesize downloadName = _downloadName; @synthesize delegate = _delegate; +@synthesize retrievedDownloadResult = _retrievedDownloadResult; - (instancetype)initWithUpdateItem:(SUAppcastItem *)updateItem host:(SUHost *)host userAgent:(NSString *)userAgent delegate:(id)delegate { @@ -77,12 +79,12 @@ assert(bundleIdentifier != nil); __weak SUDownloadDriver *weakSelf = self; - __block BOOL retrievedDownloadResult = NO; self.connection.interruptionHandler = ^{ dispatch_async(dispatch_get_main_queue(), ^{ - if (!retrievedDownloadResult) { - [weakSelf.connection invalidate]; + SUDownloadDriver *strongSelf = weakSelf; + if (strongSelf != nil && !strongSelf.retrievedDownloadResult) { + [strongSelf.connection invalidate]; } }); }; @@ -90,7 +92,7 @@ self.connection.invalidationHandler = ^{ dispatch_async(dispatch_get_main_queue(), ^{ SUDownloadDriver *strongSelf = weakSelf; - if (!retrievedDownloadResult && strongSelf != nil) { + if (strongSelf != nil && !strongSelf.retrievedDownloadResult) { SULog(@"Connection to update downloader was invalidated"); NSDictionary *userInfo = @@ -108,37 +110,7 @@ [self.delegate downloadDriverWillBeginDownload]; NSString *desiredFilename = [NSString stringWithFormat:@"%@ %@", [self.host name], [self.updateItem versionString]]; - [self.downloader startDownloadWithRequest:self.request bundleIdentifier:bundleIdentifier desiredFilename:desiredFilename completion:^(BOOL success, NSError * _Nullable error) { - dispatch_async(dispatch_get_main_queue(), ^{ - retrievedDownloadResult = YES; - SUDownloadDriver *strongSelf = weakSelf; - if (strongSelf == nil) { - return; - } - - 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]; - [strongSelf.delegate downloadDriverDidFailToDownloadUpdateWithError:downloadError]; - } else { - [strongSelf.delegate downloadDriverDidDownloadUpdate]; - } - - [strongSelf cleanup]; - }); - }]; + [self.downloader startDownloadWithRequest:self.request bundleIdentifier:bundleIdentifier desiredFilename:desiredFilename]; } - (void)dealloc @@ -162,6 +134,41 @@ }]; } +- (void)downloaderDidFinishDownloading +{ + dispatch_async(dispatch_get_main_queue(), ^{ + self.retrievedDownloadResult = YES; + + [self.delegate downloadDriverDidDownloadUpdate]; + [self cleanup]; + }); +} + +- (void)downloaderDidFailWithError:(NSError *)error +{ + dispatch_async(dispatch_get_main_queue(), ^{ + self.retrievedDownloadResult = YES; + + 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]; + + [self cleanup]; + }); +} + - (void)downloaderDidSetDestinationName:(NSString *)destinationName temporaryDirectory:(NSString *)temporaryDirectory { dispatch_async(dispatch_get_main_queue(), ^{ @@ -170,14 +177,6 @@ }); } -- (void)downloaderDidFailToCreateTemporaryDirectoryWithError:(NSError *)error -{ - dispatch_async(dispatch_get_main_queue(), ^{ - [self cleanup]; - [self.delegate downloadDriverDidFailToDownloadUpdateWithError:error]; - }); -} - - (void)downloaderDidReceiveResponse:(NSURLResponse *)response { dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/Sparkle/SUDownloaderDelegate.h b/Sparkle/SUDownloaderDelegate.h index 90842887..625682cf 100644 --- a/Sparkle/SUDownloaderDelegate.h +++ b/Sparkle/SUDownloaderDelegate.h @@ -14,12 +14,14 @@ NS_ASSUME_NONNULL_BEGIN - (void)downloaderDidSetDestinationName:(NSString *)destinationName temporaryDirectory:(NSString *)temporaryDirectory; -- (void)downloaderDidFailToCreateTemporaryDirectoryWithError:(NSError *)error; - - (void)downloaderDidReceiveResponse:(NSURLResponse *)response; - (void)downloaderDidReceiveDataOfLength:(NSUInteger)length; +- (void)downloaderDidFinishDownloading; + +- (void)downloaderDidFailWithError:(NSError *)error; + @end NS_ASSUME_NONNULL_END diff --git a/UpdateDownloader/SUUpdateDownloader.m b/UpdateDownloader/SUUpdateDownloader.m index 40f23bed..49b45723 100644 --- a/UpdateDownloader/SUUpdateDownloader.m +++ b/UpdateDownloader/SUUpdateDownloader.m @@ -10,13 +10,16 @@ #import "SUDownloaderDelegate.h" #import "SUErrors.h" +static NSString *SUUpdateDownloadingReason = @"Downloading Update"; + @interface SUUpdateDownloader () +// Delegate is intentionally strongly referenced; see header @property (nonatomic) id delegate; @property (nonatomic) NSURLDownload *download; @property (nonatomic, copy) NSString *bundleIdentifier; @property (nonatomic, copy) NSString *desiredFilename; -@property (nonatomic, copy) void (^completionBlock)(BOOL, NSError * _Nullable); +@property (nonatomic) BOOL disabledAutomaticTermination; @end @@ -26,7 +29,7 @@ @synthesize download = _download; @synthesize bundleIdentifier = _bundleIdentifier; @synthesize desiredFilename = _desiredFilename; -@synthesize completionBlock = _completionBlock; +@synthesize disabledAutomaticTermination = _disabledAutomaticTermination; - (instancetype)initWithDelegate:(id )delegate { @@ -37,12 +40,9 @@ return self; } -- (void)dealloc -{ - [self cleanup]; -} +// Don't implement dealloc - make the client call cleanup, which is the only way to remove the reference cycle from the delegate anyway -- (void)startDownloadWithRequest:(NSURLRequest *)request bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename completion:(void (^)(BOOL success, NSError * _Nullable error))completionBlock +- (void)startDownloadWithRequest:(NSURLRequest *)request bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename { dispatch_async(dispatch_get_main_queue(), ^{ // Remove our old caches path so we don't start accumulating files in there @@ -51,15 +51,27 @@ [[NSFileManager defaultManager] removeItemAtPath:cachePath error:NULL]; } + // Prevent service from automatically terminating while downloading the update asynchronously without any reply blocks + [[NSProcessInfo processInfo] disableAutomaticTermination:SUUpdateDownloadingReason]; + self.disabledAutomaticTermination = YES; + self.download = [[NSURLDownload alloc] initWithRequest:request delegate:self]; self.desiredFilename = desiredFilename; self.bundleIdentifier = bundleIdentifier; - self.completionBlock = completionBlock; }); } +- (void)enableAutomaticTermination +{ + if (self.disabledAutomaticTermination) { + [[NSProcessInfo processInfo] enableAutomaticTermination:SUUpdateDownloadingReason]; + self.disabledAutomaticTermination = NO; + } +} + - (void)cleanup { + [self enableAutomaticTermination]; [self.download cancel]; self.download = nil; self.delegate = nil; @@ -106,7 +118,7 @@ NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUTemporaryDirectoryError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Can't make a temporary directory for the update download at %@.", tempDir] }]; - [self.delegate downloaderDidFailToCreateTemporaryDirectoryWithError:error]; + [self.delegate downloaderDidFailWithError:error]; } else { [self.download setDestination:[tempDir stringByAppendingPathComponent:name] allowOverwrite:YES]; @@ -127,15 +139,13 @@ - (void)downloadDidFinish:(NSURLDownload *)__unused d { self.download = nil; - self.completionBlock(YES, nil); - self.completionBlock = nil; + [self.delegate downloaderDidFinishDownloading]; } - (void)download:(NSURLDownload *)__unused download didFailWithError:(NSError *)error { self.download = nil; - self.completionBlock(NO, error); - self.completionBlock = nil; + [self.delegate downloaderDidFailWithError:error]; } - (BOOL)download:(NSURLDownload *)__unused download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType diff --git a/UpdateDownloader/SUUpdateDownloaderProtocol.h b/UpdateDownloader/SUUpdateDownloaderProtocol.h index 9ffc461e..853479f7 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 completion:(void (^)(BOOL success, NSError * _Nullable error))completionBlock; +- (void)startDownloadWithRequest:(NSURLRequest *)request bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename; - (void)cleanupWithCompletion:(void (^)(void))completionBlock;