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.
5 #ifndef CHROMEOS_NETWORK_ONC_ONC_MAPPER_H_
6 #define CHROMEOS_NETWORK_ONC_ONC_MAPPER_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "chromeos/chromeos_export.h"
14 class DictionaryValue
;
22 struct OncValueSignature
;
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
26 // field signature without value signature or an array signature without entry
27 // signature is reached.
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
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
33 // overriding methods.
35 // The ONC validator and normalizer derive from this class and adapt the default
37 class CHROMEOS_EXPORT Mapper
{
43 // Calls |MapObject|, |MapArray| and |MapPrimitive| according to |onc_value|'s
44 // type, which always return an object of the according type. Result of the
45 // mapping is returned. Only on error sets |error| to true.
46 virtual scoped_ptr
<base::Value
> MapValue(const OncValueSignature
& signature
,
47 const base::Value
& onc_value
,
50 // Maps objects/dictionaries. By default calls |MapFields|, which recurses
51 // into each field of |onc_object|, and drops unknown fields. Result of the
52 // mapping is returned. Only on error sets |error| to true. In this
53 // implementation only unknown fields are errors.
54 virtual scoped_ptr
<base::DictionaryValue
> MapObject(
55 const OncValueSignature
& signature
,
56 const base::DictionaryValue
& onc_object
,
59 // Maps primitive values like BinaryValue, StringValue, IntegerValue... (all
60 // but dictionaries and lists). By default copies |onc_primitive|. Result of
61 // the mapping is returned. Only on error sets |error| to true.
62 virtual scoped_ptr
<base::Value
> MapPrimitive(
63 const OncValueSignature
& signature
,
64 const base::Value
& onc_primitive
,
67 // Maps each field of the given |onc_object| according to |object_signature|.
68 // Adds the mapping of each field to |result| using |MapField| and drops
69 // unknown fields by default. Sets |found_unknown_field| to true if this
70 // dictionary contains any unknown fields. Set |nested_error| to true only if
71 // nested errors occured.
72 virtual void MapFields(const OncValueSignature
& object_signature
,
73 const base::DictionaryValue
& onc_object
,
74 bool* found_unknown_field
,
76 base::DictionaryValue
* result
);
78 // Maps the value |onc_value| of field |field_name| according to its field
79 // signature in |object_signature| using |MapValue|. Sets
80 // |found_unknown_field| to true and returns NULL if |field_name| cannot be
81 // found in |object_signature|. Otherwise returns the mapping of |onc_value|.
82 virtual scoped_ptr
<base::Value
> MapField(
83 const std::string
& field_name
,
84 const OncValueSignature
& object_signature
,
85 const base::Value
& onc_value
,
86 bool* found_unknown_field
,
89 // Maps the array |onc_array| according to |array_signature|, which defines
90 // the type of the entries. Maps each entry by calling |MapValue|. If any of
91 // the nested mappings failed, the flag |nested_error| is set to true and the
92 // entry is dropped from the result. Otherwise |nested_error| isn't
93 // modified. The resulting array is returned.
94 virtual scoped_ptr
<base::ListValue
> MapArray(
95 const OncValueSignature
& array_signature
,
96 const base::ListValue
& onc_array
,
99 // Calls |MapValue| and returns its result. Called by |MapArray| for each
100 // entry and its index in the enclosing array.
101 virtual scoped_ptr
<base::Value
> MapEntry(int index
,
102 const OncValueSignature
& signature
,
103 const base::Value
& onc_value
,
107 DISALLOW_COPY_AND_ASSIGN(Mapper
);
111 } // namespace chromeos
113 #endif // CHROMEOS_NETWORK_ONC_ONC_MAPPER_H_