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 <string> 9 10 #include "base/memory/scoped_ptr.h" 11 #include "chromeos/chromeos_export.h" 12 13 namespace base { 14 class DictionaryValue; 15 class ListValue; 16 class Value; 17 } 18 19 namespace chromeos { 20 namespace onc { 21 22 struct OncValueSignature; 23 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. 28 // 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. 34 // 35 // The ONC validator and normalizer derive from this class and adapt the default 36 // copy behavior. 37 class CHROMEOS_EXPORT Mapper { 38 public: 39 Mapper(); 40 virtual ~Mapper(); 41 42 protected: 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, 48 bool* error); 49 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, 57 bool* error); 58 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, 65 bool* error); 66 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, 75 bool* nested_error, 76 base::DictionaryValue* result); 77 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, 87 bool* error); 88 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, 97 bool* nested_error); 98 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, 104 bool* error); 105 106 private: 107 DISALLOW_COPY_AND_ASSIGN(Mapper); 108 }; 109 110 } // namespace onc 111 } // namespace chromeos 112 113 #endif // CHROMEOS_NETWORK_ONC_ONC_MAPPER_H_ 114