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.
4
5#include "ash/system/chromeos/supervised/tray_supervised_user.h"
6
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"
21
22using message_center::Notification;
23
24namespace ash {
25
26const char TraySupervisedUser::kNotificationId[] =
27    "chrome://user/locally-managed";
28
29TraySupervisedUser::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}
35
36TraySupervisedUser::~TraySupervisedUser() {
37}
38
39void TraySupervisedUser::UpdateMessage() {
40  base::string16 message = Shell::GetInstance()->system_tray_delegate()->
41      GetSupervisedUserMessage();
42  if (tray_view_)
43    tray_view_->SetMessage(message);
44  if (message_center::MessageCenter::Get()->FindVisibleNotificationById(
45      kNotificationId))
46    CreateOrUpdateNotification(message);
47}
48
49views::View* TraySupervisedUser::CreateDefaultView(
50    user::LoginStatus status) {
51  CHECK(tray_view_ == NULL);
52  SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
53  if (!delegate->IsUserSupervised())
54    return NULL;
55
56  tray_view_ = new LabelTrayView(this, IDR_AURA_UBER_TRAY_SUPERVISED_USER);
57  UpdateMessage();
58  return tray_view_;
59}
60
61void TraySupervisedUser::DestroyDefaultView() {
62  tray_view_ = NULL;
63}
64
65void TraySupervisedUser::OnViewClicked(views::View* sender) {
66  Shell::GetInstance()->system_tray_delegate()->ShowSupervisedUserInfo();
67}
68
69void TraySupervisedUser::UpdateAfterLoginStatusChange(
70    user::LoginStatus status) {
71  SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
72
73  bool is_user_supervised = delegate->IsUserSupervised();
74  if (status == status_ && is_user_supervised == is_user_supervised_)
75    return;
76
77  if (is_user_supervised &&
78      status_ != ash::user::LOGGED_IN_LOCKED) {
79    CreateOrUpdateNotification(delegate->GetSupervisedUserMessage());
80  }
81  status_ = status;
82  is_user_supervised_ = is_user_supervised;
83}
84
85void TraySupervisedUser::CreateOrUpdateNotification(
86    const base::string16& new_message) {
87  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
88  scoped_ptr<Notification> notification(
89      message_center::Notification::CreateSystemNotification(
90          kNotificationId,
91          base::string16() /* no title */,
92          new_message,
93          bundle.GetImageNamed(IDR_AURA_UBER_TRAY_SUPERVISED_USER),
94          system_notifier::kNotifierSupervisedUser,
95          base::Closure() /* null callback */));
96  message_center::MessageCenter::Get()->AddNotification(notification.Pass());
97}
98
99}  // namespace ash
100