forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmock_platform_notification_service.cc
176 lines (138 loc) · 6.34 KB
/
mock_platform_notification_service.cc
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/test/mock_platform_notification_service.h"
#include <set>
#include <utility>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/guid.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_event_dispatcher.h"
#include "content/public/browser/platform_notification_context.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/persistent_notification_status.h"
#include "third_party/blink/public/common/notifications/platform_notification_data.h"
namespace content {
MockPlatformNotificationService::MockPlatformNotificationService(
BrowserContext* context)
: context_(context) {}
MockPlatformNotificationService::~MockPlatformNotificationService() = default;
void MockPlatformNotificationService::DisplayNotification(
const std::string& notification_id,
const GURL& origin,
const GURL& document_url,
const blink::PlatformNotificationData& notification_data,
const blink::NotificationResources& notification_resources) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
ReplaceNotificationIfNeeded(notification_id);
non_persistent_notifications_.insert(notification_id);
NotificationEventDispatcher::GetInstance()->DispatchNonPersistentShowEvent(
notification_id);
notification_id_map_[base::UTF16ToUTF8(notification_data.title)] =
notification_id;
}
void MockPlatformNotificationService::DisplayPersistentNotification(
const std::string& notification_id,
const GURL& service_worker_scope,
const GURL& origin,
const blink::PlatformNotificationData& notification_data,
const blink::NotificationResources& notification_resources) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
ReplaceNotificationIfNeeded(notification_id);
persistent_notifications_[notification_id] = origin;
notification_id_map_[base::UTF16ToUTF8(notification_data.title)] =
notification_id;
}
void MockPlatformNotificationService::CloseNotification(
const std::string& notification_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
const auto non_persistent_iter =
non_persistent_notifications_.find(notification_id);
if (non_persistent_iter == non_persistent_notifications_.end())
return;
non_persistent_notifications_.erase(non_persistent_iter);
}
void MockPlatformNotificationService::ClosePersistentNotification(
const std::string& notification_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
persistent_notifications_.erase(notification_id);
}
void MockPlatformNotificationService::GetDisplayedNotifications(
DisplayedNotificationsCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
std::set<std::string> displayed_notifications;
for (const auto& kv : persistent_notifications_)
displayed_notifications.insert(kv.first);
for (const auto& notification_id : non_persistent_notifications_)
displayed_notifications.insert(notification_id);
GetUIThreadTaskRunner({base::TaskPriority::USER_VISIBLE})
->PostTask(FROM_HERE,
base::BindOnce(std::move(callback),
std::move(displayed_notifications),
true /* supports_synchronization */));
}
void MockPlatformNotificationService::ScheduleTrigger(base::Time timestamp) {
if (timestamp > base::Time::Now())
return;
context_->ForEachStoragePartition(
base::BindRepeating([](content::StoragePartition* partition) {
partition->GetPlatformNotificationContext()->TriggerNotifications();
}));
}
base::Time MockPlatformNotificationService::ReadNextTriggerTimestamp() {
return base::Time::Max();
}
int64_t MockPlatformNotificationService::ReadNextPersistentNotificationId() {
return ++next_persistent_notification_id_;
}
void MockPlatformNotificationService::RecordNotificationUkmEvent(
const NotificationDatabaseData& data) {}
void MockPlatformNotificationService::SimulateClick(
const std::string& title,
const absl::optional<int>& action_index,
const absl::optional<std::u16string>& reply) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
const auto notification_id_iter = notification_id_map_.find(title);
if (notification_id_iter == notification_id_map_.end())
return;
const std::string& notification_id = notification_id_iter->second;
const auto persistent_iter = persistent_notifications_.find(notification_id);
const auto non_persistent_iter =
non_persistent_notifications_.find(notification_id);
if (persistent_iter != persistent_notifications_.end()) {
DCHECK(non_persistent_iter == non_persistent_notifications_.end());
NotificationEventDispatcher::GetInstance()->DispatchNotificationClickEvent(
context_, notification_id, persistent_iter->second, action_index, reply,
base::DoNothing());
} else if (non_persistent_iter != non_persistent_notifications_.end()) {
DCHECK(!action_index.has_value())
<< "Action buttons are only supported for "
"persistent notifications";
NotificationEventDispatcher::GetInstance()->DispatchNonPersistentClickEvent(
notification_id, base::DoNothing() /* callback */);
}
}
void MockPlatformNotificationService::SimulateClose(const std::string& title,
bool by_user) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
const auto notification_id_iter = notification_id_map_.find(title);
if (notification_id_iter == notification_id_map_.end())
return;
const std::string& notification_id = notification_id_iter->second;
const auto& persistent_iter = persistent_notifications_.find(notification_id);
if (persistent_iter == persistent_notifications_.end())
return;
NotificationEventDispatcher::GetInstance()->DispatchNotificationCloseEvent(
context_, notification_id, persistent_iter->second, by_user,
base::DoNothing());
}
void MockPlatformNotificationService::ReplaceNotificationIfNeeded(
const std::string& notification_id) {
persistent_notifications_.erase(notification_id);
non_persistent_notifications_.erase(notification_id);
}
} // namespace content