onc_mapper.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "chromeos/components/onc/onc_mapper.h"
  5. #include <utility>
  6. #include "base/logging.h"
  7. #include "base/memory/ptr_util.h"
  8. #include "base/values.h"
  9. #include "chromeos/components/onc/onc_signature.h"
  10. namespace chromeos {
  11. namespace onc {
  12. Mapper::Mapper() = default;
  13. Mapper::~Mapper() = default;
  14. base::Value Mapper::MapValue(const OncValueSignature& signature,
  15. const base::Value& onc_value,
  16. bool* error) {
  17. switch (onc_value.type()) {
  18. case base::Value::Type::DICTIONARY: {
  19. if (signature.onc_type != base::Value::Type::DICTIONARY) {
  20. *error = true;
  21. return {};
  22. }
  23. return MapObject(signature, onc_value, error);
  24. }
  25. case base::Value::Type::LIST: {
  26. if (signature.onc_type != base::Value::Type::LIST) {
  27. *error = true;
  28. return {};
  29. }
  30. return MapArray(signature, onc_value, error);
  31. }
  32. default: {
  33. if ((signature.onc_type == base::Value::Type::DICTIONARY) ||
  34. (signature.onc_type == base::Value::Type::LIST)) {
  35. *error = true;
  36. return {};
  37. }
  38. return MapPrimitive(signature, onc_value, error);
  39. }
  40. }
  41. }
  42. base::Value Mapper::MapObject(const OncValueSignature& signature,
  43. const base::Value& onc_object,
  44. bool* error) {
  45. base::Value result(base::Value::Type::DICTIONARY);
  46. bool found_unknown_field = false;
  47. MapFields(signature, onc_object, &found_unknown_field, error, &result);
  48. if (found_unknown_field)
  49. *error = true;
  50. return result;
  51. }
  52. base::Value Mapper::MapPrimitive(const OncValueSignature& signature,
  53. const base::Value& onc_primitive,
  54. bool* error) {
  55. return onc_primitive.Clone();
  56. }
  57. void Mapper::MapFields(const OncValueSignature& object_signature,
  58. const base::Value& onc_object,
  59. bool* found_unknown_field,
  60. bool* nested_error,
  61. base::Value* result) {
  62. DCHECK(onc_object.is_dict());
  63. for (auto it : onc_object.DictItems()) {
  64. bool current_field_unknown = false;
  65. base::Value result_value = MapField(it.first, object_signature, it.second,
  66. &current_field_unknown, nested_error);
  67. if (current_field_unknown)
  68. *found_unknown_field = true;
  69. else if (!result_value.is_none())
  70. result->SetKey(it.first, std::move(result_value));
  71. else
  72. DCHECK(*nested_error);
  73. }
  74. }
  75. base::Value Mapper::MapField(const std::string& field_name,
  76. const OncValueSignature& object_signature,
  77. const base::Value& onc_value,
  78. bool* found_unknown_field,
  79. bool* error) {
  80. const OncFieldSignature* field_signature =
  81. GetFieldSignature(object_signature, field_name);
  82. if (field_signature != nullptr) {
  83. DCHECK(field_signature->value_signature != nullptr)
  84. << "Found missing value signature at field '" << field_name << "'.";
  85. return MapValue(*field_signature->value_signature, onc_value, error);
  86. }
  87. DVLOG(1) << "Found unknown field name: '" << field_name << "'";
  88. *found_unknown_field = true;
  89. return {};
  90. }
  91. base::Value Mapper::MapArray(const OncValueSignature& array_signature,
  92. const base::Value& onc_array,
  93. bool* nested_error) {
  94. DCHECK(array_signature.onc_array_entry_signature != NULL)
  95. << "Found missing onc_array_entry_signature.";
  96. base::Value result_array(base::Value::Type::LIST);
  97. int original_index = 0;
  98. for (const auto& entry : onc_array.GetListDeprecated()) {
  99. base::Value result_entry =
  100. MapEntry(original_index, *array_signature.onc_array_entry_signature,
  101. entry, nested_error);
  102. if (!result_entry.is_none())
  103. result_array.Append(std::move(result_entry));
  104. else
  105. DCHECK(*nested_error);
  106. ++original_index;
  107. }
  108. return result_array;
  109. }
  110. base::Value Mapper::MapEntry(int index,
  111. const OncValueSignature& signature,
  112. const base::Value& onc_value,
  113. bool* error) {
  114. return MapValue(signature, onc_value, error);
  115. }
  116. } // namespace onc
  117. } // namespace chromeos