tracing_notification_controller.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2018 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. #include "ash/system/tracing_notification_controller.h"
  5. #include "ash/constants/notifier_catalogs.h"
  6. #include "ash/public/cpp/notification_utils.h"
  7. #include "ash/public/cpp/system_tray_client.h"
  8. #include "ash/resources/vector_icons/vector_icons.h"
  9. #include "ash/shell.h"
  10. #include "ash/strings/grit/ash_strings.h"
  11. #include "ash/system/model/system_tray_model.h"
  12. #include "base/bind.h"
  13. #include "base/metrics/user_metrics.h"
  14. #include "ui/base/l10n/l10n_util.h"
  15. #include "ui/message_center/message_center.h"
  16. #include "ui/message_center/public/cpp/notification.h"
  17. using message_center::MessageCenter;
  18. using message_center::Notification;
  19. namespace ash {
  20. namespace {
  21. const char kNotifierId[] = "ash.tracing";
  22. void HandleNotificationClick() {
  23. base::RecordAction(
  24. base::UserMetricsAction("StatusArea_Tracing_Default_Selected"));
  25. Shell::Get()->system_tray_model()->client()->ShowChromeSlow();
  26. }
  27. } // namespace
  28. // static
  29. const char TracingNotificationController::kNotificationId[] = "chrome://slow";
  30. TracingNotificationController::TracingNotificationController()
  31. : model_(Shell::Get()->system_tray_model()->tracing()) {
  32. model_->AddObserver(this);
  33. OnTracingModeChanged();
  34. }
  35. TracingNotificationController::~TracingNotificationController() {
  36. model_->RemoveObserver(this);
  37. }
  38. void TracingNotificationController::OnTracingModeChanged() {
  39. // Return if the state doesn't change.
  40. if (was_tracing_ == model_->is_tracing())
  41. return;
  42. if (model_->is_tracing())
  43. CreateNotification();
  44. else
  45. RemoveNotification();
  46. was_tracing_ = model_->is_tracing();
  47. }
  48. void TracingNotificationController::CreateNotification() {
  49. std::unique_ptr<Notification> notification = CreateSystemNotification(
  50. message_center::NOTIFICATION_TYPE_SIMPLE, kNotificationId,
  51. l10n_util::GetStringUTF16(IDS_ASH_STATUS_TRAY_TRACING_NOTIFICATION_TITLE),
  52. l10n_util::GetStringUTF16(
  53. IDS_ASH_STATUS_TRAY_TRACING_NOTIFICATION_MESSAGE),
  54. std::u16string() /* display_source */, GURL(),
  55. message_center::NotifierId(message_center::NotifierType::SYSTEM_COMPONENT,
  56. kNotifierId,
  57. NotificationCatalogName::kTracing),
  58. message_center::RichNotificationData(),
  59. base::MakeRefCounted<message_center::HandleNotificationClickDelegate>(
  60. base::BindRepeating(&HandleNotificationClick)),
  61. kSystemMenuTracingIcon,
  62. message_center::SystemNotificationWarningLevel::NORMAL);
  63. notification->set_pinned(true);
  64. MessageCenter::Get()->AddNotification(std::move(notification));
  65. }
  66. void TracingNotificationController::RemoveNotification() {
  67. message_center::MessageCenter::Get()->RemoveNotification(kNotificationId,
  68. false /* by_user */);
  69. }
  70. } // namespace ash