This source file includes following definitions.
- DictionaryEntryToValue
- ArrayEntryToValue
- PropertyToValue
#include "components/policy/core/common/mac_util.h"
#include <string>
#include "base/mac/foundation_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/values.h"
using base::mac::CFCast;
namespace policy {
namespace {
void DictionaryEntryToValue(const void* key, const void* value, void* context) {
if (CFStringRef cf_key = CFCast<CFStringRef>(key)) {
scoped_ptr<base::Value> converted =
PropertyToValue(static_cast<CFPropertyListRef>(value));
if (converted) {
const std::string string = base::SysCFStringRefToUTF8(cf_key);
static_cast<base::DictionaryValue*>(context)->Set(
string, converted.release());
}
}
}
void ArrayEntryToValue(const void* value, void* context) {
scoped_ptr<base::Value> converted =
PropertyToValue(static_cast<CFPropertyListRef>(value));
if (converted)
static_cast<base::ListValue *>(context)->Append(converted.release());
}
}
scoped_ptr<base::Value> PropertyToValue(CFPropertyListRef property) {
if (CFCast<CFNullRef>(property))
return scoped_ptr<base::Value>(base::Value::CreateNullValue());
if (CFBooleanRef boolean = CFCast<CFBooleanRef>(property)) {
return scoped_ptr<base::Value>(
base::Value::CreateBooleanValue(CFBooleanGetValue(boolean)));
}
if (CFNumberRef number = CFCast<CFNumberRef>(property)) {
if (CFNumberIsFloatType(number)) {
double double_value = 0.0;
if (CFNumberGetValue(number, kCFNumberDoubleType, &double_value)) {
return scoped_ptr<base::Value>(
base::Value::CreateDoubleValue(double_value));
}
} else {
int int_value = 0;
if (CFNumberGetValue(number, kCFNumberIntType, &int_value)) {
return scoped_ptr<base::Value>(
base::Value::CreateIntegerValue(int_value));
}
}
}
if (CFStringRef string = CFCast<CFStringRef>(property)) {
return scoped_ptr<base::Value>(
base::Value::CreateStringValue(base::SysCFStringRefToUTF8(string)));
}
if (CFDictionaryRef dict = CFCast<CFDictionaryRef>(property)) {
scoped_ptr<base::DictionaryValue> dict_value(new base::DictionaryValue());
CFDictionaryApplyFunction(dict, DictionaryEntryToValue, dict_value.get());
return dict_value.PassAs<base::Value>();
}
if (CFArrayRef array = CFCast<CFArrayRef>(property)) {
scoped_ptr<base::ListValue> list_value(new base::ListValue());
CFArrayApplyFunction(array,
CFRangeMake(0, CFArrayGetCount(array)),
ArrayEntryToValue,
list_value.get());
return list_value.PassAs<base::Value>();
}
return scoped_ptr<base::Value>();
}
}