1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/session_state_delegate_chromeos.h"
#include "ash/multi_profile_uma.h"
#include "ash/session/session_state_observer.h"
#include "ash/system/chromeos/multi_user/user_switch_util.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/chromeos/login/lock/screen_locker.h"
#include "chrome/browser/chromeos/login/ui/user_adding_screen.h"
#include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
#include "chrome/common/pref_names.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/session_manager_client.h"
#include "chromeos/login/login_state.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "google_apis/gaia/gaia_auth_util.h"
SessionStateDelegateChromeos::SessionStateDelegateChromeos()
: session_state_(SESSION_STATE_LOGIN_PRIMARY) {
user_manager::UserManager::Get()->AddSessionStateObserver(this);
chromeos::UserAddingScreen::Get()->AddObserver(this);
// LoginState is not initialized in unit_tests.
if (chromeos::LoginState::IsInitialized()) {
chromeos::LoginState::Get()->AddObserver(this);
SetSessionState(chromeos::LoginState::Get()->IsUserLoggedIn() ?
SESSION_STATE_ACTIVE : SESSION_STATE_LOGIN_PRIMARY, true);
}
}
SessionStateDelegateChromeos::~SessionStateDelegateChromeos() {
user_manager::UserManager::Get()->RemoveSessionStateObserver(this);
chromeos::UserAddingScreen::Get()->RemoveObserver(this);
// LoginState is not initialized in unit_tests.
if (chromeos::LoginState::IsInitialized())
chromeos::LoginState::Get()->RemoveObserver(this);
}
content::BrowserContext* SessionStateDelegateChromeos::GetBrowserContextByIndex(
ash::MultiProfileIndex index) {
DCHECK_LT(index, NumberOfLoggedInUsers());
user_manager::User* user =
user_manager::UserManager::Get()->GetLRULoggedInUsers()[index];
CHECK(user);
return chromeos::ProfileHelper::Get()->GetProfileByUserUnsafe(user);
}
content::BrowserContext*
SessionStateDelegateChromeos::GetBrowserContextForWindow(
aura::Window* window) {
const std::string& user_id =
chrome::MultiUserWindowManager::GetInstance()->GetWindowOwner(window);
const user_manager::User* user =
user_manager::UserManager::Get()->FindUser(user_id);
DCHECK(user);
return chromeos::ProfileHelper::Get()->GetProfileByUserUnsafe(user);
}
int SessionStateDelegateChromeos::GetMaximumNumberOfLoggedInUsers() const {
// We limit list of logged in users to 10 due to memory constraints.
// Note that 10 seems excessive, but we want to test how many users are
// actually added to a session.
// TODO(nkostylev): Adjust this limitation based on device capabilites.
// http://crbug.com/230865
return 10;
}
int SessionStateDelegateChromeos::NumberOfLoggedInUsers() const {
return user_manager::UserManager::Get()->GetLoggedInUsers().size();
}
bool SessionStateDelegateChromeos::CanAddUserToMultiProfile(
AddUserError* error) const {
if (user_manager::UserManager::Get()
->GetUsersAllowedForMultiProfile()
.size() == 0) {
if (error)
*error = ADD_USER_ERROR_OUT_OF_USERS;
return false;
}
return SessionStateDelegate::CanAddUserToMultiProfile(error);
}
bool SessionStateDelegateChromeos::IsActiveUserSessionStarted() const {
return user_manager::UserManager::Get()->IsSessionStarted();
}
bool SessionStateDelegateChromeos::CanLockScreen() const {
const user_manager::UserList unlock_users =
user_manager::UserManager::Get()->GetUnlockUsers();
return !unlock_users.empty();
}
bool SessionStateDelegateChromeos::IsScreenLocked() const {
return chromeos::ScreenLocker::default_screen_locker() &&
chromeos::ScreenLocker::default_screen_locker()->locked();
}
bool SessionStateDelegateChromeos::ShouldLockScreenBeforeSuspending() const {
const user_manager::UserList logged_in_users =
user_manager::UserManager::Get()->GetLoggedInUsers();
for (user_manager::UserList::const_iterator it = logged_in_users.begin();
it != logged_in_users.end();
++it) {
user_manager::User* user = (*it);
Profile* profile =
chromeos::ProfileHelper::Get()->GetProfileByUserUnsafe(user);
if (profile->GetPrefs()->GetBoolean(prefs::kEnableAutoScreenLock))
return true;
}
return false;
}
void SessionStateDelegateChromeos::LockScreen() {
if (!CanLockScreen())
return;
VLOG(1) << "Requesting screen lock from SessionStateDelegate";
chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
RequestLockScreen();
}
void SessionStateDelegateChromeos::UnlockScreen() {
// This is used only for testing thus far.
NOTIMPLEMENTED();
}
bool SessionStateDelegateChromeos::IsUserSessionBlocked() const {
bool has_login_manager = base::CommandLine::ForCurrentProcess()->HasSwitch(
chromeos::switches::kLoginManager);
return (has_login_manager && !IsActiveUserSessionStarted()) ||
IsScreenLocked() ||
chromeos::UserAddingScreen::Get()->IsRunning();
}
ash::SessionStateDelegate::SessionState
SessionStateDelegateChromeos::GetSessionState() const {
return session_state_;
}
const user_manager::UserInfo* SessionStateDelegateChromeos::GetUserInfo(
ash::MultiProfileIndex index) const {
DCHECK_LT(index, NumberOfLoggedInUsers());
return user_manager::UserManager::Get()->GetLRULoggedInUsers()[index];
}
const user_manager::UserInfo* SessionStateDelegateChromeos::GetUserInfo(
content::BrowserContext* context) const {
DCHECK(context);
return chromeos::ProfileHelper::Get()->GetUserByProfile(
Profile::FromBrowserContext(context));
}
bool SessionStateDelegateChromeos::ShouldShowAvatar(
aura::Window* window) const {
return chrome::MultiUserWindowManager::GetInstance()->
ShouldShowAvatar(window);
}
void SessionStateDelegateChromeos::SwitchActiveUser(
const std::string& user_id) {
// Disallow switching to an already active user since that might crash.
// Also check that we got a user id and not an email address.
DCHECK_EQ(user_id,
gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_id)));
if (user_id == user_manager::UserManager::Get()->GetActiveUser()->email())
return;
TryToSwitchUser(user_id);
}
void SessionStateDelegateChromeos::CycleActiveUser(CycleUser cycle_user) {
// Make sure there is a user to switch to.
if (NumberOfLoggedInUsers() <= 1)
return;
const user_manager::UserList& logged_in_users =
user_manager::UserManager::Get()->GetLoggedInUsers();
std::string user_id =
user_manager::UserManager::Get()->GetActiveUser()->email();
// Get an iterator positioned at the active user.
user_manager::UserList::const_iterator it;
for (it = logged_in_users.begin();
it != logged_in_users.end(); ++it) {
if ((*it)->email() == user_id)
break;
}
// Active user not found.
if (it == logged_in_users.end())
return;
// Get the user's email to select, wrapping to the start/end of the list if
// necessary.
switch (cycle_user) {
case CYCLE_TO_NEXT_USER:
if (++it == logged_in_users.end())
user_id = (*logged_in_users.begin())->email();
else
user_id = (*it)->email();
break;
case CYCLE_TO_PREVIOUS_USER:
if (it == logged_in_users.begin())
it = logged_in_users.end();
user_id = (*(--it))->email();
break;
}
// Switch using the transformed |user_id|.
TryToSwitchUser(user_id);
}
bool SessionStateDelegateChromeos::IsMultiProfileAllowedByPrimaryUserPolicy()
const {
return chromeos::MultiProfileUserController::GetPrimaryUserPolicy() ==
chromeos::MultiProfileUserController::ALLOWED;
}
void SessionStateDelegateChromeos::AddSessionStateObserver(
ash::SessionStateObserver* observer) {
session_state_observer_list_.AddObserver(observer);
}
void SessionStateDelegateChromeos::RemoveSessionStateObserver(
ash::SessionStateObserver* observer) {
session_state_observer_list_.RemoveObserver(observer);
}
void SessionStateDelegateChromeos::LoggedInStateChanged() {
SetSessionState(chromeos::LoginState::Get()->IsUserLoggedIn() ?
SESSION_STATE_ACTIVE : SESSION_STATE_LOGIN_PRIMARY, false);
}
void SessionStateDelegateChromeos::ActiveUserChanged(
const user_manager::User* active_user) {
FOR_EACH_OBSERVER(ash::SessionStateObserver,
session_state_observer_list_,
ActiveUserChanged(active_user->email()));
}
void SessionStateDelegateChromeos::UserAddedToSession(
const user_manager::User* added_user) {
FOR_EACH_OBSERVER(ash::SessionStateObserver,
session_state_observer_list_,
UserAddedToSession(added_user->email()));
}
void SessionStateDelegateChromeos::OnUserAddingStarted() {
SetSessionState(SESSION_STATE_LOGIN_SECONDARY, false);
}
void SessionStateDelegateChromeos::OnUserAddingFinished() {
SetSessionState(SESSION_STATE_ACTIVE, false);
}
void SessionStateDelegateChromeos::SetSessionState(SessionState new_state,
bool force) {
if (session_state_ == new_state && !force)
return;
session_state_ = new_state;
NotifySessionStateChanged();
}
void SessionStateDelegateChromeos::NotifySessionStateChanged() {
FOR_EACH_OBSERVER(ash::SessionStateObserver,
session_state_observer_list_,
SessionStateChanged(session_state_));
}
void DoSwitchUser(const std::string& user_id) {
user_manager::UserManager::Get()->SwitchActiveUser(user_id);
}
void SessionStateDelegateChromeos::TryToSwitchUser(
const std::string& user_id) {
ash::TrySwitchingActiveUser(base::Bind(&DoSwitchUser, user_id));
}
|