Skip to content

Commit 537259c

Browse files
authored
Merge pull request #3090 from quicksilver/pjrobertson/process-tweaks
Further tweaks to process checks
2 parents a8b28e8 + e9561c7 commit 537259c

File tree

1 file changed

+39
-59
lines changed

1 file changed

+39
-59
lines changed

Quicksilver/Code-QuickStepCore/QSProcessMonitor.m

Lines changed: 39 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,12 @@ - (void)dealloc {
165165
}
166166

167167
- (QSObject *)imbuedFileProcessForDict:(NSDictionary *)dict {
168-
NSString *ident = [dict objectForKey:@"NSApplicationBundleIdentifier"];
169168
NSString *appPath = [dict objectForKey:@"NSApplicationPath"];
170-
171-
// We used to call the (slow) [NSBundle bundleWithIdentifier:ident], but we can pull this from dict instead
169+
170+
// We used to call the (slow) [NSBundle bundleWithIdentifier:], but we can pull BundlePath from dict instead
172171
NSString *bundlePath = [dict objectForKey:@"BundlePath"];
173172
QSObject *newObject = nil;
174-
if ([appPath isEqualToString: bundlePath]) {
173+
if ([appPath isEqualToString:bundlePath]) {
175174
newObject = [QSObject fileObjectWithPath:bundlePath];
176175
// NSLog(@"%@ %@", bundlePath, newObject);
177176
}
@@ -223,11 +222,11 @@ - (QSObject *)processObjectWithDict:(NSDictionary *)dict {
223222
}
224223

225224
- (NSDictionary *)infoForApp:(NSRunningApplication *)app {
226-
225+
227226
ProcessSerialNumber psn;
228227
GetProcessForPID(app.processIdentifier, &psn);
229-
230-
228+
229+
231230
NSDictionary *dict = [[NSDictionary dictionaryWithObjectsAndKeys:
232231
[app localizedName], @"NSApplicationName",
233232
[[app bundleURL] path], @"NSApplicationPath",
@@ -238,34 +237,19 @@ - (NSDictionary *)infoForApp:(NSRunningApplication *)app {
238237
[NSNumber numberWithLong:psn.highLongOfPSN], @"NSApplicationProcessSerialNumberHigh",
239238
[NSNumber numberWithLong:psn.lowLongOfPSN], @"NSApplicationProcessSerialNumberLow",
240239
nil] mutableCopy];
241-
240+
242241
return dict;
243242
}
244243

245244
// Helper method to determine whether we want to include a process in the processes dict
246245
- (BOOL)includeApp:(NSRunningApplication *)app {
247-
248-
// Background processes are excluded unless QSShowBackgroundProcesses is set
249-
if ((app.activationPolicy == NSApplicationActivationPolicyProhibited) &&
250-
![[NSUserDefaults standardUserDefaults] boolForKey:@"QSShowBackgroundProcesses"])
251-
return NO;
252-
253-
// Get bundle info, if available
254-
NSDictionary *bundleInfo;
255-
if(app.bundleURL)
256-
bundleInfo = [[NSBundle bundleWithURL:app.bundleURL] infoDictionary];
257-
else
258-
bundleInfo = nil;
259-
260-
// Determine bundle type
261-
NSString *fileType;
262-
fileType = bundleInfo ? [bundleInfo objectForKey:@"CFBundlePackageType"] : @"NIL ";
263-
264-
// Exclude all processes except for APPL (regular apps) and FNDR (Finder)
265-
if(![fileType isEqualToString:@"APPL"] &&
266-
![fileType isEqualToString:@"FNDR"])
246+
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QSShowBackgroundProcesses"]) {
247+
return YES;
248+
}
249+
// Exclude XPC services
250+
if ([[[app.bundleURL path] pathExtension] isEqualToString:@"xpc"]) {
267251
return NO;
268-
252+
}
269253
return YES;
270254
}
271255

@@ -294,20 +278,22 @@ - (NSDictionary *)infoForPSN:(ProcessSerialNumber)processSerialNumber {
294278
}
295279

296280
// Helper method to determine whether we want to include a process in the processes dict
297-
// Calls through to the NSRunningApplication-based method
298-
- (BOOL)includePSN:(ProcessSerialNumber)processSerialNumber {
299-
pid_t pid;
300-
NSRunningApplication *app;
301-
302-
GetProcessPID(&processSerialNumber, &pid);
303-
app = [NSRunningApplication runningApplicationWithProcessIdentifier: pid];
304-
if(app)
305-
return [self includeApp: app];
306-
307-
return NO;
281+
// Used in appLaunched: where we have Carbon process info from infoForPSN:
282+
- (BOOL)includePSN:(NSDictionary *)processInfo {
283+
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QSShowBackgroundProcesses"]) {
284+
return YES;
285+
}
286+
// Exclude background-only processes
287+
if ([[processInfo objectForKey:@"LSBackgroundOnly"] boolValue]) {
288+
return NO;
289+
}
290+
// Exclude XPC services
291+
if ([[[processInfo objectForKey:@"NSApplicationPath"] pathExtension] isEqualToString:@"xpc"]) {
292+
return NO;
293+
}
294+
return YES;
308295
}
309296

310-
311297
#pragma mark -
312298
#pragma mark Process Notifications
313299

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

338324
- (void)appLaunched:(ProcessSerialNumber)psn {
339325
NSDictionary *dict = [self infoForPSN:psn];
340-
341-
// if we're including background processes (i.e. all processes) OR if it's a foreground process, reload
342-
if ([self includePSN: psn]) {
343-
344-
#ifdef DEBUG
345-
NSLog(@"appLaunched: NSApplicationPath=%@", [dict objectForKey:@"NSApplicationPath"]);
346-
#endif
326+
327+
if ([self includePSN:dict]) {
347328
[self reloadProcesses];
348329
}
349330
if (dict) {
@@ -446,20 +427,19 @@ - (void)_reloadProcesses {
446427

447428
NSArray *tempProcesses = [[NSWorkspace sharedWorkspace] runningApplications];
448429
NSMutableDictionary *procs = [[NSMutableDictionary alloc] initWithCapacity:tempProcesses.count];
449-
450-
451430
for (NSRunningApplication *app in tempProcesses) {
452-
if ([self includeApp: app]) {
453-
NSDictionary *info = [self infoForApp:app];
454-
QSObject *procObject = [self imbuedFileProcessForDict:info];
455-
NSNumber *pidValue = [NSNumber numberWithInt:app.processIdentifier];
456-
457-
if (procObject) {
458-
[procs setObject:procObject forKey:pidValue];
459-
}
431+
if (![self includeApp:app]) {
432+
continue;
433+
}
434+
435+
NSDictionary *info = [self infoForApp:app];
436+
QSObject *procObject = [self imbuedFileProcessForDict:info];
437+
NSNumber *pidValue = [NSNumber numberWithInt:app.processIdentifier];
438+
439+
if (procObject) {
440+
[procs setObject:procObject forKey:pidValue];
460441
}
461442
}
462-
463443
processes = [procs copy];
464444
#ifdef DEBUG
465445
NSLog(@"_reloadProcesses: Reload time: %f ms for %d processes", [date timeIntervalSinceNow]*-1000, (int) procs.count);

0 commit comments

Comments
 (0)