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