From 3230fd982bf1eefa50101405eb7b588311bac8bd Mon Sep 17 00:00:00 2001 From: Zorg Date: Sat, 27 Feb 2016 14:00:28 -0500 Subject: [PATCH] Change SUUserUpdaterDriver so that every reply block must get a response These changes are to make XPC environment more friendly. Every reply block must get a response and is not optional to respond to. For example we can no longer have a reply block that is just "cancelUpdate()"; it must now take an option whether we are done now or if we want to cancel, and the receiver should now be aware when canceling doesn't make sense (i.e, if update already aborted). Furthermore, a reply block must get exactly *one* response. --- Sparkle/SUAutomaticUpdateDriver.m | 30 +++- Sparkle/SUBasicUpdateDriver.m | 22 ++- Sparkle/SUSparkleUserUpdaterDriver.h | 2 +- Sparkle/SUSparkleUserUpdaterDriver.m | 160 +++++++++++++----- Sparkle/SUUIBasedUpdateDriver.m | 37 ++-- Sparkle/SUUserInitiatedUpdateDriver.m | 28 +-- Sparkle/SUUserUpdaterDriver.h | 30 +++- .../SUUpdateSettingsWindowController.m | 68 ++++++-- 8 files changed, 278 insertions(+), 99 deletions(-) diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index f7c40f53..092557c6 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -46,10 +46,30 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 }]; } +- (void)installUpdateWithTerminationStatus:(NSNumber *)terminationStatus +{ + switch ((SUApplicationTerminationStatus)(terminationStatus.integerValue)) { + case SUApplicationStoppedObservingTermination: + if (self.willUpdateOnTermination) { + [self abortUpdate]; + } + break; + case SUApplicationWillTerminate: + if (self.willUpdateOnTermination) { + [self installWithToolAndRelaunch:NO]; + } + break; + } +} + - (void)unarchiverDidFinish:(SUUnarchiver *)__unused ua { - [self.updater.userUpdaterDriver registerForAppTermination:^{ - [self installWithToolAndRelaunch:NO]; + [self.updater.userUpdaterDriver registerApplicationTermination:^(SUApplicationTerminationStatus terminationStatus) { + // We use -performSelectorOnMainThread:withObject:waitUntilDone: rather than GCD because if we are on the main thread already, + // we don't want to run the operation asynchronously. It's also possible we aren't on the main thread (say due to IPC through a XPC service). + // Anyway, if we're on the main thread in a single process without the app delegate delaying termination, + // we could be terminating *really soon* - so we want to install the update quickly + [self performSelectorOnMainThread:@selector(installUpdateWithTerminationStatus:) withObject:@(terminationStatus) waitUntilDone:YES]; }]; [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(systemWillPowerOff:) name:NSWorkspaceWillPowerOffNotification object:nil]; @@ -88,12 +108,12 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 { if (self.willUpdateOnTermination) { - [self.updater.userUpdaterDriver unregisterForAppTermination]; + self.willUpdateOnTermination = NO; + + [self.updater.userUpdaterDriver unregisterApplicationTermination]; [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self name:NSWorkspaceWillPowerOffNotification object:nil]; - self.willUpdateOnTermination = NO; - id updaterDelegate = [self.updater delegate]; if ([updaterDelegate respondsToSelector:@selector(updater:didCancelInstallUpdateOnQuit:)]) [updaterDelegate updater:self.updater didCancelInstallUpdateOnQuit:self.updateItem]; diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index 05f0e634..4a799e66 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -228,11 +228,13 @@ { // Okay, something's really broken with this user's file structure. [self.download cancel]; + self.download = nil; + [self abortUpdateWithError:[NSError errorWithDomain:SUSparkleErrorDomain code:SUTemporaryDirectoryError userInfo:@{ NSLocalizedDescriptionKey: [NSString stringWithFormat:@"Can't make a temporary directory for the update download at %@.", self.tempDir] }]]; + } else { + self.downloadPath = [self.tempDir stringByAppendingPathComponent:name]; + [self.download setDestination:self.downloadPath allowOverwrite:YES]; } - - self.downloadPath = [self.tempDir stringByAppendingPathComponent:name]; - [self.download setDestination:self.downloadPath allowOverwrite:YES]; } /** @@ -321,7 +323,10 @@ { assert(self.updateItem); - [self extractUpdate]; + if (self.download != nil && self.downloadPath != nil) { + self.download = nil; + [self extractUpdate]; + } } - (void)download:(NSURLDownload *)__unused download didFailWithError:(NSError *)error @@ -558,6 +563,12 @@ if (!success) [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:[self.tempDir stringByDeletingLastPathComponent] destination:@"" files:@[[self.tempDir lastPathComponent]] tag:NULL]; } + + if (self.download != nil) { + [self.download cancel]; + self.download = nil; + } + self.downloadPath = nil; } - (void)installerForHost:(SUHost *)aHost failedWithError:(NSError *)error @@ -594,9 +605,6 @@ errorToDisplay = errorToDisplay.userInfo[NSUnderlyingErrorKey]; } while(--finiteRecursion && errorToDisplay); } - if (self.download) { - [self.download cancel]; - } // Notify host app that update has aborted id updaterDelegate = [self.updater delegate]; diff --git a/Sparkle/SUSparkleUserUpdaterDriver.h b/Sparkle/SUSparkleUserUpdaterDriver.h index f70ec2a7..76578c4a 100644 --- a/Sparkle/SUSparkleUserUpdaterDriver.h +++ b/Sparkle/SUSparkleUserUpdaterDriver.h @@ -26,7 +26,7 @@ - (instancetype)initWithHost:(SUHost *)host handlesTermination:(BOOL)handlesTermination delegate:(id )delegate; @property (nonatomic, readonly, weak) id delegate; -@property (nonatomic, readonly, getter = isInstallingOnTermination) BOOL installingOnTermination; +@property (nonatomic, readonly, getter = isInstallingUpdateOnTermination) BOOL installingUpdateOnTermination; - (void)sendTerminationSignalWithCompletion:(void (^)(void))finishTermination; diff --git a/Sparkle/SUSparkleUserUpdaterDriver.m b/Sparkle/SUSparkleUserUpdaterDriver.m index d67b7a57..87351c69 100644 --- a/Sparkle/SUSparkleUserUpdaterDriver.m +++ b/Sparkle/SUSparkleUserUpdaterDriver.m @@ -37,17 +37,19 @@ @property (nonatomic, readonly) BOOL handlesTermination; @property (nonatomic) SUStatusController *checkingController; -@property (nonatomic, copy) void (^cancelUpdateCheck)(void); +@property (nonatomic, copy) void (^updateCheckStatusCompletion)(SUUserInitiatedCheckStatus); @property (nonatomic) SUWindowController *activeUpdateAlert; @property (nonatomic) SUStatusController *statusController; -@property (nonatomic, copy) void (^cancelDownload)(void); -@property (nonatomic, copy) void (^installAndRestart)(void); +@property (nonatomic, copy) void (^downloadStatusCompletion)(SUDownloadUpdateStatus); +@property (nonatomic, copy) void (^installUpdateHandler)(SUInstallUpdateStatus); -@property (nonatomic, copy) void (^applicationWillTerminate)(void); +@property (nonatomic, copy) void (^applicationTerminationHandler)(SUApplicationTerminationStatus); @property (nonatomic, copy) void (^finishTermination)(void); +@property (nonatomic) BOOL installingUpdateOnTermination; + @end @implementation SUSparkleUserUpdaterDriver @@ -56,13 +58,16 @@ @synthesize handlesTermination = _handlesTermination; @synthesize delegate = _delegate; @synthesize checkingController = _checkingController; -@synthesize cancelUpdateCheck = _cancelUpdateCheck; +@synthesize updateCheckStatusCompletion = _updateCheckStatusCompletion; @synthesize activeUpdateAlert = _activeUpdateAlert; @synthesize statusController = _statusController; -@synthesize cancelDownload = _cancelDownload; -@synthesize installAndRestart = _installAndRestart; -@synthesize applicationWillTerminate = _applicationWillTerminate; +@synthesize downloadStatusCompletion = _downloadStatusCompletion; +@synthesize installUpdateHandler = _installUpdateHandler; +@synthesize applicationTerminationHandler = _applicationTerminationHandler; @synthesize finishTermination = _finishTermination; +@synthesize installingUpdateOnTermination = _installingUpdateOnTermination; + +#pragma mark Birth - (instancetype)initWithHost:(SUHost *)host handlesTermination:(BOOL)handlesTermination delegate:(id)delegate { @@ -75,6 +80,8 @@ return self; } +#pragma mark Update Permission + - (void)requestUpdatePermissionWithSystemProfile:(NSArray *)systemProfile reply:(void (^)(SUUpdatePermissionPromptResult *))reply { dispatch_async(dispatch_get_main_queue(), ^{ @@ -85,6 +92,8 @@ }); } +#pragma mark Update Alert Focus + - (void)setUpFocusForActiveUpdateAlert { // If the app is a menubar app or the like, we need to focus it first and alter the @@ -109,6 +118,8 @@ [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationDidBecomeActiveNotification object:NSApp]; } +#pragma mark Update Found + - (void)showUpdateFoundWithAppcastItem:(SUAppcastItem *)appcastItem versionDisplayer:(id)versionDisplayer reply:(void (^)(SUUpdateAlertChoice))reply { dispatch_async(dispatch_get_main_queue(), ^{ @@ -138,7 +149,9 @@ }); } -- (void)showExtractionFinishedAndReadyToInstallAndRelaunch:(void (^)(void))installUpdateAndRelaunch +#pragma mark Install & Relaunch Update + +- (void)showExtractionFinishedAndReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler { dispatch_async(dispatch_get_main_queue(), ^{ [self.statusController beginActionWithTitle:SULocalizedString(@"Ready to Install", nil) maxProgressValue:1.0 statusText:nil]; @@ -148,22 +161,32 @@ [[self.statusController window] makeKeyAndOrderFront:self]; [NSApp requestUserAttention:NSInformationalRequest]; - self.installAndRestart = installUpdateAndRelaunch; + self.installUpdateHandler = installUpdateHandler; }); } - (void)installAndRestart:(id)__unused sender { - if (self.installAndRestart != nil) { - self.installAndRestart(); - self.installAndRestart = nil; + if (self.installUpdateHandler != nil) { + self.installUpdateHandler(SUInstallAndRelaunchUpdateNow); + self.installUpdateHandler = nil; } } -- (void)showUserInitiatedUpdateCheckWithCancelCallback:(void (^)(void))cancelUpdateCheck +- (void)cancelInstallAndRestart +{ + if (self.installUpdateHandler != nil) { + self.installUpdateHandler(SUCancelUpdateInstallation); + self.installUpdateHandler = nil; + } +} + +#pragma mark Check for Updates + +- (void)showUserInitiatedUpdateCheckWithCompletion:(void (^)(SUUserInitiatedCheckStatus))updateCheckStatusCompletion { dispatch_async(dispatch_get_main_queue(), ^{ - self.cancelUpdateCheck = cancelUpdateCheck; + self.updateCheckStatusCompletion = updateCheckStatusCompletion; self.checkingController = [[SUStatusController alloc] initWithHost:self.host]; [[self.checkingController window] center]; // Force the checking controller to load its window. @@ -180,18 +203,32 @@ }); } +- (void)cancelCheckForUpdates +{ + if (self.updateCheckStatusCompletion != nil) { + self.updateCheckStatusCompletion(SUUserInitiatedCheckCancelled); + self.updateCheckStatusCompletion = nil; + } + + if (self.checkingController != nil) + { + [[self.checkingController window] close]; + self.checkingController = nil; + } +} + - (void)cancelCheckForUpdates:(id)__unused sender { - if (self.cancelUpdateCheck != nil) { - self.cancelUpdateCheck(); - self.cancelUpdateCheck = nil; - } + [self cancelCheckForUpdates]; } - (void)dismissUserInitiatedUpdateCheck { dispatch_async(dispatch_get_main_queue(), ^{ - self.cancelUpdateCheck = nil; + if (self.updateCheckStatusCompletion != nil) { + self.updateCheckStatusCompletion(SUUserInitiatedCheckDone); + self.updateCheckStatusCompletion = nil; + } if (self.checkingController != nil) { @@ -201,6 +238,8 @@ }); } +#pragma mark Update Errors + - (void)showUpdaterError:(NSError *)error { dispatch_async(dispatch_get_main_queue(), ^{ @@ -245,10 +284,12 @@ }); } -- (void)showDownloadInitiatedWithCancelCallback:(void (^)(void))cancelDownload +#pragma mark Download & Install Updates + +- (void)showDownloadInitiatedWithCompletion:(void (^)(SUDownloadUpdateStatus))downloadUpdateStatusCompletion { dispatch_async(dispatch_get_main_queue(), ^{ - self.cancelDownload = cancelDownload; + self.downloadStatusCompletion = downloadUpdateStatusCompletion; self.statusController = [[SUStatusController alloc] initWithHost:self.host]; [self.statusController beginActionWithTitle:SULocalizedString(@"Downloading update...", @"Take care not to overflow the status window.") maxProgressValue:0.0 statusText:nil]; @@ -257,12 +298,17 @@ }); } +- (void)cancelDownload +{ + if (self.downloadStatusCompletion != nil) { + self.downloadStatusCompletion(SUDownloadUpdateCancelled); + self.downloadStatusCompletion = nil; + } +} + - (void)cancelDownload:(id)__unused sender { - if (self.cancelDownload != nil) { - self.cancelDownload(); - self.cancelDownload = nil; - } + [self cancelDownload]; } - (void)showDownloadDidReceiveResponse:(NSURLResponse *)response @@ -312,6 +358,11 @@ - (void)showDownloadFinishedAndStartedExtractingUpdate { dispatch_async(dispatch_get_main_queue(), ^{ + if (self.downloadStatusCompletion != nil) { + self.downloadStatusCompletion(SUDownloadUpdateDone); + self.downloadStatusCompletion = nil; + } + [self.statusController beginActionWithTitle:SULocalizedString(@"Extracting update...", @"Take care not to overflow the status window.") maxProgressValue:0.0 statusText:nil]; [self.statusController setButtonEnabled:NO]; }); @@ -336,13 +387,17 @@ }); } -- (void)registerForAppTermination:(void (^)(void))applicationWillTerminate +#pragma mark Application Death + +- (void)registerApplicationTermination:(void (^)(SUApplicationTerminationStatus))applicationTerminationHandler { dispatch_async(dispatch_get_main_queue(), ^{ + self.installingUpdateOnTermination = YES; + // Sudden termination is available on 10.6+ [[NSProcessInfo processInfo] disableSuddenTermination]; - self.applicationWillTerminate = applicationWillTerminate; + self.applicationTerminationHandler = applicationTerminationHandler; if (self.handlesTermination) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; @@ -350,54 +405,68 @@ }); } -- (void)unregisterForAppTermination +- (void)cancelObservingApplicationTermination { - dispatch_async(dispatch_get_main_queue(), ^{ + if (self.installingUpdateOnTermination) { [[NSProcessInfo processInfo] enableSuddenTermination]; if (self.handlesTermination) { [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil]; } - self.applicationWillTerminate = nil; + if (self.applicationTerminationHandler != nil) { + self.applicationTerminationHandler(SUApplicationStoppedObservingTermination); + self.applicationTerminationHandler = nil; + } + + self.installingUpdateOnTermination = NO; + } +} + +- (void)unregisterApplicationTermination +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self cancelObservingApplicationTermination]; }); } - (void)applicationWillTerminate:(NSNotification *)__unused note { - // Pass an empty block instead of nil to indicate the app - // has already initiated termination [self sendTerminationSignalWithCompletion:^{}]; } -- (BOOL)isInstallingOnTermination -{ - return (self.applicationWillTerminate != nil); -} - - (void)sendTerminationSignalWithCompletion:(void (^)(void))finishTermination { self.finishTermination = finishTermination; - if (self.applicationWillTerminate) { - self.applicationWillTerminate(); + if (self.applicationTerminationHandler != nil) { + self.applicationTerminationHandler(SUApplicationWillTerminate); + self.applicationTerminationHandler = nil; } } - (void)terminateApplication { dispatch_async(dispatch_get_main_queue(), ^{ - if (self.finishTermination) { - self.finishTermination(); + if (self.installingUpdateOnTermination && !self.handlesTermination) { + if (self.finishTermination != nil) { + self.finishTermination(); + self.finishTermination = nil; + } } else { [NSApp terminate:nil]; } }); } +#pragma mark Aborting Update + - (void)dismissUpdateInstallation { dispatch_async(dispatch_get_main_queue(), ^{ + [self cancelCheckForUpdates]; + [self cancelDownload]; + if (self.statusController) { [self.statusController close]; self.statusController = nil; @@ -408,9 +477,10 @@ self.activeUpdateAlert = nil; } - self.installAndRestart = nil; - self.cancelDownload = nil; - self.cancelUpdateCheck = nil; + [self cancelObservingApplicationTermination]; + [self cancelInstallAndRestart]; + + self.finishTermination = nil; }); } diff --git a/Sparkle/SUUIBasedUpdateDriver.m b/Sparkle/SUUIBasedUpdateDriver.m index 4b67e0ae..a0acae98 100644 --- a/Sparkle/SUUIBasedUpdateDriver.m +++ b/Sparkle/SUUIBasedUpdateDriver.m @@ -67,16 +67,22 @@ switch (choice) { case SUInstallUpdateChoice: { - [self.updater.userUpdaterDriver showDownloadInitiatedWithCancelCallback:^{ - dispatch_async(dispatch_get_main_queue(), ^{ - if (self.download) { - [self.download cancel]; - if ([[self.updater delegate] respondsToSelector:@selector(userDidCancelDownload:)]) { - [[self.updater delegate] userDidCancelDownload:self.updater]; - } - } - [self abortUpdate]; - }); + [self.updater.userUpdaterDriver showDownloadInitiatedWithCompletion:^(SUDownloadUpdateStatus downloadCompletionStatus) { + switch (downloadCompletionStatus) { + case SUDownloadUpdateDone: + break; + case SUDownloadUpdateCancelled: + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.download != nil) { + if ([[self.updater delegate] respondsToSelector:@selector(userDidCancelDownload:)]) { + [[self.updater delegate] userDidCancelDownload:self.updater]; + } + + [self abortUpdate]; + } + }); + break; + } }]; [self downloadUpdate]; @@ -124,9 +130,16 @@ return; } - [self.updater.userUpdaterDriver showExtractionFinishedAndReadyToInstallAndRelaunch:^{ + [self.updater.userUpdaterDriver showExtractionFinishedAndReadyToInstallAndRelaunch:^(SUInstallUpdateStatus installUpdateStatus) { dispatch_async(dispatch_get_main_queue(), ^{ - [self installWithToolAndRelaunch:YES]; + switch (installUpdateStatus) { + case SUCancelUpdateInstallation: + [self abortUpdate]; + break; + case SUInstallAndRelaunchUpdateNow: + [self installWithToolAndRelaunch:YES]; + break; + } }); }]; } diff --git a/Sparkle/SUUserInitiatedUpdateDriver.m b/Sparkle/SUUserInitiatedUpdateDriver.m index db92bf3a..fd987d13 100644 --- a/Sparkle/SUUserInitiatedUpdateDriver.m +++ b/Sparkle/SUUserInitiatedUpdateDriver.m @@ -20,24 +20,32 @@ @synthesize canceled; -- (void)closeCheckingWindow +- (void)dismissCheckingForUpdates { [self.updater.userUpdaterDriver dismissUserInitiatedUpdateCheck]; } - (void)cancelCheckForUpdates:(id)__unused sender { - [self closeCheckingWindow]; - self.canceled = YES; + if (!self.canceled) { + [self dismissCheckingForUpdates]; + self.canceled = YES; + } } #warning assign user driver's host in a superclass of this method, possibly the UI driver.. IDK - (void)checkForUpdatesAtURL:(NSURL *)URL host:(SUHost *)aHost { - [self.updater.userUpdaterDriver showUserInitiatedUpdateCheckWithCancelCallback:^{ - dispatch_async(dispatch_get_main_queue(), ^{ - [self cancelCheckForUpdates:nil]; - }); + [self.updater.userUpdaterDriver showUserInitiatedUpdateCheckWithCompletion:^(SUUserInitiatedCheckStatus completionStatus) { + switch (completionStatus) { + case SUUserInitiatedCheckDone: + break; + case SUUserInitiatedCheckCancelled: + dispatch_async(dispatch_get_main_queue(), ^{ + [self cancelCheckForUpdates:nil]; + }); + break; + } }]; [super checkForUpdatesAtURL:URL host:aHost]; @@ -50,19 +58,19 @@ [self abortUpdate]; return; } - [self closeCheckingWindow]; + [self dismissCheckingForUpdates]; [super appcastDidFinishLoading:ac]; } - (void)abortUpdateWithError:(NSError *)error { - [self closeCheckingWindow]; + [self dismissCheckingForUpdates]; [super abortUpdateWithError:error]; } - (void)abortUpdate { - [self closeCheckingWindow]; + [self dismissCheckingForUpdates]; [super abortUpdate]; } diff --git a/Sparkle/SUUserUpdaterDriver.h b/Sparkle/SUUserUpdaterDriver.h index 5c1a1e58..3f950b41 100644 --- a/Sparkle/SUUserUpdaterDriver.h +++ b/Sparkle/SUUserUpdaterDriver.h @@ -14,12 +14,32 @@ @protocol SUVersionDisplay; +typedef NS_ENUM(NSUInteger, SUUserInitiatedCheckStatus) { + SUUserInitiatedCheckDone, + SUUserInitiatedCheckCancelled +}; + +typedef NS_ENUM(NSUInteger, SUDownloadUpdateStatus) { + SUDownloadUpdateDone, + SUDownloadUpdateCancelled +}; + +typedef NS_ENUM(NSUInteger, SUApplicationTerminationStatus) { + SUApplicationWillTerminate, + SUApplicationStoppedObservingTermination +}; + +typedef NS_ENUM(NSUInteger, SUInstallUpdateStatus) { + SUInstallAndRelaunchUpdateNow, + SUCancelUpdateInstallation +}; + @protocol SUUserUpdaterDriver #warning might need to whitelist class types for systemProfile.. need to test this - (void)requestUpdatePermissionWithSystemProfile:(NSArray *)systemProfile reply:(void (^)(SUUpdatePermissionPromptResult *))reply; -- (void)showUserInitiatedUpdateCheckWithCancelCallback:(void (^)(void))cancelUpdateCheck; +- (void)showUserInitiatedUpdateCheckWithCompletion:(void (^)(SUUserInitiatedCheckStatus))updateCheckStatusCompletion; - (void)dismissUserInitiatedUpdateCheck; - (void)showUpdateFoundWithAppcastItem:(SUAppcastItem *)appcastItem versionDisplayer:(id)versionDisplayer reply:(void (^)(SUUpdateAlertChoice))reply; @@ -30,17 +50,17 @@ - (void)showUpdateNotFound; - (void)showUpdaterError:(NSError *)error; -- (void)showDownloadInitiatedWithCancelCallback:(void (^)(void))cancelDownload; +- (void)showDownloadInitiatedWithCompletion:(void (^)(SUDownloadUpdateStatus))downloadUpdateStatusCompletion; - (void)showDownloadDidReceiveResponse:(NSURLResponse *)response; - (void)showDownloadDidReceiveDataOfLength:(NSUInteger)length; - (void)showDownloadFinishedAndStartedExtractingUpdate; - (void)showExtractionReceivedProgress:(double)progress; -- (void)showExtractionFinishedAndReadyToInstallAndRelaunch:(void (^)(void))installUpdateAndRelaunch; +- (void)showExtractionFinishedAndReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler; - (void)showInstallingUpdate; -- (void)registerForAppTermination:(void (^)(void))applicationWillTerminate; -- (void)unregisterForAppTermination; +- (void)registerApplicationTermination:(void (^)(SUApplicationTerminationStatus))applicationTerminationHandler; +- (void)unregisterApplicationTermination; - (void)terminateApplication; - (void)dismissUpdateInstallation; diff --git a/TestApplication/SUUpdateSettingsWindowController.m b/TestApplication/SUUpdateSettingsWindowController.m index 88c6ce6c..a2ed5e99 100644 --- a/TestApplication/SUUpdateSettingsWindowController.m +++ b/TestApplication/SUUpdateSettingsWindowController.m @@ -13,13 +13,20 @@ @interface SULoggerUpdateDriver : NSObject -@property (nonatomic, copy) void (^applicationWillTerminate)(void); +@property (nonatomic, copy) void (^downloadUpdateStatusCompletion)(SUDownloadUpdateStatus); +@property (nonatomic, copy) void (^updateCheckStatusCompletion)(SUUserInitiatedCheckStatus); +@property (nonatomic, copy) void (^applicationTerminationHandler)(SUApplicationTerminationStatus); + +@property (nonatomic) BOOL registeredApplicationTermination; @end @implementation SULoggerUpdateDriver -@synthesize applicationWillTerminate = _applicationWillTerminate; +@synthesize downloadUpdateStatusCompletion = _downloadUpdateStatusCompletion; +@synthesize updateCheckStatusCompletion = _updateCheckStatusCompletion; +@synthesize applicationTerminationHandler = _applicationTerminationHandler; +@synthesize registeredApplicationTermination = _registeredApplicationTermination; - (void)requestUpdatePermissionWithSystemProfile:(NSArray *)__unused systemProfile reply:(void (^)(SUUpdatePermissionPromptResult *))reply { @@ -27,13 +34,18 @@ reply([SUUpdatePermissionPromptResult updatePermissionPromptResultWithChoice:SUAutomaticallyCheck shouldSendProfile:YES]); } -- (void)showUserInitiatedUpdateCheckWithCancelCallback:(void (^)(void))__unused cancelUpdateCheck +- (void)showUserInitiatedUpdateCheckWithCompletion:(void (^)(SUUserInitiatedCheckStatus))completionStatusCheck { NSLog(@"Evil user initiated an update check!"); + self.updateCheckStatusCompletion = completionStatusCheck; } - (void)dismissUserInitiatedUpdateCheck { + if (self.updateCheckStatusCompletion != nil) { + self.updateCheckStatusCompletion(SUUserInitiatedCheckDone); + self.updateCheckStatusCompletion = nil; + } NSLog(@"Update check is done!"); } @@ -59,9 +71,10 @@ NSLog(@"Update error: %@", error); } -- (void)showDownloadInitiatedWithCancelCallback:(void (^)(void))__unused cancelDownload +- (void)showDownloadInitiatedWithCompletion:(void (^)(SUDownloadUpdateStatus))downloadUpdateStatusCompletion { NSLog(@"Downloading update..."); + self.downloadUpdateStatusCompletion = downloadUpdateStatusCompletion; } - (void)showDownloadDidReceiveResponse:(NSURLResponse *)response @@ -76,6 +89,11 @@ - (void)showDownloadFinishedAndStartedExtractingUpdate { + if (self.downloadUpdateStatusCompletion != nil) { + self.downloadUpdateStatusCompletion(SUDownloadUpdateDone); + self.downloadUpdateStatusCompletion = nil; + } + NSLog(@"Download finished.. Extracting.."); } @@ -84,10 +102,10 @@ NSLog(@"Extracting progress: %f", progress); } -- (void)showExtractionFinishedAndReadyToInstallAndRelaunch:(void (^)(void))installUpdateAndRelaunch +- (void)showExtractionFinishedAndReadyToInstallAndRelaunch:(void (^)(SUInstallUpdateStatus))installUpdateHandler { NSLog(@"Extracting finished.. Letting it install & relaunch.."); - installUpdateAndRelaunch(); + installUpdateHandler(SUInstallAndRelaunchUpdateNow); } - (void)showInstallingUpdate @@ -95,27 +113,37 @@ NSLog(@"Installing update..."); } -- (void)registerForAppTermination:(void (^)(void))applicationWillTerminate +- (void)registerApplicationTermination:(void (^)(SUApplicationTerminationStatus))applicationTerminationHandler { + self.registeredApplicationTermination = YES; + NSLog(@"Registered for termination, eh?"); - self.applicationWillTerminate = applicationWillTerminate; + self.applicationTerminationHandler = applicationTerminationHandler; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; } -- (void)unregisterForAppTermination +- (void)unregisterApplicationTermination { - self.applicationWillTerminate = nil; - - [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil]; + if (self.registeredApplicationTermination) { + if (self.applicationTerminationHandler != nil) { + self.applicationTerminationHandler(SUApplicationStoppedObservingTermination); + self.applicationTerminationHandler = nil; + } + + [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil]; + + self.registeredApplicationTermination = NO; + } } - (void)applicationWillTerminate:(NSNotification *)__unused note { NSLog(@"Terminating app.."); - if (self.applicationWillTerminate) { + if (self.applicationTerminationHandler != nil) { NSLog(@"App will terminate"); - self.applicationWillTerminate(); + self.applicationTerminationHandler(SUApplicationWillTerminate); + self.applicationTerminationHandler = nil; } } @@ -127,6 +155,18 @@ - (void)dismissUpdateInstallation { NSLog(@"Dismissing the installation."); + + if (self.updateCheckStatusCompletion != nil) { + self.updateCheckStatusCompletion(SUUserInitiatedCheckCancelled); + self.updateCheckStatusCompletion = nil; + } + + if (self.downloadUpdateStatusCompletion != nil) { + self.downloadUpdateStatusCompletion(SUDownloadUpdateCancelled); + self.downloadUpdateStatusCompletion = nil; + } + + [self unregisterApplicationTermination]; } @end