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"
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
{
40 // A vector pref of the the regular users known on this device, arranged in LRU
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 string pref containing the ID of the last user who logged in if it was
61 // a regular user or an empty string if it was another type of user (guest,
62 // kiosk, public account, etc.).
63 const char kLastLoggedInRegularUser
[] = "LastLoggedInRegularUser";
65 // Upper bound for a histogram metric reporting the amount of time between
66 // one regular user logging out and a different regular user logging in.
67 const int kLogoutToLoginDelayMaxSec
= 1800;
69 // Callback that is called after user removal is complete.
70 void OnRemoveUserComplete(const std::string
& user_email
,
72 cryptohome::MountError return_code
) {
73 // Log the error, but there's not much we can do.
75 LOG(ERROR
) << "Removal of cryptohome for " << user_email
76 << " failed, return code: " << return_code
;
80 // Runs on SequencedWorkerPool thread. Passes resolved locale to UI thread.
81 void ResolveLocale(const std::string
& raw_locale
,
82 std::string
* resolved_locale
) {
83 ignore_result(l10n_util::CheckAndResolveLocale(raw_locale
, resolved_locale
));
89 void UserManagerBase::RegisterPrefs(PrefRegistrySimple
* registry
) {
90 registry
->RegisterListPref(kRegularUsers
);
91 registry
->RegisterStringPref(kLastLoggedInRegularUser
, std::string());
92 registry
->RegisterDictionaryPref(kUserDisplayName
);
93 registry
->RegisterDictionaryPref(kUserGivenName
);
94 registry
->RegisterDictionaryPref(kUserDisplayEmail
);
95 registry
->RegisterDictionaryPref(kUserOAuthTokenStatus
);
96 registry
->RegisterDictionaryPref(kUserForceOnlineSignin
);
99 UserManagerBase::UserManagerBase(
100 scoped_refptr
<base::TaskRunner
> task_runner
,
101 scoped_refptr
<base::TaskRunner
> blocking_task_runner
)
102 : active_user_(NULL
),
104 user_loading_stage_(STAGE_NOT_LOADED
),
105 session_started_(false),
106 is_current_user_owner_(false),
107 is_current_user_new_(false),
108 is_current_user_ephemeral_regular_user_(false),
109 ephemeral_users_enabled_(false),
110 manager_creation_time_(base::TimeTicks::Now()),
111 task_runner_(task_runner
),
112 blocking_task_runner_(blocking_task_runner
),
113 weak_factory_(this) {
117 UserManagerBase::~UserManagerBase() {
118 // Can't use STLDeleteElements because of the private destructor of User.
119 for (UserList::iterator it
= users_
.begin(); it
!= users_
.end();
120 it
= users_
.erase(it
)) {
123 // These are pointers to the same User instances that were in users_ list.
124 logged_in_users_
.clear();
125 lru_logged_in_users_
.clear();
127 DeleteUser(active_user_
);
130 void UserManagerBase::Shutdown() {
131 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
134 const UserList
& UserManagerBase::GetUsers() const {
135 const_cast<UserManagerBase
*>(this)->EnsureUsersLoaded();
139 const UserList
& UserManagerBase::GetLoggedInUsers() const {
140 return logged_in_users_
;
143 const UserList
& UserManagerBase::GetLRULoggedInUsers() const {
144 return lru_logged_in_users_
;
147 const std::string
& UserManagerBase::GetOwnerEmail() const {
151 void UserManagerBase::UserLoggedIn(const std::string
& user_id
,
152 const std::string
& username_hash
,
153 bool browser_restart
) {
154 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
156 User
* user
= FindUserInListAndModify(user_id
);
157 if (active_user_
&& user
) {
158 user
->set_is_logged_in(true);
159 user
->set_username_hash(username_hash
);
160 logged_in_users_
.push_back(user
);
161 lru_logged_in_users_
.push_back(user
);
163 // Reset the new user flag if the user already exists.
164 SetIsCurrentUserNew(false);
165 NotifyUserAddedToSession(user
, true /* user switch pending */);
170 if (user_id
== chromeos::login::kGuestUserName
) {
172 } else if (user_id
== chromeos::login::kRetailModeUserName
) {
173 RetailModeUserLoggedIn();
174 } else if (IsKioskApp(user_id
)) {
175 KioskAppLoggedIn(user_id
);
176 } else if (IsDemoApp(user_id
)) {
177 DemoAccountLoggedIn();
181 if (user
&& user
->GetType() == USER_TYPE_PUBLIC_ACCOUNT
) {
182 PublicAccountUserLoggedIn(user
);
183 } else if ((user
&& user
->GetType() == USER_TYPE_SUPERVISED
) ||
185 gaia::ExtractDomainName(user_id
) ==
186 chromeos::login::kSupervisedUserDomain
)) {
187 SupervisedUserLoggedIn(user_id
);
188 } else if (browser_restart
&& IsPublicAccountMarkedForRemoval(user_id
)) {
189 PublicAccountUserLoggedIn(User::CreatePublicAccountUser(user_id
));
190 } else if (user_id
!= GetOwnerEmail() && !user
&&
191 (AreEphemeralUsersEnabled() || browser_restart
)) {
192 RegularUserLoggedInAsEphemeral(user_id
);
194 RegularUserLoggedIn(user_id
);
198 DCHECK(active_user_
);
199 active_user_
->set_is_logged_in(true);
200 active_user_
->set_is_active(true);
201 active_user_
->set_username_hash(username_hash
);
203 // Place user who just signed in to the top of the logged in users.
204 logged_in_users_
.insert(logged_in_users_
.begin(), active_user_
);
205 SetLRUUser(active_user_
);
207 if (!primary_user_
) {
208 primary_user_
= active_user_
;
209 if (primary_user_
->GetType() == USER_TYPE_REGULAR
)
210 SendRegularUserLoginMetrics(user_id
);
213 UMA_HISTOGRAM_ENUMERATION(
214 "UserManager.LoginUserType", active_user_
->GetType(), NUM_USER_TYPES
);
216 GetLocalState()->SetString(
217 kLastLoggedInRegularUser
,
218 (active_user_
->GetType() == USER_TYPE_REGULAR
) ? user_id
: "");
221 PerformPostUserLoggedInActions(browser_restart
);
224 void UserManagerBase::SwitchActiveUser(const std::string
& user_id
) {
225 User
* user
= FindUserAndModify(user_id
);
227 NOTREACHED() << "Switching to a non-existing user";
230 if (user
== active_user_
) {
231 NOTREACHED() << "Switching to a user who is already active";
234 if (!user
->is_logged_in()) {
235 NOTREACHED() << "Switching to a user that is not logged in";
238 if (user
->GetType() != USER_TYPE_REGULAR
) {
239 NOTREACHED() << "Switching to a non-regular user";
242 if (user
->username_hash().empty()) {
243 NOTREACHED() << "Switching to a user that doesn't have username_hash set";
247 DCHECK(active_user_
);
248 active_user_
->set_is_active(false);
249 user
->set_is_active(true);
252 // Move the user to the front.
253 SetLRUUser(active_user_
);
255 NotifyActiveUserHashChanged(active_user_
->username_hash());
256 NotifyActiveUserChanged(active_user_
);
259 void UserManagerBase::SessionStarted() {
260 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
261 session_started_
= true;
264 session_manager::SessionManager::Get()->SetSessionState(
265 session_manager::SESSION_STATE_ACTIVE
);
267 if (IsCurrentUserNew()) {
268 // Make sure that the new user's data is persisted to Local State.
269 GetLocalState()->CommitPendingWrite();
273 void UserManagerBase::RemoveUser(const std::string
& user_id
,
274 RemoveUserDelegate
* delegate
) {
275 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
277 if (!CanUserBeRemoved(FindUser(user_id
)))
280 RemoveUserInternal(user_id
, delegate
);
283 void UserManagerBase::RemoveUserInternal(const std::string
& user_email
,
284 RemoveUserDelegate
* delegate
) {
285 RemoveNonOwnerUserInternal(user_email
, delegate
);
288 void UserManagerBase::RemoveNonOwnerUserInternal(const std::string
& user_email
,
289 RemoveUserDelegate
* delegate
) {
291 delegate
->OnBeforeUserRemoved(user_email
);
292 RemoveUserFromList(user_email
);
293 cryptohome::AsyncMethodCaller::GetInstance()->AsyncRemove(
294 user_email
, base::Bind(&OnRemoveUserComplete
, user_email
));
297 delegate
->OnUserRemoved(user_email
);
300 void UserManagerBase::RemoveUserFromList(const std::string
& user_id
) {
301 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
302 RemoveNonCryptohomeData(user_id
);
303 if (user_loading_stage_
== STAGE_LOADED
) {
304 DeleteUser(RemoveRegularOrSupervisedUserFromList(user_id
));
305 } else if (user_loading_stage_
== STAGE_LOADING
) {
306 DCHECK(gaia::ExtractDomainName(user_id
) ==
307 chromeos::login::kSupervisedUserDomain
);
308 // Special case, removing partially-constructed supervised user during user
310 ListPrefUpdate
users_update(GetLocalState(), kRegularUsers
);
311 users_update
->Remove(base::StringValue(user_id
), NULL
);
313 NOTREACHED() << "Users are not loaded yet.";
317 // Make sure that new data is persisted to Local State.
318 GetLocalState()->CommitPendingWrite();
321 bool UserManagerBase::IsKnownUser(const std::string
& user_id
) const {
322 return FindUser(user_id
) != NULL
;
325 const User
* UserManagerBase::FindUser(const std::string
& user_id
) const {
326 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
327 if (active_user_
&& active_user_
->email() == user_id
)
329 return FindUserInList(user_id
);
332 User
* UserManagerBase::FindUserAndModify(const std::string
& user_id
) {
333 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
334 if (active_user_
&& active_user_
->email() == user_id
)
336 return FindUserInListAndModify(user_id
);
339 const User
* UserManagerBase::GetLoggedInUser() const {
340 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
344 User
* UserManagerBase::GetLoggedInUser() {
345 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
349 const User
* UserManagerBase::GetActiveUser() const {
350 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
354 User
* UserManagerBase::GetActiveUser() {
355 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
359 const User
* UserManagerBase::GetPrimaryUser() const {
360 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
361 return primary_user_
;
364 void UserManagerBase::SaveUserOAuthStatus(
365 const std::string
& user_id
,
366 User::OAuthTokenStatus oauth_token_status
) {
367 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
369 DVLOG(1) << "Saving user OAuth token status in Local State";
370 User
* user
= FindUserAndModify(user_id
);
372 user
->set_oauth_token_status(oauth_token_status
);
374 // Do not update local state if data stored or cached outside the user's
375 // cryptohome is to be treated as ephemeral.
376 if (IsUserNonCryptohomeDataEphemeral(user_id
))
379 DictionaryPrefUpdate
oauth_status_update(GetLocalState(),
380 kUserOAuthTokenStatus
);
381 oauth_status_update
->SetWithoutPathExpansion(
383 new base::FundamentalValue(static_cast<int>(oauth_token_status
)));
386 void UserManagerBase::SaveForceOnlineSignin(const std::string
& user_id
,
387 bool force_online_signin
) {
388 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
390 // Do not update local state if data stored or cached outside the user's
391 // cryptohome is to be treated as ephemeral.
392 if (IsUserNonCryptohomeDataEphemeral(user_id
))
395 DictionaryPrefUpdate
force_online_update(GetLocalState(),
396 kUserForceOnlineSignin
);
397 force_online_update
->SetBooleanWithoutPathExpansion(user_id
,
398 force_online_signin
);
401 void UserManagerBase::SaveUserDisplayName(const std::string
& user_id
,
402 const base::string16
& display_name
) {
403 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
405 if (User
* user
= FindUserAndModify(user_id
)) {
406 user
->set_display_name(display_name
);
408 // Do not update local state if data stored or cached outside the user's
409 // cryptohome is to be treated as ephemeral.
410 if (!IsUserNonCryptohomeDataEphemeral(user_id
)) {
411 DictionaryPrefUpdate
display_name_update(GetLocalState(),
413 display_name_update
->SetWithoutPathExpansion(
414 user_id
, new base::StringValue(display_name
));
419 base::string16
UserManagerBase::GetUserDisplayName(
420 const std::string
& user_id
) const {
421 const User
* user
= FindUser(user_id
);
422 return user
? user
->display_name() : base::string16();
425 void UserManagerBase::SaveUserDisplayEmail(const std::string
& user_id
,
426 const std::string
& display_email
) {
427 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
429 User
* user
= FindUserAndModify(user_id
);
431 return; // Ignore if there is no such user.
433 user
->set_display_email(display_email
);
435 // Do not update local state if data stored or cached outside the user's
436 // cryptohome is to be treated as ephemeral.
437 if (IsUserNonCryptohomeDataEphemeral(user_id
))
440 DictionaryPrefUpdate
display_email_update(GetLocalState(), kUserDisplayEmail
);
441 display_email_update
->SetWithoutPathExpansion(
442 user_id
, new base::StringValue(display_email
));
445 std::string
UserManagerBase::GetUserDisplayEmail(
446 const std::string
& user_id
) const {
447 const User
* user
= FindUser(user_id
);
448 return user
? user
->display_email() : user_id
;
451 void UserManagerBase::UpdateUserAccountData(
452 const std::string
& user_id
,
453 const UserAccountData
& account_data
) {
454 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
456 SaveUserDisplayName(user_id
, account_data
.display_name());
458 if (User
* user
= FindUserAndModify(user_id
)) {
459 base::string16 given_name
= account_data
.given_name();
460 user
->set_given_name(given_name
);
461 if (!IsUserNonCryptohomeDataEphemeral(user_id
)) {
462 DictionaryPrefUpdate
given_name_update(GetLocalState(), kUserGivenName
);
463 given_name_update
->SetWithoutPathExpansion(
464 user_id
, new base::StringValue(given_name
));
468 UpdateUserAccountLocale(user_id
, account_data
.locale());
472 void UserManagerBase::ParseUserList(const base::ListValue
& users_list
,
473 const std::set
<std::string
>& existing_users
,
474 std::vector
<std::string
>* users_vector
,
475 std::set
<std::string
>* users_set
) {
476 users_vector
->clear();
478 for (size_t i
= 0; i
< users_list
.GetSize(); ++i
) {
480 if (!users_list
.GetString(i
, &email
) || email
.empty()) {
481 LOG(ERROR
) << "Corrupt entry in user list at index " << i
<< ".";
484 if (existing_users
.find(email
) != existing_users
.end() ||
485 !users_set
->insert(email
).second
) {
486 LOG(ERROR
) << "Duplicate user: " << email
;
489 users_vector
->push_back(email
);
493 bool UserManagerBase::IsCurrentUserOwner() const {
494 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
495 base::AutoLock
lk(is_current_user_owner_lock_
);
496 return is_current_user_owner_
;
499 void UserManagerBase::SetCurrentUserIsOwner(bool is_current_user_owner
) {
500 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
502 base::AutoLock
lk(is_current_user_owner_lock_
);
503 is_current_user_owner_
= is_current_user_owner
;
508 bool UserManagerBase::IsCurrentUserNew() const {
509 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
510 return is_current_user_new_
;
513 bool UserManagerBase::IsCurrentUserNonCryptohomeDataEphemeral() const {
514 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
515 return IsUserLoggedIn() &&
516 IsUserNonCryptohomeDataEphemeral(GetLoggedInUser()->email());
519 bool UserManagerBase::CanCurrentUserLock() const {
520 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
521 return IsUserLoggedIn() && active_user_
->can_lock();
524 bool UserManagerBase::IsUserLoggedIn() const {
525 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
529 bool UserManagerBase::IsLoggedInAsRegularUser() const {
530 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
531 return IsUserLoggedIn() && active_user_
->GetType() == USER_TYPE_REGULAR
;
534 bool UserManagerBase::IsLoggedInAsDemoUser() const {
535 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
536 return IsUserLoggedIn() && active_user_
->GetType() == USER_TYPE_RETAIL_MODE
;
539 bool UserManagerBase::IsLoggedInAsPublicAccount() const {
540 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
541 return IsUserLoggedIn() &&
542 active_user_
->GetType() == USER_TYPE_PUBLIC_ACCOUNT
;
545 bool UserManagerBase::IsLoggedInAsGuest() const {
546 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
547 return IsUserLoggedIn() && active_user_
->GetType() == USER_TYPE_GUEST
;
550 bool UserManagerBase::IsLoggedInAsSupervisedUser() const {
551 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
552 return IsUserLoggedIn() && active_user_
->GetType() == USER_TYPE_SUPERVISED
;
555 bool UserManagerBase::IsLoggedInAsKioskApp() const {
556 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
557 return IsUserLoggedIn() && active_user_
->GetType() == USER_TYPE_KIOSK_APP
;
560 bool UserManagerBase::IsLoggedInAsStub() const {
561 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
562 return IsUserLoggedIn() &&
563 active_user_
->email() == chromeos::login::kStubUser
;
566 bool UserManagerBase::IsSessionStarted() const {
567 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
568 return session_started_
;
571 bool UserManagerBase::IsUserNonCryptohomeDataEphemeral(
572 const std::string
& user_id
) const {
573 // Data belonging to the guest, retail mode and stub users is always
575 if (user_id
== chromeos::login::kGuestUserName
||
576 user_id
== chromeos::login::kRetailModeUserName
||
577 user_id
== chromeos::login::kStubUser
) {
581 // Data belonging to the owner, anyone found on the user list and obsolete
582 // public accounts whose data has not been removed yet is not ephemeral.
583 if (user_id
== GetOwnerEmail() || UserExistsInList(user_id
) ||
584 IsPublicAccountMarkedForRemoval(user_id
)) {
588 // Data belonging to the currently logged-in user is ephemeral when:
589 // a) The user logged into a regular account while the ephemeral users policy
592 // b) The user logged into any other account type.
593 if (IsUserLoggedIn() && (user_id
== GetLoggedInUser()->email()) &&
594 (is_current_user_ephemeral_regular_user_
|| !IsLoggedInAsRegularUser())) {
598 // Data belonging to any other user is ephemeral when:
599 // a) Going through the regular login flow and the ephemeral users policy is
602 // b) The browser is restarting after a crash.
603 return AreEphemeralUsersEnabled() ||
604 session_manager::SessionManager::HasBrowserRestarted();
607 void UserManagerBase::AddObserver(UserManager::Observer
* obs
) {
608 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
609 observer_list_
.AddObserver(obs
);
612 void UserManagerBase::RemoveObserver(UserManager::Observer
* obs
) {
613 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
614 observer_list_
.RemoveObserver(obs
);
617 void UserManagerBase::AddSessionStateObserver(
618 UserManager::UserSessionStateObserver
* obs
) {
619 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
620 session_state_observer_list_
.AddObserver(obs
);
623 void UserManagerBase::RemoveSessionStateObserver(
624 UserManager::UserSessionStateObserver
* obs
) {
625 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
626 session_state_observer_list_
.RemoveObserver(obs
);
629 void UserManagerBase::NotifyLocalStateChanged() {
630 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
632 UserManager::Observer
, observer_list_
, LocalStateChanged(this));
635 bool UserManagerBase::CanUserBeRemoved(const User
* user
) const {
636 // Only regular and supervised users are allowed to be manually removed.
637 if (!user
|| (user
->GetType() != USER_TYPE_REGULAR
&&
638 user
->GetType() != USER_TYPE_SUPERVISED
)) {
642 // Sanity check: we must not remove single user unless it's an enterprise
643 // device. This check may seem redundant at a first sight because
644 // this single user must be an owner and we perform special check later
645 // in order not to remove an owner. However due to non-instant nature of
646 // ownership assignment this later check may sometimes fail.
647 // See http://crosbug.com/12723
648 if (users_
.size() < 2 && !IsEnterpriseManaged())
651 // Sanity check: do not allow any of the the logged in users to be removed.
652 for (UserList::const_iterator it
= logged_in_users_
.begin();
653 it
!= logged_in_users_
.end();
655 if ((*it
)->email() == user
->email())
662 bool UserManagerBase::GetEphemeralUsersEnabled() const {
663 return ephemeral_users_enabled_
;
666 void UserManagerBase::SetEphemeralUsersEnabled(bool enabled
) {
667 ephemeral_users_enabled_
= enabled
;
670 void UserManagerBase::SetIsCurrentUserNew(bool is_new
) {
671 is_current_user_new_
= is_new
;
674 void UserManagerBase::SetOwnerEmail(std::string owner_user_id
) {
675 owner_email_
= owner_user_id
;
678 const std::string
& UserManagerBase::GetPendingUserSwitchID() const {
679 return pending_user_switch_
;
682 void UserManagerBase::SetPendingUserSwitchID(std::string user_id
) {
683 pending_user_switch_
= user_id
;
686 void UserManagerBase::EnsureUsersLoaded() {
687 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
688 if (!GetLocalState())
691 if (user_loading_stage_
!= STAGE_NOT_LOADED
)
693 user_loading_stage_
= STAGE_LOADING
;
695 PerformPreUserListLoadingActions();
697 PrefService
* local_state
= GetLocalState();
698 const base::ListValue
* prefs_regular_users
=
699 local_state
->GetList(kRegularUsers
);
701 const base::DictionaryValue
* prefs_display_names
=
702 local_state
->GetDictionary(kUserDisplayName
);
703 const base::DictionaryValue
* prefs_given_names
=
704 local_state
->GetDictionary(kUserGivenName
);
705 const base::DictionaryValue
* prefs_display_emails
=
706 local_state
->GetDictionary(kUserDisplayEmail
);
708 // Load public sessions first.
709 std::set
<std::string
> public_sessions_set
;
710 LoadPublicAccounts(&public_sessions_set
);
712 // Load regular users and supervised users.
713 std::vector
<std::string
> regular_users
;
714 std::set
<std::string
> regular_users_set
;
715 ParseUserList(*prefs_regular_users
,
719 for (std::vector
<std::string
>::const_iterator it
= regular_users
.begin();
720 it
!= regular_users
.end();
723 const std::string domain
= gaia::ExtractDomainName(*it
);
724 if (domain
== chromeos::login::kSupervisedUserDomain
)
725 user
= User::CreateSupervisedUser(*it
);
727 user
= User::CreateRegularUser(*it
);
728 user
->set_oauth_token_status(LoadUserOAuthStatus(*it
));
729 user
->set_force_online_signin(LoadForceOnlineSignin(*it
));
730 users_
.push_back(user
);
732 base::string16 display_name
;
733 if (prefs_display_names
->GetStringWithoutPathExpansion(*it
,
735 user
->set_display_name(display_name
);
738 base::string16 given_name
;
739 if (prefs_given_names
->GetStringWithoutPathExpansion(*it
, &given_name
)) {
740 user
->set_given_name(given_name
);
743 std::string display_email
;
744 if (prefs_display_emails
->GetStringWithoutPathExpansion(*it
,
746 user
->set_display_email(display_email
);
750 user_loading_stage_
= STAGE_LOADED
;
752 PerformPostUserListLoadingActions();
755 UserList
& UserManagerBase::GetUsersAndModify() {
760 const User
* UserManagerBase::FindUserInList(const std::string
& user_id
) const {
761 const UserList
& users
= GetUsers();
762 for (UserList::const_iterator it
= users
.begin(); it
!= users
.end(); ++it
) {
763 if ((*it
)->email() == user_id
)
769 const bool UserManagerBase::UserExistsInList(const std::string
& user_id
) const {
770 const base::ListValue
* user_list
= GetLocalState()->GetList(kRegularUsers
);
771 for (size_t i
= 0; i
< user_list
->GetSize(); ++i
) {
773 if (user_list
->GetString(i
, &email
) && (user_id
== email
))
779 User
* UserManagerBase::FindUserInListAndModify(const std::string
& user_id
) {
780 UserList
& users
= GetUsersAndModify();
781 for (UserList::iterator it
= users
.begin(); it
!= users
.end(); ++it
) {
782 if ((*it
)->email() == user_id
)
788 void UserManagerBase::GuestUserLoggedIn() {
789 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
790 active_user_
= User::CreateGuestUser();
793 void UserManagerBase::AddUserRecord(User
* user
) {
794 // Add the user to the front of the user list.
795 ListPrefUpdate
prefs_users_update(GetLocalState(), kRegularUsers
);
796 prefs_users_update
->Insert(0, new base::StringValue(user
->email()));
797 users_
.insert(users_
.begin(), user
);
800 void UserManagerBase::RegularUserLoggedIn(const std::string
& user_id
) {
801 // Remove the user from the user list.
802 active_user_
= RemoveRegularOrSupervisedUserFromList(user_id
);
804 // If the user was not found on the user list, create a new user.
805 SetIsCurrentUserNew(!active_user_
);
806 if (IsCurrentUserNew()) {
807 active_user_
= User::CreateRegularUser(user_id
);
808 active_user_
->set_oauth_token_status(LoadUserOAuthStatus(user_id
));
809 SaveUserDisplayName(active_user_
->email(),
810 base::UTF8ToUTF16(active_user_
->GetAccountName(true)));
813 AddUserRecord(active_user_
);
815 // Make sure that new data is persisted to Local State.
816 GetLocalState()->CommitPendingWrite();
819 void UserManagerBase::RegularUserLoggedInAsEphemeral(
820 const std::string
& user_id
) {
821 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
822 SetIsCurrentUserNew(true);
823 is_current_user_ephemeral_regular_user_
= true;
824 active_user_
= User::CreateRegularUser(user_id
);
827 void UserManagerBase::NotifyOnLogin() {
828 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
830 NotifyActiveUserHashChanged(active_user_
->username_hash());
831 NotifyActiveUserChanged(active_user_
);
835 User::OAuthTokenStatus
UserManagerBase::LoadUserOAuthStatus(
836 const std::string
& user_id
) const {
837 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
839 const base::DictionaryValue
* prefs_oauth_status
=
840 GetLocalState()->GetDictionary(kUserOAuthTokenStatus
);
841 int oauth_token_status
= User::OAUTH_TOKEN_STATUS_UNKNOWN
;
842 if (prefs_oauth_status
&&
843 prefs_oauth_status
->GetIntegerWithoutPathExpansion(user_id
,
844 &oauth_token_status
)) {
845 User::OAuthTokenStatus status
=
846 static_cast<User::OAuthTokenStatus
>(oauth_token_status
);
847 HandleUserOAuthTokenStatusChange(user_id
, status
);
851 return User::OAUTH_TOKEN_STATUS_UNKNOWN
;
854 bool UserManagerBase::LoadForceOnlineSignin(const std::string
& user_id
) const {
855 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
857 const base::DictionaryValue
* prefs_force_online
=
858 GetLocalState()->GetDictionary(kUserForceOnlineSignin
);
859 bool force_online_signin
= false;
860 if (prefs_force_online
) {
861 prefs_force_online
->GetBooleanWithoutPathExpansion(user_id
,
862 &force_online_signin
);
864 return force_online_signin
;
867 void UserManagerBase::RemoveNonCryptohomeData(const std::string
& user_id
) {
868 PrefService
* prefs
= GetLocalState();
869 DictionaryPrefUpdate
prefs_display_name_update(prefs
, kUserDisplayName
);
870 prefs_display_name_update
->RemoveWithoutPathExpansion(user_id
, NULL
);
872 DictionaryPrefUpdate
prefs_given_name_update(prefs
, kUserGivenName
);
873 prefs_given_name_update
->RemoveWithoutPathExpansion(user_id
, NULL
);
875 DictionaryPrefUpdate
prefs_display_email_update(prefs
, kUserDisplayEmail
);
876 prefs_display_email_update
->RemoveWithoutPathExpansion(user_id
, NULL
);
878 DictionaryPrefUpdate
prefs_oauth_update(prefs
, kUserOAuthTokenStatus
);
879 prefs_oauth_update
->RemoveWithoutPathExpansion(user_id
, NULL
);
881 DictionaryPrefUpdate
prefs_force_online_update(prefs
, kUserForceOnlineSignin
);
882 prefs_force_online_update
->RemoveWithoutPathExpansion(user_id
, NULL
);
885 User
* UserManagerBase::RemoveRegularOrSupervisedUserFromList(
886 const std::string
& user_id
) {
887 ListPrefUpdate
prefs_users_update(GetLocalState(), kRegularUsers
);
888 prefs_users_update
->Clear();
890 for (UserList::iterator it
= users_
.begin(); it
!= users_
.end();) {
891 const std::string user_email
= (*it
)->email();
892 if (user_email
== user_id
) {
894 it
= users_
.erase(it
);
896 if ((*it
)->GetType() == USER_TYPE_REGULAR
||
897 (*it
)->GetType() == USER_TYPE_SUPERVISED
) {
898 prefs_users_update
->Append(new base::StringValue(user_email
));
906 void UserManagerBase::NotifyActiveUserChanged(const User
* active_user
) {
907 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
908 FOR_EACH_OBSERVER(UserManager::UserSessionStateObserver
,
909 session_state_observer_list_
,
910 ActiveUserChanged(active_user
));
913 void UserManagerBase::NotifyUserAddedToSession(const User
* added_user
,
914 bool user_switch_pending
) {
915 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
916 FOR_EACH_OBSERVER(UserManager::UserSessionStateObserver
,
917 session_state_observer_list_
,
918 UserAddedToSession(added_user
));
921 void UserManagerBase::NotifyActiveUserHashChanged(const std::string
& hash
) {
922 DCHECK(task_runner_
->RunsTasksOnCurrentThread());
923 FOR_EACH_OBSERVER(UserManager::UserSessionStateObserver
,
924 session_state_observer_list_
,
925 ActiveUserHashChanged(hash
));
928 void UserManagerBase::UpdateLoginState() {
929 if (!chromeos::LoginState::IsInitialized())
930 return; // LoginState may not be intialized in tests.
932 chromeos::LoginState::LoggedInState logged_in_state
;
933 logged_in_state
= active_user_
? chromeos::LoginState::LOGGED_IN_ACTIVE
934 : chromeos::LoginState::LOGGED_IN_NONE
;
936 chromeos::LoginState::LoggedInUserType login_user_type
;
937 if (logged_in_state
== chromeos::LoginState::LOGGED_IN_NONE
)
938 login_user_type
= chromeos::LoginState::LOGGED_IN_USER_NONE
;
939 else if (is_current_user_owner_
)
940 login_user_type
= chromeos::LoginState::LOGGED_IN_USER_OWNER
;
941 else if (active_user_
->GetType() == USER_TYPE_GUEST
)
942 login_user_type
= chromeos::LoginState::LOGGED_IN_USER_GUEST
;
943 else if (active_user_
->GetType() == USER_TYPE_RETAIL_MODE
)
944 login_user_type
= chromeos::LoginState::LOGGED_IN_USER_RETAIL_MODE
;
945 else if (active_user_
->GetType() == USER_TYPE_PUBLIC_ACCOUNT
)
946 login_user_type
= chromeos::LoginState::LOGGED_IN_USER_PUBLIC_ACCOUNT
;
947 else if (active_user_
->GetType() == USER_TYPE_SUPERVISED
)
948 login_user_type
= chromeos::LoginState::LOGGED_IN_USER_SUPERVISED
;
949 else if (active_user_
->GetType() == USER_TYPE_KIOSK_APP
)
950 login_user_type
= chromeos::LoginState::LOGGED_IN_USER_KIOSK_APP
;
952 login_user_type
= chromeos::LoginState::LOGGED_IN_USER_REGULAR
;
955 chromeos::LoginState::Get()->SetLoggedInStateAndPrimaryUser(
956 logged_in_state
, login_user_type
, primary_user_
->username_hash());
958 chromeos::LoginState::Get()->SetLoggedInState(logged_in_state
,
963 void UserManagerBase::SetLRUUser(User
* user
) {
964 UserList::iterator it
=
965 std::find(lru_logged_in_users_
.begin(), lru_logged_in_users_
.end(), user
);
966 if (it
!= lru_logged_in_users_
.end())
967 lru_logged_in_users_
.erase(it
);
968 lru_logged_in_users_
.insert(lru_logged_in_users_
.begin(), user
);
971 void UserManagerBase::SendRegularUserLoginMetrics(const std::string
& user_id
) {
972 // If this isn't the first time Chrome was run after the system booted,
973 // assume that Chrome was restarted because a previous session ended.
974 if (!CommandLine::ForCurrentProcess()->HasSwitch(
975 chromeos::switches::kFirstExecAfterBoot
)) {
976 const std::string last_email
=
977 GetLocalState()->GetString(kLastLoggedInRegularUser
);
978 const base::TimeDelta time_to_login
=
979 base::TimeTicks::Now() - manager_creation_time_
;
980 if (!last_email
.empty() && user_id
!= last_email
&&
981 time_to_login
.InSeconds() <= kLogoutToLoginDelayMaxSec
) {
982 UMA_HISTOGRAM_CUSTOM_COUNTS("UserManager.LogoutToLoginDelay",
983 time_to_login
.InSeconds(),
985 kLogoutToLoginDelayMaxSec
,
991 void UserManagerBase::UpdateUserAccountLocale(const std::string
& user_id
,
992 const std::string
& locale
) {
993 scoped_ptr
<std::string
> resolved_locale(new std::string());
994 if (!locale
.empty() && locale
!= GetApplicationLocale()) {
995 // base::Pased will NULL out |resolved_locale|, so cache the underlying ptr.
996 std::string
* raw_resolved_locale
= resolved_locale
.get();
997 blocking_task_runner_
->PostTaskAndReply(
999 base::Bind(ResolveLocale
,
1001 base::Unretained(raw_resolved_locale
)),
1002 base::Bind(&UserManagerBase::DoUpdateAccountLocale
,
1003 weak_factory_
.GetWeakPtr(),
1005 base::Passed(&resolved_locale
)));
1007 resolved_locale
.reset(new std::string(locale
));
1008 DoUpdateAccountLocale(user_id
, resolved_locale
.Pass());
1012 void UserManagerBase::DoUpdateAccountLocale(
1013 const std::string
& user_id
,
1014 scoped_ptr
<std::string
> resolved_locale
) {
1015 User
* user
= FindUserAndModify(user_id
);
1016 if (user
&& resolved_locale
)
1017 user
->SetAccountLocale(*resolved_locale
);
1020 void UserManagerBase::DeleteUser(User
* user
) {
1021 const bool is_active_user
= (user
== active_user_
);
1024 active_user_
= NULL
;
1027 } // namespace user_manager