| 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 session_manager::kMaxmiumNumberOfUserSessions; |  | 
| 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); |  | 
| 74 } |  | 
| 75 |  | 
| 76 void SessionController::CycleActiveUser(CycleUserDirection direction) { |  | 
| 77   if (client_) |  | 
| 78     client_->CycleActiveUser(direction); |  | 
| 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   can_lock_ = info->can_lock_screen; |  | 
| 97   should_lock_screen_automatically_ = info->should_lock_screen_automatically; |  | 
| 98   add_user_session_policy_ = info->add_user_session_policy; |  | 
| 99   SetSessionState(info->state); |  | 
| 100 } |  | 
| 101 |  | 
| 102 void SessionController::UpdateUserSession(mojom::UserSessionPtr user_session) { |  | 
| 103   auto it = |  | 
| 104       std::find_if(user_sessions_.begin(), user_sessions_.end(), |  | 
| 105                    [&user_session](const mojom::UserSessionPtr& session) { |  | 
| 106                      return session->session_id == user_session->session_id; |  | 
| 107                    }); |  | 
| 108   if (it == user_sessions_.end()) { |  | 
| 109     AddUserSession(std::move(user_session)); |  | 
| 110     return; |  | 
| 111   } |  | 
| 112 |  | 
| 113   *it = std::move(user_session); |  | 
| 114   // TODO(xiyuan): Notify observers about meta change to replace things such as |  | 
| 115   //     NOTIFICATION_LOGIN_USER_IMAGE_CHANGED. http://crbug.com/670422 |  | 
| 116 } |  | 
| 117 |  | 
| 118 void SessionController::SetUserSessionOrder( |  | 
| 119     const std::vector<uint32_t>& user_session_order) { |  | 
| 120   DCHECK_EQ(user_sessions_.size(), user_session_order.size()); |  | 
| 121 |  | 
| 122   // Adjusts |user_sessions_| to match the given order. |  | 
| 123   std::vector<mojom::UserSessionPtr> sessions; |  | 
| 124   for (const auto& session_id : user_session_order) { |  | 
| 125     auto it = |  | 
| 126         std::find_if(user_sessions_.begin(), user_sessions_.end(), |  | 
| 127                      [session_id](const mojom::UserSessionPtr& session) { |  | 
| 128                        return session && session->session_id == session_id; |  | 
| 129                      }); |  | 
| 130     if (it == user_sessions_.end()) { |  | 
| 131       LOG(ERROR) << "Unknown session id =" << session_id; |  | 
| 132       continue; |  | 
| 133     } |  | 
| 134 |  | 
| 135     sessions.push_back(std::move(*it)); |  | 
| 136   } |  | 
| 137   user_sessions_.swap(sessions); |  | 
| 138 |  | 
| 139   // Check active user change and notifies observers. |  | 
| 140   if (user_sessions_[0]->session_id != active_session_id_) { |  | 
| 141     active_session_id_ = user_sessions_[0]->session_id; |  | 
| 142 |  | 
| 143     for (auto& observer : observers_) |  | 
| 144       observer.ActiveUserChanged(user_sessions_[0]->account_id); |  | 
| 145   } |  | 
| 146 } |  | 
| 147 |  | 
| 148 void SessionController::SetSessionState(session_manager::SessionState state) { |  | 
| 149   if (state_ == state) |  | 
| 150     return; |  | 
| 151 |  | 
| 152   state_ = state; |  | 
| 153   for (auto& observer : observers_) |  | 
| 154     observer.SessionStateChanged(state_); |  | 
| 155 } |  | 
| 156 |  | 
| 157 void SessionController::AddUserSession(mojom::UserSessionPtr user_session) { |  | 
| 158   const AccountId account_id(user_session->account_id); |  | 
| 159 |  | 
| 160   user_sessions_.push_back(std::move(user_session)); |  | 
| 161 |  | 
| 162   for (auto& observer : observers_) |  | 
| 163     observer.UserAddedToSession(account_id); |  | 
| 164 } |  | 
| 165 |  | 
| 166 }  // namespace ash |  | 
| OLD | NEW | 
|---|