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