| 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 "chrome/browser/ui/ash/session_controller_client.h" | 5 #include "chrome/browser/ui/ash/session_controller_client.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "ash/public/cpp/session_types.h" | 10 #include "ash/public/cpp/session_types.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" | 14 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" |
| 15 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 15 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/ui/ash/ash_util.h" | 17 #include "chrome/browser/ui/ash/ash_util.h" |
| 18 #include "chrome/browser/ui/ash/multi_user/user_switch_util.h" | 18 #include "chrome/browser/ui/ash/multi_user/user_switch_util.h" |
| 19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
| 20 #include "chrome/grit/theme_resources.h" | 20 #include "chrome/grit/theme_resources.h" |
| 21 #include "chromeos/dbus/dbus_thread_manager.h" | 21 #include "chromeos/dbus/dbus_thread_manager.h" |
| 22 #include "chromeos/dbus/session_manager_client.h" | 22 #include "chromeos/dbus/session_manager_client.h" |
| 23 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
| 24 #include "components/session_manager/core/session_manager.h" | 24 #include "components/session_manager/core/session_manager.h" |
| 25 #include "content/public/common/service_manager_connection.h" | 25 #include "content/public/common/service_manager_connection.h" |
| 26 #include "content/public/common/service_names.mojom.h" | 26 #include "content/public/common/service_names.mojom.h" |
| 27 #include "services/service_manager/public/cpp/connector.h" | 27 #include "services/service_manager/public/cpp/connector.h" |
| 28 #include "ui/base/resource/resource_bundle.h" | 28 #include "ui/base/resource/resource_bundle.h" |
| 29 | 29 |
| 30 using session_manager::SessionManager; |
| 30 using user_manager::UserManager; | 31 using user_manager::UserManager; |
| 31 using user_manager::User; | 32 using user_manager::User; |
| 32 using user_manager::UserList; | 33 using user_manager::UserList; |
| 33 | 34 |
| 34 namespace { | 35 namespace { |
| 35 | 36 |
| 36 // Limits the number of logged in users to 10 due to memory constraints. | |
| 37 constexpr uint32_t kMaxUsers = 10; | |
| 38 | |
| 39 SessionControllerClient* g_instance = nullptr; | 37 SessionControllerClient* g_instance = nullptr; |
| 40 | 38 |
| 41 uint32_t GetSessionId(const User* user) { | 39 uint32_t GetSessionId(const User* user) { |
| 42 const UserList logged_in_users = UserManager::Get()->GetLoggedInUsers(); | 40 const UserList logged_in_users = UserManager::Get()->GetLoggedInUsers(); |
| 43 // TODO(xiyuan): Update with real session id when user session tracking | 41 // TODO(xiyuan): Update with real session id when user session tracking |
| 44 // code is moved from UserManager to SessionManager. | 42 // code is moved from UserManager to SessionManager. |
| 45 for (size_t i = 0; i < logged_in_users.size(); ++i) { | 43 for (size_t i = 0; i < logged_in_users.size(); ++i) { |
| 46 if (logged_in_users[i] == user) | 44 if (logged_in_users[i] == user) |
| 47 return i + 1; | 45 return i + 1; |
| 48 } | 46 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 73 return session; | 71 return session; |
| 74 } | 72 } |
| 75 | 73 |
| 76 void DoSwitchUser(const AccountId& account_id) { | 74 void DoSwitchUser(const AccountId& account_id) { |
| 77 UserManager::Get()->SwitchActiveUser(account_id); | 75 UserManager::Get()->SwitchActiveUser(account_id); |
| 78 } | 76 } |
| 79 | 77 |
| 80 } // namespace | 78 } // namespace |
| 81 | 79 |
| 82 SessionControllerClient::SessionControllerClient() : binding_(this) { | 80 SessionControllerClient::SessionControllerClient() : binding_(this) { |
| 83 session_manager::SessionManager::Get()->AddObserver(this); | 81 SessionManager::Get()->AddObserver(this); |
| 84 UserManager::Get()->AddSessionStateObserver(this); | 82 UserManager::Get()->AddSessionStateObserver(this); |
| 85 | 83 |
| 86 ConnectToSessionControllerAndSetClient(); | 84 ConnectToSessionControllerAndSetClient(); |
| 87 SendSessionInfoIfChanged(); | 85 SendSessionInfoIfChanged(); |
| 88 // User sessions and their order will be sent via UserSessionStateObserver | 86 // User sessions and their order will be sent via UserSessionStateObserver |
| 89 // even for crash-n-restart. | 87 // even for crash-n-restart. |
| 90 | 88 |
| 91 DCHECK(!g_instance); | 89 DCHECK(!g_instance); |
| 92 g_instance = this; | 90 g_instance = this; |
| 93 } | 91 } |
| 94 | 92 |
| 95 SessionControllerClient::~SessionControllerClient() { | 93 SessionControllerClient::~SessionControllerClient() { |
| 96 DCHECK_EQ(this, g_instance); | 94 DCHECK_EQ(this, g_instance); |
| 97 g_instance = nullptr; | 95 g_instance = nullptr; |
| 98 | 96 |
| 99 session_manager::SessionManager::Get()->RemoveObserver(this); | 97 SessionManager::Get()->RemoveObserver(this); |
| 100 UserManager::Get()->RemoveSessionStateObserver(this); | 98 UserManager::Get()->RemoveSessionStateObserver(this); |
| 101 } | 99 } |
| 102 | 100 |
| 103 void SessionControllerClient::RequestLockScreen() { | 101 void SessionControllerClient::RequestLockScreen() { |
| 104 DoLockScreen(); | 102 DoLockScreen(); |
| 105 } | 103 } |
| 106 | 104 |
| 107 void SessionControllerClient::SwitchActiveUser( | 105 void SessionControllerClient::SwitchActiveUser( |
| 108 const std::string& serialized_account_id) { | 106 const std::string& serialized_account_id) { |
| 109 // TODO(xiyuan): Add type map for AccountId. | 107 // TODO(xiyuan): Add type map for AccountId. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 163 ash::AddUserSessionPolicy SessionControllerClient::GetAddUserSessionPolicy() { | 161 ash::AddUserSessionPolicy SessionControllerClient::GetAddUserSessionPolicy() { |
| 164 UserManager* const user_manager = UserManager::Get(); | 162 UserManager* const user_manager = UserManager::Get(); |
| 165 if (user_manager->GetUsersAllowedForMultiProfile().empty()) | 163 if (user_manager->GetUsersAllowedForMultiProfile().empty()) |
| 166 return ash::AddUserSessionPolicy::ERROR_NO_ELIGIBLE_USERS; | 164 return ash::AddUserSessionPolicy::ERROR_NO_ELIGIBLE_USERS; |
| 167 | 165 |
| 168 if (chromeos::MultiProfileUserController::GetPrimaryUserPolicy() != | 166 if (chromeos::MultiProfileUserController::GetPrimaryUserPolicy() != |
| 169 chromeos::MultiProfileUserController::ALLOWED) { | 167 chromeos::MultiProfileUserController::ALLOWED) { |
| 170 return ash::AddUserSessionPolicy::ERROR_NOT_ALLOWED_PRIMARY_USER; | 168 return ash::AddUserSessionPolicy::ERROR_NOT_ALLOWED_PRIMARY_USER; |
| 171 } | 169 } |
| 172 | 170 |
| 173 if (UserManager::Get()->GetLoggedInUsers().size() >= kMaxUsers) | 171 if (UserManager::Get()->GetLoggedInUsers().size() >= |
| 172 SessionManager::Get()->GetMaximumNumberOfUserSessions()) |
| 174 return ash::AddUserSessionPolicy::ERROR_MAXIMUM_USERS_REACHED; | 173 return ash::AddUserSessionPolicy::ERROR_MAXIMUM_USERS_REACHED; |
| 175 | 174 |
| 176 return ash::AddUserSessionPolicy::ALLOWED; | 175 return ash::AddUserSessionPolicy::ALLOWED; |
| 177 } | 176 } |
| 178 | 177 |
| 179 // static | 178 // static |
| 180 void SessionControllerClient::DoLockScreen() { | 179 void SessionControllerClient::DoLockScreen() { |
| 181 if (!CanLockScreen()) | 180 if (!CanLockScreen()) |
| 182 return; | 181 return; |
| 183 | 182 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 void SessionControllerClient::ConnectToSessionControllerAndSetClient() { | 245 void SessionControllerClient::ConnectToSessionControllerAndSetClient() { |
| 247 content::ServiceManagerConnection::GetForProcess() | 246 content::ServiceManagerConnection::GetForProcess() |
| 248 ->GetConnector() | 247 ->GetConnector() |
| 249 ->BindInterface(ash_util::GetAshServiceName(), &session_controller_); | 248 ->BindInterface(ash_util::GetAshServiceName(), &session_controller_); |
| 250 | 249 |
| 251 // Set as |session_controller_|'s client. | 250 // Set as |session_controller_|'s client. |
| 252 session_controller_->SetClient(binding_.CreateInterfacePtrAndBind()); | 251 session_controller_->SetClient(binding_.CreateInterfacePtrAndBind()); |
| 253 } | 252 } |
| 254 | 253 |
| 255 void SessionControllerClient::SendSessionInfoIfChanged() { | 254 void SessionControllerClient::SendSessionInfoIfChanged() { |
| 255 SessionManager* const session_manager = SessionManager::Get(); |
| 256 |
| 256 ash::mojom::SessionInfoPtr info = ash::mojom::SessionInfo::New(); | 257 ash::mojom::SessionInfoPtr info = ash::mojom::SessionInfo::New(); |
| 257 info->max_users = kMaxUsers; | 258 info->max_users = session_manager->GetMaximumNumberOfUserSessions(); |
| 258 info->can_lock_screen = CanLockScreen(); | 259 info->can_lock_screen = CanLockScreen(); |
| 259 info->should_lock_screen_automatically = ShouldLockScreenAutomatically(); | 260 info->should_lock_screen_automatically = ShouldLockScreenAutomatically(); |
| 260 info->add_user_session_policy = GetAddUserSessionPolicy(); | 261 info->add_user_session_policy = GetAddUserSessionPolicy(); |
| 261 info->state = session_manager::SessionManager::Get()->session_state(); | 262 info->state = session_manager->session_state(); |
| 262 | 263 |
| 263 if (info != last_sent_session_info_) { | 264 if (info != last_sent_session_info_) { |
| 264 last_sent_session_info_ = info->Clone(); | 265 last_sent_session_info_ = info->Clone(); |
| 265 session_controller_->SetSessionInfo(std::move(info)); | 266 session_controller_->SetSessionInfo(std::move(info)); |
| 266 } | 267 } |
| 267 } | 268 } |
| 268 | 269 |
| 269 void SessionControllerClient::SendUserSession(const User& user) { | 270 void SessionControllerClient::SendUserSession(const User& user) { |
| 270 session_controller_->UpdateUserSession(UserToUserSession(user)); | 271 session_controller_->UpdateUserSession(UserToUserSession(user)); |
| 271 } | 272 } |
| 272 | 273 |
| 273 void SessionControllerClient::SendUserSessionOrder() { | 274 void SessionControllerClient::SendUserSessionOrder() { |
| 274 UserManager* const user_manager = UserManager::Get(); | 275 UserManager* const user_manager = UserManager::Get(); |
| 275 | 276 |
| 276 const UserList logged_in_users = user_manager->GetLoggedInUsers(); | 277 const UserList logged_in_users = user_manager->GetLoggedInUsers(); |
| 277 std::vector<uint32_t> user_session_ids; | 278 std::vector<uint32_t> user_session_ids; |
| 278 for (auto* user : user_manager->GetLRULoggedInUsers()) | 279 for (auto* user : user_manager->GetLRULoggedInUsers()) |
| 279 user_session_ids.push_back(GetSessionId(user)); | 280 user_session_ids.push_back(GetSessionId(user)); |
| 280 | 281 |
| 281 session_controller_->SetUserSessionOrder(user_session_ids); | 282 session_controller_->SetUserSessionOrder(user_session_ids); |
| 282 } | 283 } |
| OLD | NEW |