Handle error handling for update downloader connection

This commit is contained in:
Zorg committed 2016-04-01 23:45:59 -04:00
1 parent 0b19d8d7fd
commit d5cd64159c
6 files changed
+71 -51

No files matched your search

-2
View File
@@ -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];
}
}];
}
-1
View File
@@ -35,7 +35,6 @@ NS_ASSUME_NONNULL_BEGIN
@property (nullable, nonatomic, readonly, copy) NSString *downloadPath;
- (void)cleanup;
- (void)cancelTrashCleanup;
@end
+62 -40
View File
@@ -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
-4
View File
@@ -20,10 +20,6 @@ NS_ASSUME_NONNULL_BEGIN
- (void)downloaderDidReceiveDataOfLength:(NSUInteger)length;
- (void)downloaderDidFinish;
- (void)downloaderDidFailWithError:(NSError *)error;
@end
NS_ASSUME_NONNULL_END
+8 -3
View File
@@ -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 <SUDownloaderDelegate>)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
@@ -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