Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

darwin: remove CoreFoundation dependency #898

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
3 changes: 0 additions & 3 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ libuv_la_SOURCES += src/unix/darwin.c \
src/unix/fsevents.c \
src/unix/kqueue.c \
src/unix/proctitle.c
libuv_la_LDFLAGS += -framework ApplicationServices \
-framework CoreServices \
-framework Foundation
endif

if FREEBSD
Expand Down
123 changes: 86 additions & 37 deletions src/unix/darwin-proctitle.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,62 +55,111 @@ int uv__set_process_title(const char* title) {
#if TARGET_OS_IPHONE
return uv__pthread_setname_np(title);
#else
typedef CFTypeRef (*LSGetCurrentApplicationASNType)(void);
typedef OSStatus (*LSSetApplicationInformationItemType)(int,
CFTypeRef,
CFStringRef,
CFStringRef,
CFDictionaryRef*);
CFStringRef (*pCFStringCreateWithCString)(CFAllocatorRef,
const char*,
CFStringEncoding);
CFBundleRef (*pCFBundleGetBundleWithIdentifier)(CFStringRef);
void *(*pCFBundleGetDataPointerForName)(CFBundleRef, CFStringRef);
void *(*pCFBundleGetFunctionPointerForName)(CFBundleRef, CFStringRef);
OSErr (*pGetCurrentProcess)(ProcessSerialNumber*);
CFTypeRef (*pLSGetCurrentApplicationASN)(void);
OSStatus (*pLSSetApplicationInformationItem)(int,
CFTypeRef,
CFStringRef,
CFStringRef,
CFDictionaryRef*);
void* application_services_handle;
void* core_foundation_handle;
CFBundleRef launch_services_bundle;
LSGetCurrentApplicationASNType ls_get_current_application_asn;
LSSetApplicationInformationItemType ls_set_application_information_item;
CFStringRef* display_name_key;
ProcessSerialNumber psn;
CFTypeRef asn;
CFStringRef display_name;
OSStatus err;
int err;

err = -ENOENT;
application_services_handle = dlopen("/System/Library/Frameworks/"
"ApplicationServices.framework/"
"Versions/A/ApplicationServices",
RTLD_LAZY | RTLD_LOCAL);
core_foundation_handle = dlopen("/System/Library/Frameworks/"
"CoreFoundation.framework/"
"Versions/A/CoreFoundation",
RTLD_LAZY | RTLD_LOCAL);

if (application_services_handle == NULL || core_foundation_handle == NULL)
goto out;

pGetCurrentProcess =
dlsym(application_services_handle, "GetCurrentProcess");
pCFStringCreateWithCString =
dlsym(core_foundation_handle, "CFStringCreateWithCString");
pCFBundleGetBundleWithIdentifier =
dlsym(core_foundation_handle, "CFBundleGetBundleWithIdentifier");
pCFBundleGetDataPointerForName =
dlsym(core_foundation_handle, "CFBundleGetDataPointerForName");
pCFBundleGetFunctionPointerForName =
dlsym(core_foundation_handle, "CFBundleGetFunctionPointerForName");

if (pGetCurrentProcess == NULL ||
pCFStringCreateWithCString == NULL ||
pCFBundleGetBundleWithIdentifier == NULL ||
pCFBundleGetDataPointerForName == NULL ||
pCFBundleGetFunctionPointerForName == NULL) {
goto out;
}

#define S(s) pCFStringCreateWithCString(NULL, (s), kCFStringEncodingUTF8)

launch_services_bundle =
CFBundleGetBundleWithIdentifier(CFSTR("com.apple.LaunchServices"));
pCFBundleGetBundleWithIdentifier(S("com.apple.LaunchServices"));

if (launch_services_bundle == NULL)
return -ENOENT;
goto out;

ls_get_current_application_asn = (LSGetCurrentApplicationASNType)
CFBundleGetFunctionPointerForName(launch_services_bundle,
CFSTR("_LSGetCurrentApplicationASN"));
pLSGetCurrentApplicationASN =
pCFBundleGetFunctionPointerForName(launch_services_bundle,
S("_LSGetCurrentApplicationASN"));

if (ls_get_current_application_asn == NULL)
return -ENOENT;
if (pLSGetCurrentApplicationASN == NULL)
goto out;

ls_set_application_information_item = (LSSetApplicationInformationItemType)
CFBundleGetFunctionPointerForName(launch_services_bundle,
CFSTR("_LSSetApplicationInformationItem"));
pLSSetApplicationInformationItem =
pCFBundleGetFunctionPointerForName(launch_services_bundle,
S("_LSSetApplicationInformationItem"));

if (ls_set_application_information_item == NULL)
return -ENOENT;
if (pLSSetApplicationInformationItem == NULL)
goto out;

display_name_key = CFBundleGetDataPointerForName(launch_services_bundle,
CFSTR("_kLSDisplayNameKey"));
display_name_key = pCFBundleGetDataPointerForName(launch_services_bundle,
S("_kLSDisplayNameKey"));

if (display_name_key == NULL || *display_name_key == NULL)
return -ENOENT;
goto out;

/* Force the process manager to initialize. */
GetCurrentProcess(&psn);

display_name = CFStringCreateWithCString(NULL, title, kCFStringEncodingUTF8);
asn = ls_get_current_application_asn();
err = ls_set_application_information_item(-2, /* Magic value. */
asn,
*display_name_key,
display_name,
NULL);
if (err != noErr)
return -ENOENT;
pGetCurrentProcess(&psn);

asn = pLSGetCurrentApplicationASN();

err = -EINVAL;
if (pLSSetApplicationInformationItem(-2, /* Magic value. */
asn,
*display_name_key,
S(title),
NULL) != noErr) {
goto out;
}

uv__pthread_setname_np(title); /* Don't care if it fails. */
err = 0;

return 0;
out:
if (core_foundation_handle != NULL)
dlclose(core_foundation_handle);

if (application_services_handle != NULL)
dlclose(application_services_handle);

return err;
#endif /* !TARGET_OS_IPHONE */
}
Loading