| 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::Session; |
| 30 using session_manager::SessionManager; | 31 using session_manager::SessionManager; |
| 31 using user_manager::UserManager; | 32 using user_manager::UserManager; |
| 32 using user_manager::User; | 33 using user_manager::User; |
| 33 using user_manager::UserList; | 34 using user_manager::UserList; |
| 34 | 35 |
| 35 namespace { | 36 namespace { |
| 36 | 37 |
| 37 SessionControllerClient* g_instance = nullptr; | 38 SessionControllerClient* g_instance = nullptr; |
| 38 | 39 |
| 39 uint32_t GetSessionId(const User* user) { | 40 // Returns the session id of a given user or 0 if user has no session. |
| 40 const UserList logged_in_users = UserManager::Get()->GetLoggedInUsers(); | 41 uint32_t GetSessionId(const User& user) { |
| 41 // TODO(xiyuan): Update with real session id when user session tracking | 42 const AccountId& account_id = user.GetAccountId(); |
| 42 // code is moved from UserManager to SessionManager. | 43 for (auto& session : SessionManager::Get()->sessions()) { |
| 43 for (size_t i = 0; i < logged_in_users.size(); ++i) { | 44 if (session.user_account_id == account_id) |
| 44 if (logged_in_users[i] == user) | 45 return session.id; |
| 45 return i + 1; | |
| 46 } | 46 } |
| 47 | 47 |
| 48 NOTREACHED(); | |
| 49 return 0u; | 48 return 0u; |
| 50 } | 49 } |
| 51 | 50 |
| 51 // Creates a mojom::UserSession for the given user. Returns nullptr if there is |
| 52 // no user session started for the given user. |
| 52 ash::mojom::UserSessionPtr UserToUserSession(const User& user) { | 53 ash::mojom::UserSessionPtr UserToUserSession(const User& user) { |
| 54 const uint32_t user_session_id = GetSessionId(user); |
| 55 if (user_session_id == 0u) |
| 56 return nullptr; |
| 57 |
| 53 ash::mojom::UserSessionPtr session = ash::mojom::UserSession::New(); | 58 ash::mojom::UserSessionPtr session = ash::mojom::UserSession::New(); |
| 54 session->session_id = GetSessionId(&user); | 59 session->session_id = user_session_id; |
| 55 session->type = user.GetType(); | 60 session->type = user.GetType(); |
| 56 session->account_id = user.GetAccountId(); | 61 session->account_id = user.GetAccountId(); |
| 57 session->display_name = base::UTF16ToUTF8(user.display_name()); | 62 session->display_name = base::UTF16ToUTF8(user.display_name()); |
| 58 session->display_email = user.display_email(); | 63 session->display_email = user.display_email(); |
| 59 | 64 |
| 60 // TODO(xiyuan): Observe user image change and update. | |
| 61 // Tracked in http://crbug.com/670422 | |
| 62 // TODO(xiyuan): Support multiple scale factor. | 65 // TODO(xiyuan): Support multiple scale factor. |
| 63 session->avatar = *user.GetImage().bitmap(); | 66 session->avatar = *user.GetImage().bitmap(); |
| 64 if (session->avatar.isNull()) { | 67 if (session->avatar.isNull()) { |
| 65 session->avatar = *ResourceBundle::GetSharedInstance() | 68 session->avatar = *ResourceBundle::GetSharedInstance() |
| 66 .GetImageSkiaNamed(IDR_PROFILE_PICTURE_LOADING) | 69 .GetImageSkiaNamed(IDR_PROFILE_PICTURE_LOADING) |
| 67 ->bitmap(); | 70 ->bitmap(); |
| 68 } | 71 } |
| 69 | 72 |
| 70 return session; | 73 return session; |
| 71 } | 74 } |
| 72 | 75 |
| 73 void DoSwitchUser(const AccountId& account_id) { | 76 void DoSwitchUser(const AccountId& account_id) { |
| 74 UserManager::Get()->SwitchActiveUser(account_id); | 77 UserManager::Get()->SwitchActiveUser(account_id); |
| 75 } | 78 } |
| 76 | 79 |
| 77 } // namespace | 80 } // namespace |
| 78 | 81 |
| 79 SessionControllerClient::SessionControllerClient() : binding_(this) { | 82 SessionControllerClient::SessionControllerClient() : binding_(this) { |
| 80 SessionManager::Get()->AddObserver(this); | 83 SessionManager::Get()->AddObserver(this); |
| 81 UserManager::Get()->AddSessionStateObserver(this); | 84 UserManager::Get()->AddSessionStateObserver(this); |
| 85 UserManager::Get()->AddObserver(this); |
| 82 | 86 |
| 83 ConnectToSessionControllerAndSetClient(); | 87 ConnectToSessionControllerAndSetClient(); |
| 84 SendSessionInfoIfChanged(); | 88 SendSessionInfoIfChanged(); |
| 85 // User sessions and their order will be sent via UserSessionStateObserver | 89 // User sessions and their order will be sent via UserSessionStateObserver |
| 86 // even for crash-n-restart. | 90 // even for crash-n-restart. |
| 87 | 91 |
| 88 DCHECK(!g_instance); | 92 DCHECK(!g_instance); |
| 89 g_instance = this; | 93 g_instance = this; |
| 90 } | 94 } |
| 91 | 95 |
| 92 SessionControllerClient::~SessionControllerClient() { | 96 SessionControllerClient::~SessionControllerClient() { |
| 93 DCHECK_EQ(this, g_instance); | 97 DCHECK_EQ(this, g_instance); |
| 94 g_instance = nullptr; | 98 g_instance = nullptr; |
| 95 | 99 |
| 96 SessionManager::Get()->RemoveObserver(this); | 100 SessionManager::Get()->RemoveObserver(this); |
| 101 UserManager::Get()->RemoveObserver(this); |
| 97 UserManager::Get()->RemoveSessionStateObserver(this); | 102 UserManager::Get()->RemoveSessionStateObserver(this); |
| 98 } | 103 } |
| 99 | 104 |
| 100 void SessionControllerClient::RequestLockScreen() { | 105 void SessionControllerClient::RequestLockScreen() { |
| 101 DoLockScreen(); | 106 DoLockScreen(); |
| 102 } | 107 } |
| 103 | 108 |
| 104 void SessionControllerClient::SwitchActiveUser(const AccountId& account_id) { | 109 void SessionControllerClient::SwitchActiveUser(const AccountId& account_id) { |
| 105 DoSwitchActiveUser(account_id); | 110 DoSwitchActiveUser(account_id); |
| 106 } | 111 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 121 } | 126 } |
| 122 | 127 |
| 123 SendUserSessionOrder(); | 128 SendUserSessionOrder(); |
| 124 } | 129 } |
| 125 | 130 |
| 126 void SessionControllerClient::UserAddedToSession(const User* added_user) { | 131 void SessionControllerClient::UserAddedToSession(const User* added_user) { |
| 127 SendSessionInfoIfChanged(); | 132 SendSessionInfoIfChanged(); |
| 128 SendUserSession(*added_user); | 133 SendUserSession(*added_user); |
| 129 } | 134 } |
| 130 | 135 |
| 136 void SessionControllerClient::OnUserImageChanged( |
| 137 const user_manager::User& user) { |
| 138 SendUserSession(user); |
| 139 } |
| 140 |
| 131 // static | 141 // static |
| 132 bool SessionControllerClient::CanLockScreen() { | 142 bool SessionControllerClient::CanLockScreen() { |
| 133 return !UserManager::Get()->GetUnlockUsers().empty(); | 143 return !UserManager::Get()->GetUnlockUsers().empty(); |
| 134 } | 144 } |
| 135 | 145 |
| 136 // static | 146 // static |
| 137 bool SessionControllerClient::ShouldLockScreenAutomatically() { | 147 bool SessionControllerClient::ShouldLockScreenAutomatically() { |
| 138 // TODO(xiyuan): Observe prefs::kEnableAutoScreenLock and update ash. | 148 // TODO(xiyuan): Observe prefs::kEnableAutoScreenLock and update ash. |
| 139 // Tracked in http://crbug.com/670423 | 149 // Tracked in http://crbug.com/670423 |
| 140 const UserList logged_in_users = UserManager::Get()->GetLoggedInUsers(); | 150 const UserList logged_in_users = UserManager::Get()->GetLoggedInUsers(); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 info->add_user_session_policy = GetAddUserSessionPolicy(); | 262 info->add_user_session_policy = GetAddUserSessionPolicy(); |
| 253 info->state = session_manager->session_state(); | 263 info->state = session_manager->session_state(); |
| 254 | 264 |
| 255 if (info != last_sent_session_info_) { | 265 if (info != last_sent_session_info_) { |
| 256 last_sent_session_info_ = info->Clone(); | 266 last_sent_session_info_ = info->Clone(); |
| 257 session_controller_->SetSessionInfo(std::move(info)); | 267 session_controller_->SetSessionInfo(std::move(info)); |
| 258 } | 268 } |
| 259 } | 269 } |
| 260 | 270 |
| 261 void SessionControllerClient::SendUserSession(const User& user) { | 271 void SessionControllerClient::SendUserSession(const User& user) { |
| 262 session_controller_->UpdateUserSession(UserToUserSession(user)); | 272 ash::mojom::UserSessionPtr user_session = UserToUserSession(user); |
| 273 |
| 274 // Bail if the user has no session. Currently the only code path that hits |
| 275 // this condition is from OnUserImageChanged when user images are changed |
| 276 // on the login screen (e.g. policy change that adds a public session user, |
| 277 // or tests that create new users on the login screen). |
| 278 if (!user_session) |
| 279 return; |
| 280 |
| 281 session_controller_->UpdateUserSession(std::move(user_session)); |
| 263 } | 282 } |
| 264 | 283 |
| 265 void SessionControllerClient::SendUserSessionOrder() { | 284 void SessionControllerClient::SendUserSessionOrder() { |
| 266 UserManager* const user_manager = UserManager::Get(); | 285 UserManager* const user_manager = UserManager::Get(); |
| 267 | 286 |
| 268 const UserList logged_in_users = user_manager->GetLoggedInUsers(); | 287 const UserList logged_in_users = user_manager->GetLoggedInUsers(); |
| 269 std::vector<uint32_t> user_session_ids; | 288 std::vector<uint32_t> user_session_ids; |
| 270 for (auto* user : user_manager->GetLRULoggedInUsers()) | 289 for (auto* user : user_manager->GetLRULoggedInUsers()) { |
| 271 user_session_ids.push_back(GetSessionId(user)); | 290 const uint32_t user_session_id = GetSessionId(*user); |
| 291 DCHECK_NE(0u, user_session_id); |
| 292 user_session_ids.push_back(user_session_id); |
| 293 } |
| 272 | 294 |
| 273 session_controller_->SetUserSessionOrder(user_session_ids); | 295 session_controller_->SetUserSessionOrder(user_session_ids); |
| 274 } | 296 } |
| OLD | NEW |