summaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authorbinjin@chromium.org <binjin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-28 14:41:52 +0000
committerbinjin@chromium.org <binjin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-02-28 14:41:52 +0000
commit01fbc5922fbdd681bcaadcaaa99b791d2058ca5e (patch)
treebad842ac5a3d2074f7dfabaf9c6736b769296498 /components
parent17c930b0b0b9a4634fb726f51b0509cbee517c0f (diff)
downloadchromium_src-01fbc5922fbdd681bcaadcaaa99b791d2058ca5e.zip
chromium_src-01fbc5922fbdd681bcaadcaaa99b791d2058ca5e.tar.gz
chromium_src-01fbc5922fbdd681bcaadcaaa99b791d2058ca5e.tar.bz2
Add touch flag to output of Schema::Normalize()
BUG=258339 Review URL: https://codereview.chromium.org/171403004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@254107 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components')
-rw-r--r--components/policy/core/browser/configuration_policy_handler.cc2
-rw-r--r--components/policy/core/common/schema.cc20
-rw-r--r--components/policy/core/common/schema.h17
-rw-r--r--components/policy/core/common/schema_unittest.cc11
4 files changed, 37 insertions, 13 deletions
diff --git a/components/policy/core/browser/configuration_policy_handler.cc b/components/policy/core/browser/configuration_policy_handler.cc
index a273e83..e290ea6 100644
--- a/components/policy/core/browser/configuration_policy_handler.cc
+++ b/components/policy/core/browser/configuration_policy_handler.cc
@@ -345,7 +345,7 @@ bool SchemaValidatingPolicyHandler::CheckAndGetValue(
std::string error_path;
std::string error;
bool result =
- schema_.Normalize(output->get(), strategy_, &error_path, &error);
+ schema_.Normalize(output->get(), strategy_, &error_path, &error, NULL);
if (errors && !error.empty()) {
if (error_path.empty())
diff --git a/components/policy/core/common/schema.cc b/components/policy/core/common/schema.cc
index 180466b..d50bd8a 100644
--- a/components/policy/core/common/schema.cc
+++ b/components/policy/core/common/schema.cc
@@ -737,7 +737,8 @@ bool Schema::Validate(const base::Value& value,
bool Schema::Normalize(base::Value* value,
SchemaOnErrorStrategy strategy,
std::string* error_path,
- std::string* error) const {
+ std::string* error,
+ bool* changed) const {
if (!valid()) {
SchemaErrorFound(error_path, error, "The schema is invalid.");
return false;
@@ -776,7 +777,8 @@ bool Schema::Normalize(base::Value* value,
if (!subschema.Normalize(sub_value,
StrategyForNextLevel(strategy),
error_path,
- error)) {
+ error,
+ changed)) {
// Invalid property was detected.
AddDictKeyPrefixToPath(it.key(), error_path);
if (StrategyAllowInvalidOnTopLevel(strategy))
@@ -786,6 +788,8 @@ bool Schema::Normalize(base::Value* value,
}
}
}
+ if (changed && !drop_list.empty())
+ *changed = true;
for (std::vector<std::string>::const_iterator it = drop_list.begin();
it != drop_list.end();
++it) {
@@ -797,11 +801,11 @@ bool Schema::Normalize(base::Value* value,
for (size_t index = 0; index < list->GetSize(); index++) {
base::Value* sub_value = NULL;
list->Get(index, &sub_value);
- if (!sub_value ||
- !GetItems().Normalize(sub_value,
- StrategyForNextLevel(strategy),
- error_path,
- error)) {
+ if (!sub_value || !GetItems().Normalize(sub_value,
+ StrategyForNextLevel(strategy),
+ error_path,
+ error,
+ changed)) {
// Invalid list item was detected.
AddListIndexPrefixToPath(index, error_path);
if (StrategyAllowInvalidOnTopLevel(strategy))
@@ -810,6 +814,8 @@ bool Schema::Normalize(base::Value* value,
return false;
}
}
+ if (changed && !drop_list.empty())
+ *changed = true;
for (std::vector<size_t>::reverse_iterator it = drop_list.rbegin();
it != drop_list.rend(); ++it) {
list->Remove(*it, NULL);
diff --git a/components/policy/core/common/schema.h b/components/policy/core/common/schema.h
index 2d92200..82ee38f 100644
--- a/components/policy/core/common/schema.h
+++ b/components/policy/core/common/schema.h
@@ -26,6 +26,12 @@ struct POLICY_EXPORT PropertiesNode;
// the strategy to handle unknown properties or invalid values for dict type.
// Note that in Schema::Normalize() allowed errors will be dropped and thus
// ignored.
+// Unknown error indicates that some value in a dictionary (may or may not be
+// the one in root) have unknown property name according to schema.
+// Invalid error indicates a validation failure against the schema. As
+// validation is done recursively, a validation failure of dict properties or
+// list items might be ignored (or dropped in Normalize()) or trigger whole
+// dictionary/list validation failure.
enum SchemaOnErrorStrategy {
// No errors will be allowed.
SCHEMA_STRICT = 0,
@@ -90,11 +96,18 @@ class POLICY_EXPORT Schema {
std::string* error_path,
std::string* error) const;
- // Same as Validate() but drop values with errors instead of ignoring them.
+ // Similar to Validate() but drop values with errors instead of ignoring them.
+ // |changed| is a pointer to a boolean value, and indicate whether |value|
+ // is changed or not (probably dropped properties or items). Be sure to set
+ // the bool that |changed| pointed to to false before calling Normalize().
+ // |changed| can be NULL and in that case no boolean will be set.
+ // This function will also take the ownership of dropped base::Value and
+ // destroy them.
bool Normalize(base::Value* value,
SchemaOnErrorStrategy strategy,
std::string* error_path,
- std::string* error) const;
+ std::string* error,
+ bool* changed) const;
// Used to iterate over the known properties of TYPE_DICTIONARY schemas.
class POLICY_EXPORT Iterator {
diff --git a/components/policy/core/common/schema_unittest.cc b/components/policy/core/common/schema_unittest.cc
index c0c3193..8effd89 100644
--- a/components/policy/core/common/schema_unittest.cc
+++ b/components/policy/core/common/schema_unittest.cc
@@ -128,16 +128,21 @@ void TestSchemaValidation(Schema schema,
// Schema::Validate().
error = kNoErrorReturned;
scoped_ptr<base::Value> cloned_value(value.DeepCopy());
- returned = schema.Normalize(cloned_value.get(), strategy, NULL, &error);
+ bool touched = false;
+ returned =
+ schema.Normalize(cloned_value.get(), strategy, NULL, &error, &touched);
EXPECT_EQ(returned, expected_return_value) << error;
+ bool strictly_valid = schema.Validate(value, SCHEMA_STRICT, NULL, &error);
+ EXPECT_EQ(!strictly_valid && returned, touched);
+
// Test that Schema::Normalize() have actually dropped invalid and unknown
// properties.
if (expected_return_value) {
EXPECT_TRUE(
schema.Validate(*cloned_value.get(), SCHEMA_STRICT, NULL, &error));
- EXPECT_TRUE(
- schema.Normalize(cloned_value.get(), SCHEMA_STRICT, NULL, &error));
+ EXPECT_TRUE(schema.Normalize(
+ cloned_value.get(), SCHEMA_STRICT, NULL, &error, NULL));
}
}