Home | History | Annotate | Download | only in managed
      1 // Copyright (c) 2012 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/managed/tray_locally_managed_user.h"
      6 
      7 #include "ash/system/chromeos/label_tray_view.h"
      8 #include "ash/system/tray/system_tray_notifier.h"
      9 #include "ash/system/tray/tray_notification_view.h"
     10 #include "ash/system/user/login_status.h"
     11 #include "base/logging.h"
     12 #include "grit/ash_resources.h"
     13 #include "ui/message_center/message_center.h"
     14 #include "ui/message_center/notification.h"
     15 #include "ui/message_center/notification_delegate.h"
     16 
     17 using message_center::Notification;
     18 
     19 namespace ash {
     20 namespace internal {
     21 namespace {
     22 
     23 const char kLocallyManagedUserNotificationId[] =
     24     "chrome://user/locally-managed";
     25 
     26 void CreateOrUpdateNotification(const base::string16& new_message) {
     27   scoped_ptr<Notification> notification(new Notification(
     28       message_center::NOTIFICATION_TYPE_SIMPLE,
     29       kLocallyManagedUserNotificationId,
     30       new_message,
     31       base::string16() /* body is empty */,
     32       gfx::Image() /* icon */,
     33       base::string16() /* display_source */,
     34       std::string() /* extension_id */,
     35       message_center::RichNotificationData(),
     36       NULL /* no delegate */));
     37   notification->SetSystemPriority();
     38   message_center::MessageCenter::Get()->AddNotification(notification.Pass());
     39 }
     40 
     41 } // namespace
     42 
     43 TrayLocallyManagedUser::TrayLocallyManagedUser(SystemTray* system_tray)
     44     : SystemTrayItem(system_tray),
     45       tray_view_(NULL),
     46       status_(ash::user::LOGGED_IN_NONE) {
     47 }
     48 
     49 TrayLocallyManagedUser::~TrayLocallyManagedUser() {
     50 }
     51 
     52 void TrayLocallyManagedUser::UpdateMessage() {
     53   base::string16 message = Shell::GetInstance()->system_tray_delegate()->
     54       GetLocallyManagedUserMessage();
     55   if (tray_view_)
     56     tray_view_->SetMessage(message);
     57   if (message_center::MessageCenter::Get()->HasNotification(
     58           kLocallyManagedUserNotificationId)) {
     59     CreateOrUpdateNotification(message);
     60   }
     61 }
     62 
     63 views::View* TrayLocallyManagedUser::CreateDefaultView(
     64     user::LoginStatus status) {
     65   CHECK(tray_view_ == NULL);
     66   if (status != ash::user::LOGGED_IN_LOCALLY_MANAGED)
     67     return NULL;
     68 
     69   tray_view_ = new LabelTrayView(this, IDR_AURA_UBER_TRAY_MANAGED_USER);
     70   UpdateMessage();
     71   return tray_view_;
     72 }
     73 
     74 void TrayLocallyManagedUser::DestroyDefaultView() {
     75   tray_view_ = NULL;
     76 }
     77 
     78 void TrayLocallyManagedUser::OnViewClicked(views::View* sender) {
     79   Shell::GetInstance()->system_tray_delegate()->ShowLocallyManagedUserInfo();
     80 }
     81 
     82 void TrayLocallyManagedUser::UpdateAfterLoginStatusChange(
     83     user::LoginStatus status) {
     84   if (status == status_)
     85     return;
     86   if (status == ash::user::LOGGED_IN_LOCALLY_MANAGED &&
     87       status_ != ash::user::LOGGED_IN_LOCKED) {
     88     SystemTrayDelegate* delegate = Shell::GetInstance()->system_tray_delegate();
     89     CreateOrUpdateNotification(delegate->GetLocallyManagedUserMessage());
     90   }
     91   status_ = status;
     92 }
     93 
     94 } // namespace internal
     95 } // namespace ash
     96