Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / ash / session_state_delegate_chromeos.cc
blobb36175591a53cb13f32d9c53b56e0d3fd6dee81a
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/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 {
33 // TODO(nkostylev): Show some UI messages why no more users could be added
34 // to this session. http://crbug.com/230863
35 // We limit list of logged in users to 10 due to memory constraints.
36 // Note that 10 seems excessive, but we want to test how many users are
37 // actually added to a session.
38 // TODO(nkostylev): Adjust this limitation based on device capabilites.
39 // http://crbug.com/230865
40 return 10;
43 int SessionStateDelegateChromeos::NumberOfLoggedInUsers() const {
44 return chromeos::UserManager::Get()->GetLoggedInUsers().size();
47 bool SessionStateDelegateChromeos::IsActiveUserSessionStarted() const {
48 return chromeos::UserManager::Get()->IsSessionStarted();
51 bool SessionStateDelegateChromeos::CanLockScreen() const {
52 const chromeos::UserList unlock_users =
53 chromeos::UserManager::Get()->GetUnlockUsers();
54 DCHECK_LE(unlock_users.size(), 1u);
55 return !unlock_users.empty() && unlock_users[0]->can_lock();
58 bool SessionStateDelegateChromeos::IsScreenLocked() const {
59 return chromeos::ScreenLocker::default_screen_locker() &&
60 chromeos::ScreenLocker::default_screen_locker()->locked();
63 bool SessionStateDelegateChromeos::ShouldLockScreenBeforeSuspending() const {
64 const chromeos::UserList unlock_users =
65 chromeos::UserManager::Get()->GetUnlockUsers();
66 DCHECK_LE(unlock_users.size(), 1u);
67 Profile* profile =
68 !unlock_users.empty()
69 ? chromeos::UserManager::Get()->GetProfileByUser(unlock_users[0])
70 : NULL;
71 return profile && profile->GetPrefs()->GetBoolean(prefs::kEnableScreenLock);
74 void SessionStateDelegateChromeos::LockScreen() {
75 if (!CanLockScreen())
76 return;
78 VLOG(1) << "Requesting screen lock from SessionStateDelegate";
79 chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
80 RequestLockScreen();
83 void SessionStateDelegateChromeos::UnlockScreen() {
84 // This is used only for testing thus far.
85 NOTIMPLEMENTED();
88 bool SessionStateDelegateChromeos::IsUserSessionBlocked() const {
89 bool has_login_manager = CommandLine::ForCurrentProcess()->HasSwitch(
90 chromeos::switches::kLoginManager);
91 return (has_login_manager && !IsActiveUserSessionStarted()) ||
92 IsScreenLocked() ||
93 chromeos::UserAddingScreen::Get()->IsRunning();
96 const base::string16 SessionStateDelegateChromeos::GetUserDisplayName(
97 ash::MultiProfileIndex index) const {
98 DCHECK_LT(index, NumberOfLoggedInUsers());
99 return chromeos::UserManager::Get()->
100 GetLRULoggedInUsers()[index]->display_name();
103 const std::string SessionStateDelegateChromeos::GetUserEmail(
104 ash::MultiProfileIndex index) const {
105 DCHECK_LT(index, NumberOfLoggedInUsers());
106 return chromeos::UserManager::Get()->
107 GetLRULoggedInUsers()[index]->display_email();
110 const std::string SessionStateDelegateChromeos::GetUserID(
111 ash::MultiProfileIndex index) const {
112 DCHECK_LT(index, NumberOfLoggedInUsers());
113 return gaia::CanonicalizeEmail(gaia::SanitizeEmail(
114 chromeos::UserManager::Get()->
115 GetLRULoggedInUsers()[index]->email()));
118 const gfx::ImageSkia& SessionStateDelegateChromeos::GetUserImage(
119 ash::MultiProfileIndex index) const {
120 DCHECK_LT(index, NumberOfLoggedInUsers());
121 return chromeos::UserManager::Get()->GetLRULoggedInUsers()[index]->image();
124 void SessionStateDelegateChromeos::GetLoggedInUsers(ash::UserIdList* users) {
125 const chromeos::UserList& logged_in_users =
126 chromeos::UserManager::Get()->GetLoggedInUsers();
127 for (chromeos::UserList::const_iterator it = logged_in_users.begin();
128 it != logged_in_users.end(); ++it) {
129 const chromeos::User* user = (*it);
130 users->push_back(user->email());
134 void SessionStateDelegateChromeos::SwitchActiveUser(
135 const std::string& user_id) {
136 // Disallow switching to an already active user since that might crash.
137 // Also check that we got a user id and not an email address.
138 DCHECK_EQ(user_id,
139 gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_id)));
140 if (user_id == chromeos::UserManager::Get()->GetActiveUser()->email())
141 return;
142 chromeos::UserManager::Get()->SwitchActiveUser(user_id);
145 void SessionStateDelegateChromeos::CycleActiveUser(CycleUser cycle_user) {
146 // Make sure there is a user to switch to.
147 if (NumberOfLoggedInUsers() <= 1)
148 return;
150 const chromeos::UserList& logged_in_users =
151 chromeos::UserManager::Get()->GetLoggedInUsers();
153 std::string user_id = chromeos::UserManager::Get()->GetActiveUser()->email();
155 // Get an iterator positioned at the active user.
156 chromeos::UserList::const_iterator it;
157 for (it = logged_in_users.begin();
158 it != logged_in_users.end(); ++it) {
159 if ((*it)->email() == user_id)
160 break;
163 // Active user not found.
164 if (it == logged_in_users.end())
165 return;
167 // Get the user's email to select, wrapping to the start/end of the list if
168 // necessary.
169 switch (cycle_user) {
170 case CYCLE_TO_NEXT_USER:
171 if (++it == logged_in_users.end())
172 user_id = (*logged_in_users.begin())->email();
173 else
174 user_id = (*it)->email();
175 break;
176 case CYCLE_TO_PREVIOUS_USER:
177 if (it == logged_in_users.begin())
178 it = logged_in_users.end();
179 user_id = (*(--it))->email();
180 break;
183 // Switch using the transformed |user_id|.
184 chromeos::UserManager::Get()->SwitchActiveUser(user_id);
187 void SessionStateDelegateChromeos::AddSessionStateObserver(
188 ash::SessionStateObserver* observer) {
189 session_state_observer_list_.AddObserver(observer);
192 void SessionStateDelegateChromeos::RemoveSessionStateObserver(
193 ash::SessionStateObserver* observer) {
194 session_state_observer_list_.RemoveObserver(observer);
197 bool SessionStateDelegateChromeos::TransferWindowToDesktopOfUser(
198 aura::Window* window,
199 ash::MultiProfileIndex index) {
200 if (chrome::MultiUserWindowManager::GetMultiProfileMode() !=
201 chrome::MultiUserWindowManager::MULTI_PROFILE_MODE_SEPARATED)
202 return false;
203 chrome::MultiUserWindowManager* window_manager =
204 chrome::MultiUserWindowManager::GetInstance();
205 if (window_manager->GetWindowOwner(window).empty())
206 return false;
208 ash::MultiProfileUMA::RecordTeleportAction(
209 ash::MultiProfileUMA::TELEPORT_WINDOW_DRAG_AND_DROP);
211 DCHECK_LT(index, NumberOfLoggedInUsers());
212 window_manager->ShowWindowForUser(window,
213 chromeos::UserManager::Get()->GetLRULoggedInUsers()[index]->email());
214 return true;
217 void SessionStateDelegateChromeos::ActiveUserChanged(
218 const chromeos::User* active_user) {
219 FOR_EACH_OBSERVER(ash::SessionStateObserver,
220 session_state_observer_list_,
221 ActiveUserChanged(active_user->email()));
224 void SessionStateDelegateChromeos::UserAddedToSession(
225 const chromeos::User* added_user) {
226 FOR_EACH_OBSERVER(ash::SessionStateObserver,
227 session_state_observer_list_,
228 UserAddedToSession(added_user->email()));