OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ | 5 #ifndef COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ |
6 #define COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ | 6 #define COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "components/policy/policy_export.h" | 13 #include "components/policy/policy_export.h" |
14 | 14 |
15 namespace policy { | 15 namespace policy { |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 struct POLICY_EXPORT SchemaData; | 18 struct POLICY_EXPORT SchemaData; |
19 struct POLICY_EXPORT SchemaNode; | 19 struct POLICY_EXPORT SchemaNode; |
20 struct POLICY_EXPORT PropertyNode; | 20 struct POLICY_EXPORT PropertyNode; |
21 struct POLICY_EXPORT PropertiesNode; | 21 struct POLICY_EXPORT PropertiesNode; |
22 | 22 |
23 } // namespace internal | 23 } // namespace internal |
24 | 24 |
| 25 // Option flags passed to Schema::Validate() and Schema::Normalize(), describing |
| 26 // the strategy to handle unknown properties or invalid values for dict type. |
| 27 // Note that in Schema::Normalize() allowed errors will be dropped and thus |
| 28 // ignored. |
| 29 enum SchemaOnErrorStrategy { |
| 30 // No errors will be allowed. |
| 31 SCHEMA_STRICT = 0, |
| 32 // Unknown properties in the top-level dictionary will be ignored. |
| 33 SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, |
| 34 // Unknown properties in any dictionary will be ignored. |
| 35 SCHEMA_ALLOW_UNKNOWN, |
| 36 // Mismatched values will be ignored at the toplevel. |
| 37 SCHEMA_ALLOW_INVALID_TOPLEVEL, |
| 38 // Mismatched values will be ignored at the top-level value. |
| 39 // Unknown properties in any dictionary will be ignored. |
| 40 SCHEMA_ALLOW_INVALID_TOPLEVEL_AND_ALLOW_UNKNOWN, |
| 41 // Mismatched values will be ignored. |
| 42 SCHEMA_ALLOW_INVALID, |
| 43 }; |
| 44 |
25 // Describes the expected type of one policy. Also recursively describes the | 45 // Describes the expected type of one policy. Also recursively describes the |
26 // types of inner elements, for structured types. | 46 // types of inner elements, for structured types. |
27 // Objects of this class refer to external, immutable data and are cheap to | 47 // Objects of this class refer to external, immutable data and are cheap to |
28 // copy. | 48 // copy. |
29 class POLICY_EXPORT Schema { | 49 class POLICY_EXPORT Schema { |
30 public: | 50 public: |
31 // Used internally to store shared data. | 51 // Used internally to store shared data. |
32 class InternalStorage; | 52 class InternalStorage; |
33 | 53 |
34 // Builds an empty, invalid schema. | 54 // Builds an empty, invalid schema. |
(...skipping 15 matching lines...) Expand all Loading... |
50 // the internal representation. If |schema| is invalid then an invalid Schema | 70 // the internal representation. If |schema| is invalid then an invalid Schema |
51 // is returned and |error| contains a reason for the failure. | 71 // is returned and |error| contains a reason for the failure. |
52 static Schema Parse(const std::string& schema, std::string* error); | 72 static Schema Parse(const std::string& schema, std::string* error); |
53 | 73 |
54 // Returns true if this Schema is valid. Schemas returned by the methods below | 74 // Returns true if this Schema is valid. Schemas returned by the methods below |
55 // may be invalid, and in those cases the other methods must not be used. | 75 // may be invalid, and in those cases the other methods must not be used. |
56 bool valid() const { return node_ != NULL; } | 76 bool valid() const { return node_ != NULL; } |
57 | 77 |
58 base::Value::Type type() const; | 78 base::Value::Type type() const; |
59 | 79 |
60 // Returns true if |value| conforms to this Schema. | 80 // Validate |value| against current schema, |strategy| is the strategy to |
61 bool Validate(const base::Value& value) const; | 81 // handle unknown properties or invalid values. Allowed errors will be |
| 82 // ignored. If |value| don't conform the schema, false will be returned and |
| 83 // |error| will contain the detailed reason. |
| 84 bool Validate(const base::Value& value, |
| 85 SchemaOnErrorStrategy strategy, |
| 86 std::string* error) const; |
| 87 |
| 88 // Validate |value| against current schema, |strategy| is the strategy to |
| 89 // handle unknown properties or invalid values. Allowed errors will be |
| 90 // dropped in place. If |value| don't conform the schema, false will be |
| 91 // returned and |error| will contain the detailed message. |
| 92 bool Normalize(base::Value* value, |
| 93 SchemaOnErrorStrategy strategy, |
| 94 std::string* error) const; |
62 | 95 |
63 // Used to iterate over the known properties of TYPE_DICTIONARY schemas. | 96 // Used to iterate over the known properties of TYPE_DICTIONARY schemas. |
64 class POLICY_EXPORT Iterator { | 97 class POLICY_EXPORT Iterator { |
65 public: | 98 public: |
66 Iterator(const scoped_refptr<const InternalStorage>& storage, | 99 Iterator(const scoped_refptr<const InternalStorage>& storage, |
67 const internal::PropertiesNode* node); | 100 const internal::PropertiesNode* node); |
68 Iterator(const Iterator& iterator); | 101 Iterator(const Iterator& iterator); |
69 ~Iterator(); | 102 ~Iterator(); |
70 | 103 |
71 Iterator& operator=(const Iterator& iterator); | 104 Iterator& operator=(const Iterator& iterator); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 bool ValidateIntegerRestriction(int index, int value) const; | 154 bool ValidateIntegerRestriction(int index, int value) const; |
122 bool ValidateStringRestriction(int index, const char *str) const; | 155 bool ValidateStringRestriction(int index, const char *str) const; |
123 | 156 |
124 scoped_refptr<const InternalStorage> storage_; | 157 scoped_refptr<const InternalStorage> storage_; |
125 const internal::SchemaNode* node_; | 158 const internal::SchemaNode* node_; |
126 }; | 159 }; |
127 | 160 |
128 } // namespace policy | 161 } // namespace policy |
129 | 162 |
130 #endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ | 163 #endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ |
OLD | NEW |