| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_MAC_FOUNDATION_UTIL_H_ | 5 #ifndef BASE_MAC_FOUNDATION_UTIL_H_ |
| 6 #define BASE_MAC_FOUNDATION_UTIL_H_ | 6 #define BASE_MAC_FOUNDATION_UTIL_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <CoreFoundation/CoreFoundation.h> | 9 #include <CoreFoundation/CoreFoundation.h> |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/base_api.h" | 14 #include "base/base_export.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 | 16 |
| 17 #if defined(__OBJC__) | 17 #if defined(__OBJC__) |
| 18 #import <Foundation/Foundation.h> | 18 #import <Foundation/Foundation.h> |
| 19 #else // __OBJC__ | 19 #else // __OBJC__ |
| 20 class NSBundle; | 20 class NSBundle; |
| 21 class NSString; | 21 class NSString; |
| 22 #endif // __OBJC__ | 22 #endif // __OBJC__ |
| 23 | 23 |
| 24 class FilePath; | 24 class FilePath; |
| 25 | 25 |
| 26 // Adapted from NSPathUtilities.h and NSObjCRuntime.h. | 26 // Adapted from NSPathUtilities.h and NSObjCRuntime.h. |
| 27 #if __LP64__ || NS_BUILD_32_LIKE_64 | 27 #if __LP64__ || NS_BUILD_32_LIKE_64 |
| 28 typedef unsigned long NSSearchPathDirectory; | 28 typedef unsigned long NSSearchPathDirectory; |
| 29 typedef unsigned long NSSearchPathDomainMask; | 29 typedef unsigned long NSSearchPathDomainMask; |
| 30 #else | 30 #else |
| 31 typedef unsigned int NSSearchPathDirectory; | 31 typedef unsigned int NSSearchPathDirectory; |
| 32 typedef unsigned int NSSearchPathDomainMask; | 32 typedef unsigned int NSSearchPathDomainMask; |
| 33 #endif | 33 #endif |
| 34 | 34 |
| 35 namespace base { | 35 namespace base { |
| 36 namespace mac { | 36 namespace mac { |
| 37 | 37 |
| 38 // Returns true if the application is running from a bundle | 38 // Returns true if the application is running from a bundle |
| 39 BASE_API bool AmIBundled(); | 39 BASE_EXPORT bool AmIBundled(); |
| 40 BASE_API void SetOverrideAmIBundled(bool value); | 40 BASE_EXPORT void SetOverrideAmIBundled(bool value); |
| 41 | 41 |
| 42 // Returns true if this process is marked as a "Background only process". | 42 // Returns true if this process is marked as a "Background only process". |
| 43 BASE_API bool IsBackgroundOnlyProcess(); | 43 BASE_EXPORT bool IsBackgroundOnlyProcess(); |
| 44 | 44 |
| 45 // Returns the main bundle or the override, used for code that needs | 45 // Returns the main bundle or the override, used for code that needs |
| 46 // to fetch resources from bundles, but work within a unittest where we | 46 // to fetch resources from bundles, but work within a unittest where we |
| 47 // aren't a bundle. | 47 // aren't a bundle. |
| 48 BASE_API NSBundle* MainAppBundle(); | 48 BASE_EXPORT NSBundle* MainAppBundle(); |
| 49 BASE_API FilePath MainAppBundlePath(); | 49 BASE_EXPORT FilePath MainAppBundlePath(); |
| 50 | 50 |
| 51 // Returns the path to a resource within the MainAppBundle. | 51 // Returns the path to a resource within the MainAppBundle. |
| 52 FilePath PathForMainAppBundleResource(CFStringRef resourceName); | 52 FilePath PathForMainAppBundleResource(CFStringRef resourceName); |
| 53 | 53 |
| 54 // Set the bundle that MainAppBundle will return, overriding the default value | 54 // Set the bundle that MainAppBundle will return, overriding the default value |
| 55 // (Restore the default by calling SetOverrideAppBundle(nil)). | 55 // (Restore the default by calling SetOverrideAppBundle(nil)). |
| 56 BASE_API void SetOverrideAppBundle(NSBundle* bundle); | 56 BASE_EXPORT void SetOverrideAppBundle(NSBundle* bundle); |
| 57 BASE_API void SetOverrideAppBundlePath(const FilePath& file_path); | 57 BASE_EXPORT void SetOverrideAppBundlePath(const FilePath& file_path); |
| 58 | 58 |
| 59 // Returns the creator code associated with the CFBundleRef at bundle. | 59 // Returns the creator code associated with the CFBundleRef at bundle. |
| 60 OSType CreatorCodeForCFBundleRef(CFBundleRef bundle); | 60 OSType CreatorCodeForCFBundleRef(CFBundleRef bundle); |
| 61 | 61 |
| 62 // Returns the creator code associated with this application, by calling | 62 // Returns the creator code associated with this application, by calling |
| 63 // CreatorCodeForCFBundleRef for the application's main bundle. If this | 63 // CreatorCodeForCFBundleRef for the application's main bundle. If this |
| 64 // information cannot be determined, returns kUnknownType ('????'). This | 64 // information cannot be determined, returns kUnknownType ('????'). This |
| 65 // does not respect the override app bundle because it's based on CFBundle | 65 // does not respect the override app bundle because it's based on CFBundle |
| 66 // instead of NSBundle, and because callers probably don't want the override | 66 // instead of NSBundle, and because callers probably don't want the override |
| 67 // app bundle's creator code anyway. | 67 // app bundle's creator code anyway. |
| 68 BASE_API OSType CreatorCodeForApplication(); | 68 BASE_EXPORT OSType CreatorCodeForApplication(); |
| 69 | 69 |
| 70 // Searches for directories for the given key in only the given |domain_mask|. | 70 // Searches for directories for the given key in only the given |domain_mask|. |
| 71 // If found, fills result (which must always be non-NULL) with the | 71 // If found, fills result (which must always be non-NULL) with the |
| 72 // first found directory and returns true. Otherwise, returns false. | 72 // first found directory and returns true. Otherwise, returns false. |
| 73 bool GetSearchPathDirectory(NSSearchPathDirectory directory, | 73 bool GetSearchPathDirectory(NSSearchPathDirectory directory, |
| 74 NSSearchPathDomainMask domain_mask, | 74 NSSearchPathDomainMask domain_mask, |
| 75 FilePath* result); | 75 FilePath* result); |
| 76 | 76 |
| 77 // Searches for directories for the given key in only the local domain. | 77 // Searches for directories for the given key in only the local domain. |
| 78 // If found, fills result (which must always be non-NULL) with the | 78 // If found, fills result (which must always be non-NULL) with the |
| 79 // first found directory and returns true. Otherwise, returns false. | 79 // first found directory and returns true. Otherwise, returns false. |
| 80 BASE_API bool GetLocalDirectory(NSSearchPathDirectory directory, | 80 BASE_EXPORT bool GetLocalDirectory(NSSearchPathDirectory directory, |
| 81 FilePath* result); | 81 FilePath* result); |
| 82 | 82 |
| 83 // Searches for directories for the given key in only the user domain. | 83 // Searches for directories for the given key in only the user domain. |
| 84 // If found, fills result (which must always be non-NULL) with the | 84 // If found, fills result (which must always be non-NULL) with the |
| 85 // first found directory and returns true. Otherwise, returns false. | 85 // first found directory and returns true. Otherwise, returns false. |
| 86 BASE_API bool GetUserDirectory(NSSearchPathDirectory directory, | 86 BASE_EXPORT bool GetUserDirectory(NSSearchPathDirectory directory, |
| 87 FilePath* result); | 87 FilePath* result); |
| 88 | 88 |
| 89 // Returns the ~/Library directory. | 89 // Returns the ~/Library directory. |
| 90 BASE_API FilePath GetUserLibraryPath(); | 90 BASE_EXPORT FilePath GetUserLibraryPath(); |
| 91 | 91 |
| 92 // Takes a path to an (executable) binary and tries to provide the path to an | 92 // Takes a path to an (executable) binary and tries to provide the path to an |
| 93 // application bundle containing it. It takes the outermost bundle that it can | 93 // application bundle containing it. It takes the outermost bundle that it can |
| 94 // find (so for "/Foo/Bar.app/.../Baz.app/..." it produces "/Foo/Bar.app"). | 94 // find (so for "/Foo/Bar.app/.../Baz.app/..." it produces "/Foo/Bar.app"). |
| 95 // |exec_name| - path to the binary | 95 // |exec_name| - path to the binary |
| 96 // returns - path to the application bundle, or empty on error | 96 // returns - path to the application bundle, or empty on error |
| 97 BASE_API FilePath GetAppBundlePath(const FilePath& exec_name); | 97 BASE_EXPORT FilePath GetAppBundlePath(const FilePath& exec_name); |
| 98 | 98 |
| 99 // Utility function to pull out a value from a dictionary, check its type, and | 99 // Utility function to pull out a value from a dictionary, check its type, and |
| 100 // return it. Returns NULL if the key is not present or of the wrong type. | 100 // return it. Returns NULL if the key is not present or of the wrong type. |
| 101 BASE_API CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, | 101 BASE_EXPORT CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, |
| 102 CFStringRef key, | 102 CFStringRef key, |
| 103 CFTypeID expected_type); | 103 CFTypeID expected_type); |
| 104 | 104 |
| 105 // Retain/release calls for memory management in C++. | 105 // Retain/release calls for memory management in C++. |
| 106 BASE_API void NSObjectRetain(void* obj); | 106 BASE_EXPORT void NSObjectRetain(void* obj); |
| 107 BASE_API void NSObjectRelease(void* obj); | 107 BASE_EXPORT void NSObjectRelease(void* obj); |
| 108 | 108 |
| 109 // CFTypeRefToNSObjectAutorelease transfers ownership of a Core Foundation | 109 // CFTypeRefToNSObjectAutorelease transfers ownership of a Core Foundation |
| 110 // object (one derived from CFTypeRef) to the Foundation memory management | 110 // object (one derived from CFTypeRef) to the Foundation memory management |
| 111 // system. In a traditional managed-memory environment, cf_object is | 111 // system. In a traditional managed-memory environment, cf_object is |
| 112 // autoreleased and returned as an NSObject. In a garbage-collected | 112 // autoreleased and returned as an NSObject. In a garbage-collected |
| 113 // environment, cf_object is marked as eligible for garbage collection. | 113 // environment, cf_object is marked as eligible for garbage collection. |
| 114 // | 114 // |
| 115 // This function should only be used to convert a concrete CFTypeRef type to | 115 // This function should only be used to convert a concrete CFTypeRef type to |
| 116 // its equivalent "toll-free bridged" NSObject subclass, for example, | 116 // its equivalent "toll-free bridged" NSObject subclass, for example, |
| 117 // converting a CFStringRef to NSString. | 117 // converting a CFStringRef to NSString. |
| 118 // | 118 // |
| 119 // By calling this function, callers relinquish any ownership claim to | 119 // By calling this function, callers relinquish any ownership claim to |
| 120 // cf_object. In a managed-memory environment, the object's ownership will be | 120 // cf_object. In a managed-memory environment, the object's ownership will be |
| 121 // managed by the innermost NSAutoreleasePool, so after this function returns, | 121 // managed by the innermost NSAutoreleasePool, so after this function returns, |
| 122 // callers should not assume that cf_object is valid any longer than the | 122 // callers should not assume that cf_object is valid any longer than the |
| 123 // returned NSObject. | 123 // returned NSObject. |
| 124 // | 124 // |
| 125 // Returns an id, typed here for C++'s sake as a void*. | 125 // Returns an id, typed here for C++'s sake as a void*. |
| 126 BASE_API void* CFTypeRefToNSObjectAutorelease(CFTypeRef cf_object); | 126 BASE_EXPORT void* CFTypeRefToNSObjectAutorelease(CFTypeRef cf_object); |
| 127 | 127 |
| 128 // Returns the base bundle ID, which can be set by SetBaseBundleID but | 128 // Returns the base bundle ID, which can be set by SetBaseBundleID but |
| 129 // defaults to a reasonable string. This never returns NULL. BaseBundleID | 129 // defaults to a reasonable string. This never returns NULL. BaseBundleID |
| 130 // returns a pointer to static storage that must not be freed. | 130 // returns a pointer to static storage that must not be freed. |
| 131 BASE_API const char* BaseBundleID(); | 131 BASE_EXPORT const char* BaseBundleID(); |
| 132 | 132 |
| 133 // Sets the base bundle ID to override the default. The implementation will | 133 // Sets the base bundle ID to override the default. The implementation will |
| 134 // make its own copy of new_base_bundle_id. | 134 // make its own copy of new_base_bundle_id. |
| 135 BASE_API void SetBaseBundleID(const char* new_base_bundle_id); | 135 BASE_EXPORT void SetBaseBundleID(const char* new_base_bundle_id); |
| 136 | 136 |
| 137 } // namespace mac | 137 } // namespace mac |
| 138 } // namespace base | 138 } // namespace base |
| 139 | 139 |
| 140 #if !defined(__OBJC__) | 140 #if !defined(__OBJC__) |
| 141 #define OBJC_CPP_CLASS_DECL(x) class x; | 141 #define OBJC_CPP_CLASS_DECL(x) class x; |
| 142 #else // __OBJC__ | 142 #else // __OBJC__ |
| 143 #define OBJC_CPP_CLASS_DECL(x) | 143 #define OBJC_CPP_CLASS_DECL(x) |
| 144 #endif // __OBJC__ | 144 #endif // __OBJC__ |
| 145 | 145 |
| 146 // Convert toll-free bridged CFTypes to NSTypes and vice-versa. This does not | 146 // Convert toll-free bridged CFTypes to NSTypes and vice-versa. This does not |
| 147 // autorelease |cf_val|. This is useful for the case where there is a CFType in | 147 // autorelease |cf_val|. This is useful for the case where there is a CFType in |
| 148 // a call that expects an NSType and the compiler is complaining about const | 148 // a call that expects an NSType and the compiler is complaining about const |
| 149 // casting problems. | 149 // casting problems. |
| 150 // The calls are used like this: | 150 // The calls are used like this: |
| 151 // NSString *foo = CFToNSCast(CFSTR("Hello")); | 151 // NSString *foo = CFToNSCast(CFSTR("Hello")); |
| 152 // CFStringRef foo2 = NSToCFCast(@"Hello"); | 152 // CFStringRef foo2 = NSToCFCast(@"Hello"); |
| 153 // The macro magic below is to enforce safe casting. It could possibly have | 153 // The macro magic below is to enforce safe casting. It could possibly have |
| 154 // been done using template function specialization, but template function | 154 // been done using template function specialization, but template function |
| 155 // specialization doesn't always work intuitively, | 155 // specialization doesn't always work intuitively, |
| 156 // (http://www.gotw.ca/publications/mill17.htm) so the trusty combination | 156 // (http://www.gotw.ca/publications/mill17.htm) so the trusty combination |
| 157 // of macros and function overloading is used instead. | 157 // of macros and function overloading is used instead. |
| 158 | 158 |
| 159 #define CF_TO_NS_CAST_DECL(TypeCF, TypeNS) \ | 159 #define CF_TO_NS_CAST_DECL(TypeCF, TypeNS) \ |
| 160 OBJC_CPP_CLASS_DECL(TypeNS) \ | 160 OBJC_CPP_CLASS_DECL(TypeNS) \ |
| 161 \ | 161 \ |
| 162 namespace base { \ | 162 namespace base { \ |
| 163 namespace mac { \ | 163 namespace mac { \ |
| 164 BASE_API TypeNS* CFToNSCast(TypeCF##Ref cf_val); \ | 164 BASE_EXPORT TypeNS* CFToNSCast(TypeCF##Ref cf_val); \ |
| 165 BASE_API TypeCF##Ref NSToCFCast(TypeNS* ns_val); \ | 165 BASE_EXPORT TypeCF##Ref NSToCFCast(TypeNS* ns_val); \ |
| 166 } \ | 166 } \ |
| 167 } \ | 167 } \ |
| 168 | 168 |
| 169 #define CF_TO_NS_MUTABLE_CAST_DECL(name) \ | 169 #define CF_TO_NS_MUTABLE_CAST_DECL(name) \ |
| 170 CF_TO_NS_CAST_DECL(CF##name, NS##name) \ | 170 CF_TO_NS_CAST_DECL(CF##name, NS##name) \ |
| 171 OBJC_CPP_CLASS_DECL(NSMutable##name) \ | 171 OBJC_CPP_CLASS_DECL(NSMutable##name) \ |
| 172 \ | 172 \ |
| 173 namespace base { \ | 173 namespace base { \ |
| 174 namespace mac { \ | 174 namespace mac { \ |
| 175 BASE_API NSMutable##name* CFToNSCast(CFMutable##name##Ref cf_val); \ | 175 BASE_EXPORT NSMutable##name* CFToNSCast(CFMutable##name##Ref cf_val); \ |
| 176 BASE_API CFMutable##name##Ref NSToCFCast(NSMutable##name* ns_val); \ | 176 BASE_EXPORT CFMutable##name##Ref NSToCFCast(NSMutable##name* ns_val); \ |
| 177 } \ | 177 } \ |
| 178 } \ | 178 } \ |
| 179 | 179 |
| 180 // List of toll-free bridged types taken from: | 180 // List of toll-free bridged types taken from: |
| 181 // http://www.cocoadev.com/index.pl?TollFreeBridged | 181 // http://www.cocoadev.com/index.pl?TollFreeBridged |
| 182 | 182 |
| 183 CF_TO_NS_MUTABLE_CAST_DECL(Array); | 183 CF_TO_NS_MUTABLE_CAST_DECL(Array); |
| 184 CF_TO_NS_MUTABLE_CAST_DECL(AttributedString); | 184 CF_TO_NS_MUTABLE_CAST_DECL(AttributedString); |
| 185 CF_TO_NS_CAST_DECL(CFCalendar, NSCalendar); | 185 CF_TO_NS_CAST_DECL(CFCalendar, NSCalendar); |
| 186 CF_TO_NS_MUTABLE_CAST_DECL(CharacterSet); | 186 CF_TO_NS_MUTABLE_CAST_DECL(CharacterSet); |
| 187 CF_TO_NS_MUTABLE_CAST_DECL(Data); | 187 CF_TO_NS_MUTABLE_CAST_DECL(Data); |
| 188 CF_TO_NS_CAST_DECL(CFDate, NSDate); | 188 CF_TO_NS_CAST_DECL(CFDate, NSDate); |
| 189 CF_TO_NS_MUTABLE_CAST_DECL(Dictionary); | 189 CF_TO_NS_MUTABLE_CAST_DECL(Dictionary); |
| 190 CF_TO_NS_CAST_DECL(CFError, NSError); | 190 CF_TO_NS_CAST_DECL(CFError, NSError); |
| 191 CF_TO_NS_CAST_DECL(CFLocale, NSLocale); | 191 CF_TO_NS_CAST_DECL(CFLocale, NSLocale); |
| 192 CF_TO_NS_CAST_DECL(CFNumber, NSNumber); | 192 CF_TO_NS_CAST_DECL(CFNumber, NSNumber); |
| 193 CF_TO_NS_CAST_DECL(CFRunLoopTimer, NSTimer); | 193 CF_TO_NS_CAST_DECL(CFRunLoopTimer, NSTimer); |
| 194 CF_TO_NS_CAST_DECL(CFTimeZone, NSTimeZone); | 194 CF_TO_NS_CAST_DECL(CFTimeZone, NSTimeZone); |
| 195 CF_TO_NS_MUTABLE_CAST_DECL(Set); | 195 CF_TO_NS_MUTABLE_CAST_DECL(Set); |
| 196 CF_TO_NS_CAST_DECL(CFReadStream, NSInputStream); | 196 CF_TO_NS_CAST_DECL(CFReadStream, NSInputStream); |
| 197 CF_TO_NS_CAST_DECL(CFWriteStream, NSOutputStream); | 197 CF_TO_NS_CAST_DECL(CFWriteStream, NSOutputStream); |
| 198 CF_TO_NS_MUTABLE_CAST_DECL(String); | 198 CF_TO_NS_MUTABLE_CAST_DECL(String); |
| 199 CF_TO_NS_CAST_DECL(CFURL, NSURL); | 199 CF_TO_NS_CAST_DECL(CFURL, NSURL); |
| 200 | 200 |
| 201 // Stream operations for CFTypes. They can be used with NSTypes as well | 201 // Stream operations for CFTypes. They can be used with NSTypes as well |
| 202 // by using the NSToCFCast methods above. | 202 // by using the NSToCFCast methods above. |
| 203 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); | 203 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); |
| 204 // Operator << can not be overloaded for ObjectiveC types as the compiler | 204 // Operator << can not be overloaded for ObjectiveC types as the compiler |
| 205 // can not distinguish between overloads for id with overloads for void*. | 205 // can not distinguish between overloads for id with overloads for void*. |
| 206 BASE_API extern std::ostream& operator<<(std::ostream& o, const CFErrorRef err); | 206 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, |
| 207 BASE_API extern std::ostream& operator<<(std::ostream& o, | 207 const CFErrorRef err); |
| 208 const CFStringRef str); | 208 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o, |
| 209 const CFStringRef str); |
| 209 | 210 |
| 210 #endif // BASE_MAC_FOUNDATION_UTIL_H_ | 211 #endif // BASE_MAC_FOUNDATION_UTIL_H_ |
| OLD | NEW |