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/common/session/session_state_observer.h" | |
8 #include "ash/common/wm_window.h" | 7 #include "ash/common/wm_window.h" |
9 #include "ash/content/shell_content_state.h" | 8 #include "ash/content/shell_content_state.h" |
10 #include "base/bind.h" | |
11 #include "base/callback.h" | |
12 #include "base/command_line.h" | |
13 #include "base/logging.h" | |
14 #include "chrome/browser/chromeos/login/lock/screen_locker.h" | |
15 #include "chrome/browser/chromeos/login/ui/user_adding_screen.h" | |
16 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" | 9 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" |
17 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
18 #include "chrome/browser/profiles/profile.h" | |
19 #include "chrome/browser/profiles/profile_manager.h" | |
20 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" | 10 #include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h" |
21 #include "chrome/browser/ui/ash/session_controller_client.h" | |
22 #include "chrome/browser/ui/ash/session_util.h" | 11 #include "chrome/browser/ui/ash/session_util.h" |
23 #include "chromeos/chromeos_switches.h" | |
24 #include "chromeos/login/login_state.h" | |
25 #include "components/session_manager/core/session_manager.h" | |
26 #include "components/signin/core/account_id/account_id.h" | |
27 #include "components/user_manager/user.h" | |
28 #include "components/user_manager/user_info.h" | |
29 #include "components/user_manager/user_manager.h" | |
30 #include "google_apis/gaia/gaia_auth_util.h" | |
31 #include "ui/gfx/image/image_skia.h" | 12 #include "ui/gfx/image/image_skia.h" |
32 | 13 |
33 SessionStateDelegateChromeos::SessionStateDelegateChromeos() | 14 SessionStateDelegateChromeos::SessionStateDelegateChromeos() {} |
34 : session_state_(session_manager::SessionState::LOGIN_PRIMARY) { | |
35 user_manager::UserManager::Get()->AddSessionStateObserver(this); | |
36 chromeos::UserAddingScreen::Get()->AddObserver(this); | |
37 | 15 |
38 // LoginState is not initialized in unit_tests. | 16 SessionStateDelegateChromeos::~SessionStateDelegateChromeos() {} |
39 if (chromeos::LoginState::IsInitialized()) { | |
40 chromeos::LoginState::Get()->AddObserver(this); | |
41 // Note that the session state is only set to ACTIVE or LOGIN_PRIMARY | |
42 // instead of using SessionManager::Get()->session_state(). This is | |
43 // an intermediate state of replacing SessionStateDelegate with | |
44 // mojo interfaces. The replacement mojo interface would reflect | |
45 // real session state in SessionManager and have getters to translate | |
46 // them in a sensible way to ash code. | |
47 SetSessionState(chromeos::LoginState::Get()->IsUserLoggedIn() | |
48 ? session_manager::SessionState::ACTIVE | |
49 : session_manager::SessionState::LOGIN_PRIMARY, | |
50 true); | |
51 } | |
52 } | |
53 | |
54 SessionStateDelegateChromeos::~SessionStateDelegateChromeos() { | |
55 user_manager::UserManager::Get()->RemoveSessionStateObserver(this); | |
56 chromeos::UserAddingScreen::Get()->RemoveObserver(this); | |
57 | |
58 // LoginState is not initialized in unit_tests. | |
59 if (chromeos::LoginState::IsInitialized()) | |
60 chromeos::LoginState::Get()->RemoveObserver(this); | |
61 } | |
62 | |
63 int SessionStateDelegateChromeos::GetMaximumNumberOfLoggedInUsers() const { | |
64 // We limit list of logged in users to 10 due to memory constraints. | |
65 // Note that 10 seems excessive, but we want to test how many users are | |
66 // actually added to a session. | |
67 // TODO(nkostylev): Adjust this limitation based on device capabilites. | |
68 // http://crbug.com/230865 | |
69 return session_manager::kMaxmiumNumberOfUserSessions; | |
70 } | |
71 | |
72 int SessionStateDelegateChromeos::NumberOfLoggedInUsers() const { | |
73 return user_manager::UserManager::Get()->GetLoggedInUsers().size(); | |
74 } | |
75 | |
76 ash::AddUserSessionPolicy | |
77 SessionStateDelegateChromeos::GetAddUserSessionPolicy() const { | |
78 return SessionControllerClient::GetAddUserSessionPolicy(); | |
79 } | |
80 | |
81 bool SessionStateDelegateChromeos::IsActiveUserSessionStarted() const { | |
82 return session_manager::SessionManager::Get() && | |
83 session_manager::SessionManager::Get()->IsSessionStarted(); | |
84 } | |
85 | |
86 bool SessionStateDelegateChromeos::CanLockScreen() const { | |
87 return SessionControllerClient::CanLockScreen(); | |
88 } | |
89 | |
90 bool SessionStateDelegateChromeos::IsScreenLocked() const { | |
91 return chromeos::ScreenLocker::default_screen_locker() && | |
92 chromeos::ScreenLocker::default_screen_locker()->locked(); | |
93 } | |
94 | |
95 bool SessionStateDelegateChromeos::ShouldLockScreenAutomatically() const { | |
96 return SessionControllerClient::ShouldLockScreenAutomatically(); | |
97 } | |
98 | |
99 void SessionStateDelegateChromeos::LockScreen() { | |
100 return SessionControllerClient::DoLockScreen(); | |
101 } | |
102 | |
103 void SessionStateDelegateChromeos::UnlockScreen() { | |
104 // This is used only for testing thus far. | |
105 NOTIMPLEMENTED(); | |
106 } | |
107 | |
108 bool SessionStateDelegateChromeos::IsUserSessionBlocked() const { | |
109 bool has_login_manager = base::CommandLine::ForCurrentProcess()->HasSwitch( | |
110 chromeos::switches::kLoginManager); | |
111 return (has_login_manager && !IsActiveUserSessionStarted()) || | |
112 IsScreenLocked() || | |
113 chromeos::UserAddingScreen::Get()->IsRunning(); | |
114 } | |
115 | |
116 session_manager::SessionState SessionStateDelegateChromeos::GetSessionState() | |
117 const { | |
118 return session_state_; | |
119 } | |
120 | |
121 const user_manager::UserInfo* SessionStateDelegateChromeos::GetUserInfo( | |
122 ash::UserIndex index) const { | |
123 DCHECK_LT(index, NumberOfLoggedInUsers()); | |
124 return user_manager::UserManager::Get()->GetLRULoggedInUsers()[index]; | |
125 } | |
126 | 17 |
127 bool SessionStateDelegateChromeos::ShouldShowAvatar( | 18 bool SessionStateDelegateChromeos::ShouldShowAvatar( |
128 ash::WmWindow* window) const { | 19 ash::WmWindow* window) const { |
129 return chrome::MultiUserWindowManager::GetInstance()->ShouldShowAvatar( | 20 return chrome::MultiUserWindowManager::GetInstance()->ShouldShowAvatar( |
130 ash::WmWindow::GetAuraWindow(window)); | 21 ash::WmWindow::GetAuraWindow(window)); |
131 } | 22 } |
132 | 23 |
133 gfx::ImageSkia SessionStateDelegateChromeos::GetAvatarImageForWindow( | 24 gfx::ImageSkia SessionStateDelegateChromeos::GetAvatarImageForWindow( |
134 ash::WmWindow* window) const { | 25 ash::WmWindow* window) const { |
135 content::BrowserContext* context = | 26 content::BrowserContext* context = |
136 ash::ShellContentState::GetInstance()->GetBrowserContextForWindow( | 27 ash::ShellContentState::GetInstance()->GetBrowserContextForWindow( |
137 ash::WmWindow::GetAuraWindow(window)); | 28 ash::WmWindow::GetAuraWindow(window)); |
138 return GetAvatarImageForContext(context); | 29 return GetAvatarImageForContext(context); |
139 } | 30 } |
140 | |
141 void SessionStateDelegateChromeos::SwitchActiveUser( | |
142 const AccountId& account_id) { | |
143 SessionControllerClient::DoSwitchActiveUser(account_id); | |
144 } | |
145 | |
146 void SessionStateDelegateChromeos::CycleActiveUser( | |
147 ash::CycleUserDirection direction) { | |
148 SessionControllerClient::DoCycleActiveUser(direction); | |
149 } | |
150 | |
151 bool SessionStateDelegateChromeos::IsMultiProfileAllowedByPrimaryUserPolicy() | |
152 const { | |
153 return chromeos::MultiProfileUserController::GetPrimaryUserPolicy() == | |
154 chromeos::MultiProfileUserController::ALLOWED; | |
155 } | |
156 | |
157 void SessionStateDelegateChromeos::AddSessionStateObserver( | |
158 ash::SessionStateObserver* observer) { | |
159 session_state_observer_list_.AddObserver(observer); | |
160 } | |
161 | |
162 void SessionStateDelegateChromeos::RemoveSessionStateObserver( | |
163 ash::SessionStateObserver* observer) { | |
164 session_state_observer_list_.RemoveObserver(observer); | |
165 } | |
166 | |
167 void SessionStateDelegateChromeos::LoggedInStateChanged() { | |
168 SetSessionState(chromeos::LoginState::Get()->IsUserLoggedIn() | |
169 ? session_manager::SessionState::ACTIVE | |
170 : session_manager::SessionState::LOGIN_PRIMARY, | |
171 false); | |
172 } | |
173 | |
174 void SessionStateDelegateChromeos::ActiveUserChanged( | |
175 const user_manager::User* active_user) { | |
176 for (ash::SessionStateObserver& observer : session_state_observer_list_) | |
177 observer.ActiveUserChanged(active_user->GetAccountId()); | |
178 } | |
179 | |
180 void SessionStateDelegateChromeos::UserAddedToSession( | |
181 const user_manager::User* added_user) { | |
182 for (ash::SessionStateObserver& observer : session_state_observer_list_) | |
183 observer.UserAddedToSession(added_user->GetAccountId()); | |
184 } | |
185 | |
186 void SessionStateDelegateChromeos::OnUserAddingStarted() { | |
187 SetSessionState(session_manager::SessionState::LOGIN_SECONDARY, false); | |
188 } | |
189 | |
190 void SessionStateDelegateChromeos::OnUserAddingFinished() { | |
191 SetSessionState(session_manager::SessionState::ACTIVE, false); | |
192 } | |
193 | |
194 void SessionStateDelegateChromeos::SetSessionState( | |
195 session_manager::SessionState new_state, | |
196 bool force) { | |
197 if (session_state_ == new_state && !force) | |
198 return; | |
199 | |
200 session_state_ = new_state; | |
201 NotifySessionStateChanged(); | |
202 } | |
203 | |
204 void SessionStateDelegateChromeos::NotifySessionStateChanged() { | |
205 for (ash::SessionStateObserver& observer : session_state_observer_list_) | |
206 observer.SessionStateChanged(session_state_); | |
207 } | |
OLD | NEW |