OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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 "chrome/browser/policy/policy_schema.h" | |
6 | |
7 #include "base/json/json_reader.h" | |
8 #include "base/logging.h" | |
9 #include "chrome/common/json_schema/json_schema_constants.h" | |
10 #include "chrome/common/json_schema/json_schema_validator.h" | |
11 | |
12 namespace policy { | |
13 | |
14 namespace { | |
15 | |
16 const char kJSONSchemaVersion[] = "http://json-schema.org/draft-03/schema#"; | |
17 | |
18 bool SchemaTypeToValueType(const std::string& type_string, | |
19 base::Value::Type* type) { | |
20 // Note: "any" is not an accepted type. | |
21 static const struct { | |
22 const char* schema_type; | |
23 base::Value::Type value_type; | |
24 } kSchemaToValueTypeMap[] = { | |
25 { json_schema_constants::kArray, base::Value::TYPE_LIST }, | |
26 { json_schema_constants::kBoolean, base::Value::TYPE_BOOLEAN }, | |
27 { json_schema_constants::kInteger, base::Value::TYPE_INTEGER }, | |
28 { json_schema_constants::kNull, base::Value::TYPE_NULL }, | |
29 { json_schema_constants::kNumber, base::Value::TYPE_DOUBLE }, | |
30 { json_schema_constants::kObject, base::Value::TYPE_DICTIONARY }, | |
31 { json_schema_constants::kString, base::Value::TYPE_STRING }, | |
32 }; | |
33 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSchemaToValueTypeMap); ++i) { | |
34 if (kSchemaToValueTypeMap[i].schema_type == type_string) { | |
35 *type = kSchemaToValueTypeMap[i].value_type; | |
36 return true; | |
37 } | |
38 } | |
39 return false; | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 // static | |
45 scoped_ptr<PolicySchema> PolicySchema::Parse(const std::string& content, | |
46 std::string* error) { | |
47 // Validate as a generic JSON schema. | |
48 scoped_ptr<base::DictionaryValue> schema = | |
49 JSONSchemaValidator::IsValidSchema(content, error); | |
50 if (!schema) | |
51 return scoped_ptr<PolicySchema>(); | |
52 | |
53 // Validate the schema version. | |
54 std::string string_value; | |
55 if (!schema->GetString(json_schema_constants::kSchema, &string_value) || | |
56 string_value != kJSONSchemaVersion) { | |
57 *error = "Must declare JSON Schema v3 version in \"$schema\""; | |
Mattias Nissler (ping if slow)
2013/05/15 10:04:12
Is this a good idea? This will prevent upgrading t
Joao da Silva
2013/05/19 13:17:08
If a new format is introduced then Chrome X will s
| |
58 return scoped_ptr<PolicySchema>(); | |
59 } | |
60 | |
61 // Validate the main type. | |
62 if (!schema->GetString(json_schema_constants::kType, &string_value) || | |
63 string_value != json_schema_constants::kObject) { | |
64 *error = "The main schema must have a type attribute with \"object\" value"; | |
65 return scoped_ptr<PolicySchema>(); | |
66 } | |
67 | |
68 // Validate the properties and convert into a TypeMap. | |
69 if (schema->HasKey(json_schema_constants::kAdditionalProperties) || | |
70 schema->HasKey(json_schema_constants::kPatternProperties)) { | |
71 *error = "\"additionalProperties\" and \"patternProperties\" are not " | |
72 "supported at the main schema"; | |
Mattias Nissler (ping if slow)
2013/05/15 10:04:12
Ditto: Should this be a hard error or merely a war
Joao da Silva
2013/05/19 13:17:08
This restriction is forced because some policy loa
| |
73 return scoped_ptr<PolicySchema>(); | |
74 } | |
75 | |
76 TypeMap map; | |
77 const base::DictionaryValue* properties = NULL; | |
78 if (schema->GetDictionary(json_schema_constants::kProperties, &properties)) { | |
79 for (base::DictionaryValue::Iterator it(*properties); | |
80 !it.IsAtEnd(); it.Advance()) { | |
81 const base::DictionaryValue* dict = NULL; | |
82 // This should have been verified by the JSONSchemaValidator. | |
83 CHECK(it.value().GetAsDictionary(&dict)); | |
84 std::string type_string; | |
85 if (!dict->GetString(json_schema_constants::kType, &type_string)) { | |
86 *error = "The types of the main properties must be string values"; | |
87 return scoped_ptr<PolicySchema>(); | |
88 } | |
89 base::Value::Type type = base::Value::TYPE_NULL; | |
90 if (!SchemaTypeToValueType(type_string, &type)) { | |
91 *error = "The type of main properties can't be \"any\""; | |
Mattias Nissler (ping if slow)
2013/05/15 10:04:12
Forward-compatibility?
Joao da Silva
2013/05/19 13:17:08
Same rationale again; we can roll an updated versi
| |
92 return scoped_ptr<PolicySchema>(); | |
93 } | |
94 map[it.key()] = type; | |
95 } | |
96 } | |
97 | |
98 scoped_ptr<PolicySchema> policy_schema(new PolicySchema()); | |
99 policy_schema->type_map_.swap(map); | |
100 return policy_schema.Pass(); | |
101 } | |
102 | |
103 PolicySchema::~PolicySchema() {} | |
104 | |
105 PolicySchema::PolicySchema() {} | |
106 | |
107 } // namespace policy | |
OLD | NEW |