OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chromeos/network/onc/onc_mapper.h" | 5 #include "chromeos/network/onc/onc_mapper.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chromeos/network/onc/onc_signature.h" | 9 #include "chromeos/network/onc/onc_signature.h" |
10 | 10 |
11 namespace chromeos { | 11 namespace chromeos { |
12 namespace onc { | 12 namespace onc { |
13 | 13 |
14 Mapper::Mapper() { | 14 Mapper::Mapper() { |
15 } | 15 } |
16 | 16 |
17 Mapper::~Mapper() { | 17 Mapper::~Mapper() { |
18 } | 18 } |
19 | 19 |
20 scoped_ptr<base::Value> Mapper::MapValue( | 20 scoped_ptr<base::Value> Mapper::MapValue(const OncValueSignature& signature, |
21 const OncValueSignature& signature, | 21 const base::Value& onc_value, |
22 const base::Value& onc_value) { | 22 bool* error) { |
23 scoped_ptr<base::Value> result_value; | 23 scoped_ptr<base::Value> result_value; |
24 switch (onc_value.GetType()) { | 24 switch (onc_value.GetType()) { |
25 case base::Value::TYPE_DICTIONARY: { | 25 case base::Value::TYPE_DICTIONARY: { |
26 const base::DictionaryValue* dict = NULL; | 26 const base::DictionaryValue* dict = NULL; |
27 onc_value.GetAsDictionary(&dict); | 27 onc_value.GetAsDictionary(&dict); |
28 result_value = MapObject(signature, *dict); | 28 result_value = MapObject(signature, *dict, error); |
29 break; | 29 break; |
30 } | 30 } |
31 case base::Value::TYPE_LIST: { | 31 case base::Value::TYPE_LIST: { |
32 const base::ListValue* list = NULL; | 32 const base::ListValue* list = NULL; |
33 onc_value.GetAsList(&list); | 33 onc_value.GetAsList(&list); |
34 bool nested_error_occured = false; | 34 result_value = MapArray(signature, *list, error); |
35 result_value = MapArray(signature, *list, &nested_error_occured); | |
36 if (nested_error_occured) | |
37 result_value.reset(); | |
38 break; | 35 break; |
39 } | 36 } |
40 default: { | 37 default: { |
41 result_value = MapPrimitive(signature, onc_value); | 38 result_value = MapPrimitive(signature, onc_value, error); |
42 break; | 39 break; |
43 } | 40 } |
44 } | 41 } |
45 | 42 |
46 return result_value.Pass(); | 43 return result_value.Pass(); |
47 } | 44 } |
48 | 45 |
49 scoped_ptr<base::DictionaryValue> Mapper::MapObject( | 46 scoped_ptr<base::DictionaryValue> Mapper::MapObject( |
50 const OncValueSignature& signature, | 47 const OncValueSignature& signature, |
51 const base::DictionaryValue& onc_object) { | 48 const base::DictionaryValue& onc_object, |
49 bool* error) { | |
52 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); | 50 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); |
53 | 51 |
54 bool found_unknown_field = false; | 52 bool found_unknown_field = false; |
55 bool nested_error_occured = false; | 53 MapFields(signature, onc_object, &found_unknown_field, error, result.get()); |
56 MapFields(signature, onc_object, &found_unknown_field, &nested_error_occured, | 54 if (found_unknown_field) |
57 result.get()); | 55 *error = true; |
58 if (!nested_error_occured && !found_unknown_field) | 56 return result.Pass(); |
59 return result.Pass(); | |
60 else | |
61 return scoped_ptr<base::DictionaryValue>(); | |
62 } | 57 } |
63 | 58 |
64 scoped_ptr<base::Value> Mapper::MapPrimitive( | 59 scoped_ptr<base::Value> Mapper::MapPrimitive(const OncValueSignature& signature, |
65 const OncValueSignature& signature, | 60 const base::Value& onc_primitive, |
66 const base::Value& onc_primitive) { | 61 bool* error) { |
67 return make_scoped_ptr(onc_primitive.DeepCopy()); | 62 return make_scoped_ptr(onc_primitive.DeepCopy()); |
68 } | 63 } |
69 | 64 |
70 void Mapper::MapFields( | 65 void Mapper::MapFields(const OncValueSignature& object_signature, |
71 const OncValueSignature& object_signature, | 66 const base::DictionaryValue& onc_object, |
72 const base::DictionaryValue& onc_object, | 67 bool* found_unknown_field, |
73 bool* found_unknown_field, | 68 bool* nested_error, |
74 bool* nested_error_occured, | 69 base::DictionaryValue* result) { |
75 base::DictionaryValue* result) { | |
76 | 70 |
77 for (base::DictionaryValue::Iterator it(onc_object); it.HasNext(); | 71 for (base::DictionaryValue::Iterator it(onc_object); it.HasNext(); |
78 it.Advance()) { | 72 it.Advance()) { |
79 bool current_field_unknown = false; | 73 bool current_field_unknown = false; |
80 scoped_ptr<base::Value> result_value = MapField( | 74 scoped_ptr<base::Value> result_value = MapField(it.key(), |
81 it.key(), object_signature, it.value(), ¤t_field_unknown); | 75 object_signature, |
76 it.value(), | |
77 ¤t_field_unknown, | |
78 nested_error); | |
82 | 79 |
83 if (current_field_unknown) | 80 if (current_field_unknown) |
84 *found_unknown_field = true; | 81 *found_unknown_field = true; |
85 else if (result_value.get() != NULL) | 82 else if (result_value.get() != NULL) |
86 result->SetWithoutPathExpansion(it.key(), result_value.release()); | 83 result->SetWithoutPathExpansion(it.key(), result_value.release()); |
87 else | 84 else |
88 *nested_error_occured = true; | 85 DCHECK(*nested_error); |
89 } | 86 } |
90 } | 87 } |
91 | 88 |
92 scoped_ptr<base::Value> Mapper::MapField( | 89 scoped_ptr<base::Value> Mapper::MapField( |
93 const std::string& field_name, | 90 const std::string& field_name, |
94 const OncValueSignature& object_signature, | 91 const OncValueSignature& object_signature, |
95 const base::Value& onc_value, | 92 const base::Value& onc_value, |
96 bool* found_unknown_field) { | 93 bool* found_unknown_field, |
94 bool* error) { | |
97 const OncFieldSignature* field_signature = | 95 const OncFieldSignature* field_signature = |
98 GetFieldSignature(object_signature, field_name); | 96 GetFieldSignature(object_signature, field_name); |
99 | 97 |
100 if (field_signature != NULL) { | 98 if (field_signature != NULL) { |
101 if (field_signature->value_signature == NULL) { | 99 DCHECK(field_signature->value_signature != NULL) |
pastarmovj
2012/12/13 10:15:44
Leave just DCHECK(pointer) it's equivalent.
pneubeck (no reviews)
2012/12/13 14:10:03
For consistency with the other null-pointer checks
| |
102 NOTREACHED() << "Found missing value signature at field '" | 100 << "Found missing value signature at field '" << field_name << "'."; |
103 << field_name << "'."; | |
104 return scoped_ptr<base::Value>(); | |
105 } | |
106 | 101 |
107 return MapValue(*field_signature->value_signature, onc_value); | 102 return MapValue(*field_signature->value_signature, onc_value, error); |
108 } else { | 103 } else { |
109 DVLOG(1) << "Found unknown field name: '" << field_name << "'"; | 104 DVLOG(1) << "Found unknown field name: '" << field_name << "'"; |
110 *found_unknown_field = true; | 105 *found_unknown_field = true; |
111 return scoped_ptr<base::Value>(); | 106 return scoped_ptr<base::Value>(); |
112 } | 107 } |
113 } | 108 } |
114 | 109 |
115 scoped_ptr<base::ListValue> Mapper::MapArray( | 110 scoped_ptr<base::ListValue> Mapper::MapArray( |
116 const OncValueSignature& array_signature, | 111 const OncValueSignature& array_signature, |
117 const base::ListValue& onc_array, | 112 const base::ListValue& onc_array, |
118 bool* nested_error_occured) { | 113 bool* nested_error) { |
119 if (array_signature.onc_array_entry_signature == NULL) { | 114 DCHECK(array_signature.onc_array_entry_signature != NULL) |
pastarmovj
2012/12/13 10:15:44
Ditto.
pneubeck (no reviews)
2012/12/13 14:10:03
see above.
| |
120 NOTREACHED() << "Found missing onc_array_entry_signature."; | 115 << "Found missing onc_array_entry_signature."; |
121 return scoped_ptr<base::ListValue>(); | |
122 } | |
123 | 116 |
124 scoped_ptr<base::ListValue> result_array(new base::ListValue); | 117 scoped_ptr<base::ListValue> result_array(new base::ListValue); |
118 int original_index = 0; | |
125 for (base::ListValue::const_iterator it = onc_array.begin(); | 119 for (base::ListValue::const_iterator it = onc_array.begin(); |
126 it != onc_array.end(); ++it) { | 120 it != onc_array.end(); ++it, ++original_index) { |
127 const base::Value* entry = *it; | 121 const base::Value* entry = *it; |
128 | 122 |
129 scoped_ptr<base::Value> result_entry; | 123 scoped_ptr<base::Value> result_entry; |
130 result_entry = MapValue(*array_signature.onc_array_entry_signature, *entry); | 124 result_entry = MapEntry(original_index, |
125 *array_signature.onc_array_entry_signature, | |
126 *entry, | |
127 nested_error); | |
131 if (result_entry.get() != NULL) | 128 if (result_entry.get() != NULL) |
132 result_array->Append(result_entry.release()); | 129 result_array->Append(result_entry.release()); |
133 else | 130 else |
134 *nested_error_occured = true; | 131 DCHECK(*nested_error); |
135 } | 132 } |
136 return result_array.Pass(); | 133 return result_array.Pass(); |
137 } | 134 } |
138 | 135 |
136 scoped_ptr<base::Value> Mapper::MapEntry(int index, | |
137 const OncValueSignature& signature, | |
138 const base::Value& onc_value, | |
139 bool* error) { | |
140 return MapValue(signature, onc_value, error); | |
141 } | |
142 | |
139 } // namespace onc | 143 } // namespace onc |
140 } // namespace chromeos | 144 } // namespace chromeos |
OLD | NEW |