Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/policy/policy_domain_descriptor.h" | |
| 6 | |
| 7 #include "base/stl_util.h" | |
| 8 #include "chrome/browser/policy/policy_bundle.h" | |
| 9 #include "chrome/browser/policy/policy_map.h" | |
| 10 #include "chrome/browser/policy/policy_schema.h" | |
| 11 | |
| 12 namespace policy { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 bool Matches(const PolicySchema* schema, const base::Value& value) { | |
|
bartfab (slow)
2013/05/21 12:10:56
#include "base/values.h"
Joao da Silva
2013/05/21 17:50:14
Done.
| |
| 17 if (!schema) { | |
| 18 // Schema not found, invalid entry. | |
| 19 return false; | |
| 20 } | |
| 21 | |
| 22 if (!value.IsType(schema->type())) | |
| 23 return false; | |
| 24 | |
| 25 const base::DictionaryValue* dict = NULL; | |
| 26 const base::ListValue* list = NULL; | |
| 27 if (value.GetAsDictionary(&dict)) { | |
| 28 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); | |
| 29 it.Advance()) { | |
| 30 if (!Matches(schema->GetSchemaForProperty(it.key()), it.value())) | |
| 31 return false; | |
| 32 } | |
| 33 } else if (value.GetAsList(&list)) { | |
| 34 for (base::ListValue::const_iterator it = list->begin(); | |
| 35 it != list->end(); ++it) { | |
| 36 if (!*it || !Matches(schema->GetSchemaForItems(), **it)) | |
| 37 return false; | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 return true; | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 PolicyDomainDescriptor::PolicyDomainDescriptor(PolicyDomain domain) | |
| 47 : domain_(domain) {} | |
| 48 | |
| 49 void PolicyDomainDescriptor::SetComponent(const std::string& component_id, | |
| 50 scoped_ptr<PolicySchema> schema) { | |
| 51 const PolicySchema*& previous = schema_map_[component_id]; | |
| 52 delete previous; | |
| 53 previous = schema.release(); | |
|
bartfab (slow)
2013/05/21 12:10:56
Could you rename |previous| to something like |ent
Joao da Silva
2013/05/21 17:50:14
Done.
| |
| 54 } | |
| 55 | |
| 56 void PolicyDomainDescriptor::FilterBundle(PolicyBundle* bundle) const { | |
| 57 // Chrome policies are not filtered, so that typos appear in about:policy. | |
| 58 DCHECK_NE(POLICY_DOMAIN_CHROME, domain_); | |
|
bartfab (slow)
2013/05/21 12:10:56
#include "base/logging.h"
Joao da Silva
2013/05/21 17:50:14
Done.
| |
| 59 | |
| 60 for (PolicyBundle::const_iterator it_bundle = bundle->begin(); | |
| 61 it_bundle != bundle->end(); ++it_bundle) { | |
| 62 const PolicyNamespace& ns = it_bundle->first; | |
| 63 if (ns.domain != domain_) | |
| 64 continue; | |
| 65 | |
| 66 SchemaMap::const_iterator it_schema = schema_map_.find(ns.component_id); | |
| 67 if (it_schema == schema_map_.end()) { | |
| 68 // Component ID not found. | |
| 69 bundle->Get(ns).Clear(); | |
|
bartfab (slow)
2013/05/21 12:10:56
Why not make |it_bundle| a non-const iterator, all
Joao da Silva
2013/05/21 17:50:14
PolicyBundle only has const_iterators, but there's
| |
| 70 continue; | |
| 71 } | |
| 72 | |
| 73 // TODO(joaodasilva): if a component is registered but doesn't have a schema | |
| 74 // then its policies aren't filtered. This behavior is enabled for M29 to | |
| 75 // allow a graceful update of the Legacy Browser Support extension; it'll | |
| 76 // be removed for M30. http://crbug.com/240704 | |
| 77 if (!it_schema->second) | |
| 78 continue; | |
| 79 | |
| 80 const PolicySchema* component_schema = it_schema->second; | |
| 81 PolicyMap& map = bundle->Get(ns); | |
|
bartfab (slow)
2013/05/21 12:10:56
As above, if |it_bundle| were non-const, you could
Joao da Silva
2013/05/21 17:50:14
Done.
| |
| 82 PolicyMap::const_iterator it_map = map.begin(); | |
|
bartfab (slow)
2013/05/21 12:10:56
for and while are equivalent of course. In terms o
Joao da Silva
2013/05/21 17:50:14
Done.
| |
| 83 while (it_map != map.end()) { | |
| 84 const std::string& policy_name = it_map->first; | |
| 85 const base::Value* policy_value = it_map->second.value; | |
| 86 const PolicySchema* policy_schema = | |
| 87 component_schema->GetSchemaForProperty(policy_name); | |
| 88 ++it_map; | |
|
bartfab (slow)
2013/05/21 12:10:56
Alternatively (and using the STL more efficiently)
Joao da Silva
2013/05/21 17:50:14
PolicyMap is not an std::map: it doesn't have iter
| |
| 89 if (!policy_value || !Matches(policy_schema, *policy_value)) | |
| 90 map.Erase(policy_name); | |
| 91 } | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 PolicyDomainDescriptor::~PolicyDomainDescriptor() { | |
| 96 STLDeleteValues(&schema_map_); | |
| 97 } | |
| 98 | |
| 99 } // namespace policy | |
| OLD | NEW |