notification.cc 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "components/exo/notification.h"
  5. #include <memory>
  6. #include "ash/session/session_controller_impl.h"
  7. #include "ash/shell.h"
  8. #include "base/strings/utf_string_conversions.h"
  9. #include "ui/message_center/message_center.h"
  10. #include "ui/message_center/public/cpp/notification.h"
  11. #include "ui/message_center/public/cpp/notification_delegate.h"
  12. namespace exo {
  13. namespace {
  14. // Ref-counted delegate for handling events on notification with callbacks.
  15. class NotificationDelegate : public message_center::NotificationDelegate {
  16. public:
  17. NotificationDelegate(
  18. const base::RepeatingCallback<void(bool)>& close_callback,
  19. const base::RepeatingCallback<void(const absl::optional<int>&)>&
  20. click_callback)
  21. : close_callback_(close_callback), click_callback_(click_callback) {}
  22. NotificationDelegate(const NotificationDelegate&) = delete;
  23. NotificationDelegate& operator=(const NotificationDelegate&) = delete;
  24. // message_center::NotificationDelegate:
  25. void Close(bool by_user) override {
  26. if (!close_callback_)
  27. return;
  28. close_callback_.Run(by_user);
  29. }
  30. void Click(const absl::optional<int>& button_index,
  31. const absl::optional<std::u16string>& reply) override {
  32. if (!click_callback_)
  33. return;
  34. click_callback_.Run(button_index);
  35. }
  36. private:
  37. // The destructor is private since this class is ref-counted.
  38. ~NotificationDelegate() override = default;
  39. const base::RepeatingCallback<void(bool)> close_callback_;
  40. const base::RepeatingCallback<void(const absl::optional<int>&)>
  41. click_callback_;
  42. };
  43. } // namespace
  44. Notification::Notification(
  45. const std::string& title,
  46. const std::string& message,
  47. const std::string& display_source,
  48. const std::string& notification_id,
  49. const std::string& notifier_id,
  50. const std::vector<std::string>& buttons,
  51. const base::RepeatingCallback<void(bool)>& close_callback,
  52. const base::RepeatingCallback<void(const absl::optional<int>&)>&
  53. click_callback)
  54. : notification_id_(notification_id) {
  55. // Currently, exo::Notification is used only for Crostini notifications.
  56. // TODO(toshikikikuchi): When this class is used for other reasons,
  57. // re-consider the way to set the notifier type.
  58. auto notifier = message_center::NotifierId(
  59. message_center::NotifierType::CROSTINI_APPLICATION, notifier_id);
  60. notifier.profile_id = ash::Shell::Get()
  61. ->session_controller()
  62. ->GetPrimaryUserSession()
  63. ->user_info.account_id.GetUserEmail();
  64. message_center::RichNotificationData data;
  65. for (const auto& button : buttons)
  66. data.buttons.emplace_back(base::UTF8ToUTF16(button));
  67. auto notification = std::make_unique<message_center::Notification>(
  68. message_center::NOTIFICATION_TYPE_SIMPLE, notification_id,
  69. base::UTF8ToUTF16(title), base::UTF8ToUTF16(message), ui::ImageModel(),
  70. base::UTF8ToUTF16(display_source), GURL(), notifier, data,
  71. base::MakeRefCounted<NotificationDelegate>(close_callback,
  72. click_callback));
  73. message_center::MessageCenter::Get()->AddNotification(
  74. std::move(notification));
  75. }
  76. void Notification::Close() {
  77. message_center::MessageCenter::Get()->RemoveNotification(notification_id_,
  78. false /* by_user */);
  79. }
  80. } // namespace exo