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 "chrome/browser/policy/policy_service_impl.h" | 5 #include "chrome/browser/policy/policy_service_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | |
10 #include "base/message_loop.h" | |
9 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
10 #include "chrome/browser/policy/policy_map.h" | 12 #include "chrome/browser/policy/policy_map.h" |
11 | 13 |
12 namespace policy { | 14 namespace policy { |
13 | 15 |
14 typedef PolicyServiceImpl::Providers::const_iterator Iterator; | 16 typedef PolicyServiceImpl::Providers::const_iterator Iterator; |
15 | 17 |
16 PolicyServiceImpl::PolicyServiceImpl(const Providers& providers) { | 18 PolicyServiceImpl::PolicyServiceImpl(const Providers& providers) |
19 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
17 initialization_complete_ = true; | 20 initialization_complete_ = true; |
18 providers_ = providers; | 21 providers_ = providers; |
19 for (Iterator it = providers.begin(); it != providers.end(); ++it) { | 22 for (Iterator it = providers.begin(); it != providers.end(); ++it) { |
20 ConfigurationPolicyProvider* provider = *it; | 23 ConfigurationPolicyProvider* provider = *it; |
21 provider->AddObserver(this); | 24 provider->AddObserver(this); |
22 initialization_complete_ &= provider->IsInitializationComplete(); | 25 initialization_complete_ &= provider->IsInitializationComplete(); |
23 } | 26 } |
24 // There are no observers yet, but calls to GetPolicies() should already get | 27 // There are no observers yet, but calls to GetPolicies() should already get |
25 // the processed policy values. | 28 // the processed policy values. |
26 MergeAndTriggerUpdates(); | 29 MergeAndTriggerUpdates(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 void PolicyServiceImpl::OnUpdatePolicy(ConfigurationPolicyProvider* provider) { | 87 void PolicyServiceImpl::OnUpdatePolicy(ConfigurationPolicyProvider* provider) { |
85 DCHECK_EQ(1, std::count(providers_.begin(), providers_.end(), provider)); | 88 DCHECK_EQ(1, std::count(providers_.begin(), providers_.end(), provider)); |
86 refresh_pending_.erase(provider); | 89 refresh_pending_.erase(provider); |
87 MergeAndTriggerUpdates(); | 90 MergeAndTriggerUpdates(); |
88 } | 91 } |
89 | 92 |
90 void PolicyServiceImpl::NotifyNamespaceUpdated( | 93 void PolicyServiceImpl::NotifyNamespaceUpdated( |
91 const PolicyBundle::PolicyNamespace& ns, | 94 const PolicyBundle::PolicyNamespace& ns, |
92 const PolicyMap& previous, | 95 const PolicyMap& previous, |
93 const PolicyMap& current) { | 96 const PolicyMap& current) { |
94 ObserverMap::iterator iterator = observers_.find(ns.first); | 97 // If running a unit test that hasn't setup a MessageLoop, don't send any |
98 // notifications. | |
99 if (!MessageLoop::current()) | |
100 return; | |
101 | |
102 // Don't queue up a task if we have no observers - that way Observers added | |
103 // later don't get notified of changes that happened during construction time. | |
104 if (observers_.find(ns.first) == observers_.end()) | |
105 return; | |
106 | |
107 // Notify Observers via a queued task, so Observers can't trigger a re-entrant | |
108 // call to MergeAndTriggerUpdates() by modifying policy. | |
109 scoped_ptr<PolicyChangeInfo> changes( | |
Joao da Silva
2013/01/07 09:23:52
#include "base/memory/scoped_ptr.h"
Andrew T Wilson (Slow)
2013/01/07 14:11:32
Done.
| |
110 new PolicyChangeInfo(ns, previous, current)); | |
111 MessageLoop::current()->PostTask( | |
112 FROM_HERE, | |
113 base::Bind(&PolicyServiceImpl::NotifyNamespaceUpdatedTask, | |
114 weak_ptr_factory_.GetWeakPtr(), | |
115 base::Passed(&changes))); | |
116 } | |
117 | |
118 void PolicyServiceImpl::NotifyNamespaceUpdatedTask( | |
119 scoped_ptr<PolicyChangeInfo> changes) { | |
120 ObserverMap::iterator iterator = observers_.find( | |
121 changes->policy_namespace_.first); | |
95 if (iterator != observers_.end()) { | 122 if (iterator != observers_.end()) { |
96 FOR_EACH_OBSERVER( | 123 FOR_EACH_OBSERVER( |
97 PolicyService::Observer, | 124 PolicyService::Observer, |
98 *iterator->second, | 125 *iterator->second, |
99 OnPolicyUpdated(ns.first, ns.second, previous, current)); | 126 OnPolicyUpdated(changes->policy_namespace_.first, |
127 changes->policy_namespace_.second, | |
128 changes->previous_, | |
129 changes->current_)); | |
100 } | 130 } |
101 } | 131 } |
102 | 132 |
133 PolicyServiceImpl::PolicyChangeInfo::PolicyChangeInfo( | |
Joao da Silva
2013/01/07 09:23:52
Place the definitions for PolicyChangeInfo before
Andrew T Wilson (Slow)
2013/01/07 14:11:32
Done.
| |
134 const PolicyBundle::PolicyNamespace& policy_namespace, | |
135 const PolicyMap& previous, const PolicyMap& current) | |
136 : policy_namespace_(policy_namespace) { | |
137 previous_.CopyFrom(previous); | |
138 current_.CopyFrom(current); | |
139 } | |
140 | |
141 PolicyServiceImpl::PolicyChangeInfo::~PolicyChangeInfo() { | |
142 } | |
143 | |
103 void PolicyServiceImpl::MergeAndTriggerUpdates() { | 144 void PolicyServiceImpl::MergeAndTriggerUpdates() { |
104 // Merge from each provider in their order of priority. | 145 // Merge from each provider in their order of priority. |
105 PolicyBundle bundle; | 146 PolicyBundle bundle; |
106 for (Iterator it = providers_.begin(); it != providers_.end(); ++it) | 147 for (Iterator it = providers_.begin(); it != providers_.end(); ++it) |
107 bundle.MergeFrom((*it)->policies()); | 148 bundle.MergeFrom((*it)->policies()); |
108 | 149 |
109 // Swap first, so that observers that call GetPolicies() see the current | 150 // Swap first, so that observers that call GetPolicies() see the current |
110 // values. | 151 // values. |
111 policy_bundle_.Swap(&bundle); | 152 policy_bundle_.Swap(&bundle); |
112 | 153 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 if (refresh_pending_.empty() && !refresh_callbacks_.empty()) { | 214 if (refresh_pending_.empty() && !refresh_callbacks_.empty()) { |
174 std::vector<base::Closure> callbacks; | 215 std::vector<base::Closure> callbacks; |
175 callbacks.swap(refresh_callbacks_); | 216 callbacks.swap(refresh_callbacks_); |
176 std::vector<base::Closure>::iterator it; | 217 std::vector<base::Closure>::iterator it; |
177 for (it = callbacks.begin(); it != callbacks.end(); ++it) | 218 for (it = callbacks.begin(); it != callbacks.end(); ++it) |
178 it->Run(); | 219 it->Run(); |
179 } | 220 } |
180 } | 221 } |
181 | 222 |
182 } // namespace policy | 223 } // namespace policy |
OLD | NEW |