Remove all npapi plugins from windows metro chrome
[chromium-blink-merge.git] / base / mac / foundation_util.h
blob0f687602a4462259e6146ecb6c729a4d52b3580f
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_MAC_FOUNDATION_UTIL_H_
6 #define BASE_MAC_FOUNDATION_UTIL_H_
7 #pragma once
9 #include <CoreFoundation/CoreFoundation.h>
11 #include <string>
12 #include <vector>
14 #include "base/base_export.h"
15 #include "base/logging.h"
16 #include "base/mac/scoped_cftyperef.h"
18 #if defined(__OBJC__)
19 #import <Foundation/Foundation.h>
20 #else // __OBJC__
21 class NSBundle;
22 class NSString;
23 #endif // __OBJC__
25 class FilePath;
27 // Adapted from NSPathUtilities.h and NSObjCRuntime.h.
28 #if __LP64__ || NS_BUILD_32_LIKE_64
29 typedef unsigned long NSSearchPathDirectory;
30 typedef unsigned long NSSearchPathDomainMask;
31 #else
32 typedef unsigned int NSSearchPathDirectory;
33 typedef unsigned int NSSearchPathDomainMask;
34 #endif
36 typedef struct OpaqueSecTrustRef* SecACLRef;
37 typedef struct OpaqueSecTrustedApplicationRef* SecTrustedApplicationRef;
39 namespace base {
40 namespace mac {
42 // Returns true if the application is running from a bundle
43 BASE_EXPORT bool AmIBundled();
44 BASE_EXPORT void SetOverrideAmIBundled(bool value);
46 // Returns true if this process is marked as a "Background only process".
47 BASE_EXPORT bool IsBackgroundOnlyProcess();
49 // Returns the path to a resource within the framework bundle.
50 BASE_EXPORT FilePath PathForFrameworkBundleResource(CFStringRef resourceName);
52 // Returns the creator code associated with the CFBundleRef at bundle.
53 OSType CreatorCodeForCFBundleRef(CFBundleRef bundle);
55 // Returns the creator code associated with this application, by calling
56 // CreatorCodeForCFBundleRef for the application's main bundle. If this
57 // information cannot be determined, returns kUnknownType ('????'). This
58 // does not respect the override app bundle because it's based on CFBundle
59 // instead of NSBundle, and because callers probably don't want the override
60 // app bundle's creator code anyway.
61 BASE_EXPORT OSType CreatorCodeForApplication();
63 // Searches for directories for the given key in only the given |domain_mask|.
64 // If found, fills result (which must always be non-NULL) with the
65 // first found directory and returns true. Otherwise, returns false.
66 BASE_EXPORT bool GetSearchPathDirectory(NSSearchPathDirectory directory,
67 NSSearchPathDomainMask domain_mask,
68 FilePath* result);
70 // Searches for directories for the given key in only the local domain.
71 // If found, fills result (which must always be non-NULL) with the
72 // first found directory and returns true. Otherwise, returns false.
73 BASE_EXPORT bool GetLocalDirectory(NSSearchPathDirectory directory,
74 FilePath* result);
76 // Searches for directories for the given key in only the user domain.
77 // If found, fills result (which must always be non-NULL) with the
78 // first found directory and returns true. Otherwise, returns false.
79 BASE_EXPORT bool GetUserDirectory(NSSearchPathDirectory directory,
80 FilePath* result);
82 // Returns the ~/Library directory.
83 BASE_EXPORT FilePath GetUserLibraryPath();
85 // Takes a path to an (executable) binary and tries to provide the path to an
86 // application bundle containing it. It takes the outermost bundle that it can
87 // find (so for "/Foo/Bar.app/.../Baz.app/..." it produces "/Foo/Bar.app").
88 // |exec_name| - path to the binary
89 // returns - path to the application bundle, or empty on error
90 BASE_EXPORT FilePath GetAppBundlePath(const FilePath& exec_name);
92 #define TYPE_NAME_FOR_CF_TYPE_DECL(TypeCF) \
93 BASE_EXPORT std::string TypeNameForCFType(TypeCF##Ref);
95 TYPE_NAME_FOR_CF_TYPE_DECL(CFArray);
96 TYPE_NAME_FOR_CF_TYPE_DECL(CFBag);
97 TYPE_NAME_FOR_CF_TYPE_DECL(CFBoolean);
98 TYPE_NAME_FOR_CF_TYPE_DECL(CFData);
99 TYPE_NAME_FOR_CF_TYPE_DECL(CFDate);
100 TYPE_NAME_FOR_CF_TYPE_DECL(CFDictionary);
101 TYPE_NAME_FOR_CF_TYPE_DECL(CFNull);
102 TYPE_NAME_FOR_CF_TYPE_DECL(CFNumber);
103 TYPE_NAME_FOR_CF_TYPE_DECL(CFSet);
104 TYPE_NAME_FOR_CF_TYPE_DECL(CFString);
106 #undef TYPE_NAME_FOR_CF_TYPE_DECL
108 // Retain/release calls for memory management in C++.
109 BASE_EXPORT void NSObjectRetain(void* obj);
110 BASE_EXPORT void NSObjectRelease(void* obj);
112 // CFTypeRefToNSObjectAutorelease transfers ownership of a Core Foundation
113 // object (one derived from CFTypeRef) to the Foundation memory management
114 // system. In a traditional managed-memory environment, cf_object is
115 // autoreleased and returned as an NSObject. In a garbage-collected
116 // environment, cf_object is marked as eligible for garbage collection.
118 // This function should only be used to convert a concrete CFTypeRef type to
119 // its equivalent "toll-free bridged" NSObject subclass, for example,
120 // converting a CFStringRef to NSString.
122 // By calling this function, callers relinquish any ownership claim to
123 // cf_object. In a managed-memory environment, the object's ownership will be
124 // managed by the innermost NSAutoreleasePool, so after this function returns,
125 // callers should not assume that cf_object is valid any longer than the
126 // returned NSObject.
128 // Returns an id, typed here for C++'s sake as a void*.
129 BASE_EXPORT void* CFTypeRefToNSObjectAutorelease(CFTypeRef cf_object);
131 // Returns the base bundle ID, which can be set by SetBaseBundleID but
132 // defaults to a reasonable string. This never returns NULL. BaseBundleID
133 // returns a pointer to static storage that must not be freed.
134 BASE_EXPORT const char* BaseBundleID();
136 // Sets the base bundle ID to override the default. The implementation will
137 // make its own copy of new_base_bundle_id.
138 BASE_EXPORT void SetBaseBundleID(const char* new_base_bundle_id);
140 } // namespace mac
141 } // namespace base
143 #if !defined(__OBJC__)
144 #define OBJC_CPP_CLASS_DECL(x) class x;
145 #else // __OBJC__
146 #define OBJC_CPP_CLASS_DECL(x)
147 #endif // __OBJC__
149 // Convert toll-free bridged CFTypes to NSTypes and vice-versa. This does not
150 // autorelease |cf_val|. This is useful for the case where there is a CFType in
151 // a call that expects an NSType and the compiler is complaining about const
152 // casting problems.
153 // The calls are used like this:
154 // NSString *foo = CFToNSCast(CFSTR("Hello"));
155 // CFStringRef foo2 = NSToCFCast(@"Hello");
156 // The macro magic below is to enforce safe casting. It could possibly have
157 // been done using template function specialization, but template function
158 // specialization doesn't always work intuitively,
159 // (http://www.gotw.ca/publications/mill17.htm) so the trusty combination
160 // of macros and function overloading is used instead.
162 #define CF_TO_NS_CAST_DECL(TypeCF, TypeNS) \
163 OBJC_CPP_CLASS_DECL(TypeNS) \
165 namespace base { \
166 namespace mac { \
167 BASE_EXPORT TypeNS* CFToNSCast(TypeCF##Ref cf_val); \
168 BASE_EXPORT TypeCF##Ref NSToCFCast(TypeNS* ns_val); \
172 #define CF_TO_NS_MUTABLE_CAST_DECL(name) \
173 CF_TO_NS_CAST_DECL(CF##name, NS##name) \
174 OBJC_CPP_CLASS_DECL(NSMutable##name) \
176 namespace base { \
177 namespace mac { \
178 BASE_EXPORT NSMutable##name* CFToNSCast(CFMutable##name##Ref cf_val); \
179 BASE_EXPORT CFMutable##name##Ref NSToCFCast(NSMutable##name* ns_val); \
183 // List of toll-free bridged types taken from:
184 // http://www.cocoadev.com/index.pl?TollFreeBridged
186 CF_TO_NS_MUTABLE_CAST_DECL(Array);
187 CF_TO_NS_MUTABLE_CAST_DECL(AttributedString);
188 CF_TO_NS_CAST_DECL(CFCalendar, NSCalendar);
189 CF_TO_NS_MUTABLE_CAST_DECL(CharacterSet);
190 CF_TO_NS_MUTABLE_CAST_DECL(Data);
191 CF_TO_NS_CAST_DECL(CFDate, NSDate);
192 CF_TO_NS_MUTABLE_CAST_DECL(Dictionary);
193 CF_TO_NS_CAST_DECL(CFError, NSError);
194 CF_TO_NS_CAST_DECL(CFLocale, NSLocale);
195 CF_TO_NS_CAST_DECL(CFNumber, NSNumber);
196 CF_TO_NS_CAST_DECL(CFRunLoopTimer, NSTimer);
197 CF_TO_NS_CAST_DECL(CFTimeZone, NSTimeZone);
198 CF_TO_NS_MUTABLE_CAST_DECL(Set);
199 CF_TO_NS_CAST_DECL(CFReadStream, NSInputStream);
200 CF_TO_NS_CAST_DECL(CFWriteStream, NSOutputStream);
201 CF_TO_NS_MUTABLE_CAST_DECL(String);
202 CF_TO_NS_CAST_DECL(CFURL, NSURL);
204 #undef CF_TO_NS_CAST_DECL
205 #undef CF_TO_NS_MUTABLE_CAST_DECL
206 #undef OBJC_CPP_CLASS_DECL
208 namespace base {
209 namespace mac {
211 // CFCast<>() and CFCastStrict<>() cast a basic CFTypeRef to a more
212 // specific CoreFoundation type. The compatibility of the passed
213 // object is found by comparing its opaque type against the
214 // requested type identifier. If the supplied object is not
215 // compatible with the requested return type, CFCast<>() returns
216 // NULL and CFCastStrict<>() will DCHECK. Providing a NULL pointer
217 // to either variant results in NULL being returned without
218 // triggering any DCHECK.
220 // Example usage:
221 // CFNumberRef some_number = base::mac::CFCast<CFNumberRef>(
222 // CFArrayGetValueAtIndex(array, index));
224 // CFTypeRef hello = CFSTR("hello world");
225 // CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>(hello);
227 template<typename T>
228 T CFCast(const CFTypeRef& cf_val);
230 template<typename T>
231 T CFCastStrict(const CFTypeRef& cf_val);
233 #define CF_CAST_DECL(TypeCF) \
234 template<> BASE_EXPORT TypeCF##Ref \
235 CFCast<TypeCF##Ref>(const CFTypeRef& cf_val);\
237 template<> BASE_EXPORT TypeCF##Ref \
238 CFCastStrict<TypeCF##Ref>(const CFTypeRef& cf_val);
240 CF_CAST_DECL(CFArray);
241 CF_CAST_DECL(CFBag);
242 CF_CAST_DECL(CFBoolean);
243 CF_CAST_DECL(CFData);
244 CF_CAST_DECL(CFDate);
245 CF_CAST_DECL(CFDictionary);
246 CF_CAST_DECL(CFNull);
247 CF_CAST_DECL(CFNumber);
248 CF_CAST_DECL(CFSet);
249 CF_CAST_DECL(CFString);
251 CF_CAST_DECL(SecACL);
252 CF_CAST_DECL(SecTrustedApplication);
254 #undef CF_CAST_DECL
256 #if defined(__OBJC__)
258 // ObjCCast<>() and ObjCCastStrict<>() cast a basic id to a more
259 // specific (NSObject-derived) type. The compatibility of the passed
260 // object is found by checking if it's a kind of the requested type
261 // identifier. If the supplied object is not compatible with the
262 // requested return type, ObjCCast<>() returns nil and
263 // ObjCCastStrict<>() will DCHECK. Providing a nil pointer to either
264 // variant results in nil being returned without triggering any DCHECK.
266 // The strict variant is useful when retrieving a value from a
267 // collection which only has values of a specific type, e.g. an
268 // NSArray of NSStrings. The non-strict variant is useful when
269 // retrieving values from data that you can't fully control. For
270 // example, a plist read from disk may be beyond your exclusive
271 // control, so you'd only want to check that the values you retrieve
272 // from it are of the expected types, but not crash if they're not.
274 // Example usage:
275 // NSString* version = base::mac::ObjCCast<NSString>(
276 // [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]);
278 // NSString* str = base::mac::ObjCCastStrict<NSString>(
279 // [ns_arr_of_ns_strs objectAtIndex:0]);
280 template<typename T>
281 T* ObjCCast(id objc_val) {
282 if ([objc_val isKindOfClass:[T class]]) {
283 return reinterpret_cast<T*>(objc_val);
285 return nil;
288 template<typename T>
289 T* ObjCCastStrict(id objc_val) {
290 T* rv = ObjCCast<T>(objc_val);
291 DCHECK(objc_val == nil || rv);
292 return rv;
295 #endif // defined(__OBJC__)
297 // Helper function for GetValueFromDictionary to create the error message
298 // that appears when a type mismatch is encountered.
299 BASE_EXPORT std::string GetValueFromDictionaryErrorMessage(
300 CFStringRef key, const std::string& expected_type, CFTypeRef value);
302 // Utility function to pull out a value from a dictionary, check its type, and
303 // return it. Returns NULL if the key is not present or of the wrong type.
304 template<typename T>
305 T GetValueFromDictionary(CFDictionaryRef dict, CFStringRef key) {
306 CFTypeRef value = CFDictionaryGetValue(dict, key);
307 T value_specific = CFCast<T>(value);
309 if (value && !value_specific) {
310 std::string expected_type = TypeNameForCFType(value_specific);
311 DLOG(WARNING) << GetValueFromDictionaryErrorMessage(key,
312 expected_type,
313 value);
316 return value_specific;
319 // Converts |path| to an autoreleased NSString. Returns nil if |path| is empty.
320 BASE_EXPORT NSString* FilePathToNSString(const FilePath& path);
322 // Converts |str| to a FilePath. Returns an empty path if |str| is nil.
323 BASE_EXPORT FilePath NSStringToFilePath(NSString* str);
325 } // namespace mac
326 } // namespace base
328 // Stream operations for CFTypes. They can be used with NSTypes as well
329 // by using the NSToCFCast methods above.
330 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo");
331 // Operator << can not be overloaded for ObjectiveC types as the compiler
332 // can not distinguish between overloads for id with overloads for void*.
333 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o,
334 const CFErrorRef err);
335 BASE_EXPORT extern std::ostream& operator<<(std::ostream& o,
336 const CFStringRef str);
338 #endif // BASE_MAC_FOUNDATION_UTIL_H_