Chromium Code Reviews| Index: ash/common/session/session_controller.cc |
| diff --git a/ash/common/session/session_controller.cc b/ash/common/session/session_controller.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ee8f115a0c37064f532864b1b6fb213083874504 |
| --- /dev/null |
| +++ b/ash/common/session/session_controller.cc |
| @@ -0,0 +1,152 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ash/common/session/session_controller.h" |
| + |
| +#include <set> |
| + |
| +#include "ash/common/session/session_state_observer.h" |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "components/signin/core/account_id/account_id.h" |
| +#include "services/service_manager/public/cpp/connector.h" |
| + |
| +namespace ash { |
| + |
| +SessionController::SessionController() {} |
| + |
| +SessionController::~SessionController() {} |
| + |
| +void SessionController::BindRequest(mojom::SessionControllerRequest request) { |
| + bindings_.AddBinding(this, std::move(request)); |
| +} |
| + |
| +int SessionController::GetMaximumNumberOfLoggedInUsers() const { |
| + return static_cast<int>(max_users_); |
| +} |
| + |
| +int SessionController::NumberOfLoggedInUsers() const { |
| + return static_cast<int>(user_sessions_.size()); |
| +} |
| + |
| +AddUserSessionPolicy SessionController::GetAddUserPolicy() const { |
| + return add_user_session_policy_; |
| +} |
| + |
| +bool SessionController::IsActiveUserSessionStarted() const { |
| + return !user_sessions_.empty(); |
| +} |
| + |
| +bool SessionController::CanLockScreen() const { |
| + return can_lock_; |
| +} |
| + |
| +bool SessionController::IsScreenLocked() const { |
| + return state_ == session_manager::SessionState::LOCKED; |
| +} |
| + |
| +bool SessionController::ShouldLockScreenAutomatically() const { |
| + return should_lock_screen_automatically_; |
| +} |
| + |
| +bool SessionController::IsUserSessionBlocked() const { |
| + return state_ != session_manager::SessionState::ACTIVE; |
| +} |
| + |
| +session_manager::SessionState SessionController::GetSessionState() const { |
| + return state_; |
| +} |
| + |
| +const std::vector<mojom::UserSessionPtr>& SessionController::GetUserSessions() |
| + const { |
| + return user_sessions_; |
| +} |
| + |
| +void SessionController::LockScreen() { |
| + client_->RequestLockScreen(); |
|
James Cook
2016/12/01 23:01:43
Check if (client_) since it's possible someday we'
xiyuan
2016/12/06 00:46:33
Done.
|
| +} |
| + |
| +void SessionController::SwitchActiveUser(const AccountId& account_id) { |
| + client_->SwitchActiveUser(account_id.Serialize()); |
| +} |
| + |
| +void SessionController::CycleActiveUser(bool next_user) { |
| + client_->CycleActiveUser(next_user); |
| +} |
| + |
| +void SessionController::AddSessionStateObserver( |
| + SessionStateObserver* observer) { |
| + observers_.AddObserver(observer); |
| +} |
| + |
| +void SessionController::RemoveSessionStateObserver( |
| + SessionStateObserver* observer) { |
| + observers_.RemoveObserver(observer); |
| +} |
| + |
| +void SessionController::SetClient(mojom::SessionControllerClientPtr client) { |
| + client_ = std::move(client); |
| +} |
| + |
| +void SessionController::SetMaxUsers(uint32_t max_users) { |
| + max_users_ = max_users; |
| +} |
| + |
| +void SessionController::SetCanLockScreen(bool can_lock) { |
| + can_lock_ = can_lock; |
| +} |
| + |
| +void SessionController::SetShouldLockScreenAutomatically(bool should_lock) { |
| + should_lock_screen_automatically_ = should_lock; |
| +} |
| + |
| +void SessionController::SetAddUserSessionPolicy( |
| + AddUserSessionPolicy add_user_session_policy) { |
| + add_user_session_policy_ = add_user_session_policy; |
| +} |
| + |
| +void SessionController::SetSessionState(session_manager::SessionState state) { |
| + if (state_ == state) |
| + return; |
| + |
| + state_ = state; |
| + for (auto& observer : observers_) |
| + observer.SessionStateChanged(state_); |
| +} |
| + |
| +void SessionController::SetUserSessions( |
| + std::vector<mojom::UserSessionPtr> user_sessions) { |
| + const std::string old_active_user = |
| + user_sessions_.empty() ? std::string() : user_sessions_[0]->account_id; |
| + |
| + std::set<std::string> existing_users; |
| + for (auto& user_session : user_sessions_) |
|
James Cook
2016/12/01 23:01:43
const auto&, if possible, here and below
xiyuan
2016/12/06 00:46:33
Code removed.
|
| + existing_users.insert(user_session->account_id); |
| + |
| + user_sessions_ = std::move(user_sessions); |
| + |
| + for (auto& user_session : user_sessions) { |
|
James Cook
2016/12/01 23:01:43
user_sessions_? isn't |user_sessions| empty now th
xiyuan
2016/12/06 00:46:33
Right. The code is wrong. Removed in new patch sin
|
| + if (existing_users.find(user_session->account_id) == existing_users.end()) { |
|
James Cook
2016/12/01 23:01:43
optional: if ( ... != end) continue; which would r
xiyuan
2016/12/06 00:46:33
Code removed.
|
| + AccountId account_id(EmptyAccountId()); |
| + if (!AccountId::Deserialize(user_session->account_id, &account_id)) { |
| + LOG(ERROR) << "Failed to deserialize account id."; |
| + continue; |
| + } |
| + for (auto& observer : observers_) |
| + observer.UserAddedToSession(account_id); |
| + } |
| + } |
|
James Cook
2016/12/01 23:01:43
nit: this function could use some comments explain
xiyuan
2016/12/06 00:46:33
Acknowledged.
|
| + |
| + const std::string new_active_user = |
| + user_sessions_.empty() ? std::string() : user_sessions_[0]->account_id; |
| + if (new_active_user != old_active_user) { |
| + AccountId account_id(EmptyAccountId()); |
| + if (AccountId::Deserialize(new_active_user, &account_id)) { |
| + for (auto& observer : observers_) |
| + observer.ActiveUserChanged(account_id); |
| + } |
| + } |
| +} |
|
James Cook
2016/12/01 23:01:43
Can you add a simple unit test for this class? Jus
xiyuan
2016/12/06 00:46:33
Done.
|
| + |
| +} // namespace ash |