| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "ash/common/session/session_controller.h" | 5 #include "ash/common/session/session_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ash/common/login_status.h" | |
| 10 #include "ash/common/session/session_state_observer.h" | 9 #include "ash/common/session/session_state_observer.h" |
| 11 #include "base/bind.h" | 10 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 13 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 14 #include "chromeos/chromeos_switches.h" | 13 #include "chromeos/chromeos_switches.h" |
| 15 #include "components/signin/core/account_id/account_id.h" | 14 #include "components/signin/core/account_id/account_id.h" |
| 16 #include "services/service_manager/public/cpp/connector.h" | 15 #include "services/service_manager/public/cpp/connector.h" |
| 17 | 16 |
| 18 namespace ash { | 17 namespace ash { |
| 19 | 18 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 void SessionController::AddSessionStateObserver( | 117 void SessionController::AddSessionStateObserver( |
| 119 SessionStateObserver* observer) { | 118 SessionStateObserver* observer) { |
| 120 observers_.AddObserver(observer); | 119 observers_.AddObserver(observer); |
| 121 } | 120 } |
| 122 | 121 |
| 123 void SessionController::RemoveSessionStateObserver( | 122 void SessionController::RemoveSessionStateObserver( |
| 124 SessionStateObserver* observer) { | 123 SessionStateObserver* observer) { |
| 125 observers_.RemoveObserver(observer); | 124 observers_.RemoveObserver(observer); |
| 126 } | 125 } |
| 127 | 126 |
| 128 LoginStatus SessionController::GetLoginStatus() const { | |
| 129 using session_manager::SessionState; | |
| 130 | |
| 131 // TODO(jamescook|xiyuan): There is not a 1:1 mapping of SessionState to | |
| 132 // LoginStatus. Fix the cases that don't match. http://crbug.com/701193 | |
| 133 switch (state_) { | |
| 134 case SessionState::UNKNOWN: | |
| 135 case SessionState::OOBE: | |
| 136 case SessionState::LOGIN_PRIMARY: | |
| 137 case SessionState::LOGGED_IN_NOT_ACTIVE: | |
| 138 return LoginStatus::NOT_LOGGED_IN; | |
| 139 | |
| 140 case SessionState::ACTIVE: | |
| 141 return GetLoginStatusForActiveSession(); | |
| 142 | |
| 143 case SessionState::LOCKED: | |
| 144 return LoginStatus::LOCKED; | |
| 145 | |
| 146 case SessionState::LOGIN_SECONDARY: | |
| 147 // TODO: There is no LoginStatus for this. | |
| 148 return LoginStatus::USER; | |
| 149 } | |
| 150 NOTREACHED(); | |
| 151 return LoginStatus::NOT_LOGGED_IN; | |
| 152 } | |
| 153 | |
| 154 void SessionController::SetClient(mojom::SessionControllerClientPtr client) { | 127 void SessionController::SetClient(mojom::SessionControllerClientPtr client) { |
| 155 client_ = std::move(client); | 128 client_ = std::move(client); |
| 156 } | 129 } |
| 157 | 130 |
| 158 void SessionController::SetSessionInfo(mojom::SessionInfoPtr info) { | 131 void SessionController::SetSessionInfo(mojom::SessionInfoPtr info) { |
| 159 can_lock_ = info->can_lock_screen; | 132 can_lock_ = info->can_lock_screen; |
| 160 should_lock_screen_automatically_ = info->should_lock_screen_automatically; | 133 should_lock_screen_automatically_ = info->should_lock_screen_automatically; |
| 161 add_user_session_policy_ = info->add_user_session_policy; | 134 add_user_session_policy_ = info->add_user_session_policy; |
| 162 SetSessionState(info->state); | 135 SetSessionState(info->state); |
| 163 } | 136 } |
| 164 | 137 |
| 165 void SessionController::UpdateUserSession(mojom::UserSessionPtr user_session) { | 138 void SessionController::UpdateUserSession(mojom::UserSessionPtr user_session) { |
| 166 auto it = | 139 auto it = |
| 167 std::find_if(user_sessions_.begin(), user_sessions_.end(), | 140 std::find_if(user_sessions_.begin(), user_sessions_.end(), |
| 168 [&user_session](const mojom::UserSessionPtr& session) { | 141 [&user_session](const mojom::UserSessionPtr& session) { |
| 169 return session->session_id == user_session->session_id; | 142 return session->session_id == user_session->session_id; |
| 170 }); | 143 }); |
| 171 if (it == user_sessions_.end()) { | 144 if (it == user_sessions_.end()) { |
| 172 AddUserSession(std::move(user_session)); | 145 AddUserSession(std::move(user_session)); |
| 173 return; | 146 return; |
| 174 } | 147 } |
| 175 | 148 |
| 176 *it = std::move(user_session); | 149 *it = std::move(user_session); |
| 177 for (auto& observer : observers_) | 150 for (auto& observer : observers_) |
| 178 observer.UserSessionUpdated((*it)->account_id); | 151 observer.UserSessionUpdated((*it)->account_id); |
| 152 |
| 153 UpdateLoginStatus(); |
| 179 } | 154 } |
| 180 | 155 |
| 181 void SessionController::SetUserSessionOrder( | 156 void SessionController::SetUserSessionOrder( |
| 182 const std::vector<uint32_t>& user_session_order) { | 157 const std::vector<uint32_t>& user_session_order) { |
| 183 DCHECK_EQ(user_sessions_.size(), user_session_order.size()); | 158 DCHECK_EQ(user_sessions_.size(), user_session_order.size()); |
| 184 | 159 |
| 185 // Adjusts |user_sessions_| to match the given order. | 160 // Adjusts |user_sessions_| to match the given order. |
| 186 std::vector<mojom::UserSessionPtr> sessions; | 161 std::vector<mojom::UserSessionPtr> sessions; |
| 187 for (const auto& session_id : user_session_order) { | 162 for (const auto& session_id : user_session_order) { |
| 188 auto it = | 163 auto it = |
| 189 std::find_if(user_sessions_.begin(), user_sessions_.end(), | 164 std::find_if(user_sessions_.begin(), user_sessions_.end(), |
| 190 [session_id](const mojom::UserSessionPtr& session) { | 165 [session_id](const mojom::UserSessionPtr& session) { |
| 191 return session && session->session_id == session_id; | 166 return session && session->session_id == session_id; |
| 192 }); | 167 }); |
| 193 if (it == user_sessions_.end()) { | 168 if (it == user_sessions_.end()) { |
| 194 LOG(ERROR) << "Unknown session id =" << session_id; | 169 LOG(ERROR) << "Unknown session id =" << session_id; |
| 195 continue; | 170 continue; |
| 196 } | 171 } |
| 197 | 172 |
| 198 sessions.push_back(std::move(*it)); | 173 sessions.push_back(std::move(*it)); |
| 199 } | 174 } |
| 200 user_sessions_.swap(sessions); | 175 user_sessions_.swap(sessions); |
| 201 | 176 |
| 202 // Check active user change and notifies observers. | 177 // Check active user change and notifies observers. |
| 203 if (user_sessions_[0]->session_id != active_session_id_) { | 178 if (user_sessions_[0]->session_id != active_session_id_) { |
| 204 active_session_id_ = user_sessions_[0]->session_id; | 179 active_session_id_ = user_sessions_[0]->session_id; |
| 205 | 180 |
| 206 for (auto& observer : observers_) | 181 for (auto& observer : observers_) |
| 207 observer.ActiveUserChanged(user_sessions_[0]->account_id); | 182 observer.ActiveUserChanged(user_sessions_[0]->account_id); |
| 183 |
| 184 UpdateLoginStatus(); |
| 208 } | 185 } |
| 209 } | 186 } |
| 210 | 187 |
| 211 void SessionController::ClearUserSessionsForTest() { | 188 void SessionController::ClearUserSessionsForTest() { |
| 212 user_sessions_.clear(); | 189 user_sessions_.clear(); |
| 213 } | 190 } |
| 214 | 191 |
| 215 void SessionController::FlushMojoForTest() { | 192 void SessionController::FlushMojoForTest() { |
| 216 client_.FlushForTesting(); | 193 client_.FlushForTesting(); |
| 217 } | 194 } |
| 218 | 195 |
| 219 void SessionController::LockScreenAndFlushForTest() { | 196 void SessionController::LockScreenAndFlushForTest() { |
| 220 LockScreen(); | 197 LockScreen(); |
| 221 FlushMojoForTest(); | 198 FlushMojoForTest(); |
| 222 } | 199 } |
| 223 | 200 |
| 224 void SessionController::SetSessionState(session_manager::SessionState state) { | 201 void SessionController::SetSessionState(session_manager::SessionState state) { |
| 225 if (state_ == state) | 202 if (state_ == state) |
| 226 return; | 203 return; |
| 227 | 204 |
| 228 state_ = state; | 205 state_ = state; |
| 229 for (auto& observer : observers_) | 206 for (auto& observer : observers_) |
| 230 observer.SessionStateChanged(state_); | 207 observer.SessionStateChanged(state_); |
| 208 |
| 209 UpdateLoginStatus(); |
| 231 } | 210 } |
| 232 | 211 |
| 233 void SessionController::AddUserSession(mojom::UserSessionPtr user_session) { | 212 void SessionController::AddUserSession(mojom::UserSessionPtr user_session) { |
| 234 const AccountId account_id(user_session->account_id); | 213 const AccountId account_id(user_session->account_id); |
| 235 | 214 |
| 236 user_sessions_.push_back(std::move(user_session)); | 215 user_sessions_.push_back(std::move(user_session)); |
| 237 | 216 |
| 238 for (auto& observer : observers_) | 217 for (auto& observer : observers_) |
| 239 observer.UserAddedToSession(account_id); | 218 observer.UserAddedToSession(account_id); |
| 240 } | 219 } |
| 241 | 220 |
| 242 LoginStatus SessionController::GetLoginStatusForActiveSession() const { | 221 LoginStatus SessionController::CalculateLoginStatus() const { |
| 222 using session_manager::SessionState; |
| 223 |
| 224 // TODO(jamescook|xiyuan): There is not a 1:1 mapping of SessionState to |
| 225 // LoginStatus. Fix the cases that don't match. http://crbug.com/701193 |
| 226 switch (state_) { |
| 227 case SessionState::UNKNOWN: |
| 228 case SessionState::OOBE: |
| 229 case SessionState::LOGIN_PRIMARY: |
| 230 case SessionState::LOGGED_IN_NOT_ACTIVE: |
| 231 return LoginStatus::NOT_LOGGED_IN; |
| 232 |
| 233 case SessionState::ACTIVE: |
| 234 return CalculateLoginStatusForActiveSession(); |
| 235 |
| 236 case SessionState::LOCKED: |
| 237 return LoginStatus::LOCKED; |
| 238 |
| 239 case SessionState::LOGIN_SECONDARY: |
| 240 // TODO: There is no LoginStatus for this. |
| 241 return LoginStatus::USER; |
| 242 } |
| 243 NOTREACHED(); |
| 244 return LoginStatus::NOT_LOGGED_IN; |
| 245 } |
| 246 |
| 247 LoginStatus SessionController::CalculateLoginStatusForActiveSession() const { |
| 243 DCHECK(state_ == session_manager::SessionState::ACTIVE); | 248 DCHECK(state_ == session_manager::SessionState::ACTIVE); |
| 244 | 249 |
| 245 if (user_sessions_.empty()) // Can be empty in tests. | 250 if (user_sessions_.empty()) // Can be empty in tests. |
| 246 return LoginStatus::USER; | 251 return LoginStatus::USER; |
| 247 | 252 |
| 248 switch (user_sessions_[0]->type) { | 253 switch (user_sessions_[0]->type) { |
| 249 case user_manager::USER_TYPE_REGULAR: | 254 case user_manager::USER_TYPE_REGULAR: |
| 250 // TODO: This needs to distinguish between owner and non-owner. | 255 // TODO: This needs to distinguish between owner and non-owner. |
| 251 return LoginStatus::USER; | 256 return LoginStatus::USER; |
| 252 case user_manager::USER_TYPE_GUEST: | 257 case user_manager::USER_TYPE_GUEST: |
| (...skipping 13 matching lines...) Expand all Loading... |
| 266 return LoginStatus::USER; | 271 return LoginStatus::USER; |
| 267 case user_manager::NUM_USER_TYPES: | 272 case user_manager::NUM_USER_TYPES: |
| 268 // Avoid having a "default" case so the compiler catches new enum values. | 273 // Avoid having a "default" case so the compiler catches new enum values. |
| 269 NOTREACHED(); | 274 NOTREACHED(); |
| 270 return LoginStatus::USER; | 275 return LoginStatus::USER; |
| 271 } | 276 } |
| 272 NOTREACHED(); | 277 NOTREACHED(); |
| 273 return LoginStatus::USER; | 278 return LoginStatus::USER; |
| 274 } | 279 } |
| 275 | 280 |
| 281 void SessionController::UpdateLoginStatus() { |
| 282 const LoginStatus new_login_status = CalculateLoginStatus(); |
| 283 if (new_login_status == login_status_) |
| 284 return; |
| 285 |
| 286 login_status_ = new_login_status; |
| 287 for (auto& observer : observers_) |
| 288 observer.LoginStatusChanged(login_status_); |
| 289 } |
| 290 |
| 276 } // namespace ash | 291 } // namespace ash |
| OLD | NEW |