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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/ash/session_state_delegate_chromeos.h"
7 #include "ash/multi_profile_uma.h"
8 #include "ash/session_state_observer.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/chromeos/login/screen_locker.h"
13 #include "chrome/browser/chromeos/login/user.h"
14 #include "chrome/browser/chromeos/login/user_adding_screen.h"
15 #include "chrome/browser/chromeos/login/user_manager.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/browser/ui/ash/multi_user_window_manager.h"
19 #include "chrome/common/pref_names.h"
20 #include "chromeos/chromeos_switches.h"
21 #include "chromeos/dbus/dbus_thread_manager.h"
22 #include "chromeos/dbus/session_manager_client.h"
23 #include "google_apis/gaia/gaia_auth_util.h"
25 SessionStateDelegateChromeos::SessionStateDelegateChromeos() {
26 chromeos::UserManager::Get()->AddSessionStateObserver(this);
29 SessionStateDelegateChromeos::~SessionStateDelegateChromeos() {
32 int SessionStateDelegateChromeos::GetMaximumNumberOfLoggedInUsers() const {
36 int SessionStateDelegateChromeos::NumberOfLoggedInUsers() const {
37 return chromeos::UserManager::Get()->GetLoggedInUsers().size();
40 bool SessionStateDelegateChromeos::IsActiveUserSessionStarted() const {
41 return chromeos::UserManager::Get()->IsSessionStarted();
44 bool SessionStateDelegateChromeos::CanLockScreen() const {
45 return chromeos::UserManager::Get()->CanCurrentUserLock();
48 bool SessionStateDelegateChromeos::IsScreenLocked() const {
49 return chromeos::ScreenLocker::default_screen_locker() &&
50 chromeos::ScreenLocker::default_screen_locker()->locked();
53 bool SessionStateDelegateChromeos::ShouldLockScreenBeforeSuspending() const {
54 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
55 return profile && profile->GetPrefs()->GetBoolean(prefs::kEnableScreenLock);
58 void SessionStateDelegateChromeos::LockScreen() {
62 VLOG(1) << "Requesting screen lock from SessionStateDelegate";
63 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
67 void SessionStateDelegateChromeos::UnlockScreen() {
68 // This is used only for testing thus far.
72 bool SessionStateDelegateChromeos::IsUserSessionBlocked() const {
73 bool has_login_manager = CommandLine::ForCurrentProcess()->HasSwitch(
74 chromeos::switches::kLoginManager);
75 return (has_login_manager && !IsActiveUserSessionStarted()) ||
77 chromeos::UserAddingScreen::Get()->IsRunning();
80 const base::string16 SessionStateDelegateChromeos::GetUserDisplayName(
81 ash::MultiProfileIndex index) const {
82 DCHECK_LT(index, NumberOfLoggedInUsers());
83 return chromeos::UserManager::Get()->
84 GetLRULoggedInUsers()[index]->display_name();
87 const std::string SessionStateDelegateChromeos::GetUserEmail(
88 ash::MultiProfileIndex index) const {
89 DCHECK_LT(index, NumberOfLoggedInUsers());
90 return chromeos::UserManager::Get()->
91 GetLRULoggedInUsers()[index]->display_email();
94 const std::string SessionStateDelegateChromeos::GetUserID(
95 ash::MultiProfileIndex index) const {
96 DCHECK_LT(index, NumberOfLoggedInUsers());
97 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(
98 chromeos::UserManager::Get()->
99 GetLRULoggedInUsers()[index]->email()));
102 const gfx::ImageSkia& SessionStateDelegateChromeos::GetUserImage(
103 ash::MultiProfileIndex index) const {
104 DCHECK_LT(index, NumberOfLoggedInUsers());
105 return chromeos::UserManager::Get()->GetLRULoggedInUsers()[index]->image();
108 void SessionStateDelegateChromeos::GetLoggedInUsers(ash::UserIdList* users) {
109 const chromeos::UserList& logged_in_users =
110 chromeos::UserManager::Get()->GetLoggedInUsers();
111 for (chromeos::UserList::const_iterator it = logged_in_users.begin();
112 it != logged_in_users.end(); ++it) {
113 const chromeos::User* user = (*it);
114 users->push_back(user->email());
118 void SessionStateDelegateChromeos::SwitchActiveUser(
119 const std::string& user_id) {
120 // Disallow switching to an already active user since that might crash.
121 // Also check that we got a user id and not an email address.
123 gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_id)));
124 if (user_id == chromeos::UserManager::Get()->GetActiveUser()->email())
126 chromeos::UserManager::Get()->SwitchActiveUser(user_id);
129 void SessionStateDelegateChromeos::SwitchActiveUserToNext() {
130 // Make sure there is a user to switch to.
131 if (NumberOfLoggedInUsers() <= 1)
134 const chromeos::UserList& logged_in_users =
135 chromeos::UserManager::Get()->GetLoggedInUsers();
137 std::string user_id = chromeos::UserManager::Get()->GetActiveUser()->email();
139 // Get an iterator positioned at the active user.
140 chromeos::UserList::const_iterator it;
141 for (it = logged_in_users.begin();
142 it != logged_in_users.end(); ++it) {
143 if ((*it)->email() == user_id)
147 // Active user not found.
148 if (it == logged_in_users.end())
151 // Get the next user's email, wrapping to the start of the list if necessary.
152 if (++it == logged_in_users.end())
153 user_id = (*logged_in_users.begin())->email();
155 user_id = (*it)->email();
157 // Switch using the transformed |user_id|.
158 chromeos::UserManager::Get()->SwitchActiveUser(user_id);
161 void SessionStateDelegateChromeos::AddSessionStateObserver(
162 ash::SessionStateObserver* observer) {
163 session_state_observer_list_.AddObserver(observer);
166 void SessionStateDelegateChromeos::RemoveSessionStateObserver(
167 ash::SessionStateObserver* observer) {
168 session_state_observer_list_.RemoveObserver(observer);
171 bool SessionStateDelegateChromeos::TransferWindowToDesktopOfUser(
172 aura::Window* window,
173 ash::MultiProfileIndex index) {
174 chrome::MultiUserWindowManager* window_manager =
175 chrome::MultiUserWindowManager::GetInstance();
176 if (!window_manager || window_manager->GetWindowOwner(window).empty())
179 ash::MultiProfileUMA::RecordTeleportAction(
180 ash::MultiProfileUMA::TELEPORT_WINDOW_DRAG_AND_DROP);
182 DCHECK_LT(index, NumberOfLoggedInUsers());
183 window_manager->ShowWindowForUser(window,
184 chromeos::UserManager::Get()->GetLRULoggedInUsers()[index]->email());
188 void SessionStateDelegateChromeos::ActiveUserChanged(
189 const chromeos::User* active_user) {
190 FOR_EACH_OBSERVER(ash::SessionStateObserver,
191 session_state_observer_list_,
192 ActiveUserChanged(active_user->email()));
195 void SessionStateDelegateChromeos::UserAddedToSession(
196 const chromeos::User* added_user) {
197 FOR_EACH_OBSERVER(ash::SessionStateObserver,
198 session_state_observer_list_,
199 UserAddedToSession(added_user->email()));