Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Side by Side Diff: components/policy/core/common/schema.h

Issue 47513018: Make the internal storage of policy::Schemas ref counted. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@chrome-policy-schema-4-new-generate
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 Schema(const Schema& schema);
dconnelly 2013/10/29 16:43:58 Can you add a comment explaining that the new inst
Joao da Silva 2013/10/29 20:38:42 Done.
36 // rooted at |node|.
37 Schema(const internal::SchemaData* data, const internal::SchemaNode* node);
38 38
39 Schema(const Schema& schema); 39 ~Schema();
40 40
41 Schema& operator=(const Schema& schema); 41 Schema& operator=(const Schema& schema);
42 42
43 // Returns a Schema that references static data. This can be used by
44 // the embedder to pass structures generated at compile time, which can then
45 // be quickly loaded at runtime.
46 static Schema Wrap(const internal::SchemaData* data);
47
48 // Parses the JSON schema in |schema| and returns a Schema that owns
49 // the internal representation. If |schema| is invalid then an invalid Schema
50 // is returned and |error| contains a reason for the failure.
51 static Schema Parse(const std::string& schema, std::string* error);
52
43 // Returns true if this Schema is valid. Schemas returned by the methods below 53 // 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. 54 // may be invalid, and in those cases the other methods must not be used.
45 bool valid() const { return data_ != NULL; } 55 bool valid() const { return node_ != NULL; }
46 56
47 base::Value::Type type() const; 57 base::Value::Type type() const;
48 58
49 // Used to iterate over the known properties of TYPE_DICTIONARY schemas. 59 // Used to iterate over the known properties of TYPE_DICTIONARY schemas.
50 class POLICY_EXPORT Iterator { 60 class POLICY_EXPORT Iterator {
51 public: 61 public:
52 Iterator(const internal::SchemaData* data, 62 Iterator(const scoped_refptr<const InternalStorage>& storage,
53 const internal::PropertiesNode* node); 63 const internal::PropertiesNode* node);
54 Iterator(const Iterator& iterator); 64 Iterator(const Iterator& iterator);
55 ~Iterator(); 65 ~Iterator();
56 66
57 Iterator& operator=(const Iterator& iterator); 67 Iterator& operator=(const Iterator& iterator);
58 68
59 // The other methods must not be called if the iterator is at the end. 69 // The other methods must not be called if the iterator is at the end.
60 bool IsAtEnd() const; 70 bool IsAtEnd() const;
61 71
62 // Advances the iterator to the next property. 72 // Advances the iterator to the next property.
63 void Advance(); 73 void Advance();
64 74
65 // Returns the name of the current property. 75 // Returns the name of the current property.
66 const char* key() const; 76 const char* key() const;
67 77
68 // Returns the Schema for the current property. This Schema is always valid. 78 // Returns the Schema for the current property. This Schema is always valid.
69 Schema schema() const; 79 Schema schema() const;
70 80
71 private: 81 private:
72 const internal::SchemaData* data_; 82 scoped_refptr<const InternalStorage> storage_;
73 const internal::PropertyNode* it_; 83 const internal::PropertyNode* it_;
74 const internal::PropertyNode* end_; 84 const internal::PropertyNode* end_;
75 }; 85 };
76 86
77 // These methods should be called only if type() == TYPE_DICTIONARY, 87 // These methods should be called only if type() == TYPE_DICTIONARY,
78 // otherwise invalid memory will be read. A CHECK is currently enforcing this. 88 // otherwise invalid memory will be read. A CHECK is currently enforcing this.
79 89
80 // Returns an iterator that goes over the named properties of this schema. 90 // Returns an iterator that goes over the named properties of this schema.
81 // The returned iterator is at the beginning. 91 // The returned iterator is at the beginning.
82 Iterator GetPropertiesIterator() const; 92 Iterator GetPropertiesIterator() const;
83 93
84 // Returns the Schema for the property named |key|. If |key| is not a known 94 // Returns the Schema for the property named |key|. If |key| is not a known
85 // property name then the returned Schema is not valid. 95 // property name then the returned Schema is not valid.
86 Schema GetKnownProperty(const std::string& key) const; 96 Schema GetKnownProperty(const std::string& key) const;
87 97
88 // Returns the Schema for additional properties. If additional properties are 98 // Returns the Schema for additional properties. If additional properties are
89 // not allowed for this Schema then the Schema returned is not valid. 99 // not allowed for this Schema then the Schema returned is not valid.
90 Schema GetAdditionalProperties() const; 100 Schema GetAdditionalProperties() const;
91 101
92 // Returns the Schema for |key| if it is a known property, otherwise returns 102 // Returns the Schema for |key| if it is a known property, otherwise returns
93 // the Schema for additional properties. 103 // the Schema for additional properties.
94 Schema GetProperty(const std::string& key) const; 104 Schema GetProperty(const std::string& key) const;
95 105
96 // Returns the Schema for items of an array. 106 // Returns the Schema for items of an array.
97 // This method should be called only if type() == TYPE_LIST, 107 // This method should be called only if type() == TYPE_LIST,
98 // otherwise invalid memory will be read. A CHECK is currently enforcing this. 108 // otherwise invalid memory will be read. A CHECK is currently enforcing this.
99 Schema GetItems() const; 109 Schema GetItems() const;
100 110
101 private: 111 private:
102 const internal::SchemaData* data_; 112 // Builds a schema pointing to the inner structure of |storage|,
113 // rooted at |node|.
114 Schema(const scoped_refptr<const InternalStorage>& storage,
115 const internal::SchemaNode* node);
116
117 scoped_refptr<const InternalStorage> storage_;
103 const internal::SchemaNode* node_; 118 const internal::SchemaNode* node_;
104 }; 119 };
105 120
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 121 } // namespace policy
141 122
142 #endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_ 123 #endif // COMPONENTS_POLICY_CORE_COMMON_SCHEMA_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698