Elim cr-checkbox
[chromium-blink-merge.git] / ash / system / chromeos / supervised / tray_supervised_user.cc
blob65b748157789f5fd03f5180ab4bbb9115bc5468d
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ash/system/chromeos/supervised/tray_supervised_user.h"
7 #include "ash/shell.h"
8 #include "ash/system/chromeos/label_tray_view.h"
9 #include "ash/system/system_notifier.h"
10 #include "ash/system/tray/system_tray_delegate.h"
11 #include "ash/system/tray/system_tray_notifier.h"
12 #include "ash/system/tray/tray_notification_view.h"
13 #include "ash/system/user/login_status.h"
14 #include "base/callback.h"
15 #include "base/logging.h"
16 #include "grit/ash_resources.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/message_center/message_center.h"
19 #include "ui/message_center/notification.h"
20 #include "ui/message_center/notification_delegate.h"
22 using message_center::Notification;
24 namespace ash {
26 const char TraySupervisedUser::kNotificationId[] =
27 "chrome://user/locally-managed";
29 TraySupervisedUser::TraySupervisedUser(SystemTray* system_tray)
30 : SystemTrayItem(system_tray),
31 tray_view_(NULL),
32 status_(ash::user::LOGGED_IN_NONE),
33 is_user_supervised_(false) {
34 Shell::GetInstance()->system_tray_delegate()->
35 AddCustodianInfoTrayObserver(this);
38 TraySupervisedUser::~TraySupervisedUser() {
39 // We need the check as on shell destruction delegate is destroyed first.
40 SystemTrayDelegate* system_tray_delegate =
41 Shell::GetInstance()->system_tray_delegate();
42 if (system_tray_delegate)
43 system_tray_delegate->RemoveCustodianInfoTrayObserver(this);
46 void TraySupervisedUser::UpdateMessage() {
47 base::string16 message = Shell::GetInstance()->system_tray_delegate()->
48 GetSupervisedUserMessage();
49 if (tray_view_)
50 tray_view_->SetMessage(message);
51 if (message_center::MessageCenter::Get()->FindVisibleNotificationById(
52 kNotificationId))
53 CreateOrUpdateNotification(message);
56 views::View* TraySupervisedUser::CreateDefaultView(
57 user::LoginStatus status) {
58 CHECK(tray_view_ == NULL);
59 SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
60 if (!delegate->IsUserSupervised())
61 return NULL;
63 tray_view_ = new LabelTrayView(this, GetSupervisedUserIconId());
64 UpdateMessage();
65 return tray_view_;
68 void TraySupervisedUser::DestroyDefaultView() {
69 tray_view_ = NULL;
72 void TraySupervisedUser::OnViewClicked(views::View* sender) {
73 Shell::GetInstance()->system_tray_delegate()->ShowSupervisedUserInfo();
76 void TraySupervisedUser::UpdateAfterLoginStatusChange(
77 user::LoginStatus status) {
78 SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
80 bool is_user_supervised = delegate->IsUserSupervised();
81 if (status == status_ && is_user_supervised == is_user_supervised_)
82 return;
84 if (is_user_supervised &&
85 !delegate->IsUserChild() &&
86 status_ != ash::user::LOGGED_IN_LOCKED &&
87 !delegate->GetSupervisedUserManager().empty())
88 CreateOrUpdateSupervisedWarningNotification();
90 status_ = status;
91 is_user_supervised_ = is_user_supervised;
94 void TraySupervisedUser::CreateOrUpdateNotification(
95 const base::string16& new_message) {
96 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
97 scoped_ptr<Notification> notification(
98 message_center::Notification::CreateSystemNotification(
99 kNotificationId, base::string16() /* no title */, new_message,
100 bundle.GetImageNamed(GetSupervisedUserIconId()),
101 system_notifier::kNotifierSupervisedUser,
102 base::Closure() /* null callback */));
103 message_center::MessageCenter::Get()->AddNotification(notification.Pass());
106 void TraySupervisedUser::CreateOrUpdateSupervisedWarningNotification() {
107 SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
108 CreateOrUpdateNotification(delegate->GetSupervisedUserMessage());
111 void TraySupervisedUser::OnCustodianInfoChanged() {
112 SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
113 std::string manager_name = delegate->GetSupervisedUserManager();
114 if (!manager_name.empty()) {
115 if (!delegate->IsUserChild() &&
116 !message_center::MessageCenter::Get()->FindVisibleNotificationById(
117 kNotificationId)) {
118 CreateOrUpdateSupervisedWarningNotification();
120 UpdateMessage();
124 int TraySupervisedUser::GetSupervisedUserIconId() const {
125 SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
127 // Not intended to be used for non-supervised users.
128 CHECK(delegate->IsUserSupervised());
130 if (delegate->IsUserChild())
131 return IDR_AURA_UBER_TRAY_CHILD_USER;
132 return IDR_AURA_UBER_TRAY_SUPERVISED_USER;
135 } // namespace ash