mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-30 05:28:50 +00:00
Remove completion block from SUUpdateDownloaderProtocol
Only use the delegate now for communication via this protocol. This cleans up some inconsistency.
This commit is contained in:
1 parent
033f59e643
commit
70582639ee
4 files changed
+70
-59
No files matched your search
Vendored
+42
-43
@@ -30,6 +30,7 @@
|
||||
@property (nonatomic, copy) NSString *temporaryDirectory;
|
||||
@property (nonatomic, copy) NSString *downloadName;
|
||||
@property (nonatomic, weak) id<SUDownloadDriverDelegate> 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<SUDownloadDriverDelegate>)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(), ^{
|
||||
|
||||
Vendored
+4
-2
@@ -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
|
||||
@@ -10,13 +10,16 @@
|
||||
#import "SUDownloaderDelegate.h"
|
||||
#import "SUErrors.h"
|
||||
|
||||
static NSString *SUUpdateDownloadingReason = @"Downloading Update";
|
||||
|
||||
@interface SUUpdateDownloader () <NSURLDownloadDelegate>
|
||||
|
||||
// Delegate is intentionally strongly referenced; see header
|
||||
@property (nonatomic) id <SUDownloaderDelegate> 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 <SUDownloaderDelegate>)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
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in new issue
Block a user