| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/user_manager/user_manager_base.h" | 5 #include "components/user_manager/user_manager_base.h" |
| 6 | 6 |
| 7 #include <cstddef> | 7 #include <cstddef> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 55 |
| 56 // A dictionary that maps user IDs to a flag indicating whether online | 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. | 57 // authentication against GAIA should be enforced during the next sign-in. |
| 58 const char kUserForceOnlineSignin[] = "UserForceOnlineSignin"; | 58 const char kUserForceOnlineSignin[] = "UserForceOnlineSignin"; |
| 59 | 59 |
| 60 // A string pref containing the ID of the last user who logged in if it was | 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, | 61 // a regular user or an empty string if it was another type of user (guest, |
| 62 // kiosk, public account, etc.). | 62 // kiosk, public account, etc.). |
| 63 const char kLastLoggedInRegularUser[] = "LastLoggedInRegularUser"; | 63 const char kLastLoggedInRegularUser[] = "LastLoggedInRegularUser"; |
| 64 | 64 |
| 65 // A string pref containing the ID of the last active user. |
| 66 // In case of browser crash, this pref will be used to set active user after |
| 67 // session restore. |
| 68 const char kLastActiveUser[] = "LastActiveUser"; |
| 69 |
| 65 // Upper bound for a histogram metric reporting the amount of time between | 70 // 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. | 71 // one regular user logging out and a different regular user logging in. |
| 67 const int kLogoutToLoginDelayMaxSec = 1800; | 72 const int kLogoutToLoginDelayMaxSec = 1800; |
| 68 | 73 |
| 69 // Callback that is called after user removal is complete. | 74 // Callback that is called after user removal is complete. |
| 70 void OnRemoveUserComplete(const std::string& user_email, | 75 void OnRemoveUserComplete(const std::string& user_email, |
| 71 bool success, | 76 bool success, |
| 72 cryptohome::MountError return_code) { | 77 cryptohome::MountError return_code) { |
| 73 // Log the error, but there's not much we can do. | 78 // Log the error, but there's not much we can do. |
| 74 if (!success) { | 79 if (!success) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 87 | 92 |
| 88 // static | 93 // static |
| 89 void UserManagerBase::RegisterPrefs(PrefRegistrySimple* registry) { | 94 void UserManagerBase::RegisterPrefs(PrefRegistrySimple* registry) { |
| 90 registry->RegisterListPref(kRegularUsers); | 95 registry->RegisterListPref(kRegularUsers); |
| 91 registry->RegisterStringPref(kLastLoggedInRegularUser, std::string()); | 96 registry->RegisterStringPref(kLastLoggedInRegularUser, std::string()); |
| 92 registry->RegisterDictionaryPref(kUserDisplayName); | 97 registry->RegisterDictionaryPref(kUserDisplayName); |
| 93 registry->RegisterDictionaryPref(kUserGivenName); | 98 registry->RegisterDictionaryPref(kUserGivenName); |
| 94 registry->RegisterDictionaryPref(kUserDisplayEmail); | 99 registry->RegisterDictionaryPref(kUserDisplayEmail); |
| 95 registry->RegisterDictionaryPref(kUserOAuthTokenStatus); | 100 registry->RegisterDictionaryPref(kUserOAuthTokenStatus); |
| 96 registry->RegisterDictionaryPref(kUserForceOnlineSignin); | 101 registry->RegisterDictionaryPref(kUserForceOnlineSignin); |
| 102 registry->RegisterStringPref(kLastActiveUser, std::string()); |
| 97 } | 103 } |
| 98 | 104 |
| 99 UserManagerBase::UserManagerBase( | 105 UserManagerBase::UserManagerBase( |
| 100 scoped_refptr<base::TaskRunner> task_runner, | 106 scoped_refptr<base::TaskRunner> task_runner, |
| 101 scoped_refptr<base::TaskRunner> blocking_task_runner) | 107 scoped_refptr<base::TaskRunner> blocking_task_runner) |
| 102 : active_user_(NULL), | 108 : active_user_(NULL), |
| 103 primary_user_(NULL), | 109 primary_user_(NULL), |
| 104 user_loading_stage_(STAGE_NOT_LOADED), | 110 user_loading_stage_(STAGE_NOT_LOADED), |
| 105 session_started_(false), | 111 session_started_(false), |
| 106 is_current_user_owner_(false), | 112 is_current_user_owner_(false), |
| 107 is_current_user_new_(false), | 113 is_current_user_new_(false), |
| 108 is_current_user_ephemeral_regular_user_(false), | 114 is_current_user_ephemeral_regular_user_(false), |
| 109 ephemeral_users_enabled_(false), | 115 ephemeral_users_enabled_(false), |
| 110 manager_creation_time_(base::TimeTicks::Now()), | 116 manager_creation_time_(base::TimeTicks::Now()), |
| 117 last_session_active_user_initialized_(false), |
| 111 task_runner_(task_runner), | 118 task_runner_(task_runner), |
| 112 blocking_task_runner_(blocking_task_runner), | 119 blocking_task_runner_(blocking_task_runner), |
| 113 weak_factory_(this) { | 120 weak_factory_(this) { |
| 114 UpdateLoginState(); | 121 UpdateLoginState(); |
| 115 } | 122 } |
| 116 | 123 |
| 117 UserManagerBase::~UserManagerBase() { | 124 UserManagerBase::~UserManagerBase() { |
| 118 // Can't use STLDeleteElements because of the private destructor of User. | 125 // Can't use STLDeleteElements because of the private destructor of User. |
| 119 for (UserList::iterator it = users_.begin(); it != users_.end(); | 126 for (UserList::iterator it = users_.begin(); it != users_.end(); |
| 120 it = users_.erase(it)) { | 127 it = users_.erase(it)) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 146 | 153 |
| 147 const std::string& UserManagerBase::GetOwnerEmail() const { | 154 const std::string& UserManagerBase::GetOwnerEmail() const { |
| 148 return owner_email_; | 155 return owner_email_; |
| 149 } | 156 } |
| 150 | 157 |
| 151 void UserManagerBase::UserLoggedIn(const std::string& user_id, | 158 void UserManagerBase::UserLoggedIn(const std::string& user_id, |
| 152 const std::string& username_hash, | 159 const std::string& username_hash, |
| 153 bool browser_restart) { | 160 bool browser_restart) { |
| 154 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 161 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 155 | 162 |
| 163 if (!last_session_active_user_initialized_) { |
| 164 last_session_active_user_ = GetLocalState()->GetString(kLastActiveUser); |
| 165 last_session_active_user_initialized_ = true; |
| 166 } |
| 167 |
| 156 User* user = FindUserInListAndModify(user_id); | 168 User* user = FindUserInListAndModify(user_id); |
| 157 if (active_user_ && user) { | 169 if (active_user_ && user) { |
| 158 user->set_is_logged_in(true); | 170 user->set_is_logged_in(true); |
| 159 user->set_username_hash(username_hash); | 171 user->set_username_hash(username_hash); |
| 160 logged_in_users_.push_back(user); | 172 logged_in_users_.push_back(user); |
| 161 lru_logged_in_users_.push_back(user); | 173 lru_logged_in_users_.push_back(user); |
| 162 | 174 |
| 163 // Reset the new user flag if the user already exists. | 175 // Reset the new user flag if the user already exists. |
| 164 SetIsCurrentUserNew(false); | 176 SetIsCurrentUserNew(false); |
| 165 NotifyUserAddedToSession(user, true /* user switch pending */); | 177 NotifyUserAddedToSession(user, true /* user switch pending */); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 user->set_is_active(true); | 261 user->set_is_active(true); |
| 250 active_user_ = user; | 262 active_user_ = user; |
| 251 | 263 |
| 252 // Move the user to the front. | 264 // Move the user to the front. |
| 253 SetLRUUser(active_user_); | 265 SetLRUUser(active_user_); |
| 254 | 266 |
| 255 NotifyActiveUserHashChanged(active_user_->username_hash()); | 267 NotifyActiveUserHashChanged(active_user_->username_hash()); |
| 256 NotifyActiveUserChanged(active_user_); | 268 NotifyActiveUserChanged(active_user_); |
| 257 } | 269 } |
| 258 | 270 |
| 271 void UserManagerBase::SwitchToLastActiveUser() { |
| 272 if (last_session_active_user_.empty()) |
| 273 return; |
| 274 |
| 275 if (GetActiveUser()->email() != last_session_active_user_) |
| 276 SwitchActiveUser(last_session_active_user_); |
| 277 |
| 278 // Make sure that this function gets run only once. |
| 279 last_session_active_user_.clear(); |
| 280 } |
| 281 |
| 259 void UserManagerBase::SessionStarted() { | 282 void UserManagerBase::SessionStarted() { |
| 260 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 283 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 261 session_started_ = true; | 284 session_started_ = true; |
| 262 | 285 |
| 263 UpdateLoginState(); | 286 UpdateLoginState(); |
| 264 session_manager::SessionManager::Get()->SetSessionState( | 287 session_manager::SessionManager::Get()->SetSessionState( |
| 265 session_manager::SESSION_STATE_ACTIVE); | 288 session_manager::SESSION_STATE_ACTIVE); |
| 266 | 289 |
| 267 if (IsCurrentUserNew()) { | 290 if (IsCurrentUserNew()) { |
| 268 // Make sure that the new user's data is persisted to Local State. | 291 // Make sure that the new user's data is persisted to Local State. |
| (...skipping 610 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 879 prefs_given_name_update->RemoveWithoutPathExpansion(user_id, NULL); | 902 prefs_given_name_update->RemoveWithoutPathExpansion(user_id, NULL); |
| 880 | 903 |
| 881 DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail); | 904 DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail); |
| 882 prefs_display_email_update->RemoveWithoutPathExpansion(user_id, NULL); | 905 prefs_display_email_update->RemoveWithoutPathExpansion(user_id, NULL); |
| 883 | 906 |
| 884 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); | 907 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); |
| 885 prefs_oauth_update->RemoveWithoutPathExpansion(user_id, NULL); | 908 prefs_oauth_update->RemoveWithoutPathExpansion(user_id, NULL); |
| 886 | 909 |
| 887 DictionaryPrefUpdate prefs_force_online_update(prefs, kUserForceOnlineSignin); | 910 DictionaryPrefUpdate prefs_force_online_update(prefs, kUserForceOnlineSignin); |
| 888 prefs_force_online_update->RemoveWithoutPathExpansion(user_id, NULL); | 911 prefs_force_online_update->RemoveWithoutPathExpansion(user_id, NULL); |
| 912 |
| 913 std::string last_active_user = GetLocalState()->GetString(kLastActiveUser); |
| 914 if (user_id == last_active_user) |
| 915 GetLocalState()->SetString(kLastActiveUser, std::string()); |
| 889 } | 916 } |
| 890 | 917 |
| 891 User* UserManagerBase::RemoveRegularOrSupervisedUserFromList( | 918 User* UserManagerBase::RemoveRegularOrSupervisedUserFromList( |
| 892 const std::string& user_id) { | 919 const std::string& user_id) { |
| 893 ListPrefUpdate prefs_users_update(GetLocalState(), kRegularUsers); | 920 ListPrefUpdate prefs_users_update(GetLocalState(), kRegularUsers); |
| 894 prefs_users_update->Clear(); | 921 prefs_users_update->Clear(); |
| 895 User* user = NULL; | 922 User* user = NULL; |
| 896 for (UserList::iterator it = users_.begin(); it != users_.end();) { | 923 for (UserList::iterator it = users_.begin(); it != users_.end();) { |
| 897 const std::string user_email = (*it)->email(); | 924 const std::string user_email = (*it)->email(); |
| 898 if (user_email == user_id) { | 925 if (user_email == user_id) { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 960 if (primary_user_) { | 987 if (primary_user_) { |
| 961 chromeos::LoginState::Get()->SetLoggedInStateAndPrimaryUser( | 988 chromeos::LoginState::Get()->SetLoggedInStateAndPrimaryUser( |
| 962 logged_in_state, login_user_type, primary_user_->username_hash()); | 989 logged_in_state, login_user_type, primary_user_->username_hash()); |
| 963 } else { | 990 } else { |
| 964 chromeos::LoginState::Get()->SetLoggedInState(logged_in_state, | 991 chromeos::LoginState::Get()->SetLoggedInState(logged_in_state, |
| 965 login_user_type); | 992 login_user_type); |
| 966 } | 993 } |
| 967 } | 994 } |
| 968 | 995 |
| 969 void UserManagerBase::SetLRUUser(User* user) { | 996 void UserManagerBase::SetLRUUser(User* user) { |
| 997 GetLocalState()->SetString(kLastActiveUser, user->email()); |
| 998 GetLocalState()->CommitPendingWrite(); |
| 999 |
| 970 UserList::iterator it = | 1000 UserList::iterator it = |
| 971 std::find(lru_logged_in_users_.begin(), lru_logged_in_users_.end(), user); | 1001 std::find(lru_logged_in_users_.begin(), lru_logged_in_users_.end(), user); |
| 972 if (it != lru_logged_in_users_.end()) | 1002 if (it != lru_logged_in_users_.end()) |
| 973 lru_logged_in_users_.erase(it); | 1003 lru_logged_in_users_.erase(it); |
| 974 lru_logged_in_users_.insert(lru_logged_in_users_.begin(), user); | 1004 lru_logged_in_users_.insert(lru_logged_in_users_.begin(), user); |
| 975 } | 1005 } |
| 976 | 1006 |
| 977 void UserManagerBase::SendRegularUserLoginMetrics(const std::string& user_id) { | 1007 void UserManagerBase::SendRegularUserLoginMetrics(const std::string& user_id) { |
| 978 // If this isn't the first time Chrome was run after the system booted, | 1008 // If this isn't the first time Chrome was run after the system booted, |
| 979 // assume that Chrome was restarted because a previous session ended. | 1009 // assume that Chrome was restarted because a previous session ended. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1024 } | 1054 } |
| 1025 | 1055 |
| 1026 void UserManagerBase::DeleteUser(User* user) { | 1056 void UserManagerBase::DeleteUser(User* user) { |
| 1027 const bool is_active_user = (user == active_user_); | 1057 const bool is_active_user = (user == active_user_); |
| 1028 delete user; | 1058 delete user; |
| 1029 if (is_active_user) | 1059 if (is_active_user) |
| 1030 active_user_ = NULL; | 1060 active_user_ = NULL; |
| 1031 } | 1061 } |
| 1032 | 1062 |
| 1033 } // namespace user_manager | 1063 } // namespace user_manager |
| OLD | NEW |