From a9944f08e4c9730993d012496a688039cf48936e Mon Sep 17 00:00:00 2001
From: "hayato@chromium.org"
 <hayato@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Thu, 10 Dec 2009 10:37:27 +0000
Subject: Separate ProxyResolverMac and ProxyConfigServiceMac into their own
 files and extract common utility functions into other files.

TEST=trybot and MacUtilTest in base_unittests
BUG=27310
Review URL: http://codereview.chromium.org/463028

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34243 0039d316-1c4b-4281-b951-d872f2087c98
---
 base/mac_util.h           |  6 ++++++
 base/mac_util.mm          | 24 ++++++++++++++++++++++++
 base/mac_util_unittest.mm | 17 +++++++++++++++++
 3 files changed, 47 insertions(+)

(limited to 'base')

diff --git a/base/mac_util.h b/base/mac_util.h
index ed49e2f..7b697c9 100644
--- a/base/mac_util.h
+++ b/base/mac_util.h
@@ -92,6 +92,12 @@ FilePath GetAppBundlePath(const FilePath& exec_name);
 // Set the Time Machine exclusion property for the given file.
 bool SetFileBackupExclusion(const FilePath& file_path, bool exclude);
 
+// 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.
+CFTypeRef GetValueFromDictionary(CFDictionaryRef dict,
+                                 CFStringRef key,
+                                 CFTypeID expected_type);
+
 }  // namespace mac_util
 
 #endif  // BASE_MAC_UTIL_H_
diff --git a/base/mac_util.mm b/base/mac_util.mm
index 1418453..f585233 100644
--- a/base/mac_util.mm
+++ b/base/mac_util.mm
@@ -254,5 +254,29 @@ bool SetFileBackupExclusion(const FilePath& file_path, bool exclude) {
   return success;
 }
 
+CFTypeRef GetValueFromDictionary(CFDictionaryRef dict,
+                                 CFStringRef key,
+                                 CFTypeID expected_type) {
+  CFTypeRef value = CFDictionaryGetValue(dict, key);
+  if (!value)
+    return value;
+
+  if (CFGetTypeID(value) != expected_type) {
+    scoped_cftyperef<CFStringRef> expected_type_ref(
+        CFCopyTypeIDDescription(expected_type));
+    scoped_cftyperef<CFStringRef> actual_type_ref(
+        CFCopyTypeIDDescription(CFGetTypeID(value)));
+    LOG(WARNING) << "Expected value for key "
+                 << base::SysCFStringRefToUTF8(key)
+                 << " to be "
+                 << base::SysCFStringRefToUTF8(expected_type_ref)
+                 << " but it was "
+                 << base::SysCFStringRefToUTF8(actual_type_ref)
+                 << " instead";
+    return NULL;
+  }
+
+  return value;
+}
 
 }  // namespace mac_util
diff --git a/base/mac_util_unittest.mm b/base/mac_util_unittest.mm
index 5e97356..9e56358 100644
--- a/base/mac_util_unittest.mm
+++ b/base/mac_util_unittest.mm
@@ -9,6 +9,7 @@
 
 #include "base/file_path.h"
 #include "base/file_util.h"
+#include "base/scoped_cftyperef.h"
 #include "base/scoped_nsobject.h"
 #include "base/scoped_ptr.h"
 #include "testing/gtest/include/gtest/gtest.h"
@@ -125,3 +126,19 @@ TEST_F(MacUtilTest, TestExcludeFileFromBackups) {
   EXPECT_TRUE(mac_util::SetFileBackupExclusion(file_path, false));
   EXPECT_FALSE(CSBackupIsItemExcluded((CFURLRef)fileURL, &excludeByPath));
 }
+
+TEST_F(MacUtilTest, TestGetValueFromDictionary) {
+  scoped_cftyperef<CFMutableDictionaryRef> dict(
+      CFDictionaryCreateMutable(0, 0,
+                                &kCFTypeDictionaryKeyCallBacks,
+                                &kCFTypeDictionaryValueCallBacks));
+  CFDictionarySetValue(dict.get(), CFSTR("key"), CFSTR("value"));
+
+  EXPECT_TRUE(CFEqual(CFSTR("value"),
+                      mac_util::GetValueFromDictionary(
+                          dict, CFSTR("key"), CFStringGetTypeID())));
+  EXPECT_FALSE(mac_util::GetValueFromDictionary(
+                   dict, CFSTR("key"), CFNumberGetTypeID()));
+  EXPECT_FALSE(mac_util::GetValueFromDictionary(
+                   dict, CFSTR("no-exist"), CFStringGetTypeID()));
+}
-- 
cgit v1.1