| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "chrome/browser/ui/ash/session_state_delegate_chromeos.h" | 5 #include "chrome/browser/ui/ash/session_state_delegate_chromeos.h" |
| 6 | 6 |
| 7 #include "ash/multi_profile_uma.h" | 7 #include "ash/multi_profile_uma.h" |
| 8 #include "ash/session/session_state_observer.h" | 8 #include "ash/session/session_state_observer.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "chrome/browser/chromeos/login/lock/screen_locker.h" | 12 #include "chrome/browser/chromeos/login/lock/screen_locker.h" |
| 13 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h" | 13 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h" |
| 14 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" | 14 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" |
| 15 #include "chrome/browser/chromeos/login/users/user_manager.h" | |
| 16 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 15 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile_manager.h" |
| 19 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" | 18 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" |
| 20 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 21 #include "chromeos/chromeos_switches.h" | 20 #include "chromeos/chromeos_switches.h" |
| 22 #include "chromeos/dbus/dbus_thread_manager.h" | 21 #include "chromeos/dbus/dbus_thread_manager.h" |
| 23 #include "chromeos/dbus/session_manager_client.h" | 22 #include "chromeos/dbus/session_manager_client.h" |
| 24 #include "chromeos/login/login_state.h" | 23 #include "chromeos/login/login_state.h" |
| 25 #include "components/user_manager/user.h" | 24 #include "components/user_manager/user.h" |
| 25 #include "components/user_manager/user_manager.h" |
| 26 #include "google_apis/gaia/gaia_auth_util.h" | 26 #include "google_apis/gaia/gaia_auth_util.h" |
| 27 | 27 |
| 28 SessionStateDelegateChromeos::SessionStateDelegateChromeos() | 28 SessionStateDelegateChromeos::SessionStateDelegateChromeos() |
| 29 : session_state_(SESSION_STATE_LOGIN_PRIMARY) { | 29 : session_state_(SESSION_STATE_LOGIN_PRIMARY) { |
| 30 chromeos::UserManager::Get()->AddSessionStateObserver(this); | 30 user_manager::UserManager::Get()->AddSessionStateObserver(this); |
| 31 chromeos::UserAddingScreen::Get()->AddObserver(this); | 31 chromeos::UserAddingScreen::Get()->AddObserver(this); |
| 32 | 32 |
| 33 // LoginState is not initialized in unit_tests. | 33 // LoginState is not initialized in unit_tests. |
| 34 if (chromeos::LoginState::IsInitialized()) { | 34 if (chromeos::LoginState::IsInitialized()) { |
| 35 chromeos::LoginState::Get()->AddObserver(this); | 35 chromeos::LoginState::Get()->AddObserver(this); |
| 36 SetSessionState(chromeos::LoginState::Get()->IsUserLoggedIn() ? | 36 SetSessionState(chromeos::LoginState::Get()->IsUserLoggedIn() ? |
| 37 SESSION_STATE_ACTIVE : SESSION_STATE_LOGIN_PRIMARY, true); | 37 SESSION_STATE_ACTIVE : SESSION_STATE_LOGIN_PRIMARY, true); |
| 38 } | 38 } |
| 39 } | 39 } |
| 40 | 40 |
| 41 SessionStateDelegateChromeos::~SessionStateDelegateChromeos() { | 41 SessionStateDelegateChromeos::~SessionStateDelegateChromeos() { |
| 42 chromeos::UserManager::Get()->RemoveSessionStateObserver(this); | 42 user_manager::UserManager::Get()->RemoveSessionStateObserver(this); |
| 43 chromeos::UserAddingScreen::Get()->RemoveObserver(this); | 43 chromeos::UserAddingScreen::Get()->RemoveObserver(this); |
| 44 | 44 |
| 45 // LoginState is not initialized in unit_tests. | 45 // LoginState is not initialized in unit_tests. |
| 46 if (chromeos::LoginState::IsInitialized()) | 46 if (chromeos::LoginState::IsInitialized()) |
| 47 chromeos::LoginState::Get()->RemoveObserver(this); | 47 chromeos::LoginState::Get()->RemoveObserver(this); |
| 48 } | 48 } |
| 49 | 49 |
| 50 content::BrowserContext* SessionStateDelegateChromeos::GetBrowserContextByIndex( | 50 content::BrowserContext* SessionStateDelegateChromeos::GetBrowserContextByIndex( |
| 51 ash::MultiProfileIndex index) { | 51 ash::MultiProfileIndex index) { |
| 52 DCHECK_LT(index, NumberOfLoggedInUsers()); | 52 DCHECK_LT(index, NumberOfLoggedInUsers()); |
| 53 user_manager::User* user = | 53 user_manager::User* user = |
| 54 chromeos::UserManager::Get()->GetLRULoggedInUsers()[index]; | 54 user_manager::UserManager::Get()->GetLRULoggedInUsers()[index]; |
| 55 DCHECK(user); | 55 DCHECK(user); |
| 56 return chromeos::ProfileHelper::Get()->GetProfileByUserUnsafe(user); | 56 return chromeos::ProfileHelper::Get()->GetProfileByUserUnsafe(user); |
| 57 } | 57 } |
| 58 | 58 |
| 59 content::BrowserContext* | 59 content::BrowserContext* |
| 60 SessionStateDelegateChromeos::GetBrowserContextForWindow( | 60 SessionStateDelegateChromeos::GetBrowserContextForWindow( |
| 61 aura::Window* window) { | 61 aura::Window* window) { |
| 62 const std::string& user_id = | 62 const std::string& user_id = |
| 63 chrome::MultiUserWindowManager::GetInstance()->GetWindowOwner(window); | 63 chrome::MultiUserWindowManager::GetInstance()->GetWindowOwner(window); |
| 64 const user_manager::User* user = | 64 const user_manager::User* user = |
| 65 chromeos::UserManager::Get()->FindUser(user_id); | 65 user_manager::UserManager::Get()->FindUser(user_id); |
| 66 DCHECK(user); | 66 DCHECK(user); |
| 67 return chromeos::ProfileHelper::Get()->GetProfileByUserUnsafe(user); | 67 return chromeos::ProfileHelper::Get()->GetProfileByUserUnsafe(user); |
| 68 } | 68 } |
| 69 | 69 |
| 70 int SessionStateDelegateChromeos::GetMaximumNumberOfLoggedInUsers() const { | 70 int SessionStateDelegateChromeos::GetMaximumNumberOfLoggedInUsers() const { |
| 71 // We limit list of logged in users to 10 due to memory constraints. | 71 // We limit list of logged in users to 10 due to memory constraints. |
| 72 // Note that 10 seems excessive, but we want to test how many users are | 72 // Note that 10 seems excessive, but we want to test how many users are |
| 73 // actually added to a session. | 73 // actually added to a session. |
| 74 // TODO(nkostylev): Adjust this limitation based on device capabilites. | 74 // TODO(nkostylev): Adjust this limitation based on device capabilites. |
| 75 // http://crbug.com/230865 | 75 // http://crbug.com/230865 |
| 76 return 10; | 76 return 10; |
| 77 } | 77 } |
| 78 | 78 |
| 79 int SessionStateDelegateChromeos::NumberOfLoggedInUsers() const { | 79 int SessionStateDelegateChromeos::NumberOfLoggedInUsers() const { |
| 80 return chromeos::UserManager::Get()->GetLoggedInUsers().size(); | 80 return user_manager::UserManager::Get()->GetLoggedInUsers().size(); |
| 81 } | 81 } |
| 82 | 82 |
| 83 bool SessionStateDelegateChromeos::IsActiveUserSessionStarted() const { | 83 bool SessionStateDelegateChromeos::IsActiveUserSessionStarted() const { |
| 84 return chromeos::UserManager::Get()->IsSessionStarted(); | 84 return user_manager::UserManager::Get()->IsSessionStarted(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 bool SessionStateDelegateChromeos::CanLockScreen() const { | 87 bool SessionStateDelegateChromeos::CanLockScreen() const { |
| 88 const user_manager::UserList unlock_users = | 88 const user_manager::UserList unlock_users = |
| 89 chromeos::UserManager::Get()->GetUnlockUsers(); | 89 user_manager::UserManager::Get()->GetUnlockUsers(); |
| 90 return !unlock_users.empty(); | 90 return !unlock_users.empty(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 bool SessionStateDelegateChromeos::IsScreenLocked() const { | 93 bool SessionStateDelegateChromeos::IsScreenLocked() const { |
| 94 return chromeos::ScreenLocker::default_screen_locker() && | 94 return chromeos::ScreenLocker::default_screen_locker() && |
| 95 chromeos::ScreenLocker::default_screen_locker()->locked(); | 95 chromeos::ScreenLocker::default_screen_locker()->locked(); |
| 96 } | 96 } |
| 97 | 97 |
| 98 bool SessionStateDelegateChromeos::ShouldLockScreenBeforeSuspending() const { | 98 bool SessionStateDelegateChromeos::ShouldLockScreenBeforeSuspending() const { |
| 99 const user_manager::UserList logged_in_users = | 99 const user_manager::UserList logged_in_users = |
| 100 chromeos::UserManager::Get()->GetLoggedInUsers(); | 100 user_manager::UserManager::Get()->GetLoggedInUsers(); |
| 101 for (user_manager::UserList::const_iterator it = logged_in_users.begin(); | 101 for (user_manager::UserList::const_iterator it = logged_in_users.begin(); |
| 102 it != logged_in_users.end(); | 102 it != logged_in_users.end(); |
| 103 ++it) { | 103 ++it) { |
| 104 user_manager::User* user = (*it); | 104 user_manager::User* user = (*it); |
| 105 Profile* profile = | 105 Profile* profile = |
| 106 chromeos::ProfileHelper::Get()->GetProfileByUserUnsafe(user); | 106 chromeos::ProfileHelper::Get()->GetProfileByUserUnsafe(user); |
| 107 if (profile->GetPrefs()->GetBoolean(prefs::kEnableAutoScreenLock)) | 107 if (profile->GetPrefs()->GetBoolean(prefs::kEnableAutoScreenLock)) |
| 108 return true; | 108 return true; |
| 109 } | 109 } |
| 110 return false; | 110 return false; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 133 } | 133 } |
| 134 | 134 |
| 135 ash::SessionStateDelegate::SessionState | 135 ash::SessionStateDelegate::SessionState |
| 136 SessionStateDelegateChromeos::GetSessionState() const { | 136 SessionStateDelegateChromeos::GetSessionState() const { |
| 137 return session_state_; | 137 return session_state_; |
| 138 } | 138 } |
| 139 | 139 |
| 140 const user_manager::UserInfo* SessionStateDelegateChromeos::GetUserInfo( | 140 const user_manager::UserInfo* SessionStateDelegateChromeos::GetUserInfo( |
| 141 ash::MultiProfileIndex index) const { | 141 ash::MultiProfileIndex index) const { |
| 142 DCHECK_LT(index, NumberOfLoggedInUsers()); | 142 DCHECK_LT(index, NumberOfLoggedInUsers()); |
| 143 return chromeos::UserManager::Get()->GetLRULoggedInUsers()[index]; | 143 return user_manager::UserManager::Get()->GetLRULoggedInUsers()[index]; |
| 144 } | 144 } |
| 145 | 145 |
| 146 const user_manager::UserInfo* SessionStateDelegateChromeos::GetUserInfo( | 146 const user_manager::UserInfo* SessionStateDelegateChromeos::GetUserInfo( |
| 147 content::BrowserContext* context) const { | 147 content::BrowserContext* context) const { |
| 148 DCHECK(context); | 148 DCHECK(context); |
| 149 return chromeos::ProfileHelper::Get()->GetUserByProfile( | 149 return chromeos::ProfileHelper::Get()->GetUserByProfile( |
| 150 Profile::FromBrowserContext(context)); | 150 Profile::FromBrowserContext(context)); |
| 151 } | 151 } |
| 152 | 152 |
| 153 bool SessionStateDelegateChromeos::ShouldShowAvatar( | 153 bool SessionStateDelegateChromeos::ShouldShowAvatar( |
| 154 aura::Window* window) const { | 154 aura::Window* window) const { |
| 155 return chrome::MultiUserWindowManager::GetInstance()-> | 155 return chrome::MultiUserWindowManager::GetInstance()-> |
| 156 ShouldShowAvatar(window); | 156 ShouldShowAvatar(window); |
| 157 } | 157 } |
| 158 | 158 |
| 159 void SessionStateDelegateChromeos::SwitchActiveUser( | 159 void SessionStateDelegateChromeos::SwitchActiveUser( |
| 160 const std::string& user_id) { | 160 const std::string& user_id) { |
| 161 // Disallow switching to an already active user since that might crash. | 161 // Disallow switching to an already active user since that might crash. |
| 162 // Also check that we got a user id and not an email address. | 162 // Also check that we got a user id and not an email address. |
| 163 DCHECK_EQ(user_id, | 163 DCHECK_EQ(user_id, |
| 164 gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_id))); | 164 gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_id))); |
| 165 if (user_id == chromeos::UserManager::Get()->GetActiveUser()->email()) | 165 if (user_id == user_manager::UserManager::Get()->GetActiveUser()->email()) |
| 166 return; | 166 return; |
| 167 chromeos::UserManager::Get()->SwitchActiveUser(user_id); | 167 user_manager::UserManager::Get()->SwitchActiveUser(user_id); |
| 168 } | 168 } |
| 169 | 169 |
| 170 void SessionStateDelegateChromeos::CycleActiveUser(CycleUser cycle_user) { | 170 void SessionStateDelegateChromeos::CycleActiveUser(CycleUser cycle_user) { |
| 171 // Make sure there is a user to switch to. | 171 // Make sure there is a user to switch to. |
| 172 if (NumberOfLoggedInUsers() <= 1) | 172 if (NumberOfLoggedInUsers() <= 1) |
| 173 return; | 173 return; |
| 174 | 174 |
| 175 const user_manager::UserList& logged_in_users = | 175 const user_manager::UserList& logged_in_users = |
| 176 chromeos::UserManager::Get()->GetLoggedInUsers(); | 176 user_manager::UserManager::Get()->GetLoggedInUsers(); |
| 177 | 177 |
| 178 std::string user_id = chromeos::UserManager::Get()->GetActiveUser()->email(); | 178 std::string user_id = |
| 179 user_manager::UserManager::Get()->GetActiveUser()->email(); |
| 179 | 180 |
| 180 // Get an iterator positioned at the active user. | 181 // Get an iterator positioned at the active user. |
| 181 user_manager::UserList::const_iterator it; | 182 user_manager::UserList::const_iterator it; |
| 182 for (it = logged_in_users.begin(); | 183 for (it = logged_in_users.begin(); |
| 183 it != logged_in_users.end(); ++it) { | 184 it != logged_in_users.end(); ++it) { |
| 184 if ((*it)->email() == user_id) | 185 if ((*it)->email() == user_id) |
| 185 break; | 186 break; |
| 186 } | 187 } |
| 187 | 188 |
| 188 // Active user not found. | 189 // Active user not found. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 199 user_id = (*it)->email(); | 200 user_id = (*it)->email(); |
| 200 break; | 201 break; |
| 201 case CYCLE_TO_PREVIOUS_USER: | 202 case CYCLE_TO_PREVIOUS_USER: |
| 202 if (it == logged_in_users.begin()) | 203 if (it == logged_in_users.begin()) |
| 203 it = logged_in_users.end(); | 204 it = logged_in_users.end(); |
| 204 user_id = (*(--it))->email(); | 205 user_id = (*(--it))->email(); |
| 205 break; | 206 break; |
| 206 } | 207 } |
| 207 | 208 |
| 208 // Switch using the transformed |user_id|. | 209 // Switch using the transformed |user_id|. |
| 209 chromeos::UserManager::Get()->SwitchActiveUser(user_id); | 210 user_manager::UserManager::Get()->SwitchActiveUser(user_id); |
| 210 } | 211 } |
| 211 | 212 |
| 212 bool SessionStateDelegateChromeos::IsMultiProfileAllowedByPrimaryUserPolicy() | 213 bool SessionStateDelegateChromeos::IsMultiProfileAllowedByPrimaryUserPolicy() |
| 213 const { | 214 const { |
| 214 return chromeos::MultiProfileUserController::GetPrimaryUserPolicy() == | 215 return chromeos::MultiProfileUserController::GetPrimaryUserPolicy() == |
| 215 chromeos::MultiProfileUserController::ALLOWED; | 216 chromeos::MultiProfileUserController::ALLOWED; |
| 216 } | 217 } |
| 217 | 218 |
| 218 void SessionStateDelegateChromeos::AddSessionStateObserver( | 219 void SessionStateDelegateChromeos::AddSessionStateObserver( |
| 219 ash::SessionStateObserver* observer) { | 220 ash::SessionStateObserver* observer) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 | 260 |
| 260 session_state_ = new_state; | 261 session_state_ = new_state; |
| 261 NotifySessionStateChanged(); | 262 NotifySessionStateChanged(); |
| 262 } | 263 } |
| 263 | 264 |
| 264 void SessionStateDelegateChromeos::NotifySessionStateChanged() { | 265 void SessionStateDelegateChromeos::NotifySessionStateChanged() { |
| 265 FOR_EACH_OBSERVER(ash::SessionStateObserver, | 266 FOR_EACH_OBSERVER(ash::SessionStateObserver, |
| 266 session_state_observer_list_, | 267 session_state_observer_list_, |
| 267 SessionStateChanged(session_state_)); | 268 SessionStateChanged(session_state_)); |
| 268 } | 269 } |
| OLD | NEW |