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> |
| 8 |
7 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
8 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
9 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
12 #include "base/sys_byteorder.h" | 14 #include "base/sys_byteorder.h" |
13 #include "base/values.h" | 15 #include "base/values.h" |
14 #include "base/win/registry.h" | 16 #include "base/win/registry.h" |
15 #include "components/policy/core/common/schema.h" | 17 #include "components/policy/core/common/schema.h" |
16 | 18 |
17 using base::win::RegistryKeyIterator; | 19 using base::win::RegistryKeyIterator; |
18 using base::win::RegistryValueIterator; | 20 using base::win::RegistryValueIterator; |
19 | 21 |
20 namespace policy { | 22 namespace policy { |
21 | 23 |
22 namespace { | 24 namespace { |
23 | 25 |
24 // Converts a value (as read from the registry) to meet |schema|, converting | 26 // 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 | 27 // types as necessary. Unconvertible types will show up as NULL values in the |
26 // result. | 28 // result. |
27 scoped_ptr<base::Value> ConvertValue(const base::Value& value, | 29 scoped_ptr<base::Value> ConvertValue(const base::Value& value, |
28 const Schema& schema) { | 30 const Schema& schema) { |
29 if (!schema.valid()) | 31 if (!schema.valid()) |
30 return make_scoped_ptr(value.DeepCopy()).Pass(); | 32 return value.CreateDeepCopy(); |
31 | 33 |
32 // If the type is good already, go with it. | 34 // If the type is good already, go with it. |
33 if (value.IsType(schema.type())) { | 35 if (value.IsType(schema.type())) { |
34 // Recurse for complex types. | 36 // Recurse for complex types. |
35 const base::DictionaryValue* dict = NULL; | 37 const base::DictionaryValue* dict = NULL; |
36 const base::ListValue* list = NULL; | 38 const base::ListValue* list = NULL; |
37 if (value.GetAsDictionary(&dict)) { | 39 if (value.GetAsDictionary(&dict)) { |
38 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 40 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
39 for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd(); | 41 for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd(); |
40 entry.Advance()) { | 42 entry.Advance()) { |
41 scoped_ptr<base::Value> converted = | 43 scoped_ptr<base::Value> converted = |
42 ConvertValue(entry.value(), schema.GetProperty(entry.key())); | 44 ConvertValue(entry.value(), schema.GetProperty(entry.key())); |
43 if (converted) | 45 if (converted) |
44 result->SetWithoutPathExpansion(entry.key(), converted.release()); | 46 result->SetWithoutPathExpansion(entry.key(), converted.release()); |
45 } | 47 } |
46 return result.Pass(); | 48 return std::move(result); |
47 } else if (value.GetAsList(&list)) { | 49 } else if (value.GetAsList(&list)) { |
48 scoped_ptr<base::ListValue> result(new base::ListValue()); | 50 scoped_ptr<base::ListValue> result(new base::ListValue()); |
49 for (base::ListValue::const_iterator entry(list->begin()); | 51 for (base::ListValue::const_iterator entry(list->begin()); |
50 entry != list->end(); ++entry) { | 52 entry != list->end(); ++entry) { |
51 scoped_ptr<base::Value> converted = | 53 scoped_ptr<base::Value> converted = |
52 ConvertValue(**entry, schema.GetItems()); | 54 ConvertValue(**entry, schema.GetItems()); |
53 if (converted) | 55 if (converted) |
54 result->Append(converted.release()); | 56 result->Append(converted.release()); |
55 } | 57 } |
56 return result.Pass(); | 58 return std::move(result); |
57 } | 59 } |
58 return make_scoped_ptr(value.DeepCopy()); | 60 return value.CreateDeepCopy(); |
59 } | 61 } |
60 | 62 |
61 // Else, do some conversions to map windows registry data types to JSON types. | 63 // Else, do some conversions to map windows registry data types to JSON types. |
62 std::string string_value; | 64 std::string string_value; |
63 int int_value = 0; | 65 int int_value = 0; |
64 switch (schema.type()) { | 66 switch (schema.type()) { |
65 case base::Value::TYPE_NULL: { | 67 case base::Value::TYPE_NULL: { |
66 return base::Value::CreateNullValue(); | 68 return base::Value::CreateNullValue(); |
67 } | 69 } |
68 case base::Value::TYPE_BOOLEAN: { | 70 case base::Value::TYPE_BOOLEAN: { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 scoped_ptr<base::ListValue> result(new base::ListValue()); | 103 scoped_ptr<base::ListValue> result(new base::ListValue()); |
102 for (int i = 1; ; ++i) { | 104 for (int i = 1; ; ++i) { |
103 const base::Value* entry = NULL; | 105 const base::Value* entry = NULL; |
104 if (!dict->Get(base::IntToString(i), &entry)) | 106 if (!dict->Get(base::IntToString(i), &entry)) |
105 break; | 107 break; |
106 scoped_ptr<base::Value> converted = | 108 scoped_ptr<base::Value> converted = |
107 ConvertValue(*entry, schema.GetItems()); | 109 ConvertValue(*entry, schema.GetItems()); |
108 if (converted) | 110 if (converted) |
109 result->Append(converted.release()); | 111 result->Append(converted.release()); |
110 } | 112 } |
111 return result.Pass(); | 113 return std::move(result); |
112 } | 114 } |
113 // Fall through in order to accept lists encoded as JSON strings. | 115 // Fall through in order to accept lists encoded as JSON strings. |
114 } | 116 } |
115 case base::Value::TYPE_DICTIONARY: { | 117 case base::Value::TYPE_DICTIONARY: { |
116 // Dictionaries may be encoded as JSON strings. | 118 // Dictionaries may be encoded as JSON strings. |
117 if (value.GetAsString(&string_value)) { | 119 if (value.GetAsString(&string_value)) { |
118 scoped_ptr<base::Value> result = base::JSONReader::Read(string_value); | 120 scoped_ptr<base::Value> result = base::JSONReader::Read(string_value); |
119 if (result && result->IsType(schema.type())) | 121 if (result && result->IsType(schema.type())) |
120 return result.Pass(); | 122 return result; |
121 } | 123 } |
122 break; | 124 break; |
123 } | 125 } |
124 case base::Value::TYPE_STRING: | 126 case base::Value::TYPE_STRING: |
125 case base::Value::TYPE_BINARY: | 127 case base::Value::TYPE_BINARY: |
126 // No conversion possible. | 128 // No conversion possible. |
127 break; | 129 break; |
128 } | 130 } |
129 | 131 |
130 LOG(WARNING) << "Failed to convert " << value.GetType() | 132 LOG(WARNING) << "Failed to convert " << value.GetType() |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 entry = dict.release(); | 170 entry = dict.release(); |
169 } | 171 } |
170 | 172 |
171 scoped_ptr<RegistryDict> RegistryDict::RemoveKey(const std::string& name) { | 173 scoped_ptr<RegistryDict> RegistryDict::RemoveKey(const std::string& name) { |
172 scoped_ptr<RegistryDict> result; | 174 scoped_ptr<RegistryDict> result; |
173 KeyMap::iterator entry = keys_.find(name); | 175 KeyMap::iterator entry = keys_.find(name); |
174 if (entry != keys_.end()) { | 176 if (entry != keys_.end()) { |
175 result.reset(entry->second); | 177 result.reset(entry->second); |
176 keys_.erase(entry); | 178 keys_.erase(entry); |
177 } | 179 } |
178 return result.Pass(); | 180 return result; |
179 } | 181 } |
180 | 182 |
181 void RegistryDict::ClearKeys() { | 183 void RegistryDict::ClearKeys() { |
182 STLDeleteValues(&keys_); | 184 STLDeleteValues(&keys_); |
183 } | 185 } |
184 | 186 |
185 base::Value* RegistryDict::GetValue(const std::string& name) { | 187 base::Value* RegistryDict::GetValue(const std::string& name) { |
186 ValueMap::iterator entry = values_.find(name); | 188 ValueMap::iterator entry = values_.find(name); |
187 return entry != values_.end() ? entry->second : NULL; | 189 return entry != values_.end() ? entry->second : NULL; |
188 } | 190 } |
(...skipping 15 matching lines...) Expand all Loading... |
204 entry = dict.release(); | 206 entry = dict.release(); |
205 } | 207 } |
206 | 208 |
207 scoped_ptr<base::Value> RegistryDict::RemoveValue(const std::string& name) { | 209 scoped_ptr<base::Value> RegistryDict::RemoveValue(const std::string& name) { |
208 scoped_ptr<base::Value> result; | 210 scoped_ptr<base::Value> result; |
209 ValueMap::iterator entry = values_.find(name); | 211 ValueMap::iterator entry = values_.find(name); |
210 if (entry != values_.end()) { | 212 if (entry != values_.end()) { |
211 result.reset(entry->second); | 213 result.reset(entry->second); |
212 values_.erase(entry); | 214 values_.erase(entry); |
213 } | 215 } |
214 return result.Pass(); | 216 return result; |
215 } | 217 } |
216 | 218 |
217 void RegistryDict::ClearValues() { | 219 void RegistryDict::ClearValues() { |
218 STLDeleteValues(&values_); | 220 STLDeleteValues(&values_); |
219 } | 221 } |
220 | 222 |
221 void RegistryDict::Merge(const RegistryDict& other) { | 223 void RegistryDict::Merge(const RegistryDict& other) { |
222 for (KeyMap::const_iterator entry(other.keys_.begin()); | 224 for (KeyMap::const_iterator entry(other.keys_.begin()); |
223 entry != other.keys_.end(); ++entry) { | 225 entry != other.keys_.end(); ++entry) { |
224 RegistryDict*& subdict = keys_[entry->first]; | 226 RegistryDict*& subdict = keys_[entry->first]; |
225 if (!subdict) | 227 if (!subdict) |
226 subdict = new RegistryDict(); | 228 subdict = new RegistryDict(); |
227 subdict->Merge(*entry->second); | 229 subdict->Merge(*entry->second); |
228 } | 230 } |
229 | 231 |
230 for (ValueMap::const_iterator entry(other.values_.begin()); | 232 for (ValueMap::const_iterator entry(other.values_.begin()); |
231 entry != other.values_.end(); ++entry) { | 233 entry != other.values_.end(); ++entry) { |
232 SetValue(entry->first, make_scoped_ptr(entry->second->DeepCopy()).Pass()); | 234 SetValue(entry->first, entry->second->CreateDeepCopy()); |
233 } | 235 } |
234 } | 236 } |
235 | 237 |
236 void RegistryDict::Swap(RegistryDict* other) { | 238 void RegistryDict::Swap(RegistryDict* other) { |
237 keys_.swap(other->keys_); | 239 keys_.swap(other->keys_); |
238 values_.swap(other->values_); | 240 values_.swap(other->values_); |
239 } | 241 } |
240 | 242 |
241 void RegistryDict::ReadRegistry(HKEY hive, const base::string16& root) { | 243 void RegistryDict::ReadRegistry(HKEY hive, const base::string16& root) { |
242 ClearKeys(); | 244 ClearKeys(); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 LOG(WARNING) << "Failed to read hive " << hive << " at " | 281 LOG(WARNING) << "Failed to read hive " << hive << " at " |
280 << root << "\\" << name | 282 << root << "\\" << name |
281 << " type " << it.Type(); | 283 << " type " << it.Type(); |
282 } | 284 } |
283 | 285 |
284 // Recurse for all subkeys. | 286 // Recurse for all subkeys. |
285 for (RegistryKeyIterator it(hive, root.c_str()); it.Valid(); ++it) { | 287 for (RegistryKeyIterator it(hive, root.c_str()); it.Valid(); ++it) { |
286 std::string name(base::UTF16ToUTF8(it.Name())); | 288 std::string name(base::UTF16ToUTF8(it.Name())); |
287 scoped_ptr<RegistryDict> subdict(new RegistryDict()); | 289 scoped_ptr<RegistryDict> subdict(new RegistryDict()); |
288 subdict->ReadRegistry(hive, root + L"\\" + it.Name()); | 290 subdict->ReadRegistry(hive, root + L"\\" + it.Name()); |
289 SetKey(name, subdict.Pass()); | 291 SetKey(name, std::move(subdict)); |
290 } | 292 } |
291 } | 293 } |
292 | 294 |
293 scoped_ptr<base::Value> RegistryDict::ConvertToJSON( | 295 scoped_ptr<base::Value> RegistryDict::ConvertToJSON( |
294 const Schema& schema) const { | 296 const Schema& schema) const { |
295 base::Value::Type type = | 297 base::Value::Type type = |
296 schema.valid() ? schema.type() : base::Value::TYPE_DICTIONARY; | 298 schema.valid() ? schema.type() : base::Value::TYPE_DICTIONARY; |
297 switch (type) { | 299 switch (type) { |
298 case base::Value::TYPE_DICTIONARY: { | 300 case base::Value::TYPE_DICTIONARY: { |
299 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 301 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
300 for (RegistryDict::ValueMap::const_iterator entry(values_.begin()); | 302 for (RegistryDict::ValueMap::const_iterator entry(values_.begin()); |
301 entry != values_.end(); ++entry) { | 303 entry != values_.end(); ++entry) { |
302 Schema subschema = | 304 Schema subschema = |
303 schema.valid() ? schema.GetProperty(entry->first) : Schema(); | 305 schema.valid() ? schema.GetProperty(entry->first) : Schema(); |
304 scoped_ptr<base::Value> converted = | 306 scoped_ptr<base::Value> converted = |
305 ConvertValue(*entry->second, subschema); | 307 ConvertValue(*entry->second, subschema); |
306 if (converted) | 308 if (converted) |
307 result->SetWithoutPathExpansion(entry->first, converted.release()); | 309 result->SetWithoutPathExpansion(entry->first, converted.release()); |
308 } | 310 } |
309 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); | 311 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); |
310 entry != keys_.end(); ++entry) { | 312 entry != keys_.end(); ++entry) { |
311 Schema subschema = | 313 Schema subschema = |
312 schema.valid() ? schema.GetProperty(entry->first) : Schema(); | 314 schema.valid() ? schema.GetProperty(entry->first) : Schema(); |
313 scoped_ptr<base::Value> converted = | 315 scoped_ptr<base::Value> converted = |
314 entry->second->ConvertToJSON(subschema); | 316 entry->second->ConvertToJSON(subschema); |
315 if (converted) | 317 if (converted) |
316 result->SetWithoutPathExpansion(entry->first, converted.release()); | 318 result->SetWithoutPathExpansion(entry->first, converted.release()); |
317 } | 319 } |
318 return result.Pass(); | 320 return std::move(result); |
319 } | 321 } |
320 case base::Value::TYPE_LIST: { | 322 case base::Value::TYPE_LIST: { |
321 scoped_ptr<base::ListValue> result(new base::ListValue()); | 323 scoped_ptr<base::ListValue> result(new base::ListValue()); |
322 Schema item_schema = schema.valid() ? schema.GetItems() : Schema(); | 324 Schema item_schema = schema.valid() ? schema.GetItems() : Schema(); |
323 for (int i = 1; ; ++i) { | 325 for (int i = 1; ; ++i) { |
324 const std::string name(base::IntToString(i)); | 326 const std::string name(base::IntToString(i)); |
325 const RegistryDict* key = GetKey(name); | 327 const RegistryDict* key = GetKey(name); |
326 if (key) { | 328 if (key) { |
327 scoped_ptr<base::Value> converted = key->ConvertToJSON(item_schema); | 329 scoped_ptr<base::Value> converted = key->ConvertToJSON(item_schema); |
328 if (converted) | 330 if (converted) |
329 result->Append(converted.release()); | 331 result->Append(converted.release()); |
330 continue; | 332 continue; |
331 } | 333 } |
332 const base::Value* value = GetValue(name); | 334 const base::Value* value = GetValue(name); |
333 if (value) { | 335 if (value) { |
334 scoped_ptr<base::Value> converted = ConvertValue(*value, item_schema); | 336 scoped_ptr<base::Value> converted = ConvertValue(*value, item_schema); |
335 if (converted) | 337 if (converted) |
336 result->Append(converted.release()); | 338 result->Append(converted.release()); |
337 continue; | 339 continue; |
338 } | 340 } |
339 break; | 341 break; |
340 } | 342 } |
341 return result.Pass(); | 343 return std::move(result); |
342 } | 344 } |
343 default: | 345 default: |
344 LOG(WARNING) << "Can't convert registry key to schema type " << type; | 346 LOG(WARNING) << "Can't convert registry key to schema type " << type; |
345 } | 347 } |
346 | 348 |
347 return nullptr; | 349 return nullptr; |
348 } | 350 } |
349 | 351 |
350 } // namespace policy | 352 } // namespace policy |
OLD | NEW |