Chromium Code Reviews| 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()); | |
|
Dmitry Polukhin
2014/09/24 11:16:17
Could you please clear this field if the user is r
Nikita (slow)
2014/09/25 19:51:49
Done.
| |
| 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 687 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 956 if (primary_user_) { | 979 if (primary_user_) { |
| 957 chromeos::LoginState::Get()->SetLoggedInStateAndPrimaryUser( | 980 chromeos::LoginState::Get()->SetLoggedInStateAndPrimaryUser( |
| 958 logged_in_state, login_user_type, primary_user_->username_hash()); | 981 logged_in_state, login_user_type, primary_user_->username_hash()); |
| 959 } else { | 982 } else { |
| 960 chromeos::LoginState::Get()->SetLoggedInState(logged_in_state, | 983 chromeos::LoginState::Get()->SetLoggedInState(logged_in_state, |
| 961 login_user_type); | 984 login_user_type); |
| 962 } | 985 } |
| 963 } | 986 } |
| 964 | 987 |
| 965 void UserManagerBase::SetLRUUser(User* user) { | 988 void UserManagerBase::SetLRUUser(User* user) { |
| 989 GetLocalState()->SetString(kLastActiveUser, user->email()); | |
| 990 GetLocalState()->CommitPendingWrite(); | |
| 991 | |
| 966 UserList::iterator it = | 992 UserList::iterator it = |
| 967 std::find(lru_logged_in_users_.begin(), lru_logged_in_users_.end(), user); | 993 std::find(lru_logged_in_users_.begin(), lru_logged_in_users_.end(), user); |
| 968 if (it != lru_logged_in_users_.end()) | 994 if (it != lru_logged_in_users_.end()) |
| 969 lru_logged_in_users_.erase(it); | 995 lru_logged_in_users_.erase(it); |
| 970 lru_logged_in_users_.insert(lru_logged_in_users_.begin(), user); | 996 lru_logged_in_users_.insert(lru_logged_in_users_.begin(), user); |
| 971 } | 997 } |
| 972 | 998 |
| 973 void UserManagerBase::SendRegularUserLoginMetrics(const std::string& user_id) { | 999 void UserManagerBase::SendRegularUserLoginMetrics(const std::string& user_id) { |
| 974 // If this isn't the first time Chrome was run after the system booted, | 1000 // If this isn't the first time Chrome was run after the system booted, |
| 975 // assume that Chrome was restarted because a previous session ended. | 1001 // assume that Chrome was restarted because a previous session ended. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1020 } | 1046 } |
| 1021 | 1047 |
| 1022 void UserManagerBase::DeleteUser(User* user) { | 1048 void UserManagerBase::DeleteUser(User* user) { |
| 1023 const bool is_active_user = (user == active_user_); | 1049 const bool is_active_user = (user == active_user_); |
| 1024 delete user; | 1050 delete user; |
| 1025 if (is_active_user) | 1051 if (is_active_user) |
| 1026 active_user_ = NULL; | 1052 active_user_ = NULL; |
| 1027 } | 1053 } |
| 1028 | 1054 |
| 1029 } // namespace user_manager | 1055 } // namespace user_manager |
| OLD | NEW |