From 64c17df1e62e988bca664e6727ae7df3ef0a7cf1 Mon Sep 17 00:00:00 2001 From: Zorg Date: Sun, 13 Mar 2016 14:26:12 -0400 Subject: [PATCH] Improve TerminationListener by observing NSRunningApplication instance Listening for app termination doesn't require polling anymore. --- Sparkle/Autoupdate/AppInstaller.h | 2 +- Sparkle/Autoupdate/AppInstaller.m | 4 +- Sparkle/Autoupdate/Autoupdate.m | 13 +------ Sparkle/Autoupdate/TerminationListener.h | 6 ++- Sparkle/Autoupdate/TerminationListener.m | 48 +++++++++++++++--------- 5 files changed, 40 insertions(+), 33 deletions(-) diff --git a/Sparkle/Autoupdate/AppInstaller.h b/Sparkle/Autoupdate/AppInstaller.h index cb51ede2..ce82b40c 100644 --- a/Sparkle/Autoupdate/AppInstaller.h +++ b/Sparkle/Autoupdate/AppInstaller.h @@ -11,7 +11,7 @@ @interface AppInstaller : NSObject -- (instancetype)initWithHostPath:(NSString *)hostPath relaunchPath:(NSString *)relaunchPath hostProcessIdentifier:(NSNumber *)hostProcessIdentifier updateFolderPath:(NSString *)updateFolderPath downloadPath:(NSString *)downloadPath dsaSignature:(NSString *)dsaSignature; +- (instancetype)initWithHostPath:(NSString *)hostPath relaunchPath:(NSString *)relaunchPath updateFolderPath:(NSString *)updateFolderPath downloadPath:(NSString *)downloadPath dsaSignature:(NSString *)dsaSignature; - (void)extractAndInstallUpdate; diff --git a/Sparkle/Autoupdate/AppInstaller.m b/Sparkle/Autoupdate/AppInstaller.m index 36bc7455..500f7209 100644 --- a/Sparkle/Autoupdate/AppInstaller.m +++ b/Sparkle/Autoupdate/AppInstaller.m @@ -81,7 +81,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.5; * shouldRelaunch - indicates if the new installed app should re-launched * shouldShowUI - indicates if we should show the status window when installing the update */ -- (instancetype)initWithHostPath:(NSString *)hostPath relaunchPath:(NSString *)relaunchPath hostProcessIdentifier:(NSNumber *)hostProcessIdentifier updateFolderPath:(NSString *)updateFolderPath downloadPath:(NSString *)downloadPath dsaSignature:(NSString *)dsaSignature +- (instancetype)initWithHostPath:(NSString *)hostPath relaunchPath:(NSString *)relaunchPath updateFolderPath:(NSString *)updateFolderPath downloadPath:(NSString *)downloadPath dsaSignature:(NSString *)dsaSignature { if (!(self = [super init])) { return nil; @@ -91,7 +91,7 @@ static const NSTimeInterval SUTerminationTimeDelay = 0.5; self.host = [[SUHost alloc] initWithBundle:bundle]; self.relaunchPath = relaunchPath; - self.terminationListener = [[TerminationListener alloc] initWithProcessIdentifier:hostProcessIdentifier]; + self.terminationListener = [[TerminationListener alloc] initWithBundle:bundle]; self.updateFolderPath = updateFolderPath; self.downloadPath = downloadPath; self.dsaSignature = dsaSignature; diff --git a/Sparkle/Autoupdate/Autoupdate.m b/Sparkle/Autoupdate/Autoupdate.m index 4c4840d2..c4bb79ce 100644 --- a/Sparkle/Autoupdate/Autoupdate.m +++ b/Sparkle/Autoupdate/Autoupdate.m @@ -73,26 +73,17 @@ int main(int __unused argc, const char __unused *argv[]) return EXIT_SUCCESS; } - NSApplication *application = [NSApplication sharedApplication]; - - NSNumber *activeProcessIdentifier = nil; - for (NSRunningApplication *runningApplication in [[NSWorkspace sharedWorkspace] runningApplications]) { - if ([runningApplication.bundleURL.path isEqual:hostBundlePath]) { - activeProcessIdentifier = @(runningApplication.processIdentifier); - break; - } - } - AppInstaller *appInstaller = [[AppInstaller alloc] initWithHostPath:hostBundlePath relaunchPath:relaunchPath - hostProcessIdentifier:activeProcessIdentifier updateFolderPath:updateDirectoryPath downloadPath:downloadPath dsaSignature:dsaSignature]; AppDelegate *delegate = [[AppDelegate alloc] initWithAppInstaller:appInstaller]; + + NSApplication *application = [NSApplication sharedApplication]; [application setDelegate:delegate]; [application run]; } diff --git a/Sparkle/Autoupdate/TerminationListener.h b/Sparkle/Autoupdate/TerminationListener.h index 5578deb5..c939b870 100644 --- a/Sparkle/Autoupdate/TerminationListener.h +++ b/Sparkle/Autoupdate/TerminationListener.h @@ -8,10 +8,14 @@ #import +NS_ASSUME_NONNULL_BEGIN + @interface TerminationListener : NSObject -- (instancetype)initWithProcessIdentifier:(NSNumber *)processIdentifier; +- (instancetype)initWithBundle:(NSBundle *)bundle; - (void)startListeningWithCompletion:(void (^)(BOOL))completionBlock; @end + +NS_ASSUME_NONNULL_END diff --git a/Sparkle/Autoupdate/TerminationListener.m b/Sparkle/Autoupdate/TerminationListener.m index c6b3bf35..03e22798 100644 --- a/Sparkle/Autoupdate/TerminationListener.m +++ b/Sparkle/Autoupdate/TerminationListener.m @@ -8,55 +8,67 @@ #import "TerminationListener.h" -/*! - * Time this app uses to recheck if the host app has already died. - */ -static const NSTimeInterval SUParentQuitCheckInterval = .25; - @interface TerminationListener () -@property (nonatomic, strong) NSNumber *processIdentifier; -@property (nonatomic, strong) NSTimer *watchdogTimer; +@property (nonatomic, readonly) NSRunningApplication *runningApplication; +@property (nonatomic, copy) void (^completionBlock)(BOOL); @end @implementation TerminationListener -@synthesize processIdentifier = _processIdentifier; -@synthesize watchdogTimer = _watchdogTimer; +@synthesize runningApplication = _runningApplication; +@synthesize completionBlock = _completionBlock; -- (instancetype)initWithProcessIdentifier:(NSNumber *)processIdentifier +- (instancetype)initWithBundle:(NSBundle *)bundle { if (!(self = [super init])) { return nil; } - self.processIdentifier = processIdentifier; + NSString *bundlePath = bundle.bundlePath; + NSString *bundleIdentifier = bundle.bundleIdentifier; + + NSArray *runningApplications = + (bundleIdentifier != nil) ? + [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleIdentifier] : + [[NSWorkspace sharedWorkspace] runningApplications]; + + for (NSRunningApplication *runningApplication in runningApplications) { + // Comparing the URLs hasn't worked well for me in practice, so I'm comparing the file paths instead + if ([runningApplication.bundleURL.path isEqualToString:bundlePath]) { + _runningApplication = runningApplication; + break; + } + } return self; } - (void)cleanupWithSuccess:(BOOL)success completion:(void (^)(BOOL))completionBlock { - [self.watchdogTimer invalidate]; - completionBlock(success); } - (void)startListeningWithCompletion:(void (^)(BOOL))completionBlock { - BOOL alreadyTerminated = (self.processIdentifier == nil || (kill(self.processIdentifier.intValue, 0) != 0)); + BOOL alreadyTerminated = (self.runningApplication == nil || self.runningApplication.isTerminated); if (alreadyTerminated) { [self cleanupWithSuccess:YES completion:completionBlock]; } else { - self.watchdogTimer = [NSTimer scheduledTimerWithTimeInterval:SUParentQuitCheckInterval target:self selector:@selector(watchdog:) userInfo:completionBlock repeats:YES]; + self.completionBlock = completionBlock; + [self.runningApplication addObserver:self forKeyPath:NSStringFromSelector(@selector(isTerminated)) options:NSKeyValueObservingOptionNew context:NULL]; } } -- (void)watchdog:(NSTimer *)timer +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)__unused change context:(void *)__unused context { - if ([NSRunningApplication runningApplicationWithProcessIdentifier:self.processIdentifier.intValue] == nil) { - [self cleanupWithSuccess:YES completion:timer.userInfo]; + if (object == self.runningApplication && [keyPath isEqualToString:NSStringFromSelector(@selector(isTerminated))]) { + if (self.runningApplication.isTerminated) { + [self.runningApplication removeObserver:self forKeyPath:keyPath]; + [self cleanupWithSuccess:YES completion:self.completionBlock]; + self.completionBlock = nil; + } } }