notification_manager.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2020 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/components/phonehub/notification_manager.h"
  5. #include <sstream>
  6. #include "ash/components/multidevice/logging/logging.h"
  7. namespace ash {
  8. namespace phonehub {
  9. namespace {
  10. std::string GetIdStream(const base::flat_set<int64_t>& notification_ids) {
  11. std::ostringstream output(std::ostringstream::ate);
  12. for (const auto& id : notification_ids) {
  13. output << id << " ";
  14. }
  15. return output.str();
  16. }
  17. } // namespace
  18. NotificationManager::NotificationManager() = default;
  19. NotificationManager::~NotificationManager() = default;
  20. void NotificationManager::AddObserver(Observer* observer) {
  21. observer_list_.AddObserver(observer);
  22. }
  23. void NotificationManager::RemoveObserver(Observer* observer) {
  24. observer_list_.RemoveObserver(observer);
  25. }
  26. void NotificationManager::NotifyNotificationsAdded(
  27. const base::flat_set<int64_t>& notification_ids) {
  28. PA_LOG(INFO) << "Added the following notification ids: "
  29. << GetIdStream(notification_ids);
  30. for (auto& observer : observer_list_)
  31. observer.OnNotificationsAdded(notification_ids);
  32. }
  33. void NotificationManager::NotifyNotificationsUpdated(
  34. const base::flat_set<int64_t>& notification_ids) {
  35. PA_LOG(INFO) << "Updated the following notification id: "
  36. << GetIdStream(notification_ids);
  37. for (auto& observer : observer_list_)
  38. observer.OnNotificationsUpdated(notification_ids);
  39. }
  40. void NotificationManager::NotifyNotificationsRemoved(
  41. const base::flat_set<int64_t>& notification_ids) {
  42. PA_LOG(INFO) << "Removed the following notification id: "
  43. << GetIdStream(notification_ids);
  44. for (auto& observer : observer_list_)
  45. observer.OnNotificationsRemoved(notification_ids);
  46. }
  47. void NotificationManager::SetNotificationsInternal(
  48. const base::flat_set<Notification>& notifications) {
  49. base::flat_set<int64_t> added_ids;
  50. base::flat_set<int64_t> updated_ids;
  51. for (const Notification& notification : notifications) {
  52. int64_t id = notification.id();
  53. auto it = id_to_notification_map_.find(id);
  54. if (it == id_to_notification_map_.end()) {
  55. id_to_notification_map_.emplace(id, notification);
  56. added_ids.emplace(id);
  57. continue;
  58. }
  59. it->second = notification;
  60. updated_ids.emplace(id);
  61. }
  62. if (!added_ids.empty())
  63. NotifyNotificationsAdded(added_ids);
  64. if (!updated_ids.empty())
  65. NotifyNotificationsUpdated(updated_ids);
  66. }
  67. void NotificationManager::RemoveNotificationsInternal(
  68. const base::flat_set<int64_t>& notification_ids) {
  69. if (notification_ids.empty())
  70. return;
  71. for (int64_t id : notification_ids) {
  72. auto it = id_to_notification_map_.find(id);
  73. if (it == id_to_notification_map_.end())
  74. continue;
  75. id_to_notification_map_.erase(it);
  76. }
  77. NotifyNotificationsRemoved(notification_ids);
  78. }
  79. void NotificationManager::ClearNotificationsInternal() {
  80. base::flat_set<int64_t> removed_ids;
  81. for (const auto& pair : id_to_notification_map_) {
  82. removed_ids.emplace(pair.first);
  83. }
  84. if (!removed_ids.empty()) {
  85. id_to_notification_map_.clear();
  86. NotifyNotificationsRemoved(removed_ids);
  87. }
  88. }
  89. const Notification* NotificationManager::GetNotification(
  90. int64_t notification_id) const {
  91. auto it = id_to_notification_map_.find(notification_id);
  92. if (it == id_to_notification_map_.end())
  93. return nullptr;
  94. return &it->second;
  95. }
  96. } // namespace phonehub
  97. } // namespace ash