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 "chrome/browser/policy/registry_dict_win.h"
7 #include "base/json/json_reader.h"
8 #include "base/stl_util.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/sys_byteorder.h"
13 #include "base/values.h"
14 #include "base/win/registry.h"
15 #include "components/json_schema/json_schema_constants.h"
17 namespace schema = json_schema_constants;
19 using base::win::RegistryKeyIterator;
20 using base::win::RegistryValueIterator;
26 // Returns the entry with key |name| in |dictionary| (can be NULL), or NULL.
27 const base::DictionaryValue* GetEntry(const base::DictionaryValue* dictionary,
28 const std::string& name) {
31 const base::DictionaryValue* entry = NULL;
32 dictionary->GetDictionaryWithoutPathExpansion(name, &entry);
36 // Returns the Value type described in |schema|, or |default_type| if not found.
37 base::Value::Type GetValueTypeForSchema(const base::DictionaryValue* schema,
38 base::Value::Type default_type) {
39 // JSON-schema types to base::Value::Type mapping.
42 const char* schema_type;
43 // Correspondent value type.
44 base::Value::Type value_type;
45 } kSchemaToValueTypeMap[] = {
46 { schema::kArray, base::Value::TYPE_LIST },
47 { schema::kBoolean, base::Value::TYPE_BOOLEAN },
48 { schema::kInteger, base::Value::TYPE_INTEGER },
49 { schema::kNull, base::Value::TYPE_NULL },
50 { schema::kNumber, base::Value::TYPE_DOUBLE },
51 { schema::kObject, base::Value::TYPE_DICTIONARY },
52 { schema::kString, base::Value::TYPE_STRING },
58 if (!schema->GetStringWithoutPathExpansion(schema::kType, &type))
60 for (size_t i = 0; i < arraysize(kSchemaToValueTypeMap); ++i) {
61 if (type == kSchemaToValueTypeMap[i].schema_type)
62 return kSchemaToValueTypeMap[i].value_type;
67 // Returns the schema for property |name| given the |schema| of an object.
68 // Returns the "additionalProperties" schema if no specific schema for
69 // |name| is present. Returns NULL if no schema is found.
70 const base::DictionaryValue* GetSchemaFor(const base::DictionaryValue* schema,
71 const std::string& name) {
72 const base::DictionaryValue* properties =
73 GetEntry(schema, schema::kProperties);
74 const base::DictionaryValue* sub_schema = GetEntry(properties, name);
77 // "additionalProperties" can be a boolean, but that case is ignored.
78 return GetEntry(schema, schema::kAdditionalProperties);
81 // Converts a value (as read from the registry) to meet |schema|, converting
82 // types as necessary. Unconvertible types will show up as NULL values in the
84 scoped_ptr<base::Value> ConvertValue(const base::Value& value,
85 const base::DictionaryValue* schema) {
86 // Figure out the type to convert to from the schema.
87 const base::Value::Type result_type(
88 GetValueTypeForSchema(schema, value.GetType()));
90 // If the type is good already, go with it.
91 if (value.IsType(result_type)) {
92 // Recurse for complex types if there is a schema.
94 const base::DictionaryValue* dict = NULL;
95 const base::ListValue* list = NULL;
96 if (value.GetAsDictionary(&dict)) {
97 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
98 for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd();
100 scoped_ptr<base::Value> converted_value(
101 ConvertValue(entry.value(), GetSchemaFor(schema, entry.key())));
102 result->SetWithoutPathExpansion(entry.key(),
103 converted_value.release());
105 return result.Pass();
106 } else if (value.GetAsList(&list)) {
107 scoped_ptr<base::ListValue> result(new base::ListValue());
108 const base::DictionaryValue* item_schema =
109 GetEntry(schema, schema::kItems);
110 for (base::ListValue::const_iterator entry(list->begin());
111 entry != list->end(); ++entry) {
112 result->Append(ConvertValue(**entry, item_schema).release());
114 return result.Pass();
117 return make_scoped_ptr(value.DeepCopy());
120 // Else, do some conversions to map windows registry data types to JSON types.
121 std::string string_value;
123 switch (result_type) {
124 case base::Value::TYPE_NULL: {
125 return make_scoped_ptr(base::Value::CreateNullValue());
127 case base::Value::TYPE_BOOLEAN: {
128 // Accept booleans encoded as either string or integer.
129 if (value.GetAsInteger(&int_value) ||
130 (value.GetAsString(&string_value) &&
131 base::StringToInt(string_value, &int_value))) {
132 return make_scoped_ptr(Value::CreateBooleanValue(int_value != 0));
136 case base::Value::TYPE_INTEGER: {
137 // Integers may be string-encoded.
138 if (value.GetAsString(&string_value) &&
139 base::StringToInt(string_value, &int_value)) {
140 return make_scoped_ptr(base::Value::CreateIntegerValue(int_value));
144 case base::Value::TYPE_DOUBLE: {
145 // Doubles may be string-encoded or integer-encoded.
146 double double_value = 0;
147 if (value.GetAsInteger(&int_value)) {
148 return make_scoped_ptr(base::Value::CreateDoubleValue(int_value));
149 } else if (value.GetAsString(&string_value) &&
150 base::StringToDouble(string_value, &double_value)) {
151 return make_scoped_ptr(base::Value::CreateDoubleValue(double_value));
155 case base::Value::TYPE_LIST: {
156 // Lists are encoded as subkeys with numbered value in the registry.
157 const base::DictionaryValue* dict = NULL;
158 if (value.GetAsDictionary(&dict)) {
159 scoped_ptr<base::ListValue> result(new base::ListValue());
160 const base::DictionaryValue* item_schema =
161 GetEntry(schema, schema::kItems);
162 for (int i = 1; ; ++i) {
163 const base::Value* entry = NULL;
164 if (!dict->Get(base::IntToString(i), &entry))
166 result->Append(ConvertValue(*entry, item_schema).release());
168 return result.Pass();
170 // Fall through in order to accept lists encoded as JSON strings.
172 case base::Value::TYPE_DICTIONARY: {
173 // Dictionaries may be encoded as JSON strings.
174 if (value.GetAsString(&string_value)) {
175 scoped_ptr<base::Value> result(base::JSONReader::Read(string_value));
176 if (result && result->IsType(result_type))
177 return result.Pass();
181 case base::Value::TYPE_STRING:
182 case base::Value::TYPE_BINARY:
183 // No conversion possible.
187 LOG(WARNING) << "Failed to convert " << value.GetType()
188 << " to " << result_type;
189 return make_scoped_ptr(base::Value::CreateNullValue());
194 bool CaseInsensitiveStringCompare::operator()(const std::string& a,
195 const std::string& b) const {
196 return base::strcasecmp(a.c_str(), b.c_str()) < 0;
199 RegistryDict::RegistryDict() {}
201 RegistryDict::~RegistryDict() {
206 RegistryDict* RegistryDict::GetKey(const std::string& name) {
207 KeyMap::iterator entry = keys_.find(name);
208 return entry != keys_.end() ? entry->second : NULL;
211 const RegistryDict* RegistryDict::GetKey(const std::string& name) const {
212 KeyMap::const_iterator entry = keys_.find(name);
213 return entry != keys_.end() ? entry->second : NULL;
216 void RegistryDict::SetKey(const std::string& name,
217 scoped_ptr<RegistryDict> dict) {
223 RegistryDict*& entry = keys_[name];
225 entry = dict.release();
228 scoped_ptr<RegistryDict> RegistryDict::RemoveKey(const std::string& name) {
229 scoped_ptr<RegistryDict> result;
230 KeyMap::iterator entry = keys_.find(name);
231 if (entry != keys_.end()) {
232 result.reset(entry->second);
235 return result.Pass();
238 void RegistryDict::ClearKeys() {
239 STLDeleteValues(&keys_);
242 base::Value* RegistryDict::GetValue(const std::string& name) {
243 ValueMap::iterator entry = values_.find(name);
244 return entry != values_.end() ? entry->second : NULL;
247 const base::Value* RegistryDict::GetValue(const std::string& name) const {
248 ValueMap::const_iterator entry = values_.find(name);
249 return entry != values_.end() ? entry->second : NULL;
252 void RegistryDict::SetValue(const std::string& name,
253 scoped_ptr<base::Value> dict) {
259 Value*& entry = values_[name];
261 entry = dict.release();
264 scoped_ptr<base::Value> RegistryDict::RemoveValue(const std::string& name) {
265 scoped_ptr<base::Value> result;
266 ValueMap::iterator entry = values_.find(name);
267 if (entry != values_.end()) {
268 result.reset(entry->second);
269 values_.erase(entry);
271 return result.Pass();
274 void RegistryDict::ClearValues() {
275 STLDeleteValues(&values_);
278 void RegistryDict::Merge(const RegistryDict& other) {
279 for (KeyMap::const_iterator entry(other.keys_.begin());
280 entry != other.keys_.end(); ++entry) {
281 RegistryDict*& subdict = keys_[entry->first];
283 subdict = new RegistryDict();
284 subdict->Merge(*entry->second);
287 for (ValueMap::const_iterator entry(other.values_.begin());
288 entry != other.values_.end(); ++entry) {
289 SetValue(entry->first, make_scoped_ptr(entry->second->DeepCopy()));
293 void RegistryDict::Swap(RegistryDict* other) {
294 keys_.swap(other->keys_);
295 values_.swap(other->values_);
298 void RegistryDict::ReadRegistry(HKEY hive, const string16& root) {
302 // First, read all the values of the key.
303 for (RegistryValueIterator it(hive, root.c_str()); it.Valid(); ++it) {
304 const std::string name = UTF16ToUTF8(it.Name());
310 make_scoped_ptr(new base::StringValue(UTF16ToUTF8(it.Value()))));
312 case REG_DWORD_LITTLE_ENDIAN:
313 case REG_DWORD_BIG_ENDIAN:
314 if (it.ValueSize() == sizeof(DWORD)) {
315 DWORD dword_value = *(reinterpret_cast<const DWORD*>(it.Value()));
316 if (it.Type() == REG_DWORD_BIG_ENDIAN)
317 dword_value = base::NetToHost32(dword_value);
319 dword_value = base::ByteSwapToLE32(dword_value);
322 make_scoped_ptr(base::Value::CreateIntegerValue(dword_value)));
328 case REG_RESOURCE_LIST:
329 case REG_FULL_RESOURCE_DESCRIPTOR:
330 case REG_RESOURCE_REQUIREMENTS_LIST:
331 case REG_QWORD_LITTLE_ENDIAN:
332 // Unsupported type, message gets logged below.
336 LOG(WARNING) << "Failed to read hive " << hive << " at "
337 << root << "\\" << name
338 << " type " << it.Type();
341 // Recurse for all subkeys.
342 for (RegistryKeyIterator it(hive, root.c_str()); it.Valid(); ++it) {
343 std::string name(UTF16ToUTF8(it.Name()));
344 scoped_ptr<RegistryDict> subdict(new RegistryDict());
345 subdict->ReadRegistry(hive, root + L"\\" + it.Name());
346 SetKey(name, subdict.Pass());
350 scoped_ptr<base::Value> RegistryDict::ConvertToJSON(
351 const base::DictionaryValue* schema) const {
352 base::Value::Type type =
353 GetValueTypeForSchema(schema, base::Value::TYPE_DICTIONARY);
355 case base::Value::TYPE_DICTIONARY: {
356 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
357 for (RegistryDict::ValueMap::const_iterator entry(values_.begin());
358 entry != values_.end(); ++entry) {
359 result->SetWithoutPathExpansion(
361 ConvertValue(*entry->second,
362 GetSchemaFor(schema, entry->first)).release());
364 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin());
365 entry != keys_.end(); ++entry) {
366 result->SetWithoutPathExpansion(
368 entry->second->ConvertToJSON(
369 GetSchemaFor(schema, entry->first)).release());
371 return result.Pass();
373 case base::Value::TYPE_LIST: {
374 scoped_ptr<base::ListValue> result(new base::ListValue());
375 const base::DictionaryValue* item_schema =
376 GetEntry(schema, schema::kItems);
377 for (int i = 1; ; ++i) {
378 const std::string name(base::IntToString(i));
379 const RegistryDict* key = GetKey(name);
381 result->Append(key->ConvertToJSON(item_schema).release());
384 const base::Value* value = GetValue(name);
386 result->Append(ConvertValue(*value, item_schema).release());
391 return result.Pass();
394 LOG(WARNING) << "Can't convert registry key to schema type " << type;
397 return make_scoped_ptr(base::Value::CreateNullValue());
400 } // namespace policy