| 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/registry_dict_win.h" | 5 #include "components/policy/core/common/registry_dict_win.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/sys_byteorder.h" | 12 #include "base/sys_byteorder.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "base/win/registry.h" | 14 #include "base/win/registry.h" |
| 15 #include "components/policy/core/common/schema.h" | 15 #include "components/policy/core/common/schema.h" |
| 16 | 16 |
| 17 using base::win::RegistryKeyIterator; | 17 using base::win::RegistryKeyIterator; |
| 18 using base::win::RegistryValueIterator; | 18 using base::win::RegistryValueIterator; |
| 19 | 19 |
| 20 namespace policy { | 20 namespace policy { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 // Converts a value (as read from the registry) to meet |schema|, converting | 24 // Converts a value (as read from the registry) to meet |schema|, converting |
| 25 // types as necessary. Unconvertible types will show up as NULL values in the | 25 // types as necessary. Unconvertible types will show up as NULL values in the |
| 26 // result. | 26 // result. |
| 27 scoped_ptr<base::Value> ConvertValue(const base::Value& value, | 27 scoped_ptr<base::Value> ConvertValue(const base::Value& value, |
| 28 const Schema& schema) { | 28 const Schema& schema) { |
| 29 if (!schema.valid()) | 29 if (!schema.valid()) |
| 30 return make_scoped_ptr(value.DeepCopy()); | 30 return make_scoped_ptr(value.DeepCopy()).Pass(); |
| 31 | 31 |
| 32 // If the type is good already, go with it. | 32 // If the type is good already, go with it. |
| 33 if (value.IsType(schema.type())) { | 33 if (value.IsType(schema.type())) { |
| 34 // Recurse for complex types. | 34 // Recurse for complex types. |
| 35 const base::DictionaryValue* dict = NULL; | 35 const base::DictionaryValue* dict = NULL; |
| 36 const base::ListValue* list = NULL; | 36 const base::ListValue* list = NULL; |
| 37 if (value.GetAsDictionary(&dict)) { | 37 if (value.GetAsDictionary(&dict)) { |
| 38 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 38 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
| 39 for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd(); | 39 for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd(); |
| 40 entry.Advance()) { | 40 entry.Advance()) { |
| 41 scoped_ptr<base::Value> converted = | 41 scoped_ptr<base::Value> converted = |
| 42 ConvertValue(entry.value(), schema.GetProperty(entry.key())); | 42 ConvertValue(entry.value(), schema.GetProperty(entry.key())); |
| 43 if (converted) | 43 if (converted) |
| 44 result->SetWithoutPathExpansion(entry.key(), converted.release()); | 44 result->SetWithoutPathExpansion(entry.key(), converted.release()); |
| 45 } | 45 } |
| 46 return result.Pass(); | 46 return result.PassAs<base::Value>(); |
| 47 } else if (value.GetAsList(&list)) { | 47 } else if (value.GetAsList(&list)) { |
| 48 scoped_ptr<base::ListValue> result(new base::ListValue()); | 48 scoped_ptr<base::ListValue> result(new base::ListValue()); |
| 49 for (base::ListValue::const_iterator entry(list->begin()); | 49 for (base::ListValue::const_iterator entry(list->begin()); |
| 50 entry != list->end(); ++entry) { | 50 entry != list->end(); ++entry) { |
| 51 scoped_ptr<base::Value> converted = | 51 scoped_ptr<base::Value> converted = |
| 52 ConvertValue(**entry, schema.GetItems()); | 52 ConvertValue(**entry, schema.GetItems()); |
| 53 if (converted) | 53 if (converted) |
| 54 result->Append(converted.release()); | 54 result->Append(converted.release()); |
| 55 } | 55 } |
| 56 return result.Pass(); | 56 return result.PassAs<base::Value>(); |
| 57 } | 57 } |
| 58 return make_scoped_ptr(value.DeepCopy()); | 58 return make_scoped_ptr(value.DeepCopy()).Pass(); |
| 59 } | 59 } |
| 60 | 60 |
| 61 // Else, do some conversions to map windows registry data types to JSON types. | 61 // Else, do some conversions to map windows registry data types to JSON types. |
| 62 std::string string_value; | 62 std::string string_value; |
| 63 int int_value = 0; | 63 int int_value = 0; |
| 64 switch (schema.type()) { | 64 switch (schema.type()) { |
| 65 case base::Value::TYPE_NULL: { | 65 case base::Value::TYPE_NULL: { |
| 66 return make_scoped_ptr(base::Value::CreateNullValue()); | 66 return make_scoped_ptr(base::Value::CreateNullValue()).Pass(); |
| 67 } | 67 } |
| 68 case base::Value::TYPE_BOOLEAN: { | 68 case base::Value::TYPE_BOOLEAN: { |
| 69 // Accept booleans encoded as either string or integer. | 69 // Accept booleans encoded as either string or integer. |
| 70 if (value.GetAsInteger(&int_value) || | 70 if (value.GetAsInteger(&int_value) || |
| 71 (value.GetAsString(&string_value) && | 71 (value.GetAsString(&string_value) && |
| 72 base::StringToInt(string_value, &int_value))) { | 72 base::StringToInt(string_value, &int_value))) { |
| 73 return make_scoped_ptr(base::Value::CreateBooleanValue(int_value != 0)); | 73 return make_scoped_ptr(base::Value::CreateBooleanValue(int_value != 0)) |
| 74 .PassAs<base::Value>(); |
| 74 } | 75 } |
| 75 break; | 76 break; |
| 76 } | 77 } |
| 77 case base::Value::TYPE_INTEGER: { | 78 case base::Value::TYPE_INTEGER: { |
| 78 // Integers may be string-encoded. | 79 // Integers may be string-encoded. |
| 79 if (value.GetAsString(&string_value) && | 80 if (value.GetAsString(&string_value) && |
| 80 base::StringToInt(string_value, &int_value)) { | 81 base::StringToInt(string_value, &int_value)) { |
| 81 return make_scoped_ptr(base::Value::CreateIntegerValue(int_value)); | 82 return make_scoped_ptr(base::Value::CreateIntegerValue(int_value)) |
| 83 .PassAs<base::Value>(); |
| 82 } | 84 } |
| 83 break; | 85 break; |
| 84 } | 86 } |
| 85 case base::Value::TYPE_DOUBLE: { | 87 case base::Value::TYPE_DOUBLE: { |
| 86 // Doubles may be string-encoded or integer-encoded. | 88 // Doubles may be string-encoded or integer-encoded. |
| 87 double double_value = 0; | 89 double double_value = 0; |
| 88 if (value.GetAsInteger(&int_value)) { | 90 if (value.GetAsInteger(&int_value)) { |
| 89 return make_scoped_ptr(base::Value::CreateDoubleValue(int_value)); | 91 return make_scoped_ptr(base::Value::CreateDoubleValue(int_value)) |
| 92 .PassAs<base::Value>(); |
| 90 } else if (value.GetAsString(&string_value) && | 93 } else if (value.GetAsString(&string_value) && |
| 91 base::StringToDouble(string_value, &double_value)) { | 94 base::StringToDouble(string_value, &double_value)) { |
| 92 return make_scoped_ptr(base::Value::CreateDoubleValue(double_value)); | 95 return make_scoped_ptr(base::Value::CreateDoubleValue(double_value)) |
| 96 .PassAs<base::Value>(); |
| 93 } | 97 } |
| 94 break; | 98 break; |
| 95 } | 99 } |
| 96 case base::Value::TYPE_LIST: { | 100 case base::Value::TYPE_LIST: { |
| 97 // Lists are encoded as subkeys with numbered value in the registry. | 101 // Lists are encoded as subkeys with numbered value in the registry. |
| 98 const base::DictionaryValue* dict = NULL; | 102 const base::DictionaryValue* dict = NULL; |
| 99 if (value.GetAsDictionary(&dict)) { | 103 if (value.GetAsDictionary(&dict)) { |
| 100 scoped_ptr<base::ListValue> result(new base::ListValue()); | 104 scoped_ptr<base::ListValue> result(new base::ListValue()); |
| 101 for (int i = 1; ; ++i) { | 105 for (int i = 1; ; ++i) { |
| 102 const base::Value* entry = NULL; | 106 const base::Value* entry = NULL; |
| 103 if (!dict->Get(base::IntToString(i), &entry)) | 107 if (!dict->Get(base::IntToString(i), &entry)) |
| 104 break; | 108 break; |
| 105 scoped_ptr<base::Value> converted = | 109 scoped_ptr<base::Value> converted = |
| 106 ConvertValue(*entry, schema.GetItems()); | 110 ConvertValue(*entry, schema.GetItems()); |
| 107 if (converted) | 111 if (converted) |
| 108 result->Append(converted.release()); | 112 result->Append(converted.release()); |
| 109 } | 113 } |
| 110 return result.Pass(); | 114 return result.PassAs<base::Value>(); |
| 111 } | 115 } |
| 112 // Fall through in order to accept lists encoded as JSON strings. | 116 // Fall through in order to accept lists encoded as JSON strings. |
| 113 } | 117 } |
| 114 case base::Value::TYPE_DICTIONARY: { | 118 case base::Value::TYPE_DICTIONARY: { |
| 115 // Dictionaries may be encoded as JSON strings. | 119 // Dictionaries may be encoded as JSON strings. |
| 116 if (value.GetAsString(&string_value)) { | 120 if (value.GetAsString(&string_value)) { |
| 117 scoped_ptr<base::Value> result(base::JSONReader::Read(string_value)); | 121 scoped_ptr<base::Value> result(base::JSONReader::Read(string_value)); |
| 118 if (result && result->IsType(schema.type())) | 122 if (result && result->IsType(schema.type())) |
| 119 return result.Pass(); | 123 return result.Pass(); |
| 120 } | 124 } |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 for (KeyMap::const_iterator entry(other.keys_.begin()); | 225 for (KeyMap::const_iterator entry(other.keys_.begin()); |
| 222 entry != other.keys_.end(); ++entry) { | 226 entry != other.keys_.end(); ++entry) { |
| 223 RegistryDict*& subdict = keys_[entry->first]; | 227 RegistryDict*& subdict = keys_[entry->first]; |
| 224 if (!subdict) | 228 if (!subdict) |
| 225 subdict = new RegistryDict(); | 229 subdict = new RegistryDict(); |
| 226 subdict->Merge(*entry->second); | 230 subdict->Merge(*entry->second); |
| 227 } | 231 } |
| 228 | 232 |
| 229 for (ValueMap::const_iterator entry(other.values_.begin()); | 233 for (ValueMap::const_iterator entry(other.values_.begin()); |
| 230 entry != other.values_.end(); ++entry) { | 234 entry != other.values_.end(); ++entry) { |
| 231 SetValue(entry->first, make_scoped_ptr(entry->second->DeepCopy())); | 235 SetValue(entry->first, make_scoped_ptr(entry->second->DeepCopy()).Pass()); |
| 232 } | 236 } |
| 233 } | 237 } |
| 234 | 238 |
| 235 void RegistryDict::Swap(RegistryDict* other) { | 239 void RegistryDict::Swap(RegistryDict* other) { |
| 236 keys_.swap(other->keys_); | 240 keys_.swap(other->keys_); |
| 237 values_.swap(other->values_); | 241 values_.swap(other->values_); |
| 238 } | 242 } |
| 239 | 243 |
| 240 void RegistryDict::ReadRegistry(HKEY hive, const base::string16& root) { | 244 void RegistryDict::ReadRegistry(HKEY hive, const base::string16& root) { |
| 241 ClearKeys(); | 245 ClearKeys(); |
| 242 ClearValues(); | 246 ClearValues(); |
| 243 | 247 |
| 244 // First, read all the values of the key. | 248 // First, read all the values of the key. |
| 245 for (RegistryValueIterator it(hive, root.c_str()); it.Valid(); ++it) { | 249 for (RegistryValueIterator it(hive, root.c_str()); it.Valid(); ++it) { |
| 246 const std::string name = base::UTF16ToUTF8(it.Name()); | 250 const std::string name = base::UTF16ToUTF8(it.Name()); |
| 247 switch (it.Type()) { | 251 switch (it.Type()) { |
| 248 case REG_SZ: | 252 case REG_SZ: |
| 249 case REG_EXPAND_SZ: | 253 case REG_EXPAND_SZ: |
| 250 SetValue( | 254 SetValue(name, |
| 251 name, | 255 make_scoped_ptr(new base::StringValue( |
| 252 make_scoped_ptr( | 256 base::UTF16ToUTF8(it.Value()))).Pass()); |
| 253 new base::StringValue(base::UTF16ToUTF8(it.Value())))); | |
| 254 continue; | 257 continue; |
| 255 case REG_DWORD_LITTLE_ENDIAN: | 258 case REG_DWORD_LITTLE_ENDIAN: |
| 256 case REG_DWORD_BIG_ENDIAN: | 259 case REG_DWORD_BIG_ENDIAN: |
| 257 if (it.ValueSize() == sizeof(DWORD)) { | 260 if (it.ValueSize() == sizeof(DWORD)) { |
| 258 DWORD dword_value = *(reinterpret_cast<const DWORD*>(it.Value())); | 261 DWORD dword_value = *(reinterpret_cast<const DWORD*>(it.Value())); |
| 259 if (it.Type() == REG_DWORD_BIG_ENDIAN) | 262 if (it.Type() == REG_DWORD_BIG_ENDIAN) |
| 260 dword_value = base::NetToHost32(dword_value); | 263 dword_value = base::NetToHost32(dword_value); |
| 261 else | 264 else |
| 262 dword_value = base::ByteSwapToLE32(dword_value); | 265 dword_value = base::ByteSwapToLE32(dword_value); |
| 263 SetValue( | 266 SetValue(name, |
| 264 name, | 267 make_scoped_ptr(base::Value::CreateIntegerValue(dword_value)) |
| 265 make_scoped_ptr(base::Value::CreateIntegerValue(dword_value))); | 268 .PassAs<base::Value>()); |
| 266 continue; | 269 continue; |
| 267 } | 270 } |
| 268 case REG_NONE: | 271 case REG_NONE: |
| 269 case REG_LINK: | 272 case REG_LINK: |
| 270 case REG_MULTI_SZ: | 273 case REG_MULTI_SZ: |
| 271 case REG_RESOURCE_LIST: | 274 case REG_RESOURCE_LIST: |
| 272 case REG_FULL_RESOURCE_DESCRIPTOR: | 275 case REG_FULL_RESOURCE_DESCRIPTOR: |
| 273 case REG_RESOURCE_REQUIREMENTS_LIST: | 276 case REG_RESOURCE_REQUIREMENTS_LIST: |
| 274 case REG_QWORD_LITTLE_ENDIAN: | 277 case REG_QWORD_LITTLE_ENDIAN: |
| 275 // Unsupported type, message gets logged below. | 278 // Unsupported type, message gets logged below. |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 return result.Pass(); | 344 return result.Pass(); |
| 342 } | 345 } |
| 343 default: | 346 default: |
| 344 LOG(WARNING) << "Can't convert registry key to schema type " << type; | 347 LOG(WARNING) << "Can't convert registry key to schema type " << type; |
| 345 } | 348 } |
| 346 | 349 |
| 347 return scoped_ptr<base::Value>(); | 350 return scoped_ptr<base::Value>(); |
| 348 } | 351 } |
| 349 | 352 |
| 350 } // namespace policy | 353 } // namespace policy |
| OLD | NEW |