uv: Upgrade to v0.10.19
[platform/upstream/nodejs.git] / deps / uv / src / unix / darwin-proctitle.c
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  * Permission is hereby granted, free of charge, to any person obtaining a copy
3  * of this software and associated documentation files (the "Software"), to
4  * deal in the Software without restriction, including without limitation the
5  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6  * sell copies of the Software, and to permit persons to whom the Software is
7  * furnished to do so, subject to the following conditions:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18  * IN THE SOFTWARE.
19  */
20
21 #include <TargetConditionals.h>
22
23 #if !TARGET_OS_IPHONE
24 # include <CoreFoundation/CoreFoundation.h>
25 # include <ApplicationServices/ApplicationServices.h>
26 #endif
27
28
29 int uv__set_process_title(const char* title) {
30 #if TARGET_OS_IPHONE
31   return -1;
32 #else
33   typedef CFTypeRef (*LSGetCurrentApplicationASNType)(void);
34   typedef OSStatus (*LSSetApplicationInformationItemType)(int,
35                                                           CFTypeRef,
36                                                           CFStringRef,
37                                                           CFStringRef,
38                                                           CFDictionaryRef*);
39   typedef CFDictionaryRef (*LSApplicationCheckInType)(int, CFDictionaryRef);
40   typedef OSStatus (*SetApplicationIsDaemonType)(int);
41   typedef void (*LSSetApplicationLaunchServicesServerConnectionStatusType)(
42       uint64_t, void*);
43   CFBundleRef launch_services_bundle;
44   LSGetCurrentApplicationASNType ls_get_current_application_asn;
45   LSSetApplicationInformationItemType ls_set_application_information_item;
46   CFStringRef* display_name_key;
47   CFTypeRef asn;
48   CFStringRef display_name;
49   OSStatus err;
50   CFBundleRef hi_services_bundle;
51   LSApplicationCheckInType ls_application_check_in;
52   SetApplicationIsDaemonType set_application_is_daemon;
53   LSSetApplicationLaunchServicesServerConnectionStatusType
54       ls_set_application_launch_services_server_connection_status;
55
56   launch_services_bundle =
57       CFBundleGetBundleWithIdentifier(CFSTR("com.apple.LaunchServices"));
58
59   if (launch_services_bundle == NULL)
60     return -1;
61
62   ls_get_current_application_asn = (LSGetCurrentApplicationASNType)
63       CFBundleGetFunctionPointerForName(launch_services_bundle,
64                                         CFSTR("_LSGetCurrentApplicationASN"));
65
66   if (ls_get_current_application_asn == NULL)
67     return -1;
68
69   ls_set_application_information_item = (LSSetApplicationInformationItemType)
70       CFBundleGetFunctionPointerForName(launch_services_bundle,
71                                         CFSTR("_LSSetApplicationInformationItem"));
72
73   if (ls_set_application_information_item == NULL)
74     return -1;
75
76   display_name_key = CFBundleGetDataPointerForName(launch_services_bundle,
77                                                    CFSTR("_kLSDisplayNameKey"));
78
79   if (display_name_key == NULL || *display_name_key == NULL)
80     return -1;
81
82   /* Black 10.9 magic, to remove (Not responding) mark in Activity Monitor */
83   hi_services_bundle =
84       CFBundleGetBundleWithIdentifier(CFSTR("com.apple.HIServices"));
85   if (hi_services_bundle == NULL)
86     return -1;
87
88   set_application_is_daemon = CFBundleGetFunctionPointerForName(
89       hi_services_bundle,
90       CFSTR("SetApplicationIsDaemon"));
91   ls_application_check_in = CFBundleGetFunctionPointerForName(
92       launch_services_bundle,
93       CFSTR("_LSApplicationCheckIn"));
94   ls_set_application_launch_services_server_connection_status =
95       CFBundleGetFunctionPointerForName(
96           launch_services_bundle,
97           CFSTR("_LSSetApplicationLaunchServicesServerConnectionStatus"));
98   if (set_application_is_daemon == NULL ||
99       ls_application_check_in == NULL ||
100       ls_set_application_launch_services_server_connection_status == NULL) {
101     return -1;
102   }
103
104   if (set_application_is_daemon(1) != noErr)
105     return -1;
106
107   ls_set_application_launch_services_server_connection_status(0, NULL);
108
109   /* Check into process manager?! */
110   ls_application_check_in(-2,
111                           CFBundleGetInfoDictionary(CFBundleGetMainBundle()));
112
113   display_name = CFStringCreateWithCString(NULL, title, kCFStringEncodingUTF8);
114   asn = ls_get_current_application_asn();
115   err = ls_set_application_information_item(-2,  /* Magic value. */
116                                             asn,
117                                             *display_name_key,
118                                             display_name,
119                                             NULL);
120
121   return (err == noErr) ? 0 : -1;
122 #endif  /* !TARGET_OS_IPHONE */
123 }