| 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 |
| 17 Mapper::~Mapper() { |
| 18 } |
| 19 |
| 20 scoped_ptr<base::Value> Mapper::MapValue( |
| 21 const OncValueSignature& signature, |
| 22 const base::Value& onc_value) { |
| 23 scoped_ptr<base::Value> result_value; |
| 24 switch (onc_value.GetType()) { |
| 25 case base::Value::TYPE_DICTIONARY: { |
| 26 const base::DictionaryValue* dict = NULL; |
| 27 onc_value.GetAsDictionary(&dict); |
| 28 result_value = MapObject(signature, *dict); |
| 29 break; |
| 30 } |
| 31 case base::Value::TYPE_LIST: { |
| 32 const base::ListValue* list = NULL; |
| 33 onc_value.GetAsList(&list); |
| 34 bool nested_error_occured = false; |
| 35 result_value = MapArray(signature, *list, &nested_error_occured); |
| 36 if (nested_error_occured) |
| 37 result_value.reset(); |
| 38 break; |
| 39 } |
| 40 default: { |
| 41 result_value = MapPrimitive(signature, onc_value); |
| 42 break; |
| 43 } |
| 44 } |
| 45 |
| 46 return result_value.Pass(); |
| 47 } |
| 48 |
| 49 scoped_ptr<base::DictionaryValue> Mapper::MapObject( |
| 50 const OncValueSignature& signature, |
| 51 const base::DictionaryValue& onc_object) { |
| 52 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); |
| 53 |
| 54 bool found_unknown_field = false; |
| 55 bool nested_error_occured = false; |
| 56 MapFields(signature, onc_object, &found_unknown_field, &nested_error_occured, |
| 57 result.get()); |
| 58 if (!nested_error_occured && !found_unknown_field) |
| 59 return result.Pass(); |
| 60 else |
| 61 return scoped_ptr<base::DictionaryValue>(); |
| 62 } |
| 63 |
| 64 scoped_ptr<base::Value> Mapper::MapPrimitive( |
| 65 const OncValueSignature& signature, |
| 66 const base::Value& onc_primitive) { |
| 67 return make_scoped_ptr(onc_primitive.DeepCopy()); |
| 68 } |
| 69 |
| 70 void Mapper::MapFields( |
| 71 const OncValueSignature& object_signature, |
| 72 const base::DictionaryValue& onc_object, |
| 73 bool* found_unknown_field, |
| 74 bool* nested_error_occured, |
| 75 base::DictionaryValue* result) { |
| 76 |
| 77 for (base::DictionaryValue::Iterator it(onc_object); it.HasNext(); |
| 78 it.Advance()) { |
| 79 bool current_field_unknown = false; |
| 80 scoped_ptr<base::Value> result_value = MapField( |
| 81 it.key(), object_signature, it.value(), ¤t_field_unknown); |
| 82 |
| 83 if (current_field_unknown) |
| 84 *found_unknown_field = true; |
| 85 else if (result_value.get() != NULL) |
| 86 result->SetWithoutPathExpansion(it.key(), result_value.release()); |
| 87 else |
| 88 *nested_error_occured = true; |
| 89 } |
| 90 } |
| 91 |
| 92 scoped_ptr<base::Value> Mapper::MapField( |
| 93 const std::string& field_name, |
| 94 const OncValueSignature& object_signature, |
| 95 const base::Value& onc_value, |
| 96 bool* found_unknown_field) { |
| 97 const OncFieldSignature* field_signature = |
| 98 GetFieldSignature(object_signature, field_name); |
| 99 |
| 100 if (field_signature != NULL) { |
| 101 if (field_signature->value_signature == NULL) { |
| 102 NOTREACHED() << "Found missing value signature at field '" |
| 103 << field_name << "'."; |
| 104 return scoped_ptr<base::Value>(); |
| 105 } |
| 106 |
| 107 return MapValue(*field_signature->value_signature, onc_value); |
| 108 } else { |
| 109 DVLOG(1) << "Found unknown field name: '" << field_name << "'"; |
| 110 *found_unknown_field = 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."; |
| 121 return scoped_ptr<base::ListValue>(); |
| 122 } |
| 123 |
| 124 scoped_ptr<base::ListValue> result_array(new base::ListValue); |
| 125 for (base::ListValue::const_iterator it = onc_array.begin(); |
| 126 it != onc_array.end(); ++it) { |
| 127 const base::Value* entry = *it; |
| 128 |
| 129 scoped_ptr<base::Value> result_entry; |
| 130 result_entry = MapValue(*array_signature.onc_array_entry_signature, *entry); |
| 131 if (result_entry.get() != NULL) |
| 132 result_array->Append(result_entry.release()); |
| 133 else |
| 134 *nested_error_occured = true; |
| 135 } |
| 136 return result_array.Pass(); |
| 137 } |
| 138 |
| 139 } // namespace onc |
| 140 } // namespace chromeos |
| OLD | NEW |