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

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

Issue 2832903002: cros: Remove supervised user methods from SystemTrayDelegate (Closed)
Patch Set: remove DCHECK 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 "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
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) {
xiyuan 2017/04/21 23:14:35 nit: We could use ProfileHelper to look up user pr
James Cook 2017/04/24 17:47:26 Done.
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
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();
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 // NOTE: Some tests send the notification multiple times, so we can't
321 // DCHECK(!login_profile_);
322 login_profile_ = profile;
323
324 // Update the user session data with information from the profile.
325 SendActiveUserSession();
326
327 // Watch for changes to supervised user manager/custodians. Supervised users
328 // cannot use multiprofile so we don't need to worry about profile switching
329 // and adding/removing observers.
330 SupervisedUserServiceFactory::GetForProfile(login_profile_)
331 ->AddObserver(this);
332 }
333
334 void SessionControllerClient::ConnectToSessionController() {
335 // Tests may bind to their own SessionController.
336 if (session_controller_)
337 return;
338
285 content::ServiceManagerConnection::GetForProcess() 339 content::ServiceManagerConnection::GetForProcess()
286 ->GetConnector() 340 ->GetConnector()
287 ->BindInterface(ash::mojom::kServiceName, &session_controller_); 341 ->BindInterface(ash::mojom::kServiceName, &session_controller_);
288
289 // Set as |session_controller_|'s client.
290 session_controller_->SetClient(binding_.CreateInterfacePtrAndBind());
291 } 342 }
292 343
293 void SessionControllerClient::SendSessionInfoIfChanged() { 344 void SessionControllerClient::SendSessionInfoIfChanged() {
294 SessionManager* const session_manager = SessionManager::Get(); 345 SessionManager* const session_manager = SessionManager::Get();
295 346
296 ash::mojom::SessionInfoPtr info = ash::mojom::SessionInfo::New(); 347 ash::mojom::SessionInfoPtr info = ash::mojom::SessionInfo::New();
297 info->can_lock_screen = CanLockScreen(); 348 info->can_lock_screen = CanLockScreen();
298 info->should_lock_screen_automatically = ShouldLockScreenAutomatically(); 349 info->should_lock_screen_automatically = ShouldLockScreenAutomatically();
299 info->add_user_session_policy = GetAddUserSessionPolicy(); 350 info->add_user_session_policy = GetAddUserSessionPolicy();
300 info->state = session_manager->session_state(); 351 info->state = session_manager->session_state();
301 352
302 if (info != last_sent_session_info_) { 353 if (info != last_sent_session_info_) {
303 last_sent_session_info_ = info->Clone(); 354 last_sent_session_info_ = info->Clone();
304 session_controller_->SetSessionInfo(std::move(info)); 355 session_controller_->SetSessionInfo(std::move(info));
305 } 356 }
306 } 357 }
307 358
308 void SessionControllerClient::SendUserSession(const User& user) { 359 void SessionControllerClient::SendUserSession(const User& user) {
309 ash::mojom::UserSessionPtr user_session = UserToUserSession(user); 360 ash::mojom::UserSessionPtr user_session =
361 UserToUserSession(user, login_profile_);
310 362
311 // Bail if the user has no session. Currently the only code path that hits 363 // 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 364 // 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, 365 // 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). 366 // or tests that create new users on the login screen).
315 if (!user_session) 367 if (!user_session)
316 return; 368 return;
317 369
370 // TODO(jamescook): Only send if it changed. This will require an Equals()
371 // method for gfx::ImageSkia to allow mojom::UserSession comparison.
318 session_controller_->UpdateUserSession(std::move(user_session)); 372 session_controller_->UpdateUserSession(std::move(user_session));
319 } 373 }
320 374
375 void SessionControllerClient::SendActiveUserSession() {
376 User* active_user = UserManager::Get()->GetActiveUser();
377 DCHECK(active_user);
378 SendUserSession(*active_user);
379 }
380
321 void SessionControllerClient::SendUserSessionOrder() { 381 void SessionControllerClient::SendUserSessionOrder() {
322 UserManager* const user_manager = UserManager::Get(); 382 UserManager* const user_manager = UserManager::Get();
323 383
324 const UserList logged_in_users = user_manager->GetLoggedInUsers(); 384 const UserList logged_in_users = user_manager->GetLoggedInUsers();
325 std::vector<uint32_t> user_session_ids; 385 std::vector<uint32_t> user_session_ids;
326 for (auto* user : user_manager->GetLRULoggedInUsers()) { 386 for (auto* user : user_manager->GetLRULoggedInUsers()) {
327 const uint32_t user_session_id = GetSessionId(*user); 387 const uint32_t user_session_id = GetSessionId(*user);
328 DCHECK_NE(0u, user_session_id); 388 DCHECK_NE(0u, user_session_id);
329 user_session_ids.push_back(user_session_id); 389 user_session_ids.push_back(user_session_id);
330 } 390 }
331 391
332 session_controller_->SetUserSessionOrder(user_session_ids); 392 session_controller_->SetUserSessionOrder(user_session_ids);
333 } 393 }
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