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