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