Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / components / user_manager / user_manager_base.cc
blobb93d023153a85986ac726cf667213a82302d1fb4
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/user_manager/user_manager_base.h"
7 #include <cstddef>
8 #include <set>
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/command_line.h"
13 #include "base/compiler_specific.h"
14 #include "base/format_macros.h"
15 #include "base/location.h"
16 #include "base/logging.h"
17 #include "base/macros.h"
18 #include "base/metrics/histogram.h"
19 #include "base/prefs/pref_registry_simple.h"
20 #include "base/prefs/pref_service.h"
21 #include "base/prefs/scoped_user_pref_update.h"
22 #include "base/strings/string_util.h"
23 #include "base/strings/stringprintf.h"
24 #include "base/strings/utf_string_conversions.h"
25 #include "base/task_runner.h"
26 #include "base/values.h"
27 #include "chromeos/chromeos_switches.h"
28 #include "chromeos/cryptohome/async_method_caller.h"
29 #include "chromeos/login/login_state.h"
30 #include "chromeos/login/user_names.h"
31 #include "components/session_manager/core/session_manager.h"
32 #include "components/user_manager/remove_user_delegate.h"
33 #include "components/user_manager/user_type.h"
34 #include "google_apis/gaia/gaia_auth_util.h"
35 #include "ui/base/l10n/l10n_util.h"
37 namespace user_manager {
38 namespace {
40 // A vector pref of the the regular users known on this device, arranged in LRU
41 // order.
42 const char kRegularUsers[] = "LoggedInUsers";
44 // A dictionary that maps user IDs to the displayed name.
45 const char kUserDisplayName[] = "UserDisplayName";
47 // A dictionary that maps user IDs to the user's given name.
48 const char kUserGivenName[] = "UserGivenName";
50 // A dictionary that maps user IDs to the displayed (non-canonical) emails.
51 const char kUserDisplayEmail[] = "UserDisplayEmail";
53 // A dictionary that maps user IDs to OAuth token presence flag.
54 const char kUserOAuthTokenStatus[] = "OAuthTokenStatus";
56 // A dictionary that maps user IDs to a flag indicating whether online
57 // authentication against GAIA should be enforced during the next sign-in.
58 const char kUserForceOnlineSignin[] = "UserForceOnlineSignin";
60 // A dictionary that maps user ID to the user type.
61 const char kUserType[] = "UserType";
63 // A string pref containing the ID of the last user who logged in if it was
64 // a user with gaia account (regular) or an empty string if it was another type
65 // of user (guest, kiosk, public account, etc.).
66 const char kLastLoggedInGaiaUser[] = "LastLoggedInRegularUser";
68 // A string pref containing the ID of the last active user.
69 // In case of browser crash, this pref will be used to set active user after
70 // session restore.
71 const char kLastActiveUser[] = "LastActiveUser";
73 // A vector pref of preferences of known users. All new preferences should be
74 // placed in this list.
75 const char kKnownUsers[] = "KnownUsers";
77 // Known user preferences keys (stored in Local State).
79 // Key of canonical e-mail value.
80 const char kCanonicalEmail[] = "email";
82 // Key of obfuscated GAIA id value.
83 const char kGAIAIdKey[] = "gaia_id";
85 // Key of whether this user ID refers to a SAML user.
86 const char kUsingSAMLKey[] = "using_saml";
88 // Key of Device Id.
89 const char kDeviceId[] = "device_id";
91 // Key of GAPS cookie.
92 const char kGAPSCookie[] = "gaps_cookie";
94 // Key of the reason for re-auth.
95 const char kReauthReasonKey[] = "reauth_reason";
97 // Upper bound for a histogram metric reporting the amount of time between
98 // one regular user logging out and a different regular user logging in.
99 const int kLogoutToLoginDelayMaxSec = 1800;
101 // Callback that is called after user removal is complete.
102 void OnRemoveUserComplete(const std::string& user_email,
103 bool success,
104 cryptohome::MountError return_code) {
105 // Log the error, but there's not much we can do.
106 if (!success) {
107 LOG(ERROR) << "Removal of cryptohome for " << user_email
108 << " failed, return code: " << return_code;
112 // Runs on SequencedWorkerPool thread. Passes resolved locale to UI thread.
113 void ResolveLocale(const std::string& raw_locale,
114 std::string* resolved_locale) {
115 ignore_result(l10n_util::CheckAndResolveLocale(raw_locale, resolved_locale));
118 // Checks if values in |dict| correspond with |user_id| identity.
119 bool UserMatches(const UserID& user_id, const base::DictionaryValue& dict) {
120 std::string value;
122 bool has_email = dict.GetString(kCanonicalEmail, &value);
123 if (has_email && user_id == value)
124 return true;
126 // TODO(antrim): update code once user id is really a struct.
127 bool has_gaia_id = dict.GetString(kGAIAIdKey, &value);
128 if (has_gaia_id && user_id == value)
129 return true;
131 return false;
134 // Fills relevant |dict| values based on |user_id|.
135 void UpdateIdentity(const UserID& user_id, base::DictionaryValue& dict) {
136 dict.SetString(kCanonicalEmail, user_id);
139 } // namespace
141 // static
142 void UserManagerBase::RegisterPrefs(PrefRegistrySimple* registry) {
143 registry->RegisterListPref(kRegularUsers);
144 registry->RegisterListPref(kKnownUsers);
145 registry->RegisterStringPref(kLastLoggedInGaiaUser, std::string());
146 registry->RegisterDictionaryPref(kUserDisplayName);
147 registry->RegisterDictionaryPref(kUserGivenName);
148 registry->RegisterDictionaryPref(kUserDisplayEmail);
149 registry->RegisterDictionaryPref(kUserOAuthTokenStatus);
150 registry->RegisterDictionaryPref(kUserForceOnlineSignin);
151 registry->RegisterDictionaryPref(kUserType);
152 registry->RegisterStringPref(kLastActiveUser, std::string());
155 UserManagerBase::UserManagerBase(
156 scoped_refptr<base::TaskRunner> task_runner,
157 scoped_refptr<base::TaskRunner> blocking_task_runner)
158 : active_user_(NULL),
159 primary_user_(NULL),
160 user_loading_stage_(STAGE_NOT_LOADED),
161 session_started_(false),
162 is_current_user_owner_(false),
163 is_current_user_new_(false),
164 is_current_user_ephemeral_regular_user_(false),
165 ephemeral_users_enabled_(false),
166 manager_creation_time_(base::TimeTicks::Now()),
167 last_session_active_user_initialized_(false),
168 task_runner_(task_runner),
169 blocking_task_runner_(blocking_task_runner),
170 weak_factory_(this) {
171 UpdateLoginState();
174 UserManagerBase::~UserManagerBase() {
175 // Can't use STLDeleteElements because of the private destructor of User.
176 for (UserList::iterator it = users_.begin(); it != users_.end();
177 it = users_.erase(it)) {
178 DeleteUser(*it);
180 // These are pointers to the same User instances that were in users_ list.
181 logged_in_users_.clear();
182 lru_logged_in_users_.clear();
184 DeleteUser(active_user_);
187 void UserManagerBase::Shutdown() {
188 DCHECK(task_runner_->RunsTasksOnCurrentThread());
191 const UserList& UserManagerBase::GetUsers() const {
192 const_cast<UserManagerBase*>(this)->EnsureUsersLoaded();
193 return users_;
196 const UserList& UserManagerBase::GetLoggedInUsers() const {
197 return logged_in_users_;
200 const UserList& UserManagerBase::GetLRULoggedInUsers() const {
201 return lru_logged_in_users_;
204 const std::string& UserManagerBase::GetOwnerEmail() const {
205 return owner_email_;
208 void UserManagerBase::UserLoggedIn(const std::string& user_id,
209 const std::string& username_hash,
210 bool browser_restart) {
211 DCHECK(task_runner_->RunsTasksOnCurrentThread());
213 if (!last_session_active_user_initialized_) {
214 last_session_active_user_ = GetLocalState()->GetString(kLastActiveUser);
215 last_session_active_user_initialized_ = true;
218 User* user = FindUserInListAndModify(user_id);
219 if (active_user_ && user) {
220 user->set_is_logged_in(true);
221 user->set_username_hash(username_hash);
222 logged_in_users_.push_back(user);
223 lru_logged_in_users_.push_back(user);
225 // Reset the new user flag if the user already exists.
226 SetIsCurrentUserNew(false);
227 NotifyUserAddedToSession(user, true /* user switch pending */);
229 return;
232 if (user_id == chromeos::login::kGuestUserName) {
233 GuestUserLoggedIn();
234 } else if (IsKioskApp(user_id)) {
235 KioskAppLoggedIn(user_id);
236 } else if (IsDemoApp(user_id)) {
237 DemoAccountLoggedIn();
238 } else {
239 EnsureUsersLoaded();
241 if (user && user->GetType() == USER_TYPE_PUBLIC_ACCOUNT) {
242 PublicAccountUserLoggedIn(user);
243 } else if ((user && user->GetType() == USER_TYPE_SUPERVISED) ||
244 (!user &&
245 gaia::ExtractDomainName(user_id) ==
246 chromeos::login::kSupervisedUserDomain)) {
247 SupervisedUserLoggedIn(user_id);
248 } else if (browser_restart && IsPublicAccountMarkedForRemoval(user_id)) {
249 PublicAccountUserLoggedIn(User::CreatePublicAccountUser(user_id));
250 } else if (user_id != GetOwnerEmail() && !user &&
251 (AreEphemeralUsersEnabled() || browser_restart)) {
252 RegularUserLoggedInAsEphemeral(user_id);
253 } else {
254 RegularUserLoggedIn(user_id);
258 DCHECK(active_user_);
259 active_user_->set_is_logged_in(true);
260 active_user_->set_is_active(true);
261 active_user_->set_username_hash(username_hash);
263 // Place user who just signed in to the top of the logged in users.
264 logged_in_users_.insert(logged_in_users_.begin(), active_user_);
265 SetLRUUser(active_user_);
267 if (!primary_user_) {
268 primary_user_ = active_user_;
269 if (primary_user_->HasGaiaAccount())
270 SendGaiaUserLoginMetrics(user_id);
273 UMA_HISTOGRAM_ENUMERATION(
274 "UserManager.LoginUserType", active_user_->GetType(), NUM_USER_TYPES);
276 GetLocalState()->SetString(
277 kLastLoggedInGaiaUser, active_user_->HasGaiaAccount() ? user_id : "");
279 NotifyOnLogin();
280 PerformPostUserLoggedInActions(browser_restart);
283 void UserManagerBase::SwitchActiveUser(const std::string& user_id) {
284 User* user = FindUserAndModify(user_id);
285 if (!user) {
286 NOTREACHED() << "Switching to a non-existing user";
287 return;
289 if (user == active_user_) {
290 NOTREACHED() << "Switching to a user who is already active";
291 return;
293 if (!user->is_logged_in()) {
294 NOTREACHED() << "Switching to a user that is not logged in";
295 return;
297 if (!user->HasGaiaAccount()) {
298 NOTREACHED() <<
299 "Switching to a user without gaia account (non-regular one)";
300 return;
302 if (user->username_hash().empty()) {
303 NOTREACHED() << "Switching to a user that doesn't have username_hash set";
304 return;
307 DCHECK(active_user_);
308 active_user_->set_is_active(false);
309 user->set_is_active(true);
310 active_user_ = user;
312 // Move the user to the front.
313 SetLRUUser(active_user_);
315 NotifyActiveUserHashChanged(active_user_->username_hash());
316 NotifyActiveUserChanged(active_user_);
319 void UserManagerBase::SwitchToLastActiveUser() {
320 if (last_session_active_user_.empty())
321 return;
323 if (GetActiveUser()->email() != last_session_active_user_)
324 SwitchActiveUser(last_session_active_user_);
326 // Make sure that this function gets run only once.
327 last_session_active_user_.clear();
330 void UserManagerBase::SessionStarted() {
331 DCHECK(task_runner_->RunsTasksOnCurrentThread());
332 session_started_ = true;
334 UpdateLoginState();
335 session_manager::SessionManager::Get()->SetSessionState(
336 session_manager::SESSION_STATE_ACTIVE);
338 if (IsCurrentUserNew()) {
339 // Make sure that the new user's data is persisted to Local State.
340 GetLocalState()->CommitPendingWrite();
344 void UserManagerBase::RemoveUser(const std::string& user_id,
345 RemoveUserDelegate* delegate) {
346 DCHECK(task_runner_->RunsTasksOnCurrentThread());
348 if (!CanUserBeRemoved(FindUser(user_id)))
349 return;
351 RemoveUserInternal(user_id, delegate);
354 void UserManagerBase::RemoveUserInternal(const std::string& user_email,
355 RemoveUserDelegate* delegate) {
356 RemoveNonOwnerUserInternal(user_email, delegate);
359 void UserManagerBase::RemoveNonOwnerUserInternal(const std::string& user_email,
360 RemoveUserDelegate* delegate) {
361 if (delegate)
362 delegate->OnBeforeUserRemoved(user_email);
363 RemoveUserFromList(user_email);
364 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove(
365 user_email, base::Bind(&OnRemoveUserComplete, user_email));
367 if (delegate)
368 delegate->OnUserRemoved(user_email);
371 void UserManagerBase::RemoveUserFromList(const std::string& user_id) {
372 DCHECK(task_runner_->RunsTasksOnCurrentThread());
373 RemoveNonCryptohomeData(user_id);
374 if (user_loading_stage_ == STAGE_LOADED) {
375 DeleteUser(RemoveRegularOrSupervisedUserFromList(user_id));
376 } else if (user_loading_stage_ == STAGE_LOADING) {
377 DCHECK(gaia::ExtractDomainName(user_id) ==
378 chromeos::login::kSupervisedUserDomain ||
379 HasPendingBootstrap(user_id));
380 // Special case, removing partially-constructed supervised user or
381 // boostrapping user during user list loading.
382 ListPrefUpdate users_update(GetLocalState(), kRegularUsers);
383 users_update->Remove(base::StringValue(user_id), NULL);
384 } else {
385 NOTREACHED() << "Users are not loaded yet.";
386 return;
389 // Make sure that new data is persisted to Local State.
390 GetLocalState()->CommitPendingWrite();
393 bool UserManagerBase::IsKnownUser(const std::string& user_id) const {
394 return FindUser(user_id) != NULL;
397 const User* UserManagerBase::FindUser(const std::string& user_id) const {
398 DCHECK(task_runner_->RunsTasksOnCurrentThread());
399 if (active_user_ && active_user_->email() == user_id)
400 return active_user_;
401 return FindUserInList(user_id);
404 User* UserManagerBase::FindUserAndModify(const std::string& user_id) {
405 DCHECK(task_runner_->RunsTasksOnCurrentThread());
406 if (active_user_ && active_user_->email() == user_id)
407 return active_user_;
408 return FindUserInListAndModify(user_id);
411 const User* UserManagerBase::GetLoggedInUser() const {
412 DCHECK(task_runner_->RunsTasksOnCurrentThread());
413 return active_user_;
416 User* UserManagerBase::GetLoggedInUser() {
417 DCHECK(task_runner_->RunsTasksOnCurrentThread());
418 return active_user_;
421 const User* UserManagerBase::GetActiveUser() const {
422 DCHECK(task_runner_->RunsTasksOnCurrentThread());
423 return active_user_;
426 User* UserManagerBase::GetActiveUser() {
427 DCHECK(task_runner_->RunsTasksOnCurrentThread());
428 return active_user_;
431 const User* UserManagerBase::GetPrimaryUser() const {
432 DCHECK(task_runner_->RunsTasksOnCurrentThread());
433 return primary_user_;
436 void UserManagerBase::SaveUserOAuthStatus(
437 const std::string& user_id,
438 User::OAuthTokenStatus oauth_token_status) {
439 DCHECK(task_runner_->RunsTasksOnCurrentThread());
441 DVLOG(1) << "Saving user OAuth token status in Local State";
442 User* user = FindUserAndModify(user_id);
443 if (user)
444 user->set_oauth_token_status(oauth_token_status);
446 // Do not update local state if data stored or cached outside the user's
447 // cryptohome is to be treated as ephemeral.
448 if (IsUserNonCryptohomeDataEphemeral(user_id))
449 return;
451 DictionaryPrefUpdate oauth_status_update(GetLocalState(),
452 kUserOAuthTokenStatus);
453 oauth_status_update->SetWithoutPathExpansion(
454 user_id,
455 new base::FundamentalValue(static_cast<int>(oauth_token_status)));
458 void UserManagerBase::SaveForceOnlineSignin(const std::string& user_id,
459 bool force_online_signin) {
460 DCHECK(task_runner_->RunsTasksOnCurrentThread());
462 // Do not update local state if data stored or cached outside the user's
463 // cryptohome is to be treated as ephemeral.
464 if (IsUserNonCryptohomeDataEphemeral(user_id))
465 return;
467 DictionaryPrefUpdate force_online_update(GetLocalState(),
468 kUserForceOnlineSignin);
469 force_online_update->SetBooleanWithoutPathExpansion(user_id,
470 force_online_signin);
473 void UserManagerBase::SaveUserDisplayName(const std::string& user_id,
474 const base::string16& display_name) {
475 DCHECK(task_runner_->RunsTasksOnCurrentThread());
477 if (User* user = FindUserAndModify(user_id)) {
478 user->set_display_name(display_name);
480 // Do not update local state if data stored or cached outside the user's
481 // cryptohome is to be treated as ephemeral.
482 if (!IsUserNonCryptohomeDataEphemeral(user_id)) {
483 DictionaryPrefUpdate display_name_update(GetLocalState(),
484 kUserDisplayName);
485 display_name_update->SetWithoutPathExpansion(
486 user_id, new base::StringValue(display_name));
491 base::string16 UserManagerBase::GetUserDisplayName(
492 const std::string& user_id) const {
493 const User* user = FindUser(user_id);
494 return user ? user->display_name() : base::string16();
497 void UserManagerBase::SaveUserDisplayEmail(const std::string& user_id,
498 const std::string& display_email) {
499 DCHECK(task_runner_->RunsTasksOnCurrentThread());
501 User* user = FindUserAndModify(user_id);
502 if (!user) {
503 LOG(ERROR) << "User not found: " << user_id;
504 return; // Ignore if there is no such user.
507 user->set_display_email(display_email);
509 // Do not update local state if data stored or cached outside the user's
510 // cryptohome is to be treated as ephemeral.
511 if (IsUserNonCryptohomeDataEphemeral(user_id))
512 return;
514 DictionaryPrefUpdate display_email_update(GetLocalState(), kUserDisplayEmail);
515 display_email_update->SetWithoutPathExpansion(
516 user_id, new base::StringValue(display_email));
519 std::string UserManagerBase::GetUserDisplayEmail(
520 const std::string& user_id) const {
521 const User* user = FindUser(user_id);
522 return user ? user->display_email() : user_id;
525 void UserManagerBase::SaveUserType(const std::string& user_id,
526 const UserType& user_type) {
527 DCHECK(task_runner_->RunsTasksOnCurrentThread());
529 User* user = FindUserAndModify(user_id);
530 if (!user) {
531 LOG(ERROR) << "User not found: " << user_id;
532 return; // Ignore if there is no such user.
535 // Do not update local state if data stored or cached outside the user's
536 // cryptohome is to be treated as ephemeral.
537 if (IsUserNonCryptohomeDataEphemeral(user_id))
538 return;
540 DictionaryPrefUpdate user_type_update(GetLocalState(), kUserType);
541 user_type_update->SetWithoutPathExpansion(
542 user_id, new base::FundamentalValue(static_cast<int>(user_type)));
543 GetLocalState()->CommitPendingWrite();
546 void UserManagerBase::UpdateUsingSAML(const std::string& user_id,
547 const bool using_saml) {
548 SetKnownUserBooleanPref(user_id, kUsingSAMLKey, using_saml);
551 bool UserManagerBase::FindUsingSAML(const std::string& user_id) {
552 bool using_saml;
553 if (GetKnownUserBooleanPref(user_id, kUsingSAMLKey, &using_saml))
554 return using_saml;
555 return false;
558 void UserManagerBase::UpdateReauthReason(const std::string& user_id,
559 const int reauth_reason) {
560 SetKnownUserIntegerPref(user_id, kReauthReasonKey, reauth_reason);
563 bool UserManagerBase::FindReauthReason(const std::string& user_id,
564 int* out_value) {
565 return GetKnownUserIntegerPref(user_id, kReauthReasonKey, out_value);
568 void UserManagerBase::UpdateUserAccountData(
569 const std::string& user_id,
570 const UserAccountData& account_data) {
571 DCHECK(task_runner_->RunsTasksOnCurrentThread());
573 SaveUserDisplayName(user_id, account_data.display_name());
575 if (User* user = FindUserAndModify(user_id)) {
576 base::string16 given_name = account_data.given_name();
577 user->set_given_name(given_name);
578 if (!IsUserNonCryptohomeDataEphemeral(user_id)) {
579 DictionaryPrefUpdate given_name_update(GetLocalState(), kUserGivenName);
580 given_name_update->SetWithoutPathExpansion(
581 user_id, new base::StringValue(given_name));
585 UpdateUserAccountLocale(user_id, account_data.locale());
588 // static
589 void UserManagerBase::ParseUserList(const base::ListValue& users_list,
590 const std::set<std::string>& existing_users,
591 std::vector<std::string>* users_vector,
592 std::set<std::string>* users_set) {
593 users_vector->clear();
594 users_set->clear();
595 for (size_t i = 0; i < users_list.GetSize(); ++i) {
596 std::string email;
597 if (!users_list.GetString(i, &email) || email.empty()) {
598 LOG(ERROR) << "Corrupt entry in user list at index " << i << ".";
599 continue;
601 if (existing_users.find(email) != existing_users.end() ||
602 !users_set->insert(email).second) {
603 LOG(ERROR) << "Duplicate user: " << email;
604 continue;
606 users_vector->push_back(email);
610 bool UserManagerBase::IsCurrentUserOwner() const {
611 DCHECK(task_runner_->RunsTasksOnCurrentThread());
612 base::AutoLock lk(is_current_user_owner_lock_);
613 return is_current_user_owner_;
616 void UserManagerBase::SetCurrentUserIsOwner(bool is_current_user_owner) {
617 DCHECK(task_runner_->RunsTasksOnCurrentThread());
619 base::AutoLock lk(is_current_user_owner_lock_);
620 is_current_user_owner_ = is_current_user_owner;
622 UpdateLoginState();
625 bool UserManagerBase::IsCurrentUserNew() const {
626 DCHECK(task_runner_->RunsTasksOnCurrentThread());
627 return is_current_user_new_;
630 bool UserManagerBase::IsCurrentUserNonCryptohomeDataEphemeral() const {
631 DCHECK(task_runner_->RunsTasksOnCurrentThread());
632 return IsUserLoggedIn() &&
633 IsUserNonCryptohomeDataEphemeral(GetLoggedInUser()->email());
636 bool UserManagerBase::CanCurrentUserLock() const {
637 DCHECK(task_runner_->RunsTasksOnCurrentThread());
638 return IsUserLoggedIn() && active_user_->can_lock();
641 bool UserManagerBase::IsUserLoggedIn() const {
642 DCHECK(task_runner_->RunsTasksOnCurrentThread());
643 return active_user_;
646 bool UserManagerBase::IsLoggedInAsUserWithGaiaAccount() const {
647 DCHECK(task_runner_->RunsTasksOnCurrentThread());
648 return IsUserLoggedIn() && active_user_->HasGaiaAccount();
651 bool UserManagerBase::IsLoggedInAsChildUser() const {
652 DCHECK(task_runner_->RunsTasksOnCurrentThread());
653 return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_CHILD;
656 bool UserManagerBase::IsLoggedInAsPublicAccount() const {
657 DCHECK(task_runner_->RunsTasksOnCurrentThread());
658 return IsUserLoggedIn() &&
659 active_user_->GetType() == USER_TYPE_PUBLIC_ACCOUNT;
662 bool UserManagerBase::IsLoggedInAsGuest() const {
663 DCHECK(task_runner_->RunsTasksOnCurrentThread());
664 return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_GUEST;
667 bool UserManagerBase::IsLoggedInAsSupervisedUser() const {
668 DCHECK(task_runner_->RunsTasksOnCurrentThread());
669 return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_SUPERVISED;
672 bool UserManagerBase::IsLoggedInAsKioskApp() const {
673 DCHECK(task_runner_->RunsTasksOnCurrentThread());
674 return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_KIOSK_APP;
677 bool UserManagerBase::IsLoggedInAsStub() const {
678 DCHECK(task_runner_->RunsTasksOnCurrentThread());
679 return IsUserLoggedIn() &&
680 active_user_->email() == chromeos::login::kStubUser;
683 bool UserManagerBase::IsSessionStarted() const {
684 DCHECK(task_runner_->RunsTasksOnCurrentThread());
685 return session_started_;
688 bool UserManagerBase::IsUserNonCryptohomeDataEphemeral(
689 const std::string& user_id) const {
690 // Data belonging to the guest and stub users is always ephemeral.
691 if (user_id == chromeos::login::kGuestUserName ||
692 user_id == chromeos::login::kStubUser) {
693 return true;
696 // Data belonging to the owner, anyone found on the user list and obsolete
697 // public accounts whose data has not been removed yet is not ephemeral.
698 if (user_id == GetOwnerEmail() || UserExistsInList(user_id) ||
699 IsPublicAccountMarkedForRemoval(user_id)) {
700 return false;
703 // Data belonging to the currently logged-in user is ephemeral when:
704 // a) The user logged into a regular gaia account while the ephemeral users
705 // policy was enabled.
706 // - or -
707 // b) The user logged into any other account type.
708 if (IsUserLoggedIn() && (user_id == GetLoggedInUser()->email()) &&
709 (is_current_user_ephemeral_regular_user_ ||
710 !IsLoggedInAsUserWithGaiaAccount())) {
711 return true;
714 // Data belonging to any other user is ephemeral when:
715 // a) Going through the regular login flow and the ephemeral users policy is
716 // enabled.
717 // - or -
718 // b) The browser is restarting after a crash.
719 return AreEphemeralUsersEnabled() ||
720 session_manager::SessionManager::HasBrowserRestarted();
723 void UserManagerBase::AddObserver(UserManager::Observer* obs) {
724 DCHECK(task_runner_->RunsTasksOnCurrentThread());
725 observer_list_.AddObserver(obs);
728 void UserManagerBase::RemoveObserver(UserManager::Observer* obs) {
729 DCHECK(task_runner_->RunsTasksOnCurrentThread());
730 observer_list_.RemoveObserver(obs);
733 void UserManagerBase::AddSessionStateObserver(
734 UserManager::UserSessionStateObserver* obs) {
735 DCHECK(task_runner_->RunsTasksOnCurrentThread());
736 session_state_observer_list_.AddObserver(obs);
739 void UserManagerBase::RemoveSessionStateObserver(
740 UserManager::UserSessionStateObserver* obs) {
741 DCHECK(task_runner_->RunsTasksOnCurrentThread());
742 session_state_observer_list_.RemoveObserver(obs);
745 void UserManagerBase::NotifyLocalStateChanged() {
746 DCHECK(task_runner_->RunsTasksOnCurrentThread());
747 FOR_EACH_OBSERVER(
748 UserManager::Observer, observer_list_, LocalStateChanged(this));
751 bool UserManagerBase::CanUserBeRemoved(const User* user) const {
752 // Only regular and supervised users are allowed to be manually removed.
753 if (!user || !(user->HasGaiaAccount() || user->IsSupervised()))
754 return false;
756 // Sanity check: we must not remove single user unless it's an enterprise
757 // device. This check may seem redundant at a first sight because
758 // this single user must be an owner and we perform special check later
759 // in order not to remove an owner. However due to non-instant nature of
760 // ownership assignment this later check may sometimes fail.
761 // See http://crosbug.com/12723
762 if (users_.size() < 2 && !IsEnterpriseManaged())
763 return false;
765 // Sanity check: do not allow any of the the logged in users to be removed.
766 for (UserList::const_iterator it = logged_in_users_.begin();
767 it != logged_in_users_.end();
768 ++it) {
769 if ((*it)->email() == user->email())
770 return false;
773 return true;
776 bool UserManagerBase::GetEphemeralUsersEnabled() const {
777 return ephemeral_users_enabled_;
780 void UserManagerBase::SetEphemeralUsersEnabled(bool enabled) {
781 ephemeral_users_enabled_ = enabled;
784 void UserManagerBase::SetIsCurrentUserNew(bool is_new) {
785 is_current_user_new_ = is_new;
788 bool UserManagerBase::HasPendingBootstrap(const std::string& user_id) const {
789 return false;
792 void UserManagerBase::SetOwnerEmail(std::string owner_user_id) {
793 owner_email_ = owner_user_id;
796 const std::string& UserManagerBase::GetPendingUserSwitchID() const {
797 return pending_user_switch_;
800 void UserManagerBase::SetPendingUserSwitchID(std::string user_id) {
801 pending_user_switch_ = user_id;
804 void UserManagerBase::EnsureUsersLoaded() {
805 DCHECK(task_runner_->RunsTasksOnCurrentThread());
806 if (!GetLocalState())
807 return;
809 if (user_loading_stage_ != STAGE_NOT_LOADED)
810 return;
811 user_loading_stage_ = STAGE_LOADING;
813 PerformPreUserListLoadingActions();
815 PrefService* local_state = GetLocalState();
816 const base::ListValue* prefs_regular_users =
817 local_state->GetList(kRegularUsers);
819 const base::DictionaryValue* prefs_display_names =
820 local_state->GetDictionary(kUserDisplayName);
821 const base::DictionaryValue* prefs_given_names =
822 local_state->GetDictionary(kUserGivenName);
823 const base::DictionaryValue* prefs_display_emails =
824 local_state->GetDictionary(kUserDisplayEmail);
825 const base::DictionaryValue* prefs_user_types =
826 local_state->GetDictionary(kUserType);
828 // Load public sessions first.
829 std::set<std::string> public_sessions_set;
830 LoadPublicAccounts(&public_sessions_set);
832 // Load regular users and supervised users.
833 std::vector<std::string> regular_users;
834 std::set<std::string> regular_users_set;
835 ParseUserList(*prefs_regular_users,
836 public_sessions_set,
837 &regular_users,
838 &regular_users_set);
839 for (std::vector<std::string>::const_iterator it = regular_users.begin();
840 it != regular_users.end();
841 ++it) {
842 User* user = NULL;
843 const std::string domain = gaia::ExtractDomainName(*it);
844 if (domain == chromeos::login::kSupervisedUserDomain) {
845 user = User::CreateSupervisedUser(*it);
846 } else {
847 user = User::CreateRegularUser(*it);
848 int user_type;
849 if (prefs_user_types->GetIntegerWithoutPathExpansion(*it, &user_type) &&
850 user_type == USER_TYPE_CHILD) {
851 ChangeUserChildStatus(user, true /* is child */);
854 user->set_oauth_token_status(LoadUserOAuthStatus(*it));
855 user->set_force_online_signin(LoadForceOnlineSignin(*it));
856 user->set_using_saml(FindUsingSAML(*it));
857 users_.push_back(user);
859 base::string16 display_name;
860 if (prefs_display_names->GetStringWithoutPathExpansion(*it,
861 &display_name)) {
862 user->set_display_name(display_name);
865 base::string16 given_name;
866 if (prefs_given_names->GetStringWithoutPathExpansion(*it, &given_name)) {
867 user->set_given_name(given_name);
870 std::string display_email;
871 if (prefs_display_emails->GetStringWithoutPathExpansion(*it,
872 &display_email)) {
873 user->set_display_email(display_email);
877 user_loading_stage_ = STAGE_LOADED;
879 PerformPostUserListLoadingActions();
882 UserList& UserManagerBase::GetUsersAndModify() {
883 EnsureUsersLoaded();
884 return users_;
887 const User* UserManagerBase::FindUserInList(const std::string& user_id) const {
888 const UserList& users = GetUsers();
889 for (UserList::const_iterator it = users.begin(); it != users.end(); ++it) {
890 if ((*it)->email() == user_id)
891 return *it;
893 return NULL;
896 bool UserManagerBase::UserExistsInList(const std::string& user_id) const {
897 const base::ListValue* user_list = GetLocalState()->GetList(kRegularUsers);
898 for (size_t i = 0; i < user_list->GetSize(); ++i) {
899 std::string email;
900 if (user_list->GetString(i, &email) && (user_id == email))
901 return true;
903 return false;
906 User* UserManagerBase::FindUserInListAndModify(const std::string& user_id) {
907 UserList& users = GetUsersAndModify();
908 for (UserList::iterator it = users.begin(); it != users.end(); ++it) {
909 if ((*it)->email() == user_id)
910 return *it;
912 return NULL;
915 void UserManagerBase::GuestUserLoggedIn() {
916 DCHECK(task_runner_->RunsTasksOnCurrentThread());
917 active_user_ = User::CreateGuestUser();
920 void UserManagerBase::AddUserRecord(User* user) {
921 // Add the user to the front of the user list.
922 ListPrefUpdate prefs_users_update(GetLocalState(), kRegularUsers);
923 prefs_users_update->Insert(0, new base::StringValue(user->email()));
924 users_.insert(users_.begin(), user);
927 void UserManagerBase::RegularUserLoggedIn(const std::string& user_id) {
928 // Remove the user from the user list.
929 active_user_ = RemoveRegularOrSupervisedUserFromList(user_id);
931 // If the user was not found on the user list, create a new user.
932 SetIsCurrentUserNew(!active_user_);
933 if (IsCurrentUserNew()) {
934 active_user_ = User::CreateRegularUser(user_id);
935 active_user_->set_oauth_token_status(LoadUserOAuthStatus(user_id));
936 SaveUserDisplayName(active_user_->email(),
937 base::UTF8ToUTF16(active_user_->GetAccountName(true)));
940 AddUserRecord(active_user_);
942 // Make sure that new data is persisted to Local State.
943 GetLocalState()->CommitPendingWrite();
946 void UserManagerBase::RegularUserLoggedInAsEphemeral(
947 const std::string& user_id) {
948 DCHECK(task_runner_->RunsTasksOnCurrentThread());
949 SetIsCurrentUserNew(true);
950 is_current_user_ephemeral_regular_user_ = true;
951 active_user_ = User::CreateRegularUser(user_id);
954 void UserManagerBase::NotifyOnLogin() {
955 DCHECK(task_runner_->RunsTasksOnCurrentThread());
957 NotifyActiveUserHashChanged(active_user_->username_hash());
958 NotifyActiveUserChanged(active_user_);
959 UpdateLoginState();
962 User::OAuthTokenStatus UserManagerBase::LoadUserOAuthStatus(
963 const std::string& user_id) const {
964 DCHECK(task_runner_->RunsTasksOnCurrentThread());
966 const base::DictionaryValue* prefs_oauth_status =
967 GetLocalState()->GetDictionary(kUserOAuthTokenStatus);
968 int oauth_token_status = User::OAUTH_TOKEN_STATUS_UNKNOWN;
969 if (prefs_oauth_status &&
970 prefs_oauth_status->GetIntegerWithoutPathExpansion(user_id,
971 &oauth_token_status)) {
972 User::OAuthTokenStatus status =
973 static_cast<User::OAuthTokenStatus>(oauth_token_status);
974 HandleUserOAuthTokenStatusChange(user_id, status);
976 return status;
978 return User::OAUTH_TOKEN_STATUS_UNKNOWN;
981 bool UserManagerBase::LoadForceOnlineSignin(const std::string& user_id) const {
982 DCHECK(task_runner_->RunsTasksOnCurrentThread());
984 const base::DictionaryValue* prefs_force_online =
985 GetLocalState()->GetDictionary(kUserForceOnlineSignin);
986 bool force_online_signin = false;
987 if (prefs_force_online) {
988 prefs_force_online->GetBooleanWithoutPathExpansion(user_id,
989 &force_online_signin);
991 return force_online_signin;
994 void UserManagerBase::RemoveNonCryptohomeData(const std::string& user_id) {
995 PrefService* prefs = GetLocalState();
996 DictionaryPrefUpdate prefs_display_name_update(prefs, kUserDisplayName);
997 prefs_display_name_update->RemoveWithoutPathExpansion(user_id, NULL);
999 DictionaryPrefUpdate prefs_given_name_update(prefs, kUserGivenName);
1000 prefs_given_name_update->RemoveWithoutPathExpansion(user_id, NULL);
1002 DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail);
1003 prefs_display_email_update->RemoveWithoutPathExpansion(user_id, NULL);
1005 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus);
1006 prefs_oauth_update->RemoveWithoutPathExpansion(user_id, NULL);
1008 DictionaryPrefUpdate prefs_force_online_update(prefs, kUserForceOnlineSignin);
1009 prefs_force_online_update->RemoveWithoutPathExpansion(user_id, NULL);
1011 RemoveKnownUserPrefs(user_id);
1013 std::string last_active_user = GetLocalState()->GetString(kLastActiveUser);
1014 if (user_id == last_active_user)
1015 GetLocalState()->SetString(kLastActiveUser, std::string());
1018 bool UserManagerBase::FindKnownUserPrefs(
1019 const UserID& user_id,
1020 const base::DictionaryValue** out_value) {
1021 PrefService* local_state = GetLocalState();
1023 // Local State may not be initialized in tests.
1024 if (!local_state)
1025 return false;
1026 if (IsUserNonCryptohomeDataEphemeral(user_id))
1027 return false;
1029 const base::ListValue* known_users = local_state->GetList(kKnownUsers);
1030 for (size_t i = 0; i < known_users->GetSize(); ++i) {
1031 const base::DictionaryValue* element = nullptr;
1032 if (known_users->GetDictionary(i, &element)) {
1033 if (UserMatches(user_id, *element)) {
1034 known_users->GetDictionary(i, out_value);
1035 return true;
1039 return false;
1042 void UserManagerBase::UpdateKnownUserPrefs(const UserID& user_id,
1043 const base::DictionaryValue& values,
1044 bool clear) {
1045 PrefService* local_state = GetLocalState();
1047 // Local State may not be initialized in tests.
1048 if (!local_state)
1049 return;
1051 if (IsUserNonCryptohomeDataEphemeral(user_id))
1052 return;
1054 ListPrefUpdate update(local_state, kKnownUsers);
1055 for (size_t i = 0; i < update->GetSize(); ++i) {
1056 base::DictionaryValue* element = nullptr;
1057 if (update->GetDictionary(i, &element)) {
1058 if (UserMatches(user_id, *element)) {
1059 if (clear)
1060 element->Clear();
1061 element->MergeDictionary(&values);
1062 UpdateIdentity(user_id, *element);
1063 return;
1067 scoped_ptr<base::DictionaryValue> new_value(new base::DictionaryValue());
1068 new_value->MergeDictionary(&values);
1069 UpdateIdentity(user_id, *new_value);
1070 update->Append(new_value.release());
1073 bool UserManagerBase::GetKnownUserStringPref(const UserID& user_id,
1074 const std::string& path,
1075 std::string* out_value) {
1076 const base::DictionaryValue* user_pref_dict = nullptr;
1077 if (!FindKnownUserPrefs(user_id, &user_pref_dict))
1078 return false;
1080 return user_pref_dict->GetString(path, out_value);
1083 void UserManagerBase::SetKnownUserStringPref(const UserID& user_id,
1084 const std::string& path,
1085 const std::string& in_value) {
1086 PrefService* local_state = GetLocalState();
1088 // Local State may not be initialized in tests.
1089 if (!local_state)
1090 return;
1092 ListPrefUpdate update(local_state, kKnownUsers);
1093 base::DictionaryValue dict;
1094 dict.SetString(path, in_value);
1095 UpdateKnownUserPrefs(user_id, dict, false);
1098 bool UserManagerBase::GetKnownUserBooleanPref(const UserID& user_id,
1099 const std::string& path,
1100 bool* out_value) {
1101 const base::DictionaryValue* user_pref_dict = nullptr;
1102 if (!FindKnownUserPrefs(user_id, &user_pref_dict))
1103 return false;
1105 return user_pref_dict->GetBoolean(path, out_value);
1108 void UserManagerBase::SetKnownUserBooleanPref(const UserID& user_id,
1109 const std::string& path,
1110 const bool in_value) {
1111 PrefService* local_state = GetLocalState();
1113 // Local State may not be initialized in tests.
1114 if (!local_state)
1115 return;
1117 ListPrefUpdate update(local_state, kKnownUsers);
1118 base::DictionaryValue dict;
1119 dict.SetBoolean(path, in_value);
1120 UpdateKnownUserPrefs(user_id, dict, false);
1123 bool UserManagerBase::GetKnownUserIntegerPref(const UserID& user_id,
1124 const std::string& path,
1125 int* out_value) {
1126 const base::DictionaryValue* user_pref_dict = nullptr;
1127 if (!FindKnownUserPrefs(user_id, &user_pref_dict))
1128 return false;
1129 return user_pref_dict->GetInteger(path, out_value);
1132 void UserManagerBase::SetKnownUserIntegerPref(const UserID& user_id,
1133 const std::string& path,
1134 const int in_value) {
1135 PrefService* local_state = GetLocalState();
1137 // Local State may not be initialized in tests.
1138 if (!local_state)
1139 return;
1141 ListPrefUpdate update(local_state, kKnownUsers);
1142 base::DictionaryValue dict;
1143 dict.SetInteger(path, in_value);
1144 UpdateKnownUserPrefs(user_id, dict, false);
1147 void UserManagerBase::UpdateGaiaID(const UserID& user_id,
1148 const std::string& gaia_id) {
1149 SetKnownUserStringPref(user_id, kGAIAIdKey, gaia_id);
1152 bool UserManagerBase::FindGaiaID(const UserID& user_id,
1153 std::string* out_value) {
1154 return GetKnownUserStringPref(user_id, kGAIAIdKey, out_value);
1157 void UserManagerBase::SetKnownUserDeviceId(const UserID& user_id,
1158 const std::string& device_id) {
1159 const std::string known_device_id = GetKnownUserDeviceId(user_id);
1160 if (!known_device_id.empty() && device_id != known_device_id) {
1161 NOTREACHED() << "Trying to change device ID for known user.";
1163 SetKnownUserStringPref(user_id, kDeviceId, device_id);
1166 std::string UserManagerBase::GetKnownUserDeviceId(const UserID& user_id) {
1167 std::string device_id;
1168 if (GetKnownUserStringPref(user_id, kDeviceId, &device_id)) {
1169 return device_id;
1171 return std::string();
1174 void UserManagerBase::SetKnownUserGAPSCookie(const UserID& user_id,
1175 const std::string& gaps_cookie) {
1176 SetKnownUserStringPref(user_id, kGAPSCookie, gaps_cookie);
1179 std::string UserManagerBase::GetKnownUserGAPSCookie(const UserID& user_id) {
1180 std::string gaps_cookie;
1181 if (GetKnownUserStringPref(user_id, kGAPSCookie, &gaps_cookie)) {
1182 return gaps_cookie;
1184 return std::string();
1187 User* UserManagerBase::RemoveRegularOrSupervisedUserFromList(
1188 const std::string& user_id) {
1189 ListPrefUpdate prefs_users_update(GetLocalState(), kRegularUsers);
1190 prefs_users_update->Clear();
1191 User* user = NULL;
1192 for (UserList::iterator it = users_.begin(); it != users_.end();) {
1193 const std::string user_email = (*it)->email();
1194 if (user_email == user_id) {
1195 user = *it;
1196 it = users_.erase(it);
1197 } else {
1198 if ((*it)->HasGaiaAccount() || (*it)->IsSupervised())
1199 prefs_users_update->Append(new base::StringValue(user_email));
1200 ++it;
1203 return user;
1206 void UserManagerBase::RemoveKnownUserPrefs(const UserID& user_id) {
1207 ListPrefUpdate update(GetLocalState(), kKnownUsers);
1208 for (size_t i = 0; i < update->GetSize(); ++i) {
1209 base::DictionaryValue* element = nullptr;
1210 if (update->GetDictionary(i, &element)) {
1211 if (UserMatches(user_id, *element)) {
1212 update->Remove(i, nullptr);
1213 break;
1219 void UserManagerBase::NotifyActiveUserChanged(const User* active_user) {
1220 DCHECK(task_runner_->RunsTasksOnCurrentThread());
1221 FOR_EACH_OBSERVER(UserManager::UserSessionStateObserver,
1222 session_state_observer_list_,
1223 ActiveUserChanged(active_user));
1226 void UserManagerBase::NotifyUserAddedToSession(const User* added_user,
1227 bool user_switch_pending) {
1228 DCHECK(task_runner_->RunsTasksOnCurrentThread());
1229 FOR_EACH_OBSERVER(UserManager::UserSessionStateObserver,
1230 session_state_observer_list_,
1231 UserAddedToSession(added_user));
1234 void UserManagerBase::NotifyActiveUserHashChanged(const std::string& hash) {
1235 DCHECK(task_runner_->RunsTasksOnCurrentThread());
1236 FOR_EACH_OBSERVER(UserManager::UserSessionStateObserver,
1237 session_state_observer_list_,
1238 ActiveUserHashChanged(hash));
1241 void UserManagerBase::ChangeUserChildStatus(User* user, bool is_child) {
1242 DCHECK(task_runner_->RunsTasksOnCurrentThread());
1243 if (user->IsSupervised() == is_child)
1244 return;
1245 user->SetIsChild(is_child);
1246 SaveUserType(user->email(), is_child ? user_manager::USER_TYPE_CHILD
1247 : user_manager::USER_TYPE_REGULAR);
1248 FOR_EACH_OBSERVER(UserManager::UserSessionStateObserver,
1249 session_state_observer_list_,
1250 UserChangedChildStatus(user));
1253 void UserManagerBase::UpdateLoginState() {
1254 if (!chromeos::LoginState::IsInitialized())
1255 return; // LoginState may not be initialized in tests.
1257 chromeos::LoginState::LoggedInState logged_in_state;
1258 logged_in_state = active_user_ ? chromeos::LoginState::LOGGED_IN_ACTIVE
1259 : chromeos::LoginState::LOGGED_IN_NONE;
1261 chromeos::LoginState::LoggedInUserType login_user_type;
1262 if (logged_in_state == chromeos::LoginState::LOGGED_IN_NONE)
1263 login_user_type = chromeos::LoginState::LOGGED_IN_USER_NONE;
1264 else if (is_current_user_owner_)
1265 login_user_type = chromeos::LoginState::LOGGED_IN_USER_OWNER;
1266 else if (active_user_->GetType() == USER_TYPE_GUEST)
1267 login_user_type = chromeos::LoginState::LOGGED_IN_USER_GUEST;
1268 else if (active_user_->GetType() == USER_TYPE_PUBLIC_ACCOUNT)
1269 login_user_type = chromeos::LoginState::LOGGED_IN_USER_PUBLIC_ACCOUNT;
1270 else if (active_user_->GetType() == USER_TYPE_SUPERVISED)
1271 login_user_type = chromeos::LoginState::LOGGED_IN_USER_SUPERVISED;
1272 else if (active_user_->GetType() == USER_TYPE_KIOSK_APP)
1273 login_user_type = chromeos::LoginState::LOGGED_IN_USER_KIOSK_APP;
1274 else
1275 login_user_type = chromeos::LoginState::LOGGED_IN_USER_REGULAR;
1277 if (primary_user_) {
1278 chromeos::LoginState::Get()->SetLoggedInStateAndPrimaryUser(
1279 logged_in_state, login_user_type, primary_user_->username_hash());
1280 } else {
1281 chromeos::LoginState::Get()->SetLoggedInState(logged_in_state,
1282 login_user_type);
1286 void UserManagerBase::SetLRUUser(User* user) {
1287 GetLocalState()->SetString(kLastActiveUser, user->email());
1288 GetLocalState()->CommitPendingWrite();
1290 UserList::iterator it =
1291 std::find(lru_logged_in_users_.begin(), lru_logged_in_users_.end(), user);
1292 if (it != lru_logged_in_users_.end())
1293 lru_logged_in_users_.erase(it);
1294 lru_logged_in_users_.insert(lru_logged_in_users_.begin(), user);
1297 void UserManagerBase::SendGaiaUserLoginMetrics(const std::string& user_id) {
1298 // If this isn't the first time Chrome was run after the system booted,
1299 // assume that Chrome was restarted because a previous session ended.
1300 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
1301 chromeos::switches::kFirstExecAfterBoot)) {
1302 const std::string last_email =
1303 GetLocalState()->GetString(kLastLoggedInGaiaUser);
1304 const base::TimeDelta time_to_login =
1305 base::TimeTicks::Now() - manager_creation_time_;
1306 if (!last_email.empty() && user_id != last_email &&
1307 time_to_login.InSeconds() <= kLogoutToLoginDelayMaxSec) {
1308 UMA_HISTOGRAM_CUSTOM_COUNTS("UserManager.LogoutToLoginDelay",
1309 time_to_login.InSeconds(),
1311 kLogoutToLoginDelayMaxSec,
1312 50);
1317 void UserManagerBase::UpdateUserAccountLocale(const std::string& user_id,
1318 const std::string& locale) {
1319 scoped_ptr<std::string> resolved_locale(new std::string());
1320 if (!locale.empty() && locale != GetApplicationLocale()) {
1321 // base::Pased will NULL out |resolved_locale|, so cache the underlying ptr.
1322 std::string* raw_resolved_locale = resolved_locale.get();
1323 blocking_task_runner_->PostTaskAndReply(
1324 FROM_HERE,
1325 base::Bind(ResolveLocale,
1326 locale,
1327 base::Unretained(raw_resolved_locale)),
1328 base::Bind(&UserManagerBase::DoUpdateAccountLocale,
1329 weak_factory_.GetWeakPtr(),
1330 user_id,
1331 base::Passed(&resolved_locale)));
1332 } else {
1333 resolved_locale.reset(new std::string(locale));
1334 DoUpdateAccountLocale(user_id, resolved_locale.Pass());
1338 void UserManagerBase::DoUpdateAccountLocale(
1339 const std::string& user_id,
1340 scoped_ptr<std::string> resolved_locale) {
1341 User* user = FindUserAndModify(user_id);
1342 if (user && resolved_locale)
1343 user->SetAccountLocale(*resolved_locale);
1346 void UserManagerBase::DeleteUser(User* user) {
1347 const bool is_active_user = (user == active_user_);
1348 delete user;
1349 if (is_active_user)
1350 active_user_ = NULL;
1353 } // namespace user_manager