Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/network_settings/onc_mapper.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/values.h" | |
| 9 #include "chrome/browser/chromeos/network_settings/onc_signature.h" | |
| 10 | |
| 11 namespace chromeos { | |
| 12 namespace onc { | |
| 13 | |
| 14 Mapper::Mapper() {} | |
| 15 | |
| 16 scoped_ptr<base::Value> Mapper::MapValue( | |
| 17 const OncValueSignature& signature, | |
| 18 const base::Value& onc_value) { | |
| 19 scoped_ptr<base::Value> result_value; | |
| 20 switch (onc_value.GetType()) { | |
| 21 case base::Value::TYPE_DICTIONARY: { | |
| 22 const base::DictionaryValue* dict = NULL; | |
| 23 onc_value.GetAsDictionary(&dict); | |
| 24 CHECK(dict != NULL); | |
| 25 result_value = MapObject(signature, *dict); | |
| 26 break; | |
| 27 } | |
| 28 case base::Value::TYPE_LIST: { | |
| 29 const base::ListValue* list = NULL; | |
| 30 onc_value.GetAsList(&list); | |
| 31 CHECK(list != NULL); | |
| 32 bool nested_error_occured = false; | |
| 33 result_value = MapArray(signature, *list, &nested_error_occured); | |
| 34 if (nested_error_occured) | |
| 35 result_value.reset(); | |
| 36 break; | |
| 37 } | |
| 38 default: { | |
| 39 result_value = MapPrimitive(signature, onc_value); | |
| 40 break; | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 return result_value.Pass(); | |
| 45 } | |
| 46 | |
| 47 scoped_ptr<base::DictionaryValue> Mapper::MapObject( | |
| 48 const OncValueSignature& signature, | |
| 49 const base::DictionaryValue& onc_object) { | |
| 50 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); | |
| 51 | |
| 52 bool found_unknown_field = false; | |
| 53 if (MapFields(signature, onc_object, &found_unknown_field, result.get()) && | |
| 54 !found_unknown_field) | |
|
stevenjb
2012/10/11 19:54:00
nit: {} around multi-line if
pneubeck (no reviews)
2012/10/15 13:57:44
Done.
| |
| 55 return result.Pass(); | |
| 56 else | |
| 57 return scoped_ptr<base::DictionaryValue>(); | |
| 58 } | |
| 59 | |
| 60 scoped_ptr<base::Value> Mapper::MapPrimitive( | |
| 61 const OncValueSignature& signature, | |
| 62 const base::Value& onc_primitive) { | |
| 63 return make_scoped_ptr(onc_primitive.DeepCopy()); | |
| 64 } | |
| 65 | |
| 66 bool Mapper::MapFields( | |
| 67 const OncValueSignature& object_signature, | |
| 68 const base::DictionaryValue& onc_object, | |
| 69 bool* found_unknown_fieldname, | |
| 70 base::DictionaryValue* result) { | |
| 71 bool nested_error_occured = false; | |
| 72 for (base::DictionaryValue::key_iterator it = onc_object.begin_keys(); | |
| 73 it != onc_object.end_keys(); ++it) { | |
| 74 const std::string& fieldname = *it; | |
| 75 const base::Value* value; | |
| 76 onc_object.GetWithoutPathExpansion(fieldname, &value); | |
| 77 CHECK(value != NULL); | |
| 78 | |
| 79 bool current_field_unknown = false; | |
| 80 scoped_ptr<base::Value> result_value = | |
| 81 MapField(fieldname, object_signature, *value, ¤t_field_unknown); | |
| 82 | |
| 83 if (current_field_unknown) | |
| 84 *found_unknown_fieldname = true; | |
| 85 | |
| 86 if (result_value.get()) | |
| 87 result->SetWithoutPathExpansion(fieldname, result_value.release()); | |
| 88 else if (!current_field_unknown) | |
| 89 nested_error_occured = true; | |
| 90 } | |
| 91 return !nested_error_occured; | |
| 92 } | |
| 93 | |
| 94 scoped_ptr<base::Value> Mapper::MapField( | |
| 95 const std::string& fieldname, | |
| 96 const OncValueSignature& object_signature, | |
| 97 const base::Value& onc_value, | |
| 98 bool* found_unknown_fieldname) { | |
| 99 const OncFieldSignature* field_signature = | |
| 100 object_signature.GetFieldSignature(fieldname); | |
| 101 | |
| 102 if (field_signature) { | |
| 103 if (field_signature->value_signature == NULL) | |
| 104 NOTREACHED() << "Found missing value signature at field '" | |
| 105 << fieldname << "'."; | |
|
stevenjb
2012/10/11 19:54:00
nit: {}, alignment
pneubeck (no reviews)
2012/10/15 13:57:44
Done.
| |
| 106 | |
| 107 return MapValue(*field_signature->value_signature, onc_value); | |
| 108 } else { | |
| 109 LOG(INFO) << "Found unknown field name: '" << fieldname << "'"; | |
| 110 *found_unknown_fieldname = true; | |
| 111 return scoped_ptr<base::Value>(); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 scoped_ptr<base::ListValue> Mapper::MapArray( | |
| 116 const OncValueSignature& array_signature, | |
| 117 const base::ListValue& onc_array, | |
| 118 bool* nested_error_occured) { | |
| 119 if (array_signature.onc_array_entry_signature == NULL) | |
| 120 NOTREACHED() << "Found missing onc_array_entry_signature."; | |
|
stevenjb
2012/10/11 19:54:00
CHECK()?
pneubeck (no reviews)
2012/10/15 13:57:44
It's a fatal error but we can still ignore some in
| |
| 121 | |
| 122 scoped_ptr<base::ListValue> result_array(new base::ListValue); | |
| 123 for (base::ListValue::const_iterator it = onc_array.begin(); | |
| 124 it != onc_array.end(); ++it) { | |
| 125 const base::Value* entry = *it; | |
| 126 | |
| 127 scoped_ptr<base::Value> result_entry; | |
| 128 result_entry = MapValue(*array_signature.onc_array_entry_signature, | |
| 129 *entry); | |
|
stevenjb
2012/10/11 19:54:00
nit: align
pneubeck (no reviews)
2012/10/15 13:57:44
Done.
| |
| 130 if (result_entry.get()) | |
| 131 result_array->Append(result_entry.release()); | |
| 132 else | |
| 133 *nested_error_occured = true; | |
| 134 } | |
| 135 return result_array.Pass(); | |
| 136 } | |
| 137 | |
| 138 } // namespace onc | |
| 139 } // namespace chromeos | |
| OLD | NEW |