Index: components/policy/core/common/schema.cc |
diff --git a/components/policy/core/common/schema.cc b/components/policy/core/common/schema.cc |
index 0129094c75a0da6773318f81e40b3dfca581177c..28e8313fd2bee34737b7414dc0cd85c71fa246b4 100644 |
--- a/components/policy/core/common/schema.cc |
+++ b/components/policy/core/common/schema.cc |
@@ -88,7 +88,8 @@ class Schema::InternalStorage |
static scoped_refptr<const InternalStorage> ParseSchema( |
const base::DictionaryValue& schema, |
- std::string* error); |
+ std::string* error, |
+ bool with_backwards_compatibility); |
const SchemaData* data() const { return &schema_data_; } |
@@ -117,7 +118,8 @@ class Schema::InternalStorage |
// Determines the expected |sizes| of the storage for the representation |
// of |schema|. |
static void DetermineStorageSizes(const base::DictionaryValue& schema, |
- StorageSizes* sizes); |
+ StorageSizes* sizes, |
+ bool with_backwards_compatibility); |
// Parses the JSON schema in |schema|. |
// |
@@ -134,7 +136,8 @@ class Schema::InternalStorage |
int* index, |
IdMap* id_map, |
ReferenceList* reference_list, |
- std::string* error); |
+ std::string* error, |
+ bool with_backwards_compatibility); |
// Helper for Parse() that gets an already assigned |schema_node| instead of |
// an |index| pointer. |
@@ -142,7 +145,8 @@ class Schema::InternalStorage |
SchemaNode* schema_node, |
IdMap* id_map, |
ReferenceList* reference_list, |
- std::string* error); |
+ std::string* error, |
+ bool with_backwards_compatibility); |
// Helper for Parse() that gets an already assigned |schema_node| instead of |
// an |index| pointer. |
@@ -150,7 +154,8 @@ class Schema::InternalStorage |
SchemaNode* schema_node, |
IdMap* id_map, |
ReferenceList* reference_list, |
- std::string* error); |
+ std::string* error, |
+ bool with_backwards_compatibility); |
// Assigns the IDs in |id_map| to the pending references in the |
// |reference_list|. If an ID is missing then |error| is set and false is |
@@ -185,13 +190,14 @@ scoped_refptr<const Schema::InternalStorage> Schema::InternalStorage::Wrap( |
// static |
scoped_refptr<const Schema::InternalStorage> |
Schema::InternalStorage::ParseSchema(const base::DictionaryValue& schema, |
- std::string* error) { |
+ std::string* error, |
+ bool with_backwards_compatibility) { |
// Determine the sizes of the storage arrays and reserve the capacity before |
// starting to append nodes and strings. This is important to prevent the |
// arrays from being reallocated, which would invalidate the c_str() pointers |
// and the addresses of indices to fix. |
StorageSizes sizes; |
- DetermineStorageSizes(schema, &sizes); |
+ DetermineStorageSizes(schema, &sizes, with_backwards_compatibility); |
scoped_refptr<InternalStorage> storage = new InternalStorage(); |
storage->strings_.reserve(sizes.strings); |
@@ -202,8 +208,10 @@ Schema::InternalStorage::ParseSchema(const base::DictionaryValue& schema, |
int root_index = kInvalid; |
IdMap id_map; |
ReferenceList reference_list; |
- if (!storage->Parse(schema, &root_index, &id_map, &reference_list, error)) |
+ if (!storage->Parse(schema, &root_index, &id_map, &reference_list, error, |
+ with_backwards_compatibility)) { |
return NULL; |
+ } |
if (root_index == kInvalid) { |
*error = "The main schema can't have a $ref"; |
@@ -236,7 +244,8 @@ Schema::InternalStorage::ParseSchema(const base::DictionaryValue& schema, |
// static |
void Schema::InternalStorage::DetermineStorageSizes( |
const base::DictionaryValue& schema, |
- StorageSizes* sizes) { |
+ StorageSizes* sizes, |
+ bool with_backwards_compatibility) { |
std::string ref_string; |
if (schema.GetString(schema::kRef, &ref_string)) { |
// Schemas with a "$ref" attribute don't take additional storage. |
@@ -255,14 +264,20 @@ void Schema::InternalStorage::DetermineStorageSizes( |
if (type == base::Value::TYPE_LIST) { |
const base::DictionaryValue* items = NULL; |
- if (schema.GetDictionary(schema::kItems, &items)) |
- DetermineStorageSizes(*items, sizes); |
+ if (schema.GetDictionary(schema::kItems, &items)) { |
+ DetermineStorageSizes(*items, sizes, with_backwards_compatibility); |
+ } else if (with_backwards_compatibility) { |
+ // TODO(joaodasilva): remove this. http://crbug.com/325349 |
+ base::DictionaryValue items_schema; |
+ items_schema.SetString("type", "string"); |
+ DetermineStorageSizes(items_schema, sizes, true); |
+ } |
} else if (type == base::Value::TYPE_DICTIONARY) { |
sizes->properties_nodes++; |
const base::DictionaryValue* dict = NULL; |
if (schema.GetDictionary(schema::kAdditionalProperties, &dict)) |
- DetermineStorageSizes(*dict, sizes); |
+ DetermineStorageSizes(*dict, sizes, with_backwards_compatibility); |
const base::DictionaryValue* properties = NULL; |
if (schema.GetDictionary(schema::kProperties, &properties)) { |
@@ -270,7 +285,7 @@ void Schema::InternalStorage::DetermineStorageSizes( |
!it.IsAtEnd(); it.Advance()) { |
// This should have been verified by the JSONSchemaValidator. |
CHECK(it.value().GetAsDictionary(&dict)); |
- DetermineStorageSizes(*dict, sizes); |
+ DetermineStorageSizes(*dict, sizes, with_backwards_compatibility); |
sizes->strings++; |
sizes->property_nodes++; |
} |
@@ -282,7 +297,8 @@ bool Schema::InternalStorage::Parse(const base::DictionaryValue& schema, |
int* index, |
IdMap* id_map, |
ReferenceList* reference_list, |
- std::string* error) { |
+ std::string* error, |
+ bool with_backwards_compatibility) { |
std::string ref_string; |
if (schema.GetString(schema::kRef, &ref_string)) { |
std::string id_string; |
@@ -313,11 +329,15 @@ bool Schema::InternalStorage::Parse(const base::DictionaryValue& schema, |
schema_node->extra = kInvalid; |
if (type == base::Value::TYPE_DICTIONARY) { |
- if (!ParseDictionary(schema, schema_node, id_map, reference_list, error)) |
+ if (!ParseDictionary(schema, schema_node, id_map, reference_list, error, |
+ with_backwards_compatibility)) { |
return false; |
+ } |
} else if (type == base::Value::TYPE_LIST) { |
- if (!ParseList(schema, schema_node, id_map, reference_list, error)) |
+ if (!ParseList(schema, schema_node, id_map, reference_list, error, |
+ with_backwards_compatibility)) { |
return false; |
+ } |
} |
std::string id_string; |
@@ -337,7 +357,8 @@ bool Schema::InternalStorage::ParseDictionary( |
SchemaNode* schema_node, |
IdMap* id_map, |
ReferenceList* reference_list, |
- std::string* error) { |
+ std::string* error, |
+ bool with_backwards_compatibility) { |
int extra = static_cast<int>(properties_nodes_.size()); |
properties_nodes_.push_back(PropertiesNode()); |
properties_nodes_[extra].begin = kInvalid; |
@@ -348,7 +369,7 @@ bool Schema::InternalStorage::ParseDictionary( |
const base::DictionaryValue* dict = NULL; |
if (schema.GetDictionary(schema::kAdditionalProperties, &dict)) { |
if (!Parse(*dict, &properties_nodes_[extra].additional, |
- id_map, reference_list, error)) { |
+ id_map, reference_list, error, with_backwards_compatibility)) { |
return false; |
} |
} |
@@ -369,7 +390,7 @@ bool Schema::InternalStorage::ParseDictionary( |
strings_.push_back(it.key()); |
property_nodes_[index].key = strings_.back().c_str(); |
if (!Parse(*dict, &property_nodes_[index].schema, |
- id_map, reference_list, error)) { |
+ id_map, reference_list, error, with_backwards_compatibility)) { |
return false; |
} |
} |
@@ -385,13 +406,24 @@ bool Schema::InternalStorage::ParseList(const base::DictionaryValue& schema, |
SchemaNode* schema_node, |
IdMap* id_map, |
ReferenceList* reference_list, |
- std::string* error) { |
+ std::string* error, |
+ bool with_backwards_compatibility) { |
+ // TODO(joaodasilva): remove this. http://crbug.com/325349 |
+ base::DictionaryValue items; |
+ items.SetString("type", "string"); |
+ |
const base::DictionaryValue* dict = NULL; |
if (!schema.GetDictionary(schema::kItems, &dict)) { |
- *error = "Arrays must declare a single schema for their items."; |
- return false; |
+ if (with_backwards_compatibility) { |
+ dict = &items; |
+ } else { |
+ *error = "Arrays must declare a single schema for their items."; |
+ return false; |
+ } |
} |
- return Parse(*dict, &schema_node->extra, id_map, reference_list, error); |
+ |
+ return Parse(*dict, &schema_node->extra, id_map, reference_list, error, |
+ with_backwards_compatibility); |
} |
// static |
@@ -500,6 +532,14 @@ bool Schema::Validate(const base::Value& value) const { |
// static |
Schema Schema::Parse(const std::string& content, std::string* error) { |
+ return ParseWithBackwardsCompatibility(content, error, false); |
+} |
+ |
+// static |
+Schema Schema::ParseWithBackwardsCompatibility( |
+ const std::string& content, |
+ std::string* error, |
+ bool with_backwards_compatibility) { |
// Validate as a generic JSON schema. |
scoped_ptr<base::DictionaryValue> dict = |
JSONSchemaValidator::IsValidSchema(content, error); |
@@ -524,7 +564,7 @@ Schema Schema::Parse(const std::string& content, std::string* error) { |
} |
scoped_refptr<const InternalStorage> storage = |
- InternalStorage::ParseSchema(*dict, error); |
+ InternalStorage::ParseSchema(*dict, error, with_backwards_compatibility); |
if (!storage) |
return Schema(); |
return Schema(storage, storage->root_node()); |