From 0e97f0b4b66e22f7a2b0b2a66c5fa545b658dc23 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 21 Feb 2016 13:24:54 -0500 Subject: [PATCH] Handle app termination in the user driver If an automatic update needs to be applied at termination, the app delegate will need to hold responsibility of delaying termination until Sparkle is ready to relaunch --- Sparkle/SUAutomaticUpdateDriver.m | 16 ------- Sparkle/SUBasicUpdateDriver.m | 8 +--- Sparkle/SUSparkleUserUpdaterDriver.h | 5 ++ Sparkle/SUSparkleUserUpdaterDriver.m | 46 +++++++++++++++++-- Sparkle/SUUserUpdaterDriver.h | 1 + .../SUUpdateSettingsWindowController.m | 9 +++- 6 files changed, 57 insertions(+), 28 deletions(-) diff --git a/Sparkle/SUAutomaticUpdateDriver.m b/Sparkle/SUAutomaticUpdateDriver.m index 1156f868..63f947d7 100644 --- a/Sparkle/SUAutomaticUpdateDriver.m +++ b/Sparkle/SUAutomaticUpdateDriver.m @@ -26,7 +26,6 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 @property (assign) BOOL postponingInstallation; @property (assign) BOOL showErrors; @property (assign) BOOL willUpdateOnTermination; -@property (assign) BOOL isTerminating; @property (strong) NSTimer *showUpdateAlertTimer; @end @@ -36,7 +35,6 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 @synthesize postponingInstallation; @synthesize showErrors; @synthesize willUpdateOnTermination; -@synthesize isTerminating; @synthesize showUpdateAlertTimer; - (void)showUpdateAlert @@ -51,12 +49,6 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 - (void)unarchiverDidFinish:(SUUnarchiver *)__unused ua { [self.updater.userUpdaterDriver registerForAppTermination:^{ - // We don't want to terminate the app if the user or someone else initiated a termination - // Use a property instead of passing an argument to installWithToolAndRelaunch: - // because we give the delegate an invocation to our install methods and - // this code was added later :| - self.isTerminating = YES; - [self installWithToolAndRelaunch:NO]; }]; @@ -122,7 +114,6 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 - (void)abortUpdate { - self.isTerminating = NO; [self stopUpdatingOnTermination]; [self invalidateShowUpdateAlertTimer]; @@ -171,13 +162,6 @@ static const NSTimeInterval SUAutomaticUpdatePromptImpatienceTimer = 60 * 60 * 2 }]]; } -- (void)terminateApp -{ - if (!self.isTerminating) { - [super terminateApp]; - } -} - - (void)abortUpdateWithError:(NSError *)error { if (self.showErrors) { diff --git a/Sparkle/SUBasicUpdateDriver.m b/Sparkle/SUBasicUpdateDriver.m index b60e09a3..de389410 100644 --- a/Sparkle/SUBasicUpdateDriver.m +++ b/Sparkle/SUBasicUpdateDriver.m @@ -544,13 +544,7 @@ // Note: this is overridden by the automatic update driver to not terminate in some cases - (void)terminateApp { - for (NSRunningApplication *runningApplication in [[NSWorkspace sharedWorkspace] runningApplications]) { - if ([runningApplication.bundleURL.path isEqual:self.host.bundlePath]) { - [runningApplication terminate]; - } - } - - [NSApp terminate:self]; + [self.updater.userUpdaterDriver terminateApplication]; } - (void)cleanUpDownload diff --git a/Sparkle/SUSparkleUserUpdaterDriver.h b/Sparkle/SUSparkleUserUpdaterDriver.h index d02c7d41..0cdefd0b 100644 --- a/Sparkle/SUSparkleUserUpdaterDriver.h +++ b/Sparkle/SUSparkleUserUpdaterDriver.h @@ -13,6 +13,11 @@ @interface SUSparkleUserUpdaterDriver : NSObject +- (instancetype)initWithHost:(SUHost *)host handlesTermination:(BOOL)handlesTermination; - (instancetype)initWithHost:(SUHost *)host; +@property (nonatomic, readonly, getter = isInstallingOnTermination) BOOL installingOnTermination; + +- (void)sendTerminationSignalWithCompletion:(void (^)(void))finishTermination; + @end diff --git a/Sparkle/SUSparkleUserUpdaterDriver.m b/Sparkle/SUSparkleUserUpdaterDriver.m index c0f6d925..cc3a03bd 100644 --- a/Sparkle/SUSparkleUserUpdaterDriver.m +++ b/Sparkle/SUSparkleUserUpdaterDriver.m @@ -34,6 +34,7 @@ @interface SUSparkleUserUpdaterDriver () @property (nonatomic, readonly) SUHost *host; +@property (nonatomic, readonly) BOOL handlesTermination; @property (nonatomic) SUStatusController *checkingController; @property (nonatomic, copy) void (^cancelUpdateCheck)(void); @@ -45,12 +46,14 @@ @property (nonatomic, copy) void (^installAndRestart)(void); @property (nonatomic, copy) void (^applicationWillTerminate)(void); +@property (nonatomic, copy) void (^finishTermination)(void); @end @implementation SUSparkleUserUpdaterDriver @synthesize host = _host; +@synthesize handlesTermination = _handlesTermination; @synthesize checkingController = _checkingController; @synthesize cancelUpdateCheck = _cancelUpdateCheck; @synthesize activeUpdateAlert = _activeUpdateAlert; @@ -58,16 +61,23 @@ @synthesize cancelDownload = _cancelDownload; @synthesize installAndRestart = _installAndRestart; @synthesize applicationWillTerminate = _applicationWillTerminate; +@synthesize finishTermination = _finishTermination; -- (instancetype)initWithHost:(SUHost *)host +- (instancetype)initWithHost:(SUHost *)host handlesTermination:(BOOL)handlesTermination { self = [super init]; if (self != nil) { _host = host; + _handlesTermination = handlesTermination; } return self; } +- (instancetype)initWithHost:(SUHost *)host +{ + return [self initWithHost:host handlesTermination:YES]; +} + - (void)requestUpdatePermissionWithSystemProfile:(NSArray *)systemProfile reply:(void (^)(SUUpdatePermissionPromptResult *))reply { dispatch_async(dispatch_get_main_queue(), ^{ @@ -332,7 +342,9 @@ self.applicationWillTerminate = applicationWillTerminate; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; + if (self.handlesTermination) { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:NSApplicationWillTerminateNotification object:nil]; + } }); } @@ -341,18 +353,46 @@ dispatch_async(dispatch_get_main_queue(), ^{ [[NSProcessInfo processInfo] enableSuddenTermination]; - [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil]; + if (self.handlesTermination) { + [[NSNotificationCenter defaultCenter] removeObserver:self name:NSApplicationWillTerminateNotification object:nil]; + } + self.applicationWillTerminate = nil; }); } - (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(); } } +- (void)terminateApplication +{ + dispatch_async(dispatch_get_main_queue(), ^{ + if (self.finishTermination) { + self.finishTermination(); + } else { + [NSApp terminate:nil]; + } + }); +} + - (void)dismissUpdateInstallation { dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/Sparkle/SUUserUpdaterDriver.h b/Sparkle/SUUserUpdaterDriver.h index a2def1d8..24c356b8 100644 --- a/Sparkle/SUUserUpdaterDriver.h +++ b/Sparkle/SUUserUpdaterDriver.h @@ -44,6 +44,7 @@ - (void)registerForAppTermination:(void (^)(void))applicationWillTerminate; - (void)unregisterForAppTermination; +- (void)terminateApplication; - (void)dismissUpdateInstallation; diff --git a/TestApplication/SUUpdateSettingsWindowController.m b/TestApplication/SUUpdateSettingsWindowController.m index 0898eab8..625d2b3e 100644 --- a/TestApplication/SUUpdateSettingsWindowController.m +++ b/TestApplication/SUUpdateSettingsWindowController.m @@ -129,6 +129,11 @@ } } +- (void)terminateApplication +{ + [NSApp terminate:nil]; +} + - (void)dismissUpdateInstallation { NSLog(@"Dismissing the installation."); @@ -148,8 +153,8 @@ - (void)windowDidLoad { - //self.updater.userUpdaterDriver = [[SUSparkleUserUpdaterDriver alloc] initWithHost:[[SUHost alloc] initWithBundle:[NSBundle mainBundle]]]; - self.updater.userUpdaterDriver = [[SULoggerUpdateDriver alloc] init]; + self.updater.userUpdaterDriver = [[SUSparkleUserUpdaterDriver alloc] initWithHost:[[SUHost alloc] initWithBundle:[NSBundle mainBundle]]]; + //self.updater.userUpdaterDriver = [[SULoggerUpdateDriver alloc] init]; } - (NSString *)windowNibName