1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
ash / assistant / model / assistant_notification_model.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/assistant/model/assistant_notification_model.h"
#include <utility>
#include "ash/assistant/model/assistant_notification_model_observer.h"
#include "base/containers/contains.h"
#include "chromeos/ash/services/libassistant/public/cpp/assistant_notification.h"
namespace ash {
AssistantNotificationModel::AssistantNotificationModel() = default;
AssistantNotificationModel::~AssistantNotificationModel() = default;
void AssistantNotificationModel::AddObserver(
AssistantNotificationModelObserver* observer) const {
observers_.AddObserver(observer);
}
void AssistantNotificationModel::RemoveObserver(
AssistantNotificationModelObserver* observer) const {
observers_.RemoveObserver(observer);
}
void AssistantNotificationModel::AddOrUpdateNotification(
AssistantNotification&& notification) {
const auto client_id = notification.client_id;
DCHECK(!client_id.empty());
bool is_update = HasNotificationForId(client_id);
notifications_[client_id] = std::move(notification);
if (is_update)
NotifyNotificationUpdated(notifications_[client_id]);
else
NotifyNotificationAdded(notifications_[client_id]);
}
void AssistantNotificationModel::RemoveNotificationById(const std::string& id,
bool from_server) {
auto it = notifications_.find(id);
if (it == notifications_.end())
return;
AssistantNotification notification = std::move(it->second);
notifications_.erase(id);
NotifyNotificationRemoved(notification, from_server);
}
void AssistantNotificationModel::RemoveNotificationsByGroupingKey(
const std::string& grouping_key,
bool from_server) {
for (auto it = notifications_.begin(); it != notifications_.end();) {
if (it->second.grouping_key == grouping_key) {
AssistantNotification notification =
std::move(notifications_[it->second.client_id]);
it = notifications_.erase(it);
NotifyNotificationRemoved(notification, from_server);
continue;
}
++it;
}
}
void AssistantNotificationModel::RemoveAllNotifications(bool from_server) {
if (notifications_.empty())
return;
notifications_.clear();
NotifyAllNotificationsRemoved(from_server);
}
const assistant::AssistantNotification*
AssistantNotificationModel::GetNotificationById(const std::string& id) const {
auto it = notifications_.find(id);
return it != notifications_.end() ? &it->second : nullptr;
}
std::vector<const assistant::AssistantNotification*>
AssistantNotificationModel::GetNotifications() const {
std::vector<const AssistantNotification*> notifications;
for (const auto& notification : notifications_)
notifications.push_back(¬ification.second);
return notifications;
}
bool AssistantNotificationModel::HasNotificationForId(
const std::string& id) const {
return base::Contains(notifications_, id);
}
void AssistantNotificationModel::NotifyNotificationAdded(
const AssistantNotification& notification) {
for (auto& observer : observers_)
observer.OnNotificationAdded(notification);
}
void AssistantNotificationModel::NotifyNotificationUpdated(
const AssistantNotification& notification) {
for (auto& observer : observers_)
observer.OnNotificationUpdated(notification);
}
void AssistantNotificationModel::NotifyNotificationRemoved(
const AssistantNotification& notification,
bool from_server) {
for (auto& observer : observers_)
observer.OnNotificationRemoved(notification, from_server);
}
void AssistantNotificationModel::NotifyAllNotificationsRemoved(
bool from_server) {
for (auto& observer : observers_)
observer.OnAllNotificationsRemoved(from_server);
}
} // namespace ash