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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
// 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/thread_task_runner_handle.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/common/notification_constants.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"
using blink::WebNotificationPermission;
namespace content {
namespace {
int CurrentWorkerId() {
return WorkerThread::GetCurrentId();
}
// Checks whether |notification_data| specifies any non-empty resources that
// need to be fetched.
bool HasResourcesToFetch(const blink::WebNotificationData& notification_data) {
if (!notification_data.icon.isEmpty())
return true;
if (!notification_data.badge.isEmpty())
return true;
for (const auto& action : notification_data.actions) {
if (!action.icon.isEmpty())
return true;
}
return false;
}
} // namespace
static base::LazyInstance<base::ThreadLocalPointer<NotificationManager>>::Leaky
g_notification_manager_tls = LAZY_INSTANCE_INITIALIZER;
NotificationManager::NotificationManager(
ThreadSafeSender* thread_safe_sender,
base::SingleThreadTaskRunner* main_thread_task_runner,
NotificationDispatcher* notification_dispatcher)
: thread_safe_sender_(thread_safe_sender),
notification_dispatcher_(notification_dispatcher),
notifications_tracker_(main_thread_task_runner) {
g_notification_manager_tls.Pointer()->Set(this);
}
NotificationManager::~NotificationManager() {
g_notification_manager_tls.Pointer()->Set(nullptr);
}
NotificationManager* NotificationManager::ThreadSpecificInstance(
ThreadSafeSender* thread_safe_sender,
base::SingleThreadTaskRunner* main_thread_task_runner,
NotificationDispatcher* notification_dispatcher) {
if (g_notification_manager_tls.Pointer()->Get())
return g_notification_manager_tls.Pointer()->Get();
NotificationManager* manager = new NotificationManager(
thread_safe_sender, main_thread_task_runner, 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,
blink::WebNotificationDelegate* delegate) {
DCHECK_EQ(0u, notification_data.actions.size());
if (!HasResourcesToFetch(notification_data)) {
DisplayPageNotification(origin, notification_data, delegate,
NotificationResources());
return;
}
notifications_tracker_.FetchResources(
notification_data, delegate,
base::Bind(&NotificationManager::DisplayPageNotification,
base::Unretained(this), // this owns |notifications_tracker_|
origin, notification_data, delegate));
}
void NotificationManager::showPersistent(
const blink::WebSecurityOrigin& origin,
const blink::WebNotificationData& notification_data,
blink::WebServiceWorkerRegistration* service_worker_registration,
blink::WebNotificationShowCallbacks* callbacks) {
DCHECK(service_worker_registration);
int64_t service_worker_registration_id =
static_cast<WebServiceWorkerRegistrationImpl*>(
service_worker_registration)
->registration_id();
std::unique_ptr<blink::WebNotificationShowCallbacks> owned_callbacks(
callbacks);
// 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) {
owned_callbacks->onError();
return;
}
if (!HasResourcesToFetch(notification_data)) {
NotificationResources notification_resources;
// Action indices are expected to have a corresponding icon bitmap, which
// may be empty when the developer provided no (or an invalid) icon.
if (!notification_data.actions.isEmpty()) {
notification_resources.action_icons.resize(
notification_data.actions.size());
}
DisplayPersistentNotification(
origin, notification_data, service_worker_registration_id,
std::move(owned_callbacks), notification_resources);
return;
}
notifications_tracker_.FetchResources(
notification_data, nullptr /* delegate */,
base::Bind(&NotificationManager::DisplayPersistentNotification,
base::Unretained(this), // this owns |notifications_tracker_|
origin, notification_data, service_worker_registration_id,
base::Passed(&owned_callbacks)));
}
void NotificationManager::getNotifications(
const blink::WebString& filter_tag,
blink::WebServiceWorkerRegistration* service_worker_registration,
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->registration_id();
// 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(callbacks, request_id);
thread_safe_sender_->Send(new PlatformNotificationHostMsg_GetNotifications(
request_id, service_worker_registration_id, origin,
base::UTF16ToUTF8(base::StringPiece16(filter_tag))));
}
void NotificationManager::close(blink::WebNotificationDelegate* delegate) {
if (notifications_tracker_.CancelResourceFetches(delegate))
return;
for (auto& iter : active_page_notifications_) {
if (iter.second != delegate)
continue;
thread_safe_sender_->Send(
new PlatformNotificationHostMsg_Close(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,
int64_t persistent_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.
blink::WebStringToGURL(origin.toString()), persistent_notification_id));
}
void NotificationManager::notifyDelegateDestroyed(
blink::WebNotificationDelegate* delegate) {
if (notifications_tracker_.CancelResourceFetches(delegate))
return;
for (auto& iter : active_page_notifications_) {
if (iter.second != delegate)
continue;
active_page_notifications_.erase(iter.first);
return;
}
}
WebNotificationPermission NotificationManager::checkPermission(
const blink::WebSecurityOrigin& origin) {
WebNotificationPermission permission =
blink::WebNotificationPermissionAllowed;
// 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_CheckPermission(
blink::WebStringToGURL(origin.toString()), &permission));
return permission;
}
size_t NotificationManager::maxActions() {
return kPlatformNotificationMaxActions;
}
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->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->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->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.persistentId = 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);
}
void NotificationManager::DisplayPageNotification(
const blink::WebSecurityOrigin& origin,
const blink::WebNotificationData& notification_data,
blink::WebNotificationDelegate* delegate,
const NotificationResources& notification_resources) {
DCHECK_EQ(0u, notification_data.actions.size());
DCHECK_EQ(0u, notification_resources.action_icons.size());
int notification_id =
notification_dispatcher_->GenerateNotificationId(CurrentWorkerId());
active_page_notifications_[notification_id] = delegate;
// 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, blink::WebStringToGURL(origin.toString()),
ToPlatformNotificationData(notification_data), notification_resources));
}
void NotificationManager::DisplayPersistentNotification(
const blink::WebSecurityOrigin& origin,
const blink::WebNotificationData& notification_data,
int64_t service_worker_registration_id,
std::unique_ptr<blink::WebNotificationShowCallbacks> callbacks,
const NotificationResources& notification_resources) {
DCHECK_EQ(notification_data.actions.size(),
notification_resources.action_icons.size());
// 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(callbacks.release(),
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,
blink::WebStringToGURL(origin.toString()),
ToPlatformNotificationData(notification_data), notification_resources));
}
} // namespace content
|