OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/cloud_policy_provider_impl.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 11 |
| 12 namespace policy { |
| 13 |
| 14 CloudPolicyProviderImpl::CloudPolicyProviderImpl( |
| 15 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list, |
| 16 CloudPolicyCacheBase::PolicyLevel level) |
| 17 : CloudPolicyProvider(policy_list), |
| 18 level_(level) {} |
| 19 |
| 20 CloudPolicyProviderImpl::~CloudPolicyProviderImpl() { |
| 21 for (ListType::iterator i = caches_.begin(); i != caches_.end(); ++i) |
| 22 (*i)->RemoveObserver(this); |
| 23 |
| 24 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, |
| 25 observer_list_, OnProviderGoingAway()); |
| 26 } |
| 27 |
| 28 bool CloudPolicyProviderImpl::Provide( |
| 29 ConfigurationPolicyStoreInterface* store) { |
| 30 ApplyPolicyMap(&combined_, store); |
| 31 return true; |
| 32 } |
| 33 |
| 34 bool CloudPolicyProviderImpl::IsInitializationComplete() const { |
| 35 for (ListType::const_iterator i = caches_.begin(); i != caches_.end(); ++i) { |
| 36 if (!(*i)->IsReady()) { |
| 37 return false; |
| 38 } |
| 39 } |
| 40 return true; |
| 41 } |
| 42 |
| 43 void CloudPolicyProviderImpl::AddObserver( |
| 44 ConfigurationPolicyProvider::Observer* observer) { |
| 45 observer_list_.AddObserver(observer); |
| 46 } |
| 47 |
| 48 void CloudPolicyProviderImpl::RemoveObserver( |
| 49 ConfigurationPolicyProvider::Observer* observer) { |
| 50 observer_list_.RemoveObserver(observer); |
| 51 } |
| 52 |
| 53 void CloudPolicyProviderImpl::OnCacheUpdate(CloudPolicyCacheBase* cache) { |
| 54 RecombineCachesAndMaybeTriggerUpdate(); |
| 55 } |
| 56 |
| 57 void CloudPolicyProviderImpl::OnCacheGoingAway(CloudPolicyCacheBase* cache) { |
| 58 cache->RemoveObserver(this); |
| 59 for (ListType::iterator i = caches_.begin(); i != caches_.end(); ++i) { |
| 60 if (*i == cache) { |
| 61 caches_.erase(i); |
| 62 break; |
| 63 } |
| 64 } |
| 65 |
| 66 RecombineCachesAndMaybeTriggerUpdate(); |
| 67 } |
| 68 |
| 69 void CloudPolicyProviderImpl::AppendCache(CloudPolicyCacheBase* cache) { |
| 70 cache->AddObserver(this); |
| 71 caches_.push_back(cache); |
| 72 RecombineCachesAndMaybeTriggerUpdate(); |
| 73 } |
| 74 |
| 75 void CloudPolicyProviderImpl::PrependCache(CloudPolicyCacheBase* cache) { |
| 76 cache->AddObserver(this); |
| 77 caches_.insert(caches_.begin(), cache); |
| 78 RecombineCachesAndMaybeTriggerUpdate(); |
| 79 } |
| 80 |
| 81 // static |
| 82 void CloudPolicyProviderImpl::CombineTwoPolicyMaps(const PolicyMap& base, |
| 83 const PolicyMap& overlay, |
| 84 PolicyMap* out_map) { |
| 85 bool added_proxy_policy = false; |
| 86 out_map->Clear(); |
| 87 |
| 88 for (PolicyMap::const_iterator i = base.begin(); i != base.end(); ++i) { |
| 89 out_map->Set(i->first, i->second->DeepCopy()); |
| 90 added_proxy_policy = added_proxy_policy || |
| 91 ConfigurationPolicyPrefStore::IsProxyPolicy(i->first); |
| 92 } |
| 93 |
| 94 // Add every entry of |overlay| which has not been added by |base|. Only add |
| 95 // proxy policies if none of them was added by |base|. |
| 96 for (PolicyMap::const_iterator i = overlay.begin(); i != overlay.end(); ++i) { |
| 97 if (ConfigurationPolicyPrefStore::IsProxyPolicy(i->first)) { |
| 98 if (!added_proxy_policy) { |
| 99 out_map->Set(i->first, i->second->DeepCopy()); |
| 100 } |
| 101 } else if (!out_map->Get(i->first)) { |
| 102 out_map->Set(i->first, i->second->DeepCopy()); |
| 103 } |
| 104 } |
| 105 } |
| 106 |
| 107 void CloudPolicyProviderImpl::RecombineCachesAndMaybeTriggerUpdate() { |
| 108 PolicyMap newly_combined; |
| 109 for (ListType::iterator i = caches_.begin(); i != caches_.end(); ++i) { |
| 110 if (!(*i)->IsReady()) |
| 111 continue; |
| 112 PolicyMap tmp_map; |
| 113 CombineTwoPolicyMaps(newly_combined, *(*i)->policy(level_), &tmp_map); |
| 114 newly_combined.Swap(&tmp_map); |
| 115 } |
| 116 if (newly_combined.Equals(combined_)) |
| 117 return; |
| 118 |
| 119 // Trigger a notification if there was a change. |
| 120 combined_.Swap(&newly_combined); |
| 121 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, |
| 122 observer_list_, OnUpdatePolicy()); |
| 123 } |
| 124 |
| 125 } // namespace policy |
OLD | NEW |