onc_mapper.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef CHROMEOS_COMPONENTS_ONC_ONC_MAPPER_H_
  5. #define CHROMEOS_COMPONENTS_ONC_ONC_MAPPER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/component_export.h"
  9. #include "base/values.h"
  10. namespace chromeos {
  11. namespace onc {
  12. struct OncValueSignature;
  13. // This class implements a DeepCopy of base::Values for ONC objects that
  14. // iterates over both the ONC signature and the object hierarchy. DCHECKs if a
  15. // field signature without value signature or an array signature without entry
  16. // signature is reached.
  17. //
  18. // The general term "map" is used here, as this class is meant as base class and
  19. // the copy behavior can be adapted by overriding the methods. By comparing the
  20. // address of a signature object to the list of signatures in "onc_signature.h",
  21. // accurate signature-specific translations or validations can be applied in the
  22. // overriding methods.
  23. //
  24. // The ONC validator and normalizer derive from this class and adapt the default
  25. // copy behavior.
  26. class COMPONENT_EXPORT(CHROMEOS_ONC) Mapper {
  27. public:
  28. Mapper();
  29. Mapper(const Mapper&) = delete;
  30. Mapper& operator=(const Mapper&) = delete;
  31. virtual ~Mapper();
  32. protected:
  33. // Calls |MapObject|, |MapArray| and |MapPrimitive| according to |onc_value|'s
  34. // type, which always return an object of the according type. Result of the
  35. // mapping is returned. Only on error sets |error| to true.
  36. virtual base::Value MapValue(const OncValueSignature& signature,
  37. const base::Value& onc_value,
  38. bool* error);
  39. // Maps objects/dictionaries. By default calls |MapFields|, which recurses
  40. // into each field of |onc_object|, and drops unknown fields. Result of the
  41. // mapping is returned. Only on error sets |error| to true. In this
  42. // implementation only unknown fields are errors.
  43. virtual base::Value MapObject(const OncValueSignature& signature,
  44. const base::Value& onc_object,
  45. bool* error);
  46. // Maps primitive values like BinaryValue, StringValue, IntegerValue... (all
  47. // but dictionaries and lists). By default copies |onc_primitive|. Result of
  48. // the mapping is returned. Only on error sets |error| to true.
  49. virtual base::Value MapPrimitive(const OncValueSignature& signature,
  50. const base::Value& onc_primitive,
  51. bool* error);
  52. // Maps each field of the given |onc_object| according to |object_signature|.
  53. // Adds the mapping of each field to |result| using |MapField| and drops
  54. // unknown fields by default. Sets |found_unknown_field| to true if this
  55. // dictionary contains any unknown fields. Set |nested_error| to true only if
  56. // nested errors occured.
  57. virtual void 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. // Maps the value |onc_value| of field |field_name| according to its field
  63. // signature in |object_signature| using |MapValue|. Sets
  64. // |found_unknown_field| to true and returns a Value of type
  65. // base::Value::Type::NONEif |field_name| cannot be found in
  66. // |object_signature|. Otherwise returns the mapping of |onc_value|.
  67. virtual base::Value MapField(const std::string& field_name,
  68. const OncValueSignature& object_signature,
  69. const base::Value& onc_value,
  70. bool* found_unknown_field,
  71. bool* error);
  72. // Maps the array |onc_array| according to |array_signature|, which defines
  73. // the type of the entries. Maps each entry by calling |MapValue|. If any of
  74. // the nested mappings failed, the flag |nested_error| is set to true and the
  75. // entry is dropped from the result. Otherwise |nested_error| isn't
  76. // modified. The resulting array is returned.
  77. virtual base::Value MapArray(const OncValueSignature& array_signature,
  78. const base::Value& onc_array,
  79. bool* nested_error);
  80. // Calls |MapValue| and returns its result. Called by |MapArray| for each
  81. // entry and its index in the enclosing array.
  82. virtual base::Value MapEntry(int index,
  83. const OncValueSignature& signature,
  84. const base::Value& onc_value,
  85. bool* error);
  86. };
  87. } // namespace onc
  88. } // namespace chromeos
  89. // TODO(https://crbug.com/1164001): remove when it moved to ash.
  90. namespace ash::onc {
  91. using ::chromeos::onc::Mapper;
  92. }
  93. #endif // CHROMEOS_COMPONENTS_ONC_ONC_MAPPER_H_