Home | History | Annotate | Download | only in common
      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.
      4 
      5 #include "components/policy/core/common/schema_map.h"
      6 
      7 #include "base/values.h"
      8 #include "components/policy/core/common/policy_bundle.h"
      9 #include "components/policy/core/common/policy_map.h"
     10 
     11 namespace policy {
     12 
     13 SchemaMap::SchemaMap() {}
     14 
     15 SchemaMap::SchemaMap(DomainMap& map) {
     16   map_.swap(map);
     17 }
     18 
     19 SchemaMap::~SchemaMap() {}
     20 
     21 const DomainMap& SchemaMap::GetDomains() const {
     22   return map_;
     23 }
     24 
     25 const ComponentMap* SchemaMap::GetComponents(PolicyDomain domain) const {
     26   DomainMap::const_iterator it = map_.find(domain);
     27   return it == map_.end() ? NULL : &it->second;
     28 }
     29 
     30 const Schema* SchemaMap::GetSchema(const PolicyNamespace& ns) const {
     31   const ComponentMap* map = GetComponents(ns.domain);
     32   if (!map)
     33     return NULL;
     34   ComponentMap::const_iterator it = map->find(ns.component_id);
     35   return it == map->end() ? NULL : &it->second;
     36 }
     37 
     38 void SchemaMap::FilterBundle(PolicyBundle* bundle) const {
     39   for (PolicyBundle::iterator it = bundle->begin(); it != bundle->end(); ++it) {
     40     // Chrome policies are not filtered, so that typos appear in about:policy.
     41     // Everything else gets filtered, so that components only see valid policy.
     42     if (it->first.domain == POLICY_DOMAIN_CHROME)
     43       continue;
     44 
     45     const Schema* schema = GetSchema(it->first);
     46 
     47     if (!schema) {
     48       it->second->Clear();
     49       continue;
     50     }
     51 
     52     // TODO(joaodasilva): if a component is registered but doesn't have a schema
     53     // then its policies aren't filtered. This behavior is enabled to allow a
     54     // graceful update of the Legacy Browser Support extension; it'll be removed
     55     // in a future release. http://crbug.com/240704
     56     if (!schema->valid())
     57       continue;
     58 
     59     PolicyMap* map = it->second;
     60     for (PolicyMap::const_iterator it_map = map->begin();
     61          it_map != map->end();) {
     62       const std::string& policy_name = it_map->first;
     63       const base::Value* policy_value = it_map->second.value;
     64       Schema policy_schema = schema->GetProperty(policy_name);
     65       ++it_map;
     66       if (!policy_value || !policy_schema.Validate(*policy_value))
     67         map->Erase(policy_name);
     68     }
     69   }
     70 }
     71 
     72 bool SchemaMap::HasComponents() const {
     73   for (DomainMap::const_iterator domain = map_.begin();
     74        domain != map_.end(); ++domain) {
     75     if (domain->first == POLICY_DOMAIN_CHROME)
     76       continue;
     77     if (!domain->second.empty())
     78       return true;
     79   }
     80   return false;
     81 }
     82 
     83 void SchemaMap::GetChanges(const scoped_refptr<SchemaMap>& older,
     84                            PolicyNamespaceList* removed,
     85                            PolicyNamespaceList* added) const {
     86   GetNamespacesNotInOther(older, added);
     87   older->GetNamespacesNotInOther(this, removed);
     88 }
     89 
     90 void SchemaMap::GetNamespacesNotInOther(const SchemaMap* other,
     91                                         PolicyNamespaceList* list) const {
     92   list->clear();
     93   for (DomainMap::const_iterator domain = map_.begin();
     94        domain != map_.end(); ++domain) {
     95     const ComponentMap& components = domain->second;
     96     for (ComponentMap::const_iterator comp = components.begin();
     97          comp != components.end(); ++comp) {
     98       PolicyNamespace ns(domain->first, comp->first);
     99       if (!other->GetSchema(ns))
    100         list->push_back(ns);
    101     }
    102   }
    103 }
    104 
    105 }  // namespace policy
    106