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 CHROME_BROWSER_CHROMEOS_NETWORK_SETTINGS_ONC_VALIDATOR_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_NETWORK_SETTINGS_ONC_VALIDATOR_H_ |
6 #define CHROME_BROWSER_CHROMEOS_NETWORK_SETTINGS_ONC_VALIDATOR_H_ | 6 #define CHROME_BROWSER_CHROMEOS_NETWORK_SETTINGS_ONC_VALIDATOR_H_ |
7 | 7 |
| 8 #include <vector> |
| 9 |
8 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
9 #include "chrome/browser/chromeos/network_settings/onc_mapper.h" | 11 #include "chrome/browser/chromeos/network_settings/onc_mapper.h" |
10 | 12 |
11 namespace base { | 13 namespace base { |
12 class Value; | 14 class Value; |
13 class DictionaryValue; | 15 class DictionaryValue; |
14 } | 16 } |
15 | 17 |
16 namespace chromeos { | 18 namespace chromeos { |
17 namespace onc { | 19 namespace onc { |
18 | 20 |
19 struct OncValueSignature; | 21 struct OncValueSignature; |
20 | 22 |
21 class Validator : public Mapper { | 23 class Validator : public Mapper { |
22 public: | 24 public: |
| 25 enum Result { |
| 26 VALID, |
| 27 VALID_WITH_WARNINGS, |
| 28 INVALID |
| 29 }; |
| 30 |
23 // Creates a Validator that searches for the following invalid cases: | 31 // Creates a Validator that searches for the following invalid cases: |
24 // - a field name is found that is not part of the signature | 32 // - a field name is found that is not part of the signature |
25 // (controlled by |error_on_unknown_field|) | 33 // (controlled by |error_on_unknown_field|) |
26 // | 34 // |
27 // - a kRecommended array contains a field name that is not part of the | 35 // - a kRecommended array contains a field name that is not part of the |
28 // enclosing object's signature or if that field is dictionary typed | 36 // enclosing object's signature or if that field is dictionary typed |
29 // (controlled by |error_on_wrong_recommended|) | 37 // (controlled by |error_on_wrong_recommended|) |
30 // | 38 // |
31 // - |managed_onc| is false and a field with name kRecommended is found | 39 // - |managed_onc| is false and a field with name kRecommended is found |
32 // (always ignored) | 40 // (always ignored) |
33 // | 41 // |
34 // - a required field is missing (controlled by |error_on_missing_field|) | 42 // - a required field is missing (controlled by |error_on_missing_field|) |
35 // | 43 // |
36 // If one of these invalid cases occurs and the controlling flag is true, then | 44 // If one of these invalid cases occurs and the controlling flag is true, then |
37 // it is an error and the validation stops. The function | 45 // it is an error. The function ValidateAndRepairObject sets |result| to |
38 // ValidateAndRepairObject returns NULL. | 46 // INVALID and returns NULL. |
39 // | 47 // |
40 // If no error occurred, then a DeepCopy of the validated object is created, | 48 // Otherwise, a DeepCopy of the validated object is created, which contains |
41 // which contains all but the invalid fields and values. | 49 // all but the invalid fields and values. |
| 50 // |
| 51 // If one of the invalid cases occurs and the controlling flag is false, then |
| 52 // it is a warning. The function ValidateAndRepairObject sets |result| to |
| 53 // VALID_WITH_WARNINGS and returns the repaired copy. |
| 54 // |
| 55 // If no error occurred, |result| is set to VALID and an exact DeepCopy is |
| 56 // returned. |
42 Validator(bool error_on_unknown_field, | 57 Validator(bool error_on_unknown_field, |
43 bool error_on_wrong_recommended, | 58 bool error_on_wrong_recommended, |
44 bool error_on_missing_field, | 59 bool error_on_missing_field, |
45 bool managed_onc); | 60 bool managed_onc); |
46 | 61 |
47 virtual ~Validator(); | 62 virtual ~Validator(); |
48 | 63 |
49 // Validate the given |onc_object| according to |object_signature|. The | 64 // Validate the given |onc_object| according to |object_signature|. The |
50 // |object_signature| has to be a pointer to one of the signatures in | 65 // |object_signature| has to be a pointer to one of the signatures in |
51 // |onc_signature.h|. If an error is found, the function returns NULL. If | 66 // |onc_signature.h|. If an error is found, the function returns NULL. If |
52 // possible (no error encountered) a DeepCopy is created that contains all but | 67 // possible (no error encountered) a DeepCopy is created that contains all but |
53 // the invalid fields and values and returns this "repaired" object. | 68 // the invalid fields and values and returns this "repaired" object. |
54 // That means, if not handled as an error, then the following are ignored: | 69 // That means, if not handled as an error, then the following are dropped from |
| 70 // the copy: |
55 // - unknown fields | 71 // - unknown fields |
56 // - invalid field names in kRecommended arrays | 72 // - invalid field names in kRecommended arrays |
57 // - kRecommended fields in an unmanaged ONC | 73 // - kRecommended fields in an unmanaged ONC |
58 // For details, see the comment at the Constructor. | 74 // For details, see the comment at the Constructor. |
59 scoped_ptr<base::DictionaryValue> ValidateAndRepairObject( | 75 scoped_ptr<base::DictionaryValue> ValidateAndRepairObject( |
60 const OncValueSignature* object_signature, | 76 const OncValueSignature* object_signature, |
61 const base::DictionaryValue& onc_object); | 77 const base::DictionaryValue& onc_object, |
| 78 Result* result); |
62 | 79 |
63 private: | 80 private: |
64 // Overriden from Mapper: | 81 // Overridden from Mapper: |
65 // Compare |onc_value|s type with |onc_type| and validate/repair according to | 82 // Compare |onc_value|s type with |onc_type| and validate/repair according to |
66 // |signature|. On error returns NULL. | 83 // |signature|. On error returns NULL. |
67 virtual scoped_ptr<base::Value> MapValue( | 84 virtual scoped_ptr<base::Value> MapValue( |
68 const OncValueSignature& signature, | 85 const OncValueSignature& signature, |
69 const base::Value& onc_value) OVERRIDE; | 86 const base::Value& onc_value, |
| 87 bool* error) OVERRIDE; |
70 | 88 |
71 // Dispatch to the right validation function according to | 89 // Dispatch to the right validation function according to |
72 // |signature|. Iterates over all fields and recursively validates/repairs | 90 // |signature|. Iterates over all fields and recursively validates/repairs |
73 // these. All valid fields are added to the result dictionary. Returns the | 91 // these. All valid fields are added to the result dictionary. Returns the |
74 // repaired dictionary. On error returns NULL. | 92 // repaired dictionary. On error returns NULL. |
75 virtual scoped_ptr<base::DictionaryValue> MapObject( | 93 virtual scoped_ptr<base::DictionaryValue> MapObject( |
76 const OncValueSignature& signature, | 94 const OncValueSignature& signature, |
77 const base::DictionaryValue& onc_object) OVERRIDE; | 95 const base::DictionaryValue& onc_object, |
| 96 bool* error) OVERRIDE; |
| 97 |
| 98 // Pushes/pops the |field_name| to |path_|, otherwise like |Mapper::MapField|. |
| 99 virtual scoped_ptr<base::Value> MapField( |
| 100 const std::string& field_name, |
| 101 const OncValueSignature& object_signature, |
| 102 const base::Value& onc_value, |
| 103 bool* found_unknown_field, |
| 104 bool* error) OVERRIDE; |
| 105 |
| 106 // Pushes/pops the index to |path_|, otherwise like |Mapper::MapEntry|. |
| 107 virtual scoped_ptr<base::Value> MapEntry( |
| 108 int index, |
| 109 const OncValueSignature& signature, |
| 110 const base::Value& onc_value, |
| 111 bool* error) OVERRIDE; |
78 | 112 |
79 // This is the default validation of objects/dictionaries. Validates | 113 // This is the default validation of objects/dictionaries. Validates |
80 // |onc_object| according to |object_signature|. |result| must point to a | 114 // |onc_object| according to |object_signature|. |result| must point to a |
81 // dictionary into which the repaired fields are written. | 115 // dictionary into which the repaired fields are written. |
82 bool ValidateObjectDefault( | 116 bool ValidateObjectDefault( |
83 const OncValueSignature& object_signature, | 117 const OncValueSignature& object_signature, |
84 const base::DictionaryValue& onc_object, | 118 const base::DictionaryValue& onc_object, |
85 base::DictionaryValue* result); | 119 base::DictionaryValue* result); |
86 | 120 |
87 // Validates/repairs the kRecommended array in |result| according to | 121 // Validates/repairs the kRecommended array in |result| according to |
88 // |object_signature| of the enclosing object. | 122 // |object_signature| of the enclosing object. |
89 bool ValidateRecommendedField( | 123 bool ValidateRecommendedField( |
90 const OncValueSignature& object_signature, | 124 const OncValueSignature& object_signature, |
91 base::DictionaryValue* result); | 125 base::DictionaryValue* result); |
92 | 126 |
| 127 bool ValidateToplevelConfiguration( |
| 128 const base::DictionaryValue& onc_object, |
| 129 base::DictionaryValue* result); |
| 130 |
93 bool ValidateNetworkConfiguration( | 131 bool ValidateNetworkConfiguration( |
94 const base::DictionaryValue& onc_object, | 132 const base::DictionaryValue& onc_object, |
95 base::DictionaryValue* result); | 133 base::DictionaryValue* result); |
96 | 134 |
97 bool ValidateEthernet( | 135 bool ValidateEthernet( |
98 const base::DictionaryValue& onc_object, | 136 const base::DictionaryValue& onc_object, |
99 base::DictionaryValue* result); | 137 base::DictionaryValue* result); |
100 | 138 |
101 bool ValidateIPConfig( | 139 bool ValidateIPConfig( |
102 const base::DictionaryValue& onc_object, | 140 const base::DictionaryValue& onc_object, |
(...skipping 28 matching lines...) Expand all Loading... |
131 base::DictionaryValue* result); | 169 base::DictionaryValue* result); |
132 | 170 |
133 bool ValidateEAP( | 171 bool ValidateEAP( |
134 const base::DictionaryValue& onc_object, | 172 const base::DictionaryValue& onc_object, |
135 base::DictionaryValue* result); | 173 base::DictionaryValue* result); |
136 | 174 |
137 bool ValidateCertificate( | 175 bool ValidateCertificate( |
138 const base::DictionaryValue& onc_object, | 176 const base::DictionaryValue& onc_object, |
139 base::DictionaryValue* result); | 177 base::DictionaryValue* result); |
140 | 178 |
| 179 bool FieldExistsAndHasNoValueOf(const base::DictionaryValue& object, |
| 180 const std::string &field_name, |
| 181 const char** valid_values); |
| 182 |
| 183 bool FieldExistsAndIsNotInRange(const base::DictionaryValue& object, |
| 184 const std::string &field_name, |
| 185 int lower_bound, |
| 186 int upper_bound); |
| 187 |
| 188 bool RequireField(const base::DictionaryValue& dict, const std::string& key); |
| 189 |
| 190 std::string WarningHeader(); |
| 191 std::string ErrorHeader(); |
| 192 std::string MessageHeader(bool is_error); |
| 193 |
141 const bool error_on_unknown_field_; | 194 const bool error_on_unknown_field_; |
142 const bool error_on_wrong_recommended_; | 195 const bool error_on_wrong_recommended_; |
143 const bool error_on_missing_field_; | 196 const bool error_on_missing_field_; |
144 const bool managed_onc_; | 197 const bool managed_onc_; |
145 | 198 |
| 199 // The path of field names and indices to the current value. Indices |
| 200 // are stored as strings in decimal notation. |
| 201 std::vector<std::string> path_; |
| 202 |
| 203 // Tracks if an error or warning occurred within validation initiated by |
| 204 // function ValidateAndRepairObject. |
| 205 bool error_or_warning_found_; |
| 206 |
146 DISALLOW_COPY_AND_ASSIGN(Validator); | 207 DISALLOW_COPY_AND_ASSIGN(Validator); |
147 }; | 208 }; |
148 | 209 |
149 } // namespace onc | 210 } // namespace onc |
150 } // namespace chromeos | 211 } // namespace chromeos |
151 | 212 |
152 #endif // CHROME_BROWSER_CHROMEOS_NETWORK_SETTINGS_ONC_VALIDATOR_H_ | 213 #endif // CHROME_BROWSER_CHROMEOS_NETWORK_SETTINGS_ONC_VALIDATOR_H_ |
OLD | NEW |