Chromium Code Reviews| 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 #include "chrome/browser/policy/policy_notifier.h" | |
| 12 | |
| 13 namespace policy { | |
| 14 | |
| 15 CloudPolicyProviderImpl::CloudPolicyProviderImpl( | |
| 16 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list, | |
| 17 CloudPolicyCacheBase::PolicyLevel level) | |
| 18 : CloudPolicyProvider(policy_list), | |
| 19 level_(level) {} | |
| 20 | |
| 21 CloudPolicyProviderImpl::~CloudPolicyProviderImpl() { | |
| 22 for (ListType::iterator i = caches_.begin(); i != caches_.end(); ++i) | |
| 23 (*i)->RemoveObserver(this); | |
| 24 | |
| 25 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, | |
| 26 observer_list_, OnProviderGoingAway()); | |
| 27 } | |
| 28 | |
| 29 bool CloudPolicyProviderImpl::Provide( | |
| 30 ConfigurationPolicyStoreInterface* store) { | |
| 31 ApplyPolicyMap(&combined_, store); | |
| 32 return true; | |
| 33 } | |
| 34 | |
| 35 bool CloudPolicyProviderImpl::IsInitializationComplete() const { | |
| 36 // If we would return false when we don't have policy, that would block the | |
| 37 // profile from being initialized. On the other hand, in case of user | |
| 38 // policies, the first policy fetch is delayed until after the profile is | |
| 39 // initialized. | |
| 40 // TODO(gfeher,mnissler): Break this loop of dependencies and implement | |
| 41 // a proper initialization completion check here. | |
|
Mattias Nissler (ping if slow)
2011/06/24 09:16:46
As discussed offline: Let's do the local initializ
gfeher
2011/06/24 15:32:44
Done.
| |
| 42 return true; | |
| 43 } | |
| 44 | |
| 45 void CloudPolicyProviderImpl::AddObserver( | |
| 46 ConfigurationPolicyProvider::Observer* observer) { | |
| 47 observer_list_.AddObserver(observer); | |
| 48 } | |
| 49 | |
| 50 void CloudPolicyProviderImpl::RemoveObserver( | |
| 51 ConfigurationPolicyProvider::Observer* observer) { | |
| 52 observer_list_.RemoveObserver(observer); | |
| 53 } | |
| 54 | |
| 55 void CloudPolicyProviderImpl::OnCacheUpdate(CloudPolicyCacheBase* cache) { | |
| 56 RecombineCachesAndMaybeTriggerUpdate(); | |
| 57 } | |
| 58 | |
| 59 void CloudPolicyProviderImpl::OnCacheGoingAway(CloudPolicyCacheBase* cache) { | |
| 60 cache->RemoveObserver(this); | |
| 61 for (ListType::iterator i = caches_.begin(); i != caches_.end(); ++i) { | |
| 62 if (*i == cache) { | |
| 63 caches_.erase(i); | |
| 64 break; | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 RecombineCachesAndMaybeTriggerUpdate(); | |
| 69 } | |
| 70 | |
| 71 void CloudPolicyProviderImpl::AppendCache(CloudPolicyCacheBase* cache) { | |
| 72 cache->AddObserver(this); | |
| 73 caches_.push_back(cache); | |
| 74 RecombineCachesAndMaybeTriggerUpdate(); | |
| 75 } | |
| 76 | |
| 77 void CloudPolicyProviderImpl::PrependCache(CloudPolicyCacheBase* cache) { | |
| 78 cache->AddObserver(this); | |
| 79 caches_.insert(caches_.begin(), cache); | |
| 80 RecombineCachesAndMaybeTriggerUpdate(); | |
| 81 } | |
| 82 | |
| 83 // static | |
| 84 ConfigurationPolicyType CloudPolicyProviderImpl::proxy_policies[] = { | |
| 85 kPolicyProxyMode, | |
| 86 kPolicyProxyServerMode, | |
| 87 kPolicyProxyServer, | |
| 88 kPolicyProxyPacUrl, | |
| 89 kPolicyProxyBypassList | |
| 90 }; | |
| 91 | |
| 92 // static | |
| 93 bool CloudPolicyProviderImpl::is_proxy_policy(ConfigurationPolicyType policy) { | |
| 94 const unsigned int n = arraysize(proxy_policies); | |
| 95 for (unsigned int i = 0; i < n; ++i) { | |
| 96 if (proxy_policies[i] == policy) | |
| 97 return true; | |
| 98 } | |
| 99 return false; | |
| 100 } | |
| 101 | |
| 102 // static | |
| 103 unsigned int CloudPolicyProviderImpl::proxy_policy_count() { | |
| 104 return arraysize(CloudPolicyProviderImpl::proxy_policies); | |
| 105 } | |
| 106 | |
| 107 // static | |
| 108 void CloudPolicyProviderImpl::CombineTwoPolicyMaps(const PolicyMap& base, | |
| 109 const PolicyMap& overlay, | |
| 110 PolicyMap* out_map) { | |
| 111 bool added_proxy_policy = false; | |
| 112 out_map->Clear(); | |
| 113 | |
| 114 for (PolicyMap::const_iterator i = base.begin(); i != base.end(); ++i) { | |
| 115 out_map->Set(i->first, i->second->DeepCopy()); | |
| 116 added_proxy_policy = added_proxy_policy || is_proxy_policy(i->first); | |
| 117 } | |
| 118 | |
| 119 // Add every entry of |overlay| which has not been added by |base|. Only add | |
| 120 // proxy policies if none of them was added by |base|. | |
| 121 for (PolicyMap::const_iterator i = overlay.begin(); i != overlay.end(); ++i) { | |
| 122 if (is_proxy_policy(i->first)) { | |
| 123 if (!added_proxy_policy) { | |
| 124 out_map->Set(i->first, i->second->DeepCopy()); | |
| 125 } | |
| 126 } else if (!out_map->Get(i->first)) { | |
| 127 out_map->Set(i->first, i->second->DeepCopy()); | |
| 128 } | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 void CloudPolicyProviderImpl::RecombineCachesAndMaybeTriggerUpdate() { | |
| 133 PolicyMap newly_combined; | |
| 134 for (ListType::iterator i = caches_.begin(); i != caches_.end(); ++i) { | |
| 135 if (!(*i)->initialization_complete()) | |
| 136 continue; | |
| 137 PolicyMap tmp_map; | |
| 138 CombineTwoPolicyMaps(newly_combined, *(*i)->policy(level_), &tmp_map); | |
| 139 newly_combined.Swap(&tmp_map); | |
| 140 } | |
| 141 if (newly_combined.Equals(combined_)) | |
| 142 return; | |
| 143 | |
| 144 // Trigger a notification if there was a change. | |
| 145 combined_.Swap(&newly_combined); | |
| 146 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, | |
| 147 observer_list_, OnUpdatePolicy()); | |
| 148 } | |
| 149 | |
| 150 } // namespace policy | |
| OLD | NEW |