assistant_notification_model.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/assistant/model/assistant_notification_model.h"
  5. #include <utility>
  6. #include "ash/assistant/model/assistant_notification_model_observer.h"
  7. #include "base/containers/contains.h"
  8. #include "chromeos/services/libassistant/public/cpp/assistant_notification.h"
  9. namespace ash {
  10. AssistantNotificationModel::AssistantNotificationModel() = default;
  11. AssistantNotificationModel::~AssistantNotificationModel() = default;
  12. void AssistantNotificationModel::AddObserver(
  13. AssistantNotificationModelObserver* observer) const {
  14. observers_.AddObserver(observer);
  15. }
  16. void AssistantNotificationModel::RemoveObserver(
  17. AssistantNotificationModelObserver* observer) const {
  18. observers_.RemoveObserver(observer);
  19. }
  20. void AssistantNotificationModel::AddOrUpdateNotification(
  21. AssistantNotification&& notification) {
  22. const auto client_id = notification.client_id;
  23. DCHECK(!client_id.empty());
  24. bool is_update = HasNotificationForId(client_id);
  25. notifications_[client_id] = std::move(notification);
  26. if (is_update)
  27. NotifyNotificationUpdated(notifications_[client_id]);
  28. else
  29. NotifyNotificationAdded(notifications_[client_id]);
  30. }
  31. void AssistantNotificationModel::RemoveNotificationById(const std::string& id,
  32. bool from_server) {
  33. auto it = notifications_.find(id);
  34. if (it == notifications_.end())
  35. return;
  36. AssistantNotification notification = std::move(it->second);
  37. notifications_.erase(id);
  38. NotifyNotificationRemoved(notification, from_server);
  39. }
  40. void AssistantNotificationModel::RemoveNotificationsByGroupingKey(
  41. const std::string& grouping_key,
  42. bool from_server) {
  43. for (auto it = notifications_.begin(); it != notifications_.end();) {
  44. if (it->second.grouping_key == grouping_key) {
  45. AssistantNotification notification =
  46. std::move(notifications_[it->second.client_id]);
  47. it = notifications_.erase(it);
  48. NotifyNotificationRemoved(notification, from_server);
  49. continue;
  50. }
  51. ++it;
  52. }
  53. }
  54. void AssistantNotificationModel::RemoveAllNotifications(bool from_server) {
  55. if (notifications_.empty())
  56. return;
  57. notifications_.clear();
  58. NotifyAllNotificationsRemoved(from_server);
  59. }
  60. const chromeos::assistant::AssistantNotification*
  61. AssistantNotificationModel::GetNotificationById(const std::string& id) const {
  62. auto it = notifications_.find(id);
  63. return it != notifications_.end() ? &it->second : nullptr;
  64. }
  65. std::vector<const chromeos::assistant::AssistantNotification*>
  66. AssistantNotificationModel::GetNotifications() const {
  67. std::vector<const AssistantNotification*> notifications;
  68. for (const auto& notification : notifications_)
  69. notifications.push_back(&notification.second);
  70. return notifications;
  71. }
  72. bool AssistantNotificationModel::HasNotificationForId(
  73. const std::string& id) const {
  74. return base::Contains(notifications_, id);
  75. }
  76. void AssistantNotificationModel::NotifyNotificationAdded(
  77. const AssistantNotification& notification) {
  78. for (auto& observer : observers_)
  79. observer.OnNotificationAdded(notification);
  80. }
  81. void AssistantNotificationModel::NotifyNotificationUpdated(
  82. const AssistantNotification& notification) {
  83. for (auto& observer : observers_)
  84. observer.OnNotificationUpdated(notification);
  85. }
  86. void AssistantNotificationModel::NotifyNotificationRemoved(
  87. const AssistantNotification& notification,
  88. bool from_server) {
  89. for (auto& observer : observers_)
  90. observer.OnNotificationRemoved(notification, from_server);
  91. }
  92. void AssistantNotificationModel::NotifyAllNotificationsRemoved(
  93. bool from_server) {
  94. for (auto& observer : observers_)
  95. observer.OnAllNotificationsRemoved(from_server);
  96. }
  97. } // namespace ash