| 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.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 |
| 17 #if defined(OS_WIN) |
| 16 #include "base/win/registry.h" | 18 #include "base/win/registry.h" |
| 17 #include "components/policy/core/common/schema.h" | 19 #include "components/policy/core/common/schema.h" |
| 18 | 20 |
| 19 using base::win::RegistryKeyIterator; | 21 using base::win::RegistryKeyIterator; |
| 20 using base::win::RegistryValueIterator; | 22 using base::win::RegistryValueIterator; |
| 23 #endif // #if defined(OS_WIN) |
| 21 | 24 |
| 22 namespace policy { | 25 namespace policy { |
| 23 | 26 |
| 24 namespace { | 27 namespace { |
| 25 | 28 |
| 29 #if defined(OS_WIN) |
| 26 // Validates that a key is numerical. Used for lists below. | 30 // Validates that a key is numerical. Used for lists below. |
| 27 bool IsKeyNumerical(const std::string& key) { | 31 bool IsKeyNumerical(const std::string& key) { |
| 28 int temp = 0; | 32 int temp = 0; |
| 29 return base::StringToInt(key, &temp); | 33 return base::StringToInt(key, &temp); |
| 30 } | 34 } |
| 31 | 35 |
| 32 // Converts a value (as read from the registry) to meet |schema|, converting | 36 // Converts a value (as read from the registry) to meet |schema|, converting |
| 33 // types as necessary. Unconvertible types will show up as null values in the | 37 // types as necessary. Unconvertible types will show up as null values in the |
| 34 // result. | 38 // result. |
| 35 std::unique_ptr<base::Value> ConvertValue(const base::Value& value, | 39 std::unique_ptr<base::Value> ConvertValue(const base::Value& value, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 53 result->SetWithoutPathExpansion(entry.key(), converted.release()); | 57 result->SetWithoutPathExpansion(entry.key(), converted.release()); |
| 54 } | 58 } |
| 55 return std::move(result); | 59 return std::move(result); |
| 56 } else if (value.GetAsList(&list)) { | 60 } else if (value.GetAsList(&list)) { |
| 57 std::unique_ptr<base::ListValue> result(new base::ListValue()); | 61 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 58 for (base::ListValue::const_iterator entry(list->begin()); | 62 for (base::ListValue::const_iterator entry(list->begin()); |
| 59 entry != list->end(); ++entry) { | 63 entry != list->end(); ++entry) { |
| 60 std::unique_ptr<base::Value> converted = | 64 std::unique_ptr<base::Value> converted = |
| 61 ConvertValue(**entry, schema.GetItems()); | 65 ConvertValue(**entry, schema.GetItems()); |
| 62 if (converted) | 66 if (converted) |
| 63 result->Append(converted.release()); | 67 result->Append(std::move(converted)); |
| 64 } | 68 } |
| 65 return std::move(result); | 69 return std::move(result); |
| 66 } | 70 } |
| 67 return value.CreateDeepCopy(); | 71 return value.CreateDeepCopy(); |
| 68 } | 72 } |
| 69 | 73 |
| 70 // 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. |
| 71 std::string string_value; | 75 std::string string_value; |
| 72 int int_value = 0; | 76 int int_value = 0; |
| 73 switch (schema.type()) { | 77 switch (schema.type()) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 const base::DictionaryValue* dict = nullptr; | 114 const base::DictionaryValue* dict = nullptr; |
| 111 if (value.GetAsDictionary(&dict)) { | 115 if (value.GetAsDictionary(&dict)) { |
| 112 std::unique_ptr<base::ListValue> result(new base::ListValue()); | 116 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 113 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); | 117 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); |
| 114 it.Advance()) { | 118 it.Advance()) { |
| 115 if (!IsKeyNumerical(it.key())) | 119 if (!IsKeyNumerical(it.key())) |
| 116 continue; | 120 continue; |
| 117 std::unique_ptr<base::Value> converted = | 121 std::unique_ptr<base::Value> converted = |
| 118 ConvertValue(it.value(), schema.GetItems()); | 122 ConvertValue(it.value(), schema.GetItems()); |
| 119 if (converted) | 123 if (converted) |
| 120 result->Append(converted.release()); | 124 result->Append(std::move(converted)); |
| 121 } | 125 } |
| 122 return std::move(result); | 126 return std::move(result); |
| 123 } | 127 } |
| 124 // Fall through in order to accept lists encoded as JSON strings. | 128 // Fall through in order to accept lists encoded as JSON strings. |
| 125 } | 129 } |
| 126 case base::Value::TYPE_DICTIONARY: { | 130 case base::Value::TYPE_DICTIONARY: { |
| 127 // Dictionaries may be encoded as JSON strings. | 131 // Dictionaries may be encoded as JSON strings. |
| 128 if (value.GetAsString(&string_value)) { | 132 if (value.GetAsString(&string_value)) { |
| 129 std::unique_ptr<base::Value> result = | 133 std::unique_ptr<base::Value> result = |
| 130 base::JSONReader::Read(string_value); | 134 base::JSONReader::Read(string_value); |
| 131 if (result && result->IsType(schema.type())) | 135 if (result && result->IsType(schema.type())) |
| 132 return result; | 136 return result; |
| 133 } | 137 } |
| 134 break; | 138 break; |
| 135 } | 139 } |
| 136 case base::Value::TYPE_STRING: | 140 case base::Value::TYPE_STRING: |
| 137 case base::Value::TYPE_BINARY: | 141 case base::Value::TYPE_BINARY: |
| 138 // No conversion possible. | 142 // No conversion possible. |
| 139 break; | 143 break; |
| 140 } | 144 } |
| 141 | 145 |
| 142 LOG(WARNING) << "Failed to convert " << value.GetType() | 146 LOG(WARNING) << "Failed to convert " << value.GetType() |
| 143 << " to " << schema.type(); | 147 << " to " << schema.type(); |
| 144 return nullptr; | 148 return nullptr; |
| 145 } | 149 } |
| 150 #endif // #if defined(OS_WIN) |
| 146 | 151 |
| 147 } // namespace | 152 } // namespace |
| 148 | 153 |
| 149 bool CaseInsensitiveStringCompare::operator()(const std::string& a, | 154 bool CaseInsensitiveStringCompare::operator()(const std::string& a, |
| 150 const std::string& b) const { | 155 const std::string& b) const { |
| 151 return base::CompareCaseInsensitiveASCII(a, b) < 0; | 156 return base::CompareCaseInsensitiveASCII(a, b) < 0; |
| 152 } | 157 } |
| 153 | 158 |
| 154 RegistryDict::RegistryDict() {} | 159 RegistryDict::RegistryDict() {} |
| 155 | 160 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 entry != other.values_.end(); ++entry) { | 245 entry != other.values_.end(); ++entry) { |
| 241 SetValue(entry->first, entry->second->CreateDeepCopy()); | 246 SetValue(entry->first, entry->second->CreateDeepCopy()); |
| 242 } | 247 } |
| 243 } | 248 } |
| 244 | 249 |
| 245 void RegistryDict::Swap(RegistryDict* other) { | 250 void RegistryDict::Swap(RegistryDict* other) { |
| 246 keys_.swap(other->keys_); | 251 keys_.swap(other->keys_); |
| 247 values_.swap(other->values_); | 252 values_.swap(other->values_); |
| 248 } | 253 } |
| 249 | 254 |
| 255 #if defined(OS_WIN) |
| 250 void RegistryDict::ReadRegistry(HKEY hive, const base::string16& root) { | 256 void RegistryDict::ReadRegistry(HKEY hive, const base::string16& root) { |
| 251 ClearKeys(); | 257 ClearKeys(); |
| 252 ClearValues(); | 258 ClearValues(); |
| 253 | 259 |
| 254 // First, read all the values of the key. | 260 // First, read all the values of the key. |
| 255 for (RegistryValueIterator it(hive, root.c_str()); it.Valid(); ++it) { | 261 for (RegistryValueIterator it(hive, root.c_str()); it.Valid(); ++it) { |
| 256 const std::string name = base::UTF16ToUTF8(it.Name()); | 262 const std::string name = base::UTF16ToUTF8(it.Name()); |
| 257 switch (it.Type()) { | 263 switch (it.Type()) { |
| 258 case REG_SZ: | 264 case REG_SZ: |
| 259 case REG_EXPAND_SZ: | 265 case REG_EXPAND_SZ: |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 case base::Value::TYPE_LIST: { | 335 case base::Value::TYPE_LIST: { |
| 330 std::unique_ptr<base::ListValue> result(new base::ListValue()); | 336 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 331 Schema item_schema = schema.valid() ? schema.GetItems() : Schema(); | 337 Schema item_schema = schema.valid() ? schema.GetItems() : Schema(); |
| 332 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); | 338 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); |
| 333 entry != keys_.end(); ++entry) { | 339 entry != keys_.end(); ++entry) { |
| 334 if (!IsKeyNumerical(entry->first)) | 340 if (!IsKeyNumerical(entry->first)) |
| 335 continue; | 341 continue; |
| 336 std::unique_ptr<base::Value> converted = | 342 std::unique_ptr<base::Value> converted = |
| 337 entry->second->ConvertToJSON(item_schema); | 343 entry->second->ConvertToJSON(item_schema); |
| 338 if (converted) | 344 if (converted) |
| 339 result->Append(converted.release()); | 345 result->Append(std::move(converted)); |
| 340 } | 346 } |
| 341 for (RegistryDict::ValueMap::const_iterator entry(values_.begin()); | 347 for (RegistryDict::ValueMap::const_iterator entry(values_.begin()); |
| 342 entry != values_.end(); ++entry) { | 348 entry != values_.end(); ++entry) { |
| 343 if (!IsKeyNumerical(entry->first)) | 349 if (!IsKeyNumerical(entry->first)) |
| 344 continue; | 350 continue; |
| 345 std::unique_ptr<base::Value> converted = | 351 std::unique_ptr<base::Value> converted = |
| 346 ConvertValue(*entry->second, item_schema); | 352 ConvertValue(*entry->second, item_schema); |
| 347 if (converted) | 353 if (converted) |
| 348 result->Append(converted.release()); | 354 result->Append(std::move(converted)); |
| 349 } | 355 } |
| 350 return std::move(result); | 356 return std::move(result); |
| 351 } | 357 } |
| 352 default: | 358 default: |
| 353 LOG(WARNING) << "Can't convert registry key to schema type " << type; | 359 LOG(WARNING) << "Can't convert registry key to schema type " << type; |
| 354 } | 360 } |
| 355 | 361 |
| 356 return nullptr; | 362 return nullptr; |
| 357 } | 363 } |
| 358 | 364 #endif // #if defined(OS_WIN) |
| 359 } // namespace policy | 365 } // namespace policy |
| OLD | NEW |