Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: components/policy/core/common/registry_dict_win.cc

Issue 1902633006: Convert //components/policy from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments and use namespace alias Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/stl_util.h" 10 #include "base/stl_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 // 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
27 // 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
28 // result. 28 // result.
29 scoped_ptr<base::Value> ConvertValue(const base::Value& value, 29 std::unique_ptr<base::Value> ConvertValue(const base::Value& value,
30 const Schema& schema) { 30 const Schema& schema) {
31 if (!schema.valid()) 31 if (!schema.valid())
32 return value.CreateDeepCopy(); 32 return value.CreateDeepCopy();
33 33
34 // If the type is good already, go with it. 34 // If the type is good already, go with it.
35 if (value.IsType(schema.type())) { 35 if (value.IsType(schema.type())) {
36 // Recurse for complex types. 36 // Recurse for complex types.
37 const base::DictionaryValue* dict = NULL; 37 const base::DictionaryValue* dict = NULL;
38 const base::ListValue* list = NULL; 38 const base::ListValue* list = NULL;
39 if (value.GetAsDictionary(&dict)) { 39 if (value.GetAsDictionary(&dict)) {
40 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); 40 std::unique_ptr<base::DictionaryValue> result(
41 new base::DictionaryValue());
41 for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd(); 42 for (base::DictionaryValue::Iterator entry(*dict); !entry.IsAtEnd();
42 entry.Advance()) { 43 entry.Advance()) {
43 scoped_ptr<base::Value> converted = 44 std::unique_ptr<base::Value> converted =
44 ConvertValue(entry.value(), schema.GetProperty(entry.key())); 45 ConvertValue(entry.value(), schema.GetProperty(entry.key()));
45 if (converted) 46 if (converted)
46 result->SetWithoutPathExpansion(entry.key(), converted.release()); 47 result->SetWithoutPathExpansion(entry.key(), converted.release());
47 } 48 }
48 return std::move(result); 49 return std::move(result);
49 } else if (value.GetAsList(&list)) { 50 } else if (value.GetAsList(&list)) {
50 scoped_ptr<base::ListValue> result(new base::ListValue()); 51 std::unique_ptr<base::ListValue> result(new base::ListValue());
51 for (base::ListValue::const_iterator entry(list->begin()); 52 for (base::ListValue::const_iterator entry(list->begin());
52 entry != list->end(); ++entry) { 53 entry != list->end(); ++entry) {
53 scoped_ptr<base::Value> converted = 54 std::unique_ptr<base::Value> converted =
54 ConvertValue(**entry, schema.GetItems()); 55 ConvertValue(**entry, schema.GetItems());
55 if (converted) 56 if (converted)
56 result->Append(converted.release()); 57 result->Append(converted.release());
57 } 58 }
58 return std::move(result); 59 return std::move(result);
59 } 60 }
60 return value.CreateDeepCopy(); 61 return value.CreateDeepCopy();
61 } 62 }
62 63
63 // Else, do some conversions to map windows registry data types to JSON types. 64 // Else, do some conversions to map windows registry data types to JSON types.
64 std::string string_value; 65 std::string string_value;
65 int int_value = 0; 66 int int_value = 0;
66 switch (schema.type()) { 67 switch (schema.type()) {
67 case base::Value::TYPE_NULL: { 68 case base::Value::TYPE_NULL: {
68 return base::Value::CreateNullValue(); 69 return base::Value::CreateNullValue();
69 } 70 }
70 case base::Value::TYPE_BOOLEAN: { 71 case base::Value::TYPE_BOOLEAN: {
71 // Accept booleans encoded as either string or integer. 72 // Accept booleans encoded as either string or integer.
72 if (value.GetAsInteger(&int_value) || 73 if (value.GetAsInteger(&int_value) ||
73 (value.GetAsString(&string_value) && 74 (value.GetAsString(&string_value) &&
74 base::StringToInt(string_value, &int_value))) { 75 base::StringToInt(string_value, &int_value))) {
75 return scoped_ptr<base::Value>( 76 return std::unique_ptr<base::Value>(
76 new base::FundamentalValue(int_value != 0)); 77 new base::FundamentalValue(int_value != 0));
77 } 78 }
78 break; 79 break;
79 } 80 }
80 case base::Value::TYPE_INTEGER: { 81 case base::Value::TYPE_INTEGER: {
81 // Integers may be string-encoded. 82 // Integers may be string-encoded.
82 if (value.GetAsString(&string_value) && 83 if (value.GetAsString(&string_value) &&
83 base::StringToInt(string_value, &int_value)) { 84 base::StringToInt(string_value, &int_value)) {
84 return scoped_ptr<base::Value>(new base::FundamentalValue(int_value)); 85 return std::unique_ptr<base::Value>(
86 new base::FundamentalValue(int_value));
85 } 87 }
86 break; 88 break;
87 } 89 }
88 case base::Value::TYPE_DOUBLE: { 90 case base::Value::TYPE_DOUBLE: {
89 // Doubles may be string-encoded or integer-encoded. 91 // Doubles may be string-encoded or integer-encoded.
90 double double_value = 0; 92 double double_value = 0;
91 if (value.GetAsDouble(&double_value) || 93 if (value.GetAsDouble(&double_value) ||
92 (value.GetAsString(&string_value) && 94 (value.GetAsString(&string_value) &&
93 base::StringToDouble(string_value, &double_value))) { 95 base::StringToDouble(string_value, &double_value))) {
94 return scoped_ptr<base::Value>( 96 return std::unique_ptr<base::Value>(
95 new base::FundamentalValue(double_value)); 97 new base::FundamentalValue(double_value));
96 } 98 }
97 break; 99 break;
98 } 100 }
99 case base::Value::TYPE_LIST: { 101 case base::Value::TYPE_LIST: {
100 // Lists are encoded as subkeys with numbered value in the registry. 102 // Lists are encoded as subkeys with numbered value in the registry.
101 const base::DictionaryValue* dict = NULL; 103 const base::DictionaryValue* dict = NULL;
102 if (value.GetAsDictionary(&dict)) { 104 if (value.GetAsDictionary(&dict)) {
103 scoped_ptr<base::ListValue> result(new base::ListValue()); 105 std::unique_ptr<base::ListValue> result(new base::ListValue());
104 for (int i = 1; ; ++i) { 106 for (int i = 1; ; ++i) {
105 const base::Value* entry = NULL; 107 const base::Value* entry = NULL;
106 if (!dict->Get(base::IntToString(i), &entry)) 108 if (!dict->Get(base::IntToString(i), &entry))
107 break; 109 break;
108 scoped_ptr<base::Value> converted = 110 std::unique_ptr<base::Value> converted =
109 ConvertValue(*entry, schema.GetItems()); 111 ConvertValue(*entry, schema.GetItems());
110 if (converted) 112 if (converted)
111 result->Append(converted.release()); 113 result->Append(converted.release());
112 } 114 }
113 return std::move(result); 115 return std::move(result);
114 } 116 }
115 // Fall through in order to accept lists encoded as JSON strings. 117 // Fall through in order to accept lists encoded as JSON strings.
116 } 118 }
117 case base::Value::TYPE_DICTIONARY: { 119 case base::Value::TYPE_DICTIONARY: {
118 // Dictionaries may be encoded as JSON strings. 120 // Dictionaries may be encoded as JSON strings.
119 if (value.GetAsString(&string_value)) { 121 if (value.GetAsString(&string_value)) {
120 scoped_ptr<base::Value> result = base::JSONReader::Read(string_value); 122 std::unique_ptr<base::Value> result =
123 base::JSONReader::Read(string_value);
121 if (result && result->IsType(schema.type())) 124 if (result && result->IsType(schema.type()))
122 return result; 125 return result;
123 } 126 }
124 break; 127 break;
125 } 128 }
126 case base::Value::TYPE_STRING: 129 case base::Value::TYPE_STRING:
127 case base::Value::TYPE_BINARY: 130 case base::Value::TYPE_BINARY:
128 // No conversion possible. 131 // No conversion possible.
129 break; 132 break;
130 } 133 }
(...skipping 21 matching lines...) Expand all
152 KeyMap::iterator entry = keys_.find(name); 155 KeyMap::iterator entry = keys_.find(name);
153 return entry != keys_.end() ? entry->second : NULL; 156 return entry != keys_.end() ? entry->second : NULL;
154 } 157 }
155 158
156 const RegistryDict* RegistryDict::GetKey(const std::string& name) const { 159 const RegistryDict* RegistryDict::GetKey(const std::string& name) const {
157 KeyMap::const_iterator entry = keys_.find(name); 160 KeyMap::const_iterator entry = keys_.find(name);
158 return entry != keys_.end() ? entry->second : NULL; 161 return entry != keys_.end() ? entry->second : NULL;
159 } 162 }
160 163
161 void RegistryDict::SetKey(const std::string& name, 164 void RegistryDict::SetKey(const std::string& name,
162 scoped_ptr<RegistryDict> dict) { 165 std::unique_ptr<RegistryDict> dict) {
163 if (!dict) { 166 if (!dict) {
164 RemoveKey(name); 167 RemoveKey(name);
165 return; 168 return;
166 } 169 }
167 170
168 RegistryDict*& entry = keys_[name]; 171 RegistryDict*& entry = keys_[name];
169 delete entry; 172 delete entry;
170 entry = dict.release(); 173 entry = dict.release();
171 } 174 }
172 175
173 scoped_ptr<RegistryDict> RegistryDict::RemoveKey(const std::string& name) { 176 std::unique_ptr<RegistryDict> RegistryDict::RemoveKey(const std::string& name) {
174 scoped_ptr<RegistryDict> result; 177 std::unique_ptr<RegistryDict> result;
175 KeyMap::iterator entry = keys_.find(name); 178 KeyMap::iterator entry = keys_.find(name);
176 if (entry != keys_.end()) { 179 if (entry != keys_.end()) {
177 result.reset(entry->second); 180 result.reset(entry->second);
178 keys_.erase(entry); 181 keys_.erase(entry);
179 } 182 }
180 return result; 183 return result;
181 } 184 }
182 185
183 void RegistryDict::ClearKeys() { 186 void RegistryDict::ClearKeys() {
184 STLDeleteValues(&keys_); 187 STLDeleteValues(&keys_);
185 } 188 }
186 189
187 base::Value* RegistryDict::GetValue(const std::string& name) { 190 base::Value* RegistryDict::GetValue(const std::string& name) {
188 ValueMap::iterator entry = values_.find(name); 191 ValueMap::iterator entry = values_.find(name);
189 return entry != values_.end() ? entry->second : NULL; 192 return entry != values_.end() ? entry->second : NULL;
190 } 193 }
191 194
192 const base::Value* RegistryDict::GetValue(const std::string& name) const { 195 const base::Value* RegistryDict::GetValue(const std::string& name) const {
193 ValueMap::const_iterator entry = values_.find(name); 196 ValueMap::const_iterator entry = values_.find(name);
194 return entry != values_.end() ? entry->second : NULL; 197 return entry != values_.end() ? entry->second : NULL;
195 } 198 }
196 199
197 void RegistryDict::SetValue(const std::string& name, 200 void RegistryDict::SetValue(const std::string& name,
198 scoped_ptr<base::Value> dict) { 201 std::unique_ptr<base::Value> dict) {
199 if (!dict) { 202 if (!dict) {
200 RemoveValue(name); 203 RemoveValue(name);
201 return; 204 return;
202 } 205 }
203 206
204 base::Value*& entry = values_[name]; 207 base::Value*& entry = values_[name];
205 delete entry; 208 delete entry;
206 entry = dict.release(); 209 entry = dict.release();
207 } 210 }
208 211
209 scoped_ptr<base::Value> RegistryDict::RemoveValue(const std::string& name) { 212 std::unique_ptr<base::Value> RegistryDict::RemoveValue(
210 scoped_ptr<base::Value> result; 213 const std::string& name) {
214 std::unique_ptr<base::Value> result;
211 ValueMap::iterator entry = values_.find(name); 215 ValueMap::iterator entry = values_.find(name);
212 if (entry != values_.end()) { 216 if (entry != values_.end()) {
213 result.reset(entry->second); 217 result.reset(entry->second);
214 values_.erase(entry); 218 values_.erase(entry);
215 } 219 }
216 return result; 220 return result;
217 } 221 }
218 222
219 void RegistryDict::ClearValues() { 223 void RegistryDict::ClearValues() {
220 STLDeleteValues(&values_); 224 STLDeleteValues(&values_);
(...skipping 22 matching lines...) Expand all
243 void RegistryDict::ReadRegistry(HKEY hive, const base::string16& root) { 247 void RegistryDict::ReadRegistry(HKEY hive, const base::string16& root) {
244 ClearKeys(); 248 ClearKeys();
245 ClearValues(); 249 ClearValues();
246 250
247 // First, read all the values of the key. 251 // First, read all the values of the key.
248 for (RegistryValueIterator it(hive, root.c_str()); it.Valid(); ++it) { 252 for (RegistryValueIterator it(hive, root.c_str()); it.Valid(); ++it) {
249 const std::string name = base::UTF16ToUTF8(it.Name()); 253 const std::string name = base::UTF16ToUTF8(it.Name());
250 switch (it.Type()) { 254 switch (it.Type()) {
251 case REG_SZ: 255 case REG_SZ:
252 case REG_EXPAND_SZ: 256 case REG_EXPAND_SZ:
253 SetValue(name, 257 SetValue(name, std::unique_ptr<base::Value>(new base::StringValue(
254 scoped_ptr<base::Value>( 258 base::UTF16ToUTF8(it.Value()))));
255 new base::StringValue(base::UTF16ToUTF8(it.Value()))));
256 continue; 259 continue;
257 case REG_DWORD_LITTLE_ENDIAN: 260 case REG_DWORD_LITTLE_ENDIAN:
258 case REG_DWORD_BIG_ENDIAN: 261 case REG_DWORD_BIG_ENDIAN:
259 if (it.ValueSize() == sizeof(DWORD)) { 262 if (it.ValueSize() == sizeof(DWORD)) {
260 DWORD dword_value = *(reinterpret_cast<const DWORD*>(it.Value())); 263 DWORD dword_value = *(reinterpret_cast<const DWORD*>(it.Value()));
261 if (it.Type() == REG_DWORD_BIG_ENDIAN) 264 if (it.Type() == REG_DWORD_BIG_ENDIAN)
262 dword_value = base::NetToHost32(dword_value); 265 dword_value = base::NetToHost32(dword_value);
263 else 266 else
264 dword_value = base::ByteSwapToLE32(dword_value); 267 dword_value = base::ByteSwapToLE32(dword_value);
265 SetValue(name, 268 SetValue(name,
266 scoped_ptr<base::Value>(new base::FundamentalValue( 269 std::unique_ptr<base::Value>(new base::FundamentalValue(
267 static_cast<int>(dword_value)))); 270 static_cast<int>(dword_value))));
268 continue; 271 continue;
269 } 272 }
270 case REG_NONE: 273 case REG_NONE:
271 case REG_LINK: 274 case REG_LINK:
272 case REG_MULTI_SZ: 275 case REG_MULTI_SZ:
273 case REG_RESOURCE_LIST: 276 case REG_RESOURCE_LIST:
274 case REG_FULL_RESOURCE_DESCRIPTOR: 277 case REG_FULL_RESOURCE_DESCRIPTOR:
275 case REG_RESOURCE_REQUIREMENTS_LIST: 278 case REG_RESOURCE_REQUIREMENTS_LIST:
276 case REG_QWORD_LITTLE_ENDIAN: 279 case REG_QWORD_LITTLE_ENDIAN:
277 // Unsupported type, message gets logged below. 280 // Unsupported type, message gets logged below.
278 break; 281 break;
279 } 282 }
280 283
281 LOG(WARNING) << "Failed to read hive " << hive << " at " 284 LOG(WARNING) << "Failed to read hive " << hive << " at "
282 << root << "\\" << name 285 << root << "\\" << name
283 << " type " << it.Type(); 286 << " type " << it.Type();
284 } 287 }
285 288
286 // Recurse for all subkeys. 289 // Recurse for all subkeys.
287 for (RegistryKeyIterator it(hive, root.c_str()); it.Valid(); ++it) { 290 for (RegistryKeyIterator it(hive, root.c_str()); it.Valid(); ++it) {
288 std::string name(base::UTF16ToUTF8(it.Name())); 291 std::string name(base::UTF16ToUTF8(it.Name()));
289 scoped_ptr<RegistryDict> subdict(new RegistryDict()); 292 std::unique_ptr<RegistryDict> subdict(new RegistryDict());
290 subdict->ReadRegistry(hive, root + L"\\" + it.Name()); 293 subdict->ReadRegistry(hive, root + L"\\" + it.Name());
291 SetKey(name, std::move(subdict)); 294 SetKey(name, std::move(subdict));
292 } 295 }
293 } 296 }
294 297
295 scoped_ptr<base::Value> RegistryDict::ConvertToJSON( 298 std::unique_ptr<base::Value> RegistryDict::ConvertToJSON(
296 const Schema& schema) const { 299 const Schema& schema) const {
297 base::Value::Type type = 300 base::Value::Type type =
298 schema.valid() ? schema.type() : base::Value::TYPE_DICTIONARY; 301 schema.valid() ? schema.type() : base::Value::TYPE_DICTIONARY;
299 switch (type) { 302 switch (type) {
300 case base::Value::TYPE_DICTIONARY: { 303 case base::Value::TYPE_DICTIONARY: {
301 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue()); 304 std::unique_ptr<base::DictionaryValue> result(
305 new base::DictionaryValue());
302 for (RegistryDict::ValueMap::const_iterator entry(values_.begin()); 306 for (RegistryDict::ValueMap::const_iterator entry(values_.begin());
303 entry != values_.end(); ++entry) { 307 entry != values_.end(); ++entry) {
304 Schema subschema = 308 Schema subschema =
305 schema.valid() ? schema.GetProperty(entry->first) : Schema(); 309 schema.valid() ? schema.GetProperty(entry->first) : Schema();
306 scoped_ptr<base::Value> converted = 310 std::unique_ptr<base::Value> converted =
307 ConvertValue(*entry->second, subschema); 311 ConvertValue(*entry->second, subschema);
308 if (converted) 312 if (converted)
309 result->SetWithoutPathExpansion(entry->first, converted.release()); 313 result->SetWithoutPathExpansion(entry->first, converted.release());
310 } 314 }
311 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin()); 315 for (RegistryDict::KeyMap::const_iterator entry(keys_.begin());
312 entry != keys_.end(); ++entry) { 316 entry != keys_.end(); ++entry) {
313 Schema subschema = 317 Schema subschema =
314 schema.valid() ? schema.GetProperty(entry->first) : Schema(); 318 schema.valid() ? schema.GetProperty(entry->first) : Schema();
315 scoped_ptr<base::Value> converted = 319 std::unique_ptr<base::Value> converted =
316 entry->second->ConvertToJSON(subschema); 320 entry->second->ConvertToJSON(subschema);
317 if (converted) 321 if (converted)
318 result->SetWithoutPathExpansion(entry->first, converted.release()); 322 result->SetWithoutPathExpansion(entry->first, converted.release());
319 } 323 }
320 return std::move(result); 324 return std::move(result);
321 } 325 }
322 case base::Value::TYPE_LIST: { 326 case base::Value::TYPE_LIST: {
323 scoped_ptr<base::ListValue> result(new base::ListValue()); 327 std::unique_ptr<base::ListValue> result(new base::ListValue());
324 Schema item_schema = schema.valid() ? schema.GetItems() : Schema(); 328 Schema item_schema = schema.valid() ? schema.GetItems() : Schema();
325 for (int i = 1; ; ++i) { 329 for (int i = 1; ; ++i) {
326 const std::string name(base::IntToString(i)); 330 const std::string name(base::IntToString(i));
327 const RegistryDict* key = GetKey(name); 331 const RegistryDict* key = GetKey(name);
328 if (key) { 332 if (key) {
329 scoped_ptr<base::Value> converted = key->ConvertToJSON(item_schema); 333 std::unique_ptr<base::Value> converted =
334 key->ConvertToJSON(item_schema);
330 if (converted) 335 if (converted)
331 result->Append(converted.release()); 336 result->Append(converted.release());
332 continue; 337 continue;
333 } 338 }
334 const base::Value* value = GetValue(name); 339 const base::Value* value = GetValue(name);
335 if (value) { 340 if (value) {
336 scoped_ptr<base::Value> converted = ConvertValue(*value, item_schema); 341 std::unique_ptr<base::Value> converted =
342 ConvertValue(*value, item_schema);
337 if (converted) 343 if (converted)
338 result->Append(converted.release()); 344 result->Append(converted.release());
339 continue; 345 continue;
340 } 346 }
341 break; 347 break;
342 } 348 }
343 return std::move(result); 349 return std::move(result);
344 } 350 }
345 default: 351 default:
346 LOG(WARNING) << "Can't convert registry key to schema type " << type; 352 LOG(WARNING) << "Can't convert registry key to schema type " << type;
347 } 353 }
348 354
349 return nullptr; 355 return nullptr;
350 } 356 }
351 357
352 } // namespace policy 358 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/common/registry_dict_win.h ('k') | components/policy/core/common/registry_dict_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698