-
Notifications
You must be signed in to change notification settings - Fork 234
Make process name also match bundle name and bundle id #399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,58 @@ find_active_process_name(void) | |
| GetFrontProcess(&psn); | ||
| return find_process_name_for_psn(&psn); | ||
| } | ||
|
|
||
| static inline char * | ||
| find_process_bundle_id_for_psn(ProcessSerialNumber *psn) | ||
| { | ||
| CFDictionaryRef process_info_ref = ProcessInformationCopyDictionary(psn, kProcessDictionaryIncludeAllInformationMask); | ||
| if (!process_info_ref) { | ||
| return NULL; | ||
| } | ||
| CFStringRef process_bundle_id_ref = CFDictionaryGetValue(process_info_ref, kCFBundleIdentifierKey); | ||
| CFRelease(process_info_ref); | ||
| if (!process_bundle_id_ref) { | ||
| return NULL; | ||
| } | ||
| char *process_bundle_id = copy_cfstring(process_bundle_id_ref); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copy_cfstring_to_cstring will cause memory leak if it fails. How are we ensuring it will not return NULL and dereference?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. Added checks for null value. |
||
| CFRelease(process_bundle_id_ref); | ||
| for (char *s = process_bundle_id; *s; ++s) *s = tolower(*s); | ||
| return process_bundle_id; | ||
| } | ||
|
|
||
| static inline char * | ||
| find_active_process_bundle_id(void) | ||
| { | ||
| ProcessSerialNumber psn; | ||
| GetFrontProcess(&psn); | ||
| return find_process_bundle_id_for_psn(&psn); | ||
| } | ||
|
|
||
| static inline char * | ||
| find_process_bundle_name_for_psn(ProcessSerialNumber *psn) | ||
| { | ||
| CFDictionaryRef process_info_ref = ProcessInformationCopyDictionary(psn, kProcessDictionaryIncludeAllInformationMask); | ||
| if (!process_info_ref) { | ||
| return NULL; | ||
| } | ||
| CFStringRef process_bundle_name_ref = CFDictionaryGetValue(process_info_ref, kCFBundleNameKey); | ||
| CFRelease(process_info_ref); | ||
| if (process_bundle_name_ref) { | ||
| return NULL; | ||
| } | ||
| char *process_bundle_name = copy_cfstring(process_bundle_name_ref); | ||
| CFRelease(process_bundle_name_ref); | ||
| for (char *s = process_bundle_name; *s; ++s) *s = tolower(*s); | ||
| return process_bundle_name; | ||
| } | ||
|
|
||
| static inline char * | ||
| find_active_process_bundle_name(void) | ||
| { | ||
| ProcessSerialNumber psn; | ||
| GetFrontProcess(&psn); | ||
| return find_process_bundle_name_for_psn(&psn); | ||
| } | ||
| #pragma clang diagnostic pop | ||
|
|
||
| static OSStatus | ||
|
|
@@ -55,6 +107,20 @@ carbon_event_handler(EventHandlerCallRef ref, EventRef event, void *context) | |
|
|
||
| carbon->process_name = find_process_name_for_psn(&psn); | ||
|
|
||
| if (carbon->process_bundle_id) { | ||
| free(carbon->process_bundle_id); | ||
| carbon->process_bundle_id = NULL; | ||
| } | ||
|
|
||
| carbon->process_bundle_id = find_process_bundle_id_for_psn(&psn); | ||
|
|
||
| if (carbon->process_bundle_name) { | ||
| free(carbon->process_bundle_name); | ||
| carbon->process_bundle_name = NULL; | ||
| } | ||
|
|
||
| carbon->process_bundle_name = find_process_bundle_name_for_psn(&psn); | ||
|
|
||
| return noErr; | ||
| } | ||
|
|
||
|
|
@@ -65,6 +131,8 @@ bool carbon_event_init(struct carbon_event *carbon) | |
| carbon->type.eventClass = kEventClassApplication; | ||
| carbon->type.eventKind = kEventAppFrontSwitched; | ||
| carbon->process_name = find_active_process_name(); | ||
| carbon->process_bundle_id = find_active_process_bundle_id(); | ||
| carbon->process_bundle_name = find_active_process_bundle_name(); | ||
|
|
||
| return InstallEventHandler(carbon->target, | ||
| carbon->handler, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we assume this call will never return NULL?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not according to my tests and I can't find any documentation since it's long deprecated. But I'll add checks just to be sure.