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. | |
|
Mattias Nissler (ping if slow)
2011/06/21 20:09:56
I must say I don't particularly like this. I think
gfeher
2011/06/22 19:18:34
Me neither.
We could trigger the policy fetch for
| |
| 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 ConfigurationPolicyType CloudPolicyProviderImpl::proxy_policies[] = { | |
| 83 kPolicyProxyMode, | |
| 84 kPolicyProxyServerMode, | |
| 85 kPolicyProxyServer, | |
| 86 kPolicyProxyPacUrl, | |
| 87 kPolicyProxyBypassList }; | |
|
Mattias Nissler (ping if slow)
2011/06/21 20:09:56
closing brace goes on next line.
gfeher
2011/06/22 19:18:34
Done.
| |
| 88 | |
| 89 // static | |
| 90 bool CloudPolicyProviderImpl::is_proxy_policy(ConfigurationPolicyType policy) { | |
| 91 const unsigned int n = arraysize(proxy_policies); | |
| 92 for (unsigned int i = 0; i < n; ++i) | |
|
Mattias Nissler (ping if slow)
2011/06/21 20:09:56
need curlies
gfeher
2011/06/22 19:18:34
Done.
| |
| 93 if (proxy_policies[i] == policy) | |
| 94 return true; | |
| 95 return false; | |
| 96 } | |
| 97 | |
| 98 // static | |
| 99 unsigned int CloudPolicyProviderImpl::proxy_policy_count() { | |
| 100 return arraysize(CloudPolicyProviderImpl::proxy_policies); | |
| 101 } | |
| 102 | |
| 103 void CloudPolicyProviderImpl::CombineTwoPolicyMaps(const PolicyMap& base, | |
| 104 const PolicyMap& overlay, | |
| 105 PolicyMap* out_map) { | |
| 106 bool added_proxy_policy = false; | |
| 107 out_map->Clear(); | |
| 108 | |
| 109 for (PolicyMap::const_iterator i = base.begin(); i != base.end(); ++i) { | |
| 110 out_map->Set(i->first, i->second->DeepCopy()); | |
| 111 added_proxy_policy = added_proxy_policy || is_proxy_policy(i->first); | |
| 112 } | |
| 113 | |
| 114 // Add every entry of |overlay| which has not been added by |base|. Only add | |
| 115 // proxy policies if none of them was added by |base|. | |
| 116 for (PolicyMap::const_iterator i = overlay.begin(); i != overlay.end(); ++i) { | |
| 117 if (is_proxy_policy(i->first)) { | |
| 118 if (!added_proxy_policy) { | |
| 119 out_map->Set(i->first, i->second->DeepCopy()); | |
| 120 } | |
| 121 } else if (!out_map->Get(i->first)) { | |
| 122 out_map->Set(i->first, i->second->DeepCopy()); | |
| 123 } | |
| 124 } | |
| 125 } | |
| 126 | |
| 127 void CloudPolicyProviderImpl::RecombineCachesAndMaybeTriggerUpdate() { | |
| 128 PolicyMap newly_combined; | |
| 129 for (ListType::iterator i = caches_.begin(); i != caches_.end(); ++i) { | |
| 130 if (!(*i)->initialization_complete()) | |
| 131 continue; | |
| 132 PolicyMap tmp_map; | |
| 133 CombineTwoPolicyMaps(newly_combined, *(*i)->policy(level_), &tmp_map); | |
| 134 newly_combined.Swap(&tmp_map); | |
| 135 } | |
| 136 if (newly_combined.Equals(combined_)) | |
| 137 return; | |
| 138 | |
| 139 // Trigger a notification if there was a change. | |
| 140 combined_.Swap(&newly_combined); | |
| 141 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, | |
| 142 observer_list_, OnUpdatePolicy()); | |
| 143 } | |
| 144 | |
| 145 } // namespace policy | |
| OLD | NEW |