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.
5 #include "chromeos/network/onc/onc_mapper.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "chromeos/network/onc/onc_signature.h"
20 scoped_ptr<base::Value> Mapper::MapValue(const OncValueSignature& signature,
21 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, error);
31 case base::Value::TYPE_LIST: {
32 const base::ListValue* list = NULL;
33 onc_value.GetAsList(&list);
34 result_value = MapArray(signature, *list, error);
38 result_value = MapPrimitive(signature, onc_value, error);
43 return result_value.Pass();
46 scoped_ptr<base::DictionaryValue> Mapper::MapObject(
47 const OncValueSignature& signature,
48 const base::DictionaryValue& onc_object,
50 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
52 bool found_unknown_field = false;
53 MapFields(signature, onc_object, &found_unknown_field, error, result.get());
54 if (found_unknown_field)
59 scoped_ptr<base::Value> Mapper::MapPrimitive(const OncValueSignature& signature,
60 const base::Value& onc_primitive,
62 return make_scoped_ptr(onc_primitive.DeepCopy());
65 void Mapper::MapFields(const OncValueSignature& object_signature,
66 const base::DictionaryValue& onc_object,
67 bool* found_unknown_field,
69 base::DictionaryValue* result) {
70 for (base::DictionaryValue::Iterator it(onc_object); !it.IsAtEnd();
72 bool current_field_unknown = false;
73 scoped_ptr<base::Value> result_value = MapField(it.key(),
76 ¤t_field_unknown,
79 if (current_field_unknown)
80 *found_unknown_field = true;
81 else if (result_value.get() != NULL)
82 result->SetWithoutPathExpansion(it.key(), result_value.release());
84 DCHECK(*nested_error);
88 scoped_ptr<base::Value> Mapper::MapField(
89 const std::string& field_name,
90 const OncValueSignature& object_signature,
91 const base::Value& onc_value,
92 bool* found_unknown_field,
94 const OncFieldSignature* field_signature =
95 GetFieldSignature(object_signature, field_name);
97 if (field_signature != NULL) {
98 DCHECK(field_signature->value_signature != NULL)
99 << "Found missing value signature at field '" << field_name << "'.";
101 return MapValue(*field_signature->value_signature, onc_value, error);
103 DVLOG(1) << "Found unknown field name: '" << field_name << "'";
104 *found_unknown_field = true;
105 return scoped_ptr<base::Value>();
109 scoped_ptr<base::ListValue> Mapper::MapArray(
110 const OncValueSignature& array_signature,
111 const base::ListValue& onc_array,
112 bool* nested_error) {
113 DCHECK(array_signature.onc_array_entry_signature != NULL)
114 << "Found missing onc_array_entry_signature.";
116 scoped_ptr<base::ListValue> result_array(new base::ListValue);
117 int original_index = 0;
118 for (base::ListValue::const_iterator it = onc_array.begin();
119 it != onc_array.end(); ++it, ++original_index) {
120 const base::Value* entry = *it;
122 scoped_ptr<base::Value> result_entry;
123 result_entry = MapEntry(original_index,
124 *array_signature.onc_array_entry_signature,
127 if (result_entry.get() != NULL)
128 result_array->Append(result_entry.release());
130 DCHECK(*nested_error);
132 return result_array.Pass();
135 scoped_ptr<base::Value> Mapper::MapEntry(int index,
136 const OncValueSignature& signature,
137 const base::Value& onc_value,
139 return MapValue(signature, onc_value, error);
143 } // namespace chromeos