From 95416e4f6551278b1fa792e438c5c39f2e751bf7 Mon Sep 17 00:00:00 2001 From: Zorg Date: Fri, 13 May 2016 22:11:47 -0400 Subject: [PATCH] Add SUURLDownload specifically for downloading temporary files --- Sparkle.xcodeproj/project.pbxproj | 8 +++ Sparkle/SUAppcast.m | 94 ++++++++++--------------------- Sparkle/SUURLDownload.h | 17 ++++++ Sparkle/SUURLDownload.m | 60 ++++++++++++++++++++ 4 files changed, 114 insertions(+), 65 deletions(-) create mode 100644 Sparkle/SUURLDownload.h create mode 100644 Sparkle/SUURLDownload.m diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 8c5e0404..7b2bd15b 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -243,6 +243,8 @@ 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 /* SUURLDownload.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B09CE51CE6BCBD0052EF9E /* SUURLDownload.h */; }; + 72B09CE81CE6BCBD0052EF9E /* SUURLDownload.m in Sources */ = {isa = PBXBuildFile; fileRef = 72B09CE61CE6BCBD0052EF9E /* SUURLDownload.m */; }; 72B767CA1C9B707000A07552 /* SUAppcastDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B767C81C9B707000A07552 /* SUAppcastDriver.h */; }; 72B767CB1C9B707000A07552 /* SUAppcastDriver.m in Sources */ = {isa = PBXBuildFile; fileRef = 72B767C91C9B707000A07552 /* SUAppcastDriver.m */; }; 72B767CE1C9B924900A07552 /* SUInstallerDriver.h in Headers */ = {isa = PBXBuildFile; fileRef = 72B767CC1C9B924900A07552 /* SUInstallerDriver.h */; }; @@ -873,6 +875,8 @@ 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 /* SUURLDownload.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUURLDownload.h; sourceTree = ""; }; + 72B09CE61CE6BCBD0052EF9E /* SUURLDownload.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUURLDownload.m; sourceTree = ""; }; 72B767C81C9B707000A07552 /* SUAppcastDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUAppcastDriver.h; sourceTree = ""; }; 72B767C91C9B707000A07552 /* SUAppcastDriver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUAppcastDriver.m; sourceTree = ""; }; 72B767CC1C9B924900A07552 /* SUInstallerDriver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUInstallerDriver.h; sourceTree = ""; }; @@ -1311,6 +1315,8 @@ 61B5FB9509C4F04600B25A18 /* SUAppcast.m */, 61B5FC5309C5182000B25A18 /* SUAppcastItem.h */, 61B5FC5409C5182000B25A18 /* SUAppcastItem.m */, + 72B09CE51CE6BCBD0052EF9E /* SUURLDownload.h */, + 72B09CE61CE6BCBD0052EF9E /* SUURLDownload.m */, 61A225A20D1C4AC000430CCD /* SUStandardVersionComparator.h */, 61A225A30D1C4AC000430CCD /* SUStandardVersionComparator.m */, 61A2279A0D1CEE7600430CCD /* SUSystemProfiler.h */, @@ -1729,6 +1735,7 @@ 726E075C1CA3A6D6001A286B /* SUSecureCoding.h in Headers */, 618FA5050DAE8AB80026945C /* SUPlainInstaller.h in Headers */, 72B767E21C9CE90A00A07552 /* SUCoreBasedUpdateDriver.h in Headers */, + 72B09CE71CE6BCBD0052EF9E /* SUURLDownload.h in Headers */, 61A225A40D1C4AC000430CCD /* SUStandardVersionComparator.h in Headers */, 7229E1B91C97CC4D00CB50D0 /* SUScheduledUpdateDriver.h in Headers */, 72E41A391CB209CA003D47E0 /* SURemoteMessagePortProtocol.h in Headers */, @@ -2615,6 +2622,7 @@ 726E075D1CA3A6D6001A286B /* SUSecureCoding.m in Sources */, 72B767DB1C9CD2E400A07552 /* SUUIBasedUpdateDriver.m in Sources */, 729746461C91212400FC134E /* SURemoteMessagePort.m in Sources */, + 72B09CE81CE6BCBD0052EF9E /* SUURLDownload.m in Sources */, 729746581C936D5E00FC134E /* SUMessageTypes.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index fd1d0cac..1d48fbc5 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -15,6 +15,7 @@ #import "SULocalizations.h" #import "SUAppcastDownloader.h" #import "SUXPCServiceInfo.h" +#import "SUURLDownload.h" #ifdef _APPKITDEFINES_H #error This is a "core" class and should NOT import AppKit @@ -75,76 +76,39 @@ } [request setValue:@"application/rss+xml,*/*;q=0.1" forHTTPHeaderField:@"Accept"]; - - id appcastDownloader = nil; - NSXPCConnection *connection = nil; - __block BOOL retrievedDownloadResult = NO; - if (!SUXPCServiceExists(@APPCAST_DOWNLOADER_PRODUCT_NAME)) { - appcastDownloader = [[SUAppcastDownloader alloc] init]; - } else { - connection = [[NSXPCConnection alloc] initWithServiceName:@APPCAST_DOWNLOADER_BUNDLE_ID]; - connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SUAppcastDownloaderProtocol)]; - - __weak NSXPCConnection *weakConnection = connection; - connection.interruptionHandler = ^{ - dispatch_async(dispatch_get_main_queue(), ^{ - if (!retrievedDownloadResult) { - [weakConnection invalidate]; - } - }); - }; - - connection.invalidationHandler = ^{ - dispatch_async(dispatch_get_main_queue(), ^{ - if (!retrievedDownloadResult) { - SULog(@"Appcast Downloader connection was invalidated"); - - NSDictionary *userInfo = [NSDictionary - dictionaryWithObject: SULocalizedString(@"An error occurred while downloading the update feed.", nil) - forKey: NSLocalizedDescriptionKey]; - - [self reportError:[NSError errorWithDomain:SUSparkleErrorDomain - code:SUDownloadError - userInfo:userInfo]]; - } - }); - }; - - [connection resume]; - - appcastDownloader = connection.remoteObjectProxy; - } - - [appcastDownloader startDownloadWithRequest:request completion:^(NSData * _Nullable appcastData, NSError * _Nullable error) { - dispatch_async(dispatch_get_main_queue(), ^{ - retrievedDownloadResult = YES; - [connection invalidate]; + SUDownloadURLWithRequest(request, ^(NSData * _Nullable data, NSError * _Nullable error) { + if (data != nil) { + NSError *parseError = nil; + NSArray *appcastItems = [self parseAppcastItemsFromXMLData:data error:&parseError]; - if (appcastData == nil) { - [self reportError:error]; + if (appcastItems != nil) { + self.items = appcastItems; + self.completionBlock(nil); + self.completionBlock = nil; } else { - NSError *parseError = nil; - NSArray *appcastItems = [self parseAppcastItemsFromXMLData:appcastData 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]]; + 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(@"Encountered download feed error: %@", error); + + NSDictionary *userInfo = [NSDictionary + dictionaryWithObject: SULocalizedString(@"An error occurred while downloading the update feed.", nil) + forKey: NSLocalizedDescriptionKey]; + + [self reportError:[NSError errorWithDomain:SUSparkleErrorDomain + code:SUDownloadError + userInfo:userInfo]]; + } + }); } -(NSArray *)parseAppcastItemsFromXMLData:(NSData *)appcastData error:(NSError *__autoreleasing*)errorp { diff --git a/Sparkle/SUURLDownload.h b/Sparkle/SUURLDownload.h new file mode 100644 index 00000000..1b0e92f3 --- /dev/null +++ b/Sparkle/SUURLDownload.h @@ -0,0 +1,17 @@ +// +// SUURLDownload.h +// Sparkle +// +// Created by Mayur Pawashe on 5/13/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +// From the completion block, either data or error can be nil (or both can be nil if an unknown error occured) +// The completion block returns on the main queue +void SUDownloadURLWithRequest(NSURLRequest * request, void (^completionBlock)(NSData * _Nullable data, NSError * _Nullable error)); + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/SUURLDownload.m b/Sparkle/SUURLDownload.m new file mode 100644 index 00000000..44798736 --- /dev/null +++ b/Sparkle/SUURLDownload.m @@ -0,0 +1,60 @@ +// +// SUURLDownload.m +// Sparkle +// +// Created by Mayur Pawashe on 5/13/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SUURLDownload.h" +#import "SUXPCServiceInfo.h" +#import "SUAppcastDownloader.h" +#import "SUAppcastDownloaderProtocol.h" + +void SUDownloadURLWithRequest(NSURLRequest * request, void (^completionBlock)(NSData * _Nullable, NSError * _Nullable)) +{ + id downloader = nil; + NSXPCConnection *connection = nil; + __block BOOL retrievedDownloadResult = NO; + + if (!SUXPCServiceExists(@APPCAST_DOWNLOADER_PRODUCT_NAME)) { + downloader = [[SUAppcastDownloader alloc] init]; + } else { + connection = [[NSXPCConnection alloc] initWithServiceName:@APPCAST_DOWNLOADER_BUNDLE_ID]; + connection.remoteObjectInterface = [NSXPCInterface interfaceWithProtocol:@protocol(SUAppcastDownloaderProtocol)]; + + __weak NSXPCConnection *weakConnection = connection; + connection.interruptionHandler = ^{ + dispatch_async(dispatch_get_main_queue(), ^{ + if (!retrievedDownloadResult) { + [weakConnection invalidate]; + } + }); + }; + + connection.invalidationHandler = ^{ + dispatch_async(dispatch_get_main_queue(), ^{ + if (!retrievedDownloadResult) { + completionBlock(nil, nil); + } + }); + }; + + [connection resume]; + + downloader = connection.remoteObjectProxy; + } + + [downloader startDownloadWithRequest:request completion:^(NSData * _Nullable data, NSError * _Nullable error) { + dispatch_async(dispatch_get_main_queue(), ^{ + retrievedDownloadResult = YES; + [connection invalidate]; + + if (data == nil) { + completionBlock(nil, error); + } else { + completionBlock(data, nil); + } + }); + }]; +}