| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/sys_byteorder.h" | 14 #include "base/sys_byteorder.h" |
| 15 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "base/win/registry.h" | 16 #include "base/win/registry.h" |
| 17 #include "components/policy/core/common/schema.h" | 17 #include "components/policy/core/common/schema.h" |
| 18 | 18 |
| 19 using base::win::RegistryKeyIterator; | 19 using base::win::RegistryKeyIterator; |
| 20 using base::win::RegistryValueIterator; | 20 using base::win::RegistryValueIterator; |
| 21 | 21 |
| 22 namespace policy { | 22 namespace policy { |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 // Validates that a key is numerical. Used for lists below. |
| 27 bool IsKeyNumerical(const std::string& key) { |
| 28 int temp = 0; |
| 29 return base::StringToInt(key, &temp); |
| 30 } |
| 31 |
| 26 // Converts a value (as read from the registry) to meet |schema|, converting | 32 // Converts a value (as read from the registry) to meet |schema|, converting |
| 27 // types as necessary. Unconvertible types will show up as null values in the | 33 // types as necessary. Unconvertible types will show up as null values in the |
| 28 // result. | 34 // result. |
| 29 std::unique_ptr<base::Value> ConvertValue(const base::Value& value, | 35 std::unique_ptr<base::Value> ConvertValue(const base::Value& value, |
| 30 const Schema& schema) { | 36 const Schema& schema) { |
| 31 if (!schema.valid()) | 37 if (!schema.valid()) |
| 32 return value.CreateDeepCopy(); | 38 return value.CreateDeepCopy(); |
| 33 | 39 |
| 34 // If the type is good already, go with it. | 40 // If the type is good already, go with it. |
| 35 if (value.IsType(schema.type())) { | 41 if (value.IsType(schema.type())) { |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 double double_value = 0; | 98 double double_value = 0; |
| 93 if (value.GetAsDouble(&double_value) || | 99 if (value.GetAsDouble(&double_value) || |
| 94 (value.GetAsString(&string_value) && | 100 (value.GetAsString(&string_value) && |
| 95 base::StringToDouble(string_value, &double_value))) { | 101 base::StringToDouble(string_value, &double_value))) { |
| 96 return std::unique_ptr<base::Value>( | 102 return std::unique_ptr<base::Value>( |
| 97 new base::FundamentalValue(double_value)); | 103 new base::FundamentalValue(double_value)); |
| 98 } | 104 } |
| 99 break; | 105 break; |
| 100 } | 106 } |
| 101 case base::Value::TYPE_LIST: { | 107 case base::Value::TYPE_LIST: { |
| 102 // Lists are encoded as subkeys with numbered value in the registry. | 108 // Lists are encoded as subkeys with numbered value in the registry |
| 109 // (non-numerical keys are ignored). |
| 103 const base::DictionaryValue* dict = nullptr; | 110 const base::DictionaryValue* dict = nullptr; |
| 104 if (value.GetAsDictionary(&dict)) { | 111 if (value.GetAsDictionary(&dict)) { |
| 105 std::unique_ptr<base::ListValue> result(new base::ListValue()); | 112 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 106 for (int i = 1; ; ++i) { | 113 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); |
| 107 const base::Value* entry = nullptr; | 114 it.Advance()) { |
| 108 if (!dict->Get(base::IntToString(i), &entry)) | 115 if (!IsKeyNumerical(it.key())) |
| 109 break; | 116 continue; |
| 110 std::unique_ptr<base::Value> converted = | 117 std::unique_ptr<base::Value> converted = |
| 111 ConvertValue(*entry, schema.GetItems()); | 118 ConvertValue(it.value(), schema.GetItems()); |
| 112 if (converted) | 119 if (converted) |
| 113 result->Append(converted.release()); | 120 result->Append(converted.release()); |
| 114 } | 121 } |
| 115 return std::move(result); | 122 return std::move(result); |
| 116 } | 123 } |
| 117 // Fall through in order to accept lists encoded as JSON strings. | 124 // Fall through in order to accept lists encoded as JSON strings. |
| 118 } | 125 } |
| 119 case base::Value::TYPE_DICTIONARY: { | 126 case base::Value::TYPE_DICTIONARY: { |
| 120 // Dictionaries may be encoded as JSON strings. | 127 // Dictionaries may be encoded as JSON strings. |
| 121 if (value.GetAsString(&string_value)) { | 128 if (value.GetAsString(&string_value)) { |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 std::unique_ptr<base::Value> converted = | 322 std::unique_ptr<base::Value> converted = |
| 316 entry->second->ConvertToJSON(subschema); | 323 entry->second->ConvertToJSON(subschema); |
| 317 if (converted) | 324 if (converted) |
| 318 result->SetWithoutPathExpansion(entry->first, converted.release()); | 325 result->SetWithoutPathExpansion(entry->first, converted.release()); |
| 319 } | 326 } |
| 320 return std::move(result); | 327 return std::move(result); |
| 321 } | 328 } |
| 322 case base::Value::TYPE_LIST: { | 329 case base::Value::TYPE_LIST: { |
| 323 std::unique_ptr<base::ListValue> result(new base::ListValue()); | 330 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 324 Schema item_schema = schema.valid() ? schema.GetItems() : Schema(); | 331 Schema item_schema = schema.valid() ? schema.GetItems() : Schema(); |
| 325 for (int i = 1; ; ++i) { | 332 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); |
| 326 const std::string name(base::IntToString(i)); | 333 entry != keys_.end(); ++entry) { |
| 327 const RegistryDict* key = GetKey(name); | 334 if (!IsKeyNumerical(entry->first)) |
| 328 if (key) { | |
| 329 std::unique_ptr<base::Value> converted = | |
| 330 key->ConvertToJSON(item_schema); | |
| 331 if (converted) | |
| 332 result->Append(converted.release()); | |
| 333 continue; | 335 continue; |
| 334 } | 336 std::unique_ptr<base::Value> converted = |
| 335 const base::Value* value = GetValue(name); | 337 entry->second->ConvertToJSON(item_schema); |
| 336 if (value) { | 338 if (converted) |
| 337 std::unique_ptr<base::Value> converted = | 339 result->Append(converted.release()); |
| 338 ConvertValue(*value, item_schema); | 340 } |
| 339 if (converted) | 341 for (RegistryDict::ValueMap::const_iterator entry(values_.begin()); |
| 340 result->Append(converted.release()); | 342 entry != values_.end(); ++entry) { |
| 343 if (!IsKeyNumerical(entry->first)) |
| 341 continue; | 344 continue; |
| 342 } | 345 std::unique_ptr<base::Value> converted = |
| 343 break; | 346 ConvertValue(*entry->second, item_schema); |
| 347 if (converted) |
| 348 result->Append(converted.release()); |
| 344 } | 349 } |
| 345 return std::move(result); | 350 return std::move(result); |
| 346 } | 351 } |
| 347 default: | 352 default: |
| 348 LOG(WARNING) << "Can't convert registry key to schema type " << type; | 353 LOG(WARNING) << "Can't convert registry key to schema type " << type; |
| 349 } | 354 } |
| 350 | 355 |
| 351 return nullptr; | 356 return nullptr; |
| 352 } | 357 } |
| 353 | 358 |
| 354 } // namespace policy | 359 } // namespace policy |
| OLD | NEW |