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
3 // found in the LICENSE file.
5 #include "components/policy/core/common/schema_map.h"
7 #include "base/logging.h"
8 #include "base/values.h"
9 #include "components/policy/core/common/policy_bundle.h"
10 #include "components/policy/core/common/policy_map.h"
14 SchemaMap::SchemaMap() {}
16 SchemaMap::SchemaMap(DomainMap
& map
) {
20 SchemaMap::~SchemaMap() {}
22 const DomainMap
& SchemaMap::GetDomains() const {
26 const ComponentMap
* SchemaMap::GetComponents(PolicyDomain domain
) const {
27 DomainMap::const_iterator it
= map_
.find(domain
);
28 return it
== map_
.end() ? NULL
: &it
->second
;
31 const Schema
* SchemaMap::GetSchema(const PolicyNamespace
& ns
) const {
32 const ComponentMap
* map
= GetComponents(ns
.domain
);
35 ComponentMap::const_iterator it
= map
->find(ns
.component_id
);
36 return it
== map
->end() ? NULL
: &it
->second
;
39 void SchemaMap::FilterBundle(PolicyBundle
* bundle
) const {
40 for (PolicyBundle::iterator it
= bundle
->begin(); it
!= bundle
->end(); ++it
) {
41 // Chrome policies are not filtered, so that typos appear in about:policy.
42 // Everything else gets filtered, so that components only see valid policy.
43 if (it
->first
.domain
== POLICY_DOMAIN_CHROME
)
46 const Schema
* schema
= GetSchema(it
->first
);
53 // TODO(joaodasilva): if a component is registered but doesn't have a schema
54 // then its policies aren't filtered. This behavior is enabled to allow a
55 // graceful update of the Legacy Browser Support extension; it'll be removed
56 // in a future release. http://crbug.com/240704
60 PolicyMap
* map
= it
->second
;
61 for (PolicyMap::const_iterator it_map
= map
->begin();
62 it_map
!= map
->end();) {
63 const std::string
& policy_name
= it_map
->first
;
64 const base::Value
* policy_value
= it_map
->second
.value
;
65 Schema policy_schema
= schema
->GetProperty(policy_name
);
67 std::string error_path
;
70 !policy_schema
.Validate(*policy_value
,
74 LOG(ERROR
) << "Dropping policy " << policy_name
<< " for "
75 << it
->first
.component_id
76 << " because it's not valid: " << error
77 << " at " << error_path
;
78 map
->Erase(policy_name
);
84 bool SchemaMap::HasComponents() const {
85 for (DomainMap::const_iterator domain
= map_
.begin();
86 domain
!= map_
.end(); ++domain
) {
87 if (domain
->first
== POLICY_DOMAIN_CHROME
)
89 if (!domain
->second
.empty())
95 void SchemaMap::GetChanges(const scoped_refptr
<SchemaMap
>& older
,
96 PolicyNamespaceList
* removed
,
97 PolicyNamespaceList
* added
) const {
98 GetNamespacesNotInOther(older
, added
);
99 older
->GetNamespacesNotInOther(this, removed
);
102 void SchemaMap::GetNamespacesNotInOther(const SchemaMap
* other
,
103 PolicyNamespaceList
* list
) const {
105 for (DomainMap::const_iterator domain
= map_
.begin();
106 domain
!= map_
.end(); ++domain
) {
107 const ComponentMap
& components
= domain
->second
;
108 for (ComponentMap::const_iterator comp
= components
.begin();
109 comp
!= components
.end(); ++comp
) {
110 PolicyNamespace
ns(domain
->first
, comp
->first
);
111 if (!other
->GetSchema(ns
))
117 } // namespace policy