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_service.h" | 5 #include "chrome/browser/chromeos/policy/device_local_account_policy_service.h" |
6 | 6 |
7 #include <vector> | |
8 | |
9 #include "base/bind.h" | |
7 #include "base/logging.h" | 10 #include "base/logging.h" |
8 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "chrome/browser/chromeos/policy/device_local_account.h" | |
9 #include "chrome/browser/chromeos/policy/device_local_account_policy_store.h" | 13 #include "chrome/browser/chromeos/policy/device_local_account_policy_store.h" |
14 #include "chrome/browser/chromeos/settings/cros_settings.h" | |
15 #include "chrome/browser/chromeos/settings/cros_settings_names.h" | |
16 #include "chrome/browser/chromeos/settings/cros_settings_provider.h" | |
17 #include "chrome/browser/chromeos/settings/device_settings_service.h" | |
10 #include "chrome/browser/policy/cloud/cloud_policy_client.h" | 18 #include "chrome/browser/policy/cloud/cloud_policy_client.h" |
11 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 19 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
12 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h" | 20 #include "chrome/browser/policy/cloud/cloud_policy_refresh_scheduler.h" |
13 #include "chrome/browser/policy/cloud/device_management_service.h" | 21 #include "chrome/browser/policy/cloud/device_management_service.h" |
14 #include "chrome/browser/policy/proto/chromeos/chrome_device_policy.pb.h" | |
15 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" | 22 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" |
23 #include "chrome/common/chrome_notification_types.h" | |
16 #include "chromeos/dbus/session_manager_client.h" | 24 #include "chromeos/dbus/session_manager_client.h" |
25 #include "content/public/browser/notification_details.h" | |
17 #include "policy/policy_constants.h" | 26 #include "policy/policy_constants.h" |
18 | 27 |
19 namespace em = enterprise_management; | 28 namespace em = enterprise_management; |
20 | 29 |
21 namespace policy { | 30 namespace policy { |
22 | 31 |
23 DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker( | 32 DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker( |
33 const std::string& user_id, | |
24 scoped_ptr<DeviceLocalAccountPolicyStore> store) | 34 scoped_ptr<DeviceLocalAccountPolicyStore> store) |
25 : store_(store.Pass()), | 35 : user_id_(user_id), |
36 store_(store.Pass()), | |
26 core_(PolicyNamespaceKey(dm_protocol::kChromePublicAccountPolicyType, | 37 core_(PolicyNamespaceKey(dm_protocol::kChromePublicAccountPolicyType, |
27 store_->account_id()), | 38 store_->account_id()), |
28 store_.get()) {} | 39 store_.get()) {} |
29 | 40 |
30 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() {} | 41 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() {} |
31 | 42 |
32 const std::string& DeviceLocalAccountPolicyBroker::account_id() const { | |
33 return store_->account_id(); | |
34 } | |
35 | |
36 void DeviceLocalAccountPolicyBroker::Connect( | 43 void DeviceLocalAccountPolicyBroker::Connect( |
37 scoped_ptr<CloudPolicyClient> client) { | 44 scoped_ptr<CloudPolicyClient> client) { |
38 core_.Connect(client.Pass()); | 45 core_.Connect(client.Pass()); |
39 core_.StartRefreshScheduler(); | 46 core_.StartRefreshScheduler(); |
40 UpdateRefreshDelay(); | 47 UpdateRefreshDelay(); |
41 } | 48 } |
42 | 49 |
43 void DeviceLocalAccountPolicyBroker::Disconnect() { | 50 void DeviceLocalAccountPolicyBroker::Disconnect() { |
44 core_.Disconnect(); | 51 core_.Disconnect(); |
45 } | 52 } |
(...skipping 10 matching lines...) Expand all Loading... | |
56 | 63 |
57 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const { | 64 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const { |
58 std::string display_name; | 65 std::string display_name; |
59 const base::Value* display_name_value = | 66 const base::Value* display_name_value = |
60 store_->policy_map().GetValue(policy::key::kUserDisplayName); | 67 store_->policy_map().GetValue(policy::key::kUserDisplayName); |
61 if (display_name_value) | 68 if (display_name_value) |
62 display_name_value->GetAsString(&display_name); | 69 display_name_value->GetAsString(&display_name); |
63 return display_name; | 70 return display_name; |
64 } | 71 } |
65 | 72 |
73 DeviceLocalAccountPolicyService::PolicyBrokerWrapper::PolicyBrokerWrapper() | |
74 : broker(NULL) { | |
75 } | |
76 | |
66 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService( | 77 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService( |
67 chromeos::SessionManagerClient* session_manager_client, | 78 chromeos::SessionManagerClient* session_manager_client, |
68 chromeos::DeviceSettingsService* device_settings_service) | 79 chromeos::DeviceSettingsService* device_settings_service, |
80 chromeos::CrosSettings* cros_settings) | |
69 : session_manager_client_(session_manager_client), | 81 : session_manager_client_(session_manager_client), |
70 device_settings_service_(device_settings_service), | 82 device_settings_service_(device_settings_service), |
71 device_management_service_(NULL) { | 83 cros_settings_(cros_settings), |
72 device_settings_service_->AddObserver(this); | 84 device_management_service_(NULL), |
73 DeviceSettingsUpdated(); | 85 cros_settings_callback_factory_(this) { |
86 cros_settings_->AddSettingsObserver( | |
87 chromeos::kAccountsPrefDeviceLocalAccounts, this); | |
88 UpdateAccountList(); | |
74 } | 89 } |
75 | 90 |
76 DeviceLocalAccountPolicyService::~DeviceLocalAccountPolicyService() { | 91 DeviceLocalAccountPolicyService::~DeviceLocalAccountPolicyService() { |
77 device_settings_service_->RemoveObserver(this); | 92 cros_settings_->RemoveSettingsObserver( |
93 chromeos::kAccountsPrefDeviceLocalAccounts, this); | |
78 DeleteBrokers(&policy_brokers_); | 94 DeleteBrokers(&policy_brokers_); |
79 } | 95 } |
80 | 96 |
81 void DeviceLocalAccountPolicyService::Connect( | 97 void DeviceLocalAccountPolicyService::Connect( |
82 DeviceManagementService* device_management_service) { | 98 DeviceManagementService* device_management_service) { |
83 DCHECK(!device_management_service_); | 99 DCHECK(!device_management_service_); |
84 device_management_service_ = device_management_service; | 100 device_management_service_ = device_management_service; |
85 | 101 |
86 // Connect the brokers. | 102 // Connect the brokers. |
87 for (PolicyBrokerMap::iterator broker(policy_brokers_.begin()); | 103 for (PolicyBrokerMap::iterator it(policy_brokers_.begin()); |
88 broker != policy_brokers_.end(); ++broker) { | 104 it != policy_brokers_.end(); ++it) { |
89 DCHECK(!broker->second->core()->client()); | 105 if (!it->second.broker) |
Mattias Nissler (ping if slow)
2013/05/15 09:38:47
I think this is inconsistent with UpdateAccountLis
bartfab (slow)
2013/05/17 11:14:28
Done, although I am not sure adding the helper met
| |
90 broker->second->Connect( | 106 continue; |
91 CreateClientForAccount(broker->second->account_id()).Pass()); | 107 DCHECK(!it->second.broker->core()->client()); |
108 it->second.broker->Connect(CreateClient().Pass()); | |
92 } | 109 } |
93 } | 110 } |
94 | 111 |
95 void DeviceLocalAccountPolicyService::Disconnect() { | 112 void DeviceLocalAccountPolicyService::Disconnect() { |
96 DCHECK(device_management_service_); | 113 DCHECK(device_management_service_); |
97 device_management_service_ = NULL; | 114 device_management_service_ = NULL; |
98 | 115 |
99 // Disconnect the brokers. | 116 // Disconnect the brokers. |
100 for (PolicyBrokerMap::iterator broker(policy_brokers_.begin()); | 117 for (PolicyBrokerMap::iterator it(policy_brokers_.begin()); |
101 broker != policy_brokers_.end(); ++broker) { | 118 it != policy_brokers_.end(); ++it) { |
102 broker->second->Disconnect(); | 119 if (!it->second.broker) |
120 continue; | |
121 it->second.broker->Disconnect(); | |
Mattias Nissler (ping if slow)
2013/05/15 09:38:47
Maybe just make BrokerWrapper::Disconnect() functi
bartfab (slow)
2013/05/17 11:14:28
Done.
| |
103 } | 122 } |
104 } | 123 } |
105 | 124 |
106 DeviceLocalAccountPolicyBroker* | 125 DeviceLocalAccountPolicyBroker* |
107 DeviceLocalAccountPolicyService::GetBrokerForAccount( | 126 DeviceLocalAccountPolicyService::GetBrokerForUser( |
108 const std::string& account_id) { | 127 const std::string& user_id) { |
109 PolicyBrokerMap::iterator entry = policy_brokers_.find(account_id); | 128 PolicyBrokerMap::iterator entry = policy_brokers_.find(user_id); |
110 if (entry == policy_brokers_.end()) | 129 if (entry == policy_brokers_.end()) |
111 return NULL; | 130 return NULL; |
112 | 131 |
113 if (!entry->second) | 132 if (!entry->second.broker) { |
Mattias Nissler (ping if slow)
2013/05/15 09:38:47
Could just move this to a BrokerWrapper::GetBroker
bartfab (slow)
2013/05/17 11:14:28
Done.
| |
114 entry->second = CreateBroker(account_id).release(); | 133 entry->second.broker = |
134 CreateBroker(user_id, entry->second.account_id).release(); | |
135 } | |
115 | 136 |
116 return entry->second; | 137 return entry->second.broker; |
117 } | 138 } |
118 | 139 |
119 bool DeviceLocalAccountPolicyService::IsPolicyAvailableForAccount( | 140 bool DeviceLocalAccountPolicyService::IsPolicyAvailableForUser( |
120 const std::string& account_id) { | 141 const std::string& user_id) { |
121 DeviceLocalAccountPolicyBroker* broker = GetBrokerForAccount(account_id); | 142 DeviceLocalAccountPolicyBroker* broker = GetBrokerForUser(user_id); |
122 return broker && broker->core()->store()->is_managed(); | 143 return broker && broker->core()->store()->is_managed(); |
123 } | 144 } |
124 | 145 |
125 void DeviceLocalAccountPolicyService::AddObserver(Observer* observer) { | 146 void DeviceLocalAccountPolicyService::AddObserver(Observer* observer) { |
126 observers_.AddObserver(observer); | 147 observers_.AddObserver(observer); |
127 } | 148 } |
128 | 149 |
129 void DeviceLocalAccountPolicyService::RemoveObserver(Observer* observer) { | 150 void DeviceLocalAccountPolicyService::RemoveObserver(Observer* observer) { |
130 observers_.RemoveObserver(observer); | 151 observers_.RemoveObserver(observer); |
131 } | 152 } |
132 | 153 |
133 void DeviceLocalAccountPolicyService::OwnershipStatusChanged() { | 154 void DeviceLocalAccountPolicyService::Observe( |
134 // TODO(mnissler): The policy key has changed, re-fetch policy. For | 155 int type, |
135 // consumer devices, re-sign the current settings and send updates to | 156 const content::NotificationSource& source, |
136 // session_manager. | 157 const content::NotificationDetails& details) { |
137 } | 158 if (type != chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED || |
159 *content::Details<const std::string>(details).ptr() != | |
160 chromeos::kAccountsPrefDeviceLocalAccounts) { | |
161 NOTREACHED(); | |
162 return; | |
163 } | |
138 | 164 |
139 void DeviceLocalAccountPolicyService::DeviceSettingsUpdated() { | 165 // Avoid unnecessary calls to UpdateAccountList(): If an earlier call is still |
140 const em::ChromeDeviceSettingsProto* device_settings = | 166 // pending (because the |cros_settings_| are not trusted yet), the updated |
141 device_settings_service_->device_settings(); | 167 // account list will be processed by that call when it eventually runs. |
142 if (device_settings) | 168 if (!cros_settings_callback_factory_.HasWeakPtrs()) |
143 UpdateAccountList(*device_settings); | 169 UpdateAccountList(); |
144 } | 170 } |
145 | 171 |
146 void DeviceLocalAccountPolicyService::OnStoreLoaded(CloudPolicyStore* store) { | 172 void DeviceLocalAccountPolicyService::OnStoreLoaded(CloudPolicyStore* store) { |
147 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store); | 173 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store); |
174 DCHECK(broker); | |
175 if (!broker) | |
176 return; | |
148 broker->UpdateRefreshDelay(); | 177 broker->UpdateRefreshDelay(); |
149 FOR_EACH_OBSERVER(Observer, observers_, | 178 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyUpdated(broker->user_id())); |
150 OnPolicyUpdated(broker->account_id())); | |
151 } | 179 } |
152 | 180 |
153 void DeviceLocalAccountPolicyService::OnStoreError(CloudPolicyStore* store) { | 181 void DeviceLocalAccountPolicyService::OnStoreError(CloudPolicyStore* store) { |
154 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store); | 182 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store); |
155 FOR_EACH_OBSERVER(Observer, observers_, | 183 DCHECK(broker); |
156 OnPolicyUpdated(broker->account_id())); | 184 if (!broker) |
185 return; | |
186 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyUpdated(broker->user_id())); | |
157 } | 187 } |
158 | 188 |
159 void DeviceLocalAccountPolicyService::UpdateAccountList( | 189 void DeviceLocalAccountPolicyService::UpdateAccountList() { |
160 const em::ChromeDeviceSettingsProto& device_settings) { | 190 if (chromeos::CrosSettingsProvider::TRUSTED != |
161 using google::protobuf::RepeatedPtrField; | 191 cros_settings_->PrepareTrustedValues( |
192 base::Bind(&DeviceLocalAccountPolicyService::UpdateAccountList, | |
193 cros_settings_callback_factory_.GetWeakPtr()))) { | |
194 return; | |
195 } | |
162 | 196 |
163 // Update |policy_brokers_|, keeping existing entries. | 197 // Update |policy_brokers_|, keeping existing entries. |
164 PolicyBrokerMap new_policy_brokers; | 198 PolicyBrokerMap new_policy_brokers; |
165 const RepeatedPtrField<em::DeviceLocalAccountInfoProto>& accounts = | 199 const base::ListValue* device_local_account_list; |
166 device_settings.device_local_accounts().account(); | 200 cros_settings_->GetList(chromeos::kAccountsPrefDeviceLocalAccounts, |
167 RepeatedPtrField<em::DeviceLocalAccountInfoProto>::const_iterator entry; | 201 &device_local_account_list); |
168 for (entry = accounts.begin(); entry != accounts.end(); ++entry) { | 202 const std::vector<DeviceLocalAccount> device_local_accounts = |
169 std::string account_id; | 203 DecodeDeviceLocalAccountsList(device_local_account_list); |
170 if (entry->has_type() && | 204 for (std::vector<DeviceLocalAccount>::const_iterator it = |
171 entry->type() == | 205 device_local_accounts.begin(); |
172 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION) { | 206 it != device_local_accounts.end(); ++it) { |
173 account_id = entry->account_id(); | 207 PolicyBrokerWrapper& wrapper = new_policy_brokers[it->user_id]; |
174 } else if (entry->has_deprecated_public_session_id()) { | 208 wrapper.account_id = it->account_id; |
175 account_id = entry->deprecated_public_session_id(); | |
176 } | |
177 | |
178 if (account_id.empty()) | |
179 continue; | |
180 | |
181 // Sanity check for whether this account ID has already been processed. | |
182 DeviceLocalAccountPolicyBroker*& new_broker = | |
183 new_policy_brokers[account_id]; | |
184 if (new_broker) { | |
185 LOG(WARNING) << "Duplicate public account " << account_id; | |
186 continue; | |
187 } | |
188 | 209 |
189 // Reuse the existing broker if present. | 210 // Reuse the existing broker if present. |
190 DeviceLocalAccountPolicyBroker*& existing_broker = | 211 PolicyBrokerWrapper& existing_wrapper = policy_brokers_[it->user_id]; |
191 policy_brokers_[account_id]; | 212 wrapper.broker = existing_wrapper.broker; |
192 new_broker = existing_broker; | 213 existing_wrapper.broker = NULL; |
193 existing_broker = NULL; | |
194 | 214 |
195 // Fire up the cloud connection for fetching policy for the account from | 215 // Fire up the cloud connection for fetching policy for the account from |
196 // the cloud if this is an enterprise-managed device. | 216 // the cloud if this is an enterprise-managed device. |
197 if (!new_broker || !new_broker->core()->client()) { | 217 if (!wrapper.broker || !wrapper.broker->core()->client()) { |
198 scoped_ptr<CloudPolicyClient> client( | 218 scoped_ptr<CloudPolicyClient> client(CreateClient()); |
199 CreateClientForAccount(account_id)); | 219 if (client) { |
200 if (client.get()) { | 220 if (!wrapper.broker) |
201 if (!new_broker) | 221 wrapper.broker = CreateBroker(it->user_id, it->account_id).release(); |
202 new_broker = CreateBroker(account_id).release(); | 222 wrapper.broker->Connect(client.Pass()); |
203 new_broker->Connect(client.Pass()); | |
204 } | 223 } |
205 } | 224 } |
206 } | 225 } |
207 policy_brokers_.swap(new_policy_brokers); | 226 policy_brokers_.swap(new_policy_brokers); |
208 DeleteBrokers(&new_policy_brokers); | 227 DeleteBrokers(&new_policy_brokers); |
209 | 228 |
210 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceLocalAccountsChanged()); | 229 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceLocalAccountsChanged()); |
211 } | 230 } |
212 | 231 |
213 scoped_ptr<DeviceLocalAccountPolicyBroker> | 232 scoped_ptr<DeviceLocalAccountPolicyBroker> |
214 DeviceLocalAccountPolicyService::CreateBroker( | 233 DeviceLocalAccountPolicyService::CreateBroker( |
234 const std::string& user_id, | |
215 const std::string& account_id) { | 235 const std::string& account_id) { |
216 scoped_ptr<DeviceLocalAccountPolicyStore> store( | 236 scoped_ptr<DeviceLocalAccountPolicyStore> store( |
217 new DeviceLocalAccountPolicyStore(account_id, session_manager_client_, | 237 new DeviceLocalAccountPolicyStore(account_id, session_manager_client_, |
218 device_settings_service_)); | 238 device_settings_service_)); |
219 scoped_ptr<DeviceLocalAccountPolicyBroker> broker( | 239 scoped_ptr<DeviceLocalAccountPolicyBroker> broker( |
220 new DeviceLocalAccountPolicyBroker(store.Pass())); | 240 new DeviceLocalAccountPolicyBroker(user_id, store.Pass())); |
221 broker->core()->store()->AddObserver(this); | 241 broker->core()->store()->AddObserver(this); |
222 broker->core()->store()->Load(); | 242 broker->core()->store()->Load(); |
223 return broker.Pass(); | 243 return broker.Pass(); |
224 } | 244 } |
225 | 245 |
226 void DeviceLocalAccountPolicyService::DeleteBrokers(PolicyBrokerMap* map) { | 246 void DeviceLocalAccountPolicyService::DeleteBrokers(PolicyBrokerMap* map) { |
227 for (PolicyBrokerMap::iterator broker = map->begin(); broker != map->end(); | 247 for (PolicyBrokerMap::iterator it = map->begin(); it != map->end(); ++it) { |
228 ++broker) { | 248 if (it->second.broker) { |
229 if (broker->second) { | 249 it->second.broker->core()->store()->RemoveObserver(this); |
230 broker->second->core()->store()->RemoveObserver(this); | 250 delete it->second.broker; |
231 delete broker->second; | |
232 } | 251 } |
233 } | 252 } |
234 map->clear(); | 253 map->clear(); |
235 } | 254 } |
236 | 255 |
237 DeviceLocalAccountPolicyBroker* | 256 DeviceLocalAccountPolicyBroker* |
238 DeviceLocalAccountPolicyService::GetBrokerForStore( | 257 DeviceLocalAccountPolicyService::GetBrokerForStore( |
239 CloudPolicyStore* store) { | 258 CloudPolicyStore* store) { |
240 for (PolicyBrokerMap::iterator broker(policy_brokers_.begin()); | 259 for (PolicyBrokerMap::iterator it(policy_brokers_.begin()); |
241 broker != policy_brokers_.end(); ++broker) { | 260 it != policy_brokers_.end(); ++it) { |
242 if (broker->second->core()->store() == store) | 261 if (it->second.broker && it->second.broker->core()->store() == store) |
243 return broker->second; | 262 return it->second.broker; |
244 } | 263 } |
245 return NULL; | 264 return NULL; |
246 } | 265 } |
247 | 266 |
248 scoped_ptr<CloudPolicyClient> | 267 scoped_ptr<CloudPolicyClient> DeviceLocalAccountPolicyService::CreateClient() { |
249 DeviceLocalAccountPolicyService::CreateClientForAccount( | |
250 const std::string& account_id) { | |
251 const em::PolicyData* policy_data = device_settings_service_->policy_data(); | 268 const em::PolicyData* policy_data = device_settings_service_->policy_data(); |
252 if (!policy_data || | 269 if (!policy_data || |
253 !policy_data->has_request_token() || | 270 !policy_data->has_request_token() || |
254 !policy_data->has_device_id() || | 271 !policy_data->has_device_id() || |
255 !device_management_service_) { | 272 !device_management_service_) { |
256 return scoped_ptr<CloudPolicyClient>(); | 273 return scoped_ptr<CloudPolicyClient>(); |
257 } | 274 } |
258 | 275 |
259 scoped_ptr<CloudPolicyClient> client( | 276 scoped_ptr<CloudPolicyClient> client( |
260 new CloudPolicyClient(std::string(), std::string(), | 277 new CloudPolicyClient(std::string(), std::string(), |
261 USER_AFFILIATION_MANAGED, | 278 USER_AFFILIATION_MANAGED, |
262 NULL, device_management_service_)); | 279 NULL, device_management_service_)); |
263 client->SetupRegistration(policy_data->request_token(), | 280 client->SetupRegistration(policy_data->request_token(), |
264 policy_data->device_id()); | 281 policy_data->device_id()); |
265 return client.Pass(); | 282 return client.Pass(); |
266 } | 283 } |
267 | 284 |
268 } // namespace policy | 285 } // namespace policy |
OLD | NEW |