Infobar material design refresh: bg color
[chromium-blink-merge.git] / components / user_manager / user_manager_base.cc
blob4dcf5212ab23fb3a3d379221e688d1ccff452fe4
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 OnUserRemoved(user_id);
385 } else {
386 NOTREACHED() << "Users are not loaded yet.";
387 return;
390 // Make sure that new data is persisted to Local State.
391 GetLocalState()->CommitPendingWrite();
394 bool UserManagerBase::IsKnownUser(const std::string& user_id) const {
395 return FindUser(user_id) != NULL;
398 const User* UserManagerBase::FindUser(const std::string& user_id) const {
399 DCHECK(task_runner_->RunsTasksOnCurrentThread());
400 if (active_user_ && active_user_->email() == user_id)
401 return active_user_;
402 return FindUserInList(user_id);
405 User* UserManagerBase::FindUserAndModify(const std::string& user_id) {
406 DCHECK(task_runner_->RunsTasksOnCurrentThread());
407 if (active_user_ && active_user_->email() == user_id)
408 return active_user_;
409 return FindUserInListAndModify(user_id);
412 const User* UserManagerBase::GetLoggedInUser() const {
413 DCHECK(task_runner_->RunsTasksOnCurrentThread());
414 return active_user_;
417 User* UserManagerBase::GetLoggedInUser() {
418 DCHECK(task_runner_->RunsTasksOnCurrentThread());
419 return active_user_;
422 const User* UserManagerBase::GetActiveUser() const {
423 DCHECK(task_runner_->RunsTasksOnCurrentThread());
424 return active_user_;
427 User* UserManagerBase::GetActiveUser() {
428 DCHECK(task_runner_->RunsTasksOnCurrentThread());
429 return active_user_;
432 const User* UserManagerBase::GetPrimaryUser() const {
433 DCHECK(task_runner_->RunsTasksOnCurrentThread());
434 return primary_user_;
437 void UserManagerBase::SaveUserOAuthStatus(
438 const std::string& user_id,
439 User::OAuthTokenStatus oauth_token_status) {
440 DCHECK(task_runner_->RunsTasksOnCurrentThread());
442 DVLOG(1) << "Saving user OAuth token status in Local State";
443 User* user = FindUserAndModify(user_id);
444 if (user)
445 user->set_oauth_token_status(oauth_token_status);
447 // Do not update local state if data stored or cached outside the user's
448 // cryptohome is to be treated as ephemeral.
449 if (IsUserNonCryptohomeDataEphemeral(user_id))
450 return;
452 DictionaryPrefUpdate oauth_status_update(GetLocalState(),
453 kUserOAuthTokenStatus);
454 oauth_status_update->SetWithoutPathExpansion(
455 user_id,
456 new base::FundamentalValue(static_cast<int>(oauth_token_status)));
459 void UserManagerBase::SaveForceOnlineSignin(const std::string& user_id,
460 bool force_online_signin) {
461 DCHECK(task_runner_->RunsTasksOnCurrentThread());
463 // Do not update local state if data stored or cached outside the user's
464 // cryptohome is to be treated as ephemeral.
465 if (IsUserNonCryptohomeDataEphemeral(user_id))
466 return;
468 DictionaryPrefUpdate force_online_update(GetLocalState(),
469 kUserForceOnlineSignin);
470 force_online_update->SetBooleanWithoutPathExpansion(user_id,
471 force_online_signin);
474 void UserManagerBase::SaveUserDisplayName(const std::string& user_id,
475 const base::string16& display_name) {
476 DCHECK(task_runner_->RunsTasksOnCurrentThread());
478 if (User* user = FindUserAndModify(user_id)) {
479 user->set_display_name(display_name);
481 // Do not update local state if data stored or cached outside the user's
482 // cryptohome is to be treated as ephemeral.
483 if (!IsUserNonCryptohomeDataEphemeral(user_id)) {
484 DictionaryPrefUpdate display_name_update(GetLocalState(),
485 kUserDisplayName);
486 display_name_update->SetWithoutPathExpansion(
487 user_id, new base::StringValue(display_name));
492 base::string16 UserManagerBase::GetUserDisplayName(
493 const std::string& user_id) const {
494 const User* user = FindUser(user_id);
495 return user ? user->display_name() : base::string16();
498 void UserManagerBase::SaveUserDisplayEmail(const std::string& user_id,
499 const std::string& display_email) {
500 DCHECK(task_runner_->RunsTasksOnCurrentThread());
502 User* user = FindUserAndModify(user_id);
503 if (!user) {
504 LOG(ERROR) << "User not found: " << user_id;
505 return; // Ignore if there is no such user.
508 user->set_display_email(display_email);
510 // Do not update local state if data stored or cached outside the user's
511 // cryptohome is to be treated as ephemeral.
512 if (IsUserNonCryptohomeDataEphemeral(user_id))
513 return;
515 DictionaryPrefUpdate display_email_update(GetLocalState(), kUserDisplayEmail);
516 display_email_update->SetWithoutPathExpansion(
517 user_id, new base::StringValue(display_email));
520 std::string UserManagerBase::GetUserDisplayEmail(
521 const std::string& user_id) const {
522 const User* user = FindUser(user_id);
523 return user ? user->display_email() : user_id;
526 void UserManagerBase::SaveUserType(const std::string& user_id,
527 const UserType& user_type) {
528 DCHECK(task_runner_->RunsTasksOnCurrentThread());
530 User* user = FindUserAndModify(user_id);
531 if (!user) {
532 LOG(ERROR) << "User not found: " << user_id;
533 return; // Ignore if there is no such user.
536 // Do not update local state if data stored or cached outside the user's
537 // cryptohome is to be treated as ephemeral.
538 if (IsUserNonCryptohomeDataEphemeral(user_id))
539 return;
541 DictionaryPrefUpdate user_type_update(GetLocalState(), kUserType);
542 user_type_update->SetWithoutPathExpansion(
543 user_id, new base::FundamentalValue(static_cast<int>(user_type)));
544 GetLocalState()->CommitPendingWrite();
547 void UserManagerBase::UpdateUsingSAML(const std::string& user_id,
548 const bool using_saml) {
549 SetKnownUserBooleanPref(user_id, kUsingSAMLKey, using_saml);
552 bool UserManagerBase::FindUsingSAML(const std::string& user_id) {
553 bool using_saml;
554 if (GetKnownUserBooleanPref(user_id, kUsingSAMLKey, &using_saml))
555 return using_saml;
556 return false;
559 void UserManagerBase::UpdateReauthReason(const std::string& user_id,
560 const int reauth_reason) {
561 SetKnownUserIntegerPref(user_id, kReauthReasonKey, reauth_reason);
564 bool UserManagerBase::FindReauthReason(const std::string& user_id,
565 int* out_value) {
566 return GetKnownUserIntegerPref(user_id, kReauthReasonKey, out_value);
569 void UserManagerBase::UpdateUserAccountData(
570 const std::string& user_id,
571 const UserAccountData& account_data) {
572 DCHECK(task_runner_->RunsTasksOnCurrentThread());
574 SaveUserDisplayName(user_id, account_data.display_name());
576 if (User* user = FindUserAndModify(user_id)) {
577 base::string16 given_name = account_data.given_name();
578 user->set_given_name(given_name);
579 if (!IsUserNonCryptohomeDataEphemeral(user_id)) {
580 DictionaryPrefUpdate given_name_update(GetLocalState(), kUserGivenName);
581 given_name_update->SetWithoutPathExpansion(
582 user_id, new base::StringValue(given_name));
586 UpdateUserAccountLocale(user_id, account_data.locale());
589 // static
590 void UserManagerBase::ParseUserList(const base::ListValue& users_list,
591 const std::set<std::string>& existing_users,
592 std::vector<std::string>* users_vector,
593 std::set<std::string>* users_set) {
594 users_vector->clear();
595 users_set->clear();
596 for (size_t i = 0; i < users_list.GetSize(); ++i) {
597 std::string email;
598 if (!users_list.GetString(i, &email) || email.empty()) {
599 LOG(ERROR) << "Corrupt entry in user list at index " << i << ".";
600 continue;
602 if (existing_users.find(email) != existing_users.end() ||
603 !users_set->insert(email).second) {
604 LOG(ERROR) << "Duplicate user: " << email;
605 continue;
607 users_vector->push_back(email);
611 bool UserManagerBase::IsCurrentUserOwner() const {
612 DCHECK(task_runner_->RunsTasksOnCurrentThread());
613 base::AutoLock lk(is_current_user_owner_lock_);
614 return is_current_user_owner_;
617 void UserManagerBase::SetCurrentUserIsOwner(bool is_current_user_owner) {
618 DCHECK(task_runner_->RunsTasksOnCurrentThread());
620 base::AutoLock lk(is_current_user_owner_lock_);
621 is_current_user_owner_ = is_current_user_owner;
623 UpdateLoginState();
626 bool UserManagerBase::IsCurrentUserNew() const {
627 DCHECK(task_runner_->RunsTasksOnCurrentThread());
628 return is_current_user_new_;
631 bool UserManagerBase::IsCurrentUserNonCryptohomeDataEphemeral() const {
632 DCHECK(task_runner_->RunsTasksOnCurrentThread());
633 return IsUserLoggedIn() &&
634 IsUserNonCryptohomeDataEphemeral(GetLoggedInUser()->email());
637 bool UserManagerBase::CanCurrentUserLock() const {
638 DCHECK(task_runner_->RunsTasksOnCurrentThread());
639 return IsUserLoggedIn() && active_user_->can_lock();
642 bool UserManagerBase::IsUserLoggedIn() const {
643 DCHECK(task_runner_->RunsTasksOnCurrentThread());
644 return active_user_;
647 bool UserManagerBase::IsLoggedInAsUserWithGaiaAccount() const {
648 DCHECK(task_runner_->RunsTasksOnCurrentThread());
649 return IsUserLoggedIn() && active_user_->HasGaiaAccount();
652 bool UserManagerBase::IsLoggedInAsChildUser() const {
653 DCHECK(task_runner_->RunsTasksOnCurrentThread());
654 return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_CHILD;
657 bool UserManagerBase::IsLoggedInAsPublicAccount() const {
658 DCHECK(task_runner_->RunsTasksOnCurrentThread());
659 return IsUserLoggedIn() &&
660 active_user_->GetType() == USER_TYPE_PUBLIC_ACCOUNT;
663 bool UserManagerBase::IsLoggedInAsGuest() const {
664 DCHECK(task_runner_->RunsTasksOnCurrentThread());
665 return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_GUEST;
668 bool UserManagerBase::IsLoggedInAsSupervisedUser() const {
669 DCHECK(task_runner_->RunsTasksOnCurrentThread());
670 return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_SUPERVISED;
673 bool UserManagerBase::IsLoggedInAsKioskApp() const {
674 DCHECK(task_runner_->RunsTasksOnCurrentThread());
675 return IsUserLoggedIn() && active_user_->GetType() == USER_TYPE_KIOSK_APP;
678 bool UserManagerBase::IsLoggedInAsStub() const {
679 DCHECK(task_runner_->RunsTasksOnCurrentThread());
680 return IsUserLoggedIn() &&
681 active_user_->email() == chromeos::login::kStubUser;
684 bool UserManagerBase::IsSessionStarted() const {
685 DCHECK(task_runner_->RunsTasksOnCurrentThread());
686 return session_started_;
689 bool UserManagerBase::IsUserNonCryptohomeDataEphemeral(
690 const std::string& user_id) const {
691 // Data belonging to the guest and stub users is always ephemeral.
692 if (user_id == chromeos::login::kGuestUserName ||
693 user_id == chromeos::login::kStubUser) {
694 return true;
697 // Data belonging to the owner, anyone found on the user list and obsolete
698 // public accounts whose data has not been removed yet is not ephemeral.
699 if (user_id == GetOwnerEmail() || UserExistsInList(user_id) ||
700 IsPublicAccountMarkedForRemoval(user_id)) {
701 return false;
704 // Data belonging to the currently logged-in user is ephemeral when:
705 // a) The user logged into a regular gaia account while the ephemeral users
706 // policy was enabled.
707 // - or -
708 // b) The user logged into any other account type.
709 if (IsUserLoggedIn() && (user_id == GetLoggedInUser()->email()) &&
710 (is_current_user_ephemeral_regular_user_ ||
711 !IsLoggedInAsUserWithGaiaAccount())) {
712 return true;
715 // Data belonging to any other user is ephemeral when:
716 // a) Going through the regular login flow and the ephemeral users policy is
717 // enabled.
718 // - or -
719 // b) The browser is restarting after a crash.
720 return AreEphemeralUsersEnabled() ||
721 session_manager::SessionManager::HasBrowserRestarted();
724 void UserManagerBase::AddObserver(UserManager::Observer* obs) {
725 DCHECK(task_runner_->RunsTasksOnCurrentThread());
726 observer_list_.AddObserver(obs);
729 void UserManagerBase::RemoveObserver(UserManager::Observer* obs) {
730 DCHECK(task_runner_->RunsTasksOnCurrentThread());
731 observer_list_.RemoveObserver(obs);
734 void UserManagerBase::AddSessionStateObserver(
735 UserManager::UserSessionStateObserver* obs) {
736 DCHECK(task_runner_->RunsTasksOnCurrentThread());
737 session_state_observer_list_.AddObserver(obs);
740 void UserManagerBase::RemoveSessionStateObserver(
741 UserManager::UserSessionStateObserver* obs) {
742 DCHECK(task_runner_->RunsTasksOnCurrentThread());
743 session_state_observer_list_.RemoveObserver(obs);
746 void UserManagerBase::NotifyLocalStateChanged() {
747 DCHECK(task_runner_->RunsTasksOnCurrentThread());
748 FOR_EACH_OBSERVER(
749 UserManager::Observer, observer_list_, LocalStateChanged(this));
752 bool UserManagerBase::CanUserBeRemoved(const User* user) const {
753 // Only regular and supervised users are allowed to be manually removed.
754 if (!user || !(user->HasGaiaAccount() || user->IsSupervised()))
755 return false;
757 // Sanity check: we must not remove single user unless it's an enterprise
758 // device. This check may seem redundant at a first sight because
759 // this single user must be an owner and we perform special check later
760 // in order not to remove an owner. However due to non-instant nature of
761 // ownership assignment this later check may sometimes fail.
762 // See http://crosbug.com/12723
763 if (users_.size() < 2 && !IsEnterpriseManaged())
764 return false;
766 // Sanity check: do not allow any of the the logged in users to be removed.
767 for (UserList::const_iterator it = logged_in_users_.begin();
768 it != logged_in_users_.end();
769 ++it) {
770 if ((*it)->email() == user->email())
771 return false;
774 return true;
777 bool UserManagerBase::GetEphemeralUsersEnabled() const {
778 return ephemeral_users_enabled_;
781 void UserManagerBase::SetEphemeralUsersEnabled(bool enabled) {
782 ephemeral_users_enabled_ = enabled;
785 void UserManagerBase::SetIsCurrentUserNew(bool is_new) {
786 is_current_user_new_ = is_new;
789 bool UserManagerBase::HasPendingBootstrap(const std::string& user_id) const {
790 return false;
793 void UserManagerBase::SetOwnerEmail(std::string owner_user_id) {
794 owner_email_ = owner_user_id;
797 const std::string& UserManagerBase::GetPendingUserSwitchID() const {
798 return pending_user_switch_;
801 void UserManagerBase::SetPendingUserSwitchID(std::string user_id) {
802 pending_user_switch_ = user_id;
805 void UserManagerBase::EnsureUsersLoaded() {
806 DCHECK(task_runner_->RunsTasksOnCurrentThread());
807 if (!GetLocalState())
808 return;
810 if (user_loading_stage_ != STAGE_NOT_LOADED)
811 return;
812 user_loading_stage_ = STAGE_LOADING;
814 PerformPreUserListLoadingActions();
816 PrefService* local_state = GetLocalState();
817 const base::ListValue* prefs_regular_users =
818 local_state->GetList(kRegularUsers);
820 const base::DictionaryValue* prefs_display_names =
821 local_state->GetDictionary(kUserDisplayName);
822 const base::DictionaryValue* prefs_given_names =
823 local_state->GetDictionary(kUserGivenName);
824 const base::DictionaryValue* prefs_display_emails =
825 local_state->GetDictionary(kUserDisplayEmail);
826 const base::DictionaryValue* prefs_user_types =
827 local_state->GetDictionary(kUserType);
829 // Load public sessions first.
830 std::set<std::string> public_sessions_set;
831 LoadPublicAccounts(&public_sessions_set);
833 // Load regular users and supervised users.
834 std::vector<std::string> regular_users;
835 std::set<std::string> regular_users_set;
836 ParseUserList(*prefs_regular_users,
837 public_sessions_set,
838 &regular_users,
839 &regular_users_set);
840 for (std::vector<std::string>::const_iterator it = regular_users.begin();
841 it != regular_users.end();
842 ++it) {
843 User* user = NULL;
844 const std::string domain = gaia::ExtractDomainName(*it);
845 if (domain == chromeos::login::kSupervisedUserDomain) {
846 user = User::CreateSupervisedUser(*it);
847 } else {
848 user = User::CreateRegularUser(*it);
849 int user_type;
850 if (prefs_user_types->GetIntegerWithoutPathExpansion(*it, &user_type) &&
851 user_type == USER_TYPE_CHILD) {
852 ChangeUserChildStatus(user, true /* is child */);
855 user->set_oauth_token_status(LoadUserOAuthStatus(*it));
856 user->set_force_online_signin(LoadForceOnlineSignin(*it));
857 user->set_using_saml(FindUsingSAML(*it));
858 users_.push_back(user);
860 base::string16 display_name;
861 if (prefs_display_names->GetStringWithoutPathExpansion(*it,
862 &display_name)) {
863 user->set_display_name(display_name);
866 base::string16 given_name;
867 if (prefs_given_names->GetStringWithoutPathExpansion(*it, &given_name)) {
868 user->set_given_name(given_name);
871 std::string display_email;
872 if (prefs_display_emails->GetStringWithoutPathExpansion(*it,
873 &display_email)) {
874 user->set_display_email(display_email);
878 user_loading_stage_ = STAGE_LOADED;
880 PerformPostUserListLoadingActions();
883 UserList& UserManagerBase::GetUsersAndModify() {
884 EnsureUsersLoaded();
885 return users_;
888 const User* UserManagerBase::FindUserInList(const std::string& user_id) const {
889 const UserList& users = GetUsers();
890 for (UserList::const_iterator it = users.begin(); it != users.end(); ++it) {
891 if ((*it)->email() == user_id)
892 return *it;
894 return NULL;
897 bool UserManagerBase::UserExistsInList(const std::string& user_id) const {
898 const base::ListValue* user_list = GetLocalState()->GetList(kRegularUsers);
899 for (size_t i = 0; i < user_list->GetSize(); ++i) {
900 std::string email;
901 if (user_list->GetString(i, &email) && (user_id == email))
902 return true;
904 return false;
907 User* UserManagerBase::FindUserInListAndModify(const std::string& user_id) {
908 UserList& users = GetUsersAndModify();
909 for (UserList::iterator it = users.begin(); it != users.end(); ++it) {
910 if ((*it)->email() == user_id)
911 return *it;
913 return NULL;
916 void UserManagerBase::GuestUserLoggedIn() {
917 DCHECK(task_runner_->RunsTasksOnCurrentThread());
918 active_user_ = User::CreateGuestUser();
921 void UserManagerBase::AddUserRecord(User* user) {
922 // Add the user to the front of the user list.
923 ListPrefUpdate prefs_users_update(GetLocalState(), kRegularUsers);
924 prefs_users_update->Insert(0, new base::StringValue(user->email()));
925 users_.insert(users_.begin(), user);
928 void UserManagerBase::RegularUserLoggedIn(const std::string& user_id) {
929 // Remove the user from the user list.
930 active_user_ = RemoveRegularOrSupervisedUserFromList(user_id);
932 // If the user was not found on the user list, create a new user.
933 SetIsCurrentUserNew(!active_user_);
934 if (IsCurrentUserNew()) {
935 active_user_ = User::CreateRegularUser(user_id);
936 active_user_->set_oauth_token_status(LoadUserOAuthStatus(user_id));
937 SaveUserDisplayName(active_user_->email(),
938 base::UTF8ToUTF16(active_user_->GetAccountName(true)));
941 AddUserRecord(active_user_);
943 // Make sure that new data is persisted to Local State.
944 GetLocalState()->CommitPendingWrite();
947 void UserManagerBase::RegularUserLoggedInAsEphemeral(
948 const std::string& user_id) {
949 DCHECK(task_runner_->RunsTasksOnCurrentThread());
950 SetIsCurrentUserNew(true);
951 is_current_user_ephemeral_regular_user_ = true;
952 active_user_ = User::CreateRegularUser(user_id);
955 void UserManagerBase::NotifyOnLogin() {
956 DCHECK(task_runner_->RunsTasksOnCurrentThread());
958 NotifyActiveUserHashChanged(active_user_->username_hash());
959 NotifyActiveUserChanged(active_user_);
960 UpdateLoginState();
963 User::OAuthTokenStatus UserManagerBase::LoadUserOAuthStatus(
964 const std::string& user_id) const {
965 DCHECK(task_runner_->RunsTasksOnCurrentThread());
967 const base::DictionaryValue* prefs_oauth_status =
968 GetLocalState()->GetDictionary(kUserOAuthTokenStatus);
969 int oauth_token_status = User::OAUTH_TOKEN_STATUS_UNKNOWN;
970 if (prefs_oauth_status &&
971 prefs_oauth_status->GetIntegerWithoutPathExpansion(user_id,
972 &oauth_token_status)) {
973 User::OAuthTokenStatus status =
974 static_cast<User::OAuthTokenStatus>(oauth_token_status);
975 HandleUserOAuthTokenStatusChange(user_id, status);
977 return status;
979 return User::OAUTH_TOKEN_STATUS_UNKNOWN;
982 bool UserManagerBase::LoadForceOnlineSignin(const std::string& user_id) const {
983 DCHECK(task_runner_->RunsTasksOnCurrentThread());
985 const base::DictionaryValue* prefs_force_online =
986 GetLocalState()->GetDictionary(kUserForceOnlineSignin);
987 bool force_online_signin = false;
988 if (prefs_force_online) {
989 prefs_force_online->GetBooleanWithoutPathExpansion(user_id,
990 &force_online_signin);
992 return force_online_signin;
995 void UserManagerBase::RemoveNonCryptohomeData(const std::string& user_id) {
996 PrefService* prefs = GetLocalState();
997 DictionaryPrefUpdate prefs_display_name_update(prefs, kUserDisplayName);
998 prefs_display_name_update->RemoveWithoutPathExpansion(user_id, NULL);
1000 DictionaryPrefUpdate prefs_given_name_update(prefs, kUserGivenName);
1001 prefs_given_name_update->RemoveWithoutPathExpansion(user_id, NULL);
1003 DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail);
1004 prefs_display_email_update->RemoveWithoutPathExpansion(user_id, NULL);
1006 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus);
1007 prefs_oauth_update->RemoveWithoutPathExpansion(user_id, NULL);
1009 DictionaryPrefUpdate prefs_force_online_update(prefs, kUserForceOnlineSignin);
1010 prefs_force_online_update->RemoveWithoutPathExpansion(user_id, NULL);
1012 RemoveKnownUserPrefs(user_id);
1014 std::string last_active_user = GetLocalState()->GetString(kLastActiveUser);
1015 if (user_id == last_active_user)
1016 GetLocalState()->SetString(kLastActiveUser, std::string());
1019 bool UserManagerBase::FindKnownUserPrefs(
1020 const UserID& user_id,
1021 const base::DictionaryValue** out_value) {
1022 PrefService* local_state = GetLocalState();
1024 // Local State may not be initialized in tests.
1025 if (!local_state)
1026 return false;
1027 if (IsUserNonCryptohomeDataEphemeral(user_id))
1028 return false;
1030 const base::ListValue* known_users = local_state->GetList(kKnownUsers);
1031 for (size_t i = 0; i < known_users->GetSize(); ++i) {
1032 const base::DictionaryValue* element = nullptr;
1033 if (known_users->GetDictionary(i, &element)) {
1034 if (UserMatches(user_id, *element)) {
1035 known_users->GetDictionary(i, out_value);
1036 return true;
1040 return false;
1043 void UserManagerBase::UpdateKnownUserPrefs(const UserID& user_id,
1044 const base::DictionaryValue& values,
1045 bool clear) {
1046 PrefService* local_state = GetLocalState();
1048 // Local State may not be initialized in tests.
1049 if (!local_state)
1050 return;
1052 if (IsUserNonCryptohomeDataEphemeral(user_id))
1053 return;
1055 ListPrefUpdate update(local_state, kKnownUsers);
1056 for (size_t i = 0; i < update->GetSize(); ++i) {
1057 base::DictionaryValue* element = nullptr;
1058 if (update->GetDictionary(i, &element)) {
1059 if (UserMatches(user_id, *element)) {
1060 if (clear)
1061 element->Clear();
1062 element->MergeDictionary(&values);
1063 UpdateIdentity(user_id, *element);
1064 return;
1068 scoped_ptr<base::DictionaryValue> new_value(new base::DictionaryValue());
1069 new_value->MergeDictionary(&values);
1070 UpdateIdentity(user_id, *new_value);
1071 update->Append(new_value.release());
1074 bool UserManagerBase::GetKnownUserStringPref(const UserID& user_id,
1075 const std::string& path,
1076 std::string* out_value) {
1077 const base::DictionaryValue* user_pref_dict = nullptr;
1078 if (!FindKnownUserPrefs(user_id, &user_pref_dict))
1079 return false;
1081 return user_pref_dict->GetString(path, out_value);
1084 void UserManagerBase::SetKnownUserStringPref(const UserID& user_id,
1085 const std::string& path,
1086 const std::string& in_value) {
1087 PrefService* local_state = GetLocalState();
1089 // Local State may not be initialized in tests.
1090 if (!local_state)
1091 return;
1093 ListPrefUpdate update(local_state, kKnownUsers);
1094 base::DictionaryValue dict;
1095 dict.SetString(path, in_value);
1096 UpdateKnownUserPrefs(user_id, dict, false);
1099 bool UserManagerBase::GetKnownUserBooleanPref(const UserID& user_id,
1100 const std::string& path,
1101 bool* out_value) {
1102 const base::DictionaryValue* user_pref_dict = nullptr;
1103 if (!FindKnownUserPrefs(user_id, &user_pref_dict))
1104 return false;
1106 return user_pref_dict->GetBoolean(path, out_value);
1109 void UserManagerBase::SetKnownUserBooleanPref(const UserID& user_id,
1110 const std::string& path,
1111 const bool in_value) {
1112 PrefService* local_state = GetLocalState();
1114 // Local State may not be initialized in tests.
1115 if (!local_state)
1116 return;
1118 ListPrefUpdate update(local_state, kKnownUsers);
1119 base::DictionaryValue dict;
1120 dict.SetBoolean(path, in_value);
1121 UpdateKnownUserPrefs(user_id, dict, false);
1124 bool UserManagerBase::GetKnownUserIntegerPref(const UserID& user_id,
1125 const std::string& path,
1126 int* out_value) {
1127 const base::DictionaryValue* user_pref_dict = nullptr;
1128 if (!FindKnownUserPrefs(user_id, &user_pref_dict))
1129 return false;
1130 return user_pref_dict->GetInteger(path, out_value);
1133 void UserManagerBase::SetKnownUserIntegerPref(const UserID& user_id,
1134 const std::string& path,
1135 const int in_value) {
1136 PrefService* local_state = GetLocalState();
1138 // Local State may not be initialized in tests.
1139 if (!local_state)
1140 return;
1142 ListPrefUpdate update(local_state, kKnownUsers);
1143 base::DictionaryValue dict;
1144 dict.SetInteger(path, in_value);
1145 UpdateKnownUserPrefs(user_id, dict, false);
1148 void UserManagerBase::UpdateGaiaID(const UserID& user_id,
1149 const std::string& gaia_id) {
1150 SetKnownUserStringPref(user_id, kGAIAIdKey, gaia_id);
1153 bool UserManagerBase::FindGaiaID(const UserID& user_id,
1154 std::string* out_value) {
1155 return GetKnownUserStringPref(user_id, kGAIAIdKey, out_value);
1158 void UserManagerBase::SetKnownUserDeviceId(const UserID& user_id,
1159 const std::string& device_id) {
1160 const std::string known_device_id = GetKnownUserDeviceId(user_id);
1161 if (!known_device_id.empty() && device_id != known_device_id) {
1162 NOTREACHED() << "Trying to change device ID for known user.";
1164 SetKnownUserStringPref(user_id, kDeviceId, device_id);
1167 std::string UserManagerBase::GetKnownUserDeviceId(const UserID& user_id) {
1168 std::string device_id;
1169 if (GetKnownUserStringPref(user_id, kDeviceId, &device_id)) {
1170 return device_id;
1172 return std::string();
1175 void UserManagerBase::SetKnownUserGAPSCookie(const UserID& user_id,
1176 const std::string& gaps_cookie) {
1177 SetKnownUserStringPref(user_id, kGAPSCookie, gaps_cookie);
1180 std::string UserManagerBase::GetKnownUserGAPSCookie(const UserID& user_id) {
1181 std::string gaps_cookie;
1182 if (GetKnownUserStringPref(user_id, kGAPSCookie, &gaps_cookie)) {
1183 return gaps_cookie;
1185 return std::string();
1188 User* UserManagerBase::RemoveRegularOrSupervisedUserFromList(
1189 const std::string& user_id) {
1190 ListPrefUpdate prefs_users_update(GetLocalState(), kRegularUsers);
1191 prefs_users_update->Clear();
1192 User* user = NULL;
1193 for (UserList::iterator it = users_.begin(); it != users_.end();) {
1194 const std::string user_email = (*it)->email();
1195 if (user_email == user_id) {
1196 user = *it;
1197 it = users_.erase(it);
1198 } else {
1199 if ((*it)->HasGaiaAccount() || (*it)->IsSupervised())
1200 prefs_users_update->Append(new base::StringValue(user_email));
1201 ++it;
1204 OnUserRemoved(user_id);
1205 return user;
1208 void UserManagerBase::RemoveKnownUserPrefs(const UserID& user_id) {
1209 ListPrefUpdate update(GetLocalState(), kKnownUsers);
1210 for (size_t i = 0; i < update->GetSize(); ++i) {
1211 base::DictionaryValue* element = nullptr;
1212 if (update->GetDictionary(i, &element)) {
1213 if (UserMatches(user_id, *element)) {
1214 update->Remove(i, nullptr);
1215 break;
1221 void UserManagerBase::NotifyActiveUserChanged(const User* active_user) {
1222 DCHECK(task_runner_->RunsTasksOnCurrentThread());
1223 FOR_EACH_OBSERVER(UserManager::UserSessionStateObserver,
1224 session_state_observer_list_,
1225 ActiveUserChanged(active_user));
1228 void UserManagerBase::NotifyUserAddedToSession(const User* added_user,
1229 bool user_switch_pending) {
1230 DCHECK(task_runner_->RunsTasksOnCurrentThread());
1231 FOR_EACH_OBSERVER(UserManager::UserSessionStateObserver,
1232 session_state_observer_list_,
1233 UserAddedToSession(added_user));
1236 void UserManagerBase::NotifyActiveUserHashChanged(const std::string& hash) {
1237 DCHECK(task_runner_->RunsTasksOnCurrentThread());
1238 FOR_EACH_OBSERVER(UserManager::UserSessionStateObserver,
1239 session_state_observer_list_,
1240 ActiveUserHashChanged(hash));
1243 void UserManagerBase::ChangeUserChildStatus(User* user, bool is_child) {
1244 DCHECK(task_runner_->RunsTasksOnCurrentThread());
1245 if (user->IsSupervised() == is_child)
1246 return;
1247 user->SetIsChild(is_child);
1248 SaveUserType(user->email(), is_child ? user_manager::USER_TYPE_CHILD
1249 : user_manager::USER_TYPE_REGULAR);
1250 FOR_EACH_OBSERVER(UserManager::UserSessionStateObserver,
1251 session_state_observer_list_,
1252 UserChangedChildStatus(user));
1255 void UserManagerBase::UpdateLoginState() {
1256 if (!chromeos::LoginState::IsInitialized())
1257 return; // LoginState may not be initialized in tests.
1259 chromeos::LoginState::LoggedInState logged_in_state;
1260 logged_in_state = active_user_ ? chromeos::LoginState::LOGGED_IN_ACTIVE
1261 : chromeos::LoginState::LOGGED_IN_NONE;
1263 chromeos::LoginState::LoggedInUserType login_user_type;
1264 if (logged_in_state == chromeos::LoginState::LOGGED_IN_NONE)
1265 login_user_type = chromeos::LoginState::LOGGED_IN_USER_NONE;
1266 else if (is_current_user_owner_)
1267 login_user_type = chromeos::LoginState::LOGGED_IN_USER_OWNER;
1268 else if (active_user_->GetType() == USER_TYPE_GUEST)
1269 login_user_type = chromeos::LoginState::LOGGED_IN_USER_GUEST;
1270 else if (active_user_->GetType() == USER_TYPE_PUBLIC_ACCOUNT)
1271 login_user_type = chromeos::LoginState::LOGGED_IN_USER_PUBLIC_ACCOUNT;
1272 else if (active_user_->GetType() == USER_TYPE_SUPERVISED)
1273 login_user_type = chromeos::LoginState::LOGGED_IN_USER_SUPERVISED;
1274 else if (active_user_->GetType() == USER_TYPE_KIOSK_APP)
1275 login_user_type = chromeos::LoginState::LOGGED_IN_USER_KIOSK_APP;
1276 else
1277 login_user_type = chromeos::LoginState::LOGGED_IN_USER_REGULAR;
1279 if (primary_user_) {
1280 chromeos::LoginState::Get()->SetLoggedInStateAndPrimaryUser(
1281 logged_in_state, login_user_type, primary_user_->username_hash());
1282 } else {
1283 chromeos::LoginState::Get()->SetLoggedInState(logged_in_state,
1284 login_user_type);
1288 void UserManagerBase::SetLRUUser(User* user) {
1289 GetLocalState()->SetString(kLastActiveUser, user->email());
1290 GetLocalState()->CommitPendingWrite();
1292 UserList::iterator it =
1293 std::find(lru_logged_in_users_.begin(), lru_logged_in_users_.end(), user);
1294 if (it != lru_logged_in_users_.end())
1295 lru_logged_in_users_.erase(it);
1296 lru_logged_in_users_.insert(lru_logged_in_users_.begin(), user);
1299 void UserManagerBase::SendGaiaUserLoginMetrics(const std::string& user_id) {
1300 // If this isn't the first time Chrome was run after the system booted,
1301 // assume that Chrome was restarted because a previous session ended.
1302 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
1303 chromeos::switches::kFirstExecAfterBoot)) {
1304 const std::string last_email =
1305 GetLocalState()->GetString(kLastLoggedInGaiaUser);
1306 const base::TimeDelta time_to_login =
1307 base::TimeTicks::Now() - manager_creation_time_;
1308 if (!last_email.empty() && user_id != last_email &&
1309 time_to_login.InSeconds() <= kLogoutToLoginDelayMaxSec) {
1310 UMA_HISTOGRAM_CUSTOM_COUNTS("UserManager.LogoutToLoginDelay",
1311 time_to_login.InSeconds(),
1313 kLogoutToLoginDelayMaxSec,
1314 50);
1319 void UserManagerBase::UpdateUserAccountLocale(const std::string& user_id,
1320 const std::string& locale) {
1321 scoped_ptr<std::string> resolved_locale(new std::string());
1322 if (!locale.empty() && locale != GetApplicationLocale()) {
1323 // base::Pased will NULL out |resolved_locale|, so cache the underlying ptr.
1324 std::string* raw_resolved_locale = resolved_locale.get();
1325 blocking_task_runner_->PostTaskAndReply(
1326 FROM_HERE,
1327 base::Bind(ResolveLocale,
1328 locale,
1329 base::Unretained(raw_resolved_locale)),
1330 base::Bind(&UserManagerBase::DoUpdateAccountLocale,
1331 weak_factory_.GetWeakPtr(),
1332 user_id,
1333 base::Passed(&resolved_locale)));
1334 } else {
1335 resolved_locale.reset(new std::string(locale));
1336 DoUpdateAccountLocale(user_id, resolved_locale.Pass());
1340 void UserManagerBase::DoUpdateAccountLocale(
1341 const std::string& user_id,
1342 scoped_ptr<std::string> resolved_locale) {
1343 User* user = FindUserAndModify(user_id);
1344 if (user && resolved_locale)
1345 user->SetAccountLocale(*resolved_locale);
1348 void UserManagerBase::DeleteUser(User* user) {
1349 const bool is_active_user = (user == active_user_);
1350 delete user;
1351 if (is_active_user)
1352 active_user_ = NULL;
1355 } // namespace user_manager