Chromium Code Reviews| Index: components/policy/core/common/schema.h |
| diff --git a/components/policy/core/common/schema.h b/components/policy/core/common/schema.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..40c48b8f3417d4c68083367317e1eb4fc3306b55 |
| --- /dev/null |
| +++ b/components/policy/core/common/schema.h |
| @@ -0,0 +1,125 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ |
| +#define COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ |
| + |
| +#include <string> |
| + |
| +#include "base/basictypes.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/values.h" |
| +#include "components/policy/policy_export.h" |
| + |
| +namespace policy { |
| + |
|
Mattias Nissler (ping if slow)
2013/09/13 12:56:48
I think we usually don't put blank lines between n
Joao da Silva
2013/09/13 14:02:54
The style guide has an example with nested namespa
|
| +namespace schema { |
|
Mattias Nissler (ping if slow)
2013/09/13 12:56:48
This namespace is not really useful (cf. current d
Joao da Silva
2013/09/13 14:02:54
Alright, removed. Renamed PolicySchema to SchemaOw
|
| + |
| +namespace internal { |
| + |
| +struct POLICY_EXPORT SchemaNode; |
| +struct POLICY_EXPORT PropertyNode; |
| +struct POLICY_EXPORT PropertiesNode; |
| + |
| +} // namespace internal |
| + |
| +// Describes the expected type of one policy. Also recursively describes the |
| +// types of inner elements, for structured types. |
| +// Objects of this class refer to external, immutable data and are cheap to |
| +// copy. |
| +// Use the PolicySchema class to parse a schema and get Schema objects. |
| +class POLICY_EXPORT Schema { |
| + public: |
| + explicit Schema(const internal::SchemaNode* schema); |
| + Schema(const Schema& schema); |
| + |
| + Schema& operator=(const Schema& schema); |
| + |
| + // Returns true if this Schema is valid. Schemas returned by the methods below |
| + // may be invalid, and in those cases the other methods must not be used. |
| + bool valid() const { return schema_ != NULL; } |
| + |
| + base::Value::Type type() const; |
| + |
| + // Used to iterate over the known properties of TYPE_DICTIONARY schemas. |
| + class POLICY_EXPORT Iterator { |
| + public: |
| + explicit Iterator(const internal::PropertiesNode* properties); |
| + Iterator(const Iterator& iterator); |
| + ~Iterator(); |
| + |
| + Iterator& operator=(const Iterator& iterator); |
| + |
| + // The other methods must not be called if the iterator is at the end. |
| + bool IsAtEnd() const; |
| + |
| + // Advances the iterator to the next property. |
| + void Advance(); |
| + |
| + // Returns the name of the current property. |
| + const char* key() const; |
| + |
| + // Returns the Schema for the current property. This Schema is always valid. |
| + Schema schema() const; |
| + |
| + private: |
| + const internal::PropertyNode* it_; |
| + const internal::PropertyNode* end_; |
| + }; |
| + |
|
Mattias Nissler (ping if slow)
2013/09/13 12:56:48
remove extra whitespace
Joao da Silva
2013/09/13 14:02:54
Done.
|
| + |
| + // These methods can be called only if type() == TYPE_DICTIONARY. |
|
Mattias Nissler (ping if slow)
2013/09/13 12:56:48
s/can/should/, and ideally describe what happens i
Joao da Silva
2013/09/13 14:02:54
Done.
|
| + |
| + // Returns an iterator that goes over the named properties of this schema. |
| + // The returned iterator is at the beginning. |
| + Iterator GetPropertiesIterator() const; |
| + |
| + // Returns the Schema for the property named |key|. If |key| is not a known |
| + // property name then the returned Schema is not valid. |
| + Schema GetKnownProperty(const std::string& key) const; |
| + |
| + // Returns the Schema for additional properties. If additional properties are |
| + // not allowed for this Schema then the Schema returned is not valid. |
| + Schema GetAdditionalProperties() const; |
| + |
| + // Returns the Schema for |key| if it is a known property, otherwise returns |
| + // the Schema for additional properties. |
| + Schema GetProperty(const std::string& key) const; |
| + |
|
Mattias Nissler (ping if slow)
2013/09/13 12:56:48
remove extra whitespace.
Joao da Silva
2013/09/13 14:02:54
Done.
|
| + |
| + // Returns the Schema for items of an array. |
| + // This method can be called only if type() == TYPE_LIST. |
| + Schema GetItems() const; |
| + |
| + private: |
| + const internal::SchemaNode* schema_; |
| +}; |
| + |
| +// Abstract class that owns schemas for policies of a given component. |
| +class POLICY_EXPORT PolicySchema { |
| + public: |
| + virtual ~PolicySchema() {} |
| + |
| + // The returned Schema is valid only during the lifetime of the PolicySchema |
| + // that created it. It may be obtained multiple times. |
| + virtual Schema schema() const = 0; |
| + |
| + // Returns a PolicySchema that references static data. This can be used by |
| + // the embedder to pass structures generated at compile time, which can then |
| + // be quickly loaded at runtime. |
| + // Note: PropertiesNodes must have their PropertyNodes sorted by key. |
| + static scoped_ptr<PolicySchema> Wrap(const internal::SchemaNode* schema); |
| + |
| + // Parses the JSON schema in |schema| and returns a PolicySchema that owns |
| + // the internal representation. If |schema| is invalid then NULL is returned |
| + // and |error| contains a reason for the failure. |
| + static scoped_ptr<PolicySchema> Parse(const std::string& schema, |
| + std::string* error); |
| +}; |
| + |
| +} // namespace schema |
| + |
| +} // namespace policy |
| + |
| +#endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ |