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

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

Issue 8540021: Create a nicer interface for base::mac::GetValueFromDictionary (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: undefs, alignment, ScopedCFTypeRefs Created 9 years, 1 month 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
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 #include "base/mac/foundation_util.h" 5 #include "base/mac/foundation_util.h"
6 6
7 #include <stdlib.h> 7 #include <stdlib.h>
8 #include <string.h> 8 #include <string.h>
9 9
10 #include "base/file_path.h" 10 #include "base/file_path.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/mac/scoped_cftyperef.h"
13 #include "base/sys_string_conversions.h" 12 #include "base/sys_string_conversions.h"
14 13
15 namespace base { 14 namespace base {
16 namespace mac { 15 namespace mac {
17 16
18 static bool g_override_am_i_bundled = false; 17 static bool g_override_am_i_bundled = false;
19 static bool g_override_am_i_bundled_value = false; 18 static bool g_override_am_i_bundled_value = false;
20 19
21 // Adapted from http://developer.apple.com/carbon/tipsandtricks.html#AmIBundled 20 // Adapted from http://developer.apple.com/carbon/tipsandtricks.html#AmIBundled
22 static bool UncachedAmIBundled() { 21 static bool UncachedAmIBundled() {
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 !it->compare(it->length() - kExtLength, kExtLength, kExt, kExtLength)) 192 !it->compare(it->length() - kExtLength, kExtLength, kExt, kExtLength))
194 return FilePath(bundle_name); 193 return FilePath(bundle_name);
195 194
196 // Separate this component from the next one. 195 // Separate this component from the next one.
197 bundle_name += '/'; 196 bundle_name += '/';
198 } 197 }
199 198
200 return FilePath(); 199 return FilePath();
201 } 200 }
202 201
202 #define TYPE_NAME_FOR_CF_TYPE_DEFN(TypeCF) \
203 std::string TypeNameForCFType(TypeCF##Ref) { \
204 return #TypeCF; \
205 }
206
207 TYPE_NAME_FOR_CF_TYPE_DEFN(CFArray);
208 TYPE_NAME_FOR_CF_TYPE_DEFN(CFBag);
209 TYPE_NAME_FOR_CF_TYPE_DEFN(CFBoolean);
210 TYPE_NAME_FOR_CF_TYPE_DEFN(CFData);
211 TYPE_NAME_FOR_CF_TYPE_DEFN(CFDate);
212 TYPE_NAME_FOR_CF_TYPE_DEFN(CFDictionary);
213 TYPE_NAME_FOR_CF_TYPE_DEFN(CFNull);
214 TYPE_NAME_FOR_CF_TYPE_DEFN(CFNumber);
215 TYPE_NAME_FOR_CF_TYPE_DEFN(CFSet);
216 TYPE_NAME_FOR_CF_TYPE_DEFN(CFString);
217
218 #undef TYPE_NAME_FOR_CF_TYPE_DEFN
219
220 std::string GetValueFromDictionaryErrorMessage(
221 CFStringRef key, const std::string& expected_type, CFTypeRef value) {
222 ScopedCFTypeRef<CFStringRef> actual_type_ref(
223 CFCopyTypeIDDescription(CFGetTypeID(value)));
224 return "Expected value for key " +
225 base::SysCFStringRefToUTF8(key) +
226 " to be " +
227 expected_type +
228 " but it was " +
229 base::SysCFStringRefToUTF8(actual_type_ref) +
230 " instead";
231 }
232
203 CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, 233 CFTypeRef GetValueFromDictionary(CFDictionaryRef dict,
204 CFStringRef key, 234 CFStringRef key,
205 CFTypeID expected_type) { 235 CFTypeID expected_type) {
206 CFTypeRef value = CFDictionaryGetValue(dict, key); 236 CFTypeRef value = CFDictionaryGetValue(dict, key);
207 if (!value) 237 if (!value)
208 return value; 238 return value;
209 239
210 if (CFGetTypeID(value) != expected_type) { 240 if (CFGetTypeID(value) != expected_type) {
211 ScopedCFTypeRef<CFStringRef> expected_type_ref( 241 std::string expected_type_name = base::SysCFStringRefToUTF8(
212 CFCopyTypeIDDescription(expected_type)); 242 CFCopyTypeIDDescription(expected_type));
213 ScopedCFTypeRef<CFStringRef> actual_type_ref( 243 DLOG(WARNING) << GetValueFromDictionaryErrorMessage(key,
214 CFCopyTypeIDDescription(CFGetTypeID(value))); 244 expected_type_name,
215 DLOG(WARNING) << "Expected value for key " 245 value);
216 << base::SysCFStringRefToUTF8(key)
217 << " to be "
218 << base::SysCFStringRefToUTF8(expected_type_ref)
219 << " but it was "
220 << base::SysCFStringRefToUTF8(actual_type_ref)
221 << " instead";
222 return NULL; 246 return NULL;
223 } 247 }
224 248
225 return value; 249 return value;
226 } 250 }
227 251
228 void NSObjectRetain(void* obj) { 252 void NSObjectRetain(void* obj) {
229 id<NSObject> nsobj = static_cast<id<NSObject> >(obj); 253 id<NSObject> nsobj = static_cast<id<NSObject> >(obj);
230 [nsobj retain]; 254 [nsobj retain];
231 } 255 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 CF_TO_NS_CAST_DEFN(CFLocale, NSLocale); 335 CF_TO_NS_CAST_DEFN(CFLocale, NSLocale);
312 CF_TO_NS_CAST_DEFN(CFNumber, NSNumber); 336 CF_TO_NS_CAST_DEFN(CFNumber, NSNumber);
313 CF_TO_NS_CAST_DEFN(CFRunLoopTimer, NSTimer); 337 CF_TO_NS_CAST_DEFN(CFRunLoopTimer, NSTimer);
314 CF_TO_NS_CAST_DEFN(CFTimeZone, NSTimeZone); 338 CF_TO_NS_CAST_DEFN(CFTimeZone, NSTimeZone);
315 CF_TO_NS_MUTABLE_CAST_DEFN(Set); 339 CF_TO_NS_MUTABLE_CAST_DEFN(Set);
316 CF_TO_NS_CAST_DEFN(CFReadStream, NSInputStream); 340 CF_TO_NS_CAST_DEFN(CFReadStream, NSInputStream);
317 CF_TO_NS_CAST_DEFN(CFWriteStream, NSOutputStream); 341 CF_TO_NS_CAST_DEFN(CFWriteStream, NSOutputStream);
318 CF_TO_NS_MUTABLE_CAST_DEFN(String); 342 CF_TO_NS_MUTABLE_CAST_DEFN(String);
319 CF_TO_NS_CAST_DEFN(CFURL, NSURL); 343 CF_TO_NS_CAST_DEFN(CFURL, NSURL);
320 344
345 #undef CF_TO_NS_CAST_DEFN
346 #undef CF_TO_NS_MUTABLE_CAST_DEFN
347
321 #define CF_CAST_DEFN(TypeCF) \ 348 #define CF_CAST_DEFN(TypeCF) \
322 template<> TypeCF##Ref \ 349 template<> TypeCF##Ref \
323 CFCast<TypeCF##Ref>(const CFTypeRef& cf_val) { \ 350 CFCast<TypeCF##Ref>(const CFTypeRef& cf_val) { \
324 if (cf_val == NULL) { \ 351 if (cf_val == NULL) { \
325 return NULL; \ 352 return NULL; \
326 } \ 353 } \
327 if (CFGetTypeID(cf_val) == TypeCF##GetTypeID()) { \ 354 if (CFGetTypeID(cf_val) == TypeCF##GetTypeID()) { \
328 return reinterpret_cast<TypeCF##Ref>(cf_val); \ 355 return reinterpret_cast<TypeCF##Ref>(cf_val); \
329 } \ 356 } \
330 return NULL; \ 357 return NULL; \
(...skipping 10 matching lines...) Expand all
341 CF_CAST_DEFN(CFBag); 368 CF_CAST_DEFN(CFBag);
342 CF_CAST_DEFN(CFBoolean); 369 CF_CAST_DEFN(CFBoolean);
343 CF_CAST_DEFN(CFData); 370 CF_CAST_DEFN(CFData);
344 CF_CAST_DEFN(CFDate); 371 CF_CAST_DEFN(CFDate);
345 CF_CAST_DEFN(CFDictionary); 372 CF_CAST_DEFN(CFDictionary);
346 CF_CAST_DEFN(CFNull); 373 CF_CAST_DEFN(CFNull);
347 CF_CAST_DEFN(CFNumber); 374 CF_CAST_DEFN(CFNumber);
348 CF_CAST_DEFN(CFSet); 375 CF_CAST_DEFN(CFSet);
349 CF_CAST_DEFN(CFString); 376 CF_CAST_DEFN(CFString);
350 377
378 #undef CF_CAST_DEFN
379
351 } // namespace mac 380 } // namespace mac
352 } // namespace base 381 } // namespace base
353 382
354 std::ostream& operator<<(std::ostream& o, const CFStringRef string) { 383 std::ostream& operator<<(std::ostream& o, const CFStringRef string) {
355 return o << base::SysCFStringRefToUTF8(string); 384 return o << base::SysCFStringRefToUTF8(string);
356 } 385 }
357 386
358 std::ostream& operator<<(std::ostream& o, const CFErrorRef err) { 387 std::ostream& operator<<(std::ostream& o, const CFErrorRef err) {
359 base::mac::ScopedCFTypeRef<CFStringRef> desc(CFErrorCopyDescription(err)); 388 base::mac::ScopedCFTypeRef<CFStringRef> desc(CFErrorCopyDescription(err));
360 base::mac::ScopedCFTypeRef<CFDictionaryRef> user_info( 389 base::mac::ScopedCFTypeRef<CFDictionaryRef> user_info(
361 CFErrorCopyUserInfo(err)); 390 CFErrorCopyUserInfo(err));
362 CFStringRef errorDesc = NULL; 391 CFStringRef errorDesc = NULL;
363 if (user_info.get()) { 392 if (user_info.get()) {
364 errorDesc = reinterpret_cast<CFStringRef>( 393 errorDesc = reinterpret_cast<CFStringRef>(
365 CFDictionaryGetValue(user_info.get(), kCFErrorDescriptionKey)); 394 CFDictionaryGetValue(user_info.get(), kCFErrorDescriptionKey));
366 } 395 }
367 o << "Code: " << CFErrorGetCode(err) 396 o << "Code: " << CFErrorGetCode(err)
368 << " Domain: " << CFErrorGetDomain(err) 397 << " Domain: " << CFErrorGetDomain(err)
369 << " Desc: " << desc.get(); 398 << " Desc: " << desc.get();
370 if(errorDesc) { 399 if(errorDesc) {
371 o << "(" << errorDesc << ")"; 400 o << "(" << errorDesc << ")";
372 } 401 }
373 return o; 402 return o;
374 } 403 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698