From 392cb65716dfeacc7a8fb40b259dffaeef4da286 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesin=CC=81ski?= Date: Tue, 10 Jan 2017 02:37:00 +0000 Subject: [PATCH] Tell OS when networking happens without UI --- Sparkle/SUAppcast.h | 2 +- Sparkle/SUAppcast.m | 6 +++++- Sparkle/SUAutomaticUpdateDriver.m | 4 ++++ Sparkle/SUBasicUpdateDriver.m | 10 +++++++--- Sparkle/SUUpdateDriver.h | 2 ++ Sparkle/SUUpdateDriver.m | 8 ++++++++ Sparkle/SUUserInitiatedUpdateDriver.m | 4 ++++ 7 files changed, 31 insertions(+), 5 deletions(-) diff --git a/Sparkle/SUAppcast.h b/Sparkle/SUAppcast.h index c81994b4..f4b2aea4 100644 --- a/Sparkle/SUAppcast.h +++ b/Sparkle/SUAppcast.h @@ -24,7 +24,7 @@ SU_EXPORT @interface SUAppcast : NSObject @property (copy, nullable) NSString *userAgentString; @property (copy, nullable) NSDictionary *httpHeaders; -- (void)fetchAppcastFromURL:(NSURL *)url completionBlock:(void (^)(NSError *_Nullable))err; +- (void)fetchAppcastFromURL:(NSURL *)url inBackground:(BOOL)bg completionBlock:(void (^)(NSError *_Nullable))err; - (SUAppcast *)copyWithoutDeltaUpdates; @property (readonly, copy, nullable) NSArray *items; diff --git a/Sparkle/SUAppcast.m b/Sparkle/SUAppcast.m index 8873854a..2af333a3 100644 --- a/Sparkle/SUAppcast.m +++ b/Sparkle/SUAppcast.m @@ -54,11 +54,15 @@ @synthesize download; @synthesize items; -- (void)fetchAppcastFromURL:(NSURL *)url completionBlock:(void (^)(NSError *))block +- (void)fetchAppcastFromURL:(NSURL *)url inBackground:(BOOL)background completionBlock:(void (^)(NSError *))block { self.completionBlock = block; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30.0]; + if (background) { + request.networkServiceType = NSURLNetworkServiceTypeBackground; + } + if (self.userAgentString) { [request setValue:self.userAgentString forHTTPHeaderField:@"User-Agent"]; } diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index 46817b1c..44fb18fd 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -121,6 +121,10 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 return NO; } +-(BOOL)downloadsUpdatesInBackground { + return YES; +} + - (void)stopUpdatingOnTermination { if (self.willUpdateOnTermination) diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 6769690c..f628b29f 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -65,7 +65,7 @@ id updater = self.updater; [appcast setUserAgentString:[updater userAgentString]]; [appcast setHttpHeaders:[updater httpHeaders]]; - [appcast fetchAppcastFromURL:URL completionBlock:^(NSError *error) { + [appcast fetchAppcastFromURL:URL inBackground:self.downloadsAppcastInBackground completionBlock:^(NSError *error) { if (error) { [self abortUpdateWithError:error]; } else { @@ -258,10 +258,14 @@ if ([[NSFileManager defaultManager] fileExistsAtPath:appCachePath]) { [[NSFileManager defaultManager] removeItemAtPath:appCachePath error:NULL]; } - + id updater = self.updater; - + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[self.updateItem fileURL]]; + if (self.downloadsUpdatesInBackground) { + request.networkServiceType = NSURLNetworkServiceTypeBackground; + } + [request setValue:[updater userAgentString] forHTTPHeaderField:@"User-Agent"]; if ([[updater delegate] respondsToSelector:@selector(updater:willDownloadUpdate:withRequest:)]) { [[updater delegate] updater:self.updater diff --git a/Sparkle/SUUpdateDriver.h b/Sparkle/SUUpdateDriver.h index ab03781d..7c276efd 100644 --- a/Sparkle/SUUpdateDriver.h +++ b/Sparkle/SUUpdateDriver.h @@ -32,6 +32,8 @@ extern NSString *const SUUpdateDriverFinishedNotification; @property (getter=isInterruptible, readonly) BOOL interruptible; @property (readonly) BOOL finished; +@property (readonly) BOOL downloadsAppcastInBackground; +@property (readonly) BOOL downloadsUpdatesInBackground; @property BOOL automaticallyInstallUpdates; @end diff --git a/Sparkle/SUUpdateDriver.m b/Sparkle/SUUpdateDriver.m index f83d2355..ccf84d71 100644 --- a/Sparkle/SUUpdateDriver.m +++ b/Sparkle/SUUpdateDriver.m @@ -56,6 +56,14 @@ NSString *const SUUpdateDriverFinishedNotification = @"SUUpdateDriverFinished"; return NO; } +-(BOOL)downloadsAppcastInBackground { + return YES; +} + +-(BOOL)downloadsUpdatesInBackground { + return NO; +} + - (void)showAlert:(NSAlert *)alert { // Only UI-based subclass shows the actual alert SULog(@"ALERT: %@\n%@", alert.messageText, alert.informativeText); diff --git a/Sparkle/SUUserInitiatedUpdateDriver.m b/Sparkle/SUUserInitiatedUpdateDriver.m index 816640aa..5742df12 100644 --- a/Sparkle/SUUserInitiatedUpdateDriver.m +++ b/Sparkle/SUUserInitiatedUpdateDriver.m @@ -80,6 +80,10 @@ [super abortUpdate]; } +-(BOOL)downloadsAppcastInBackground { + return NO; +} + - (BOOL)itemContainsValidUpdate:(SUAppcastItem *)ui { // We don't check to see if this update's been skipped, because the user explicitly *asked* if he had the latest version.