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 602 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
613 if (!valid()) { | 613 if (!valid()) { |
614 // Schema not found, invalid entry. | 614 // Schema not found, invalid entry. |
615 return false; | 615 return false; |
616 } | 616 } |
617 | 617 |
618 if (!value.IsType(type())) | 618 if (!value.IsType(type())) |
619 return false; | 619 return false; |
620 | 620 |
621 const base::DictionaryValue* dict = NULL; | 621 const base::DictionaryValue* dict = NULL; |
622 const base::ListValue* list = NULL; | 622 const base::ListValue* list = NULL; |
623 int int_value; | |
624 std::string str_value; | |
623 if (value.GetAsDictionary(&dict)) { | 625 if (value.GetAsDictionary(&dict)) { |
624 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); | 626 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); |
625 it.Advance()) { | 627 it.Advance()) { |
626 if (!GetProperty(it.key()).Validate(it.value())) | 628 if (!GetProperty(it.key()).Validate(it.value())) |
627 return false; | 629 return false; |
628 } | 630 } |
629 } else if (value.GetAsList(&list)) { | 631 } else if (value.GetAsList(&list)) { |
630 for (base::ListValue::const_iterator it = list->begin(); | 632 for (base::ListValue::const_iterator it = list->begin(); |
631 it != list->end(); ++it) { | 633 it != list->end(); ++it) { |
632 if (!*it || !GetItems().Validate(**it)) | 634 if (!*it || !GetItems().Validate(**it)) |
633 return false; | 635 return false; |
634 } | 636 } |
637 } else if (value.GetAsInteger(&int_value)) { | |
638 return node_->extra == kInvalid || | |
639 ValidateIntegerRestriction(node_->extra, int_value); | |
640 } else if (value.GetAsString(&str_value)) { | |
641 return node_->extra == kInvalid || | |
642 ValidateStringRestriction(node_->extra, str_value.c_str()); | |
635 } | 643 } |
636 | 644 |
637 return true; | 645 return true; |
638 } | 646 } |
639 | 647 |
640 // static | 648 // static |
641 Schema Schema::Parse(const std::string& content, std::string* error) { | 649 Schema Schema::Parse(const std::string& content, std::string* error) { |
642 // Validate as a generic JSON schema, and ignore unknown attributes; they | 650 // Validate as a generic JSON schema, and ignore unknown attributes; they |
643 // may become used in a future version of the schema format. | 651 // may become used in a future version of the schema format. |
644 scoped_ptr<base::DictionaryValue> dict = JSONSchemaValidator::IsValidSchema( | 652 scoped_ptr<base::DictionaryValue> dict = JSONSchemaValidator::IsValidSchema( |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
716 } | 724 } |
717 | 725 |
718 Schema Schema::GetItems() const { | 726 Schema Schema::GetItems() const { |
719 CHECK(valid()); | 727 CHECK(valid()); |
720 CHECK_EQ(base::Value::TYPE_LIST, type()); | 728 CHECK_EQ(base::Value::TYPE_LIST, type()); |
721 if (node_->extra == kInvalid) | 729 if (node_->extra == kInvalid) |
722 return Schema(); | 730 return Schema(); |
723 return Schema(storage_, storage_->schema(node_->extra)); | 731 return Schema(storage_, storage_->schema(node_->extra)); |
724 } | 732 } |
725 | 733 |
734 // static | |
Joao da Silva
2014/01/21 16:30:05
This method is not really static: it uses instance
| |
735 bool Schema::ValidateIntegerRestriction(int index, int value) const { | |
736 const RestrictionNode* rnode = storage_->restriction(index); | |
737 if (rnode->ranged_restriction.min_value <= | |
738 rnode->ranged_restriction.max_value) { | |
739 return rnode->ranged_restriction.min_value <= value && | |
740 rnode->ranged_restriction.max_value >= value; | |
741 } else { | |
742 for (int i = rnode->enumeration_restriction.offset_begin; | |
743 i < rnode->enumeration_restriction.offset_end; i++) { | |
744 if (*storage_->int_enums(i) == value) | |
745 return true; | |
746 } | |
747 return false; | |
748 } | |
749 } | |
750 | |
751 // static | |
Joao da Silva
2014/01/21 16:30:05
Same here
| |
752 bool Schema::ValidateStringRestriction(int index, const char *str) const { | |
753 const RestrictionNode* rnode = storage_->restriction(index); | |
754 for (int i = rnode->enumeration_restriction.offset_begin; | |
755 i < rnode->enumeration_restriction.offset_end; i++) { | |
756 if (strcmp(*storage_->string_enums(i), str) == 0) | |
757 return true; | |
758 } | |
759 return false; | |
760 } | |
761 | |
726 } // namespace policy | 762 } // namespace policy |
OLD | NEW |