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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
|
// 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/child/notifications/notification_manager.h"
#include <utility>
#include "base/lazy_instance.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_local.h"
#include "content/child/notifications/notification_data_conversions.h"
#include "content/child/notifications/notification_dispatcher.h"
#include "content/child/service_worker/web_service_worker_registration_impl.h"
#include "content/child/thread_safe_sender.h"
#include "content/public/common/notification_resources.h"
#include "content/public/common/platform_notification_data.h"
#include "third_party/WebKit/public/platform/URLConversion.h"
#include "third_party/WebKit/public/platform/WebSecurityOrigin.h"
#include "third_party/WebKit/public/platform/modules/notifications/WebNotificationDelegate.h"
#include "url/origin.h"
using blink::WebString;
namespace content {
namespace {
int CurrentWorkerId() {
return WorkerThread::GetCurrentId();
}
NotificationResources ToNotificationResources(
std::unique_ptr<blink::WebNotificationResources> web_resources) {
NotificationResources resources;
resources.image = web_resources->image;
resources.notification_icon = web_resources->icon;
resources.badge = web_resources->badge;
for (const auto& action_icon : web_resources->actionIcons)
resources.action_icons.push_back(action_icon);
return resources;
}
} // namespace
static base::LazyInstance<base::ThreadLocalPointer<NotificationManager>>::Leaky
g_notification_manager_tls = LAZY_INSTANCE_INITIALIZER;
NotificationManager::ActiveNotificationData::ActiveNotificationData(
blink::WebNotificationDelegate* delegate,
const GURL& origin,
const std::string& tag)
: delegate(delegate), origin(origin), tag(tag) {}
NotificationManager::ActiveNotificationData::~ActiveNotificationData() {}
NotificationManager::NotificationManager(
ThreadSafeSender* thread_safe_sender,
NotificationDispatcher* notification_dispatcher)
: thread_safe_sender_(thread_safe_sender),
notification_dispatcher_(notification_dispatcher) {
g_notification_manager_tls.Pointer()->Set(this);
}
NotificationManager::~NotificationManager() {
g_notification_manager_tls.Pointer()->Set(nullptr);
}
NotificationManager* NotificationManager::ThreadSpecificInstance(
ThreadSafeSender* thread_safe_sender,
NotificationDispatcher* notification_dispatcher) {
if (g_notification_manager_tls.Pointer()->Get())
return g_notification_manager_tls.Pointer()->Get();
NotificationManager* manager =
new NotificationManager(thread_safe_sender, notification_dispatcher);
if (CurrentWorkerId())
WorkerThread::AddObserver(manager);
return manager;
}
void NotificationManager::WillStopCurrentWorkerThread() {
delete this;
}
void NotificationManager::show(
const blink::WebSecurityOrigin& origin,
const blink::WebNotificationData& notification_data,
std::unique_ptr<blink::WebNotificationResources> notification_resources,
blink::WebNotificationDelegate* delegate) {
DCHECK_EQ(0u, notification_data.actions.size());
DCHECK_EQ(0u, notification_resources->actionIcons.size());
GURL origin_gurl = url::Origin(origin).GetURL();
int notification_id =
notification_dispatcher_->GenerateNotificationId(CurrentWorkerId());
active_page_notifications_[notification_id] = ActiveNotificationData(
delegate, origin_gurl,
notification_data.tag.utf8(
WebString::UTF8ConversionMode::kStrictReplacingErrorsWithFFFD));
// TODO(mkwst): This is potentially doing the wrong thing with unique
// origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See
// https://crbug.com/490074 for detail.
thread_safe_sender_->Send(new PlatformNotificationHostMsg_Show(
notification_id, origin_gurl,
ToPlatformNotificationData(notification_data),
ToNotificationResources(std::move(notification_resources))));
}
void NotificationManager::showPersistent(
const blink::WebSecurityOrigin& origin,
const blink::WebNotificationData& notification_data,
std::unique_ptr<blink::WebNotificationResources> notification_resources,
blink::WebServiceWorkerRegistration* service_worker_registration,
std::unique_ptr<blink::WebNotificationShowCallbacks> callbacks) {
DCHECK(service_worker_registration);
DCHECK_EQ(notification_data.actions.size(),
notification_resources->actionIcons.size());
int64_t service_worker_registration_id =
static_cast<WebServiceWorkerRegistrationImpl*>(
service_worker_registration)
->registrationId();
// Verify that the author-provided payload size does not exceed our limit.
// This is an implementation-defined limit to prevent abuse of notification
// data as a storage mechanism. A UMA histogram records the requested sizes,
// which enables us to track how much data authors are attempting to store.
//
// If the size exceeds this limit, reject the showNotification() promise. This
// is outside of the boundaries set by the specification, but it gives authors
// an indication that something has gone wrong.
size_t author_data_size = notification_data.data.size();
UMA_HISTOGRAM_COUNTS_1000("Notifications.AuthorDataSize", author_data_size);
if (author_data_size > PlatformNotificationData::kMaximumDeveloperDataSize) {
callbacks->onError();
return;
}
// TODO(peter): GenerateNotificationId is more of a request id. Consider
// renaming the method in the NotificationDispatcher if this makes sense.
int request_id =
notification_dispatcher_->GenerateNotificationId(CurrentWorkerId());
pending_show_notification_requests_.AddWithID(std::move(callbacks),
request_id);
// TODO(mkwst): This is potentially doing the wrong thing with unique
// origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See
// https://crbug.com/490074 for detail.
thread_safe_sender_->Send(new PlatformNotificationHostMsg_ShowPersistent(
request_id, service_worker_registration_id, url::Origin(origin).GetURL(),
ToPlatformNotificationData(notification_data),
ToNotificationResources(std::move(notification_resources))));
}
void NotificationManager::getNotifications(
const blink::WebString& filter_tag,
blink::WebServiceWorkerRegistration* service_worker_registration,
std::unique_ptr<blink::WebNotificationGetCallbacks> callbacks) {
DCHECK(service_worker_registration);
DCHECK(callbacks);
WebServiceWorkerRegistrationImpl* service_worker_registration_impl =
static_cast<WebServiceWorkerRegistrationImpl*>(
service_worker_registration);
GURL origin = GURL(service_worker_registration_impl->scope()).GetOrigin();
int64_t service_worker_registration_id =
service_worker_registration_impl->registrationId();
// TODO(peter): GenerateNotificationId is more of a request id. Consider
// renaming the method in the NotificationDispatcher if this makes sense.
int request_id =
notification_dispatcher_->GenerateNotificationId(CurrentWorkerId());
pending_get_notification_requests_.AddWithID(std::move(callbacks),
request_id);
thread_safe_sender_->Send(new PlatformNotificationHostMsg_GetNotifications(
request_id, service_worker_registration_id, origin,
filter_tag.utf8(
WebString::UTF8ConversionMode::kStrictReplacingErrorsWithFFFD)));
}
void NotificationManager::close(blink::WebNotificationDelegate* delegate) {
for (auto& iter : active_page_notifications_) {
if (iter.second.delegate != delegate)
continue;
thread_safe_sender_->Send(new PlatformNotificationHostMsg_Close(
iter.second.origin, iter.second.tag, iter.first));
active_page_notifications_.erase(iter.first);
return;
}
// It should not be possible for Blink to call close() on a Notification which
// does not exist in either the pending or active notification lists.
NOTREACHED();
}
void NotificationManager::closePersistent(
const blink::WebSecurityOrigin& origin,
const blink::WebString& tag,
const blink::WebString& notification_id) {
thread_safe_sender_->Send(new PlatformNotificationHostMsg_ClosePersistent(
// TODO(mkwst): This is potentially doing the wrong thing with unique
// origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See
// https://crbug.com/490074 for detail.
url::Origin(origin).GetURL(),
tag.utf8(WebString::UTF8ConversionMode::kStrictReplacingErrorsWithFFFD),
notification_id.utf8(
WebString::UTF8ConversionMode::kStrictReplacingErrorsWithFFFD)));
}
void NotificationManager::notifyDelegateDestroyed(
blink::WebNotificationDelegate* delegate) {
for (auto& iter : active_page_notifications_) {
if (iter.second.delegate != delegate)
continue;
active_page_notifications_.erase(iter.first);
return;
}
}
bool NotificationManager::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(NotificationManager, message)
IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShow, OnDidShow);
IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShowPersistent,
OnDidShowPersistent)
IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClose, OnDidClose);
IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClick, OnDidClick);
IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidGetNotifications,
OnDidGetNotifications)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
return handled;
}
void NotificationManager::OnDidShow(int notification_id) {
const auto& iter = active_page_notifications_.find(notification_id);
if (iter == active_page_notifications_.end())
return;
iter->second.delegate->dispatchShowEvent();
}
void NotificationManager::OnDidShowPersistent(int request_id, bool success) {
blink::WebNotificationShowCallbacks* callbacks =
pending_show_notification_requests_.Lookup(request_id);
DCHECK(callbacks);
if (!callbacks)
return;
if (success)
callbacks->onSuccess();
else
callbacks->onError();
pending_show_notification_requests_.Remove(request_id);
}
void NotificationManager::OnDidClose(int notification_id) {
const auto& iter = active_page_notifications_.find(notification_id);
if (iter == active_page_notifications_.end())
return;
iter->second.delegate->dispatchCloseEvent();
active_page_notifications_.erase(iter);
}
void NotificationManager::OnDidClick(int notification_id) {
const auto& iter = active_page_notifications_.find(notification_id);
if (iter == active_page_notifications_.end())
return;
iter->second.delegate->dispatchClickEvent();
}
void NotificationManager::OnDidGetNotifications(
int request_id,
const std::vector<PersistentNotificationInfo>& notification_infos) {
blink::WebNotificationGetCallbacks* callbacks =
pending_get_notification_requests_.Lookup(request_id);
DCHECK(callbacks);
if (!callbacks)
return;
blink::WebVector<blink::WebPersistentNotificationInfo> notifications(
notification_infos.size());
for (size_t i = 0; i < notification_infos.size(); ++i) {
blink::WebPersistentNotificationInfo web_notification_info;
web_notification_info.notificationId =
blink::WebString::fromUTF8(notification_infos[i].first);
web_notification_info.data =
ToWebNotificationData(notification_infos[i].second);
notifications[i] = web_notification_info;
}
callbacks->onSuccess(notifications);
pending_get_notification_requests_.Remove(request_id);
}
} // namespace content
|