Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include "components/policy/core/common/schema.h" | 5 #include "components/policy/core/common/schema.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <climits> | 8 #include <climits> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 } | 119 } |
| 120 | 120 |
| 121 const int* int_enums(int index) const { | 121 const int* int_enums(int index) const { |
| 122 return schema_data_.int_enums + index; | 122 return schema_data_.int_enums + index; |
| 123 } | 123 } |
| 124 | 124 |
| 125 const char** string_enums(int index) const { | 125 const char** string_enums(int index) const { |
| 126 return schema_data_.string_enums + index; | 126 return schema_data_.string_enums + index; |
| 127 } | 127 } |
| 128 | 128 |
| 129 bool ValidateIntegerRestriction(int index, int value) const; | |
| 130 | |
| 131 bool ValidateStringRestriction(int index, const char *str) const; | |
|
Joao da Silva
2014/01/20 20:22:18
These should be private methods of Schema::, not o
binjin
2014/01/21 16:12:07
Done.
| |
| 132 | |
| 129 private: | 133 private: |
| 130 friend class base::RefCountedThreadSafe<InternalStorage>; | 134 friend class base::RefCountedThreadSafe<InternalStorage>; |
| 131 | 135 |
| 132 InternalStorage(); | 136 InternalStorage(); |
| 133 ~InternalStorage(); | 137 ~InternalStorage(); |
| 134 | 138 |
| 135 // Determines the expected |sizes| of the storage for the representation | 139 // Determines the expected |sizes| of the storage for the representation |
| 136 // of |schema|. | 140 // of |schema|. |
| 137 static void DetermineStorageSizes(const base::DictionaryValue& schema, | 141 static void DetermineStorageSizes(const base::DictionaryValue& schema, |
| 138 StorageSizes* sizes); | 142 StorageSizes* sizes); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 268 SchemaData* data = &storage->schema_data_; | 272 SchemaData* data = &storage->schema_data_; |
| 269 data->schema_nodes = vector_as_array(&storage->schema_nodes_); | 273 data->schema_nodes = vector_as_array(&storage->schema_nodes_); |
| 270 data->property_nodes = vector_as_array(&storage->property_nodes_); | 274 data->property_nodes = vector_as_array(&storage->property_nodes_); |
| 271 data->properties_nodes = vector_as_array(&storage->properties_nodes_); | 275 data->properties_nodes = vector_as_array(&storage->properties_nodes_); |
| 272 data->restriction_nodes = vector_as_array(&storage->restriction_nodes_); | 276 data->restriction_nodes = vector_as_array(&storage->restriction_nodes_); |
| 273 data->int_enums = vector_as_array(&storage->int_enums_); | 277 data->int_enums = vector_as_array(&storage->int_enums_); |
| 274 data->string_enums = vector_as_array(&storage->string_enums_); | 278 data->string_enums = vector_as_array(&storage->string_enums_); |
| 275 return storage; | 279 return storage; |
| 276 } | 280 } |
| 277 | 281 |
| 282 bool Schema::InternalStorage::ValidateIntegerRestriction( | |
| 283 int index, int value) const { | |
| 284 const RestrictionNode* rnode = restriction(index); | |
| 285 if (rnode->ranged_restriction.min_value <= | |
| 286 rnode->ranged_restriction.max_value) { | |
| 287 return rnode->ranged_restriction.min_value <= value && | |
| 288 rnode->ranged_restriction.max_value >= value; | |
| 289 } else { | |
| 290 for (int i = rnode->enumeration_restriction.offset_begin; | |
| 291 i < rnode->enumeration_restriction.offset_end; i++) { | |
| 292 if (*int_enums(i) == value) | |
| 293 return true; | |
| 294 } | |
| 295 return false; | |
| 296 } | |
| 297 } | |
| 298 | |
| 299 bool Schema::InternalStorage::ValidateStringRestriction( | |
| 300 int index, const char *str) const { | |
| 301 const RestrictionNode* rnode = restriction(index); | |
| 302 for (int i = rnode->enumeration_restriction.offset_begin; | |
| 303 i < rnode->enumeration_restriction.offset_end; i++) { | |
| 304 if (strcmp(*string_enums(i), str) == 0) | |
| 305 return true; | |
| 306 } | |
| 307 return false; | |
| 308 } | |
| 309 | |
| 278 // static | 310 // static |
| 279 void Schema::InternalStorage::DetermineStorageSizes( | 311 void Schema::InternalStorage::DetermineStorageSizes( |
| 280 const base::DictionaryValue& schema, | 312 const base::DictionaryValue& schema, |
| 281 StorageSizes* sizes) { | 313 StorageSizes* sizes) { |
| 282 std::string ref_string; | 314 std::string ref_string; |
| 283 if (schema.GetString(schema::kRef, &ref_string)) { | 315 if (schema.GetString(schema::kRef, &ref_string)) { |
| 284 // Schemas with a "$ref" attribute don't take additional storage. | 316 // Schemas with a "$ref" attribute don't take additional storage. |
| 285 return; | 317 return; |
| 286 } | 318 } |
| 287 | 319 |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 613 if (!valid()) { | 645 if (!valid()) { |
| 614 // Schema not found, invalid entry. | 646 // Schema not found, invalid entry. |
| 615 return false; | 647 return false; |
| 616 } | 648 } |
| 617 | 649 |
| 618 if (!value.IsType(type())) | 650 if (!value.IsType(type())) |
| 619 return false; | 651 return false; |
| 620 | 652 |
| 621 const base::DictionaryValue* dict = NULL; | 653 const base::DictionaryValue* dict = NULL; |
| 622 const base::ListValue* list = NULL; | 654 const base::ListValue* list = NULL; |
| 655 int int_value; | |
| 656 std::string str_value; | |
| 623 if (value.GetAsDictionary(&dict)) { | 657 if (value.GetAsDictionary(&dict)) { |
| 624 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); | 658 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); |
| 625 it.Advance()) { | 659 it.Advance()) { |
| 626 if (!GetProperty(it.key()).Validate(it.value())) | 660 if (!GetProperty(it.key()).Validate(it.value())) |
| 627 return false; | 661 return false; |
| 628 } | 662 } |
| 629 } else if (value.GetAsList(&list)) { | 663 } else if (value.GetAsList(&list)) { |
| 630 for (base::ListValue::const_iterator it = list->begin(); | 664 for (base::ListValue::const_iterator it = list->begin(); |
| 631 it != list->end(); ++it) { | 665 it != list->end(); ++it) { |
| 632 if (!*it || !GetItems().Validate(**it)) | 666 if (!*it || !GetItems().Validate(**it)) |
| 633 return false; | 667 return false; |
| 634 } | 668 } |
| 669 } else if (value.GetAsInteger(&int_value)) { | |
| 670 return node_->extra == kInvalid || | |
| 671 storage_->ValidateIntegerRestriction(node_->extra, int_value); | |
| 672 } else if (value.GetAsString(&str_value)) { | |
| 673 return node_->extra == kInvalid || | |
| 674 storage_->ValidateStringRestriction(node_->extra, str_value.c_str()); | |
| 635 } | 675 } |
| 636 | 676 |
| 637 return true; | 677 return true; |
| 638 } | 678 } |
| 639 | 679 |
| 640 // static | 680 // static |
| 641 Schema Schema::Parse(const std::string& content, std::string* error) { | 681 Schema Schema::Parse(const std::string& content, std::string* error) { |
| 642 // Validate as a generic JSON schema, and ignore unknown attributes; they | 682 // Validate as a generic JSON schema, and ignore unknown attributes; they |
| 643 // may become used in a future version of the schema format. | 683 // may become used in a future version of the schema format. |
| 644 scoped_ptr<base::DictionaryValue> dict = JSONSchemaValidator::IsValidSchema( | 684 scoped_ptr<base::DictionaryValue> dict = JSONSchemaValidator::IsValidSchema( |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 717 | 757 |
| 718 Schema Schema::GetItems() const { | 758 Schema Schema::GetItems() const { |
| 719 CHECK(valid()); | 759 CHECK(valid()); |
| 720 CHECK_EQ(base::Value::TYPE_LIST, type()); | 760 CHECK_EQ(base::Value::TYPE_LIST, type()); |
| 721 if (node_->extra == kInvalid) | 761 if (node_->extra == kInvalid) |
| 722 return Schema(); | 762 return Schema(); |
| 723 return Schema(storage_, storage_->schema(node_->extra)); | 763 return Schema(storage_, storage_->schema(node_->extra)); |
| 724 } | 764 } |
| 725 | 765 |
| 726 } // namespace policy | 766 } // namespace policy |
| OLD | NEW |