Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(662)

Side by Side Diff: base/mac/foundation_util.h

Issue 7474010: mac: Components build for base, easy part (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/file_version_info.h ('k') | base/mac/mac_util.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/logging.h" 15 #include "base/logging.h"
15 16
16 #if defined(__OBJC__) 17 #if defined(__OBJC__)
17 #import <Foundation/Foundation.h> 18 #import <Foundation/Foundation.h>
18 #else // __OBJC__ 19 #else // __OBJC__
19 class NSBundle; 20 class NSBundle;
20 class NSString; 21 class NSString;
21 #endif // __OBJC__ 22 #endif // __OBJC__
22 23
23 class FilePath; 24 class FilePath;
24 25
25 // Adapted from NSPathUtilities.h and NSObjCRuntime.h. 26 // Adapted from NSPathUtilities.h and NSObjCRuntime.h.
26 #if __LP64__ || NS_BUILD_32_LIKE_64 27 #if __LP64__ || NS_BUILD_32_LIKE_64
27 typedef unsigned long NSSearchPathDirectory; 28 typedef unsigned long NSSearchPathDirectory;
28 typedef unsigned long NSSearchPathDomainMask; 29 typedef unsigned long NSSearchPathDomainMask;
29 #else 30 #else
30 typedef unsigned int NSSearchPathDirectory; 31 typedef unsigned int NSSearchPathDirectory;
31 typedef unsigned int NSSearchPathDomainMask; 32 typedef unsigned int NSSearchPathDomainMask;
32 #endif 33 #endif
33 34
34 namespace base { 35 namespace base {
35 namespace mac { 36 namespace mac {
36 37
37 // Returns true if the application is running from a bundle 38 // Returns true if the application is running from a bundle
38 bool AmIBundled(); 39 BASE_API bool AmIBundled();
39 void SetOverrideAmIBundled(bool value); 40 BASE_API void SetOverrideAmIBundled(bool value);
40 41
41 // 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".
42 bool IsBackgroundOnlyProcess(); 43 BASE_API bool IsBackgroundOnlyProcess();
43 44
44 // 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
45 // 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
46 // aren't a bundle. 47 // aren't a bundle.
47 NSBundle* MainAppBundle(); 48 BASE_API NSBundle* MainAppBundle();
48 FilePath MainAppBundlePath(); 49 BASE_API FilePath MainAppBundlePath();
49 50
50 // Returns the path to a resource within the MainAppBundle. 51 // Returns the path to a resource within the MainAppBundle.
51 FilePath PathForMainAppBundleResource(CFStringRef resourceName); 52 FilePath PathForMainAppBundleResource(CFStringRef resourceName);
52 53
53 // Set the bundle that MainAppBundle will return, overriding the default value 54 // Set the bundle that MainAppBundle will return, overriding the default value
54 // (Restore the default by calling SetOverrideAppBundle(nil)). 55 // (Restore the default by calling SetOverrideAppBundle(nil)).
55 void SetOverrideAppBundle(NSBundle* bundle); 56 BASE_API void SetOverrideAppBundle(NSBundle* bundle);
56 void SetOverrideAppBundlePath(const FilePath& file_path); 57 BASE_API void SetOverrideAppBundlePath(const FilePath& file_path);
57 58
58 // Returns the creator code associated with the CFBundleRef at bundle. 59 // Returns the creator code associated with the CFBundleRef at bundle.
59 OSType CreatorCodeForCFBundleRef(CFBundleRef bundle); 60 OSType CreatorCodeForCFBundleRef(CFBundleRef bundle);
60 61
61 // Returns the creator code associated with this application, by calling 62 // Returns the creator code associated with this application, by calling
62 // CreatorCodeForCFBundleRef for the application's main bundle. If this 63 // CreatorCodeForCFBundleRef for the application's main bundle. If this
63 // information cannot be determined, returns kUnknownType ('????'). This 64 // information cannot be determined, returns kUnknownType ('????'). This
64 // 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
65 // 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
66 // app bundle's creator code anyway. 67 // app bundle's creator code anyway.
67 OSType CreatorCodeForApplication(); 68 BASE_API OSType CreatorCodeForApplication();
68 69
69 // 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|.
70 // 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
71 // first found directory and returns true. Otherwise, returns false. 72 // first found directory and returns true. Otherwise, returns false.
72 bool GetSearchPathDirectory(NSSearchPathDirectory directory, 73 bool GetSearchPathDirectory(NSSearchPathDirectory directory,
73 NSSearchPathDomainMask domain_mask, 74 NSSearchPathDomainMask domain_mask,
74 FilePath* result); 75 FilePath* result);
75 76
76 // 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.
77 // 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
78 // first found directory and returns true. Otherwise, returns false. 79 // first found directory and returns true. Otherwise, returns false.
79 bool GetLocalDirectory(NSSearchPathDirectory directory, FilePath* result); 80 BASE_API bool GetLocalDirectory(NSSearchPathDirectory directory,
81 FilePath* result);
80 82
81 // 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.
82 // 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
83 // first found directory and returns true. Otherwise, returns false. 85 // first found directory and returns true. Otherwise, returns false.
84 bool GetUserDirectory(NSSearchPathDirectory directory, FilePath* result); 86 BASE_API bool GetUserDirectory(NSSearchPathDirectory directory,
87 FilePath* result);
85 88
86 // Returns the ~/Library directory. 89 // Returns the ~/Library directory.
87 FilePath GetUserLibraryPath(); 90 BASE_API FilePath GetUserLibraryPath();
88 91
89 // 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
90 // 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
91 // 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").
92 // |exec_name| - path to the binary 95 // |exec_name| - path to the binary
93 // returns - path to the application bundle, or empty on error 96 // returns - path to the application bundle, or empty on error
94 FilePath GetAppBundlePath(const FilePath& exec_name); 97 BASE_API FilePath GetAppBundlePath(const FilePath& exec_name);
95 98
96 // 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
97 // 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.
98 CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, 101 BASE_API CFTypeRef GetValueFromDictionary(CFDictionaryRef dict,
99 CFStringRef key, 102 CFStringRef key,
100 CFTypeID expected_type); 103 CFTypeID expected_type);
101 104
102 // Retain/release calls for memory management in C++. 105 // Retain/release calls for memory management in C++.
103 void NSObjectRetain(void* obj); 106 BASE_API void NSObjectRetain(void* obj);
104 void NSObjectRelease(void* obj); 107 BASE_API void NSObjectRelease(void* obj);
105 108
106 // CFTypeRefToNSObjectAutorelease transfers ownership of a Core Foundation 109 // CFTypeRefToNSObjectAutorelease transfers ownership of a Core Foundation
107 // object (one derived from CFTypeRef) to the Foundation memory management 110 // object (one derived from CFTypeRef) to the Foundation memory management
108 // system. In a traditional managed-memory environment, cf_object is 111 // system. In a traditional managed-memory environment, cf_object is
109 // autoreleased and returned as an NSObject. In a garbage-collected 112 // autoreleased and returned as an NSObject. In a garbage-collected
110 // environment, cf_object is marked as eligible for garbage collection. 113 // environment, cf_object is marked as eligible for garbage collection.
111 // 114 //
112 // 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
113 // its equivalent "toll-free bridged" NSObject subclass, for example, 116 // its equivalent "toll-free bridged" NSObject subclass, for example,
114 // converting a CFStringRef to NSString. 117 // converting a CFStringRef to NSString.
115 // 118 //
116 // By calling this function, callers relinquish any ownership claim to 119 // By calling this function, callers relinquish any ownership claim to
117 // 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
118 // managed by the innermost NSAutoreleasePool, so after this function returns, 121 // managed by the innermost NSAutoreleasePool, so after this function returns,
119 // 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
120 // returned NSObject. 123 // returned NSObject.
121 // 124 //
122 // 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*.
123 void* CFTypeRefToNSObjectAutorelease(CFTypeRef cf_object); 126 BASE_API void* CFTypeRefToNSObjectAutorelease(CFTypeRef cf_object);
124 127
125 // 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
126 // defaults to a reasonable string. This never returns NULL. BaseBundleID 129 // defaults to a reasonable string. This never returns NULL. BaseBundleID
127 // returns a pointer to static storage that must not be freed. 130 // returns a pointer to static storage that must not be freed.
128 const char* BaseBundleID(); 131 BASE_API const char* BaseBundleID();
129 132
130 // 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
131 // make its own copy of new_base_bundle_id. 134 // make its own copy of new_base_bundle_id.
132 void SetBaseBundleID(const char* new_base_bundle_id); 135 BASE_API void SetBaseBundleID(const char* new_base_bundle_id);
133 136
134 } // namespace mac 137 } // namespace mac
135 } // namespace base 138 } // namespace base
136 139
137 #if !defined(__OBJC__) 140 #if !defined(__OBJC__)
138 #define OBJC_CPP_CLASS_DECL(x) class x; 141 #define OBJC_CPP_CLASS_DECL(x) class x;
139 #else // __OBJC__ 142 #else // __OBJC__
140 #define OBJC_CPP_CLASS_DECL(x) 143 #define OBJC_CPP_CLASS_DECL(x)
141 #endif // __OBJC__ 144 #endif // __OBJC__
142 145
143 // 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
144 // 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
145 // 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
146 // casting problems. 149 // casting problems.
147 // The calls are used like this: 150 // The calls are used like this:
148 // NSString *foo = CFToNSCast(CFSTR("Hello")); 151 // NSString *foo = CFToNSCast(CFSTR("Hello"));
149 // CFStringRef foo2 = NSToCFCast(@"Hello"); 152 // CFStringRef foo2 = NSToCFCast(@"Hello");
150 // 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
151 // been done using template function specialization, but template function 154 // been done using template function specialization, but template function
152 // specialization doesn't always work intuitively, 155 // specialization doesn't always work intuitively,
153 // (http://www.gotw.ca/publications/mill17.htm) so the trusty combination 156 // (http://www.gotw.ca/publications/mill17.htm) so the trusty combination
154 // of macros and function overloading is used instead. 157 // of macros and function overloading is used instead.
155 158
156 #define CF_TO_NS_CAST_DECL(TypeCF, TypeNS) \ 159 #define CF_TO_NS_CAST_DECL(TypeCF, TypeNS) \
157 OBJC_CPP_CLASS_DECL(TypeNS) \ 160 OBJC_CPP_CLASS_DECL(TypeNS) \
158 \ 161 \
159 namespace base { \ 162 namespace base { \
160 namespace mac { \ 163 namespace mac { \
161 TypeNS* CFToNSCast(TypeCF##Ref cf_val); \ 164 BASE_API TypeNS* CFToNSCast(TypeCF##Ref cf_val); \
162 TypeCF##Ref NSToCFCast(TypeNS* ns_val); \ 165 BASE_API TypeCF##Ref NSToCFCast(TypeNS* ns_val); \
163 } \ 166 } \
164 } \ 167 } \
165 168
166 #define CF_TO_NS_MUTABLE_CAST_DECL(name) \ 169 #define CF_TO_NS_MUTABLE_CAST_DECL(name) \
167 CF_TO_NS_CAST_DECL(CF##name, NS##name) \ 170 CF_TO_NS_CAST_DECL(CF##name, NS##name) \
168 OBJC_CPP_CLASS_DECL(NSMutable##name) \ 171 OBJC_CPP_CLASS_DECL(NSMutable##name) \
169 \ 172 \
170 namespace base { \ 173 namespace base { \
171 namespace mac { \ 174 namespace mac { \
172 NSMutable##name* CFToNSCast(CFMutable##name##Ref cf_val); \ 175 BASE_API NSMutable##name* CFToNSCast(CFMutable##name##Ref cf_val); \
173 CFMutable##name##Ref NSToCFCast(NSMutable##name* ns_val); \ 176 BASE_API CFMutable##name##Ref NSToCFCast(NSMutable##name* ns_val); \
174 } \ 177 } \
175 } \ 178 } \
176 179
177 // List of toll-free bridged types taken from: 180 // List of toll-free bridged types taken from:
178 // http://www.cocoadev.com/index.pl?TollFreeBridged 181 // http://www.cocoadev.com/index.pl?TollFreeBridged
179 182
180 CF_TO_NS_MUTABLE_CAST_DECL(Array); 183 CF_TO_NS_MUTABLE_CAST_DECL(Array);
181 CF_TO_NS_MUTABLE_CAST_DECL(AttributedString); 184 CF_TO_NS_MUTABLE_CAST_DECL(AttributedString);
182 CF_TO_NS_CAST_DECL(CFCalendar, NSCalendar); 185 CF_TO_NS_CAST_DECL(CFCalendar, NSCalendar);
183 CF_TO_NS_MUTABLE_CAST_DECL(CharacterSet); 186 CF_TO_NS_MUTABLE_CAST_DECL(CharacterSet);
184 CF_TO_NS_MUTABLE_CAST_DECL(Data); 187 CF_TO_NS_MUTABLE_CAST_DECL(Data);
185 CF_TO_NS_CAST_DECL(CFDate, NSDate); 188 CF_TO_NS_CAST_DECL(CFDate, NSDate);
186 CF_TO_NS_MUTABLE_CAST_DECL(Dictionary); 189 CF_TO_NS_MUTABLE_CAST_DECL(Dictionary);
187 CF_TO_NS_CAST_DECL(CFError, NSError); 190 CF_TO_NS_CAST_DECL(CFError, NSError);
188 CF_TO_NS_CAST_DECL(CFLocale, NSLocale); 191 CF_TO_NS_CAST_DECL(CFLocale, NSLocale);
189 CF_TO_NS_CAST_DECL(CFNumber, NSNumber); 192 CF_TO_NS_CAST_DECL(CFNumber, NSNumber);
190 CF_TO_NS_CAST_DECL(CFRunLoopTimer, NSTimer); 193 CF_TO_NS_CAST_DECL(CFRunLoopTimer, NSTimer);
191 CF_TO_NS_CAST_DECL(CFTimeZone, NSTimeZone); 194 CF_TO_NS_CAST_DECL(CFTimeZone, NSTimeZone);
192 CF_TO_NS_MUTABLE_CAST_DECL(Set); 195 CF_TO_NS_MUTABLE_CAST_DECL(Set);
193 CF_TO_NS_CAST_DECL(CFReadStream, NSInputStream); 196 CF_TO_NS_CAST_DECL(CFReadStream, NSInputStream);
194 CF_TO_NS_CAST_DECL(CFWriteStream, NSOutputStream); 197 CF_TO_NS_CAST_DECL(CFWriteStream, NSOutputStream);
195 CF_TO_NS_MUTABLE_CAST_DECL(String); 198 CF_TO_NS_MUTABLE_CAST_DECL(String);
196 CF_TO_NS_CAST_DECL(CFURL, NSURL); 199 CF_TO_NS_CAST_DECL(CFURL, NSURL);
197 200
198 // 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
199 // by using the NSToCFCast methods above. 202 // by using the NSToCFCast methods above.
200 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo"); 203 // e.g. LOG(INFO) << base::mac::NSToCFCast(@"foo");
201 // Operator << can not be overloaded for ObjectiveC types as the compiler 204 // Operator << can not be overloaded for ObjectiveC types as the compiler
202 // can not distinguish between overloads for id with overloads for void*. 205 // can not distinguish between overloads for id with overloads for void*.
203 extern std::ostream& operator<<(std::ostream& o, const CFErrorRef err); 206 BASE_API extern std::ostream& operator<<(std::ostream& o, const CFErrorRef err);
204 extern std::ostream& operator<<(std::ostream& o, const CFStringRef str); 207 BASE_API extern std::ostream& operator<<(std::ostream& o,
208 const CFStringRef str);
205 209
206 #endif // BASE_MAC_FOUNDATION_UTIL_H_ 210 #endif // BASE_MAC_FOUNDATION_UTIL_H_
OLDNEW
« no previous file with comments | « base/file_version_info.h ('k') | base/mac/mac_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698