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 <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 } | 71 } |
72 } | 72 } |
73 | 73 |
74 } // namespace | 74 } // namespace |
75 | 75 |
76 PolicyServiceImpl::PolicyServiceImpl(const Providers& providers) | 76 PolicyServiceImpl::PolicyServiceImpl(const Providers& providers) |
77 : update_task_ptr_factory_(this) { | 77 : update_task_ptr_factory_(this) { |
78 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) | 78 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) |
79 initialization_complete_[domain] = true; | 79 initialization_complete_[domain] = true; |
80 providers_ = providers; | 80 providers_ = providers; |
81 for (auto provider : providers) { | 81 for (auto* provider : providers) { |
82 provider->AddObserver(this); | 82 provider->AddObserver(this); |
83 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) { | 83 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) { |
84 initialization_complete_[domain] &= | 84 initialization_complete_[domain] &= |
85 provider->IsInitializationComplete(static_cast<PolicyDomain>(domain)); | 85 provider->IsInitializationComplete(static_cast<PolicyDomain>(domain)); |
86 } | 86 } |
87 } | 87 } |
88 // There are no observers yet, but calls to GetPolicies() should already get | 88 // There are no observers yet, but calls to GetPolicies() should already get |
89 // the processed policy values. | 89 // the processed policy values. |
90 MergeAndTriggerUpdates(); | 90 MergeAndTriggerUpdates(); |
91 } | 91 } |
92 | 92 |
93 PolicyServiceImpl::~PolicyServiceImpl() { | 93 PolicyServiceImpl::~PolicyServiceImpl() { |
94 DCHECK(thread_checker_.CalledOnValidThread()); | 94 DCHECK(thread_checker_.CalledOnValidThread()); |
95 for (auto provider : providers_) | 95 for (auto* provider : providers_) |
96 provider->RemoveObserver(this); | 96 provider->RemoveObserver(this); |
97 } | 97 } |
98 | 98 |
99 void PolicyServiceImpl::AddObserver(PolicyDomain domain, | 99 void PolicyServiceImpl::AddObserver(PolicyDomain domain, |
100 PolicyService::Observer* observer) { | 100 PolicyService::Observer* observer) { |
101 DCHECK(thread_checker_.CalledOnValidThread()); | 101 DCHECK(thread_checker_.CalledOnValidThread()); |
102 std::unique_ptr<Observers>& list = observers_[domain]; | 102 std::unique_ptr<Observers>& list = observers_[domain]; |
103 if (!list) | 103 if (!list) |
104 list = base::MakeUnique<Observers>(); | 104 list = base::MakeUnique<Observers>(); |
105 list->AddObserver(observer); | 105 list->AddObserver(observer); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 if (providers_.empty()) { | 140 if (providers_.empty()) { |
141 // Refresh is immediately complete if there are no providers. See the note | 141 // Refresh is immediately complete if there are no providers. See the note |
142 // on OnUpdatePolicy() about why this is a posted task. | 142 // on OnUpdatePolicy() about why this is a posted task. |
143 update_task_ptr_factory_.InvalidateWeakPtrs(); | 143 update_task_ptr_factory_.InvalidateWeakPtrs(); |
144 base::ThreadTaskRunnerHandle::Get()->PostTask( | 144 base::ThreadTaskRunnerHandle::Get()->PostTask( |
145 FROM_HERE, base::Bind(&PolicyServiceImpl::MergeAndTriggerUpdates, | 145 FROM_HERE, base::Bind(&PolicyServiceImpl::MergeAndTriggerUpdates, |
146 update_task_ptr_factory_.GetWeakPtr())); | 146 update_task_ptr_factory_.GetWeakPtr())); |
147 } else { | 147 } else { |
148 // Some providers might invoke OnUpdatePolicy synchronously while handling | 148 // Some providers might invoke OnUpdatePolicy synchronously while handling |
149 // RefreshPolicies. Mark all as pending before refreshing. | 149 // RefreshPolicies. Mark all as pending before refreshing. |
150 for (auto provider : providers_) | 150 for (auto* provider : providers_) |
151 refresh_pending_.insert(provider); | 151 refresh_pending_.insert(provider); |
152 for (auto provider : providers_) | 152 for (auto* provider : providers_) |
153 provider->RefreshPolicies(); | 153 provider->RefreshPolicies(); |
154 } | 154 } |
155 } | 155 } |
156 | 156 |
157 void PolicyServiceImpl::OnUpdatePolicy(ConfigurationPolicyProvider* provider) { | 157 void PolicyServiceImpl::OnUpdatePolicy(ConfigurationPolicyProvider* provider) { |
158 DCHECK_EQ(1, std::count(providers_.begin(), providers_.end(), provider)); | 158 DCHECK_EQ(1, std::count(providers_.begin(), providers_.end(), provider)); |
159 refresh_pending_.erase(provider); | 159 refresh_pending_.erase(provider); |
160 | 160 |
161 // Note: a policy change may trigger further policy changes in some providers. | 161 // Note: a policy change may trigger further policy changes in some providers. |
162 // For example, disabling SigninAllowed would cause the CloudPolicyManager to | 162 // For example, disabling SigninAllowed would cause the CloudPolicyManager to |
(...skipping 18 matching lines...) Expand all Loading... |
181 if (iterator != observers_.end()) { | 181 if (iterator != observers_.end()) { |
182 for (auto& observer : *iterator->second) | 182 for (auto& observer : *iterator->second) |
183 observer.OnPolicyUpdated(ns, previous, current); | 183 observer.OnPolicyUpdated(ns, previous, current); |
184 } | 184 } |
185 } | 185 } |
186 | 186 |
187 void PolicyServiceImpl::MergeAndTriggerUpdates() { | 187 void PolicyServiceImpl::MergeAndTriggerUpdates() { |
188 // Merge from each provider in their order of priority. | 188 // Merge from each provider in their order of priority. |
189 const PolicyNamespace chrome_namespace(POLICY_DOMAIN_CHROME, std::string()); | 189 const PolicyNamespace chrome_namespace(POLICY_DOMAIN_CHROME, std::string()); |
190 PolicyBundle bundle; | 190 PolicyBundle bundle; |
191 for (auto provider : providers_) { | 191 for (auto* provider : providers_) { |
192 PolicyBundle provided_bundle; | 192 PolicyBundle provided_bundle; |
193 provided_bundle.CopyFrom(provider->policies()); | 193 provided_bundle.CopyFrom(provider->policies()); |
194 RemapProxyPolicies(&provided_bundle.Get(chrome_namespace)); | 194 RemapProxyPolicies(&provided_bundle.Get(chrome_namespace)); |
195 bundle.MergeFrom(provided_bundle); | 195 bundle.MergeFrom(provided_bundle); |
196 } | 196 } |
197 | 197 |
198 // Swap first, so that observers that call GetPolicies() see the current | 198 // Swap first, so that observers that call GetPolicies() see the current |
199 // values. | 199 // values. |
200 policy_bundle_.Swap(&bundle); | 200 policy_bundle_.Swap(&bundle); |
201 | 201 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
241 | 241 |
242 // Check if all the providers just became initialized for each domain; if so, | 242 // Check if all the providers just became initialized for each domain; if so, |
243 // notify that domain's observers. | 243 // notify that domain's observers. |
244 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) { | 244 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) { |
245 if (initialization_complete_[domain]) | 245 if (initialization_complete_[domain]) |
246 continue; | 246 continue; |
247 | 247 |
248 PolicyDomain policy_domain = static_cast<PolicyDomain>(domain); | 248 PolicyDomain policy_domain = static_cast<PolicyDomain>(domain); |
249 | 249 |
250 bool all_complete = true; | 250 bool all_complete = true; |
251 for (auto provider : providers_) { | 251 for (auto* provider : providers_) { |
252 if (!provider->IsInitializationComplete(policy_domain)) { | 252 if (!provider->IsInitializationComplete(policy_domain)) { |
253 all_complete = false; | 253 all_complete = false; |
254 break; | 254 break; |
255 } | 255 } |
256 } | 256 } |
257 if (all_complete) { | 257 if (all_complete) { |
258 initialization_complete_[domain] = true; | 258 initialization_complete_[domain] = true; |
259 auto iter = observers_.find(policy_domain); | 259 auto iter = observers_.find(policy_domain); |
260 if (iter != observers_.end()) { | 260 if (iter != observers_.end()) { |
261 for (auto& observer : *iter->second) | 261 for (auto& observer : *iter->second) |
262 observer.OnPolicyServiceInitialized(policy_domain); | 262 observer.OnPolicyServiceInitialized(policy_domain); |
263 } | 263 } |
264 } | 264 } |
265 } | 265 } |
266 } | 266 } |
267 | 267 |
268 void PolicyServiceImpl::CheckRefreshComplete() { | 268 void PolicyServiceImpl::CheckRefreshComplete() { |
269 // Invoke all the callbacks if a refresh has just fully completed. | 269 // Invoke all the callbacks if a refresh has just fully completed. |
270 if (refresh_pending_.empty() && !refresh_callbacks_.empty()) { | 270 if (refresh_pending_.empty() && !refresh_callbacks_.empty()) { |
271 std::vector<base::Closure> callbacks; | 271 std::vector<base::Closure> callbacks; |
272 callbacks.swap(refresh_callbacks_); | 272 callbacks.swap(refresh_callbacks_); |
273 std::vector<base::Closure>::iterator it; | 273 std::vector<base::Closure>::iterator it; |
274 for (it = callbacks.begin(); it != callbacks.end(); ++it) | 274 for (it = callbacks.begin(); it != callbacks.end(); ++it) |
275 it->Run(); | 275 it->Run(); |
276 } | 276 } |
277 } | 277 } |
278 | 278 |
279 } // namespace policy | 279 } // namespace policy |
OLD | NEW |