| 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/scoped_ptr.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 // Describes the expected type of one policy. Also recursively describes the | 25 // Describes the expected type of one policy. Also recursively describes the |
| 26 // types of inner elements, for structured types. | 26 // types of inner elements, for structured types. |
| 27 // 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 |
| 28 // copy. | 28 // copy. |
| 29 // Use the SchemaOwner class to parse a schema and get Schema objects. | |
| 30 class POLICY_EXPORT Schema { | 29 class POLICY_EXPORT Schema { |
| 31 public: | 30 public: |
| 31 // Used internally to store shared data. |
| 32 class InternalStorage; |
| 33 |
| 32 // Builds an empty, invalid schema. | 34 // Builds an empty, invalid schema. |
| 33 Schema(); | 35 Schema(); |
| 34 | 36 |
| 35 // Builds a schema pointing to the inner structure of |data|, | 37 // Makes a copy of |schema| that shares the same internal storage. |
| 36 // rooted at |node|. | 38 Schema(const Schema& schema); |
| 37 Schema(const internal::SchemaData* data, const internal::SchemaNode* node); | |
| 38 | 39 |
| 39 Schema(const Schema& schema); | 40 ~Schema(); |
| 40 | 41 |
| 41 Schema& operator=(const Schema& schema); | 42 Schema& operator=(const Schema& schema); |
| 42 | 43 |
| 44 // Returns a Schema that references static data. This can be used by |
| 45 // the embedder to pass structures generated at compile time, which can then |
| 46 // be quickly loaded at runtime. |
| 47 static Schema Wrap(const internal::SchemaData* data); |
| 48 |
| 49 // Parses the JSON schema in |schema| and returns a Schema that owns |
| 50 // the internal representation. If |schema| is invalid then an invalid Schema |
| 51 // is returned and |error| contains a reason for the failure. |
| 52 static Schema Parse(const std::string& schema, std::string* error); |
| 53 |
| 43 // Returns true if this Schema is valid. Schemas returned by the methods below | 54 // Returns true if this Schema is valid. Schemas returned by the methods below |
| 44 // may be invalid, and in those cases the other methods must not be used. | 55 // may be invalid, and in those cases the other methods must not be used. |
| 45 bool valid() const { return data_ != NULL; } | 56 bool valid() const { return node_ != NULL; } |
| 46 | 57 |
| 47 base::Value::Type type() const; | 58 base::Value::Type type() const; |
| 48 | 59 |
| 49 // Used to iterate over the known properties of TYPE_DICTIONARY schemas. | 60 // Used to iterate over the known properties of TYPE_DICTIONARY schemas. |
| 50 class POLICY_EXPORT Iterator { | 61 class POLICY_EXPORT Iterator { |
| 51 public: | 62 public: |
| 52 Iterator(const internal::SchemaData* data, | 63 Iterator(const scoped_refptr<const InternalStorage>& storage, |
| 53 const internal::PropertiesNode* node); | 64 const internal::PropertiesNode* node); |
| 54 Iterator(const Iterator& iterator); | 65 Iterator(const Iterator& iterator); |
| 55 ~Iterator(); | 66 ~Iterator(); |
| 56 | 67 |
| 57 Iterator& operator=(const Iterator& iterator); | 68 Iterator& operator=(const Iterator& iterator); |
| 58 | 69 |
| 59 // The other methods must not be called if the iterator is at the end. | 70 // The other methods must not be called if the iterator is at the end. |
| 60 bool IsAtEnd() const; | 71 bool IsAtEnd() const; |
| 61 | 72 |
| 62 // Advances the iterator to the next property. | 73 // Advances the iterator to the next property. |
| 63 void Advance(); | 74 void Advance(); |
| 64 | 75 |
| 65 // Returns the name of the current property. | 76 // Returns the name of the current property. |
| 66 const char* key() const; | 77 const char* key() const; |
| 67 | 78 |
| 68 // Returns the Schema for the current property. This Schema is always valid. | 79 // Returns the Schema for the current property. This Schema is always valid. |
| 69 Schema schema() const; | 80 Schema schema() const; |
| 70 | 81 |
| 71 private: | 82 private: |
| 72 const internal::SchemaData* data_; | 83 scoped_refptr<const InternalStorage> storage_; |
| 73 const internal::PropertyNode* it_; | 84 const internal::PropertyNode* it_; |
| 74 const internal::PropertyNode* end_; | 85 const internal::PropertyNode* end_; |
| 75 }; | 86 }; |
| 76 | 87 |
| 77 // These methods should be called only if type() == TYPE_DICTIONARY, | 88 // These methods should be called only if type() == TYPE_DICTIONARY, |
| 78 // otherwise invalid memory will be read. A CHECK is currently enforcing this. | 89 // otherwise invalid memory will be read. A CHECK is currently enforcing this. |
| 79 | 90 |
| 80 // Returns an iterator that goes over the named properties of this schema. | 91 // Returns an iterator that goes over the named properties of this schema. |
| 81 // The returned iterator is at the beginning. | 92 // The returned iterator is at the beginning. |
| 82 Iterator GetPropertiesIterator() const; | 93 Iterator GetPropertiesIterator() const; |
| 83 | 94 |
| 84 // Returns the Schema for the property named |key|. If |key| is not a known | 95 // Returns the Schema for the property named |key|. If |key| is not a known |
| 85 // property name then the returned Schema is not valid. | 96 // property name then the returned Schema is not valid. |
| 86 Schema GetKnownProperty(const std::string& key) const; | 97 Schema GetKnownProperty(const std::string& key) const; |
| 87 | 98 |
| 88 // Returns the Schema for additional properties. If additional properties are | 99 // Returns the Schema for additional properties. If additional properties are |
| 89 // not allowed for this Schema then the Schema returned is not valid. | 100 // not allowed for this Schema then the Schema returned is not valid. |
| 90 Schema GetAdditionalProperties() const; | 101 Schema GetAdditionalProperties() const; |
| 91 | 102 |
| 92 // Returns the Schema for |key| if it is a known property, otherwise returns | 103 // Returns the Schema for |key| if it is a known property, otherwise returns |
| 93 // the Schema for additional properties. | 104 // the Schema for additional properties. |
| 94 Schema GetProperty(const std::string& key) const; | 105 Schema GetProperty(const std::string& key) const; |
| 95 | 106 |
| 96 // Returns the Schema for items of an array. | 107 // Returns the Schema for items of an array. |
| 97 // This method should be called only if type() == TYPE_LIST, | 108 // This method should be called only if type() == TYPE_LIST, |
| 98 // otherwise invalid memory will be read. A CHECK is currently enforcing this. | 109 // otherwise invalid memory will be read. A CHECK is currently enforcing this. |
| 99 Schema GetItems() const; | 110 Schema GetItems() const; |
| 100 | 111 |
| 101 private: | 112 private: |
| 102 const internal::SchemaData* data_; | 113 // Builds a schema pointing to the inner structure of |storage|, |
| 114 // rooted at |node|. |
| 115 Schema(const scoped_refptr<const InternalStorage>& storage, |
| 116 const internal::SchemaNode* node); |
| 117 |
| 118 scoped_refptr<const InternalStorage> storage_; |
| 103 const internal::SchemaNode* node_; | 119 const internal::SchemaNode* node_; |
| 104 }; | 120 }; |
| 105 | 121 |
| 106 // Owns schemas for policies of a given component. | |
| 107 class POLICY_EXPORT SchemaOwner { | |
| 108 public: | |
| 109 ~SchemaOwner(); | |
| 110 | |
| 111 // The returned Schema is valid only during the lifetime of the SchemaOwner | |
| 112 // that created it. It may be obtained multiple times. | |
| 113 Schema schema() const; | |
| 114 | |
| 115 // Returns a SchemaOwner that references static data. This can be used by | |
| 116 // the embedder to pass structures generated at compile time, which can then | |
| 117 // be quickly loaded at runtime. | |
| 118 static scoped_ptr<SchemaOwner> Wrap(const internal::SchemaData* data); | |
| 119 | |
| 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 | |
| 122 // and |error| contains a reason for the failure. | |
| 123 static scoped_ptr<SchemaOwner> Parse(const std::string& schema, | |
| 124 std::string* error); | |
| 125 | |
| 126 private: | |
| 127 class InternalStorage; | |
| 128 | |
| 129 SchemaOwner(const internal::SchemaData* data, | |
| 130 scoped_ptr<InternalStorage> storage); | |
| 131 | |
| 132 // Holds the internal structures when a SchemaOwner is created via Parse(). | |
| 133 // SchemaOwners that Wrap() a SchemaData have a NULL storage. | |
| 134 scoped_ptr<InternalStorage> storage_; | |
| 135 const internal::SchemaData* data_; | |
| 136 | |
| 137 DISALLOW_COPY_AND_ASSIGN(SchemaOwner); | |
| 138 }; | |
| 139 | |
| 140 } // namespace policy | 122 } // namespace policy |
| 141 | 123 |
| 142 #endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ | 124 #endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ |
| OLD | NEW |