2 * Copyright (c) 2014 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 /* CFUserNotification.c
25 Copyright (c) 2000-2013, Apple Inc. All rights reserved.
26 Original Author: Doug Davidson
27 Responsibility: Kevin Perry
30 #include <CoreFoundation/CFUserNotification.h>
31 #include <CoreFoundation/CFPropertyList.h>
32 #include <CoreFoundation/CFNumber.h>
33 #include <CoreFoundation/CFRunLoop.h>
34 #include "CFInternal.h"
35 #include <CoreFoundation/CFMachPort.h>
39 #include <mach/mach.h>
40 #include <mach/error.h>
41 #include <bootstrap_priv.h>
46 #define CFUserNotificationLog(alertHeader, alertMessage) CFLog(3, CFSTR("%@: %@"), alertHeader, alertMessage);
49 kCFUserNotificationCancelFlag
= (1 << 3),
50 kCFUserNotificationUpdateFlag
= (1 << 4)
53 CONST_STRING_DECL(kCFUserNotificationTokenKey
, "Token")
54 CONST_STRING_DECL(kCFUserNotificationTimeoutKey
, "Timeout")
55 CONST_STRING_DECL(kCFUserNotificationFlagsKey
, "Flags")
56 CONST_STRING_DECL(kCFUserNotificationIconPathKey
, "IconPath")
57 CONST_STRING_DECL(kCFUserNotificationSoundPathKey
, "SoundPath")
58 CONST_STRING_DECL(kCFUserNotificationLocalizationPathKey
, "LocalizationPath")
59 CONST_STRING_DECL(kCFUserNotificationAlertSourceKey
, "AlertSource")
60 CONST_STRING_DECL(kCFUserNotificationTextFieldLabelsKey
, "TextFieldTitles")
61 CONST_STRING_DECL(kCFUserNotificationCheckBoxLabelsKey
, "CheckBoxTitles")
62 CONST_STRING_DECL(kCFUserNotificationIconURLKey
, "IconURL")
63 CONST_STRING_DECL(kCFUserNotificationSoundURLKey
, "SoundURL")
64 CONST_STRING_DECL(kCFUserNotificationLocalizationURLKey
, "LocalizationURL")
65 CONST_STRING_DECL(kCFUserNotificationAlertHeaderKey
, "AlertHeader")
66 CONST_STRING_DECL(kCFUserNotificationAlertMessageKey
, "AlertMessage")
67 CONST_STRING_DECL(kCFUserNotificationDefaultButtonTitleKey
, "DefaultButtonTitle")
68 CONST_STRING_DECL(kCFUserNotificationAlternateButtonTitleKey
, "AlternateButtonTitle")
69 CONST_STRING_DECL(kCFUserNotificationOtherButtonTitleKey
, "OtherButtonTitle")
70 CONST_STRING_DECL(kCFUserNotificationProgressIndicatorValueKey
, "ProgressIndicatorValue")
71 CONST_STRING_DECL(kCFUserNotificationSessionIDKey
, "SessionID")
72 CONST_STRING_DECL(kCFUserNotificationPopUpTitlesKey
, "PopUpTitles")
73 CONST_STRING_DECL(kCFUserNotificationTextFieldTitlesKey
, "TextFieldTitles")
74 CONST_STRING_DECL(kCFUserNotificationCheckBoxTitlesKey
, "CheckBoxTitles")
75 CONST_STRING_DECL(kCFUserNotificationTextFieldValuesKey
, "TextFieldValues")
76 CONST_STRING_DECL(kCFUserNotificationPopUpSelectionKey
, "PopUpSelection")
77 CONST_STRING_DECL(kCFUserNotificationKeyboardTypesKey
, "KeyboardTypes")
78 CONST_STRING_DECL(kCFUserNotificationAlertTopMostKey
, "AlertTopMost") // boolean value
81 static CFTypeID __kCFUserNotificationTypeID
= _kCFRuntimeNotATypeID
;
83 struct __CFUserNotification
{
87 CFTimeInterval _timeout
;
88 CFOptionFlags _requestFlags
;
89 CFOptionFlags _responseFlags
;
90 CFStringRef _sessionID
;
91 CFDictionaryRef _responseDictionary
;
92 CFMachPortRef _machPort
;
93 CFUserNotificationCallBack _callout
;
96 static CFStringRef
__CFUserNotificationCopyDescription(CFTypeRef cf
) {
97 CFMutableStringRef result
;
98 result
= CFStringCreateMutable(CFGetAllocator(cf
), 0);
99 CFStringAppendFormat(result
, NULL
, CFSTR("<CFUserNotification %p>"), cf
);
103 #define MAX_STRING_LENGTH PATH_MAX
104 #define MAX_STRING_COUNT 16
105 #define MAX_PORT_NAME_LENGTH 63
106 #define NOTIFICATION_PORT_NAME_SUFFIX ".session."
107 #define MESSAGE_TIMEOUT 100
108 #if DEPLOYMENT_TARGET_MACOSX
109 #define NOTIFICATION_PORT_NAME "com.apple.UNCUserNotification"
110 #elif DEPLOYMENT_TARGET_EMBEDDED
111 #define NOTIFICATION_PORT_NAME "com.apple.SBUserNotification"
113 #error Unknown or unspecified DEPLOYMENT_TARGET
117 static void __CFUserNotificationDeallocate(CFTypeRef cf
);
119 static const CFRuntimeClass __CFUserNotificationClass
= {
121 "CFUserNotification",
124 __CFUserNotificationDeallocate
,
128 __CFUserNotificationCopyDescription
131 CF_PRIVATE
void __CFUserNotificationInitialize(void) {
132 __kCFUserNotificationTypeID
= _CFRuntimeRegisterClass(&__CFUserNotificationClass
);
135 CFTypeID
CFUserNotificationGetTypeID(void) {
136 if (_kCFRuntimeNotATypeID
== __kCFUserNotificationTypeID
) __CFUserNotificationInitialize();
137 return __kCFUserNotificationTypeID
;
140 static void __CFUserNotificationDeallocate(CFTypeRef cf
) {
141 CFUserNotificationRef userNotification
= (CFUserNotificationRef
)cf
;
142 if (userNotification
->_machPort
) {
143 CFMachPortInvalidate(userNotification
->_machPort
);
144 CFRelease(userNotification
->_machPort
);
145 } else if (MACH_PORT_NULL
!= userNotification
->_replyPort
) {
146 mach_port_destroy(mach_task_self(), userNotification
->_replyPort
);
148 if (userNotification
->_sessionID
) CFRelease(userNotification
->_sessionID
);
149 if (userNotification
->_responseDictionary
) CFRelease(userNotification
->_responseDictionary
);
152 static void _CFUserNotificationAddToDictionary(const void *key
, const void *value
, void *context
) {
153 if (CFGetTypeID(key
) == CFStringGetTypeID()) CFDictionarySetValue((CFMutableDictionaryRef
)context
, key
, value
);
156 static CFDictionaryRef
_CFUserNotificationModifiedDictionary(CFAllocatorRef allocator
, CFDictionaryRef dictionary
, SInt32 token
, SInt32 timeout
, CFStringRef source
) {
157 CFMutableDictionaryRef md
= CFDictionaryCreateMutable(allocator
, 0, &kCFCopyStringDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
158 CFNumberRef tokenNumber
= CFNumberCreate(allocator
, kCFNumberSInt32Type
, &token
);
159 CFNumberRef timeoutNumber
= CFNumberCreate(allocator
, kCFNumberSInt32Type
, &timeout
);
161 CFStringRef path
= NULL
;
163 if (dictionary
) CFDictionaryApplyFunction(dictionary
, _CFUserNotificationAddToDictionary
, md
);
164 if (source
) CFDictionaryAddValue(md
, kCFUserNotificationAlertSourceKey
, source
);
166 CFDictionaryAddValue(md
, kCFUserNotificationTokenKey
, tokenNumber
);
167 CFRelease(tokenNumber
);
170 CFDictionaryAddValue(md
, kCFUserNotificationTimeoutKey
, timeoutNumber
);
171 CFRelease(timeoutNumber
);
174 url
= CFDictionaryGetValue(md
, kCFUserNotificationIconURLKey
);
175 if (url
&& CFGetTypeID((CFTypeRef
)url
) == CFURLGetTypeID()) {
176 url
= CFURLCopyAbsoluteURL(url
);
177 CFDictionaryRemoveValue(md
, kCFUserNotificationIconURLKey
);
178 path
= CFURLCopyFileSystemPath(url
, kCFURLPOSIXPathStyle
);
179 CFDictionaryAddValue(md
, kCFUserNotificationIconPathKey
, path
);
183 url
= CFDictionaryGetValue(md
, kCFUserNotificationSoundURLKey
);
184 if (url
&& CFGetTypeID((CFTypeRef
)url
) == CFURLGetTypeID()) {
185 url
= CFURLCopyAbsoluteURL(url
);
186 CFDictionaryRemoveValue(md
, kCFUserNotificationSoundURLKey
);
187 path
= CFURLCopyFileSystemPath(url
, kCFURLPOSIXPathStyle
);
188 CFDictionaryAddValue(md
, kCFUserNotificationSoundPathKey
, path
);
192 url
= CFDictionaryGetValue(md
, kCFUserNotificationLocalizationURLKey
);
193 if (url
&& CFGetTypeID((CFTypeRef
)url
) == CFURLGetTypeID()) {
194 url
= CFURLCopyAbsoluteURL(url
);
195 CFDictionaryRemoveValue(md
, kCFUserNotificationLocalizationURLKey
);
196 path
= CFURLCopyFileSystemPath(url
, kCFURLPOSIXPathStyle
);
197 CFDictionaryAddValue(md
, kCFUserNotificationLocalizationPathKey
, path
);
204 static SInt32
_CFUserNotificationSendRequest(CFAllocatorRef allocator
, CFStringRef sessionID
, mach_port_t replyPort
, SInt32 token
, CFTimeInterval timeout
, CFOptionFlags flags
, CFDictionaryRef dictionary
) {
205 CFDictionaryRef modifiedDictionary
= NULL
;
206 SInt32 retval
= ERR_SUCCESS
, itimeout
= (timeout
> 0.0 && timeout
< INT_MAX
) ? (SInt32
)timeout
: 0;
208 mach_msg_base_t
*msg
= NULL
;
209 mach_port_t bootstrapPort
= MACH_PORT_NULL
, serverPort
= MACH_PORT_NULL
;
211 char namebuffer
[MAX_PORT_NAME_LENGTH
+ 1];
213 strlcpy(namebuffer
, NOTIFICATION_PORT_NAME
, sizeof(namebuffer
));
215 char sessionid
[MAX_PORT_NAME_LENGTH
+ 1];
216 CFIndex len
= MAX_PORT_NAME_LENGTH
- sizeof(NOTIFICATION_PORT_NAME
) - sizeof(NOTIFICATION_PORT_NAME_SUFFIX
);
217 CFStringGetBytes(sessionID
, CFRangeMake(0, CFStringGetLength(sessionID
)), kCFStringEncodingUTF8
, 0, false, (uint8_t *)sessionid
, len
, &size
);
218 sessionid
[len
- 1] = '\0';
219 strlcat(namebuffer
, NOTIFICATION_PORT_NAME_SUFFIX
, sizeof(namebuffer
));
220 strlcat(namebuffer
, sessionid
, sizeof(namebuffer
));
223 retval
= task_get_bootstrap_port(mach_task_self(), &bootstrapPort
);
224 if (ERR_SUCCESS
== retval
&& MACH_PORT_NULL
!= bootstrapPort
) retval
= bootstrap_look_up2(bootstrapPort
, namebuffer
, &serverPort
, 0, 0);
225 if (ERR_SUCCESS
== retval
&& MACH_PORT_NULL
!= serverPort
) {
226 modifiedDictionary
= _CFUserNotificationModifiedDictionary(allocator
, dictionary
, token
, itimeout
, _CFProcessNameString());
227 if (modifiedDictionary
) {
228 data
= CFPropertyListCreateXMLData(allocator
, modifiedDictionary
);
230 size
= sizeof(mach_msg_base_t
) + ((CFDataGetLength(data
) + 3) & (~0x3));
231 msg
= (mach_msg_base_t
*)CFAllocatorAllocate(kCFAllocatorSystemDefault
, size
, 0);
232 if (__CFOASafe
) __CFSetLastAllocationEventName(msg
, "CFUserNotification (temp)");
234 memset(msg
, 0, size
);
235 msg
->header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, (replyPort
== MACH_PORT_NULL
) ? 0 : MACH_MSG_TYPE_MAKE_SEND_ONCE
);
236 msg
->header
.msgh_size
= size
;
237 msg
->header
.msgh_remote_port
= serverPort
;
238 msg
->header
.msgh_local_port
= replyPort
;
239 msg
->header
.msgh_id
= flags
;
240 msg
->body
.msgh_descriptor_count
= 0;
241 CFDataGetBytes(data
, CFRangeMake(0, CFDataGetLength(data
)), (uint8_t *)msg
+ sizeof(mach_msg_base_t
));
242 //CFShow(CFStringCreateWithBytes(kCFAllocatorSystemDefault, (UInt8 *)msg + sizeof(mach_msg_base_t), CFDataGetLength(data), kCFStringEncodingUTF8, false));
243 retval
= mach_msg((mach_msg_header_t
*)msg
, MACH_SEND_MSG
|MACH_SEND_TIMEOUT
, size
, 0, MACH_PORT_NULL
, MESSAGE_TIMEOUT
, MACH_PORT_NULL
);
244 CFAllocatorDeallocate(kCFAllocatorSystemDefault
, msg
);
246 retval
= unix_err(ENOMEM
);
250 retval
= unix_err(ENOMEM
);
252 CFRelease(modifiedDictionary
);
254 retval
= unix_err(ENOMEM
);
260 static SInt32
_getNextToken() {
261 static uint16_t tokenCounter
= 0;
262 SInt32 token
= ((getpid() << 16) | (tokenCounter
++));
266 CFUserNotificationRef
CFUserNotificationCreate(CFAllocatorRef allocator
, CFTimeInterval timeout
, CFOptionFlags flags
, SInt32
*error
, CFDictionaryRef dictionary
) {
268 CFUserNotificationRef userNotification
= NULL
;
269 SInt32 retval
= ERR_SUCCESS
;
270 SInt32 token
= _getNextToken();
271 CFStringRef sessionID
= (dictionary
? CFDictionaryGetValue(dictionary
, kCFUserNotificationSessionIDKey
) : NULL
);
272 mach_port_t replyPort
= MACH_PORT_NULL
;
274 if (!allocator
) allocator
= __CFGetDefaultAllocator();
275 retval
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &replyPort
);
276 if (ERR_SUCCESS
== retval
&& MACH_PORT_NULL
!= replyPort
) retval
= _CFUserNotificationSendRequest(allocator
, sessionID
, replyPort
, token
, timeout
, flags
, dictionary
);
277 if (ERR_SUCCESS
== retval
) {
278 userNotification
= (CFUserNotificationRef
)_CFRuntimeCreateInstance(allocator
, CFUserNotificationGetTypeID(), sizeof(struct __CFUserNotification
) - sizeof(CFRuntimeBase
), NULL
);
279 if (userNotification
) {
280 userNotification
->_replyPort
= replyPort
;
281 userNotification
->_token
= token
;
282 userNotification
->_timeout
= timeout
;
283 userNotification
->_requestFlags
= flags
;
284 userNotification
->_responseFlags
= 0;
285 userNotification
->_sessionID
= NULL
;
286 userNotification
->_responseDictionary
= NULL
;
287 userNotification
->_machPort
= NULL
;
288 userNotification
->_callout
= NULL
;
289 if (sessionID
) userNotification
->_sessionID
= CFStringCreateCopy(allocator
, sessionID
);
291 retval
= unix_err(ENOMEM
);
294 if (dictionary
) CFUserNotificationLog(CFDictionaryGetValue(dictionary
, kCFUserNotificationAlertHeaderKey
), CFDictionaryGetValue(dictionary
, kCFUserNotificationAlertMessageKey
));
296 if (ERR_SUCCESS
!= retval
&& MACH_PORT_NULL
!= replyPort
) mach_port_destroy(mach_task_self(), replyPort
);
297 if (error
) *error
= retval
;
298 return userNotification
;
301 static void _CFUserNotificationMachPortCallBack(CFMachPortRef port
, void *m
, CFIndex size
, void *info
) {
302 CFUserNotificationRef userNotification
= (CFUserNotificationRef
)info
;
303 mach_msg_base_t
*msg
= (mach_msg_base_t
*)m
;
304 CFOptionFlags responseFlags
= msg
->header
.msgh_id
;
305 if (msg
->header
.msgh_size
> sizeof(mach_msg_base_t
)) {
306 CFDataRef responseData
= CFDataCreate(kCFAllocatorSystemDefault
, (uint8_t *)msg
+ sizeof(mach_msg_base_t
), msg
->header
.msgh_size
- sizeof(mach_msg_base_t
));
308 userNotification
->_responseDictionary
= CFPropertyListCreateFromXMLData(kCFAllocatorSystemDefault
, responseData
, kCFPropertyListImmutable
, NULL
);
309 CFRelease(responseData
);
312 CFMachPortInvalidate(userNotification
->_machPort
);
313 CFRelease(userNotification
->_machPort
);
314 userNotification
->_machPort
= NULL
;
315 mach_port_destroy(mach_task_self(), userNotification
->_replyPort
);
316 userNotification
->_replyPort
= MACH_PORT_NULL
;
317 userNotification
->_callout(userNotification
, responseFlags
);
320 SInt32
CFUserNotificationReceiveResponse(CFUserNotificationRef userNotification
, CFTimeInterval timeout
, CFOptionFlags
*responseFlags
) {
322 SInt32 retval
= ERR_SUCCESS
;
323 mach_msg_timeout_t msgtime
= (timeout
> 0.0 && 1000.0 * timeout
< INT_MAX
) ? (mach_msg_timeout_t
)(1000.0 * timeout
) : 0;
324 mach_msg_base_t
*msg
= NULL
;
325 CFIndex size
= MAX_STRING_COUNT
* MAX_STRING_LENGTH
;
326 CFDataRef responseData
;
328 if (userNotification
&& MACH_PORT_NULL
!= userNotification
->_replyPort
) {
329 msg
= (mach_msg_base_t
*)CFAllocatorAllocate(kCFAllocatorSystemDefault
, size
, 0);
330 if (__CFOASafe
) __CFSetLastAllocationEventName(msg
, "CFUserNotification (temp)");
332 memset(msg
, 0, size
);
333 msg
->header
.msgh_size
= size
;
335 retval
= mach_msg((mach_msg_header_t
*)msg
, MACH_RCV_MSG
|MACH_RCV_TIMEOUT
, 0, size
, userNotification
->_replyPort
, msgtime
, MACH_PORT_NULL
);
337 retval
= mach_msg((mach_msg_header_t
*)msg
, MACH_RCV_MSG
, 0, size
, userNotification
->_replyPort
, 0, MACH_PORT_NULL
);
339 if (ERR_SUCCESS
== retval
) {
340 if (responseFlags
) *responseFlags
= msg
->header
.msgh_id
;
341 if (msg
->header
.msgh_size
> sizeof(mach_msg_base_t
)) {
342 responseData
= CFDataCreate(kCFAllocatorSystemDefault
, (uint8_t *)msg
+ sizeof(mach_msg_base_t
), msg
->header
.msgh_size
- sizeof(mach_msg_base_t
));
344 userNotification
->_responseDictionary
= CFPropertyListCreateFromXMLData(kCFAllocatorSystemDefault
, responseData
, kCFPropertyListImmutable
, NULL
);
345 CFRelease(responseData
);
348 if (userNotification
->_machPort
) {
349 CFMachPortInvalidate(userNotification
->_machPort
);
350 CFRelease(userNotification
->_machPort
);
351 userNotification
->_machPort
= NULL
;
353 mach_port_destroy(mach_task_self(), userNotification
->_replyPort
);
354 userNotification
->_replyPort
= MACH_PORT_NULL
;
356 CFAllocatorDeallocate(kCFAllocatorSystemDefault
, msg
);
358 retval
= unix_err(ENOMEM
);
364 CFStringRef
CFUserNotificationGetResponseValue(CFUserNotificationRef userNotification
, CFStringRef key
, CFIndex idx
) {
366 CFStringRef retval
= NULL
;
367 CFTypeRef value
= NULL
;
368 if (userNotification
&& userNotification
->_responseDictionary
) {
369 value
= CFDictionaryGetValue(userNotification
->_responseDictionary
, key
);
370 if (CFGetTypeID(value
) == CFStringGetTypeID()) {
371 if (0 == idx
) retval
= (CFStringRef
)value
;
372 } else if (CFGetTypeID(value
) == CFArrayGetTypeID()) {
373 if (0 <= idx
&& idx
< CFArrayGetCount((CFArrayRef
)value
)) retval
= (CFStringRef
)CFArrayGetValueAtIndex((CFArrayRef
)value
, idx
);
379 CFDictionaryRef
CFUserNotificationGetResponseDictionary(CFUserNotificationRef userNotification
) {
381 return userNotification
? userNotification
->_responseDictionary
: NULL
;
384 SInt32
CFUserNotificationUpdate(CFUserNotificationRef userNotification
, CFTimeInterval timeout
, CFOptionFlags flags
, CFDictionaryRef dictionary
) {
386 SInt32 retval
= ERR_SUCCESS
;
387 if (userNotification
&& MACH_PORT_NULL
!= userNotification
->_replyPort
) {
388 // Avoid including a new send-once right with update/cancel messages by passing MACH_PORT_NULL, since the server doesn't need to use them.
389 retval
= _CFUserNotificationSendRequest(CFGetAllocator(userNotification
), userNotification
->_sessionID
, MACH_PORT_NULL
, userNotification
->_token
, timeout
, flags
|kCFUserNotificationUpdateFlag
, dictionary
);
394 SInt32
CFUserNotificationCancel(CFUserNotificationRef userNotification
) {
396 SInt32 retval
= ERR_SUCCESS
;
397 if (userNotification
&& MACH_PORT_NULL
!= userNotification
->_replyPort
) {
398 // Avoid including a new send-once right with update/cancel messages by passing MACH_PORT_NULL, since the server doesn't need to use them.
399 retval
= _CFUserNotificationSendRequest(CFGetAllocator(userNotification
), userNotification
->_sessionID
, MACH_PORT_NULL
, userNotification
->_token
, 0, kCFUserNotificationCancelFlag
, NULL
);
404 CFRunLoopSourceRef
CFUserNotificationCreateRunLoopSource(CFAllocatorRef allocator
, CFUserNotificationRef userNotification
, CFUserNotificationCallBack callout
, CFIndex order
) {
406 CFRunLoopSourceRef source
= NULL
;
407 if (userNotification
&& callout
&& !userNotification
->_machPort
&& MACH_PORT_NULL
!= userNotification
->_replyPort
) {
408 CFMachPortContext context
= {0, userNotification
, NULL
, NULL
, NULL
};
409 userNotification
->_machPort
= CFMachPortCreateWithPort(CFGetAllocator(userNotification
), (mach_port_t
)userNotification
->_replyPort
, _CFUserNotificationMachPortCallBack
, &context
, NULL
);
411 if (userNotification
&& userNotification
->_machPort
) {
412 source
= CFMachPortCreateRunLoopSource(allocator
, userNotification
->_machPort
, order
);
413 userNotification
->_callout
= callout
;
418 SInt32
CFUserNotificationDisplayNotice(CFTimeInterval timeout
, CFOptionFlags flags
, CFURLRef iconURL
, CFURLRef soundURL
, CFURLRef localizationURL
, CFStringRef alertHeader
, CFStringRef alertMessage
, CFStringRef defaultButtonTitle
) {
420 SInt32 retval
= ERR_SUCCESS
;
421 CFMutableDictionaryRef dict
= CFDictionaryCreateMutable(kCFAllocatorSystemDefault
, 0, &kCFCopyStringDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
422 if (iconURL
) CFDictionaryAddValue(dict
, kCFUserNotificationIconURLKey
, iconURL
);
423 if (soundURL
) CFDictionaryAddValue(dict
, kCFUserNotificationSoundURLKey
, soundURL
);
424 if (localizationURL
) CFDictionaryAddValue(dict
, kCFUserNotificationLocalizationURLKey
, localizationURL
);
425 if (alertHeader
) CFDictionaryAddValue(dict
, kCFUserNotificationAlertHeaderKey
, alertHeader
);
426 if (alertMessage
) CFDictionaryAddValue(dict
, kCFUserNotificationAlertMessageKey
, alertMessage
);
427 if (defaultButtonTitle
) CFDictionaryAddValue(dict
, kCFUserNotificationDefaultButtonTitleKey
, defaultButtonTitle
);
428 retval
= _CFUserNotificationSendRequest(__CFGetDefaultAllocator(), NULL
, MACH_PORT_NULL
, _getNextToken(), timeout
, flags
, dict
);
429 if (ERR_SUCCESS
!= retval
) {
430 CFUserNotificationLog(alertHeader
, alertMessage
);
437 CF_EXPORT SInt32
CFUserNotificationDisplayAlert(CFTimeInterval timeout
, CFOptionFlags flags
, CFURLRef iconURL
, CFURLRef soundURL
, CFURLRef localizationURL
, CFStringRef alertHeader
, CFStringRef alertMessage
, CFStringRef defaultButtonTitle
, CFStringRef alternateButtonTitle
, CFStringRef otherButtonTitle
, CFOptionFlags
*responseFlags
) {
439 CFUserNotificationRef userNotification
;
440 SInt32 retval
= ERR_SUCCESS
;
441 CFMutableDictionaryRef dict
= CFDictionaryCreateMutable(kCFAllocatorSystemDefault
, 0, &kCFCopyStringDictionaryKeyCallBacks
, &kCFTypeDictionaryValueCallBacks
);
442 if (iconURL
) CFDictionaryAddValue(dict
, kCFUserNotificationIconURLKey
, iconURL
);
443 if (soundURL
) CFDictionaryAddValue(dict
, kCFUserNotificationSoundURLKey
, soundURL
);
444 if (localizationURL
) CFDictionaryAddValue(dict
, kCFUserNotificationLocalizationURLKey
, localizationURL
);
445 if (alertHeader
) CFDictionaryAddValue(dict
, kCFUserNotificationAlertHeaderKey
, alertHeader
);
446 if (alertMessage
) CFDictionaryAddValue(dict
, kCFUserNotificationAlertMessageKey
, alertMessage
);
447 if (defaultButtonTitle
) CFDictionaryAddValue(dict
, kCFUserNotificationDefaultButtonTitleKey
, defaultButtonTitle
);
448 if (alternateButtonTitle
) CFDictionaryAddValue(dict
, kCFUserNotificationAlternateButtonTitleKey
, alternateButtonTitle
);
449 if (otherButtonTitle
) CFDictionaryAddValue(dict
, kCFUserNotificationOtherButtonTitleKey
, otherButtonTitle
);
450 userNotification
= CFUserNotificationCreate(kCFAllocatorSystemDefault
, timeout
, flags
, &retval
, dict
);
451 if (userNotification
) {
452 retval
= CFUserNotificationReceiveResponse(userNotification
, timeout
, responseFlags
);
453 if (MACH_RCV_TIMED_OUT
== retval
) {
454 retval
= CFUserNotificationCancel(userNotification
);
455 if (responseFlags
) *responseFlags
= kCFUserNotificationCancelResponse
;
457 CFRelease(userNotification
);
463 #undef MAX_STRING_LENGTH
464 #undef MAX_STRING_COUNT
465 #undef NOTIFICATION_PORT_NAME
466 #undef MESSAGE_TIMEOUT
467 #undef MAX_PORT_NAME_LENGTH
468 #undef NOTIFICATION_PORT_NAME_SUFFIX