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 #include "components/policy/core/common/schema.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/scoped_vector.h" |
| 10 |
| 11 namespace policy { |
| 12 |
| 13 namespace schema { |
| 14 |
| 15 namespace internal { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // XXX: this would actually be generated but depends on the templates in |
| 20 // //chrome. |
| 21 |
| 22 // Shared types: |
| 23 |
| 24 const SchemaNode kTypeBoolean = { |
| 25 base::Value::TYPE_BOOLEAN, |
| 26 NULL, |
| 27 }; |
| 28 |
| 29 const SchemaNode kTypeDouble = { |
| 30 base::Value::TYPE_DOUBLE, |
| 31 NULL, |
| 32 }; |
| 33 |
| 34 const SchemaNode kTypeInteger = { |
| 35 base::Value::TYPE_INTEGER, |
| 36 NULL, |
| 37 }; |
| 38 |
| 39 const SchemaNode kTypeString = { |
| 40 base::Value::TYPE_STRING, |
| 41 NULL, |
| 42 }; |
| 43 |
| 44 // Custom types for structure policies (generated): |
| 45 |
| 46 const PropertyNode kPropertyList_ProxySettings[] = { |
| 47 { |
| 48 "ProxyServer", |
| 49 &kTypeString, |
| 50 }, |
| 51 { |
| 52 "ProxyMode", |
| 53 &kTypeInteger, |
| 54 }, |
| 55 // etc |
| 56 }; |
| 57 |
| 58 const PropertiesNode kProperties_ProxySettings = { |
| 59 kPropertyList_ProxySettings, |
| 60 kPropertyList_ProxySettings + arraysize(kPropertyList_ProxySettings), |
| 61 NULL, |
| 62 }; |
| 63 |
| 64 const SchemaNode kTypePolicy_ProxySettings = { |
| 65 base::Value::TYPE_DICTIONARY, |
| 66 &kProperties_ProxySettings, |
| 67 }; |
| 68 |
| 69 // Chrome Policy list: |
| 70 |
| 71 const PropertyNode kChromePolicyList[] = { |
| 72 { |
| 73 "HomepageLocation", // actualy key::kHomepageLocation |
| 74 &kTypeString, |
| 75 }, |
| 76 { |
| 77 "ShowHomeButton", // actualy key::kShowHomeButton |
| 78 &kTypeBoolean, |
| 79 }, |
| 80 { |
| 81 "ProxySettings", // actually key::kProxySettings |
| 82 &kTypePolicy_ProxySettings, |
| 83 }, |
| 84 }; |
| 85 |
| 86 const PropertiesNode kChromePolicies = { |
| 87 kChromePolicyList, |
| 88 kChromePolicyList + arraysize(kChromePolicyList), |
| 89 NULL, |
| 90 }; |
| 91 |
| 92 const SchemaNode kChromeSchema = { |
| 93 base::Value::TYPE_DICTIONARY, |
| 94 &kChromePolicies, |
| 95 }; |
| 96 |
| 97 // PolicySchema implementation for the Chrome policies. Its internal nodes are |
| 98 // generated at compile time, and don't need to be owned by this class. |
| 99 class ChromePolicySchema : public PolicySchema { |
| 100 public: |
| 101 ChromePolicySchema() {} |
| 102 virtual ~ChromePolicySchema() {} |
| 103 |
| 104 virtual Schema schema() const { |
| 105 return Schema(&kChromeSchema); |
| 106 } |
| 107 }; |
| 108 |
| 109 // A PolicySchema implementation that can own internal nodes, generated by |
| 110 // parsing a schema string at runtime. |
| 111 class ParsedPolicySchema : public PolicySchema { |
| 112 public: |
| 113 static scoped_ptr<PolicySchema> Parse(const std::string& schema, |
| 114 std::string* error) { |
| 115 // TODO: parse & return if valid. Internal nodes are owned by the returned |
| 116 // object. |
| 117 *error = "todo"; |
| 118 return scoped_ptr<PolicySchema>(); |
| 119 } |
| 120 |
| 121 virtual ~ParsedPolicySchema() {} |
| 122 |
| 123 virtual Schema schema() const { |
| 124 return Schema(root_); |
| 125 } |
| 126 |
| 127 private: |
| 128 internal::SchemaNode* root_; |
| 129 ScopedVector<internal::SchemaNode> schema_nodes_; |
| 130 ScopedVector<internal::PropertyNode> property_nodes_; |
| 131 ScopedVector<internal::PropertiesNode> properties_nodes_; |
| 132 }; |
| 133 |
| 134 } // namespace |
| 135 |
| 136 } // namespace internal |
| 137 |
| 138 |
| 139 // Schema::Iterator |
| 140 |
| 141 Schema::Iterator::Iterator(const internal::PropertiesNode* properties) |
| 142 : it_(properties->begin), |
| 143 end_(properties->end) {} |
| 144 |
| 145 Schema::Iterator::Iterator(const Iterator& iterator) |
| 146 : it_(iterator.it_), |
| 147 end_(iterator.end_) {} |
| 148 |
| 149 Schema::Iterator::~Iterator() {} |
| 150 |
| 151 Schema::Iterator& Schema::Iterator::operator=(const Iterator& iterator) { |
| 152 it_ = iterator.it_; |
| 153 end_ = iterator.end_; |
| 154 return *this; |
| 155 } |
| 156 |
| 157 bool Schema::Iterator::IsAtEnd() const { |
| 158 return it_ == end_; |
| 159 } |
| 160 |
| 161 void Schema::Iterator::Advance() { |
| 162 ++it_; |
| 163 } |
| 164 |
| 165 const char* Schema::Iterator::key() const { |
| 166 return it_->key; |
| 167 } |
| 168 |
| 169 Schema Schema::Iterator::schema() const { |
| 170 return Schema(it_->schema); |
| 171 } |
| 172 |
| 173 |
| 174 // Schema |
| 175 |
| 176 Schema::Schema(const internal::SchemaNode* schema) : schema_(schema) {} |
| 177 |
| 178 Schema::Schema(const Schema& schema) : schema_(schema.schema_) {} |
| 179 |
| 180 Schema& Schema::operator=(const Schema& schema) { |
| 181 schema_ = schema.schema_; |
| 182 return *this; |
| 183 } |
| 184 |
| 185 base::Value::Type Schema::type() const { |
| 186 CHECK(valid()); |
| 187 return schema_->type; |
| 188 } |
| 189 |
| 190 Schema::Iterator Schema::GetPropertiesIterator() const { |
| 191 CHECK(valid()); |
| 192 CHECK_EQ(base::Value::TYPE_DICTIONARY, type()); |
| 193 return Iterator( |
| 194 static_cast<const internal::PropertiesNode*>(schema_->extra)); |
| 195 } |
| 196 |
| 197 Schema Schema::GetKnownProperty(const std::string& key) const { |
| 198 CHECK(valid()); |
| 199 CHECK_EQ(base::Value::TYPE_DICTIONARY, type()); |
| 200 // TODO: binary search on schema_->extra. |
| 201 return Schema(NULL); |
| 202 } |
| 203 |
| 204 Schema Schema::GetAdditionalProperties() const { |
| 205 CHECK(valid()); |
| 206 CHECK_EQ(base::Value::TYPE_DICTIONARY, type()); |
| 207 return Schema( |
| 208 static_cast<const internal::PropertiesNode*>(schema_->extra)->additional); |
| 209 } |
| 210 |
| 211 Schema Schema::GetProperty(const std::string& key) const { |
| 212 Schema schema = GetKnownProperty(key); |
| 213 return schema.valid() ? schema : GetAdditionalProperties(); |
| 214 } |
| 215 |
| 216 Schema Schema::GetItems() const { |
| 217 CHECK(valid()); |
| 218 CHECK_EQ(base::Value::TYPE_LIST, type()); |
| 219 return Schema(static_cast<const internal::SchemaNode*>(schema_->extra)); |
| 220 } |
| 221 |
| 222 |
| 223 // PolicySchema |
| 224 |
| 225 // static |
| 226 scoped_ptr<PolicySchema> PolicySchema::GetChromePolicySchema() { |
| 227 return scoped_ptr<PolicySchema>(new internal::ChromePolicySchema()); |
| 228 } |
| 229 |
| 230 // static |
| 231 scoped_ptr<PolicySchema> PolicySchema::Parse(const std::string& schema, |
| 232 std::string* error) { |
| 233 return internal::ParsedPolicySchema::Parse(schema, error); |
| 234 } |
| 235 |
| 236 } // namespace schema |
| 237 |
| 238 } // namespace policy |
OLD | NEW |