| 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" |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 initialization_complete_[domain] &= | 81 initialization_complete_[domain] &= |
| 82 provider->IsInitializationComplete(static_cast<PolicyDomain>(domain)); | 82 provider->IsInitializationComplete(static_cast<PolicyDomain>(domain)); |
| 83 } | 83 } |
| 84 } | 84 } |
| 85 // There are no observers yet, but calls to GetPolicies() should already get | 85 // There are no observers yet, but calls to GetPolicies() should already get |
| 86 // the processed policy values. | 86 // the processed policy values. |
| 87 MergeAndTriggerUpdates(); | 87 MergeAndTriggerUpdates(); |
| 88 } | 88 } |
| 89 | 89 |
| 90 PolicyServiceImpl::~PolicyServiceImpl() { | 90 PolicyServiceImpl::~PolicyServiceImpl() { |
| 91 DCHECK(thread_checker_.CalledOnValidThread()); |
| 91 for (Iterator it = providers_.begin(); it != providers_.end(); ++it) | 92 for (Iterator it = providers_.begin(); it != providers_.end(); ++it) |
| 92 (*it)->RemoveObserver(this); | 93 (*it)->RemoveObserver(this); |
| 93 STLDeleteValues(&observers_); | 94 STLDeleteValues(&observers_); |
| 94 } | 95 } |
| 95 | 96 |
| 96 void PolicyServiceImpl::AddObserver(PolicyDomain domain, | 97 void PolicyServiceImpl::AddObserver(PolicyDomain domain, |
| 97 PolicyService::Observer* observer) { | 98 PolicyService::Observer* observer) { |
| 99 DCHECK(thread_checker_.CalledOnValidThread()); |
| 98 Observers*& list = observers_[domain]; | 100 Observers*& list = observers_[domain]; |
| 99 if (!list) | 101 if (!list) |
| 100 list = new Observers(); | 102 list = new Observers(); |
| 101 list->AddObserver(observer); | 103 list->AddObserver(observer); |
| 102 } | 104 } |
| 103 | 105 |
| 104 void PolicyServiceImpl::RemoveObserver(PolicyDomain domain, | 106 void PolicyServiceImpl::RemoveObserver(PolicyDomain domain, |
| 105 PolicyService::Observer* observer) { | 107 PolicyService::Observer* observer) { |
| 108 DCHECK(thread_checker_.CalledOnValidThread()); |
| 106 ObserverMap::iterator it = observers_.find(domain); | 109 ObserverMap::iterator it = observers_.find(domain); |
| 107 if (it == observers_.end()) { | 110 if (it == observers_.end()) { |
| 108 NOTREACHED(); | 111 NOTREACHED(); |
| 109 return; | 112 return; |
| 110 } | 113 } |
| 111 it->second->RemoveObserver(observer); | 114 it->second->RemoveObserver(observer); |
| 112 if (!it->second->might_have_observers()) { | 115 if (!it->second->might_have_observers()) { |
| 113 delete it->second; | 116 delete it->second; |
| 114 observers_.erase(it); | 117 observers_.erase(it); |
| 115 } | 118 } |
| 116 } | 119 } |
| 117 | 120 |
| 118 const PolicyMap& PolicyServiceImpl::GetPolicies( | 121 const PolicyMap& PolicyServiceImpl::GetPolicies( |
| 119 const PolicyNamespace& ns) const { | 122 const PolicyNamespace& ns) const { |
| 123 DCHECK(thread_checker_.CalledOnValidThread()); |
| 120 return policy_bundle_.Get(ns); | 124 return policy_bundle_.Get(ns); |
| 121 } | 125 } |
| 122 | 126 |
| 123 bool PolicyServiceImpl::IsInitializationComplete(PolicyDomain domain) const { | 127 bool PolicyServiceImpl::IsInitializationComplete(PolicyDomain domain) const { |
| 128 DCHECK(thread_checker_.CalledOnValidThread()); |
| 124 DCHECK(domain >= 0 && domain < POLICY_DOMAIN_SIZE); | 129 DCHECK(domain >= 0 && domain < POLICY_DOMAIN_SIZE); |
| 125 return initialization_complete_[domain]; | 130 return initialization_complete_[domain]; |
| 126 } | 131 } |
| 127 | 132 |
| 128 void PolicyServiceImpl::RefreshPolicies(const base::Closure& callback) { | 133 void PolicyServiceImpl::RefreshPolicies(const base::Closure& callback) { |
| 134 DCHECK(thread_checker_.CalledOnValidThread()); |
| 135 |
| 129 if (!callback.is_null()) | 136 if (!callback.is_null()) |
| 130 refresh_callbacks_.push_back(callback); | 137 refresh_callbacks_.push_back(callback); |
| 131 | 138 |
| 132 if (providers_.empty()) { | 139 if (providers_.empty()) { |
| 133 // Refresh is immediately complete if there are no providers. See the note | 140 // Refresh is immediately complete if there are no providers. See the note |
| 134 // on OnUpdatePolicy() about why this is a posted task. | 141 // on OnUpdatePolicy() about why this is a posted task. |
| 135 update_task_ptr_factory_.InvalidateWeakPtrs(); | 142 update_task_ptr_factory_.InvalidateWeakPtrs(); |
| 136 base::MessageLoop::current()->PostTask( | 143 base::MessageLoop::current()->PostTask( |
| 137 FROM_HERE, | 144 FROM_HERE, |
| 138 base::Bind(&PolicyServiceImpl::MergeAndTriggerUpdates, | 145 base::Bind(&PolicyServiceImpl::MergeAndTriggerUpdates, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 163 base::MessageLoop::current()->PostTask( | 170 base::MessageLoop::current()->PostTask( |
| 164 FROM_HERE, | 171 FROM_HERE, |
| 165 base::Bind(&PolicyServiceImpl::MergeAndTriggerUpdates, | 172 base::Bind(&PolicyServiceImpl::MergeAndTriggerUpdates, |
| 166 update_task_ptr_factory_.GetWeakPtr())); | 173 update_task_ptr_factory_.GetWeakPtr())); |
| 167 } | 174 } |
| 168 | 175 |
| 169 void PolicyServiceImpl::NotifyNamespaceUpdated( | 176 void PolicyServiceImpl::NotifyNamespaceUpdated( |
| 170 const PolicyNamespace& ns, | 177 const PolicyNamespace& ns, |
| 171 const PolicyMap& previous, | 178 const PolicyMap& previous, |
| 172 const PolicyMap& current) { | 179 const PolicyMap& current) { |
| 180 DCHECK(thread_checker_.CalledOnValidThread()); |
| 173 ObserverMap::iterator iterator = observers_.find(ns.domain); | 181 ObserverMap::iterator iterator = observers_.find(ns.domain); |
| 174 if (iterator != observers_.end()) { | 182 if (iterator != observers_.end()) { |
| 175 FOR_EACH_OBSERVER(PolicyService::Observer, | 183 FOR_EACH_OBSERVER(PolicyService::Observer, |
| 176 *iterator->second, | 184 *iterator->second, |
| 177 OnPolicyUpdated(ns, previous, current)); | 185 OnPolicyUpdated(ns, previous, current)); |
| 178 } | 186 } |
| 179 } | 187 } |
| 180 | 188 |
| 181 void PolicyServiceImpl::MergeAndTriggerUpdates() { | 189 void PolicyServiceImpl::MergeAndTriggerUpdates() { |
| 182 // Merge from each provider in their order of priority. | 190 // Merge from each provider in their order of priority. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 | 232 |
| 225 // Sends updates for the remaining removed namespaces, if any. | 233 // Sends updates for the remaining removed namespaces, if any. |
| 226 for (; it_old != end_old; ++it_old) | 234 for (; it_old != end_old; ++it_old) |
| 227 NotifyNamespaceUpdated(it_old->first, *it_old->second, kEmpty); | 235 NotifyNamespaceUpdated(it_old->first, *it_old->second, kEmpty); |
| 228 | 236 |
| 229 CheckInitializationComplete(); | 237 CheckInitializationComplete(); |
| 230 CheckRefreshComplete(); | 238 CheckRefreshComplete(); |
| 231 } | 239 } |
| 232 | 240 |
| 233 void PolicyServiceImpl::CheckInitializationComplete() { | 241 void PolicyServiceImpl::CheckInitializationComplete() { |
| 242 DCHECK(thread_checker_.CalledOnValidThread()); |
| 243 |
| 234 // Check if all the providers just became initialized for each domain; if so, | 244 // Check if all the providers just became initialized for each domain; if so, |
| 235 // notify that domain's observers. | 245 // notify that domain's observers. |
| 236 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) { | 246 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) { |
| 237 if (initialization_complete_[domain]) | 247 if (initialization_complete_[domain]) |
| 238 continue; | 248 continue; |
| 239 | 249 |
| 240 PolicyDomain policy_domain = static_cast<PolicyDomain>(domain); | 250 PolicyDomain policy_domain = static_cast<PolicyDomain>(domain); |
| 241 | 251 |
| 242 bool all_complete = true; | 252 bool all_complete = true; |
| 243 for (Iterator it = providers_.begin(); it != providers_.end(); ++it) { | 253 for (Iterator it = providers_.begin(); it != providers_.end(); ++it) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 263 if (refresh_pending_.empty() && !refresh_callbacks_.empty()) { | 273 if (refresh_pending_.empty() && !refresh_callbacks_.empty()) { |
| 264 std::vector<base::Closure> callbacks; | 274 std::vector<base::Closure> callbacks; |
| 265 callbacks.swap(refresh_callbacks_); | 275 callbacks.swap(refresh_callbacks_); |
| 266 std::vector<base::Closure>::iterator it; | 276 std::vector<base::Closure>::iterator it; |
| 267 for (it = callbacks.begin(); it != callbacks.end(); ++it) | 277 for (it = callbacks.begin(); it != callbacks.end(); ++it) |
| 268 it->Run(); | 278 it->Run(); |
| 269 } | 279 } |
| 270 } | 280 } |
| 271 | 281 |
| 272 } // namespace policy | 282 } // namespace policy |
| OLD | NEW |