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 #include <vector> | |
10 | 9 |
11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/scoped_vector.h" | |
14 #include "base/values.h" | 12 #include "base/values.h" |
15 #include "components/policy/policy_export.h" | 13 #include "components/policy/policy_export.h" |
16 | 14 |
17 namespace policy { | 15 namespace policy { |
18 namespace internal { | 16 namespace internal { |
19 | 17 |
| 18 struct POLICY_EXPORT SchemaData; |
20 struct POLICY_EXPORT SchemaNode; | 19 struct POLICY_EXPORT SchemaNode; |
21 struct POLICY_EXPORT PropertyNode; | 20 struct POLICY_EXPORT PropertyNode; |
22 struct POLICY_EXPORT PropertiesNode; | 21 struct POLICY_EXPORT PropertiesNode; |
23 | 22 |
24 } // namespace internal | 23 } // namespace internal |
25 | 24 |
26 // Describes the expected type of one policy. Also recursively describes the | 25 // Describes the expected type of one policy. Also recursively describes the |
27 // types of inner elements, for structured types. | 26 // types of inner elements, for structured types. |
28 // Objects of this class refer to external, immutable data and are cheap to | 27 // Objects of this class refer to external, immutable data and are cheap to |
29 // copy. | 28 // copy. |
30 // Use the SchemaOwner class to parse a schema and get Schema objects. | 29 // Use the SchemaOwner class to parse a schema and get Schema objects. |
31 class POLICY_EXPORT Schema { | 30 class POLICY_EXPORT Schema { |
32 public: | 31 public: |
33 // Builds an empty, invalid schema. | 32 // Builds an empty, invalid schema. |
34 Schema(); | 33 Schema(); |
35 | 34 |
36 // Builds a schema pointing to the inner structure of |schema|. If |schema| | 35 // Builds a schema pointing to the inner structure of |data|, |
37 // is NULL then this Schema instance will be invalid. | 36 // rooted at |node|. |
38 // Does not take ownership of |schema|. | 37 Schema(const internal::SchemaData* data, const internal::SchemaNode* node); |
39 explicit Schema(const internal::SchemaNode* schema); | |
40 | 38 |
41 Schema(const Schema& schema); | 39 Schema(const Schema& schema); |
42 | 40 |
43 Schema& operator=(const Schema& schema); | 41 Schema& operator=(const Schema& schema); |
44 | 42 |
45 // Returns true if this Schema is valid. Schemas returned by the methods below | 43 // Returns true if this Schema is valid. Schemas returned by the methods below |
46 // may be invalid, and in those cases the other methods must not be used. | 44 // may be invalid, and in those cases the other methods must not be used. |
47 bool valid() const { return schema_ != NULL; } | 45 bool valid() const { return data_ != NULL; } |
48 | 46 |
49 base::Value::Type type() const; | 47 base::Value::Type type() const; |
50 | 48 |
51 // Used to iterate over the known properties of TYPE_DICTIONARY schemas. | 49 // Used to iterate over the known properties of TYPE_DICTIONARY schemas. |
52 class POLICY_EXPORT Iterator { | 50 class POLICY_EXPORT Iterator { |
53 public: | 51 public: |
54 explicit Iterator(const internal::PropertiesNode* properties); | 52 Iterator(const internal::SchemaData* data, |
| 53 const internal::PropertiesNode* node); |
55 Iterator(const Iterator& iterator); | 54 Iterator(const Iterator& iterator); |
56 ~Iterator(); | 55 ~Iterator(); |
57 | 56 |
58 Iterator& operator=(const Iterator& iterator); | 57 Iterator& operator=(const Iterator& iterator); |
59 | 58 |
60 // The other methods must not be called if the iterator is at the end. | 59 // The other methods must not be called if the iterator is at the end. |
61 bool IsAtEnd() const; | 60 bool IsAtEnd() const; |
62 | 61 |
63 // Advances the iterator to the next property. | 62 // Advances the iterator to the next property. |
64 void Advance(); | 63 void Advance(); |
65 | 64 |
66 // Returns the name of the current property. | 65 // Returns the name of the current property. |
67 const char* key() const; | 66 const char* key() const; |
68 | 67 |
69 // Returns the Schema for the current property. This Schema is always valid. | 68 // Returns the Schema for the current property. This Schema is always valid. |
70 Schema schema() const; | 69 Schema schema() const; |
71 | 70 |
72 private: | 71 private: |
| 72 const internal::SchemaData* data_; |
73 const internal::PropertyNode* it_; | 73 const internal::PropertyNode* it_; |
74 const internal::PropertyNode* end_; | 74 const internal::PropertyNode* end_; |
75 }; | 75 }; |
76 | 76 |
77 // These methods should be called only if type() == TYPE_DICTIONARY, | 77 // These methods should be called only if type() == TYPE_DICTIONARY, |
78 // otherwise invalid memory will be read. A CHECK is currently enforcing this. | 78 // otherwise invalid memory will be read. A CHECK is currently enforcing this. |
79 | 79 |
80 // Returns an iterator that goes over the named properties of this schema. | 80 // Returns an iterator that goes over the named properties of this schema. |
81 // The returned iterator is at the beginning. | 81 // The returned iterator is at the beginning. |
82 Iterator GetPropertiesIterator() const; | 82 Iterator GetPropertiesIterator() const; |
83 | 83 |
84 // Returns the Schema for the property named |key|. If |key| is not a known | 84 // Returns the Schema for the property named |key|. If |key| is not a known |
85 // property name then the returned Schema is not valid. | 85 // property name then the returned Schema is not valid. |
86 Schema GetKnownProperty(const std::string& key) const; | 86 Schema GetKnownProperty(const std::string& key) const; |
87 | 87 |
88 // Returns the Schema for additional properties. If additional properties are | 88 // Returns the Schema for additional properties. If additional properties are |
89 // not allowed for this Schema then the Schema returned is not valid. | 89 // not allowed for this Schema then the Schema returned is not valid. |
90 Schema GetAdditionalProperties() const; | 90 Schema GetAdditionalProperties() const; |
91 | 91 |
92 // Returns the Schema for |key| if it is a known property, otherwise returns | 92 // Returns the Schema for |key| if it is a known property, otherwise returns |
93 // the Schema for additional properties. | 93 // the Schema for additional properties. |
94 Schema GetProperty(const std::string& key) const; | 94 Schema GetProperty(const std::string& key) const; |
95 | 95 |
96 // Returns the Schema for items of an array. | 96 // Returns the Schema for items of an array. |
97 // This method should be called only if type() == TYPE_LIST, | 97 // This method should be called only if type() == TYPE_LIST, |
98 // otherwise invalid memory will be read. A CHECK is currently enforcing this. | 98 // otherwise invalid memory will be read. A CHECK is currently enforcing this. |
99 Schema GetItems() const; | 99 Schema GetItems() const; |
100 | 100 |
101 private: | 101 private: |
102 const internal::SchemaNode* schema_; | 102 const internal::SchemaData* data_; |
| 103 const internal::SchemaNode* node_; |
103 }; | 104 }; |
104 | 105 |
105 // Owns schemas for policies of a given component. | 106 // Owns schemas for policies of a given component. |
106 class POLICY_EXPORT SchemaOwner { | 107 class POLICY_EXPORT SchemaOwner { |
107 public: | 108 public: |
108 ~SchemaOwner(); | 109 ~SchemaOwner(); |
109 | 110 |
110 // The returned Schema is valid only during the lifetime of the SchemaOwner | 111 // The returned Schema is valid only during the lifetime of the SchemaOwner |
111 // that created it. It may be obtained multiple times. | 112 // that created it. It may be obtained multiple times. |
112 Schema schema() const { return Schema(root_); } | 113 Schema schema() const; |
113 | 114 |
114 // Returns a SchemaOwner that references static data. This can be used by | 115 // Returns a SchemaOwner that references static data. This can be used by |
115 // the embedder to pass structures generated at compile time, which can then | 116 // the embedder to pass structures generated at compile time, which can then |
116 // be quickly loaded at runtime. | 117 // be quickly loaded at runtime. |
117 // Note: PropertiesNodes must have their PropertyNodes sorted by key. | 118 static scoped_ptr<SchemaOwner> Wrap(const internal::SchemaData* data); |
118 static scoped_ptr<SchemaOwner> Wrap(const internal::SchemaNode* schema); | |
119 | 119 |
120 // Parses the JSON schema in |schema| and returns a SchemaOwner that owns | 120 // Parses the JSON schema in |schema| and returns a SchemaOwner that owns |
121 // the internal representation. If |schema| is invalid then NULL is returned | 121 // the internal representation. If |schema| is invalid then NULL is returned |
122 // and |error| contains a reason for the failure. | 122 // and |error| contains a reason for the failure. |
123 static scoped_ptr<SchemaOwner> Parse(const std::string& schema, | 123 static scoped_ptr<SchemaOwner> Parse(const std::string& schema, |
124 std::string* error); | 124 std::string* error); |
125 | 125 |
126 private: | 126 private: |
127 explicit SchemaOwner(const internal::SchemaNode* root); | 127 class InternalStorage; |
128 | 128 |
129 // Parses the JSON schema in |schema| and returns the root SchemaNode if | 129 SchemaOwner(const internal::SchemaData* data, |
130 // successful, otherwise returns NULL. Any intermediate objects built by | 130 scoped_ptr<InternalStorage> storage); |
131 // this method are appended to the ScopedVectors. | |
132 const internal::SchemaNode* Parse(const base::DictionaryValue& schema, | |
133 std::string* error); | |
134 | 131 |
135 // Helper for Parse(). | 132 // Holds the internal structures when a SchemaOwner is created via Parse(). |
136 const internal::SchemaNode* ParseDictionary( | 133 // SchemaOwners that Wrap() a SchemaData have a NULL storage. |
137 const base::DictionaryValue& schema, | 134 scoped_ptr<InternalStorage> storage_; |
138 std::string* error); | 135 const internal::SchemaData* data_; |
139 | |
140 // Helper for Parse(). | |
141 const internal::SchemaNode* ParseList(const base::DictionaryValue& schema, | |
142 std::string* error); | |
143 | |
144 const internal::SchemaNode* root_; | |
145 ScopedVector<internal::SchemaNode> schema_nodes_; | |
146 // Note: |property_nodes_| contains PropertyNode[] elements and must be | |
147 // cleared manually to properly use delete[]. | |
148 std::vector<internal::PropertyNode*> property_nodes_; | |
149 ScopedVector<internal::PropertiesNode> properties_nodes_; | |
150 ScopedVector<std::string> keys_; | |
151 | 136 |
152 DISALLOW_COPY_AND_ASSIGN(SchemaOwner); | 137 DISALLOW_COPY_AND_ASSIGN(SchemaOwner); |
153 }; | 138 }; |
154 | 139 |
155 } // namespace policy | 140 } // namespace policy |
156 | 141 |
157 #endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ | 142 #endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ |
OLD | NEW |