Skip to content

Commit

Permalink
Use the new base::mac::GetValueFromDictionary<>() method.
Browse files Browse the repository at this point in the history
Remove all cases of the old method signature (and related tests) and
use the new form.

BUG=104200


Review URL: http://codereview.chromium.org/8769016

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@112553 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kushi.p@gmail.com committed Dec 1, 2011
1 parent 3276041 commit f54596d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 72 deletions.
27 changes: 8 additions & 19 deletions base/mac/foundation_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,6 @@ 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);

// Retain/release calls for memory management in C++.
BASE_EXPORT void NSObjectRetain(void* obj);
BASE_EXPORT void NSObjectRelease(void* obj);
Expand Down Expand Up @@ -243,10 +229,8 @@ namespace mac {
// CFNumberRef some_number = base::mac::CFCast<CFNumberRef>(
// CFArrayGetValueAtIndex(array, index));
//
// CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>(
// base::mac::GetValueFromDictionary(some_dict,
// CFSTR("a_key"),
// CFStringGetTypeID()));
// CFTypeRef hello = CFSTR("hello world");
// CFStringRef some_string = base::mac::CFCastStrict<CFStringRef>(hello);
BASE_EXPORT template<typename T>
T CFCast(const CFTypeRef& cf_val);

Expand Down Expand Up @@ -294,8 +278,13 @@ T* ObjCCastStrict(id objc_val) {

#endif // defined(__OBJC__)

// 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.
// 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);
Expand Down
47 changes: 13 additions & 34 deletions base/mac/foundation_util.mm
Original file line number Diff line number Diff line change
Expand Up @@ -217,40 +217,6 @@ FilePath GetAppBundlePath(const FilePath& exec_name) {

#undef TYPE_NAME_FOR_CF_TYPE_DEFN

std::string GetValueFromDictionaryErrorMessage(
CFStringRef key, const std::string& expected_type, CFTypeRef value) {
ScopedCFTypeRef<CFStringRef> actual_type_ref(
CFCopyTypeIDDescription(CFGetTypeID(value)));
return "Expected value for key " +
base::SysCFStringRefToUTF8(key) +
" to be " +
expected_type +
" but it was " +
base::SysCFStringRefToUTF8(actual_type_ref) +
" instead";
}

CFTypeRef GetValueFromDictionary(CFDictionaryRef dict,
CFStringRef key,
CFTypeID expected_type) {
CFTypeRef value = CFDictionaryGetValue(dict, key);
if (!value)
return value;

if (CFGetTypeID(value) != expected_type) {
ScopedCFTypeRef<CFStringRef> expected_type_name(
CFCopyTypeIDDescription(expected_type));
std::string expected_type_utf8 =
base::SysCFStringRefToUTF8(expected_type_name);
DLOG(WARNING) << GetValueFromDictionaryErrorMessage(key,
expected_type_utf8,
value);
return NULL;
}

return value;
}

void NSObjectRetain(void* obj) {
id<NSObject> nsobj = static_cast<id<NSObject> >(obj);
[nsobj retain];
Expand Down Expand Up @@ -379,6 +345,19 @@ void SetBaseBundleID(const char* new_base_bundle_id) {

#undef CF_CAST_DEFN

std::string GetValueFromDictionaryErrorMessage(
CFStringRef key, const std::string& expected_type, CFTypeRef value) {
ScopedCFTypeRef<CFStringRef> actual_type_ref(
CFCopyTypeIDDescription(CFGetTypeID(value)));
return "Expected value for key " +
base::SysCFStringRefToUTF8(key) +
" to be " +
expected_type +
" but it was " +
base::SysCFStringRefToUTF8(actual_type_ref) +
" instead";
}

} // namespace mac
} // namespace base

Expand Down
15 changes: 0 additions & 15 deletions base/mac/mac_util_unittest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,6 @@
EXPECT_FALSE(excluded_by_path);
}

TEST_F(MacUtilTest, TestGetValueFromDictionary) {
ScopedCFTypeRef<CFMutableDictionaryRef> dict(
CFDictionaryCreateMutable(0, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks));
CFDictionarySetValue(dict.get(), CFSTR("key"), CFSTR("value"));

EXPECT_TRUE(CFEqual(CFSTR("value"),
GetValueFromDictionary(
dict, CFSTR("key"), CFStringGetTypeID())));
EXPECT_FALSE(GetValueFromDictionary(dict, CFSTR("key"), CFNumberGetTypeID()));
EXPECT_FALSE(GetValueFromDictionary(
dict, CFSTR("no-exist"), CFStringGetTypeID()));
}

TEST_F(MacUtilTest, CopyNSImageToCGImage) {
scoped_nsobject<NSImage> nsImage(
[[NSImage alloc] initWithSize:NSMakeSize(20, 20)]);
Expand Down
9 changes: 5 additions & 4 deletions chrome/browser/ui/cocoa/tabs/tab_strip_drag_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -538,15 +538,16 @@ - (int)getWorkspaceID:(NSWindow*)window useCache:(BOOL)useCache {
DCHECK(CFGetTypeID(dict) == CFDictionaryGetTypeID());

// Sanity check the ID.
CFNumberRef otherIDRef = (CFNumberRef)base::mac::GetValueFromDictionary(
dict, kCGWindowNumber, CFNumberGetTypeID());
CFNumberRef otherIDRef =
base::mac::GetValueFromDictionary<CFNumberRef>(dict, kCGWindowNumber);
CGWindowID otherID;
if (otherIDRef &&
CFNumberGetValue(otherIDRef, kCGWindowIDCFNumberType, &otherID) &&
otherID == windowID) {
// And then get the workspace.
CFNumberRef workspaceRef = (CFNumberRef)base::mac::GetValueFromDictionary(
dict, kCGWindowWorkspace, CFNumberGetTypeID());
CFNumberRef workspaceRef =
base::mac::GetValueFromDictionary<CFNumberRef>(dict,
kCGWindowWorkspace);
if (!workspaceRef ||
!CFNumberGetValue(workspaceRef, kCFNumberIntType, &workspace)) {
workspace = -1;
Expand Down

0 comments on commit f54596d

Please sign in to comment.