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