| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/policy/core/common/policy_service_impl.h" | 5 #include "components/policy/core/common/policy_service_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/values.h" |
| 12 #include "components/policy/core/common/policy_bundle.h" | 13 #include "components/policy/core/common/policy_bundle.h" |
| 13 #include "components/policy/core/common/policy_map.h" | 14 #include "components/policy/core/common/policy_map.h" |
| 15 #include "policy/policy_constants.h" |
| 14 | 16 |
| 15 namespace policy { | 17 namespace policy { |
| 16 | 18 |
| 17 typedef PolicyServiceImpl::Providers::const_iterator Iterator; | 19 typedef PolicyServiceImpl::Providers::const_iterator Iterator; |
| 18 | 20 |
| 19 PolicyServiceImpl::PolicyServiceImpl( | 21 namespace { |
| 20 const Providers& providers, | 22 |
| 21 const PreprocessCallback& preprocess_callback) | 23 const char* kProxyPolicies[] = { |
| 22 : preprocess_callback_(preprocess_callback), | 24 key::kProxyMode, |
| 23 update_task_ptr_factory_(this) { | 25 key::kProxyServerMode, |
| 26 key::kProxyServer, |
| 27 key::kProxyPacUrl, |
| 28 key::kProxyBypassList, |
| 29 }; |
| 30 |
| 31 void FixDeprecatedPolicies(PolicyMap* policies) { |
| 32 // Proxy settings have been configured by 5 policies that didn't mix well |
| 33 // together, and maps of policies had to take this into account when merging |
| 34 // policy sources. The proxy settings will eventually be configured by a |
| 35 // single Dictionary policy when all providers have support for that. For |
| 36 // now, the individual policies are mapped here to a single Dictionary policy |
| 37 // that the rest of the policy machinery uses. |
| 38 |
| 39 // The highest (level, scope) pair for an existing proxy policy is determined |
| 40 // first, and then only policies with those exact attributes are merged. |
| 41 PolicyMap::Entry current_priority; // Defaults to the lowest priority. |
| 42 scoped_ptr<base::DictionaryValue> proxy_settings(new base::DictionaryValue); |
| 43 for (size_t i = 0; i < arraysize(kProxyPolicies); ++i) { |
| 44 const PolicyMap::Entry* entry = policies->Get(kProxyPolicies[i]); |
| 45 if (entry) { |
| 46 if (entry->has_higher_priority_than(current_priority)) { |
| 47 proxy_settings->Clear(); |
| 48 current_priority = *entry; |
| 49 } |
| 50 if (!entry->has_higher_priority_than(current_priority) && |
| 51 !current_priority.has_higher_priority_than(*entry)) { |
| 52 proxy_settings->Set(kProxyPolicies[i], entry->value->DeepCopy()); |
| 53 } |
| 54 policies->Erase(kProxyPolicies[i]); |
| 55 } |
| 56 } |
| 57 // Sets the new |proxy_settings| if kProxySettings isn't set yet, or if the |
| 58 // new priority is higher. |
| 59 const PolicyMap::Entry* existing = policies->Get(key::kProxySettings); |
| 60 if (!proxy_settings->empty() && |
| 61 (!existing || current_priority.has_higher_priority_than(*existing))) { |
| 62 policies->Set(key::kProxySettings, |
| 63 current_priority.level, |
| 64 current_priority.scope, |
| 65 proxy_settings.release(), |
| 66 NULL); |
| 67 } |
| 68 } |
| 69 |
| 70 } // namespace |
| 71 |
| 72 PolicyServiceImpl::PolicyServiceImpl(const Providers& providers) |
| 73 : update_task_ptr_factory_(this) { |
| 24 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) | 74 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) |
| 25 initialization_complete_[domain] = true; | 75 initialization_complete_[domain] = true; |
| 26 providers_ = providers; | 76 providers_ = providers; |
| 27 for (Iterator it = providers.begin(); it != providers.end(); ++it) { | 77 for (Iterator it = providers.begin(); it != providers.end(); ++it) { |
| 28 ConfigurationPolicyProvider* provider = *it; | 78 ConfigurationPolicyProvider* provider = *it; |
| 29 provider->AddObserver(this); | 79 provider->AddObserver(this); |
| 30 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) { | 80 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) { |
| 31 initialization_complete_[domain] &= | 81 initialization_complete_[domain] &= |
| 32 provider->IsInitializationComplete(static_cast<PolicyDomain>(domain)); | 82 provider->IsInitializationComplete(static_cast<PolicyDomain>(domain)); |
| 33 } | 83 } |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 ObserverMap::iterator iterator = observers_.find(ns.domain); | 173 ObserverMap::iterator iterator = observers_.find(ns.domain); |
| 124 if (iterator != observers_.end()) { | 174 if (iterator != observers_.end()) { |
| 125 FOR_EACH_OBSERVER(PolicyService::Observer, | 175 FOR_EACH_OBSERVER(PolicyService::Observer, |
| 126 *iterator->second, | 176 *iterator->second, |
| 127 OnPolicyUpdated(ns, previous, current)); | 177 OnPolicyUpdated(ns, previous, current)); |
| 128 } | 178 } |
| 129 } | 179 } |
| 130 | 180 |
| 131 void PolicyServiceImpl::MergeAndTriggerUpdates() { | 181 void PolicyServiceImpl::MergeAndTriggerUpdates() { |
| 132 // Merge from each provider in their order of priority. | 182 // Merge from each provider in their order of priority. |
| 183 const PolicyNamespace chrome_namespace(POLICY_DOMAIN_CHROME, std::string()); |
| 133 PolicyBundle bundle; | 184 PolicyBundle bundle; |
| 134 for (Iterator it = providers_.begin(); it != providers_.end(); ++it) { | 185 for (Iterator it = providers_.begin(); it != providers_.end(); ++it) { |
| 135 PolicyBundle provided_bundle; | 186 PolicyBundle provided_bundle; |
| 136 provided_bundle.CopyFrom((*it)->policies()); | 187 provided_bundle.CopyFrom((*it)->policies()); |
| 137 if (!preprocess_callback_.is_null()) | 188 FixDeprecatedPolicies(&provided_bundle.Get(chrome_namespace)); |
| 138 preprocess_callback_.Run(&provided_bundle); | |
| 139 bundle.MergeFrom(provided_bundle); | 189 bundle.MergeFrom(provided_bundle); |
| 140 } | 190 } |
| 141 | 191 |
| 142 // Swap first, so that observers that call GetPolicies() see the current | 192 // Swap first, so that observers that call GetPolicies() see the current |
| 143 // values. | 193 // values. |
| 144 policy_bundle_.Swap(&bundle); | 194 policy_bundle_.Swap(&bundle); |
| 145 | 195 |
| 146 // Only notify observers of namespaces that have been modified. | 196 // Only notify observers of namespaces that have been modified. |
| 147 const PolicyMap kEmpty; | 197 const PolicyMap kEmpty; |
| 148 PolicyBundle::const_iterator it_new = policy_bundle_.begin(); | 198 PolicyBundle::const_iterator it_new = policy_bundle_.begin(); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 if (refresh_pending_.empty() && !refresh_callbacks_.empty()) { | 263 if (refresh_pending_.empty() && !refresh_callbacks_.empty()) { |
| 214 std::vector<base::Closure> callbacks; | 264 std::vector<base::Closure> callbacks; |
| 215 callbacks.swap(refresh_callbacks_); | 265 callbacks.swap(refresh_callbacks_); |
| 216 std::vector<base::Closure>::iterator it; | 266 std::vector<base::Closure>::iterator it; |
| 217 for (it = callbacks.begin(); it != callbacks.end(); ++it) | 267 for (it = callbacks.begin(); it != callbacks.end(); ++it) |
| 218 it->Run(); | 268 it->Run(); |
| 219 } | 269 } |
| 220 } | 270 } |
| 221 | 271 |
| 222 } // namespace policy | 272 } // namespace policy |
| OLD | NEW |