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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
ash / assistant / assistant_notification_expiry_monitor.cc [blame]
// Copyright 2019 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/assistant_notification_expiry_monitor.h"
#include <algorithm>
#include "ash/assistant/assistant_notification_controller_impl.h"
#include "ash/assistant/model/assistant_notification_model.h"
#include "ash/assistant/model/assistant_notification_model_observer.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "chromeos/ash/services/assistant/public/cpp/assistant_service.h"
namespace ash {
namespace {
bool HasExpired(const AssistantNotificationExpiryMonitor::AssistantNotification*
notification) {
return notification->expiry_time.has_value() &&
(notification->expiry_time.value() <= base::Time::Now());
}
// Returns the minimum of the base::Time instances that actually have a value.
std::optional<base::Time> Min(std::optional<base::Time> left,
std::optional<base::Time> right) {
if (!left.has_value())
return right;
if (!right.has_value())
return left;
return std::min(left.value(), right.value());
}
} // namespace
class AssistantNotificationExpiryMonitor::Observer
: public AssistantNotificationModelObserver {
public:
explicit Observer(AssistantNotificationExpiryMonitor* monitor)
: monitor_(monitor) {}
Observer(const Observer&) = delete;
Observer& operator=(const Observer&) = delete;
~Observer() override = default;
void OnNotificationAdded(const AssistantNotification& notification) override {
monitor_->UpdateTimer();
}
void OnNotificationUpdated(
const AssistantNotification& notification) override {
monitor_->UpdateTimer();
}
void OnNotificationRemoved(const AssistantNotification& notification,
bool from_server) override {
monitor_->UpdateTimer();
}
void OnAllNotificationsRemoved(bool from_server) override {
monitor_->UpdateTimer();
}
private:
const raw_ptr<AssistantNotificationExpiryMonitor> monitor_;
};
AssistantNotificationExpiryMonitor::AssistantNotificationExpiryMonitor(
AssistantNotificationControllerImpl* controller)
: controller_(controller), observer_(std::make_unique<Observer>(this)) {
DCHECK(controller_);
controller_->model()->AddObserver(observer_.get());
}
AssistantNotificationExpiryMonitor::~AssistantNotificationExpiryMonitor() =
default;
void AssistantNotificationExpiryMonitor::UpdateTimer() {
std::optional<base::TimeDelta> timeout = GetTimerTimeout();
if (timeout) {
timer_.Start(
FROM_HERE, timeout.value(),
base::BindOnce(
&AssistantNotificationExpiryMonitor::RemoveExpiredNotifications,
base::Unretained(this)));
} else {
timer_.Stop();
}
}
std::optional<base::TimeDelta>
AssistantNotificationExpiryMonitor::GetTimerTimeout() const {
std::optional<base::Time> endtime = GetTimerEndTime();
if (endtime)
return endtime.value() - base::Time::Now();
return std::nullopt;
}
std::optional<base::Time> AssistantNotificationExpiryMonitor::GetTimerEndTime()
const {
std::optional<base::Time> result = std::nullopt;
for (const AssistantNotification* notification : GetNotifications())
result = Min(result, notification->expiry_time);
return result;
}
void AssistantNotificationExpiryMonitor::RemoveExpiredNotifications() {
for (const NotificationId& id : GetExpiredNotifications()) {
VLOG(1) << "Removing expired notification '" << id << "'";
controller_->RemoveNotificationById(id, /*from_server=*/false);
}
UpdateTimer();
}
std::vector<AssistantNotificationExpiryMonitor::NotificationId>
AssistantNotificationExpiryMonitor::GetExpiredNotifications() const {
std::vector<NotificationId> result;
for (const AssistantNotification* notification : GetNotifications()) {
if (HasExpired(notification))
result.push_back(notification->client_id);
}
return result;
}
std::vector<const AssistantNotificationExpiryMonitor::AssistantNotification*>
AssistantNotificationExpiryMonitor::GetNotifications() const {
return controller_->model()->GetNotifications();
}
} // namespace ash