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/chromeos/policy/device_local_account_policy_provider.h" | 5 #include "chrome/browser/chromeos/policy/device_local_account_policy_provider.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | |
9 #include "base/values.h" | 8 #include "base/values.h" |
10 #include "chrome/browser/chromeos/policy/device_local_account.h" | 9 #include "chrome/browser/chromeos/policy/device_local_account.h" |
11 #include "chrome/browser/chromeos/policy/device_local_account_external_data_mana
ger.h" | 10 #include "chrome/browser/chromeos/policy/device_local_account_external_data_mana
ger.h" |
12 #include "chromeos/dbus/power_policy_controller.h" | 11 #include "chromeos/dbus/power_policy_controller.h" |
13 #include "components/policy/core/common/cloud/cloud_policy_core.h" | 12 #include "components/policy/core/common/cloud/cloud_policy_core.h" |
14 #include "components/policy/core/common/cloud/cloud_policy_service.h" | 13 #include "components/policy/core/common/cloud/cloud_policy_service.h" |
| 14 #include "components/policy/core/common/cloud/component_cloud_policy_service.h" |
15 #include "components/policy/core/common/policy_bundle.h" | 15 #include "components/policy/core/common/policy_bundle.h" |
16 #include "components/policy/core/common/policy_map.h" | 16 #include "components/policy/core/common/policy_map.h" |
17 #include "components/policy/core/common/policy_namespace.h" | 17 #include "components/policy/core/common/policy_namespace.h" |
18 #include "components/policy/core/common/policy_switches.h" | |
19 #include "content/public/browser/browser_thread.h" | |
20 #include "net/url_request/url_request_context_getter.h" | |
21 #include "policy/policy_constants.h" | 18 #include "policy/policy_constants.h" |
22 | 19 |
23 namespace policy { | 20 namespace policy { |
24 | 21 |
25 DeviceLocalAccountPolicyProvider::DeviceLocalAccountPolicyProvider( | 22 DeviceLocalAccountPolicyProvider::DeviceLocalAccountPolicyProvider( |
26 const std::string& user_id, | 23 const std::string& user_id, |
27 DeviceLocalAccountPolicyService* service, | 24 DeviceLocalAccountPolicyService* service, |
28 scoped_ptr<PolicyMap> chrome_policy_overrides) | 25 scoped_ptr<PolicyMap> chrome_policy_overrides) |
29 : user_id_(user_id), | 26 : user_id_(user_id), |
30 service_(service), | 27 service_(service), |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 NULL); | 88 NULL); |
92 } | 89 } |
93 | 90 |
94 scoped_ptr<DeviceLocalAccountPolicyProvider> provider( | 91 scoped_ptr<DeviceLocalAccountPolicyProvider> provider( |
95 new DeviceLocalAccountPolicyProvider(user_id, | 92 new DeviceLocalAccountPolicyProvider(user_id, |
96 device_local_account_policy_service, | 93 device_local_account_policy_service, |
97 chrome_policy_overrides.Pass())); | 94 chrome_policy_overrides.Pass())); |
98 return provider.Pass(); | 95 return provider.Pass(); |
99 } | 96 } |
100 | 97 |
101 void DeviceLocalAccountPolicyProvider::Init(SchemaRegistry* schema_registry) { | |
102 ConfigurationPolicyProvider::Init(schema_registry); | |
103 MaybeCreateComponentPolicyService(); | |
104 } | |
105 | |
106 bool DeviceLocalAccountPolicyProvider::IsInitializationComplete( | 98 bool DeviceLocalAccountPolicyProvider::IsInitializationComplete( |
107 PolicyDomain domain) const { | 99 PolicyDomain domain) const { |
108 if (domain == POLICY_DOMAIN_CHROME) | 100 if (domain == POLICY_DOMAIN_CHROME) |
109 return store_initialized_; | 101 return store_initialized_; |
110 if (ComponentCloudPolicyService::SupportsDomain(domain) && | 102 if (ComponentCloudPolicyService::SupportsDomain(domain) && |
111 component_policy_service_) { | 103 GetBroker() && GetBroker()->component_policy_service()) { |
112 return component_policy_service_->is_initialized(); | 104 return GetBroker()->component_policy_service()->is_initialized(); |
113 } | 105 } |
114 return true; | 106 return true; |
115 } | 107 } |
116 | 108 |
117 void DeviceLocalAccountPolicyProvider::RefreshPolicies() { | 109 void DeviceLocalAccountPolicyProvider::RefreshPolicies() { |
118 DeviceLocalAccountPolicyBroker* broker = GetBroker(); | 110 DeviceLocalAccountPolicyBroker* broker = GetBroker(); |
119 if (broker && broker->core()->service()) { | 111 if (broker && broker->core()->service()) { |
120 waiting_for_policy_refresh_ = true; | 112 waiting_for_policy_refresh_ = true; |
121 broker->core()->service()->RefreshPolicy( | 113 broker->core()->service()->RefreshPolicy( |
122 base::Bind(&DeviceLocalAccountPolicyProvider::ReportPolicyRefresh, | 114 base::Bind(&DeviceLocalAccountPolicyProvider::ReportPolicyRefresh, |
123 weak_factory_.GetWeakPtr())); | 115 weak_factory_.GetWeakPtr())); |
124 } else { | 116 } else { |
125 UpdateFromBroker(); | 117 UpdateFromBroker(); |
126 } | 118 } |
127 } | 119 } |
128 | 120 |
129 void DeviceLocalAccountPolicyProvider::Shutdown() { | |
130 component_policy_service_.reset(); | |
131 ConfigurationPolicyProvider::Shutdown(); | |
132 } | |
133 | |
134 void DeviceLocalAccountPolicyProvider::OnPolicyUpdated( | 121 void DeviceLocalAccountPolicyProvider::OnPolicyUpdated( |
135 const std::string& user_id) { | 122 const std::string& user_id) { |
136 if (user_id == user_id_) { | 123 if (user_id == user_id_) |
137 MaybeCreateComponentPolicyService(); | |
138 UpdateFromBroker(); | 124 UpdateFromBroker(); |
139 } | |
140 } | 125 } |
141 | 126 |
142 void DeviceLocalAccountPolicyProvider::OnDeviceLocalAccountsChanged() { | 127 void DeviceLocalAccountPolicyProvider::OnDeviceLocalAccountsChanged() { |
143 MaybeCreateComponentPolicyService(); | |
144 UpdateFromBroker(); | 128 UpdateFromBroker(); |
145 } | 129 } |
146 | 130 |
147 void DeviceLocalAccountPolicyProvider::OnBrokerShutdown( | 131 DeviceLocalAccountPolicyBroker* DeviceLocalAccountPolicyProvider::GetBroker() |
148 DeviceLocalAccountPolicyBroker* broker) { | 132 const { |
149 if (broker->user_id() == user_id_) { | |
150 // The |component_policy_service_| relies on the broker's CloudPolicyCore, | |
151 // so destroy it if the broker is going away. | |
152 component_policy_service_.reset(); | |
153 } | |
154 } | |
155 | |
156 void DeviceLocalAccountPolicyProvider::OnComponentCloudPolicyUpdated() { | |
157 UpdateFromBroker(); | |
158 } | |
159 | |
160 DeviceLocalAccountPolicyBroker* DeviceLocalAccountPolicyProvider::GetBroker() { | |
161 return service_->GetBrokerForUser(user_id_); | 133 return service_->GetBrokerForUser(user_id_); |
162 } | 134 } |
163 | 135 |
164 void DeviceLocalAccountPolicyProvider::ReportPolicyRefresh(bool success) { | 136 void DeviceLocalAccountPolicyProvider::ReportPolicyRefresh(bool success) { |
165 waiting_for_policy_refresh_ = false; | 137 waiting_for_policy_refresh_ = false; |
166 UpdateFromBroker(); | 138 UpdateFromBroker(); |
167 } | 139 } |
168 | 140 |
169 void DeviceLocalAccountPolicyProvider::UpdateFromBroker() { | 141 void DeviceLocalAccountPolicyProvider::UpdateFromBroker() { |
170 DeviceLocalAccountPolicyBroker* broker = GetBroker(); | 142 DeviceLocalAccountPolicyBroker* broker = GetBroker(); |
171 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); | 143 scoped_ptr<PolicyBundle> bundle(new PolicyBundle()); |
172 if (broker) { | 144 if (broker) { |
173 store_initialized_ |= broker->core()->store()->is_initialized(); | 145 store_initialized_ |= broker->core()->store()->is_initialized(); |
174 if (!waiting_for_policy_refresh_) { | 146 if (!waiting_for_policy_refresh_) { |
175 // Copy policy from the broker. | 147 // Copy policy from the broker. |
176 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) | 148 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())) |
177 .CopyFrom(broker->core()->store()->policy_map()); | 149 .CopyFrom(broker->core()->store()->policy_map()); |
178 external_data_manager_ = broker->external_data_manager(); | 150 external_data_manager_ = broker->external_data_manager(); |
| 151 |
| 152 if (broker->component_policy_service()) |
| 153 bundle->MergeFrom(broker->component_policy_service()->policy()); |
179 } else { | 154 } else { |
180 // Wait for the refresh to finish. | 155 // Wait for the refresh to finish. |
181 return; | 156 return; |
182 } | 157 } |
183 } else { | 158 } else { |
184 // Keep existing policy, but do send an update. | 159 // Keep existing policy, but do send an update. |
185 waiting_for_policy_refresh_ = false; | 160 waiting_for_policy_refresh_ = false; |
186 weak_factory_.InvalidateWeakPtrs(); | 161 weak_factory_.InvalidateWeakPtrs(); |
187 bundle->CopyFrom(policies()); | 162 bundle->CopyFrom(policies()); |
188 } | 163 } |
189 | 164 |
190 if (component_policy_service_) | |
191 bundle->MergeFrom(component_policy_service_->policy()); | |
192 | |
193 // Apply overrides. | 165 // Apply overrides. |
194 if (chrome_policy_overrides_) { | 166 if (chrome_policy_overrides_) { |
195 PolicyMap& chrome_policy = | 167 PolicyMap& chrome_policy = |
196 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); | 168 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())); |
197 for (PolicyMap::const_iterator it(chrome_policy_overrides_->begin()); | 169 for (PolicyMap::const_iterator it(chrome_policy_overrides_->begin()); |
198 it != chrome_policy_overrides_->end(); | 170 it != chrome_policy_overrides_->end(); |
199 ++it) { | 171 ++it) { |
200 const PolicyMap::Entry& entry = it->second; | 172 const PolicyMap::Entry& entry = it->second; |
201 chrome_policy.Set( | 173 chrome_policy.Set( |
202 it->first, entry.level, entry.scope, entry.value->DeepCopy(), NULL); | 174 it->first, entry.level, entry.scope, entry.value->DeepCopy(), NULL); |
203 } | 175 } |
204 } | 176 } |
205 | 177 |
206 UpdatePolicy(bundle.Pass()); | 178 UpdatePolicy(bundle.Pass()); |
207 } | 179 } |
208 | 180 |
209 void DeviceLocalAccountPolicyProvider::MaybeCreateComponentPolicyService() { | |
210 if (component_policy_service_) | |
211 return; // Already started. | |
212 | |
213 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
214 switches::kDisableComponentCloudPolicy)) { | |
215 // Disabled via the command line. | |
216 return; | |
217 } | |
218 | |
219 DeviceLocalAccountPolicyBroker* broker = GetBroker(); | |
220 if (!broker || !schema_registry()) | |
221 return; // Missing broker or not initialized yet. | |
222 | |
223 scoped_ptr<ResourceCache> resource_cache( | |
224 new ResourceCache(broker->GetComponentPolicyCachePath(), | |
225 content::BrowserThread::GetMessageLoopProxyForThread( | |
226 content::BrowserThread::FILE))); | |
227 | |
228 component_policy_service_.reset(new ComponentCloudPolicyService( | |
229 this, | |
230 schema_registry(), | |
231 broker->core(), | |
232 resource_cache.Pass(), | |
233 service_->request_context(), | |
234 content::BrowserThread::GetMessageLoopProxyForThread( | |
235 content::BrowserThread::FILE), | |
236 content::BrowserThread::GetMessageLoopProxyForThread( | |
237 content::BrowserThread::IO))); | |
238 } | |
239 | |
240 } // namespace policy | 181 } // namespace policy |
OLD | NEW |