mirror of
https://github.com/sparkle-project/Sparkle
synced 2026-11-27 05:04:27 +00:00
Improve TerminationListener by observing NSRunningApplication instance
Listening for app termination doesn't require polling anymore.
This commit is contained in:
1 parent
f82a698937
commit
64c17df1e6
5 files changed
+40
-33
No files matched your search
Vendored
+1
-1
@@ -11,7 +11,7 @@
|
||||
|
||||
@interface AppInstaller : NSObject <SUUnarchiverDelegate>
|
||||
|
||||
- (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;
|
||||
|
||||
|
||||
Vendored
+2
-2
@@ -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;
|
||||
|
||||
Vendored
+2
-11
@@ -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];
|
||||
}
|
||||
|
||||
+5
-1
@@ -8,10 +8,14 @@
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
|
||||
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
|
||||
+30
-18
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user