OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/common/session/session_controller.h" |
| 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "ash/common/session/session_state_observer.h" |
| 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" |
| 12 #include "components/signin/core/account_id/account_id.h" |
| 13 #include "services/service_manager/public/cpp/connector.h" |
| 14 |
| 15 namespace ash { |
| 16 |
| 17 SessionController::SessionController() {} |
| 18 |
| 19 SessionController::~SessionController() {} |
| 20 |
| 21 void SessionController::BindRequest(mojom::SessionControllerRequest request) { |
| 22 bindings_.AddBinding(this, std::move(request)); |
| 23 } |
| 24 |
| 25 int SessionController::GetMaximumNumberOfLoggedInUsers() const { |
| 26 return static_cast<int>(max_users_); |
| 27 } |
| 28 |
| 29 int SessionController::NumberOfLoggedInUsers() const { |
| 30 return static_cast<int>(user_sessions_.size()); |
| 31 } |
| 32 |
| 33 AddUserSessionPolicy SessionController::GetAddUserPolicy() const { |
| 34 return add_user_session_policy_; |
| 35 } |
| 36 |
| 37 bool SessionController::IsActiveUserSessionStarted() const { |
| 38 return !user_sessions_.empty(); |
| 39 } |
| 40 |
| 41 bool SessionController::CanLockScreen() const { |
| 42 return can_lock_; |
| 43 } |
| 44 |
| 45 bool SessionController::IsScreenLocked() const { |
| 46 return state_ == session_manager::SessionState::LOCKED; |
| 47 } |
| 48 |
| 49 bool SessionController::ShouldLockScreenAutomatically() const { |
| 50 return should_lock_screen_automatically_; |
| 51 } |
| 52 |
| 53 bool SessionController::IsUserSessionBlocked() const { |
| 54 return state_ != session_manager::SessionState::ACTIVE; |
| 55 } |
| 56 |
| 57 session_manager::SessionState SessionController::GetSessionState() const { |
| 58 return state_; |
| 59 } |
| 60 |
| 61 const std::vector<mojom::UserSessionPtr>& SessionController::GetUserSessions() |
| 62 const { |
| 63 return user_sessions_; |
| 64 } |
| 65 |
| 66 void SessionController::LockScreen() { |
| 67 if (client_) |
| 68 client_->RequestLockScreen(); |
| 69 } |
| 70 |
| 71 void SessionController::SwitchActiveUser(const AccountId& account_id) { |
| 72 if (client_) |
| 73 client_->SwitchActiveUser(account_id.Serialize()); |
| 74 } |
| 75 |
| 76 void SessionController::CycleActiveUser(bool next_user) { |
| 77 if (client_) |
| 78 client_->CycleActiveUser(next_user); |
| 79 } |
| 80 |
| 81 void SessionController::AddSessionStateObserver( |
| 82 SessionStateObserver* observer) { |
| 83 observers_.AddObserver(observer); |
| 84 } |
| 85 |
| 86 void SessionController::RemoveSessionStateObserver( |
| 87 SessionStateObserver* observer) { |
| 88 observers_.RemoveObserver(observer); |
| 89 } |
| 90 |
| 91 void SessionController::SetClient(mojom::SessionControllerClientPtr client) { |
| 92 client_ = std::move(client); |
| 93 } |
| 94 |
| 95 void SessionController::SetSessionInfo(mojom::SessionInfoPtr info) { |
| 96 max_users_ = info->max_users; |
| 97 can_lock_ = info->can_lock_screen; |
| 98 should_lock_screen_automatically_ = info->should_lock_screen_automatically; |
| 99 add_user_session_policy_ = info->add_user_session_policy; |
| 100 SetSessionState(info->state); |
| 101 } |
| 102 |
| 103 void SessionController::UpdateUserSession(mojom::UserSessionPtr user_session) { |
| 104 auto it = |
| 105 std::find_if(user_sessions_.begin(), user_sessions_.end(), |
| 106 [&user_session](const mojom::UserSessionPtr& session) { |
| 107 return session->session_id == user_session->session_id; |
| 108 }); |
| 109 if (it == user_sessions_.end()) { |
| 110 AddUserSession(std::move(user_session)); |
| 111 return; |
| 112 } |
| 113 |
| 114 *it = std::move(user_session); |
| 115 // TODO(xiyuan): Notify observers about meta change to replace things such as |
| 116 // NOTIFICATION_LOGIN_USER_IMAGE_CHANGED. http://crbug.com/670422 |
| 117 } |
| 118 |
| 119 void SessionController::SetUserSessionOrder( |
| 120 const std::vector<uint32_t>& user_session_order) { |
| 121 DCHECK_EQ(user_sessions_.size(), user_session_order.size()); |
| 122 |
| 123 // Adjusts |user_sessions_| to match the given order. |
| 124 std::vector<mojom::UserSessionPtr> sessions; |
| 125 for (const auto& session_id : user_session_order) { |
| 126 auto it = |
| 127 std::find_if(user_sessions_.begin(), user_sessions_.end(), |
| 128 [session_id](const mojom::UserSessionPtr& session) { |
| 129 return session && session->session_id == session_id; |
| 130 }); |
| 131 if (it == user_sessions_.end()) { |
| 132 LOG(ERROR) << "Unknown session id =" << session_id; |
| 133 continue; |
| 134 } |
| 135 |
| 136 sessions.push_back(std::move(*it)); |
| 137 } |
| 138 user_sessions_.swap(sessions); |
| 139 |
| 140 // Check active user change and notifies observers. |
| 141 if (user_sessions_[0]->session_id != active_session_id_) { |
| 142 active_session_id_ = user_sessions_[0]->session_id; |
| 143 |
| 144 AccountId account_id(EmptyAccountId()); |
| 145 if (AccountId::Deserialize(user_sessions_[0]->serialized_account_id, |
| 146 &account_id)) { |
| 147 for (auto& observer : observers_) |
| 148 observer.ActiveUserChanged(account_id); |
| 149 } |
| 150 } |
| 151 } |
| 152 |
| 153 void SessionController::SetSessionState(session_manager::SessionState state) { |
| 154 if (state_ == state) |
| 155 return; |
| 156 |
| 157 state_ = state; |
| 158 for (auto& observer : observers_) |
| 159 observer.SessionStateChanged(state_); |
| 160 } |
| 161 |
| 162 void SessionController::AddUserSession(mojom::UserSessionPtr user_session) { |
| 163 AccountId account_id(EmptyAccountId()); |
| 164 if (!AccountId::Deserialize(user_session->serialized_account_id, |
| 165 &account_id)) { |
| 166 LOG(ERROR) << "Failed to deserialize account id."; |
| 167 return; |
| 168 } |
| 169 |
| 170 user_sessions_.push_back(std::move(user_session)); |
| 171 |
| 172 for (auto& observer : observers_) |
| 173 observer.UserAddedToSession(account_id); |
| 174 } |
| 175 |
| 176 } // namespace ash |
OLD | NEW |