| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "chrome/browser/policy/policy_domain_descriptor.h" | 5 #include "chrome/browser/policy/policy_domain_descriptor.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/browser/policy/policy_bundle.h" | 10 #include "chrome/browser/policy/policy_bundle.h" |
| 11 #include "chrome/browser/policy/policy_map.h" | 11 #include "chrome/browser/policy/policy_map.h" |
| 12 #include "components/policy/core/common/policy_schema.h" | |
| 13 | 12 |
| 14 namespace policy { | 13 namespace policy { |
| 15 | 14 |
| 16 namespace { | 15 namespace { |
| 17 | 16 |
| 18 bool Matches(const PolicySchema* schema, const base::Value& value) { | 17 bool Matches(Schema schema, const base::Value& value) { |
| 19 if (!schema) { | 18 if (!schema.valid()) { |
| 20 // Schema not found, invalid entry. | 19 // Schema not found, invalid entry. |
| 21 return false; | 20 return false; |
| 22 } | 21 } |
| 23 | 22 |
| 24 if (!value.IsType(schema->type())) | 23 if (!value.IsType(schema.type())) |
| 25 return false; | 24 return false; |
| 26 | 25 |
| 27 const base::DictionaryValue* dict = NULL; | 26 const base::DictionaryValue* dict = NULL; |
| 28 const base::ListValue* list = NULL; | 27 const base::ListValue* list = NULL; |
| 29 if (value.GetAsDictionary(&dict)) { | 28 if (value.GetAsDictionary(&dict)) { |
| 30 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); | 29 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); |
| 31 it.Advance()) { | 30 it.Advance()) { |
| 32 if (!Matches(schema->GetSchemaForProperty(it.key()), it.value())) | 31 if (!Matches(schema.GetProperty(it.key()), it.value())) |
| 33 return false; | 32 return false; |
| 34 } | 33 } |
| 35 } else if (value.GetAsList(&list)) { | 34 } else if (value.GetAsList(&list)) { |
| 36 for (base::ListValue::const_iterator it = list->begin(); | 35 for (base::ListValue::const_iterator it = list->begin(); |
| 37 it != list->end(); ++it) { | 36 it != list->end(); ++it) { |
| 38 if (!*it || !Matches(schema->GetSchemaForItems(), **it)) | 37 if (!*it || !Matches(schema.GetItems(), **it)) |
| 39 return false; | 38 return false; |
| 40 } | 39 } |
| 41 } | 40 } |
| 42 | 41 |
| 43 return true; | 42 return true; |
| 44 } | 43 } |
| 45 | 44 |
| 46 } // namespace | 45 } // namespace |
| 47 | 46 |
| 48 PolicyDomainDescriptor::PolicyDomainDescriptor(PolicyDomain domain) | 47 PolicyDomainDescriptor::PolicyDomainDescriptor(PolicyDomain domain) |
| 49 : domain_(domain) {} | 48 : domain_(domain) {} |
| 50 | 49 |
| 51 void PolicyDomainDescriptor::RegisterComponent( | 50 void PolicyDomainDescriptor::RegisterComponent( |
| 52 const std::string& component_id, | 51 const std::string& component_id, |
| 53 scoped_ptr<PolicySchema> schema) { | 52 scoped_ptr<SchemaOwner> schema) { |
| 54 const PolicySchema*& entry = schema_map_[component_id]; | 53 SchemaOwner*& entry = schema_owner_map_[component_id]; |
| 55 delete entry; | 54 delete entry; |
| 56 entry = schema.release(); | 55 entry = schema.release(); |
| 56 schema_map_[component_id] = entry ? entry->schema() : Schema(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void PolicyDomainDescriptor::FilterBundle(PolicyBundle* bundle) const { | 59 void PolicyDomainDescriptor::FilterBundle(PolicyBundle* bundle) const { |
| 60 // Chrome policies are not filtered, so that typos appear in about:policy. | 60 // Chrome policies are not filtered, so that typos appear in about:policy. |
| 61 DCHECK_NE(POLICY_DOMAIN_CHROME, domain_); | 61 DCHECK_NE(POLICY_DOMAIN_CHROME, domain_); |
| 62 | 62 |
| 63 for (PolicyBundle::iterator it_bundle = bundle->begin(); | 63 for (PolicyBundle::iterator it_bundle = bundle->begin(); |
| 64 it_bundle != bundle->end(); ++it_bundle) { | 64 it_bundle != bundle->end(); ++it_bundle) { |
| 65 const PolicyNamespace& ns = it_bundle->first; | 65 const PolicyNamespace& ns = it_bundle->first; |
| 66 if (ns.domain != domain_) | 66 if (ns.domain != domain_) |
| 67 continue; | 67 continue; |
| 68 | 68 |
| 69 SchemaMap::const_iterator it_schema = schema_map_.find(ns.component_id); | 69 SchemaMap::const_iterator it_schema = schema_map_.find(ns.component_id); |
| 70 if (it_schema == schema_map_.end()) { | 70 if (it_schema == schema_map_.end()) { |
| 71 // Component ID not found. | 71 // Component ID not found. |
| 72 it_bundle->second->Clear(); | 72 it_bundle->second->Clear(); |
| 73 continue; | 73 continue; |
| 74 } | 74 } |
| 75 | 75 |
| 76 // TODO(joaodasilva): if a component is registered but doesn't have a schema | 76 // TODO(joaodasilva): if a component is registered but doesn't have a schema |
| 77 // then its policies aren't filtered. This behavior is enabled for M29 to | 77 // then its policies aren't filtered. This behavior is enabled for M29 to |
| 78 // allow a graceful update of the Legacy Browser Support extension; it'll | 78 // allow a graceful update of the Legacy Browser Support extension; it'll |
| 79 // be removed for M30. http://crbug.com/240704 | 79 // be removed for M32. http://crbug.com/240704 |
| 80 if (!it_schema->second) | 80 Schema schema = it_schema->second; |
| 81 if (!schema.valid()) |
| 81 continue; | 82 continue; |
| 82 | 83 |
| 83 const PolicySchema* component_schema = it_schema->second; | |
| 84 PolicyMap* map = it_bundle->second; | 84 PolicyMap* map = it_bundle->second; |
| 85 for (PolicyMap::const_iterator it_map = map->begin(); | 85 for (PolicyMap::const_iterator it_map = map->begin(); |
| 86 it_map != map->end();) { | 86 it_map != map->end();) { |
| 87 const std::string& policy_name = it_map->first; | 87 const std::string& policy_name = it_map->first; |
| 88 const base::Value* policy_value = it_map->second.value; | 88 const base::Value* policy_value = it_map->second.value; |
| 89 const PolicySchema* policy_schema = | 89 Schema policy_schema = schema.GetProperty(policy_name); |
| 90 component_schema->GetSchemaForProperty(policy_name); | |
| 91 ++it_map; | 90 ++it_map; |
| 92 if (!policy_value || !Matches(policy_schema, *policy_value)) | 91 if (!policy_value || !Matches(policy_schema, *policy_value)) |
| 93 map->Erase(policy_name); | 92 map->Erase(policy_name); |
| 94 } | 93 } |
| 95 } | 94 } |
| 96 } | 95 } |
| 97 | 96 |
| 98 PolicyDomainDescriptor::~PolicyDomainDescriptor() { | 97 PolicyDomainDescriptor::~PolicyDomainDescriptor() { |
| 99 STLDeleteValues(&schema_map_); | 98 STLDeleteValues(&schema_owner_map_); |
| 100 } | 99 } |
| 101 | 100 |
| 102 } // namespace policy | 101 } // namespace policy |
| OLD | NEW |