diff options
-rw-r--r-- | base/mac_util.h | 62 | ||||
-rw-r--r-- | chrome/browser/ui/cocoa/authorization_util.mm | 3 |
2 files changed, 59 insertions, 6 deletions
diff --git a/base/mac_util.h b/base/mac_util.h index d31bf82..4497da3 100644 --- a/base/mac_util.h +++ b/base/mac_util.h @@ -10,16 +10,20 @@ #include <string> #include <vector> -class FilePath; +#include "base/logging.h" + +#if defined(__OBJC__) +#import <Foundation/Foundation.h> -#ifdef __OBJC__ @class NSBundle; @class NSWindow; -#else +#else // __OBJC__ class NSBundle; class NSImage; class NSWindow; -#endif +#endif // __OBJC__ + +class FilePath; // Adapted from NSPathUtilities.h and NSObjCRuntime.h. #if __LP64__ || NS_BUILD_32_LIKE_64 @@ -188,6 +192,56 @@ bool WasLaunchedAsHiddenLoginItem(); void NSObjectRetain(void* obj); void NSObjectRelease(void* obj); +#if defined(__OBJC__) + +// Convert toll-free bridged CFTypes to NSTypes. This does not autorelease +// |cf_val|. This is useful for the case where there is a CFType in a call that +// expects an NSType and the compiler is complaining about const casting +// problems. +// The call is used like this: +// NSString *foo = CFToNSCast(CFSTR("Hello")); +// The macro magic below is to enforce safe casting. It could possibly have +// been done using template function specialization, but template function +// specialization doesn't always work intuitively, +// (http://www.gotw.ca/publications/mill17.htm) so the trusty combination +// of macros and function overloading is used instead. + +#define CF_TO_NS_CAST(TypeCF, TypeNS) \ +inline TypeNS* CFToNSCast(TypeCF cf_val) { \ + TypeNS* ns_val = \ + const_cast<id>(reinterpret_cast<const struct objc_object*>(cf_val)); \ + DCHECK(!ns_val || [ns_val isKindOfClass:[TypeNS class]]); \ + return ns_val; \ +} + +// List of toll-free bridged types taken from: +// http://www.cocoadev.com/index.pl?TollFreeBridged + +CF_TO_NS_CAST(CFArrayRef, NSArray); +CF_TO_NS_CAST(CFMutableArrayRef, NSMutableArray); +CF_TO_NS_CAST(CFAttributedStringRef, NSAttributedString); +CF_TO_NS_CAST(CFMutableAttributedStringRef, NSMutableAttributedString); +CF_TO_NS_CAST(CFCalendarRef, NSCalendar); +CF_TO_NS_CAST(CFCharacterSetRef, NSCharacterSet); +CF_TO_NS_CAST(CFMutableCharacterSetRef, NSMutableCharacterSet); +CF_TO_NS_CAST(CFDataRef, NSData); +CF_TO_NS_CAST(CFMutableDataRef, NSMutableData); +CF_TO_NS_CAST(CFDateRef, NSDate); +CF_TO_NS_CAST(CFDictionaryRef, NSDictionary); +CF_TO_NS_CAST(CFMutableDictionaryRef, NSMutableDictionary); +CF_TO_NS_CAST(CFNumberRef, NSNumber); +CF_TO_NS_CAST(CFRunLoopTimerRef, NSTimer); +CF_TO_NS_CAST(CFSetRef, NSSet); +CF_TO_NS_CAST(CFMutableSetRef, NSMutableSet); +CF_TO_NS_CAST(CFStringRef, NSString); +CF_TO_NS_CAST(CFMutableStringRef, NSMutableString); +CF_TO_NS_CAST(CFURLRef, NSURL); +CF_TO_NS_CAST(CFTimeZoneRef, NSTimeZone); +CF_TO_NS_CAST(CFReadStreamRef, NSInputStream); +CF_TO_NS_CAST(CFWriteStreamRef, NSOutputStream); + +#endif // __OBJC__ + } // namespace mac_util #endif // BASE_MAC_UTIL_H_ diff --git a/chrome/browser/ui/cocoa/authorization_util.mm b/chrome/browser/ui/cocoa/authorization_util.mm index e92dd53..5893711 100644 --- a/chrome/browser/ui/cocoa/authorization_util.mm +++ b/chrome/browser/ui/cocoa/authorization_util.mm @@ -48,8 +48,7 @@ AuthorizationRef AuthorizationCreateToRunAsRoot(CFStringRef prompt) { // The OS will append " Type an administrator's name and password to allow // <CFBundleDisplayName> to make changes." - NSString* prompt_ns = const_cast<NSString*>( - reinterpret_cast<const NSString*>(prompt)); + NSString* prompt_ns = mac_util::CFToNSCast(prompt); const char* prompt_c = [prompt_ns UTF8String]; size_t prompt_length = prompt_c ? strlen(prompt_c) : 0; |