From 65d78d5129a4cd6a6cd5f8ee33f3afcbb8a840b6 Mon Sep 17 00:00:00 2001 From: Zorg Date: Wed, 10 Aug 2016 22:00:16 -0400 Subject: [PATCH] Pass text encoding and MIME type to user driver for release notes This fixes issues where the user driver may not be displaying release notes properly because it doesn't know what kind of data was downloaded (eg: html vs plain text). --- Sparkle.xcodeproj/project.pbxproj | 12 ++++ Sparkle/SPUStandardUserDriver.m | 4 +- Sparkle/SPUURLDownload.h | 4 +- Sparkle/SPUURLDownload.m | 9 +-- Sparkle/SPUUserDriver.h | 6 +- Sparkle/SUAppcast.m | 7 ++- Sparkle/SUUIBasedUpdateDriver.m | 8 ++- Sparkle/SUUpdateAlert.h | 2 +- Sparkle/SUUpdateAlert.m | 9 ++- TemporaryDownloader/SPUTemporaryDownload.h | 23 +++++++ TemporaryDownloader/SPUTemporaryDownload.m | 63 +++++++++++++++++++ TemporaryDownloader/SUTemporaryDownloader.m | 26 +++++++- .../SUTemporaryDownloaderProtocol.h | 3 +- TestApplication/SUPopUpTitlebarUserDriver.m | 2 +- Tests/SUUpdaterTest.m | 2 +- sparkle-cli/SPUCommandLineUserDriver.m | 25 ++++++-- 16 files changed, 177 insertions(+), 28 deletions(-) create mode 100644 TemporaryDownloader/SPUTemporaryDownload.h create mode 100644 TemporaryDownloader/SPUTemporaryDownload.m diff --git a/Sparkle.xcodeproj/project.pbxproj b/Sparkle.xcodeproj/project.pbxproj index 9b44883a..65666fb7 100644 --- a/Sparkle.xcodeproj/project.pbxproj +++ b/Sparkle.xcodeproj/project.pbxproj @@ -325,6 +325,10 @@ 72F9EBE31D517E2F004AC8B6 /* SUUpdater.m in Sources */ = {isa = PBXBuildFile; fileRef = 72F9EBE11D517E2F004AC8B6 /* SUUpdater.m */; }; 72F9EC341D5BF542004AC8B6 /* SUFileManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 7267E5E41D3D90AA00D1BF90 /* SUFileManager.m */; }; 72F9EC351D5BF54E004AC8B6 /* SUFileManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 7267E5E41D3D90AA00D1BF90 /* SUFileManager.m */; }; + 72F9EC381D5C04A9004AC8B6 /* SPUTemporaryDownload.h in Headers */ = {isa = PBXBuildFile; fileRef = 72F9EC361D5C04A9004AC8B6 /* SPUTemporaryDownload.h */; }; + 72F9EC3A1D5C04A9004AC8B6 /* SPUTemporaryDownload.m in Sources */ = {isa = PBXBuildFile; fileRef = 72F9EC371D5C04A9004AC8B6 /* SPUTemporaryDownload.m */; }; + 72F9EC3B1D5C15D0004AC8B6 /* SUConstants.m in Sources */ = {isa = PBXBuildFile; fileRef = 61299A5F09CA6EB100B7442F /* SUConstants.m */; }; + 72F9EC3C1D5C15EF004AC8B6 /* SPUTemporaryDownload.m in Sources */ = {isa = PBXBuildFile; fileRef = 72F9EC371D5C04A9004AC8B6 /* SPUTemporaryDownload.m */; }; C23E885B1BE7B24F0050BB73 /* SparkleTestCodeSignApp.enc.dmg in Resources */ = {isa = PBXBuildFile; fileRef = C23E88591BE7AF890050BB73 /* SparkleTestCodeSignApp.enc.dmg */; }; F8761EB11ADC5068000C9034 /* SUCodeSigningVerifierTest.m in Sources */ = {isa = PBXBuildFile; fileRef = F8761EB01ADC5068000C9034 /* SUCodeSigningVerifierTest.m */; }; F8761EB31ADC50EB000C9034 /* SparkleTestCodeSignApp.zip in Resources */ = {isa = PBXBuildFile; fileRef = F8761EB21ADC50EB000C9034 /* SparkleTestCodeSignApp.zip */; }; @@ -1023,6 +1027,8 @@ 72F94F571CC44DE1002DEE68 /* SPUXPCServiceInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPUXPCServiceInfo.m; sourceTree = ""; }; 72F9EBE01D517E2F004AC8B6 /* SUUpdater.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUUpdater.h; sourceTree = ""; }; 72F9EBE11D517E2F004AC8B6 /* SUUpdater.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SUUpdater.m; sourceTree = ""; }; + 72F9EC361D5C04A9004AC8B6 /* SPUTemporaryDownload.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SPUTemporaryDownload.h; path = TemporaryDownloader/SPUTemporaryDownload.h; sourceTree = SOURCE_ROOT; }; + 72F9EC371D5C04A9004AC8B6 /* SPUTemporaryDownload.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SPUTemporaryDownload.m; path = TemporaryDownloader/SPUTemporaryDownload.m; sourceTree = SOURCE_ROOT; }; 8DC2EF5A0486A6940098B216 /* Sparkle-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Sparkle-Info.plist"; sourceTree = ""; }; 8DC2EF5B0486A6940098B216 /* Sparkle.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Sparkle.framework; sourceTree = BUILT_PRODUCTS_DIR; }; A5BF4F1B1BC7668B007A052A /* SUTestWebServer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SUTestWebServer.h; sourceTree = ""; }; @@ -1720,6 +1726,8 @@ 724A0A351CF7AEEA00D8C3D5 /* main.m */, 724A0A371CF7AEEA00D8C3D5 /* SUTemporaryDownloader.h */, 724A0A381CF7AEEA00D8C3D5 /* SUTemporaryDownloader.m */, + 72F9EC361D5C04A9004AC8B6 /* SPUTemporaryDownload.h */, + 72F9EC371D5C04A9004AC8B6 /* SPUTemporaryDownload.m */, 724A0A391CF7AEEA00D8C3D5 /* SUTemporaryDownloaderProtocol.h */, ); name = TemporaryDownloader; @@ -1898,6 +1906,7 @@ 61B5F8ED09C4CE3C00B25A18 /* SPUUpdater.h in Headers */, 61A2259E0D1C495D00430CCD /* SUVersionComparisonProtocol.h in Headers */, 3772FEA913DE0B6B00F79537 /* SUVersionDisplayProtocol.h in Headers */, + 72F9EC381D5C04A9004AC8B6 /* SPUTemporaryDownload.h in Headers */, 7267E5E51D3D90AA00D1BF90 /* SUFileManager.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; @@ -2734,6 +2743,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 72F9EC3B1D5C15D0004AC8B6 /* SUConstants.m in Sources */, + 72F9EC3A1D5C04A9004AC8B6 /* SPUTemporaryDownload.m in Sources */, 724A0A3B1CF7AEEA00D8C3D5 /* main.m in Sources */, 7293A1B01CEE9B9800B957A7 /* SPUURLRequest.m in Sources */, 724A0A3D1CF7AEEA00D8C3D5 /* SUTemporaryDownloader.m in Sources */, @@ -2794,6 +2805,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 72F9EC3C1D5C15EF004AC8B6 /* SPUTemporaryDownload.m in Sources */, 7267E5DF1D3D8FFA00D1BF90 /* SPUInstallationInputData.m in Sources */, 724BB3B01D3482BA005D534A /* SPUSystemAuthorization.m in Sources */, 724BB3AA1D3347C2005D534A /* SUInstallerStatus.m in Sources */, diff --git a/Sparkle/SPUStandardUserDriver.m b/Sparkle/SPUStandardUserDriver.m index ca1119c4..a61358b3 100644 --- a/Sparkle/SPUStandardUserDriver.m +++ b/Sparkle/SPUStandardUserDriver.m @@ -188,10 +188,10 @@ }]; } -- (void)showUpdateReleaseNotes:(NSData *)releaseNotes +- (void)showUpdateReleaseNotesWithData:(NSData *)releaseNotesData textEncodingName:(NSString * _Nullable)textEncodingName MIMEType:(NSString * _Nullable)MIMEType { dispatch_async(dispatch_get_main_queue(), ^{ - [self.activeUpdateAlert showReleaseNotes:releaseNotes]; + [self.activeUpdateAlert showUpdateReleaseNotesWithData:releaseNotesData textEncodingName:textEncodingName MIMEType:MIMEType]; }); } diff --git a/Sparkle/SPUURLDownload.h b/Sparkle/SPUURLDownload.h index 30ce2de8..8651488e 100644 --- a/Sparkle/SPUURLDownload.h +++ b/Sparkle/SPUURLDownload.h @@ -10,8 +10,10 @@ NS_ASSUME_NONNULL_BEGIN +@class SPUTemporaryDownload; + // From the completion block, either data or error can be nil, but not both // The completion block returns on the main queue -void SPUDownloadURLWithRequest(NSURLRequest * request, void (^completionBlock)(NSData * _Nullable data, NSError * _Nullable error)); +void SPUDownloadURLWithRequest(NSURLRequest * request, void (^completionBlock)(SPUTemporaryDownload * _Nullable download, NSError * _Nullable error)); NS_ASSUME_NONNULL_END diff --git a/Sparkle/SPUURLDownload.m b/Sparkle/SPUURLDownload.m index b24c1000..92121c15 100644 --- a/Sparkle/SPUURLDownload.m +++ b/Sparkle/SPUURLDownload.m @@ -11,9 +11,10 @@ #import "SUTemporaryDownloader.h" #import "SUTemporaryDownloaderProtocol.h" #import "SPUURLRequest.h" +#import "SPUTemporaryDownload.h" #import "SUErrors.h" -void SPUDownloadURLWithRequest(NSURLRequest * request, void (^completionBlock)(NSData * _Nullable, NSError * _Nullable)) +void SPUDownloadURLWithRequest(NSURLRequest * request, void (^completionBlock)(SPUTemporaryDownload * _Nullable, NSError * _Nullable)) { id downloader = nil; NSXPCConnection *connection = nil; @@ -57,15 +58,15 @@ void SPUDownloadURLWithRequest(NSURLRequest * request, void (^completionBlock)(N downloader = connection.remoteObjectProxy; } - [downloader startDownloadWithRequest:[SPUURLRequest URLRequestWithRequest:request] completion:^(NSData * _Nullable data, NSError * _Nullable error) { + [downloader startDownloadWithRequest:[SPUURLRequest URLRequestWithRequest:request] completion:^(SPUTemporaryDownload * _Nullable download, NSError * _Nullable error) { dispatch_async(dispatch_get_main_queue(), ^{ retrievedDownloadResult = YES; [connection invalidate]; - if (data == nil) { + if (download == nil || download.data == nil) { completionBlock(nil, error); } else { - completionBlock(data, nil); + completionBlock(download, nil); } }); }]; diff --git a/Sparkle/SPUUserDriver.h b/Sparkle/SPUUserDriver.h index 1c33fc21..856a6515 100644 --- a/Sparkle/SPUUserDriver.h +++ b/Sparkle/SPUUserDriver.h @@ -161,11 +161,13 @@ SU_EXPORT @protocol SPUUserDriver * This is only applicable if the release notes are linked from the appcast, and are not directly embedded inside of the appcast file. * That is, this may be invoked if the releaseNotesURL from the appcast item is non-nil. * - * @param releaseNotes The data for the release notes that was downloaded from the new update's appcast. + * @param releaseNotesData The data for the release notes that was downloaded from the new update's appcast. + * @param textEncodingName The text encoding name (eg: "utf-8"). This may be nil if one wasn't retrieved. + * @param MIMEType The MIME type that was retrieved from the download. This may be nil if one wasn't retrieved. * * This can be called from any thread */ -- (void)showUpdateReleaseNotes:(NSData *)releaseNotes; +- (void)showUpdateReleaseNotesWithData:(NSData *)releaseNotesData textEncodingName:(NSString * _Nullable)textEncodingName MIMEType:(NSString * _Nullable)MIMEType; /*! * Show the user that the new update's release notes could not be downloaded diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index 63c195e3..de9e9368 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -15,6 +15,7 @@ #import "SULocalizations.h" #import "SPUXPCServiceInfo.h" #import "SPUURLDownload.h" +#import "SPUTemporaryDownload.h" #ifdef _APPKITDEFINES_H #error This is a "core" class and should NOT import AppKit @@ -76,10 +77,10 @@ [request setValue:@"application/rss+xml,*/*;q=0.1" forHTTPHeaderField:@"Accept"]; - SPUDownloadURLWithRequest(request, ^(NSData * _Nullable data, NSError * _Nullable error) { - if (data != nil) { + SPUDownloadURLWithRequest(request, ^(SPUTemporaryDownload * _Nullable download, NSError * _Nullable error) { + if (download != nil) { NSError *parseError = nil; - NSArray *appcastItems = [self parseAppcastItemsFromXMLData:data error:&parseError]; + NSArray *appcastItems = [self parseAppcastItemsFromXMLData:download.data error:&parseError]; if (appcastItems != nil) { self.items = appcastItems; diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 9ce94e4d..a0968b16 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -15,6 +15,7 @@ #import "SUAppcastItem.h" #import "SUErrors.h" #import "SPUURLDownload.h" +#import "SPUTemporaryDownload.h" #ifdef _APPKITDEFINES_H #error This is a "core" class and should NOT import AppKit @@ -144,9 +145,10 @@ NSURLRequest *request = [NSURLRequest requestWithURL:updateItem.releaseNotesURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30]; id userDriver = self.userDriver; - SPUDownloadURLWithRequest(request, ^(NSData * _Nullable data, NSError * _Nullable error) { - if (data != nil) { - [userDriver showUpdateReleaseNotes:(NSData * _Nonnull)data]; + SPUDownloadURLWithRequest(request, ^(SPUTemporaryDownload * _Nullable download, NSError * _Nullable error) { + if (download != nil) { + SPUTemporaryDownload *nonnullDownload = download; + [userDriver showUpdateReleaseNotesWithData:nonnullDownload.data textEncodingName:nonnullDownload.textEncoding MIMEType:nonnullDownload.MIMEType]; } else { [userDriver showUpdateReleaseNotesFailedToDownloadWithError:(NSError * _Nonnull)error]; } diff --git a/Sparkle/SUUpdateAlert.h b/Sparkle/SUUpdateAlert.h index 63325b05..ca16978c 100644 --- a/Sparkle/SUUpdateAlert.h +++ b/Sparkle/SUUpdateAlert.h @@ -24,7 +24,7 @@ - (instancetype)initWithAppcastItem:(SUAppcastItem *)item host:(SUHost *)aHost versionDisplayer:(id )aVersionDisplayer resumableCompletionBlock:(void (^)(SUInstallUpdateStatus))block; -- (void)showReleaseNotes:(NSData *)releaseNotes; +- (void)showUpdateReleaseNotesWithData:(NSData *)releaseNotesData textEncodingName:(NSString *)textEncodingName MIMEType:(NSString *)MIMEType; - (void)showReleaseNotesFailedToDownload; - (IBAction)installUpdate:sender; diff --git a/Sparkle/SUUpdateAlert.m b/Sparkle/SUUpdateAlert.m index 81e9a3af..dc502a11 100644 --- a/Sparkle/SUUpdateAlert.m +++ b/Sparkle/SUUpdateAlert.m @@ -199,11 +199,16 @@ } } -- (void)showReleaseNotes:(NSData *)releaseNotes +- (void)showUpdateReleaseNotesWithData:(NSData *)releaseNotesData textEncodingName:(NSString * _Nullable)textEncodingName MIMEType:(NSString * _Nullable)MIMEType { if (!self.webViewFinishedLoading) { NSURL *baseURL = self.updateItem.releaseNotesURL.URLByDeletingLastPathComponent; - [[self.releaseNotesView mainFrame] loadData:releaseNotes MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:baseURL]; + // If a MIME type isn't provided, we will pick html as the default, as opposed to plain text. Questionable decision.. + NSString *chosenMIMEType = (MIMEType != nil) ? MIMEType : @"text/html"; + // We'll pick utf-8 as the default text encoding name if one isn't provided which I think is reasonable + NSString *chosenTextEncodingName = (textEncodingName != nil) ? textEncodingName : @"utf-8"; + + [[self.releaseNotesView mainFrame] loadData:releaseNotesData MIMEType:chosenMIMEType textEncodingName:chosenTextEncodingName baseURL:baseURL]; } } diff --git a/TemporaryDownloader/SPUTemporaryDownload.h b/TemporaryDownloader/SPUTemporaryDownload.h new file mode 100644 index 00000000..a91c507b --- /dev/null +++ b/TemporaryDownloader/SPUTemporaryDownload.h @@ -0,0 +1,23 @@ +// +// SPUTemporaryDownload.h +// Sparkle +// +// Created by Mayur Pawashe on 8/10/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface SPUTemporaryDownload : NSObject + +- (instancetype)initWithData:(NSData *)data textEncoding:(NSString * _Nullable)textEncoding MIMEType:(NSString * _Nullable)MIMEType; + +@property (nonatomic, readonly) NSData *data; +@property (nonatomic, readonly, nullable, copy) NSString *textEncoding; +@property (nonatomic, readonly, nullable, copy) NSString *MIMEType; + +@end + +NS_ASSUME_NONNULL_END diff --git a/TemporaryDownloader/SPUTemporaryDownload.m b/TemporaryDownloader/SPUTemporaryDownload.m new file mode 100644 index 00000000..a746b515 --- /dev/null +++ b/TemporaryDownloader/SPUTemporaryDownload.m @@ -0,0 +1,63 @@ +// +// SPUTemporaryDownload.m +// Sparkle +// +// Created by Mayur Pawashe on 8/10/16. +// Copyright © 2016 Sparkle Project. All rights reserved. +// + +#import "SPUTemporaryDownload.h" + +static NSString *SPUTemporaryDownloadDataKey = @"SPUTemporaryDownloadData"; +static NSString *SPUTemporaryDownloadTextEncodingKey = @"SPUTemporaryDownloadTextEncoding"; +static NSString *SPUTemporaryDownloadMIMETypeKey = @"SPUTemporaryDownloadMIMEType"; + +@implementation SPUTemporaryDownload + +@synthesize data = _data; +@synthesize textEncoding = _textEncoding; +@synthesize MIMEType = _MIMEType; + ++ (BOOL)supportsSecureCoding +{ + return YES; +} + +- (instancetype)initWithData:(NSData *)data textEncoding:(NSString * _Nullable)textEncoding MIMEType:(NSString *)MIMEType +{ + self = [super init]; + if (self != nil) { + _data = data; + _textEncoding = textEncoding; + _MIMEType = MIMEType; + } + return self; +} + +- (void)encodeWithCoder:(NSCoder *)coder +{ + [coder encodeObject:self.data forKey:SPUTemporaryDownloadDataKey]; + + if (self.textEncoding != nil) { + [coder encodeObject:self.textEncoding forKey:SPUTemporaryDownloadTextEncodingKey]; + } + + if (self.MIMEType != nil) { + [coder encodeObject:self.MIMEType forKey:SPUTemporaryDownloadMIMETypeKey]; + } +} + +- (nullable instancetype)initWithCoder:(NSCoder *)decoder +{ + NSData *data = [decoder decodeObjectOfClass:[NSData class] forKey:SPUTemporaryDownloadDataKey]; + if (data == nil) { + return nil; + } + + NSString *textEncoding = [decoder decodeObjectOfClass:[NSString class] forKey:SPUTemporaryDownloadTextEncodingKey]; + NSString *MIMEType = [decoder decodeObjectOfClass:[NSString class] forKey:SPUTemporaryDownloadMIMETypeKey]; + + return [self initWithData:data textEncoding:textEncoding MIMEType:MIMEType]; +} + +@end diff --git a/TemporaryDownloader/SUTemporaryDownloader.m b/TemporaryDownloader/SUTemporaryDownloader.m index 86a73e76..6efdd6c4 100644 --- a/TemporaryDownloader/SUTemporaryDownloader.m +++ b/TemporaryDownloader/SUTemporaryDownloader.m @@ -7,13 +7,17 @@ // #import "SUTemporaryDownloader.h" +#import "SPUTemporaryDownload.h" #import "SPUURLRequest.h" +#import "SUErrors.h" +#import "SUConstants.h" @interface SUTemporaryDownloader () -@property (nonatomic, copy) void (^completionBlock)(NSData * _Nullable data, NSError * _Nullable error); +@property (nonatomic, copy) void (^completionBlock)(SPUTemporaryDownload * _Nullable download, NSError * _Nullable error); @property (nonatomic) NSURLDownload *download; @property (nonatomic, copy) NSString *downloadFilename; +@property (nonatomic) NSURLResponse *response; @end @@ -22,8 +26,9 @@ @synthesize completionBlock = _completionBlock; @synthesize download = _download; @synthesize downloadFilename = _downloadFilename; +@synthesize response = _response; -- (void)startDownloadWithRequest:(SPUURLRequest *)request completion:(void (^)(NSData * _Nullable data, NSError * _Nullable error))completionBlock +- (void)startDownloadWithRequest:(SPUURLRequest *)request completion:(void (^)(SPUTemporaryDownload * _Nullable download, NSError * _Nullable error))completionBlock { dispatch_async(dispatch_get_main_queue(), ^{ self.completionBlock = completionBlock; @@ -60,9 +65,24 @@ self.downloadFilename = path; } +- (void)download:(NSURLDownload *)__unused download didReceiveResponse:(NSURLResponse *)response +{ + self.response = response; +} + - (void)downloadDidFinish:(NSURLDownload *)__unused aDownload { - self.completionBlock([NSData dataWithContentsOfFile:self.downloadFilename], nil); + assert(self.response != nil); + assert(self.downloadFilename != nil); + + NSData *data = [NSData dataWithContentsOfFile:self.downloadFilename]; + if (data != nil) { + // See SUPersistentDownloader as to why sending the NSURLResponse object over is not a good idea + SPUTemporaryDownload *download = [[SPUTemporaryDownload alloc] initWithData:data textEncoding:self.response.textEncodingName MIMEType:self.response.MIMEType]; + self.completionBlock(download, nil); + } else { + self.completionBlock(nil, [NSError errorWithDomain:SUSparkleErrorDomain code:SUDownloadError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Failed to read temporary downloaded data from %@", self.downloadFilename] }]); + } self.completionBlock = nil; [self cleanup]; } diff --git a/TemporaryDownloader/SUTemporaryDownloaderProtocol.h b/TemporaryDownloader/SUTemporaryDownloaderProtocol.h index db14c056..c6fccf71 100644 --- a/TemporaryDownloader/SUTemporaryDownloaderProtocol.h +++ b/TemporaryDownloader/SUTemporaryDownloaderProtocol.h @@ -11,11 +11,12 @@ NS_ASSUME_NONNULL_BEGIN @class SPUURLRequest; +@class SPUTemporaryDownload; // 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 SUTemporaryDownloaderProtocol -- (void)startDownloadWithRequest:(SPUURLRequest *)request completion:(void (^)(NSData * _Nullable data, NSError * _Nullable error))completionBlock; +- (void)startDownloadWithRequest:(SPUURLRequest *)request completion:(void (^)(SPUTemporaryDownload * _Nullable download, NSError * _Nullable error))completionBlock; @end diff --git a/TestApplication/SUPopUpTitlebarUserDriver.m b/TestApplication/SUPopUpTitlebarUserDriver.m index 122ea4b4..48c6125b 100644 --- a/TestApplication/SUPopUpTitlebarUserDriver.m +++ b/TestApplication/SUPopUpTitlebarUserDriver.m @@ -211,7 +211,7 @@ }); } -- (void)showUpdateReleaseNotes:(NSData *)__unused releaseNotes +- (void)showUpdateReleaseNotesWithData:(NSData *)__unused releaseNotesData textEncodingName:(NSString * _Nullable)__unused textEncodingName MIMEType:(NSString * _Nullable)__unused MIMEType { // todo: this should really be implemented } diff --git a/Tests/SUUpdaterTest.m b/Tests/SUUpdaterTest.m index 1e94206a..74e83770 100644 --- a/Tests/SUUpdaterTest.m +++ b/Tests/SUUpdaterTest.m @@ -39,7 +39,7 @@ - (void)showResumableUpdateFoundWithAppcastItem:(SUAppcastItem *)__unused appcastItem reply:(void (^)(SUInstallUpdateStatus))__unused reply {} -- (void)showUpdateReleaseNotes:(NSData *)__unused releaseNotes {} +- (void)showUpdateReleaseNotesWithData:(NSData *)__unused releaseNotesData textEncodingName:(NSString * _Nullable)__unused textEncodingName MIMEType:(NSString * _Nullable)__unused MIMEType {} - (void)showUpdateReleaseNotesFailedToDownloadWithError:(NSError *)__unused error {} diff --git a/sparkle-cli/SPUCommandLineUserDriver.m b/sparkle-cli/SPUCommandLineUserDriver.m index 736ba568..841ae207 100644 --- a/sparkle-cli/SPUCommandLineUserDriver.m +++ b/sparkle-cli/SPUCommandLineUserDriver.m @@ -120,13 +120,20 @@ }); } -- (void)displayReleaseNotes:(NSData *)releaseNotes +- (void)displayHTMLReleaseNotes:(NSData *)releaseNotes { NSAttributedString *attributedString = [[NSAttributedString alloc] initWithHTML:releaseNotes documentAttributes:NULL]; fprintf(stderr, "Release notes:\n"); fprintf(stderr, "%s\n", attributedString.string.UTF8String); } +- (void)displayPlainTextReleaseNotes:(NSData *)releaseNotes encoding:(NSStringEncoding)encoding +{ + NSString *string = [[NSString alloc] initWithData:releaseNotes encoding:encoding]; + fprintf(stderr, "Release notes:\n"); + fprintf(stderr, "%s\n", string.UTF8String); +} + - (void)showUpdateWithAppcastItem:(SUAppcastItem *)appcastItem updateAdjective:(NSString *)updateAdjective { if (self.verbose) { @@ -135,7 +142,7 @@ if (appcastItem.itemDescription != nil) { NSData *descriptionData = [appcastItem.itemDescription dataUsingEncoding:NSUTF8StringEncoding]; if (descriptionData != nil) { - [self displayReleaseNotes:descriptionData]; + [self displayHTMLReleaseNotes:descriptionData]; } } } @@ -174,11 +181,21 @@ }); } -- (void)showUpdateReleaseNotes:(NSData *)releaseNotes +- (void)showUpdateReleaseNotesWithData:(NSData *)releaseNotesData textEncodingName:(NSString * _Nullable)textEncodingName MIMEType:(NSString * _Nullable)MIMEType { dispatch_async(dispatch_get_main_queue(), ^{ if (self.verbose) { - [self displayReleaseNotes:releaseNotes]; + if ([MIMEType isEqualToString:@"text/plain"]) { + NSStringEncoding encoding; + if (textEncodingName == nil) { + encoding = NSUTF8StringEncoding; + } else { + encoding = CFStringConvertEncodingToNSStringEncoding(CFStringConvertIANACharSetNameToEncoding((CFStringRef)textEncodingName)); + } + [self displayPlainTextReleaseNotes:releaseNotesData encoding:encoding]; + } else { + [self displayHTMLReleaseNotes:releaseNotesData]; + } } }); }