Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 39 additions & 59 deletions Quicksilver/Code-QuickStepCore/QSProcessMonitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,12 @@ - (void)dealloc {
}

- (QSObject *)imbuedFileProcessForDict:(NSDictionary *)dict {
NSString *ident = [dict objectForKey:@"NSApplicationBundleIdentifier"];
NSString *appPath = [dict objectForKey:@"NSApplicationPath"];
// We used to call the (slow) [NSBundle bundleWithIdentifier:ident], but we can pull this from dict instead

// We used to call the (slow) [NSBundle bundleWithIdentifier:], but we can pull BundlePath from dict instead
NSString *bundlePath = [dict objectForKey:@"BundlePath"];
QSObject *newObject = nil;
if ([appPath isEqualToString: bundlePath]) {
if ([appPath isEqualToString:bundlePath]) {
newObject = [QSObject fileObjectWithPath:bundlePath];
// NSLog(@"%@ %@", bundlePath, newObject);
}
Expand Down Expand Up @@ -223,11 +222,11 @@ - (QSObject *)processObjectWithDict:(NSDictionary *)dict {
}

- (NSDictionary *)infoForApp:(NSRunningApplication *)app {

ProcessSerialNumber psn;
GetProcessForPID(app.processIdentifier, &psn);


NSDictionary *dict = [[NSDictionary dictionaryWithObjectsAndKeys:
[app localizedName], @"NSApplicationName",
[[app bundleURL] path], @"NSApplicationPath",
Expand All @@ -238,34 +237,19 @@ - (NSDictionary *)infoForApp:(NSRunningApplication *)app {
[NSNumber numberWithLong:psn.highLongOfPSN], @"NSApplicationProcessSerialNumberHigh",
[NSNumber numberWithLong:psn.lowLongOfPSN], @"NSApplicationProcessSerialNumberLow",
nil] mutableCopy];

return dict;
}

// Helper method to determine whether we want to include a process in the processes dict
- (BOOL)includeApp:(NSRunningApplication *)app {

// Background processes are excluded unless QSShowBackgroundProcesses is set
if ((app.activationPolicy == NSApplicationActivationPolicyProhibited) &&
![[NSUserDefaults standardUserDefaults] boolForKey:@"QSShowBackgroundProcesses"])
return NO;

// Get bundle info, if available
NSDictionary *bundleInfo;
if(app.bundleURL)
bundleInfo = [[NSBundle bundleWithURL:app.bundleURL] infoDictionary];
else
bundleInfo = nil;

// Determine bundle type
NSString *fileType;
fileType = bundleInfo ? [bundleInfo objectForKey:@"CFBundlePackageType"] : @"NIL ";

// Exclude all processes except for APPL (regular apps) and FNDR (Finder)
if(![fileType isEqualToString:@"APPL"] &&
![fileType isEqualToString:@"FNDR"])
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QSShowBackgroundProcesses"]) {
return YES;
}
// Exclude XPC services
if ([[[app.bundleURL path] pathExtension] isEqualToString:@"xpc"]) {
return NO;

}
return YES;
}

Expand Down Expand Up @@ -294,20 +278,22 @@ - (NSDictionary *)infoForPSN:(ProcessSerialNumber)processSerialNumber {
}

// Helper method to determine whether we want to include a process in the processes dict
// Calls through to the NSRunningApplication-based method
- (BOOL)includePSN:(ProcessSerialNumber)processSerialNumber {
pid_t pid;
NSRunningApplication *app;

GetProcessPID(&processSerialNumber, &pid);
app = [NSRunningApplication runningApplicationWithProcessIdentifier: pid];
if(app)
return [self includeApp: app];

return NO;
// Used in appLaunched: where we have Carbon process info from infoForPSN:
- (BOOL)includePSN:(NSDictionary *)processInfo {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QSShowBackgroundProcesses"]) {
return YES;
}
// Exclude background-only processes
if ([[processInfo objectForKey:@"LSBackgroundOnly"] boolValue]) {
return NO;
}
// Exclude XPC services
if ([[[processInfo objectForKey:@"NSApplicationPath"] pathExtension] isEqualToString:@"xpc"]) {
return NO;
}
return YES;
}


#pragma mark -
#pragma mark Process Notifications

Expand Down Expand Up @@ -337,13 +323,8 @@ - (BOOL)handleProcessEvent:(NSEvent *)theEvent {

- (void)appLaunched:(ProcessSerialNumber)psn {
NSDictionary *dict = [self infoForPSN:psn];

// if we're including background processes (i.e. all processes) OR if it's a foreground process, reload
if ([self includePSN: psn]) {

#ifdef DEBUG
NSLog(@"appLaunched: NSApplicationPath=%@", [dict objectForKey:@"NSApplicationPath"]);
#endif

if ([self includePSN:dict]) {
[self reloadProcesses];
}
if (dict) {
Expand Down Expand Up @@ -446,20 +427,19 @@ - (void)_reloadProcesses {

NSArray *tempProcesses = [[NSWorkspace sharedWorkspace] runningApplications];
NSMutableDictionary *procs = [[NSMutableDictionary alloc] initWithCapacity:tempProcesses.count];


for (NSRunningApplication *app in tempProcesses) {
if ([self includeApp: app]) {
NSDictionary *info = [self infoForApp:app];
QSObject *procObject = [self imbuedFileProcessForDict:info];
NSNumber *pidValue = [NSNumber numberWithInt:app.processIdentifier];

if (procObject) {
[procs setObject:procObject forKey:pidValue];
}
if (![self includeApp:app]) {
continue;
}

NSDictionary *info = [self infoForApp:app];
QSObject *procObject = [self imbuedFileProcessForDict:info];
NSNumber *pidValue = [NSNumber numberWithInt:app.processIdentifier];

if (procObject) {
[procs setObject:procObject forKey:pidValue];
}
}

processes = [procs copy];
#ifdef DEBUG
NSLog(@"_reloadProcesses: Reload time: %f ms for %d processes", [date timeIntervalSinceNow]*-1000, (int) procs.count);
Expand Down