Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: components/policy/core/common/policy_service_impl.cc

Issue 830193002: Using PolicyServiceWatcher instead of PolicyWatcherLinux/Win/Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed building for Chrome OS. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 {
Mattias Nissler (ping if slow) 2015/01/06 09:06:11 For consistency, please add a thread check here as
Łukasz Anforowicz 2015/01/07 17:54:14 Ok. I'll also change the comment in the .h file.
120 return policy_bundle_.Get(ns); 123 return policy_bundle_.Get(ns);
121 } 124 }
122 125
123 bool PolicyServiceImpl::IsInitializationComplete(PolicyDomain domain) const { 126 bool PolicyServiceImpl::IsInitializationComplete(PolicyDomain domain) const {
Mattias Nissler (ping if slow) 2015/01/06 09:06:11 and here
Łukasz Anforowicz 2015/01/07 17:54:14 Done.
124 DCHECK(domain >= 0 && domain < POLICY_DOMAIN_SIZE); 127 DCHECK(domain >= 0 && domain < POLICY_DOMAIN_SIZE);
125 return initialization_complete_[domain]; 128 return initialization_complete_[domain];
126 } 129 }
127 130
128 void PolicyServiceImpl::RefreshPolicies(const base::Closure& callback) { 131 void PolicyServiceImpl::RefreshPolicies(const base::Closure& callback) {
Mattias Nissler (ping if slow) 2015/01/06 09:06:11 and here
Łukasz Anforowicz 2015/01/07 17:54:13 Done.
129 if (!callback.is_null()) 132 if (!callback.is_null())
130 refresh_callbacks_.push_back(callback); 133 refresh_callbacks_.push_back(callback);
131 134
132 if (providers_.empty()) { 135 if (providers_.empty()) {
133 // Refresh is immediately complete if there are no providers. See the note 136 // Refresh is immediately complete if there are no providers. See the note
134 // on OnUpdatePolicy() about why this is a posted task. 137 // on OnUpdatePolicy() about why this is a posted task.
135 update_task_ptr_factory_.InvalidateWeakPtrs(); 138 update_task_ptr_factory_.InvalidateWeakPtrs();
136 base::MessageLoop::current()->PostTask( 139 base::MessageLoop::current()->PostTask(
137 FROM_HERE, 140 FROM_HERE,
138 base::Bind(&PolicyServiceImpl::MergeAndTriggerUpdates, 141 base::Bind(&PolicyServiceImpl::MergeAndTriggerUpdates,
(...skipping 24 matching lines...) Expand all
163 base::MessageLoop::current()->PostTask( 166 base::MessageLoop::current()->PostTask(
164 FROM_HERE, 167 FROM_HERE,
165 base::Bind(&PolicyServiceImpl::MergeAndTriggerUpdates, 168 base::Bind(&PolicyServiceImpl::MergeAndTriggerUpdates,
166 update_task_ptr_factory_.GetWeakPtr())); 169 update_task_ptr_factory_.GetWeakPtr()));
167 } 170 }
168 171
169 void PolicyServiceImpl::NotifyNamespaceUpdated( 172 void PolicyServiceImpl::NotifyNamespaceUpdated(
170 const PolicyNamespace& ns, 173 const PolicyNamespace& ns,
171 const PolicyMap& previous, 174 const PolicyMap& previous,
172 const PolicyMap& current) { 175 const PolicyMap& current) {
176 DCHECK(thread_checker_.CalledOnValidThread());
173 ObserverMap::iterator iterator = observers_.find(ns.domain); 177 ObserverMap::iterator iterator = observers_.find(ns.domain);
174 if (iterator != observers_.end()) { 178 if (iterator != observers_.end()) {
175 FOR_EACH_OBSERVER(PolicyService::Observer, 179 FOR_EACH_OBSERVER(PolicyService::Observer,
176 *iterator->second, 180 *iterator->second,
177 OnPolicyUpdated(ns, previous, current)); 181 OnPolicyUpdated(ns, previous, current));
178 } 182 }
179 } 183 }
180 184
181 void PolicyServiceImpl::MergeAndTriggerUpdates() { 185 void PolicyServiceImpl::MergeAndTriggerUpdates() {
182 // Merge from each provider in their order of priority. 186 // Merge from each provider in their order of priority.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 228
225 // Sends updates for the remaining removed namespaces, if any. 229 // Sends updates for the remaining removed namespaces, if any.
226 for (; it_old != end_old; ++it_old) 230 for (; it_old != end_old; ++it_old)
227 NotifyNamespaceUpdated(it_old->first, *it_old->second, kEmpty); 231 NotifyNamespaceUpdated(it_old->first, *it_old->second, kEmpty);
228 232
229 CheckInitializationComplete(); 233 CheckInitializationComplete();
230 CheckRefreshComplete(); 234 CheckRefreshComplete();
231 } 235 }
232 236
233 void PolicyServiceImpl::CheckInitializationComplete() { 237 void PolicyServiceImpl::CheckInitializationComplete() {
238 DCHECK(thread_checker_.CalledOnValidThread());
239
234 // Check if all the providers just became initialized for each domain; if so, 240 // Check if all the providers just became initialized for each domain; if so,
235 // notify that domain's observers. 241 // notify that domain's observers.
236 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) { 242 for (int domain = 0; domain < POLICY_DOMAIN_SIZE; ++domain) {
237 if (initialization_complete_[domain]) 243 if (initialization_complete_[domain])
238 continue; 244 continue;
239 245
240 PolicyDomain policy_domain = static_cast<PolicyDomain>(domain); 246 PolicyDomain policy_domain = static_cast<PolicyDomain>(domain);
241 247
242 bool all_complete = true; 248 bool all_complete = true;
243 for (Iterator it = providers_.begin(); it != providers_.end(); ++it) { 249 for (Iterator it = providers_.begin(); it != providers_.end(); ++it) {
(...skipping 19 matching lines...) Expand all
263 if (refresh_pending_.empty() && !refresh_callbacks_.empty()) { 269 if (refresh_pending_.empty() && !refresh_callbacks_.empty()) {
264 std::vector<base::Closure> callbacks; 270 std::vector<base::Closure> callbacks;
265 callbacks.swap(refresh_callbacks_); 271 callbacks.swap(refresh_callbacks_);
266 std::vector<base::Closure>::iterator it; 272 std::vector<base::Closure>::iterator it;
267 for (it = callbacks.begin(); it != callbacks.end(); ++it) 273 for (it = callbacks.begin(); it != callbacks.end(); ++it)
268 it->Run(); 274 it->Run();
269 } 275 }
270 } 276 }
271 277
272 } // namespace policy 278 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698