Chromium Code Reviews| Index: chrome/browser/chromeos/network_settings/onc_mapper.cc |
| diff --git a/chrome/browser/chromeos/network_settings/onc_mapper.cc b/chrome/browser/chromeos/network_settings/onc_mapper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..17f5731e89112ca019dc459ffb81747a75c97339 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/network_settings/onc_mapper.cc |
| @@ -0,0 +1,139 @@ |
| +// 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 "chrome/browser/chromeos/network_settings/onc_mapper.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/chromeos/network_settings/onc_signature.h" |
| + |
| +namespace chromeos { |
| +namespace onc { |
| + |
| +Mapper::Mapper() {} |
| + |
| +scoped_ptr<base::Value> Mapper::MapValue( |
| + const OncValueSignature& signature, |
| + const base::Value& onc_value) { |
| + scoped_ptr<base::Value> result_value; |
| + switch (onc_value.GetType()) { |
| + case base::Value::TYPE_DICTIONARY: { |
| + const base::DictionaryValue* dict = NULL; |
| + onc_value.GetAsDictionary(&dict); |
| + CHECK(dict != NULL); |
| + result_value = MapObject(signature, *dict); |
| + break; |
| + } |
| + case base::Value::TYPE_LIST: { |
| + const base::ListValue* list = NULL; |
| + onc_value.GetAsList(&list); |
| + CHECK(list != NULL); |
| + bool nested_error_occured = false; |
| + result_value = MapArray(signature, *list, &nested_error_occured); |
| + if (nested_error_occured) |
| + result_value.reset(); |
| + break; |
| + } |
| + default: { |
| + result_value = MapPrimitive(signature, onc_value); |
| + break; |
| + } |
| + } |
| + |
| + return result_value.Pass(); |
| +} |
| + |
| +scoped_ptr<base::DictionaryValue> Mapper::MapObject( |
| + const OncValueSignature& signature, |
| + const base::DictionaryValue& onc_object) { |
| + scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue); |
| + |
| + bool found_unknown_field = false; |
| + if (MapFields(signature, onc_object, &found_unknown_field, result.get()) && |
| + !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.
|
| + return result.Pass(); |
| + else |
| + return scoped_ptr<base::DictionaryValue>(); |
| +} |
| + |
| +scoped_ptr<base::Value> Mapper::MapPrimitive( |
| + const OncValueSignature& signature, |
| + const base::Value& onc_primitive) { |
| + return make_scoped_ptr(onc_primitive.DeepCopy()); |
| +} |
| + |
| +bool Mapper::MapFields( |
| + const OncValueSignature& object_signature, |
| + const base::DictionaryValue& onc_object, |
| + bool* found_unknown_fieldname, |
| + base::DictionaryValue* result) { |
| + bool nested_error_occured = false; |
| + for (base::DictionaryValue::key_iterator it = onc_object.begin_keys(); |
| + it != onc_object.end_keys(); ++it) { |
| + const std::string& fieldname = *it; |
| + const base::Value* value; |
| + onc_object.GetWithoutPathExpansion(fieldname, &value); |
| + CHECK(value != NULL); |
| + |
| + bool current_field_unknown = false; |
| + scoped_ptr<base::Value> result_value = |
| + MapField(fieldname, object_signature, *value, ¤t_field_unknown); |
| + |
| + if (current_field_unknown) |
| + *found_unknown_fieldname = true; |
| + |
| + if (result_value.get()) |
| + result->SetWithoutPathExpansion(fieldname, result_value.release()); |
| + else if (!current_field_unknown) |
| + nested_error_occured = true; |
| + } |
| + return !nested_error_occured; |
| +} |
| + |
| +scoped_ptr<base::Value> Mapper::MapField( |
| + const std::string& fieldname, |
| + const OncValueSignature& object_signature, |
| + const base::Value& onc_value, |
| + bool* found_unknown_fieldname) { |
| + const OncFieldSignature* field_signature = |
| + object_signature.GetFieldSignature(fieldname); |
| + |
| + if (field_signature) { |
| + if (field_signature->value_signature == NULL) |
| + NOTREACHED() << "Found missing value signature at field '" |
| + << fieldname << "'."; |
|
stevenjb
2012/10/11 19:54:00
nit: {}, alignment
pneubeck (no reviews)
2012/10/15 13:57:44
Done.
|
| + |
| + return MapValue(*field_signature->value_signature, onc_value); |
| + } else { |
| + LOG(INFO) << "Found unknown field name: '" << fieldname << "'"; |
| + *found_unknown_fieldname = true; |
| + return scoped_ptr<base::Value>(); |
| + } |
| +} |
| + |
| +scoped_ptr<base::ListValue> Mapper::MapArray( |
| + const OncValueSignature& array_signature, |
| + const base::ListValue& onc_array, |
| + bool* nested_error_occured) { |
| + if (array_signature.onc_array_entry_signature == NULL) |
| + 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
|
| + |
| + scoped_ptr<base::ListValue> result_array(new base::ListValue); |
| + for (base::ListValue::const_iterator it = onc_array.begin(); |
| + it != onc_array.end(); ++it) { |
| + const base::Value* entry = *it; |
| + |
| + scoped_ptr<base::Value> result_entry; |
| + result_entry = MapValue(*array_signature.onc_array_entry_signature, |
| + *entry); |
|
stevenjb
2012/10/11 19:54:00
nit: align
pneubeck (no reviews)
2012/10/15 13:57:44
Done.
|
| + if (result_entry.get()) |
| + result_array->Append(result_entry.release()); |
| + else |
| + *nested_error_occured = true; |
| + } |
| + return result_array.Pass(); |
| +} |
| + |
| +} // namespace onc |
| +} // namespace chromeos |