From a6a2da7a69848ead38849af557434312ac0795d3 Mon Sep 17 00:00:00 2001 From: Zorg Date: Tue, 16 Aug 2016 18:59:13 -0400 Subject: [PATCH] Add silent termination method to user driver This is triggered if the update is automatically downloaded and the installer is started in the background, and the updater's delegate decides to handle silent installation and installs the update invoking its immediate installation block. In this scenario, this fixes an issue where the application may not be terminated silently. --- Sparkle/SPUStandardUserDriver.m | 44 +++++++++++++-------- Sparkle/SPUUserDriver.h | 17 +++++++- Sparkle/SUAutomaticUpdateDriver.m | 2 +- TestApplication/SUPopUpTitlebarUserDriver.m | 7 ++++ sparkle-cli/SPUCommandLineUserDriver.m | 5 +++ 5 files changed, 56 insertions(+), 19 deletions(-) diff --git a/Sparkle/SPUStandardUserDriver.m b/Sparkle/SPUStandardUserDriver.m index 8f6509ac..88d02e10 100644 --- a/Sparkle/SPUStandardUserDriver.m +++ b/Sparkle/SPUStandardUserDriver.m @@ -403,25 +403,30 @@ #pragma mark Aborting Everything +- (BOOL)_terminateApplicationAndWillTerminateCurrentApplication +{ + // Give the delegate a deferred chance for providing an application path + // This is a private API and used by SUUpdater + NSString *applicationPath = nil; + if ([self.delegate respondsToSelector:@selector(_pathToTerminateForStandardUserDriver)]) { + applicationPath = [(NSObject *)self.delegate _pathToTerminateForStandardUserDriver]; + } + + NSBundle *applicationBundle; + if (applicationPath != nil) { + applicationBundle = [NSBundle bundleWithPath:applicationPath]; + assert(applicationBundle != nil); + } else { + applicationBundle = self.applicationBundle; + } + + return [self.uiComponent terminateApplicationForBundleAndWillTerminateCurrentApplication:applicationBundle]; +} + - (void)terminateApplication { dispatch_async(dispatch_get_main_queue(), ^{ - // Give the delegate a deferred chance for providing an application path - // This is a private API and used by SUUpdater - NSString *applicationPath = nil; - if ([self.delegate respondsToSelector:@selector(_pathToTerminateForStandardUserDriver)]) { - applicationPath = [(NSObject *)self.delegate _pathToTerminateForStandardUserDriver]; - } - - NSBundle *applicationBundle; - if (applicationPath != nil) { - applicationBundle = [NSBundle bundleWithPath:applicationPath]; - assert(applicationBundle != nil); - } else { - applicationBundle = self.applicationBundle; - } - - if ([self.uiComponent terminateApplicationForBundleAndWillTerminateCurrentApplication:applicationBundle]) { + if ([self _terminateApplicationAndWillTerminateCurrentApplication]) { // if a user chooses to NOT relaunch the app (as is the case with WebKit // when it asks you if you are sure you want to close the app with multiple // tabs open), the status window still stays on the screen and obscures @@ -434,6 +439,13 @@ }); } +- (void)terminateApplicationSilently +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self _terminateApplicationAndWillTerminateCurrentApplication]; + }); +} + - (void)_dismissUpdateInstallation { // Make sure everything we call here does not dispatch async to main queue diff --git a/Sparkle/SPUUserDriver.h b/Sparkle/SPUUserDriver.h index 5fe7b028..c9edae09 100644 --- a/Sparkle/SPUUserDriver.h +++ b/Sparkle/SPUUserDriver.h @@ -256,13 +256,26 @@ SU_EXPORT @protocol SPUUserDriver * * Sparkle is asking us to send a request to terminate the application. * - * If this will terminate the current application, one may decide to dismiss progress UI in case the termination request is delayed or canceled. - * Or if this will terminate a remote application, one may decide to show the user that the update is currently installing. + * If this will terminate the current application, an implementor may decide to dismiss progress UI in case the termination request is delayed or canceled. + * Or if this will terminate a remote application, an implementor may decide to show the user that the update is currently installing. * * This can be called from any thread */ - (void)terminateApplication; +/*! + * Terminate the application silently. + * + * Sparkle is asking us to send a request to terminate the application without showing any UI interaction. + * This may be invoked after Sparkle has downloaded an update in the background automatically, + * and the updater's delegate has decided to install & relaunch the application without disrupting the user (think of a backgrounded application). + * + * After the application is terminated, the update will be installed and the application will be relaunched. + * + * This can be called from any thread + */ +- (void)terminateApplicationSilently; + /*! * Show the user that the update installation finished * diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index dcf6ef94..af26df29 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -105,7 +105,7 @@ // otherwise the update driver will abort the update before then - (void)installerIsRequestingAppTermination { - [self.userDriver terminateApplication]; + [self.userDriver terminateApplicationSilently]; } - (void)basicDriverIsRequestingAbortUpdateWithError:(NSError *)error diff --git a/TestApplication/SUPopUpTitlebarUserDriver.m b/TestApplication/SUPopUpTitlebarUserDriver.m index 7232df06..301eeb7d 100644 --- a/TestApplication/SUPopUpTitlebarUserDriver.m +++ b/TestApplication/SUPopUpTitlebarUserDriver.m @@ -339,6 +339,13 @@ }); } +- (void)terminateApplicationSilently +{ + dispatch_async(dispatch_get_main_queue(), ^{ + [self.uiComponent terminateApplicationForBundle:self.applicationBundle]; + }); +} + - (void)_dismissUpdateInstallation { // Make sure everything we call here does not dispatch async to main queue diff --git a/sparkle-cli/SPUCommandLineUserDriver.m b/sparkle-cli/SPUCommandLineUserDriver.m index 86a44db2..4a0c82c7 100644 --- a/sparkle-cli/SPUCommandLineUserDriver.m +++ b/sparkle-cli/SPUCommandLineUserDriver.m @@ -316,4 +316,9 @@ }); } +- (void)terminateApplicationSilently +{ + [self terminateApplication]; +} + @end