| 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.h" | 5 #include "components/policy/core/common/registry_dict.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" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 } | 68 } |
| 69 return std::move(result); | 69 return std::move(result); |
| 70 } | 70 } |
| 71 return value.CreateDeepCopy(); | 71 return value.CreateDeepCopy(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // Else, do some conversions to map windows registry data types to JSON types. | 74 // Else, do some conversions to map windows registry data types to JSON types. |
| 75 std::string string_value; | 75 std::string string_value; |
| 76 int int_value = 0; | 76 int int_value = 0; |
| 77 switch (schema.type()) { | 77 switch (schema.type()) { |
| 78 case base::Value::TYPE_NULL: { | 78 case base::Value::Type::NONE: { |
| 79 return base::Value::CreateNullValue(); | 79 return base::Value::CreateNullValue(); |
| 80 } | 80 } |
| 81 case base::Value::TYPE_BOOLEAN: { | 81 case base::Value::Type::BOOLEAN: { |
| 82 // Accept booleans encoded as either string or integer. | 82 // Accept booleans encoded as either string or integer. |
| 83 if (value.GetAsInteger(&int_value) || | 83 if (value.GetAsInteger(&int_value) || |
| 84 (value.GetAsString(&string_value) && | 84 (value.GetAsString(&string_value) && |
| 85 base::StringToInt(string_value, &int_value))) { | 85 base::StringToInt(string_value, &int_value))) { |
| 86 return std::unique_ptr<base::Value>( | 86 return std::unique_ptr<base::Value>( |
| 87 new base::FundamentalValue(int_value != 0)); | 87 new base::FundamentalValue(int_value != 0)); |
| 88 } | 88 } |
| 89 break; | 89 break; |
| 90 } | 90 } |
| 91 case base::Value::TYPE_INTEGER: { | 91 case base::Value::Type::INTEGER: { |
| 92 // Integers may be string-encoded. | 92 // Integers may be string-encoded. |
| 93 if (value.GetAsString(&string_value) && | 93 if (value.GetAsString(&string_value) && |
| 94 base::StringToInt(string_value, &int_value)) { | 94 base::StringToInt(string_value, &int_value)) { |
| 95 return std::unique_ptr<base::Value>( | 95 return std::unique_ptr<base::Value>( |
| 96 new base::FundamentalValue(int_value)); | 96 new base::FundamentalValue(int_value)); |
| 97 } | 97 } |
| 98 break; | 98 break; |
| 99 } | 99 } |
| 100 case base::Value::TYPE_DOUBLE: { | 100 case base::Value::Type::DOUBLE: { |
| 101 // Doubles may be string-encoded or integer-encoded. | 101 // Doubles may be string-encoded or integer-encoded. |
| 102 double double_value = 0; | 102 double double_value = 0; |
| 103 if (value.GetAsDouble(&double_value) || | 103 if (value.GetAsDouble(&double_value) || |
| 104 (value.GetAsString(&string_value) && | 104 (value.GetAsString(&string_value) && |
| 105 base::StringToDouble(string_value, &double_value))) { | 105 base::StringToDouble(string_value, &double_value))) { |
| 106 return std::unique_ptr<base::Value>( | 106 return std::unique_ptr<base::Value>( |
| 107 new base::FundamentalValue(double_value)); | 107 new base::FundamentalValue(double_value)); |
| 108 } | 108 } |
| 109 break; | 109 break; |
| 110 } | 110 } |
| 111 case base::Value::TYPE_LIST: { | 111 case base::Value::Type::LIST: { |
| 112 // Lists are encoded as subkeys with numbered value in the registry | 112 // Lists are encoded as subkeys with numbered value in the registry |
| 113 // (non-numerical keys are ignored). | 113 // (non-numerical keys are ignored). |
| 114 const base::DictionaryValue* dict = nullptr; | 114 const base::DictionaryValue* dict = nullptr; |
| 115 if (value.GetAsDictionary(&dict)) { | 115 if (value.GetAsDictionary(&dict)) { |
| 116 std::unique_ptr<base::ListValue> result(new base::ListValue()); | 116 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 117 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); | 117 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); |
| 118 it.Advance()) { | 118 it.Advance()) { |
| 119 if (!IsKeyNumerical(it.key())) | 119 if (!IsKeyNumerical(it.key())) |
| 120 continue; | 120 continue; |
| 121 std::unique_ptr<base::Value> converted = | 121 std::unique_ptr<base::Value> converted = |
| 122 ConvertValue(it.value(), schema.GetItems()); | 122 ConvertValue(it.value(), schema.GetItems()); |
| 123 if (converted) | 123 if (converted) |
| 124 result->Append(std::move(converted)); | 124 result->Append(std::move(converted)); |
| 125 } | 125 } |
| 126 return std::move(result); | 126 return std::move(result); |
| 127 } | 127 } |
| 128 // Fall through in order to accept lists encoded as JSON strings. | 128 // Fall through in order to accept lists encoded as JSON strings. |
| 129 } | 129 } |
| 130 case base::Value::TYPE_DICTIONARY: { | 130 case base::Value::Type::DICTIONARY: { |
| 131 // Dictionaries may be encoded as JSON strings. | 131 // Dictionaries may be encoded as JSON strings. |
| 132 if (value.GetAsString(&string_value)) { | 132 if (value.GetAsString(&string_value)) { |
| 133 std::unique_ptr<base::Value> result = | 133 std::unique_ptr<base::Value> result = |
| 134 base::JSONReader::Read(string_value); | 134 base::JSONReader::Read(string_value); |
| 135 if (result && result->IsType(schema.type())) | 135 if (result && result->IsType(schema.type())) |
| 136 return result; | 136 return result; |
| 137 } | 137 } |
| 138 break; | 138 break; |
| 139 } | 139 } |
| 140 case base::Value::TYPE_STRING: | 140 case base::Value::Type::STRING: |
| 141 case base::Value::TYPE_BINARY: | 141 case base::Value::Type::BINARY: |
| 142 // No conversion possible. | 142 // No conversion possible. |
| 143 break; | 143 break; |
| 144 } | 144 } |
| 145 | 145 |
| 146 LOG(WARNING) << "Failed to convert " << value.GetType() | 146 LOG(WARNING) << "Failed to convert " << value.GetType() |
| 147 << " to " << schema.type(); | 147 << " to " << schema.type(); |
| 148 return nullptr; | 148 return nullptr; |
| 149 } | 149 } |
| 150 #endif // #if defined(OS_WIN) | 150 #endif // #if defined(OS_WIN) |
| 151 | 151 |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 std::string name(base::UTF16ToUTF8(it.Name())); | 300 std::string name(base::UTF16ToUTF8(it.Name())); |
| 301 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); | 301 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); |
| 302 subdict->ReadRegistry(hive, root + L"\\" + it.Name()); | 302 subdict->ReadRegistry(hive, root + L"\\" + it.Name()); |
| 303 SetKey(name, std::move(subdict)); | 303 SetKey(name, std::move(subdict)); |
| 304 } | 304 } |
| 305 } | 305 } |
| 306 | 306 |
| 307 std::unique_ptr<base::Value> RegistryDict::ConvertToJSON( | 307 std::unique_ptr<base::Value> RegistryDict::ConvertToJSON( |
| 308 const Schema& schema) const { | 308 const Schema& schema) const { |
| 309 base::Value::Type type = | 309 base::Value::Type type = |
| 310 schema.valid() ? schema.type() : base::Value::TYPE_DICTIONARY; | 310 schema.valid() ? schema.type() : base::Value::Type::DICTIONARY; |
| 311 switch (type) { | 311 switch (type) { |
| 312 case base::Value::TYPE_DICTIONARY: { | 312 case base::Value::Type::DICTIONARY: { |
| 313 std::unique_ptr<base::DictionaryValue> result( | 313 std::unique_ptr<base::DictionaryValue> result( |
| 314 new base::DictionaryValue()); | 314 new base::DictionaryValue()); |
| 315 for (RegistryDict::ValueMap::const_iterator entry(values_.begin()); | 315 for (RegistryDict::ValueMap::const_iterator entry(values_.begin()); |
| 316 entry != values_.end(); ++entry) { | 316 entry != values_.end(); ++entry) { |
| 317 Schema subschema = | 317 Schema subschema = |
| 318 schema.valid() ? schema.GetProperty(entry->first) : Schema(); | 318 schema.valid() ? schema.GetProperty(entry->first) : Schema(); |
| 319 std::unique_ptr<base::Value> converted = | 319 std::unique_ptr<base::Value> converted = |
| 320 ConvertValue(*entry->second, subschema); | 320 ConvertValue(*entry->second, subschema); |
| 321 if (converted) | 321 if (converted) |
| 322 result->SetWithoutPathExpansion(entry->first, converted.release()); | 322 result->SetWithoutPathExpansion(entry->first, converted.release()); |
| 323 } | 323 } |
| 324 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); | 324 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); |
| 325 entry != keys_.end(); ++entry) { | 325 entry != keys_.end(); ++entry) { |
| 326 Schema subschema = | 326 Schema subschema = |
| 327 schema.valid() ? schema.GetProperty(entry->first) : Schema(); | 327 schema.valid() ? schema.GetProperty(entry->first) : Schema(); |
| 328 std::unique_ptr<base::Value> converted = | 328 std::unique_ptr<base::Value> converted = |
| 329 entry->second->ConvertToJSON(subschema); | 329 entry->second->ConvertToJSON(subschema); |
| 330 if (converted) | 330 if (converted) |
| 331 result->SetWithoutPathExpansion(entry->first, converted.release()); | 331 result->SetWithoutPathExpansion(entry->first, converted.release()); |
| 332 } | 332 } |
| 333 return std::move(result); | 333 return std::move(result); |
| 334 } | 334 } |
| 335 case base::Value::TYPE_LIST: { | 335 case base::Value::Type::LIST: { |
| 336 std::unique_ptr<base::ListValue> result(new base::ListValue()); | 336 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 337 Schema item_schema = schema.valid() ? schema.GetItems() : Schema(); | 337 Schema item_schema = schema.valid() ? schema.GetItems() : Schema(); |
| 338 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); | 338 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); |
| 339 entry != keys_.end(); ++entry) { | 339 entry != keys_.end(); ++entry) { |
| 340 if (!IsKeyNumerical(entry->first)) | 340 if (!IsKeyNumerical(entry->first)) |
| 341 continue; | 341 continue; |
| 342 std::unique_ptr<base::Value> converted = | 342 std::unique_ptr<base::Value> converted = |
| 343 entry->second->ConvertToJSON(item_schema); | 343 entry->second->ConvertToJSON(item_schema); |
| 344 if (converted) | 344 if (converted) |
| 345 result->Append(std::move(converted)); | 345 result->Append(std::move(converted)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 356 return std::move(result); | 356 return std::move(result); |
| 357 } | 357 } |
| 358 default: | 358 default: |
| 359 LOG(WARNING) << "Can't convert registry key to schema type " << type; | 359 LOG(WARNING) << "Can't convert registry key to schema type " << type; |
| 360 } | 360 } |
| 361 | 361 |
| 362 return nullptr; | 362 return nullptr; |
| 363 } | 363 } |
| 364 #endif // #if defined(OS_WIN) | 364 #endif // #if defined(OS_WIN) |
| 365 } // namespace policy | 365 } // namespace policy |
| OLD | NEW |