Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: components/user_manager/user_manager_base.cc

Issue 594163002: Restore last used user session after crash (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: i Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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
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 606 matching lines...) Expand 10 before | Expand all | Expand 10 after
875 prefs_given_name_update->RemoveWithoutPathExpansion(user_id, NULL); 898 prefs_given_name_update->RemoveWithoutPathExpansion(user_id, NULL);
876 899
877 DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail); 900 DictionaryPrefUpdate prefs_display_email_update(prefs, kUserDisplayEmail);
878 prefs_display_email_update->RemoveWithoutPathExpansion(user_id, NULL); 901 prefs_display_email_update->RemoveWithoutPathExpansion(user_id, NULL);
879 902
880 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus); 903 DictionaryPrefUpdate prefs_oauth_update(prefs, kUserOAuthTokenStatus);
881 prefs_oauth_update->RemoveWithoutPathExpansion(user_id, NULL); 904 prefs_oauth_update->RemoveWithoutPathExpansion(user_id, NULL);
882 905
883 DictionaryPrefUpdate prefs_force_online_update(prefs, kUserForceOnlineSignin); 906 DictionaryPrefUpdate prefs_force_online_update(prefs, kUserForceOnlineSignin);
884 prefs_force_online_update->RemoveWithoutPathExpansion(user_id, NULL); 907 prefs_force_online_update->RemoveWithoutPathExpansion(user_id, NULL);
908
909 std::string last_active_user = GetLocalState()->GetString(kLastActiveUser);
910 if (user_id == last_active_user)
911 GetLocalState()->SetString(kLastActiveUser, std::string());
885 } 912 }
886 913
887 User* UserManagerBase::RemoveRegularOrSupervisedUserFromList( 914 User* UserManagerBase::RemoveRegularOrSupervisedUserFromList(
888 const std::string& user_id) { 915 const std::string& user_id) {
889 ListPrefUpdate prefs_users_update(GetLocalState(), kRegularUsers); 916 ListPrefUpdate prefs_users_update(GetLocalState(), kRegularUsers);
890 prefs_users_update->Clear(); 917 prefs_users_update->Clear();
891 User* user = NULL; 918 User* user = NULL;
892 for (UserList::iterator it = users_.begin(); it != users_.end();) { 919 for (UserList::iterator it = users_.begin(); it != users_.end();) {
893 const std::string user_email = (*it)->email(); 920 const std::string user_email = (*it)->email();
894 if (user_email == user_id) { 921 if (user_email == user_id) {
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
956 if (primary_user_) { 983 if (primary_user_) {
957 chromeos::LoginState::Get()->SetLoggedInStateAndPrimaryUser( 984 chromeos::LoginState::Get()->SetLoggedInStateAndPrimaryUser(
958 logged_in_state, login_user_type, primary_user_->username_hash()); 985 logged_in_state, login_user_type, primary_user_->username_hash());
959 } else { 986 } else {
960 chromeos::LoginState::Get()->SetLoggedInState(logged_in_state, 987 chromeos::LoginState::Get()->SetLoggedInState(logged_in_state,
961 login_user_type); 988 login_user_type);
962 } 989 }
963 } 990 }
964 991
965 void UserManagerBase::SetLRUUser(User* user) { 992 void UserManagerBase::SetLRUUser(User* user) {
993 GetLocalState()->SetString(kLastActiveUser, user->email());
994 GetLocalState()->CommitPendingWrite();
995
966 UserList::iterator it = 996 UserList::iterator it =
967 std::find(lru_logged_in_users_.begin(), lru_logged_in_users_.end(), user); 997 std::find(lru_logged_in_users_.begin(), lru_logged_in_users_.end(), user);
968 if (it != lru_logged_in_users_.end()) 998 if (it != lru_logged_in_users_.end())
969 lru_logged_in_users_.erase(it); 999 lru_logged_in_users_.erase(it);
970 lru_logged_in_users_.insert(lru_logged_in_users_.begin(), user); 1000 lru_logged_in_users_.insert(lru_logged_in_users_.begin(), user);
971 } 1001 }
972 1002
973 void UserManagerBase::SendRegularUserLoginMetrics(const std::string& user_id) { 1003 void UserManagerBase::SendRegularUserLoginMetrics(const std::string& user_id) {
974 // If this isn't the first time Chrome was run after the system booted, 1004 // 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. 1005 // assume that Chrome was restarted because a previous session ended.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1020 } 1050 }
1021 1051
1022 void UserManagerBase::DeleteUser(User* user) { 1052 void UserManagerBase::DeleteUser(User* user) {
1023 const bool is_active_user = (user == active_user_); 1053 const bool is_active_user = (user == active_user_);
1024 delete user; 1054 delete user;
1025 if (is_active_user) 1055 if (is_active_user)
1026 active_user_ = NULL; 1056 active_user_ = NULL;
1027 } 1057 }
1028 1058
1029 } // namespace user_manager 1059 } // namespace user_manager
OLDNEW
« chrome/browser/profiles/profile_manager.cc ('K') | « components/user_manager/user_manager_base.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698