tray_supervised_user.cc revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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}
34
35TraySupervisedUser::~TraySupervisedUser() {
36}
37
38void TraySupervisedUser::UpdateMessage() {
39  base::string16 message = Shell::GetInstance()->system_tray_delegate()->
40      GetSupervisedUserMessage();
41  if (tray_view_)
42    tray_view_->SetMessage(message);
43  if (message_center::MessageCenter::Get()->FindVisibleNotificationById(
44      kNotificationId))
45    CreateOrUpdateNotification(message);
46}
47
48views::View* TraySupervisedUser::CreateDefaultView(
49    user::LoginStatus status) {
50  CHECK(tray_view_ == NULL);
51  if (status != ash::user::LOGGED_IN_SUPERVISED)
52    return NULL;
53
54  tray_view_ = new LabelTrayView(this, IDR_AURA_UBER_TRAY_SUPERVISED_USER);
55  UpdateMessage();
56  return tray_view_;
57}
58
59void TraySupervisedUser::DestroyDefaultView() {
60  tray_view_ = NULL;
61}
62
63void TraySupervisedUser::OnViewClicked(views::View* sender) {
64  Shell::GetInstance()->system_tray_delegate()->ShowSupervisedUserInfo();
65}
66
67void TraySupervisedUser::UpdateAfterLoginStatusChange(
68    user::LoginStatus status) {
69  if (status == status_)
70    return;
71  if (status == ash::user::LOGGED_IN_SUPERVISED &&
72      status_ != ash::user::LOGGED_IN_LOCKED) {
73    SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
74    CreateOrUpdateNotification(delegate->GetSupervisedUserMessage());
75  }
76  status_ = status;
77}
78
79void TraySupervisedUser::CreateOrUpdateNotification(
80    const base::string16& new_message) {
81  ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
82  scoped_ptr<Notification> notification(
83      message_center::Notification::CreateSystemNotification(
84          kNotificationId,
85          base::string16() /* no title */,
86          new_message,
87          bundle.GetImageNamed(IDR_AURA_UBER_TRAY_SUPERVISED_USER),
88          system_notifier::kNotifierSupervisedUser,
89          base::Closure() /* null callback */));
90  message_center::MessageCenter::Get()->AddNotification(notification.Pass());
91}
92
93}  // namespace ash
94