Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: chrome/browser/ui/ash/session_controller_client.cc

Issue 2832903002: cros: Remove supervised user methods from SystemTrayDelegate (Closed)
Patch Set: review comments Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/threading/thread_task_runner_handle.h"
15 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/chromeos/login/user_flow.h" 17 #include "chrome/browser/chromeos/login/user_flow.h"
17 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h" 18 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
18 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" 19 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h"
19 #include "chrome/browser/chromeos/profiles/profile_helper.h" 20 #include "chrome/browser/chromeos/profiles/profile_helper.h"
20 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 session->account_id = user.GetAccountId(); 68 session->account_id = user.GetAccountId();
66 session->display_name = base::UTF16ToUTF8(user.display_name()); 69 session->display_name = base::UTF16ToUTF8(user.display_name());
67 session->display_email = user.display_email(); 70 session->display_email = user.display_email();
68 71
69 session->avatar = user.GetImage(); 72 session->avatar = user.GetImage();
70 if (session->avatar.isNull()) { 73 if (session->avatar.isNull()) {
71 session->avatar = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed( 74 session->avatar = *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
72 IDR_PROFILE_PICTURE_LOADING); 75 IDR_PROFILE_PICTURE_LOADING);
73 } 76 }
74 77
78 if (user.IsSupervised()) {
79 Profile* profile = chromeos::ProfileHelper::Get()->GetProfileByUser(&user);
80 if (profile) {
81 SupervisedUserService* service =
82 SupervisedUserServiceFactory::GetForProfile(profile);
83 session->custodian_email = service->GetCustodianEmailAddress();
84 session->second_custodian_email =
85 service->GetSecondCustodianEmailAddress();
86 }
87 }
88
75 chromeos::UserFlow* const user_flow = 89 chromeos::UserFlow* const user_flow =
76 chromeos::ChromeUserManager::Get()->GetUserFlow(user.GetAccountId()); 90 chromeos::ChromeUserManager::Get()->GetUserFlow(user.GetAccountId());
77 session->should_enable_settings = user_flow->ShouldEnableSettings(); 91 session->should_enable_settings = user_flow->ShouldEnableSettings();
78 session->should_show_notification_tray = 92 session->should_show_notification_tray =
79 user_flow->ShouldShowNotificationTray(); 93 user_flow->ShouldShowNotificationTray();
80 94
81 return session; 95 return session;
82 } 96 }
83 97
84 void DoSwitchUser(const AccountId& account_id) { 98 void DoSwitchUser(const AccountId& account_id) {
85 UserManager::Get()->SwitchActiveUser(account_id); 99 UserManager::Get()->SwitchActiveUser(account_id);
86 } 100 }
87 101
88 } // namespace 102 } // namespace
89 103
90 SessionControllerClient::SessionControllerClient() : binding_(this) { 104 SessionControllerClient::SessionControllerClient()
105 : binding_(this), weak_ptr_factory_(this) {
91 SessionManager::Get()->AddObserver(this); 106 SessionManager::Get()->AddObserver(this);
92 UserManager::Get()->AddSessionStateObserver(this); 107 UserManager::Get()->AddSessionStateObserver(this);
93 UserManager::Get()->AddObserver(this); 108 UserManager::Get()->AddObserver(this);
94 109
95 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, 110 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
96 content::NotificationService::AllSources()); 111 content::NotificationService::AllSources());
97 112 registrar_.Add(this, chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED,
98 ConnectToSessionControllerAndSetClient(); 113 content::NotificationService::AllSources());
99 SendSessionInfoIfChanged();
100 // User sessions and their order will be sent via UserSessionStateObserver
101 // even for crash-n-restart.
102 114
103 DCHECK(!g_instance); 115 DCHECK(!g_instance);
104 g_instance = this; 116 g_instance = this;
105 } 117 }
106 118
107 SessionControllerClient::~SessionControllerClient() { 119 SessionControllerClient::~SessionControllerClient() {
108 DCHECK_EQ(this, g_instance); 120 DCHECK_EQ(this, g_instance);
109 g_instance = nullptr; 121 g_instance = nullptr;
110 122
123 if (supervised_user_profile_) {
124 SupervisedUserServiceFactory::GetForProfile(supervised_user_profile_)
125 ->RemoveObserver(this);
126 }
127
111 SessionManager::Get()->RemoveObserver(this); 128 SessionManager::Get()->RemoveObserver(this);
112 UserManager::Get()->RemoveObserver(this); 129 UserManager::Get()->RemoveObserver(this);
113 UserManager::Get()->RemoveSessionStateObserver(this); 130 UserManager::Get()->RemoveSessionStateObserver(this);
114 } 131 }
115 132
133 void SessionControllerClient::Init() {
134 ConnectToSessionController();
135 session_controller_->SetClient(binding_.CreateInterfacePtrAndBind());
136 SendSessionInfoIfChanged();
137 // User sessions and their order will be sent via UserSessionStateObserver
138 // even for crash-n-restart.
139 }
140
116 // static 141 // static
117 SessionControllerClient* SessionControllerClient::Get() { 142 SessionControllerClient* SessionControllerClient::Get() {
118 return g_instance; 143 return g_instance;
119 } 144 }
120 145
121 void SessionControllerClient::StartLock(StartLockCallback callback) { 146 void SessionControllerClient::StartLock(StartLockCallback callback) {
122 session_controller_->StartLock(callback); 147 session_controller_->StartLock(callback);
123 } 148 }
124 149
125 void SessionControllerClient::RunUnlockAnimation( 150 void SessionControllerClient::RunUnlockAnimation(
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 291
267 // static 292 // static
268 void SessionControllerClient::FlushForTesting() { 293 void SessionControllerClient::FlushForTesting() {
269 g_instance->session_controller_.FlushForTesting(); 294 g_instance->session_controller_.FlushForTesting();
270 } 295 }
271 296
272 void SessionControllerClient::OnSessionStateChanged() { 297 void SessionControllerClient::OnSessionStateChanged() {
273 SendSessionInfoIfChanged(); 298 SendSessionInfoIfChanged();
274 } 299 }
275 300
301 void SessionControllerClient::OnCustodianInfoChanged() {
302 DCHECK(supervised_user_profile_);
303 User* user = chromeos::ProfileHelper::Get()->GetUserByProfile(
304 supervised_user_profile_);
305 if (user)
306 SendUserSession(*user);
307 }
308
276 void SessionControllerClient::Observe( 309 void SessionControllerClient::Observe(
277 int type, 310 int type,
278 const content::NotificationSource& source, 311 const content::NotificationSource& source,
279 const content::NotificationDetails& details) { 312 const content::NotificationDetails& details) {
280 DCHECK_EQ(chrome::NOTIFICATION_APP_TERMINATING, type); 313 switch (type) {
281 session_controller_->NotifyChromeTerminating(); 314 case chrome::NOTIFICATION_APP_TERMINATING:
315 session_controller_->NotifyChromeTerminating();
316 break;
317 case chrome::NOTIFICATION_LOGIN_USER_PROFILE_PREPARED: {
318 Profile* profile = content::Details<Profile>(details).ptr();
319 OnLoginUserProfilePrepared(profile);
320 break;
321 }
322 default:
323 NOTREACHED() << "Unexpected notification " << type;
324 break;
325 }
282 } 326 }
283 327
284 void SessionControllerClient::ConnectToSessionControllerAndSetClient() { 328 void SessionControllerClient::OnLoginUserProfilePrepared(Profile* profile) {
329 const User* user = chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
330 DCHECK(user);
331
332 if (profile->IsSupervised()) {
333 // There can be only one supervised user per session.
334 DCHECK(!supervised_user_profile_);
335 supervised_user_profile_ = profile;
336
337 // Watch for changes to supervised user manager/custodians.
338 SupervisedUserServiceFactory::GetForProfile(supervised_user_profile_)
339 ->AddObserver(this);
340 }
341
342 // Needed because chromeos::ProfileHelper isn't updated at this point, so we
xiyuan 2017/04/24 18:10:09 ProfileHelper should be updated when a user profil
James Cook 2017/04/24 18:34:51 chromeos::ProfileHelper::Get()->GetProfileByUser(&
xiyuan 2017/04/24 19:03:07 I thought the post task is for profile -> user. An
343 // have to wait before we can do user/profile conversions.
344 base::ThreadTaskRunnerHandle::Get()->PostTask(
345 FROM_HERE, base::Bind(&SessionControllerClient::SendUserSessionForProfile,
346 weak_ptr_factory_.GetWeakPtr(), profile));
347 }
348
349 void SessionControllerClient::SendUserSessionForProfile(Profile* profile) {
350 DCHECK(profile);
351 const User* user = chromeos::ProfileHelper::Get()->GetUserByProfile(profile);
352 DCHECK(user);
353 SendUserSession(*user);
354 }
355
356 void SessionControllerClient::ConnectToSessionController() {
357 // Tests may bind to their own SessionController.
358 if (session_controller_)
359 return;
360
285 content::ServiceManagerConnection::GetForProcess() 361 content::ServiceManagerConnection::GetForProcess()
286 ->GetConnector() 362 ->GetConnector()
287 ->BindInterface(ash::mojom::kServiceName, &session_controller_); 363 ->BindInterface(ash::mojom::kServiceName, &session_controller_);
288
289 // Set as |session_controller_|'s client.
290 session_controller_->SetClient(binding_.CreateInterfacePtrAndBind());
291 } 364 }
292 365
293 void SessionControllerClient::SendSessionInfoIfChanged() { 366 void SessionControllerClient::SendSessionInfoIfChanged() {
294 SessionManager* const session_manager = SessionManager::Get(); 367 SessionManager* const session_manager = SessionManager::Get();
295 368
296 ash::mojom::SessionInfoPtr info = ash::mojom::SessionInfo::New(); 369 ash::mojom::SessionInfoPtr info = ash::mojom::SessionInfo::New();
297 info->can_lock_screen = CanLockScreen(); 370 info->can_lock_screen = CanLockScreen();
298 info->should_lock_screen_automatically = ShouldLockScreenAutomatically(); 371 info->should_lock_screen_automatically = ShouldLockScreenAutomatically();
299 info->add_user_session_policy = GetAddUserSessionPolicy(); 372 info->add_user_session_policy = GetAddUserSessionPolicy();
300 info->state = session_manager->session_state(); 373 info->state = session_manager->session_state();
301 374
302 if (info != last_sent_session_info_) { 375 if (info != last_sent_session_info_) {
303 last_sent_session_info_ = info->Clone(); 376 last_sent_session_info_ = info->Clone();
304 session_controller_->SetSessionInfo(std::move(info)); 377 session_controller_->SetSessionInfo(std::move(info));
305 } 378 }
306 } 379 }
307 380
308 void SessionControllerClient::SendUserSession(const User& user) { 381 void SessionControllerClient::SendUserSession(const User& user) {
309 ash::mojom::UserSessionPtr user_session = UserToUserSession(user); 382 ash::mojom::UserSessionPtr user_session = UserToUserSession(user);
310 383
311 // Bail if the user has no session. Currently the only code path that hits 384 // 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 385 // 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, 386 // 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). 387 // or tests that create new users on the login screen).
315 if (!user_session) 388 if (!user_session)
316 return; 389 return;
317 390
391 // TODO(jamescook): Only send if it changed. This will require an Equals()
392 // method for gfx::ImageSkia to allow mojom::UserSession comparison.
393 // http://crbug.com/714689
318 session_controller_->UpdateUserSession(std::move(user_session)); 394 session_controller_->UpdateUserSession(std::move(user_session));
319 } 395 }
320 396
321 void SessionControllerClient::SendUserSessionOrder() { 397 void SessionControllerClient::SendUserSessionOrder() {
322 UserManager* const user_manager = UserManager::Get(); 398 UserManager* const user_manager = UserManager::Get();
323 399
324 const UserList logged_in_users = user_manager->GetLoggedInUsers(); 400 const UserList logged_in_users = user_manager->GetLoggedInUsers();
325 std::vector<uint32_t> user_session_ids; 401 std::vector<uint32_t> user_session_ids;
326 for (auto* user : user_manager->GetLRULoggedInUsers()) { 402 for (auto* user : user_manager->GetLRULoggedInUsers()) {
327 const uint32_t user_session_id = GetSessionId(*user); 403 const uint32_t user_session_id = GetSessionId(*user);
328 DCHECK_NE(0u, user_session_id); 404 DCHECK_NE(0u, user_session_id);
329 user_session_ids.push_back(user_session_id); 405 user_session_ids.push_back(user_session_id);
330 } 406 }
331 407
332 session_controller_->SetUserSessionOrder(user_session_ids); 408 session_controller_->SetUserSessionOrder(user_session_ids);
333 } 409 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/ash/session_controller_client.h ('k') | chrome/browser/ui/ash/session_controller_client_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698