Index: components/policy/core/common/schema.cc |
diff --git a/components/policy/core/common/schema.cc b/components/policy/core/common/schema.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..79a79d3bc19cc95c5df0792dbcf189acd0e36d92 |
--- /dev/null |
+++ b/components/policy/core/common/schema.cc |
@@ -0,0 +1,238 @@ |
+// 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. |
+ |
+#include "components/policy/core/common/schema.h" |
+ |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/memory/scoped_vector.h" |
+ |
+namespace policy { |
+ |
+namespace schema { |
+ |
+namespace internal { |
+ |
+namespace { |
+ |
+// XXX: this would actually be generated but depends on the templates in |
+// //chrome. |
+ |
+// Shared types: |
+ |
+const SchemaNode kTypeBoolean = { |
+ base::Value::TYPE_BOOLEAN, |
+ NULL, |
+}; |
+ |
+const SchemaNode kTypeDouble = { |
+ base::Value::TYPE_DOUBLE, |
+ NULL, |
+}; |
+ |
+const SchemaNode kTypeInteger = { |
+ base::Value::TYPE_INTEGER, |
+ NULL, |
+}; |
+ |
+const SchemaNode kTypeString = { |
+ base::Value::TYPE_STRING, |
+ NULL, |
+}; |
+ |
+// Custom types for structure policies (generated): |
+ |
+const PropertyNode kPropertyList_ProxySettings[] = { |
+ { |
+ "ProxyServer", |
+ &kTypeString, |
+ }, |
+ { |
+ "ProxyMode", |
+ &kTypeInteger, |
+ }, |
+ // etc |
+}; |
+ |
+const PropertiesNode kProperties_ProxySettings = { |
+ kPropertyList_ProxySettings, |
+ kPropertyList_ProxySettings + arraysize(kPropertyList_ProxySettings), |
+ NULL, |
+}; |
+ |
+const SchemaNode kTypePolicy_ProxySettings = { |
+ base::Value::TYPE_DICTIONARY, |
+ &kProperties_ProxySettings, |
+}; |
+ |
+// Chrome Policy list: |
+ |
+const PropertyNode kChromePolicyList[] = { |
+ { |
+ "HomepageLocation", // actualy key::kHomepageLocation |
+ &kTypeString, |
+ }, |
+ { |
+ "ShowHomeButton", // actualy key::kShowHomeButton |
+ &kTypeBoolean, |
+ }, |
+ { |
+ "ProxySettings", // actually key::kProxySettings |
+ &kTypePolicy_ProxySettings, |
+ }, |
+}; |
+ |
+const PropertiesNode kChromePolicies = { |
+ kChromePolicyList, |
+ kChromePolicyList + arraysize(kChromePolicyList), |
+ NULL, |
+}; |
+ |
+const SchemaNode kChromeSchema = { |
+ base::Value::TYPE_DICTIONARY, |
+ &kChromePolicies, |
+}; |
+ |
+// PolicySchema implementation for the Chrome policies. Its internal nodes are |
+// generated at compile time, and don't need to be owned by this class. |
+class ChromePolicySchema : public PolicySchema { |
+ public: |
+ ChromePolicySchema() {} |
+ virtual ~ChromePolicySchema() {} |
+ |
+ virtual Schema schema() const { |
+ return Schema(&kChromeSchema); |
+ } |
+}; |
+ |
+// A PolicySchema implementation that can own internal nodes, generated by |
+// parsing a schema string at runtime. |
+class ParsedPolicySchema : public PolicySchema { |
+ public: |
+ static scoped_ptr<PolicySchema> Parse(const std::string& schema, |
+ std::string* error) { |
+ // TODO: parse & return if valid. Internal nodes are owned by the returned |
+ // object. |
+ *error = "todo"; |
+ return scoped_ptr<PolicySchema>(); |
+ } |
+ |
+ virtual ~ParsedPolicySchema() {} |
+ |
+ virtual Schema schema() const { |
+ return Schema(root_); |
+ } |
+ |
+ private: |
+ internal::SchemaNode* root_; |
+ ScopedVector<internal::SchemaNode> schema_nodes_; |
+ ScopedVector<internal::PropertyNode> property_nodes_; |
+ ScopedVector<internal::PropertiesNode> properties_nodes_; |
+}; |
+ |
+} // namespace |
+ |
+} // namespace internal |
+ |
+ |
+// Schema::Iterator |
+ |
+Schema::Iterator::Iterator(const internal::PropertiesNode* properties) |
+ : it_(properties->begin), |
+ end_(properties->end) {} |
+ |
+Schema::Iterator::Iterator(const Iterator& iterator) |
+ : it_(iterator.it_), |
+ end_(iterator.end_) {} |
+ |
+Schema::Iterator::~Iterator() {} |
+ |
+Schema::Iterator& Schema::Iterator::operator=(const Iterator& iterator) { |
+ it_ = iterator.it_; |
+ end_ = iterator.end_; |
+ return *this; |
+} |
+ |
+bool Schema::Iterator::IsAtEnd() const { |
+ return it_ == end_; |
+} |
+ |
+void Schema::Iterator::Advance() { |
+ ++it_; |
+} |
+ |
+const char* Schema::Iterator::key() const { |
+ return it_->key; |
+} |
+ |
+Schema Schema::Iterator::schema() const { |
+ return Schema(it_->schema); |
+} |
+ |
+ |
+// Schema |
+ |
+Schema::Schema(const internal::SchemaNode* schema) : schema_(schema) {} |
+ |
+Schema::Schema(const Schema& schema) : schema_(schema.schema_) {} |
+ |
+Schema& Schema::operator=(const Schema& schema) { |
+ schema_ = schema.schema_; |
+ return *this; |
+} |
+ |
+base::Value::Type Schema::type() const { |
+ CHECK(valid()); |
+ return schema_->type; |
+} |
+ |
+Schema::Iterator Schema::GetPropertiesIterator() const { |
+ CHECK(valid()); |
+ CHECK_EQ(base::Value::TYPE_DICTIONARY, type()); |
+ return Iterator( |
+ static_cast<const internal::PropertiesNode*>(schema_->extra)); |
+} |
+ |
+Schema Schema::GetKnownProperty(const std::string& key) const { |
+ CHECK(valid()); |
+ CHECK_EQ(base::Value::TYPE_DICTIONARY, type()); |
+ // TODO: binary search on schema_->extra. |
+ return Schema(NULL); |
+} |
+ |
+Schema Schema::GetAdditionalProperties() const { |
+ CHECK(valid()); |
+ CHECK_EQ(base::Value::TYPE_DICTIONARY, type()); |
+ return Schema( |
+ static_cast<const internal::PropertiesNode*>(schema_->extra)->additional); |
+} |
+ |
+Schema Schema::GetProperty(const std::string& key) const { |
+ Schema schema = GetKnownProperty(key); |
+ return schema.valid() ? schema : GetAdditionalProperties(); |
+} |
+ |
+Schema Schema::GetItems() const { |
+ CHECK(valid()); |
+ CHECK_EQ(base::Value::TYPE_LIST, type()); |
+ return Schema(static_cast<const internal::SchemaNode*>(schema_->extra)); |
+} |
+ |
+ |
+// PolicySchema |
+ |
+// static |
+scoped_ptr<PolicySchema> PolicySchema::GetChromePolicySchema() { |
+ return scoped_ptr<PolicySchema>(new internal::ChromePolicySchema()); |
+} |
+ |
+// static |
+scoped_ptr<PolicySchema> PolicySchema::Parse(const std::string& schema, |
+ std::string* error) { |
+ return internal::ParsedPolicySchema::Parse(schema, error); |
+} |
+ |
+} // namespace schema |
+ |
+} // namespace policy |