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

Side by Side Diff: chrome/browser/chromeos/policy/device_local_account_policy_service.cc

Issue 14927015: Translate device-local account IDs to user IDs (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Comments addressed. Kiosk apps fixed. Created 7 years, 7 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 | Annotate | Revision Log
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 "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
32 namespace {
33
34 // Creates a broker for the device-local account with the given |user_id| and
35 // |account_id|.
36 scoped_ptr<DeviceLocalAccountPolicyBroker> CreateBroker(
37 const std::string& user_id,
38 const std::string& account_id,
39 chromeos::SessionManagerClient* session_manager_client,
40 chromeos::DeviceSettingsService* device_settings_service,
41 DeviceLocalAccountPolicyService* device_local_account_policy_service) {
42 scoped_ptr<DeviceLocalAccountPolicyStore> store(
43 new DeviceLocalAccountPolicyStore(account_id, session_manager_client,
44 device_settings_service));
45 scoped_ptr<DeviceLocalAccountPolicyBroker> broker(
46 new DeviceLocalAccountPolicyBroker(user_id, store.Pass()));
47 broker->core()->store()->AddObserver(device_local_account_policy_service);
48 broker->core()->store()->Load();
49 return broker.Pass();
50 }
51
52 // Creates and initializes a cloud policy client. Returns NULL if the device
53 // doesn't have credentials in device settings (i.e. is not
54 // enterprise-enrolled).
55 scoped_ptr<CloudPolicyClient> CreateClient(
56 chromeos::DeviceSettingsService* device_settings_service,
57 DeviceManagementService* device_management_service) {
58 const em::PolicyData* policy_data = device_settings_service->policy_data();
59 if (!policy_data ||
60 !policy_data->has_request_token() ||
61 !policy_data->has_device_id() ||
62 !device_management_service) {
63 return scoped_ptr<CloudPolicyClient>();
64 }
65
66 scoped_ptr<CloudPolicyClient> client(
67 new CloudPolicyClient(std::string(), std::string(),
68 USER_AFFILIATION_MANAGED,
69 NULL, device_management_service));
70 client->SetupRegistration(policy_data->request_token(),
71 policy_data->device_id());
72 return client.Pass();
73 }
74
75 } // namespace
76
23 DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker( 77 DeviceLocalAccountPolicyBroker::DeviceLocalAccountPolicyBroker(
78 const std::string& user_id,
24 scoped_ptr<DeviceLocalAccountPolicyStore> store) 79 scoped_ptr<DeviceLocalAccountPolicyStore> store)
25 : store_(store.Pass()), 80 : user_id_(user_id),
81 store_(store.Pass()),
26 core_(PolicyNamespaceKey(dm_protocol::kChromePublicAccountPolicyType, 82 core_(PolicyNamespaceKey(dm_protocol::kChromePublicAccountPolicyType,
27 store_->account_id()), 83 store_->account_id()),
28 store_.get()) {} 84 store_.get()) {}
29 85
30 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() {} 86 DeviceLocalAccountPolicyBroker::~DeviceLocalAccountPolicyBroker() {}
31 87
32 const std::string& DeviceLocalAccountPolicyBroker::account_id() const {
33 return store_->account_id();
34 }
35
36 void DeviceLocalAccountPolicyBroker::Connect( 88 void DeviceLocalAccountPolicyBroker::Connect(
37 scoped_ptr<CloudPolicyClient> client) { 89 scoped_ptr<CloudPolicyClient> client) {
38 core_.Connect(client.Pass()); 90 core_.Connect(client.Pass());
39 core_.StartRefreshScheduler(); 91 core_.StartRefreshScheduler();
40 UpdateRefreshDelay(); 92 UpdateRefreshDelay();
41 } 93 }
42 94
43 void DeviceLocalAccountPolicyBroker::Disconnect() { 95 void DeviceLocalAccountPolicyBroker::Disconnect() {
44 core_.Disconnect(); 96 core_.Disconnect();
45 } 97 }
(...skipping 10 matching lines...) Expand all
56 108
57 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const { 109 std::string DeviceLocalAccountPolicyBroker::GetDisplayName() const {
58 std::string display_name; 110 std::string display_name;
59 const base::Value* display_name_value = 111 const base::Value* display_name_value =
60 store_->policy_map().GetValue(policy::key::kUserDisplayName); 112 store_->policy_map().GetValue(policy::key::kUserDisplayName);
61 if (display_name_value) 113 if (display_name_value)
62 display_name_value->GetAsString(&display_name); 114 display_name_value->GetAsString(&display_name);
63 return display_name; 115 return display_name;
64 } 116 }
65 117
118 DeviceLocalAccountPolicyService::PolicyBrokerWrapper::PolicyBrokerWrapper()
119 : parent(NULL), broker(NULL) {}
120
121 DeviceLocalAccountPolicyBroker*
122 DeviceLocalAccountPolicyService::PolicyBrokerWrapper::GetBroker() {
123 if (!broker) {
124 broker = CreateBroker(user_id, account_id,
125 parent->session_manager_client_,
126 parent->device_settings_service_,
127 parent).release();
128 }
129 return broker;
130 }
131
132 void DeviceLocalAccountPolicyService::PolicyBrokerWrapper::ConnectIfPossible() {
133 if (broker && broker->core()->client())
134 return;
135 scoped_ptr<CloudPolicyClient> client(CreateClient(
136 parent->device_settings_service_,
137 parent->device_management_service_));
138 if (!client)
Mattias Nissler (ping if slow) 2013/05/17 14:29:44 Hm, could as well do if(client) connect
bartfab (slow) 2013/05/17 16:08:47 Done.
139 return;
140 GetBroker()->Connect(client.Pass());
141 }
142
143 void DeviceLocalAccountPolicyService::PolicyBrokerWrapper::Disconnect() {
144 if (broker)
145 broker->Disconnect();
146 }
147
148 void DeviceLocalAccountPolicyService::PolicyBrokerWrapper::DeleteBroker() {
149 if (!broker)
150 return;
151 broker->core()->store()->RemoveObserver(parent);
152 delete broker;
153 broker = NULL;
154 }
155
66 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService( 156 DeviceLocalAccountPolicyService::DeviceLocalAccountPolicyService(
67 chromeos::SessionManagerClient* session_manager_client, 157 chromeos::SessionManagerClient* session_manager_client,
68 chromeos::DeviceSettingsService* device_settings_service) 158 chromeos::DeviceSettingsService* device_settings_service,
159 chromeos::CrosSettings* cros_settings)
69 : session_manager_client_(session_manager_client), 160 : session_manager_client_(session_manager_client),
70 device_settings_service_(device_settings_service), 161 device_settings_service_(device_settings_service),
71 device_management_service_(NULL) { 162 cros_settings_(cros_settings),
72 device_settings_service_->AddObserver(this); 163 device_management_service_(NULL),
73 DeviceSettingsUpdated(); 164 cros_settings_callback_factory_(this) {
165 cros_settings_->AddSettingsObserver(
166 chromeos::kAccountsPrefDeviceLocalAccounts, this);
167 UpdateAccountList();
74 } 168 }
75 169
76 DeviceLocalAccountPolicyService::~DeviceLocalAccountPolicyService() { 170 DeviceLocalAccountPolicyService::~DeviceLocalAccountPolicyService() {
77 device_settings_service_->RemoveObserver(this); 171 cros_settings_->RemoveSettingsObserver(
172 chromeos::kAccountsPrefDeviceLocalAccounts, this);
78 DeleteBrokers(&policy_brokers_); 173 DeleteBrokers(&policy_brokers_);
79 } 174 }
80 175
81 void DeviceLocalAccountPolicyService::Connect( 176 void DeviceLocalAccountPolicyService::Connect(
82 DeviceManagementService* device_management_service) { 177 DeviceManagementService* device_management_service) {
83 DCHECK(!device_management_service_); 178 DCHECK(!device_management_service_);
84 device_management_service_ = device_management_service; 179 device_management_service_ = device_management_service;
85 180
86 // Connect the brokers. 181 // Connect the brokers.
87 for (PolicyBrokerMap::iterator broker(policy_brokers_.begin()); 182 for (PolicyBrokerMap::iterator it(policy_brokers_.begin());
88 broker != policy_brokers_.end(); ++broker) { 183 it != policy_brokers_.end(); ++it) {
89 DCHECK(!broker->second->core()->client()); 184 it->second.ConnectIfPossible();
90 broker->second->Connect(
91 CreateClientForAccount(broker->second->account_id()).Pass());
92 } 185 }
93 } 186 }
94 187
95 void DeviceLocalAccountPolicyService::Disconnect() { 188 void DeviceLocalAccountPolicyService::Disconnect() {
96 DCHECK(device_management_service_); 189 DCHECK(device_management_service_);
97 device_management_service_ = NULL; 190 device_management_service_ = NULL;
98 191
99 // Disconnect the brokers. 192 // Disconnect the brokers.
100 for (PolicyBrokerMap::iterator broker(policy_brokers_.begin()); 193 for (PolicyBrokerMap::iterator it(policy_brokers_.begin());
101 broker != policy_brokers_.end(); ++broker) { 194 it != policy_brokers_.end(); ++it) {
102 broker->second->Disconnect(); 195 it->second.Disconnect();
103 } 196 }
104 } 197 }
105 198
106 DeviceLocalAccountPolicyBroker* 199 DeviceLocalAccountPolicyBroker*
107 DeviceLocalAccountPolicyService::GetBrokerForAccount( 200 DeviceLocalAccountPolicyService::GetBrokerForUser(
108 const std::string& account_id) { 201 const std::string& user_id) {
109 PolicyBrokerMap::iterator entry = policy_brokers_.find(account_id); 202 PolicyBrokerMap::iterator entry = policy_brokers_.find(user_id);
110 if (entry == policy_brokers_.end()) 203 if (entry == policy_brokers_.end())
111 return NULL; 204 return NULL;
112 205
113 if (!entry->second) 206 return entry->second.GetBroker();
114 entry->second = CreateBroker(account_id).release();
115
116 return entry->second;
117 } 207 }
118 208
119 bool DeviceLocalAccountPolicyService::IsPolicyAvailableForAccount( 209 bool DeviceLocalAccountPolicyService::IsPolicyAvailableForUser(
120 const std::string& account_id) { 210 const std::string& user_id) {
121 DeviceLocalAccountPolicyBroker* broker = GetBrokerForAccount(account_id); 211 DeviceLocalAccountPolicyBroker* broker = GetBrokerForUser(user_id);
122 return broker && broker->core()->store()->is_managed(); 212 return broker && broker->core()->store()->is_managed();
123 } 213 }
124 214
125 void DeviceLocalAccountPolicyService::AddObserver(Observer* observer) { 215 void DeviceLocalAccountPolicyService::AddObserver(Observer* observer) {
126 observers_.AddObserver(observer); 216 observers_.AddObserver(observer);
127 } 217 }
128 218
129 void DeviceLocalAccountPolicyService::RemoveObserver(Observer* observer) { 219 void DeviceLocalAccountPolicyService::RemoveObserver(Observer* observer) {
130 observers_.RemoveObserver(observer); 220 observers_.RemoveObserver(observer);
131 } 221 }
132 222
133 void DeviceLocalAccountPolicyService::OwnershipStatusChanged() { 223 void DeviceLocalAccountPolicyService::Observe(
134 // TODO(mnissler): The policy key has changed, re-fetch policy. For 224 int type,
135 // consumer devices, re-sign the current settings and send updates to 225 const content::NotificationSource& source,
136 // session_manager. 226 const content::NotificationDetails& details) {
137 } 227 if (type != chrome::NOTIFICATION_SYSTEM_SETTING_CHANGED ||
228 *content::Details<const std::string>(details).ptr() !=
229 chromeos::kAccountsPrefDeviceLocalAccounts) {
230 NOTREACHED();
231 return;
232 }
138 233
139 void DeviceLocalAccountPolicyService::DeviceSettingsUpdated() { 234 // Avoid unnecessary calls to UpdateAccountList(): If an earlier call is still
140 const em::ChromeDeviceSettingsProto* device_settings = 235 // pending (because the |cros_settings_| are not trusted yet), the updated
141 device_settings_service_->device_settings(); 236 // account list will be processed by that call when it eventually runs.
142 if (device_settings) 237 if (!cros_settings_callback_factory_.HasWeakPtrs())
143 UpdateAccountList(*device_settings); 238 UpdateAccountList();
144 } 239 }
145 240
146 void DeviceLocalAccountPolicyService::OnStoreLoaded(CloudPolicyStore* store) { 241 void DeviceLocalAccountPolicyService::OnStoreLoaded(CloudPolicyStore* store) {
147 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store); 242 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store);
243 DCHECK(broker);
244 if (!broker)
245 return;
148 broker->UpdateRefreshDelay(); 246 broker->UpdateRefreshDelay();
149 FOR_EACH_OBSERVER(Observer, observers_, 247 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyUpdated(broker->user_id()));
150 OnPolicyUpdated(broker->account_id()));
151 } 248 }
152 249
153 void DeviceLocalAccountPolicyService::OnStoreError(CloudPolicyStore* store) { 250 void DeviceLocalAccountPolicyService::OnStoreError(CloudPolicyStore* store) {
154 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store); 251 DeviceLocalAccountPolicyBroker* broker = GetBrokerForStore(store);
155 FOR_EACH_OBSERVER(Observer, observers_, 252 DCHECK(broker);
156 OnPolicyUpdated(broker->account_id())); 253 if (!broker)
254 return;
255 FOR_EACH_OBSERVER(Observer, observers_, OnPolicyUpdated(broker->user_id()));
157 } 256 }
158 257
159 void DeviceLocalAccountPolicyService::UpdateAccountList( 258 void DeviceLocalAccountPolicyService::UpdateAccountList() {
160 const em::ChromeDeviceSettingsProto& device_settings) { 259 if (chromeos::CrosSettingsProvider::TRUSTED !=
161 using google::protobuf::RepeatedPtrField; 260 cros_settings_->PrepareTrustedValues(
261 base::Bind(&DeviceLocalAccountPolicyService::UpdateAccountList,
262 cros_settings_callback_factory_.GetWeakPtr()))) {
263 return;
264 }
162 265
163 // Update |policy_brokers_|, keeping existing entries. 266 // Update |policy_brokers_|, keeping existing entries.
164 PolicyBrokerMap new_policy_brokers; 267 PolicyBrokerMap new_policy_brokers;
165 const RepeatedPtrField<em::DeviceLocalAccountInfoProto>& accounts = 268 const std::vector<DeviceLocalAccount> device_local_accounts =
166 device_settings.device_local_accounts().account(); 269 GetDeviceLocalAccounts(cros_settings_);
167 RepeatedPtrField<em::DeviceLocalAccountInfoProto>::const_iterator entry; 270 for (std::vector<DeviceLocalAccount>::const_iterator it =
168 for (entry = accounts.begin(); entry != accounts.end(); ++entry) { 271 device_local_accounts.begin();
169 std::string account_id; 272 it != device_local_accounts.end(); ++it) {
170 if (entry->has_type() && 273 PolicyBrokerWrapper& wrapper = new_policy_brokers[it->user_id];
171 entry->type() == 274 wrapper.user_id = it->user_id;
172 em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_PUBLIC_SESSION) { 275 wrapper.account_id = it->account_id;
173 account_id = entry->account_id(); 276 wrapper.parent = this;
174 } else if (entry->has_deprecated_public_session_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 277
189 // Reuse the existing broker if present. 278 // Reuse the existing broker if present.
190 DeviceLocalAccountPolicyBroker*& existing_broker = 279 PolicyBrokerWrapper& existing_wrapper = policy_brokers_[it->user_id];
191 policy_brokers_[account_id]; 280 wrapper.broker = existing_wrapper.broker;
192 new_broker = existing_broker; 281 existing_wrapper.broker = NULL;
193 existing_broker = NULL;
194 282
195 // Fire up the cloud connection for fetching policy for the account from 283 // Fire up the cloud connection for fetching policy for the account from
196 // the cloud if this is an enterprise-managed device. 284 // the cloud if this is an enterprise-managed device.
197 if (!new_broker || !new_broker->core()->client()) { 285 wrapper.ConnectIfPossible();
198 scoped_ptr<CloudPolicyClient> client(
199 CreateClientForAccount(account_id));
200 if (client.get()) {
201 if (!new_broker)
202 new_broker = CreateBroker(account_id).release();
203 new_broker->Connect(client.Pass());
204 }
205 }
206 } 286 }
207 policy_brokers_.swap(new_policy_brokers); 287 policy_brokers_.swap(new_policy_brokers);
208 DeleteBrokers(&new_policy_brokers); 288 DeleteBrokers(&new_policy_brokers);
209 289
210 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceLocalAccountsChanged()); 290 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceLocalAccountsChanged());
211 } 291 }
212 292
213 scoped_ptr<DeviceLocalAccountPolicyBroker>
214 DeviceLocalAccountPolicyService::CreateBroker(
215 const std::string& account_id) {
216 scoped_ptr<DeviceLocalAccountPolicyStore> store(
217 new DeviceLocalAccountPolicyStore(account_id, session_manager_client_,
218 device_settings_service_));
219 scoped_ptr<DeviceLocalAccountPolicyBroker> broker(
220 new DeviceLocalAccountPolicyBroker(store.Pass()));
221 broker->core()->store()->AddObserver(this);
222 broker->core()->store()->Load();
223 return broker.Pass();
224 }
225
226 void DeviceLocalAccountPolicyService::DeleteBrokers(PolicyBrokerMap* map) { 293 void DeviceLocalAccountPolicyService::DeleteBrokers(PolicyBrokerMap* map) {
227 for (PolicyBrokerMap::iterator broker = map->begin(); broker != map->end(); 294 for (PolicyBrokerMap::iterator it = map->begin(); it != map->end(); ++it)
228 ++broker) { 295 it->second.DeleteBroker();
229 if (broker->second) {
230 broker->second->core()->store()->RemoveObserver(this);
231 delete broker->second;
232 }
233 }
234 map->clear(); 296 map->clear();
235 } 297 }
236 298
237 DeviceLocalAccountPolicyBroker* 299 DeviceLocalAccountPolicyBroker*
238 DeviceLocalAccountPolicyService::GetBrokerForStore( 300 DeviceLocalAccountPolicyService::GetBrokerForStore(
239 CloudPolicyStore* store) { 301 CloudPolicyStore* store) {
240 for (PolicyBrokerMap::iterator broker(policy_brokers_.begin()); 302 for (PolicyBrokerMap::iterator it(policy_brokers_.begin());
241 broker != policy_brokers_.end(); ++broker) { 303 it != policy_brokers_.end(); ++it) {
242 if (broker->second->core()->store() == store) 304 if (it->second.broker && it->second.broker->core()->store() == store)
243 return broker->second; 305 return it->second.broker;
244 } 306 }
245 return NULL; 307 return NULL;
246 } 308 }
247 309
248 scoped_ptr<CloudPolicyClient>
249 DeviceLocalAccountPolicyService::CreateClientForAccount(
250 const std::string& account_id) {
251 const em::PolicyData* policy_data = device_settings_service_->policy_data();
252 if (!policy_data ||
253 !policy_data->has_request_token() ||
254 !policy_data->has_device_id() ||
255 !device_management_service_) {
256 return scoped_ptr<CloudPolicyClient>();
257 }
258
259 scoped_ptr<CloudPolicyClient> client(
260 new CloudPolicyClient(std::string(), std::string(),
261 USER_AFFILIATION_MANAGED,
262 NULL, device_management_service_));
263 client->SetupRegistration(policy_data->request_token(),
264 policy_data->device_id());
265 return client.Pass();
266 }
267
268 } // namespace policy 310 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698