Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: components/policy/core/common/schema.cc

Issue 143413002: Add additional restriction to policy schema internal (part2) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@expand-policy-schema
Patch Set: fix comments Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 bool Schema::ValidateIntegerRestriction(int index, int value) const {
735 const RestrictionNode* rnode = storage_->restriction(index);
736 if (rnode->ranged_restriction.min_value <=
737 rnode->ranged_restriction.max_value) {
738 return rnode->ranged_restriction.min_value <= value &&
739 rnode->ranged_restriction.max_value >= value;
740 } else {
741 for (int i = rnode->enumeration_restriction.offset_begin;
742 i < rnode->enumeration_restriction.offset_end; i++) {
743 if (*storage_->int_enums(i) == value)
744 return true;
745 }
746 return false;
747 }
748 }
749
750 bool Schema::ValidateStringRestriction(int index, const char *str) const {
751 const RestrictionNode* rnode = storage_->restriction(index);
752 for (int i = rnode->enumeration_restriction.offset_begin;
753 i < rnode->enumeration_restriction.offset_end; i++) {
754 if (strcmp(*storage_->string_enums(i), str) == 0)
755 return true;
756 }
757 return false;
758 }
759
726 } // namespace policy 760 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/common/schema.h ('k') | components/policy/core/common/schema_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698