Index: base/mac/foundation_util.h |
diff --git a/base/mac/foundation_util.h b/base/mac/foundation_util.h |
index f7821de08cdb09bca58aaca59da82bd689407e29..a36ad755231c2927e94395b1a139b1a0c0de5b03 100644 |
--- a/base/mac/foundation_util.h |
+++ b/base/mac/foundation_util.h |
@@ -13,6 +13,7 @@ |
#include "base/base_export.h" |
#include "base/logging.h" |
+#include "base/mac/scoped_cftyperef.h" |
#if defined(__OBJC__) |
#import <Foundation/Foundation.h> |
@@ -96,8 +97,32 @@ BASE_EXPORT FilePath GetUserLibraryPath(); |
// returns - path to the application bundle, or empty on error |
BASE_EXPORT FilePath GetAppBundlePath(const FilePath& exec_name); |
+#define TYPE_NAME_FOR_CF_TYPE_DECL(TypeCF) \ |
+std::string TypeNameForCFType(TypeCF##Ref); |
+ |
+TYPE_NAME_FOR_CF_TYPE_DECL(CFArray); |
+TYPE_NAME_FOR_CF_TYPE_DECL(CFBag); |
+TYPE_NAME_FOR_CF_TYPE_DECL(CFBoolean); |
+TYPE_NAME_FOR_CF_TYPE_DECL(CFData); |
+TYPE_NAME_FOR_CF_TYPE_DECL(CFDate); |
+TYPE_NAME_FOR_CF_TYPE_DECL(CFDictionary); |
+TYPE_NAME_FOR_CF_TYPE_DECL(CFNull); |
+TYPE_NAME_FOR_CF_TYPE_DECL(CFNumber); |
+TYPE_NAME_FOR_CF_TYPE_DECL(CFSet); |
+TYPE_NAME_FOR_CF_TYPE_DECL(CFString); |
+ |
+#undef TYPE_NAME_FOR_CF_TYPE_DECL |
+ |
+// Helper function for GetValueFromDictionary to create the error message |
+// that appears when a type mismatch is encountered. |
+std::string GetValueFromDictionaryErrorMessage( |
+ CFStringRef key, const std::string& expected_type, CFTypeRef value); |
+ |
// Utility function to pull out a value from a dictionary, check its type, and |
// return it. Returns NULL if the key is not present or of the wrong type. |
+// This is now deprecated in favor of the two-argument form below. |
+// TODO(kushi.p): Remove this function once all cases of it have been |
+// replaced with the two-argument form below. See: crbug.com/104200. |
BASE_EXPORT CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, |
CFStringRef key, |
CFTypeID expected_type); |
@@ -198,6 +223,10 @@ CF_TO_NS_CAST_DECL(CFWriteStream, NSOutputStream); |
CF_TO_NS_MUTABLE_CAST_DECL(String); |
CF_TO_NS_CAST_DECL(CFURL, NSURL); |
+#undef CF_TO_NS_CAST_DECL |
+#undef CF_TO_NS_MUTABLE_CAST_DECL |
+#undef OBJC_CPP_CLASS_DECL |
+ |
namespace base { |
namespace mac { |
@@ -218,10 +247,10 @@ namespace mac { |
// base::mac::GetValueFromDictionary(some_dict, |
// CFSTR("a_key"), |
// CFStringGetTypeID())); |
-BASE_EXPORT template<class T> |
+BASE_EXPORT template<typename T> |
T CFCast(const CFTypeRef& cf_val); |
-BASE_EXPORT template<class T> |
+BASE_EXPORT template<typename T> |
T CFCastStrict(const CFTypeRef& cf_val); |
#if defined(__OBJC__) |
@@ -248,7 +277,7 @@ T CFCastStrict(const CFTypeRef& cf_val); |
// |
// NSString* str = base::mac::ObjCCastStrict<NSString>( |
// [ns_arr_of_ns_strs objectAtIndex:0]); |
-BASE_EXPORT template<class T> |
+BASE_EXPORT template<typename T> |
T* ObjCCast(id objc_val) { |
if ([objc_val isKindOfClass:[T class]]) { |
return reinterpret_cast<T*>(objc_val); |
@@ -256,7 +285,7 @@ T* ObjCCast(id objc_val) { |
return nil; |
} |
-BASE_EXPORT template<class T> |
+BASE_EXPORT template<typename T> |
T* ObjCCastStrict(id objc_val) { |
T* rv = ObjCCast<T>(objc_val); |
DCHECK(objc_val == nil || rv); |
@@ -265,6 +294,23 @@ T* ObjCCastStrict(id objc_val) { |
#endif // defined(__OBJC__) |
+// Utility function to pull out a value from a dictionary, check its type, and |
+// return it. Returns NULL if the key is not present or of the wrong type. |
+BASE_EXPORT template<typename T> |
+T GetValueFromDictionary(CFDictionaryRef dict, CFStringRef key) { |
+ CFTypeRef value = CFDictionaryGetValue(dict, key); |
+ T value_specific = CFCast<T>(value); |
+ |
+ if (value && !value_specific) { |
+ std::string expected_type = TypeNameForCFType(value_specific); |
+ DLOG(WARNING) << GetValueFromDictionaryErrorMessage(key, |
+ expected_type, |
+ value); |
+ } |
+ |
+ return value_specific; |
+} |
+ |
} // namespace mac |
} // namespace base |