blob: ee23a17fb16e6c0c5c6856c6cd0ccbd9dd43c120 [file] [log] [blame]
[email protected]b0a28ee92012-01-19 10:55:581// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c0a5c49d2011-01-28 16:47:532// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_MAC_FOUNDATION_UTIL_H_
6#define BASE_MAC_FOUNDATION_UTIL_H_
[email protected]c0a5c49d2011-01-28 16:47:537
[email protected]151c4a62011-04-22 04:15:138#include <CoreFoundation/CoreFoundation.h>
9
[email protected]c0a5c49d2011-01-28 16:47:5310#include <string>
11#include <vector>
12
[email protected]0bea7252011-08-05 15:34:0013#include "base/base_export.h"
[email protected]c0a5c49d2011-01-28 16:47:5314#include "base/logging.h"
[email protected]8d96d622011-11-15 00:03:5415#include "base/mac/scoped_cftyperef.h"
avidbcc8412015-12-24 06:28:3216#include "build/build_config.h"
[email protected]c0a5c49d2011-01-28 16:47:5317
18#if defined(__OBJC__)
19#import <Foundation/Foundation.h>
[email protected]f84cc472013-10-30 18:21:4120@class NSFont;
21@class UIFont;
[email protected]c0a5c49d2011-01-28 16:47:5322#else // __OBJC__
[email protected]db87d6a2012-07-11 15:02:3023#include <CoreFoundation/CoreFoundation.h>
[email protected]c0a5c49d2011-01-28 16:47:5324class NSBundle;
[email protected]f84cc472013-10-30 18:21:4125class NSFont;
[email protected]151c4a62011-04-22 04:15:1326class NSString;
[email protected]f84cc472013-10-30 18:21:4127class UIFont;
[email protected]c0a5c49d2011-01-28 16:47:5328#endif // __OBJC__
29
[email protected]c83f3fad2012-07-24 15:01:5230#if defined(OS_IOS)
31#include <CoreText/CoreText.h>
32#else
33#include <ApplicationServices/ApplicationServices.h>
34#endif
35
[email protected]965bcb02013-04-05 10:45:0836// Adapted from NSObjCRuntime.h NS_ENUM definition (used in Foundation starting
37// with the OS X 10.8 SDK and the iOS 6.0 SDK).
38#if __has_extension(cxx_strong_enums) && \
39 (defined(OS_IOS) || (defined(MAC_OS_X_VERSION_10_8) && \
40 MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_8))
41#define CR_FORWARD_ENUM(_type, _name) enum _name : _type _name
42#else
43#define CR_FORWARD_ENUM(_type, _name) _type _name
44#endif
45
[email protected]c0a5c49d2011-01-28 16:47:5346// Adapted from NSPathUtilities.h and NSObjCRuntime.h.
47#if __LP64__ || NS_BUILD_32_LIKE_64
[email protected]965bcb02013-04-05 10:45:0848typedef CR_FORWARD_ENUM(unsigned long, NSSearchPathDirectory);
[email protected]c0a5c49d2011-01-28 16:47:5349typedef unsigned long NSSearchPathDomainMask;
50#else
[email protected]965bcb02013-04-05 10:45:0851typedef CR_FORWARD_ENUM(unsigned int, NSSearchPathDirectory);
[email protected]c0a5c49d2011-01-28 16:47:5352typedef unsigned int NSSearchPathDomainMask;
53#endif
54
[email protected]ec0d07e2012-05-16 16:57:5355typedef struct OpaqueSecTrustRef* SecACLRef;
56typedef struct OpaqueSecTrustedApplicationRef* SecTrustedApplicationRef;
57
[email protected]c0a5c49d2011-01-28 16:47:5358namespace base {
[email protected]a3ef4832013-02-02 05:12:3359
60class FilePath;
61
[email protected]c0a5c49d2011-01-28 16:47:5362namespace mac {
63
64// Returns true if the application is running from a bundle
[email protected]0bea7252011-08-05 15:34:0065BASE_EXPORT bool AmIBundled();
66BASE_EXPORT void SetOverrideAmIBundled(bool value);
[email protected]c0a5c49d2011-01-28 16:47:5367
Evan Stadef11e8ceb22015-01-08 23:11:2668#if defined(UNIT_TEST)
69// This is required because instantiating some tests requires checking the
70// directory structure, which sets the AmIBundled cache state. Individual tests
71// may or may not be bundled, and this would trip them up if the cache weren't
72// cleared. This should not be called from individual tests, just from test
73// instantiation code that gets a path from PathService.
74BASE_EXPORT void ClearAmIBundledCache();
75#endif
76
[email protected]c0a5c49d2011-01-28 16:47:5377// Returns true if this process is marked as a "Background only process".
[email protected]0bea7252011-08-05 15:34:0078BASE_EXPORT bool IsBackgroundOnlyProcess();
[email protected]c0a5c49d2011-01-28 16:47:5379
[email protected]b0a28ee92012-01-19 10:55:5880// Returns the path to a resource within the framework bundle.
[email protected]ed14b6942012-03-08 22:55:1581BASE_EXPORT FilePath PathForFrameworkBundleResource(CFStringRef resourceName);
[email protected]c0a5c49d2011-01-28 16:47:5382
83// Returns the creator code associated with the CFBundleRef at bundle.
84OSType CreatorCodeForCFBundleRef(CFBundleRef bundle);
85
86// Returns the creator code associated with this application, by calling
87// CreatorCodeForCFBundleRef for the application's main bundle. If this
88// information cannot be determined, returns kUnknownType ('????'). This
89// does not respect the override app bundle because it's based on CFBundle
90// instead of NSBundle, and because callers probably don't want the override
91// app bundle's creator code anyway.
[email protected]0bea7252011-08-05 15:34:0092BASE_EXPORT OSType CreatorCodeForApplication();
[email protected]c0a5c49d2011-01-28 16:47:5393
94// Searches for directories for the given key in only the given |domain_mask|.
95// If found, fills result (which must always be non-NULL) with the
96// first found directory and returns true. Otherwise, returns false.
[email protected]63b8b012012-05-10 00:38:4897BASE_EXPORT bool GetSearchPathDirectory(NSSearchPathDirectory directory,
98 NSSearchPathDomainMask domain_mask,
99 FilePath* result);
[email protected]c0a5c49d2011-01-28 16:47:53100
101// Searches for directories for the given key in only the local domain.
102// If found, fills result (which must always be non-NULL) with the
103// first found directory and returns true. Otherwise, returns false.
[email protected]0bea7252011-08-05 15:34:00104BASE_EXPORT bool GetLocalDirectory(NSSearchPathDirectory directory,
105 FilePath* result);
[email protected]c0a5c49d2011-01-28 16:47:53106
107// Searches for directories for the given key in only the user domain.
108// If found, fills result (which must always be non-NULL) with the
109// first found directory and returns true. Otherwise, returns false.
[email protected]0bea7252011-08-05 15:34:00110BASE_EXPORT bool GetUserDirectory(NSSearchPathDirectory directory,
111 FilePath* result);
[email protected]c0a5c49d2011-01-28 16:47:53112
113// Returns the ~/Library directory.
[email protected]0bea7252011-08-05 15:34:00114BASE_EXPORT FilePath GetUserLibraryPath();
[email protected]c0a5c49d2011-01-28 16:47:53115
116// Takes a path to an (executable) binary and tries to provide the path to an
117// application bundle containing it. It takes the outermost bundle that it can
118// find (so for "/Foo/Bar.app/.../Baz.app/..." it produces "/Foo/Bar.app").
119// |exec_name| - path to the binary
120// returns - path to the application bundle, or empty on error
[email protected]0bea7252011-08-05 15:34:00121BASE_EXPORT FilePath GetAppBundlePath(const FilePath& exec_name);
[email protected]c0a5c49d2011-01-28 16:47:53122
[email protected]8d96d622011-11-15 00:03:54123#define TYPE_NAME_FOR_CF_TYPE_DECL(TypeCF) \
[email protected]c3bc1ff2012-02-09 18:51:35124BASE_EXPORT std::string TypeNameForCFType(TypeCF##Ref);
[email protected]8d96d622011-11-15 00:03:54125
126TYPE_NAME_FOR_CF_TYPE_DECL(CFArray);
127TYPE_NAME_FOR_CF_TYPE_DECL(CFBag);
128TYPE_NAME_FOR_CF_TYPE_DECL(CFBoolean);
129TYPE_NAME_FOR_CF_TYPE_DECL(CFData);
130TYPE_NAME_FOR_CF_TYPE_DECL(CFDate);
131TYPE_NAME_FOR_CF_TYPE_DECL(CFDictionary);
132TYPE_NAME_FOR_CF_TYPE_DECL(CFNull);
133TYPE_NAME_FOR_CF_TYPE_DECL(CFNumber);
134TYPE_NAME_FOR_CF_TYPE_DECL(CFSet);
135TYPE_NAME_FOR_CF_TYPE_DECL(CFString);
[email protected]39fc5d322012-09-15 10:54:55136TYPE_NAME_FOR_CF_TYPE_DECL(CFURL);
137TYPE_NAME_FOR_CF_TYPE_DECL(CFUUID);
[email protected]8d96d622011-11-15 00:03:54138
[email protected]04a07542012-07-23 16:51:25139TYPE_NAME_FOR_CF_TYPE_DECL(CGColor);
140
141TYPE_NAME_FOR_CF_TYPE_DECL(CTFont);
142TYPE_NAME_FOR_CF_TYPE_DECL(CTRun);
143
[email protected]8d96d622011-11-15 00:03:54144#undef TYPE_NAME_FOR_CF_TYPE_DECL
145
[email protected]c0a5c49d2011-01-28 16:47:53146// Retain/release calls for memory management in C++.
[email protected]0bea7252011-08-05 15:34:00147BASE_EXPORT void NSObjectRetain(void* obj);
148BASE_EXPORT void NSObjectRelease(void* obj);
[email protected]c0a5c49d2011-01-28 16:47:53149
[email protected]1671162f2011-04-29 15:06:32150// CFTypeRefToNSObjectAutorelease transfers ownership of a Core Foundation
151// object (one derived from CFTypeRef) to the Foundation memory management
152// system. In a traditional managed-memory environment, cf_object is
153// autoreleased and returned as an NSObject. In a garbage-collected
154// environment, cf_object is marked as eligible for garbage collection.
155//
156// This function should only be used to convert a concrete CFTypeRef type to
157// its equivalent "toll-free bridged" NSObject subclass, for example,
158// converting a CFStringRef to NSString.
159//
160// By calling this function, callers relinquish any ownership claim to
161// cf_object. In a managed-memory environment, the object's ownership will be
162// managed by the innermost NSAutoreleasePool, so after this function returns,
163// callers should not assume that cf_object is valid any longer than the
164// returned NSObject.
165//
166// Returns an id, typed here for C++'s sake as a void*.
[email protected]0bea7252011-08-05 15:34:00167BASE_EXPORT void* CFTypeRefToNSObjectAutorelease(CFTypeRef cf_object);
[email protected]1671162f2011-04-29 15:06:32168
[email protected]151c4a62011-04-22 04:15:13169// Returns the base bundle ID, which can be set by SetBaseBundleID but
170// defaults to a reasonable string. This never returns NULL. BaseBundleID
171// returns a pointer to static storage that must not be freed.
[email protected]0bea7252011-08-05 15:34:00172BASE_EXPORT const char* BaseBundleID();
[email protected]151c4a62011-04-22 04:15:13173
174// Sets the base bundle ID to override the default. The implementation will
175// make its own copy of new_base_bundle_id.
[email protected]0bea7252011-08-05 15:34:00176BASE_EXPORT void SetBaseBundleID(const char* new_base_bundle_id);
[email protected]151c4a62011-04-22 04:15:13177
[email protected]c0a5c49d2011-01-28 16:47:53178} // namespace mac
179} // namespace base
180
[email protected]27d02e02011-05-05 21:27:07181#if !defined(__OBJC__)
182#define OBJC_CPP_CLASS_DECL(x) class x;
183#else // __OBJC__
184#define OBJC_CPP_CLASS_DECL(x)
185#endif // __OBJC__
186
187// Convert toll-free bridged CFTypes to NSTypes and vice-versa. This does not
188// autorelease |cf_val|. This is useful for the case where there is a CFType in
189// a call that expects an NSType and the compiler is complaining about const
190// casting problems.
191// The calls are used like this:
192// NSString *foo = CFToNSCast(CFSTR("Hello"));
193// CFStringRef foo2 = NSToCFCast(@"Hello");
194// The macro magic below is to enforce safe casting. It could possibly have
195// been done using template function specialization, but template function
196// specialization doesn't always work intuitively,
197// (http://www.gotw.ca/publications/mill17.htm) so the trusty combination
198// of macros and function overloading is used instead.
199
200#define CF_TO_NS_CAST_DECL(TypeCF, TypeNS) \
201OBJC_CPP_CLASS_DECL(TypeNS) \
202\
203namespace base { \
204namespace mac { \
[email protected]0bea7252011-08-05 15:34:00205BASE_EXPORT TypeNS* CFToNSCast(TypeCF##Ref cf_val); \
206BASE_EXPORT TypeCF##Ref NSToCFCast(TypeNS* ns_val); \
[email protected]27d02e02011-05-05 21:27:07207} \
[email protected]28f56492011-10-19 00:36:56208}
[email protected]27d02e02011-05-05 21:27:07209
210#define CF_TO_NS_MUTABLE_CAST_DECL(name) \
211CF_TO_NS_CAST_DECL(CF##name, NS##name) \
212OBJC_CPP_CLASS_DECL(NSMutable##name) \
213\
214namespace base { \
215namespace mac { \
[email protected]0bea7252011-08-05 15:34:00216BASE_EXPORT NSMutable##name* CFToNSCast(CFMutable##name##Ref cf_val); \
217BASE_EXPORT CFMutable##name##Ref NSToCFCast(NSMutable##name* ns_val); \
[email protected]27d02e02011-05-05 21:27:07218} \
[email protected]28f56492011-10-19 00:36:56219}
[email protected]27d02e02011-05-05 21:27:07220
221// List of toll-free bridged types taken from:
222// http://www.cocoadev.com/index.pl?TollFreeBridged
223
224CF_TO_NS_MUTABLE_CAST_DECL(Array);
225CF_TO_NS_MUTABLE_CAST_DECL(AttributedString);
226CF_TO_NS_CAST_DECL(CFCalendar, NSCalendar);
227CF_TO_NS_MUTABLE_CAST_DECL(CharacterSet);
228CF_TO_NS_MUTABLE_CAST_DECL(Data);
229CF_TO_NS_CAST_DECL(CFDate, NSDate);
230CF_TO_NS_MUTABLE_CAST_DECL(Dictionary);
231CF_TO_NS_CAST_DECL(CFError, NSError);
232CF_TO_NS_CAST_DECL(CFLocale, NSLocale);
233CF_TO_NS_CAST_DECL(CFNumber, NSNumber);
234CF_TO_NS_CAST_DECL(CFRunLoopTimer, NSTimer);
235CF_TO_NS_CAST_DECL(CFTimeZone, NSTimeZone);
236CF_TO_NS_MUTABLE_CAST_DECL(Set);
237CF_TO_NS_CAST_DECL(CFReadStream, NSInputStream);
238CF_TO_NS_CAST_DECL(CFWriteStream, NSOutputStream);
239CF_TO_NS_MUTABLE_CAST_DECL(String);
240CF_TO_NS_CAST_DECL(CFURL, NSURL);
241
[email protected]f84cc472013-10-30 18:21:41242#if defined(OS_IOS)
243CF_TO_NS_CAST_DECL(CTFont, UIFont);
244#else
245CF_TO_NS_CAST_DECL(CTFont, NSFont);
246#endif
247
[email protected]8d96d622011-11-15 00:03:54248#undef CF_TO_NS_CAST_DECL
249#undef CF_TO_NS_MUTABLE_CAST_DECL
250#undef OBJC_CPP_CLASS_DECL
251
[email protected]28f56492011-10-19 00:36:56252namespace base {
253namespace mac {
254
255// CFCast<>() and CFCastStrict<>() cast a basic CFTypeRef to a more
256// specific CoreFoundation type. The compatibility of the passed
257// object is found by comparing its opaque type against the
258// requested type identifier. If the supplied object is not
259// compatible with the requested return type, CFCast<>() returns
260// NULL and CFCastStrict<>() will DCHECK. Providing a NULL pointer
261// to either variant results in NULL being returned without
262// triggering any DCHECK.
263//
264// Example usage:
265// CFNumberRef some_number = base::mac::CFCast<CFNumberRef>(
266// CFArrayGetValueAtIndex(array, index));
267//
[email protected]f54596d2011-12-01 22:12:57268// CFTypeRef hello = CFSTR("hello world");
269// CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>(hello);
[email protected]c3bc1ff2012-02-09 18:51:35270
271template<typename T>
[email protected]28f56492011-10-19 00:36:56272T CFCast(const CFTypeRef& cf_val);
273
[email protected]c3bc1ff2012-02-09 18:51:35274template<typename T>
[email protected]28f56492011-10-19 00:36:56275T CFCastStrict(const CFTypeRef& cf_val);
276
[email protected]c3bc1ff2012-02-09 18:51:35277#define CF_CAST_DECL(TypeCF) \
278template<> BASE_EXPORT TypeCF##Ref \
279CFCast<TypeCF##Ref>(const CFTypeRef& cf_val);\
280\
281template<> BASE_EXPORT TypeCF##Ref \
282CFCastStrict<TypeCF##Ref>(const CFTypeRef& cf_val);
283
284CF_CAST_DECL(CFArray);
285CF_CAST_DECL(CFBag);
286CF_CAST_DECL(CFBoolean);
287CF_CAST_DECL(CFData);
288CF_CAST_DECL(CFDate);
289CF_CAST_DECL(CFDictionary);
290CF_CAST_DECL(CFNull);
291CF_CAST_DECL(CFNumber);
292CF_CAST_DECL(CFSet);
293CF_CAST_DECL(CFString);
[email protected]39fc5d322012-09-15 10:54:55294CF_CAST_DECL(CFURL);
295CF_CAST_DECL(CFUUID);
[email protected]c3bc1ff2012-02-09 18:51:35296
[email protected]04a07542012-07-23 16:51:25297CF_CAST_DECL(CGColor);
298
299CF_CAST_DECL(CTFont);
tapted2daf5442015-03-05 00:42:04300CF_CAST_DECL(CTFontDescriptor);
[email protected]04a07542012-07-23 16:51:25301CF_CAST_DECL(CTRun);
302
[email protected]ec0d07e2012-05-16 16:57:53303CF_CAST_DECL(SecACL);
304CF_CAST_DECL(SecTrustedApplication);
305
306#undef CF_CAST_DECL
[email protected]c3bc1ff2012-02-09 18:51:35307
[email protected]057c21bc2011-10-24 18:04:16308#if defined(__OBJC__)
309
310// ObjCCast<>() and ObjCCastStrict<>() cast a basic id to a more
311// specific (NSObject-derived) type. The compatibility of the passed
312// object is found by checking if it's a kind of the requested type
313// identifier. If the supplied object is not compatible with the
314// requested return type, ObjCCast<>() returns nil and
315// ObjCCastStrict<>() will DCHECK. Providing a nil pointer to either
316// variant results in nil being returned without triggering any DCHECK.
317//
318// The strict variant is useful when retrieving a value from a
319// collection which only has values of a specific type, e.g. an
320// NSArray of NSStrings. The non-strict variant is useful when
321// retrieving values from data that you can't fully control. For
322// example, a plist read from disk may be beyond your exclusive
323// control, so you'd only want to check that the values you retrieve
324// from it are of the expected types, but not crash if they're not.
325//
326// Example usage:
327// NSString* version = base::mac::ObjCCast<NSString>(
328// [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"]);
329//
330// NSString* str = base::mac::ObjCCastStrict<NSString>(
331// [ns_arr_of_ns_strs objectAtIndex:0]);
[email protected]c3bc1ff2012-02-09 18:51:35332template<typename T>
[email protected]057c21bc2011-10-24 18:04:16333T* ObjCCast(id objc_val) {
334 if ([objc_val isKindOfClass:[T class]]) {
335 return reinterpret_cast<T*>(objc_val);
336 }
337 return nil;
338}
339
[email protected]c3bc1ff2012-02-09 18:51:35340template<typename T>
[email protected]057c21bc2011-10-24 18:04:16341T* ObjCCastStrict(id objc_val) {
342 T* rv = ObjCCast<T>(objc_val);
343 DCHECK(objc_val == nil || rv);
344 return rv;
345}
346
347#endif // defined(__OBJC__)
348
[email protected]f54596d2011-12-01 22:12:57349// Helper function for GetValueFromDictionary to create the error message
350// that appears when a type mismatch is encountered.
[email protected]080bd4f2012-03-12 14:26:02351BASE_EXPORT std::string GetValueFromDictionaryErrorMessage(
[email protected]f54596d2011-12-01 22:12:57352 CFStringRef key, const std::string& expected_type, CFTypeRef value);
353
[email protected]8d96d622011-11-15 00:03:54354// Utility function to pull out a value from a dictionary, check its type, and
[email protected]f54596d2011-12-01 22:12:57355// return it. Returns NULL if the key is not present or of the wrong type.
[email protected]c3bc1ff2012-02-09 18:51:35356template<typename T>
[email protected]8d96d622011-11-15 00:03:54357T GetValueFromDictionary(CFDictionaryRef dict, CFStringRef key) {
358 CFTypeRef value = CFDictionaryGetValue(dict, key);
359 T value_specific = CFCast<T>(value);
360
361 if (value && !value_specific) {
362 std::string expected_type = TypeNameForCFType(value_specific);
363 DLOG(WARNING) << GetValueFromDictionaryErrorMessage(key,
364 expected_type,
365 value);
366 }
367
368 return value_specific;
369}
370
[email protected]b6b72222012-02-11 02:04:13371// Converts |path| to an autoreleased NSString. Returns nil if |path| is empty.
[email protected]ed14b6942012-03-08 22:55:15372BASE_EXPORT NSString* FilePathToNSString(const FilePath& path);
[email protected]b6b72222012-02-11 02:04:13373
374// Converts |str| to a FilePath. Returns an empty path if |str| is nil.
[email protected]ed14b6942012-03-08 22:55:15375BASE_EXPORT FilePath NSStringToFilePath(NSString* str);
[email protected]b6b72222012-02-11 02:04:13376
marq6c505002015-09-01 16:02:03377#if defined(__OBJC__)
378// Converts |range| to an NSRange, returning the new range in |range_out|.
379// Returns true if conversion was successful, false if the values of |range|
380// could not be converted to NSUIntegers.
381BASE_EXPORT bool CFRangeToNSRange(CFRange range,
382 NSRange* range_out) WARN_UNUSED_RESULT;
383#endif // defined(__OBJC__)
384
[email protected]28f56492011-10-19 00:36:56385} // namespace mac
386} // namespace base
387
[email protected]27d02e02011-05-05 21:27:07388// Stream operations for CFTypes. They can be used with NSTypes as well
389// by using the NSToCFCast methods above.
390// e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo");
391// Operator << can not be overloaded for ObjectiveC types as the compiler
392// can not distinguish between overloads for id with overloads for void*.
[email protected]0bea7252011-08-05 15:34:00393BASE_EXPORT extern std::ostream& operator<<(std::ostream& o,
394 const CFErrorRef err);
395BASE_EXPORT extern std::ostream& operator<<(std::ostream& o,
396 const CFStringRef str);
[email protected]27d02e02011-05-05 21:27:07397
[email protected]c0a5c49d2011-01-28 16:47:53398#endif // BASE_MAC_FOUNDATION_UTIL_H_