From e476ca2cf06bf134cbffcc0b37fa94a1744fd5c1 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 20 Aug 2016 11:26:49 -0400 Subject: [PATCH] Handle both temporary and persistent downloads in our Downloader I will aim to remove the TemporaryDownloader target and rename PersistentDownloader to just Downloader. --- PersistentDownloader/SUPersistentDownloader.m | 115 ++++++++++++++---- .../SUPersistentDownloaderDelegate.h | 6 +- .../SUPersistentDownloaderProtocol.h | 10 +- Sparkle/SUDownloadDriver.m | 7 +- 4 files changed, 104 insertions(+), 34 deletions(-) diff --git a/PersistentDownloader/SUPersistentDownloader.m b/PersistentDownloader/SUPersistentDownloader.m index 4453e6e5..d8aec663 100644 --- a/PersistentDownloader/SUPersistentDownloader.m +++ b/PersistentDownloader/SUPersistentDownloader.m @@ -10,6 +10,7 @@ #import "SUPersistentDownloaderDelegate.h" #import "SPULocalCacheDirectory.h" #import "SPUURLRequest.h" +#import "SPUDownloadData.h" #import "SUErrors.h" #ifdef _APPKITDEFINES_H @@ -25,7 +26,10 @@ static NSString *SUPersistentDownloadingReason = @"Downloading persistent file"; @property (nonatomic) NSURLDownload *download; @property (nonatomic, copy) NSString *bundleIdentifier; @property (nonatomic, copy) NSString *desiredFilename; +@property (nonatomic, copy) NSString *downloadFilename; @property (nonatomic) BOOL disabledAutomaticTermination; +@property (nonatomic) SPUDownloadMode mode; +@property (nonatomic) NSURLResponse *response; @end @@ -35,7 +39,10 @@ static NSString *SUPersistentDownloadingReason = @"Downloading persistent file"; @synthesize download = _download; @synthesize bundleIdentifier = _bundleIdentifier; @synthesize desiredFilename = _desiredFilename; +@synthesize downloadFilename = _downloadFilename; @synthesize disabledAutomaticTermination = _disabledAutomaticTermination; +@synthesize mode = _mode; +@synthesize response = _response; - (instancetype)initWithDelegate:(id )delegate { @@ -48,16 +55,18 @@ static NSString *SUPersistentDownloadingReason = @"Downloading persistent file"; // 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:(SPUURLRequest *)request bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename +- (void)startDownloadWithRequest:(SPUURLRequest *)request mode:(SPUDownloadMode)mode bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename { dispatch_async(dispatch_get_main_queue(), ^{ // Prevent service from automatically terminating while downloading the update asynchronously without any reply blocks [[NSProcessInfo processInfo] disableAutomaticTermination:SUPersistentDownloadingReason]; self.disabledAutomaticTermination = YES; - self.download = [[NSURLDownload alloc] initWithRequest:request.request delegate:self]; + self.mode = mode; self.desiredFilename = desiredFilename; self.bundleIdentifier = bundleIdentifier; + + self.download = [[NSURLDownload alloc] initWithRequest:request.request delegate:self]; }); } @@ -75,44 +84,72 @@ static NSString *SUPersistentDownloadingReason = @"Downloading persistent file"; [self.download cancel]; self.download = nil; self.delegate = nil; + + if (self.mode == SPUDownloadModeTemporary && self.downloadFilename != nil) + { + [[NSFileManager defaultManager] removeItemAtPath:self.downloadFilename error:NULL]; + self.downloadFilename = nil; + } } - (void)download:(NSURLDownload *)__unused d decideDestinationWithSuggestedFilename:(NSString *)name { - // Remove our old caches path so we don't start accumulating files in there - NSString *rootPersistentDownloadCachePath = [[SPULocalCacheDirectory cachePathForBundleIdentifier:self.bundleIdentifier] stringByAppendingPathComponent:@"PersistentDownloads"]; - - [SPULocalCacheDirectory removeOldItemsInDirectory:rootPersistentDownloadCachePath]; - - NSString *tempDir = [SPULocalCacheDirectory createUniqueDirectoryInDirectory:rootPersistentDownloadCachePath]; - if (tempDir == nil) + if (self.mode == SPUDownloadModeTemporary) { - // Okay, something's really broken with this user's file structure. - [self.download cancel]; - self.download = nil; + // Files downloaded in temporary mode should not last for very long, + // so it's ideal to place them in a system temporary directory + NSString *destinationFilename = NSTemporaryDirectory(); + if (destinationFilename) + { + destinationFilename = [destinationFilename stringByAppendingPathComponent:name]; + + [self.download setDestination:destinationFilename allowOverwrite:NO]; + } + } + else + { + // Remove our old caches path so we don't start accumulating files in there + NSString *rootPersistentDownloadCachePath = [[SPULocalCacheDirectory cachePathForBundleIdentifier:self.bundleIdentifier] stringByAppendingPathComponent:@"PersistentDownloads"]; - NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUTemporaryDirectoryError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Can't make a temporary directory for the update download at %@.", tempDir] }]; + [SPULocalCacheDirectory removeOldItemsInDirectory:rootPersistentDownloadCachePath]; - [self.delegate downloaderDidFailWithError:error]; - } else { - NSString *downloadFileName = self.desiredFilename; - NSString *downloadFileNameDirectory = [tempDir stringByAppendingPathComponent:downloadFileName]; - - NSError *createError = nil; - if (![[NSFileManager defaultManager] createDirectoryAtPath:downloadFileNameDirectory withIntermediateDirectories:NO attributes:nil error:&createError]) { - NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUTemporaryDirectoryError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Can't make a download file name %@ directory inside temporary directory for the update download at %@.", downloadFileName, downloadFileNameDirectory] }]; + NSString *tempDir = [SPULocalCacheDirectory createUniqueDirectoryInDirectory:rootPersistentDownloadCachePath]; + if (tempDir == nil) + { + // Okay, something's really broken with this user's file structure. + [self.download cancel]; + self.download = nil; + + 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 downloaderDidFailWithError:error]; } else { - [self.download setDestination:[downloadFileNameDirectory stringByAppendingPathComponent:name] allowOverwrite:YES]; + NSString *downloadFileName = self.desiredFilename; + NSString *downloadFileNameDirectory = [tempDir stringByAppendingPathComponent:downloadFileName]; - [self.delegate downloaderDidSetDestinationName:name temporaryDirectory:downloadFileNameDirectory]; + NSError *createError = nil; + if (![[NSFileManager defaultManager] createDirectoryAtPath:downloadFileNameDirectory withIntermediateDirectories:NO attributes:nil error:&createError]) { + NSError *error = [NSError errorWithDomain:SUSparkleErrorDomain code:SUTemporaryDirectoryError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Can't make a download file name %@ directory inside temporary directory for the update download at %@.", downloadFileName, downloadFileNameDirectory] }]; + + [self.delegate downloaderDidFailWithError:error]; + } else { + [self.download setDestination:[downloadFileNameDirectory stringByAppendingPathComponent:name] allowOverwrite:YES]; + + [self.delegate downloaderDidSetDestinationName:name temporaryDirectory:downloadFileNameDirectory]; + } } } } +- (void)download:(NSURLDownload *)__unused aDownload didCreateDestination:(NSString *)path +{ + self.downloadFilename = path; +} + - (void)download:(NSURLDownload *)__unused download didReceiveResponse:(NSURLResponse *)response { + self.response = response; + // It might be tempting to send over the response object instead of the expected content length but this isn't a good idea // For one, we are only ever concerned about the expected content length // Another reason is that NSURLResponse doesn't support NSSecureCoding in older OS releases (eg: 10.8), which cause issues with XPC @@ -126,21 +163,47 @@ static NSString *SUPersistentDownloadingReason = @"Downloading persistent file"; - (void)downloadDidFinish:(NSURLDownload *)__unused d { + assert(self.response != nil); + assert(self.downloadFilename != nil); + + SPUDownloadData *downloadData = nil; + if (self.mode == SPUDownloadModeTemporary) { + NSData *data = [NSData dataWithContentsOfFile:self.downloadFilename]; + if (data != nil) { + downloadData = [[SPUDownloadData alloc] initWithData:data textEncodingName:self.response.textEncodingName MIMEType:self.response.MIMEType]; + } + } + self.download = nil; - [self.delegate downloaderDidFinishDownloading]; + + switch (self.mode) { + case SPUDownloadModeTemporary: + if (downloadData != nil) { + [self.delegate downloaderDidFinishWithTemporaryDownloadData:downloadData]; + } else { + [self.delegate downloaderDidFailWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to read temporary downloaded data from %@", self.downloadFilename]}]]; + } + break; + case SPUDownloadModePersistent: + [self.delegate downloaderDidFinishWithTemporaryDownloadData:nil]; + break; + } + + [self cleanup]; } - (void)download:(NSURLDownload *)__unused download didFailWithError:(NSError *)error { self.download = nil; [self.delegate downloaderDidFailWithError:error]; + [self cleanup]; } - (BOOL)download:(NSURLDownload *)__unused download shouldDecodeSourceDataOfMIMEType:(NSString *)encodingType { - // We don't want the download system to extract our gzips. + // We don't want the download system to extract our gzips for persistent downloads // Note that we use a substring matching here instead of direct comparison because the docs say "application/gzip" but the system *uses* "application/x-gzip". This is a documentation bug. - return ([encodingType rangeOfString:@"gzip"].location == NSNotFound); + return (self.mode == SPUDownloadModeTemporary || [encodingType rangeOfString:@"gzip"].location == NSNotFound); } @end diff --git a/PersistentDownloader/SUPersistentDownloaderDelegate.h b/PersistentDownloader/SUPersistentDownloaderDelegate.h index 35240613..1578ef72 100644 --- a/PersistentDownloader/SUPersistentDownloaderDelegate.h +++ b/PersistentDownloader/SUPersistentDownloaderDelegate.h @@ -10,8 +10,11 @@ NS_ASSUME_NONNULL_BEGIN +@class SPUDownloadData; + @protocol SUPersistentDownloaderDelegate +// This is only invoked for persistent downloads - (void)downloaderDidSetDestinationName:(NSString *)destinationName temporaryDirectory:(NSString *)temporaryDirectory; // Under rare cases, this may be called more than once, in which case the current progress should be reset back to 0 @@ -19,7 +22,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)downloaderDidReceiveDataOfLength:(NSUInteger)length; -- (void)downloaderDidFinishDownloading; +// downloadData is nil if this is a persisent download +- (void)downloaderDidFinishWithTemporaryDownloadData:(SPUDownloadData * _Nullable)downloadData; - (void)downloaderDidFailWithError:(NSError *)error; diff --git a/PersistentDownloader/SUPersistentDownloaderProtocol.h b/PersistentDownloader/SUPersistentDownloaderProtocol.h index ae520375..6578560a 100644 --- a/PersistentDownloader/SUPersistentDownloaderProtocol.h +++ b/PersistentDownloader/SUPersistentDownloaderProtocol.h @@ -12,12 +12,16 @@ NS_ASSUME_NONNULL_BEGIN @class SPUURLRequest; +typedef NS_ENUM(NSUInteger, SPUDownloadMode) +{ + SPUDownloadModePersistent, + SPUDownloadModeTemporary +}; + // 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 SUPersistentDownloaderProtocol -- (void)startDownloadWithRequest:(SPUURLRequest *)request bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename; - -- (void)cleanup; +- (void)startDownloadWithRequest:(SPUURLRequest *)request mode:(SPUDownloadMode)mode bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename; @end diff --git a/Sparkle/SUDownloadDriver.m b/Sparkle/SUDownloadDriver.m index 70529fca..9bd45b4e 100644 --- a/Sparkle/SUDownloadDriver.m +++ b/Sparkle/SUDownloadDriver.m @@ -18,6 +18,7 @@ #import "SUErrors.h" #import "SPUURLRequest.h" #import "SUDownloadedUpdate.h" +#import "SPUDownloadData.h" #ifdef _APPKITDEFINES_H #error This is a "core" class and should NOT import AppKit @@ -118,15 +119,13 @@ [self.delegate downloadDriverWillBeginDownload]; NSString *desiredFilename = [NSString stringWithFormat:@"%@ %@", [self.host name], [self.updateItem versionString]]; - [self.downloader startDownloadWithRequest:[SPUURLRequest URLRequestWithRequest:self.request] bundleIdentifier:bundleIdentifier desiredFilename:desiredFilename]; + [self.downloader startDownloadWithRequest:[SPUURLRequest URLRequestWithRequest:self.request] mode:SPUDownloadModePersistent bundleIdentifier:bundleIdentifier desiredFilename:desiredFilename]; } - (void)cleanup { self.cleaningUp = YES; - [self.downloader cleanup]; - if (self.connection != nil) { [self.connection invalidate]; self.connection = nil; @@ -135,7 +134,7 @@ self.downloader = nil; } -- (void)downloaderDidFinishDownloading +- (void)downloaderDidFinishWithTemporaryDownloadData:(SPUDownloadData * _Nullable)__unused downloadData { dispatch_async(dispatch_get_main_queue(), ^{ self.retrievedDownloadResult = YES;