diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index c74dae2f..57ef529e 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -61,6 +61,8 @@ 34074B9F1FEABD5A001CB3A5 /* SPUDownloadData.h in Headers */ = {isa = PBXBuildFile; fileRef = 34074B9D1FEABD5A001CB3A5 /* SPUDownloadData.h */; }; 34074BA21FEABFD2001CB3A5 /* SPUDownloaderSession.h in Headers */ = {isa = PBXBuildFile; fileRef = 34074BA01FEABFD2001CB3A5 /* SPUDownloaderSession.h */; }; 34074BA31FEABFD2001CB3A5 /* SPUDownloaderSession.m in Sources */ = {isa = PBXBuildFile; fileRef = 34074BA11FEABFD2001CB3A5 /* SPUDownloaderSession.m */; }; + 34074BA71FEAC417001CB3A5 /* SPUDownloaderDeprecated.h in Headers */ = {isa = PBXBuildFile; fileRef = 34074BA51FEAC417001CB3A5 /* SPUDownloaderDeprecated.h */; }; + 34074BA81FEAC417001CB3A5 /* SPUDownloaderDeprecated.m in Sources */ = {isa = PBXBuildFile; fileRef = 34074BA61FEAC417001CB3A5 /* SPUDownloaderDeprecated.m */; }; 3772FEA913DE0B6B00F79537 /* SUVersionDisplayProtocol.h in Headers */ = {isa = PBXBuildFile; fileRef = 3772FEA813DE0B6B00F79537 /* SUVersionDisplayProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; }; 55C14BD4136EEFCE00649790 /* Autoupdate.m in Sources */ = {isa = PBXBuildFile; fileRef = 55C14BD3136EEFCE00649790 /* Autoupdate.m */; }; 55C14BD9136EF00C00649790 /* SUStatus.xib in Resources */ = {isa = PBXBuildFile; fileRef = 55C14BD8136EF00C00649790 /* SUStatus.xib */; }; @@ -545,6 +547,8 @@ 34074BA01FEABFD2001CB3A5 /* SPUDownloaderSession.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SPUDownloaderSession.h; sourceTree = ""; }; 34074BA11FEABFD2001CB3A5 /* SPUDownloaderSession.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SPUDownloaderSession.m; sourceTree = ""; }; 34074BA41FEAC0BC001CB3A5 /* SPUDownloader_Private.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SPUDownloader_Private.h; sourceTree = ""; }; + 34074BA51FEAC417001CB3A5 /* SPUDownloaderDeprecated.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SPUDownloaderDeprecated.h; sourceTree = ""; }; + 34074BA61FEAC417001CB3A5 /* SPUDownloaderDeprecated.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SPUDownloaderDeprecated.m; sourceTree = ""; }; 3772FEA813DE0B6B00F79537 /* SUVersionDisplayProtocol.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUVersionDisplayProtocol.h; sourceTree = ""; }; 4607BEA21948443800EF8DA4 /* nb */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = nb; path = nb.lproj/Sparkle.strings; sourceTree = ""; }; 4607BEA31948443800EF8DA4 /* nb */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = nb; path = nb.lproj/SUAutomaticUpdateAlert.xib; sourceTree = ""; }; @@ -1113,6 +1117,8 @@ 34074B8D1FEABD08001CB3A5 /* SPUDownloader.h */, 34074B8C1FEABD08001CB3A5 /* SPUDownloader.m */, 34074BA41FEAC0BC001CB3A5 /* SPUDownloader_Private.h */, + 34074BA51FEAC417001CB3A5 /* SPUDownloaderDeprecated.h */, + 34074BA61FEAC417001CB3A5 /* SPUDownloaderDeprecated.m */, 34074BA01FEABFD2001CB3A5 /* SPUDownloaderSession.h */, 34074BA11FEABFD2001CB3A5 /* SPUDownloaderSession.m */, 34074B8E1FEABD08001CB3A5 /* SPUDownloaderDelegate.h */, @@ -1429,6 +1435,7 @@ 61299B3609CB04E000B7442F /* Sparkle.h in Headers */, 61B5FC0D09C4FC8200B25A18 /* SUAppcast.h in Headers */, 61B5FC7009C51F4A00B25A18 /* SUAppcastItem.h in Headers */, + 34074BA71FEAC417001CB3A5 /* SPUDownloaderDeprecated.h in Headers */, 72316BDE1E0E17910039EFD9 /* SUApplicationInfo.h in Headers */, 6120721209CC5C4B007FE0F6 /* SUAutomaticUpdateAlert.h in Headers */, 34074B921FEABD08001CB3A5 /* SPUDownloaderDelegate.h in Headers */, @@ -2081,6 +2088,7 @@ 618FA5060DAE8AB80026945C /* SUPlainInstaller.m in Sources */, 6101347C0DD2541A0049ACDF /* SUProbingUpdateDriver.m in Sources */, 61B93C0A0DD112FF00DCD2F8 /* SUScheduledUpdateDriver.m in Sources */, + 34074BA81FEAC417001CB3A5 /* SPUDownloaderDeprecated.m in Sources */, 61A225A50D1C4AC000430CCD /* SUStandardVersionComparator.m in Sources */, 6196CFFA09C72149000DC222 /* SUStatusController.m in Sources */, 61A2279D0D1CEE7600430CCD /* SUSystemProfiler.m in Sources */, diff --git a/Sparkle/SPUDownloader.m b/Sparkle/SPUDownloader.m index 8e3d776f..e863a4e7 100755 --- a/Sparkle/SPUDownloader.m +++ b/Sparkle/SPUDownloader.m @@ -18,14 +18,11 @@ @interface SPUDownloader () -@property (nonatomic) NSURLSession *downloadSession; - @end @implementation SPUDownloader @synthesize delegate = _delegate; -@synthesize download = _download; @synthesize bundleIdentifier = _bundleIdentifier; @synthesize desiredFilename = _desiredFilename; @synthesize downloadFilename = _downloadFilename; @@ -42,16 +39,6 @@ return self; } -- (void)startDownloadWithRequest:(SPUURLRequest *)request -{ - self.downloadSession = [NSURLSession - sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] - delegate:self - delegateQueue:[NSOperationQueue mainQueue]]; - self.download = [self.downloadSession downloadTaskWithRequest:request.request]; - [self.download resume]; -} - // Don't implement dealloc - make the client call cleanup, which is the only way to remove the reference cycle from the delegate anyway - (void)startPersistentDownloadWithRequest:(SPUURLRequest *)request bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename @@ -73,9 +60,6 @@ - (void)cleanup { [self enableAutomaticTermination]; - [self.download cancel]; - self.download = nil; - self.downloadSession = nil; self.delegate = nil; if (self.mode == SPUDownloadModeTemporary && self.downloadFilename != nil) @@ -85,26 +69,13 @@ } } -- (void)downloadDidFinish + +-(void)downloadDidFinishWithData:(SPUDownloadData*)data { - assert(self.downloadFilename != nil); - - SPUDownloadData *downloadData = nil; - if (self.mode == SPUDownloadModeTemporary) { - NSData *data = [NSData dataWithContentsOfFile:self.downloadFilename]; - if (data != nil) { - NSURLResponse *response = self.download.response; - assert(response != nil); - downloadData = [[SPUDownloadData alloc] initWithData:data textEncodingName:response.textEncodingName MIMEType:response.MIMEType]; - } - } - - self.download = nil; - switch (self.mode) { case SPUDownloadModeTemporary: - if (downloadData != nil) { - [self.delegate downloaderDidFinishWithTemporaryDownloadData:downloadData]; + if (data != nil) { + [self.delegate downloaderDidFinishWithTemporaryDownloadData:data]; } else { [self.delegate downloaderDidFailWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to read temporary downloaded data from %@", self.downloadFilename]}]]; } @@ -113,8 +84,6 @@ [self.delegate downloaderDidFinishWithTemporaryDownloadData:nil]; break; } - - [self cleanup]; } @end diff --git a/Sparkle/SPUDownloaderDeprecated.h b/Sparkle/SPUDownloaderDeprecated.h new file mode 100644 index 00000000..e667ef1b --- /dev/null +++ b/Sparkle/SPUDownloaderDeprecated.h @@ -0,0 +1,13 @@ +// +// SPUDownloaderDeprecated.h +// Sparkle +// +// Created by School of Computing Macbook on 12/20/17. +// Copyright © 2017 Sparkle Project. All rights reserved. +// + +#import "SPUDownloader.h" + +@interface SPUDownloaderDeprecated : SPUDownloader + +@end diff --git a/Sparkle/SPUDownloaderDeprecated.m b/Sparkle/SPUDownloaderDeprecated.m new file mode 100644 index 00000000..8ae29dbb --- /dev/null +++ b/Sparkle/SPUDownloaderDeprecated.m @@ -0,0 +1,177 @@ +// +// SPUDownloaderDeprecated.m +// Sparkle +// +// Created by School of Computing Macbook on 12/20/17. +// Copyright © 2017 Sparkle Project. All rights reserved. +// + +#import "SPUDownloaderDeprecated.h" +#import "SPUURLRequest.h" +#import "SPUDownloader_Private.h" +#import "SPUDownloadData.h" +#import "SPULocalCacheDirectory.h" +#import "SUErrors.h" + +@interface SPUDownloaderDeprecated () + +@property (nonatomic) NSURLDownload *download; +@property (nonatomic) NSURLResponse *response; + +@end + +@implementation SPUDownloaderDeprecated + +@synthesize download = _download; +@synthesize response = _response; + +- (void)startPersistentDownloadWithRequest:(SPUURLRequest *)request bundleIdentifier:(NSString *)bundleIdentifier desiredFilename:(NSString *)desiredFilename +{ + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.download == nil && self.delegate != nil) { + // Prevent service from automatically terminating while downloading the update asynchronously without any reply blocks + [[NSProcessInfo processInfo] disableAutomaticTermination:SUDownloadingReason]; + self.disabledAutomaticTermination = YES; + + self.mode = SPUDownloadModePersistent; + self.desiredFilename = desiredFilename; + self.bundleIdentifier = bundleIdentifier; + + self.download = [[NSURLDownload alloc] initWithRequest:request.request delegate:self]; + } + }); +} + +- (void)startTemporaryDownloadWithRequest:(SPUURLRequest *)request +{ + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.download == nil && self.delegate != nil) { + // Prevent service from automatically terminating while downloading the update asynchronously without any reply blocks + [[NSProcessInfo processInfo] disableAutomaticTermination:SUDownloadingReason]; + self.disabledAutomaticTermination = YES; + + self.mode = SPUDownloadModeTemporary; + self.download = [[NSURLDownload alloc] initWithRequest:request.request delegate:self]; + } + }); +} + +- (void)download:(NSURLDownload *)__unused d decideDestinationWithSuggestedFilename:(NSString *)name +{ + if (self.mode == SPUDownloadModeTemporary) + { + // 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"]; + + [SPULocalCacheDirectory removeOldItemsInDirectory:rootPersistentDownloadCachePath]; + + 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 { + 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] }]; + + [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; + + if (self.mode == SPUDownloadModePersistent) { + // 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 + [self.delegate downloaderDidReceiveExpectedContentLength:response.expectedContentLength]; + } +} + +- (void)download:(NSURLDownload *)__unused download didReceiveDataOfLength:(NSUInteger)length +{ + if (self.mode == SPUDownloadModePersistent) { + [self.delegate downloaderDidReceiveDataOfLength:length]; + } +} + +- (void)downloadDidFinish:(NSURLDownload *)__unused d +{ + [self downloadDidFinish]; +} + +- (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 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 (self.mode == SPUDownloadModeTemporary || [encodingType rangeOfString:@"gzip"].location == NSNotFound); +} + +- (void)downloadDidFinish +{ + 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; + + [super downloadDidFinishWithData:downloadData]; +} + +-(void)cleanup +{ + [self.download cancel]; + self.download = nil; + self.response = nil; + [super cleanup]; +} + +@end diff --git a/Sparkle/SPUDownloaderProtocol.h b/Sparkle/SPUDownloaderProtocol.h index 72982cfd..d844eb84 100755 --- a/Sparkle/SPUDownloaderProtocol.h +++ b/Sparkle/SPUDownloaderProtocol.h @@ -19,6 +19,10 @@ NS_ASSUME_NONNULL_BEGIN - (void)startTemporaryDownloadWithRequest:(SPUURLRequest *)request; +- (void)downloadDidFinish; + +- (void)cleanup; + @end NS_ASSUME_NONNULL_END diff --git a/Sparkle/SPUDownloaderSession.h b/Sparkle/SPUDownloaderSession.h index b8a25bf1..3ca1754e 100644 --- a/Sparkle/SPUDownloaderSession.h +++ b/Sparkle/SPUDownloaderSession.h @@ -10,6 +10,6 @@ #import "SPUDownloader.h" #import "SPUDownloaderProtocol.h" -@interface SPUDownloaderSession : SPUDownloader +@interface SPUDownloaderSession : SPUDownloader @end diff --git a/Sparkle/SPUDownloaderSession.m b/Sparkle/SPUDownloaderSession.m index d9abbcf4..aae8a76e 100644 --- a/Sparkle/SPUDownloaderSession.m +++ b/Sparkle/SPUDownloaderSession.m @@ -11,16 +11,19 @@ #import "SPUDownloader_Private.h" #import "SPULocalCacheDirectory.h" #import "SUErrors.h" +#import "SPUDownloadData.h" @interface SPUDownloaderSession () @property (nonatomic) NSURLSession *downloadSession; +@property (nonatomic) NSURLSessionDownloadTask *download; @end @implementation SPUDownloaderSession @synthesize downloadSession = _downloadSession; +@synthesize download = _download; - (void)startDownloadWithRequest:(SPUURLRequest *)request { @@ -136,6 +139,33 @@ [self cleanup]; } +- (void)downloadDidFinish +{ + assert(self.downloadFilename != nil); + + SPUDownloadData *downloadData = nil; + if (self.mode == SPUDownloadModeTemporary) { + NSData *data = [NSData dataWithContentsOfFile:self.downloadFilename]; + if (data != nil) { + NSURLResponse *response = self.download.response; + assert(response != nil); + downloadData = [[SPUDownloadData alloc] initWithData:data textEncodingName:response.textEncodingName MIMEType:response.MIMEType]; + } + } + + self.download = nil; + + [super downloadDidFinishWithData:downloadData]; +} + +-(void)cleanup +{ + [self.download cancel]; + self.download = nil; + self.downloadSession = nil; + [super cleanup]; +} + // NSURLDownload has a [downlaod:shouldDecodeSourceDataOfMIMEType:] to determine if the data should be decoded. // This does not exist for NSURLSessionDownloadTask and appears unnecessary. Data tasks will decode data, but not download tasks. diff --git a/Sparkle/SPUDownloader_Private.h b/Sparkle/SPUDownloader_Private.h index 254c7846..c679c881 100644 --- a/Sparkle/SPUDownloader_Private.h +++ b/Sparkle/SPUDownloader_Private.h @@ -23,7 +23,6 @@ static NSString *SUDownloadingReason = @"Downloading update related file"; // Delegate is intentionally strongly referenced; see header @property (nonatomic) id delegate; -@property (nonatomic) NSURLSessionDownloadTask *download; @property (nonatomic, copy) NSString *bundleIdentifier; @property (nonatomic, copy) NSString *desiredFilename; @property (nonatomic, copy) NSString *downloadFilename; @@ -31,7 +30,7 @@ static NSString *SUDownloadingReason = @"Downloading update related file"; @property (nonatomic) SPUDownloadMode mode; @property (nonatomic) BOOL receivedExpectedBytes; --(void)downloadDidFinish; +-(void)downloadDidFinishWithData:(SPUDownloadData*)data; @end