OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/policy/core/common/policy_test_utils.h" | 5 #include "components/policy/core/common/policy_test_utils.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/strings/sys_string_conversions.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "components/policy/core/common/policy_bundle.h" | 16 #include "components/policy/core/common/policy_bundle.h" |
16 | 17 |
| 18 #if defined(OS_IOS) || defined(OS_MACOSX) |
| 19 #include <CoreFoundation/CoreFoundation.h> |
| 20 |
| 21 #include "base/mac/scoped_cftyperef.h" |
| 22 #endif |
| 23 |
17 namespace policy { | 24 namespace policy { |
18 | 25 |
19 PolicyDetailsMap::PolicyDetailsMap() {} | 26 PolicyDetailsMap::PolicyDetailsMap() {} |
20 | 27 |
21 PolicyDetailsMap::~PolicyDetailsMap() {} | 28 PolicyDetailsMap::~PolicyDetailsMap() {} |
22 | 29 |
23 GetChromePolicyDetailsCallback PolicyDetailsMap::GetCallback() const { | 30 GetChromePolicyDetailsCallback PolicyDetailsMap::GetCallback() const { |
24 return base::Bind(&PolicyDetailsMap::Lookup, base::Unretained(this)); | 31 return base::Bind(&PolicyDetailsMap::Lookup, base::Unretained(this)); |
25 } | 32 } |
26 | 33 |
(...skipping 12 matching lines...) Expand all Loading... |
39 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); | 46 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); |
40 if (!map.empty()) { | 47 if (!map.empty()) { |
41 base::DictionaryValue dict; | 48 base::DictionaryValue dict; |
42 for (PolicyMap::const_iterator it = map.begin(); it != map.end(); ++it) | 49 for (PolicyMap::const_iterator it = map.begin(); it != map.end(); ++it) |
43 dict.SetWithoutPathExpansion(it->first, it->second.value->DeepCopy()); | 50 dict.SetWithoutPathExpansion(it->first, it->second.value->DeepCopy()); |
44 LOG(WARNING) << "There are pre-existing policies in this machine: " << dict; | 51 LOG(WARNING) << "There are pre-existing policies in this machine: " << dict; |
45 } | 52 } |
46 return map.empty(); | 53 return map.empty(); |
47 } | 54 } |
48 | 55 |
| 56 #if defined(OS_IOS) || defined(OS_MACOSX) |
| 57 CFPropertyListRef ValueToProperty(const base::Value* value) { |
| 58 switch (value->GetType()) { |
| 59 case base::Value::TYPE_NULL: |
| 60 return kCFNull; |
| 61 |
| 62 case base::Value::TYPE_BOOLEAN: { |
| 63 bool bool_value; |
| 64 if (value->GetAsBoolean(&bool_value)) |
| 65 return bool_value ? kCFBooleanTrue : kCFBooleanFalse; |
| 66 break; |
| 67 } |
| 68 |
| 69 case base::Value::TYPE_INTEGER: { |
| 70 int int_value; |
| 71 if (value->GetAsInteger(&int_value)) { |
| 72 return CFNumberCreate( |
| 73 kCFAllocatorDefault, kCFNumberIntType, &int_value); |
| 74 } |
| 75 break; |
| 76 } |
| 77 |
| 78 case base::Value::TYPE_DOUBLE: { |
| 79 double double_value; |
| 80 if (value->GetAsDouble(&double_value)) { |
| 81 return CFNumberCreate( |
| 82 kCFAllocatorDefault, kCFNumberDoubleType, &double_value); |
| 83 } |
| 84 break; |
| 85 } |
| 86 |
| 87 case base::Value::TYPE_STRING: { |
| 88 std::string string_value; |
| 89 if (value->GetAsString(&string_value)) |
| 90 return base::SysUTF8ToCFStringRef(string_value); |
| 91 break; |
| 92 } |
| 93 |
| 94 case base::Value::TYPE_DICTIONARY: { |
| 95 const base::DictionaryValue* dict_value; |
| 96 if (value->GetAsDictionary(&dict_value)) { |
| 97 // |dict| is owned by the caller. |
| 98 CFMutableDictionaryRef dict = |
| 99 CFDictionaryCreateMutable(kCFAllocatorDefault, |
| 100 dict_value->size(), |
| 101 &kCFTypeDictionaryKeyCallBacks, |
| 102 &kCFTypeDictionaryValueCallBacks); |
| 103 for (base::DictionaryValue::Iterator iterator(*dict_value); |
| 104 !iterator.IsAtEnd(); iterator.Advance()) { |
| 105 // CFDictionaryAddValue() retains both |key| and |value|, so make sure |
| 106 // the references are balanced. |
| 107 base::ScopedCFTypeRef<CFStringRef> key( |
| 108 base::SysUTF8ToCFStringRef(iterator.key())); |
| 109 base::ScopedCFTypeRef<CFPropertyListRef> cf_value( |
| 110 ValueToProperty(&iterator.value())); |
| 111 if (cf_value) |
| 112 CFDictionaryAddValue(dict, key, cf_value); |
| 113 } |
| 114 return dict; |
| 115 } |
| 116 break; |
| 117 } |
| 118 |
| 119 case base::Value::TYPE_LIST: { |
| 120 const base::ListValue* list; |
| 121 if (value->GetAsList(&list)) { |
| 122 CFMutableArrayRef array = |
| 123 CFArrayCreateMutable(NULL, list->GetSize(), &kCFTypeArrayCallBacks); |
| 124 for (base::ListValue::const_iterator it(list->begin()); |
| 125 it != list->end(); ++it) { |
| 126 // CFArrayAppendValue() retains |value|, so make sure the reference |
| 127 // created by ValueToProperty() is released. |
| 128 base::ScopedCFTypeRef<CFPropertyListRef> cf_value( |
| 129 ValueToProperty(*it)); |
| 130 if (cf_value) |
| 131 CFArrayAppendValue(array, cf_value); |
| 132 } |
| 133 return array; |
| 134 } |
| 135 break; |
| 136 } |
| 137 |
| 138 case base::Value::TYPE_BINARY: |
| 139 // This type isn't converted (though it can be represented as CFData) |
| 140 // because there's no equivalent JSON type, and policy values can only |
| 141 // take valid JSON values. |
| 142 break; |
| 143 } |
| 144 |
| 145 return NULL; |
| 146 } |
| 147 #endif // defined(OS_IOS) || defined(OS_MACOSX) |
| 148 |
49 } // namespace policy | 149 } // namespace policy |
50 | 150 |
51 std::ostream& operator<<(std::ostream& os, | 151 std::ostream& operator<<(std::ostream& os, |
52 const policy::PolicyBundle& bundle) { | 152 const policy::PolicyBundle& bundle) { |
53 os << "{" << std::endl; | 153 os << "{" << std::endl; |
54 for (policy::PolicyBundle::const_iterator iter = bundle.begin(); | 154 for (policy::PolicyBundle::const_iterator iter = bundle.begin(); |
55 iter != bundle.end(); ++iter) { | 155 iter != bundle.end(); ++iter) { |
56 os << " \"" << iter->first << "\": " << *iter->second << "," << std::endl; | 156 os << " \"" << iter->first << "\": " << *iter->second << "," << std::endl; |
57 } | 157 } |
58 os << "}"; | 158 os << "}"; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 << " \"scope\": " << e.scope << "," << std::endl | 230 << " \"scope\": " << e.scope << "," << std::endl |
131 << " \"value\": " << value | 231 << " \"value\": " << value |
132 << "}"; | 232 << "}"; |
133 return os; | 233 return os; |
134 } | 234 } |
135 | 235 |
136 std::ostream& operator<<(std::ostream& os, const policy::PolicyNamespace& ns) { | 236 std::ostream& operator<<(std::ostream& os, const policy::PolicyNamespace& ns) { |
137 os << ns.domain << "/" << ns.component_id; | 237 os << ns.domain << "/" << ns.component_id; |
138 return os; | 238 return os; |
139 } | 239 } |
OLD | NEW |