Update V8 to version 4.6.62.
[chromium-blink-merge.git] / components / policy / core / common / policy_test_utils.cc
blobe9116b768769cb49e4e449367646d2239ce72881
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
3 // found in the LICENSE file.
5 #include "components/policy/core/common/policy_test_utils.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback.h"
12 #include "base/json/json_writer.h"
13 #include "base/logging.h"
14 #include "base/strings/sys_string_conversions.h"
15 #include "base/values.h"
16 #include "components/policy/core/common/policy_bundle.h"
18 #if defined(OS_IOS) || defined(OS_MACOSX)
19 #include <CoreFoundation/CoreFoundation.h>
21 #include "base/mac/scoped_cftyperef.h"
22 #endif
24 namespace policy {
26 PolicyDetailsMap::PolicyDetailsMap() {}
28 PolicyDetailsMap::~PolicyDetailsMap() {}
30 GetChromePolicyDetailsCallback PolicyDetailsMap::GetCallback() const {
31 return base::Bind(&PolicyDetailsMap::Lookup, base::Unretained(this));
34 void PolicyDetailsMap::SetDetails(const std::string& policy,
35 const PolicyDetails* details) {
36 map_[policy] = details;
39 const PolicyDetails* PolicyDetailsMap::Lookup(const std::string& policy) const {
40 PolicyDetailsMapping::const_iterator it = map_.find(policy);
41 return it == map_.end() ? NULL : it->second;
44 bool PolicyServiceIsEmpty(const PolicyService* service) {
45 const PolicyMap& map = service->GetPolicies(
46 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
47 if (!map.empty()) {
48 base::DictionaryValue dict;
49 for (PolicyMap::const_iterator it = map.begin(); it != map.end(); ++it)
50 dict.SetWithoutPathExpansion(it->first, it->second.value->DeepCopy());
51 LOG(WARNING) << "There are pre-existing policies in this machine: " << dict;
53 return map.empty();
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;
62 case base::Value::TYPE_BOOLEAN: {
63 bool bool_value;
64 if (value->GetAsBoolean(&bool_value))
65 return bool_value ? kCFBooleanTrue : kCFBooleanFalse;
66 break;
69 case base::Value::TYPE_INTEGER: {
70 int int_value;
71 if (value->GetAsInteger(&int_value)) {
72 return CFNumberCreate(
73 kCFAllocatorDefault, kCFNumberIntType, &int_value);
75 break;
78 case base::Value::TYPE_DOUBLE: {
79 double double_value;
80 if (value->GetAsDouble(&double_value)) {
81 return CFNumberCreate(
82 kCFAllocatorDefault, kCFNumberDoubleType, &double_value);
84 break;
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;
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);
114 return dict;
116 break;
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);
133 return array;
135 break;
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;
145 return NULL;
147 #endif // defined(OS_IOS) || defined(OS_MACOSX)
149 } // namespace policy
151 std::ostream& operator<<(std::ostream& os,
152 const policy::PolicyBundle& bundle) {
153 os << "{" << std::endl;
154 for (policy::PolicyBundle::const_iterator iter = bundle.begin();
155 iter != bundle.end(); ++iter) {
156 os << " \"" << iter->first << "\": " << *iter->second << "," << std::endl;
158 os << "}";
159 return os;
162 std::ostream& operator<<(std::ostream& os, policy::PolicyScope scope) {
163 switch (scope) {
164 case policy::POLICY_SCOPE_USER: {
165 os << "POLICY_SCOPE_USER";
166 break;
168 case policy::POLICY_SCOPE_MACHINE: {
169 os << "POLICY_SCOPE_MACHINE";
170 break;
172 default: {
173 os << "POLICY_SCOPE_UNKNOWN(" << int(scope) << ")";
176 return os;
179 std::ostream& operator<<(std::ostream& os, policy::PolicyLevel level) {
180 switch (level) {
181 case policy::POLICY_LEVEL_RECOMMENDED: {
182 os << "POLICY_LEVEL_RECOMMENDED";
183 break;
185 case policy::POLICY_LEVEL_MANDATORY: {
186 os << "POLICY_LEVEL_MANDATORY";
187 break;
189 default: {
190 os << "POLICY_LEVEL_UNKNOWN(" << int(level) << ")";
193 return os;
196 std::ostream& operator<<(std::ostream& os, policy::PolicyDomain domain) {
197 switch (domain) {
198 case policy::POLICY_DOMAIN_CHROME: {
199 os << "POLICY_DOMAIN_CHROME";
200 break;
202 case policy::POLICY_DOMAIN_EXTENSIONS: {
203 os << "POLICY_DOMAIN_EXTENSIONS";
204 break;
206 default: {
207 os << "POLICY_DOMAIN_UNKNOWN(" << int(domain) << ")";
210 return os;
213 std::ostream& operator<<(std::ostream& os, const policy::PolicyMap& policies) {
214 os << "{" << std::endl;
215 for (policy::PolicyMap::const_iterator iter = policies.begin();
216 iter != policies.end(); ++iter) {
217 os << " \"" << iter->first << "\": " << iter->second << "," << std::endl;
219 os << "}";
220 return os;
223 std::ostream& operator<<(std::ostream& os, const policy::PolicyMap::Entry& e) {
224 std::string value;
225 base::JSONWriter::WriteWithOptions(
226 *e.value, base::JSONWriter::OPTIONS_PRETTY_PRINT, &value);
227 os << "{" << std::endl
228 << " \"level\": " << e.level << "," << std::endl
229 << " \"scope\": " << e.scope << "," << std::endl
230 << " \"value\": " << value
231 << "}";
232 return os;
235 std::ostream& operator<<(std::ostream& os, const policy::PolicyNamespace& ns) {
236 os << ns.domain << "/" << ns.component_id;
237 return os;