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 "ash/public/interfaces/constants.mojom.h" | 11 #include "ash/public/interfaces/constants.mojom.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
16 #include "chrome/browser/chromeos/login/user_flow.h" | 16 #include "chrome/browser/chromeos/login/user_flow.h" |
17 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h" | 17 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h" |
18 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" | 18 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" |
19 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 19 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/browser/profiles/profile_manager.h" | |
22 #include "chrome/browser/supervised_user/supervised_user_service.h" | |
23 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | |
21 #include "chrome/browser/ui/ash/multi_user/user_switch_util.h" | 24 #include "chrome/browser/ui/ash/multi_user/user_switch_util.h" |
22 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
23 #include "chrome/grit/theme_resources.h" | 26 #include "chrome/grit/theme_resources.h" |
24 #include "chromeos/dbus/dbus_thread_manager.h" | 27 #include "chromeos/dbus/dbus_thread_manager.h" |
25 #include "chromeos/dbus/session_manager_client.h" | 28 #include "chromeos/dbus/session_manager_client.h" |
26 #include "components/prefs/pref_service.h" | 29 #include "components/prefs/pref_service.h" |
27 #include "components/session_manager/core/session_manager.h" | 30 #include "components/session_manager/core/session_manager.h" |
28 #include "content/public/browser/notification_service.h" | 31 #include "content/public/browser/notification_service.h" |
29 #include "content/public/common/service_manager_connection.h" | 32 #include "content/public/common/service_manager_connection.h" |
30 #include "content/public/common/service_names.mojom.h" | 33 #include "content/public/common/service_names.mojom.h" |
(...skipping 16 matching lines...) Expand all Loading... | |
47 for (auto& session : SessionManager::Get()->sessions()) { | 50 for (auto& session : SessionManager::Get()->sessions()) { |
48 if (session.user_account_id == account_id) | 51 if (session.user_account_id == account_id) |
49 return session.id; | 52 return session.id; |
50 } | 53 } |
51 | 54 |
52 return 0u; | 55 return 0u; |
53 } | 56 } |
54 | 57 |
55 // Creates a mojom::UserSession for the given user. Returns nullptr if there is | 58 // Creates a mojom::UserSession for the given user. Returns nullptr if there is |
56 // no user session started for the given user. | 59 // no user session started for the given user. |
57 ash::mojom::UserSessionPtr UserToUserSession(const User& user) { | 60 ash::mojom::UserSessionPtr UserToUserSession(const User& user, |
61 Profile* login_profile) { | |
58 const uint32_t user_session_id = GetSessionId(user); | 62 const uint32_t user_session_id = GetSessionId(user); |
59 if (user_session_id == 0u) | 63 if (user_session_id == 0u) |
60 return nullptr; | 64 return nullptr; |
61 | 65 |
62 ash::mojom::UserSessionPtr session = ash::mojom::UserSession::New(); | 66 ash::mojom::UserSessionPtr session = ash::mojom::UserSession::New(); |
63 session->session_id = user_session_id; | 67 session->session_id = user_session_id; |
64 session->type = user.GetType(); | 68 session->type = user.GetType(); |
65 session->account_id = user.GetAccountId(); | 69 session->account_id = user.GetAccountId(); |
66 session->display_name = base::UTF16ToUTF8(user.display_name()); | 70 session->display_name = base::UTF16ToUTF8(user.display_name()); |
67 session->display_email = user.display_email(); | 71 session->display_email = user.display_email(); |
68 | 72 |
69 session->avatar = user.GetImage(); | 73 session->avatar = user.GetImage(); |
70 if (session->avatar.isNull()) { | 74 if (session->avatar.isNull()) { |
71 session->avatar = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 75 session->avatar = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
72 IDR_PROFILE_PICTURE_LOADING); | 76 IDR_PROFILE_PICTURE_LOADING); |
73 } | 77 } |
74 | 78 |
79 if (user.IsSupervised() && login_profile) { | |
80 SupervisedUserService* service = | |
81 SupervisedUserServiceFactory::GetForProfile(login_profile); | |
82 session->custodian_email = service->GetCustodianEmailAddress(); | |
83 session->second_custodian_email = service->GetSecondCustodianEmailAddress(); | |
84 } | |
85 | |
75 chromeos::UserFlow* const user_flow = | 86 chromeos::UserFlow* const user_flow = |
76 chromeos::ChromeUserManager::Get()->GetUserFlow(user.GetAccountId()); | 87 chromeos::ChromeUserManager::Get()->GetUserFlow(user.GetAccountId()); |
77 session->should_enable_settings = user_flow->ShouldEnableSettings(); | 88 session->should_enable_settings = user_flow->ShouldEnableSettings(); |
78 session->should_show_notification_tray = | 89 session->should_show_notification_tray = |
79 user_flow->ShouldShowNotificationTray(); | 90 user_flow->ShouldShowNotificationTray(); |
80 | 91 |
81 return session; | 92 return session; |
82 } | 93 } |
83 | 94 |
84 void DoSwitchUser(const AccountId& account_id) { | 95 void DoSwitchUser(const AccountId& account_id) { |
85 UserManager::Get()->SwitchActiveUser(account_id); | 96 UserManager::Get()->SwitchActiveUser(account_id); |
86 } | 97 } |
87 | 98 |
88 } // namespace | 99 } // namespace |
89 | 100 |
90 SessionControllerClient::SessionControllerClient() : binding_(this) { | 101 SessionControllerClient::SessionControllerClient() : binding_(this) { |
91 SessionManager::Get()->AddObserver(this); | 102 SessionManager::Get()->AddObserver(this); |
92 UserManager::Get()->AddSessionStateObserver(this); | 103 UserManager::Get()->AddSessionStateObserver(this); |
93 UserManager::Get()->AddObserver(this); | 104 UserManager::Get()->AddObserver(this); |
94 | 105 |
95 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | 106 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
96 content::NotificationService::AllSources()); | 107 content::NotificationService::AllSources()); |
97 | 108 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED, |
98 ConnectToSessionControllerAndSetClient(); | 109 content::NotificationService::AllSources()); |
99 SendSessionInfoIfChanged(); | |
100 // User sessions and their order will be sent via UserSessionStateObserver | |
101 // even for crash-n-restart. | |
102 | 110 |
103 DCHECK(!g_instance); | 111 DCHECK(!g_instance); |
104 g_instance = this; | 112 g_instance = this; |
105 } | 113 } |
106 | 114 |
107 SessionControllerClient::~SessionControllerClient() { | 115 SessionControllerClient::~SessionControllerClient() { |
108 DCHECK_EQ(this, g_instance); | 116 DCHECK_EQ(this, g_instance); |
109 g_instance = nullptr; | 117 g_instance = nullptr; |
110 | 118 |
119 if (login_profile_) { | |
120 SupervisedUserServiceFactory::GetForProfile(login_profile_) | |
121 ->RemoveObserver(this); | |
122 } | |
111 SessionManager::Get()->RemoveObserver(this); | 123 SessionManager::Get()->RemoveObserver(this); |
112 UserManager::Get()->RemoveObserver(this); | 124 UserManager::Get()->RemoveObserver(this); |
113 UserManager::Get()->RemoveSessionStateObserver(this); | 125 UserManager::Get()->RemoveSessionStateObserver(this); |
114 } | 126 } |
115 | 127 |
128 void SessionControllerClient::Init() { | |
129 ConnectToSessionController(); | |
130 session_controller_->SetClient(binding_.CreateInterfacePtrAndBind()); | |
131 SendSessionInfoIfChanged(); | |
132 // User sessions and their order will be sent via UserSessionStateObserver | |
133 // even for crash-n-restart. | |
134 } | |
135 | |
116 // static | 136 // static |
117 SessionControllerClient* SessionControllerClient::Get() { | 137 SessionControllerClient* SessionControllerClient::Get() { |
118 return g_instance; | 138 return g_instance; |
119 } | 139 } |
120 | 140 |
121 void SessionControllerClient::StartLock(StartLockCallback callback) { | 141 void SessionControllerClient::StartLock(StartLockCallback callback) { |
122 session_controller_->StartLock(callback); | 142 session_controller_->StartLock(callback); |
123 } | 143 } |
124 | 144 |
125 void SessionControllerClient::RunUnlockAnimation( | 145 void SessionControllerClient::RunUnlockAnimation( |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 | 286 |
267 // static | 287 // static |
268 void SessionControllerClient::FlushForTesting() { | 288 void SessionControllerClient::FlushForTesting() { |
269 g_instance->session_controller_.FlushForTesting(); | 289 g_instance->session_controller_.FlushForTesting(); |
270 } | 290 } |
271 | 291 |
272 void SessionControllerClient::OnSessionStateChanged() { | 292 void SessionControllerClient::OnSessionStateChanged() { |
273 SendSessionInfoIfChanged(); | 293 SendSessionInfoIfChanged(); |
274 } | 294 } |
275 | 295 |
296 void SessionControllerClient::OnCustodianInfoChanged() { | |
297 SendActiveUserSession(); | |
xiyuan
2017/04/21 23:14:35
Is this safe because supervised user is not allowe
James Cook
2017/04/24 17:47:26
How does this look?
xiyuan
2017/04/24 18:10:09
Looks good.
| |
298 } | |
299 | |
276 void SessionControllerClient::Observe( | 300 void SessionControllerClient::Observe( |
277 int type, | 301 int type, |
278 const content::NotificationSource& source, | 302 const content::NotificationSource& source, |
279 const content::NotificationDetails& details) { | 303 const content::NotificationDetails& details) { |
280 DCHECK_EQ(chrome::NOTIFICATION_APP_TERMINATING, type); | 304 switch (type) { |
281 session_controller_->NotifyChromeTerminating(); | 305 case chrome::NOTIFICATION_APP_TERMINATING: |
306 session_controller_->NotifyChromeTerminating(); | |
307 break; | |
308 case chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED: { | |
309 Profile* profile = content::Details<Profile>(details).ptr(); | |
310 OnLoginUserProfilePrepared(profile); | |
311 break; | |
312 } | |
313 default: | |
314 NOTREACHED() << "Unexpected notification " << type; | |
315 break; | |
316 } | |
282 } | 317 } |
283 | 318 |
284 void SessionControllerClient::ConnectToSessionControllerAndSetClient() { | 319 void SessionControllerClient::OnLoginUserProfilePrepared(Profile* profile) { |
320 DCHECK(!login_profile_); | |
321 login_profile_ = profile; | |
322 | |
323 // Update the user session data with information from the profile. | |
324 SendActiveUserSession(); | |
325 | |
326 // Watch for changes to supervised user manager/custodians. Supervised users | |
327 // cannot use multiprofile so we don't need to worry about profile switching | |
328 // and adding/removing observers. | |
329 SupervisedUserServiceFactory::GetForProfile(login_profile_) | |
330 ->AddObserver(this); | |
331 } | |
332 | |
333 void SessionControllerClient::ConnectToSessionController() { | |
334 // Tests may bind to their own SessionController. | |
335 if (session_controller_) | |
336 return; | |
337 | |
285 content::ServiceManagerConnection::GetForProcess() | 338 content::ServiceManagerConnection::GetForProcess() |
286 ->GetConnector() | 339 ->GetConnector() |
287 ->BindInterface(ash::mojom::kServiceName, &session_controller_); | 340 ->BindInterface(ash::mojom::kServiceName, &session_controller_); |
288 | |
289 // Set as |session_controller_|'s client. | |
290 session_controller_->SetClient(binding_.CreateInterfacePtrAndBind()); | |
291 } | 341 } |
292 | 342 |
293 void SessionControllerClient::SendSessionInfoIfChanged() { | 343 void SessionControllerClient::SendSessionInfoIfChanged() { |
294 SessionManager* const session_manager = SessionManager::Get(); | 344 SessionManager* const session_manager = SessionManager::Get(); |
295 | 345 |
296 ash::mojom::SessionInfoPtr info = ash::mojom::SessionInfo::New(); | 346 ash::mojom::SessionInfoPtr info = ash::mojom::SessionInfo::New(); |
297 info->can_lock_screen = CanLockScreen(); | 347 info->can_lock_screen = CanLockScreen(); |
298 info->should_lock_screen_automatically = ShouldLockScreenAutomatically(); | 348 info->should_lock_screen_automatically = ShouldLockScreenAutomatically(); |
299 info->add_user_session_policy = GetAddUserSessionPolicy(); | 349 info->add_user_session_policy = GetAddUserSessionPolicy(); |
300 info->state = session_manager->session_state(); | 350 info->state = session_manager->session_state(); |
301 | 351 |
302 if (info != last_sent_session_info_) { | 352 if (info != last_sent_session_info_) { |
303 last_sent_session_info_ = info->Clone(); | 353 last_sent_session_info_ = info->Clone(); |
304 session_controller_->SetSessionInfo(std::move(info)); | 354 session_controller_->SetSessionInfo(std::move(info)); |
305 } | 355 } |
306 } | 356 } |
307 | 357 |
308 void SessionControllerClient::SendUserSession(const User& user) { | 358 void SessionControllerClient::SendUserSession(const User& user) { |
309 ash::mojom::UserSessionPtr user_session = UserToUserSession(user); | 359 ash::mojom::UserSessionPtr user_session = |
360 UserToUserSession(user, login_profile_); | |
310 | 361 |
311 // Bail if the user has no session. Currently the only code path that hits | 362 // Bail if the user has no session. Currently the only code path that hits |
312 // this condition is from OnUserImageChanged when user images are changed | 363 // this condition is from OnUserImageChanged when user images are changed |
313 // on the login screen (e.g. policy change that adds a public session user, | 364 // on the login screen (e.g. policy change that adds a public session user, |
314 // or tests that create new users on the login screen). | 365 // or tests that create new users on the login screen). |
315 if (!user_session) | 366 if (!user_session) |
316 return; | 367 return; |
317 | 368 |
369 // TODO(jamescook): Only send if it changed. This will require an Equals() | |
370 // method for gfx::ImageSkia to allow mojom::UserSession comparison. | |
xiyuan
2017/04/21 23:14:35
We can use ImageSkia::BackedBySameObjectAs to impl
James Cook
2017/04/24 17:47:26
Is it OK if I do this in a separate CL?
| |
318 session_controller_->UpdateUserSession(std::move(user_session)); | 371 session_controller_->UpdateUserSession(std::move(user_session)); |
319 } | 372 } |
320 | 373 |
374 void SessionControllerClient::SendActiveUserSession() { | |
375 User* active_user = UserManager::Get()->GetActiveUser(); | |
376 DCHECK(active_user); | |
377 SendUserSession(*active_user); | |
378 } | |
379 | |
321 void SessionControllerClient::SendUserSessionOrder() { | 380 void SessionControllerClient::SendUserSessionOrder() { |
322 UserManager* const user_manager = UserManager::Get(); | 381 UserManager* const user_manager = UserManager::Get(); |
323 | 382 |
324 const UserList logged_in_users = user_manager->GetLoggedInUsers(); | 383 const UserList logged_in_users = user_manager->GetLoggedInUsers(); |
325 std::vector<uint32_t> user_session_ids; | 384 std::vector<uint32_t> user_session_ids; |
326 for (auto* user : user_manager->GetLRULoggedInUsers()) { | 385 for (auto* user : user_manager->GetLRULoggedInUsers()) { |
327 const uint32_t user_session_id = GetSessionId(*user); | 386 const uint32_t user_session_id = GetSessionId(*user); |
328 DCHECK_NE(0u, user_session_id); | 387 DCHECK_NE(0u, user_session_id); |
329 user_session_ids.push_back(user_session_id); | 388 user_session_ids.push_back(user_session_id); |
330 } | 389 } |
331 | 390 |
332 session_controller_->SetUserSessionOrder(user_session_ids); | 391 session_controller_->SetUserSessionOrder(user_session_ids); |
333 } | 392 } |
OLD | NEW |