- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / ash / session_state_delegate_chromeos.cc
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.
4
5 #include "chrome/browser/ui/ash/session_state_delegate_chromeos.h"
6
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"
24
25 SessionStateDelegateChromeos::SessionStateDelegateChromeos() {
26   chromeos::UserManager::Get()->AddSessionStateObserver(this);
27 }
28
29 SessionStateDelegateChromeos::~SessionStateDelegateChromeos() {
30 }
31
32 int SessionStateDelegateChromeos::GetMaximumNumberOfLoggedInUsers() const {
33   return 3;
34 }
35
36 int SessionStateDelegateChromeos::NumberOfLoggedInUsers() const {
37   return chromeos::UserManager::Get()->GetLoggedInUsers().size();
38 }
39
40 bool SessionStateDelegateChromeos::IsActiveUserSessionStarted() const {
41   return chromeos::UserManager::Get()->IsSessionStarted();
42 }
43
44 bool SessionStateDelegateChromeos::CanLockScreen() const {
45   return chromeos::UserManager::Get()->CanCurrentUserLock();
46 }
47
48 bool SessionStateDelegateChromeos::IsScreenLocked() const {
49   return chromeos::ScreenLocker::default_screen_locker() &&
50          chromeos::ScreenLocker::default_screen_locker()->locked();
51 }
52
53 bool SessionStateDelegateChromeos::ShouldLockScreenBeforeSuspending() const {
54   Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
55   return profile && profile->GetPrefs()->GetBoolean(prefs::kEnableScreenLock);
56 }
57
58 void SessionStateDelegateChromeos::LockScreen() {
59   if (!CanLockScreen())
60     return;
61
62   VLOG(1) << "Requesting screen lock from SessionStateDelegate";
63   chromeos::DBusThreadManager::Get()->GetSessionManagerClient()->
64       RequestLockScreen();
65 }
66
67 void SessionStateDelegateChromeos::UnlockScreen() {
68   // This is used only for testing thus far.
69   NOTIMPLEMENTED();
70 }
71
72 bool SessionStateDelegateChromeos::IsUserSessionBlocked() const {
73   bool has_login_manager = CommandLine::ForCurrentProcess()->HasSwitch(
74           chromeos::switches::kLoginManager);
75   return (has_login_manager && !IsActiveUserSessionStarted()) ||
76          IsScreenLocked() ||
77          chromeos::UserAddingScreen::Get()->IsRunning();
78 }
79
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();
85 }
86
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();
92 }
93
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()));
100 }
101
102 const gfx::ImageSkia& SessionStateDelegateChromeos::GetUserImage(
103     ash::MultiProfileIndex index) const {
104   DCHECK_LT(index, NumberOfLoggedInUsers());
105   return chromeos::UserManager::Get()->GetLRULoggedInUsers()[index]->image();
106 }
107
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());
115   }
116 }
117
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.
122   DCHECK_EQ(user_id,
123             gaia::CanonicalizeEmail(gaia::SanitizeEmail(user_id)));
124   if (user_id == chromeos::UserManager::Get()->GetActiveUser()->email())
125     return;
126   chromeos::UserManager::Get()->SwitchActiveUser(user_id);
127 }
128
129 void SessionStateDelegateChromeos::SwitchActiveUserToNext() {
130   // Make sure there is a user to switch to.
131   if (NumberOfLoggedInUsers() <= 1)
132     return;
133
134   const chromeos::UserList& logged_in_users =
135       chromeos::UserManager::Get()->GetLoggedInUsers();
136
137   std::string user_id = chromeos::UserManager::Get()->GetActiveUser()->email();
138
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)
144       break;
145   }
146
147   // Active user not found.
148   if (it == logged_in_users.end())
149     return;
150
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();
154   else
155     user_id = (*it)->email();
156
157   // Switch using the transformed |user_id|.
158   chromeos::UserManager::Get()->SwitchActiveUser(user_id);
159 }
160
161 void SessionStateDelegateChromeos::AddSessionStateObserver(
162     ash::SessionStateObserver* observer) {
163   session_state_observer_list_.AddObserver(observer);
164 }
165
166 void SessionStateDelegateChromeos::RemoveSessionStateObserver(
167     ash::SessionStateObserver* observer) {
168   session_state_observer_list_.RemoveObserver(observer);
169 }
170
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())
177     return false;
178
179   ash::MultiProfileUMA::RecordTeleportAction(
180       ash::MultiProfileUMA::TELEPORT_WINDOW_DRAG_AND_DROP);
181
182   DCHECK_LT(index, NumberOfLoggedInUsers());
183   window_manager->ShowWindowForUser(window,
184       chromeos::UserManager::Get()->GetLRULoggedInUsers()[index]->email());
185   return true;
186 }
187
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()));
193 }
194
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()));
200 }