Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1209)

Side by Side Diff: content/child/notifications/notification_manager.cc

Issue 2300093002: Make //content responsible for generating notification Ids (Closed)
Patch Set: comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/child/notifications/notification_manager.h" 5 #include "content/child/notifications/notification_manager.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/lazy_instance.h" 9 #include "base/lazy_instance.h"
10 #include "base/metrics/histogram_macros.h" 10 #include "base/metrics/histogram_macros.h"
(...skipping 25 matching lines...) Expand all
36 for (const auto& action_icon : web_resources->actionIcons) 36 for (const auto& action_icon : web_resources->actionIcons)
37 resources.action_icons.push_back(action_icon); 37 resources.action_icons.push_back(action_icon);
38 return resources; 38 return resources;
39 } 39 }
40 40
41 } // namespace 41 } // namespace
42 42
43 static base::LazyInstance<base::ThreadLocalPointer<NotificationManager>>::Leaky 43 static base::LazyInstance<base::ThreadLocalPointer<NotificationManager>>::Leaky
44 g_notification_manager_tls = LAZY_INSTANCE_INITIALIZER; 44 g_notification_manager_tls = LAZY_INSTANCE_INITIALIZER;
45 45
46 NotificationManager::ActiveNotificationData::ActiveNotificationData(
47 blink::WebNotificationDelegate* delegate,
48 const GURL& origin,
49 const std::string& tag)
50 : delegate(delegate), origin(origin), tag(tag) {}
51
52 NotificationManager::ActiveNotificationData::~ActiveNotificationData() {}
53
46 NotificationManager::NotificationManager( 54 NotificationManager::NotificationManager(
47 ThreadSafeSender* thread_safe_sender, 55 ThreadSafeSender* thread_safe_sender,
48 NotificationDispatcher* notification_dispatcher) 56 NotificationDispatcher* notification_dispatcher)
49 : thread_safe_sender_(thread_safe_sender), 57 : thread_safe_sender_(thread_safe_sender),
50 notification_dispatcher_(notification_dispatcher) { 58 notification_dispatcher_(notification_dispatcher) {
51 g_notification_manager_tls.Pointer()->Set(this); 59 g_notification_manager_tls.Pointer()->Set(this);
52 } 60 }
53 61
54 NotificationManager::~NotificationManager() { 62 NotificationManager::~NotificationManager() {
55 g_notification_manager_tls.Pointer()->Set(nullptr); 63 g_notification_manager_tls.Pointer()->Set(nullptr);
(...skipping 17 matching lines...) Expand all
73 } 81 }
74 82
75 void NotificationManager::show( 83 void NotificationManager::show(
76 const blink::WebSecurityOrigin& origin, 84 const blink::WebSecurityOrigin& origin,
77 const blink::WebNotificationData& notification_data, 85 const blink::WebNotificationData& notification_data,
78 std::unique_ptr<blink::WebNotificationResources> notification_resources, 86 std::unique_ptr<blink::WebNotificationResources> notification_resources,
79 blink::WebNotificationDelegate* delegate) { 87 blink::WebNotificationDelegate* delegate) {
80 DCHECK_EQ(0u, notification_data.actions.size()); 88 DCHECK_EQ(0u, notification_data.actions.size());
81 DCHECK_EQ(0u, notification_resources->actionIcons.size()); 89 DCHECK_EQ(0u, notification_resources->actionIcons.size());
82 90
91 GURL origin_gurl = blink::WebStringToGURL(origin.toString());
92
83 int notification_id = 93 int notification_id =
84 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId()); 94 notification_dispatcher_->GenerateNotificationId(CurrentWorkerId());
85 95
86 active_page_notifications_[notification_id] = delegate; 96 active_page_notifications_[notification_id] = ActiveNotificationData(
97 delegate, origin_gurl,
98 base::UTF16ToUTF8(base::StringPiece16(notification_data.tag)));
99
87 // TODO(mkwst): This is potentially doing the wrong thing with unique 100 // TODO(mkwst): This is potentially doing the wrong thing with unique
88 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See 101 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See
89 // https://crbug.com/490074 for detail. 102 // https://crbug.com/490074 for detail.
90 thread_safe_sender_->Send(new PlatformNotificationHostMsg_Show( 103 thread_safe_sender_->Send(new PlatformNotificationHostMsg_Show(
91 notification_id, blink::WebStringToGURL(origin.toString()), 104 notification_id, origin_gurl,
92 ToPlatformNotificationData(notification_data), 105 ToPlatformNotificationData(notification_data),
93 ToNotificationResources(std::move(notification_resources)))); 106 ToNotificationResources(std::move(notification_resources))));
94 } 107 }
95 108
96 void NotificationManager::showPersistent( 109 void NotificationManager::showPersistent(
97 const blink::WebSecurityOrigin& origin, 110 const blink::WebSecurityOrigin& origin,
98 const blink::WebNotificationData& notification_data, 111 const blink::WebNotificationData& notification_data,
99 std::unique_ptr<blink::WebNotificationResources> notification_resources, 112 std::unique_ptr<blink::WebNotificationResources> notification_resources,
100 blink::WebServiceWorkerRegistration* service_worker_registration, 113 blink::WebServiceWorkerRegistration* service_worker_registration,
101 blink::WebNotificationShowCallbacks* callbacks) { 114 blink::WebNotificationShowCallbacks* callbacks) {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 181
169 pending_get_notification_requests_.AddWithID(callbacks, request_id); 182 pending_get_notification_requests_.AddWithID(callbacks, request_id);
170 183
171 thread_safe_sender_->Send(new PlatformNotificationHostMsg_GetNotifications( 184 thread_safe_sender_->Send(new PlatformNotificationHostMsg_GetNotifications(
172 request_id, service_worker_registration_id, origin, 185 request_id, service_worker_registration_id, origin,
173 base::UTF16ToUTF8(base::StringPiece16(filter_tag)))); 186 base::UTF16ToUTF8(base::StringPiece16(filter_tag))));
174 } 187 }
175 188
176 void NotificationManager::close(blink::WebNotificationDelegate* delegate) { 189 void NotificationManager::close(blink::WebNotificationDelegate* delegate) {
177 for (auto& iter : active_page_notifications_) { 190 for (auto& iter : active_page_notifications_) {
178 if (iter.second != delegate) 191 if (iter.second.delegate != delegate)
179 continue; 192 continue;
180 193
181 thread_safe_sender_->Send( 194 thread_safe_sender_->Send(new PlatformNotificationHostMsg_Close(
182 new PlatformNotificationHostMsg_Close(iter.first)); 195 iter.second.origin, iter.second.tag, iter.first));
183 active_page_notifications_.erase(iter.first); 196 active_page_notifications_.erase(iter.first);
184 return; 197 return;
185 } 198 }
186 199
187 // It should not be possible for Blink to call close() on a Notification which 200 // It should not be possible for Blink to call close() on a Notification which
188 // does not exist in either the pending or active notification lists. 201 // does not exist in either the pending or active notification lists.
189 NOTREACHED(); 202 NOTREACHED();
190 } 203 }
191 204
192 void NotificationManager::closePersistent( 205 void NotificationManager::closePersistent(
193 const blink::WebSecurityOrigin& origin, 206 const blink::WebSecurityOrigin& origin,
194 int64_t persistent_notification_id) { 207 const blink::WebString& tag,
208 const blink::WebString& notification_id) {
195 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ClosePersistent( 209 thread_safe_sender_->Send(new PlatformNotificationHostMsg_ClosePersistent(
196 // TODO(mkwst): This is potentially doing the wrong thing with unique 210 // TODO(mkwst): This is potentially doing the wrong thing with unique
197 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See 211 // origins. Perhaps also 'file:', 'blob:' and 'filesystem:'. See
198 // https://crbug.com/490074 for detail. 212 // https://crbug.com/490074 for detail.
199 blink::WebStringToGURL(origin.toString()), persistent_notification_id)); 213 blink::WebStringToGURL(origin.toString()),
214 base::UTF16ToUTF8(base::StringPiece16(tag)),
215 base::UTF16ToUTF8(base::StringPiece16(notification_id))));
200 } 216 }
201 217
202 void NotificationManager::notifyDelegateDestroyed( 218 void NotificationManager::notifyDelegateDestroyed(
203 blink::WebNotificationDelegate* delegate) { 219 blink::WebNotificationDelegate* delegate) {
204 for (auto& iter : active_page_notifications_) { 220 for (auto& iter : active_page_notifications_) {
205 if (iter.second != delegate) 221 if (iter.second.delegate != delegate)
206 continue; 222 continue;
207 223
208 active_page_notifications_.erase(iter.first); 224 active_page_notifications_.erase(iter.first);
209 return; 225 return;
210 } 226 }
211 } 227 }
212 228
213 bool NotificationManager::OnMessageReceived(const IPC::Message& message) { 229 bool NotificationManager::OnMessageReceived(const IPC::Message& message) {
214 bool handled = true; 230 bool handled = true;
215 IPC_BEGIN_MESSAGE_MAP(NotificationManager, message) 231 IPC_BEGIN_MESSAGE_MAP(NotificationManager, message)
216 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShow, OnDidShow); 232 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShow, OnDidShow);
217 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShowPersistent, 233 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidShowPersistent,
218 OnDidShowPersistent) 234 OnDidShowPersistent)
219 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClose, OnDidClose); 235 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClose, OnDidClose);
220 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClick, OnDidClick); 236 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidClick, OnDidClick);
221 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidGetNotifications, 237 IPC_MESSAGE_HANDLER(PlatformNotificationMsg_DidGetNotifications,
222 OnDidGetNotifications) 238 OnDidGetNotifications)
223 IPC_MESSAGE_UNHANDLED(handled = false) 239 IPC_MESSAGE_UNHANDLED(handled = false)
224 IPC_END_MESSAGE_MAP() 240 IPC_END_MESSAGE_MAP()
225 241
226 return handled; 242 return handled;
227 } 243 }
228 244
229 void NotificationManager::OnDidShow(int notification_id) { 245 void NotificationManager::OnDidShow(int notification_id) {
230 const auto& iter = active_page_notifications_.find(notification_id); 246 const auto& iter = active_page_notifications_.find(notification_id);
231 if (iter == active_page_notifications_.end()) 247 if (iter == active_page_notifications_.end())
232 return; 248 return;
233 249
234 iter->second->dispatchShowEvent(); 250 iter->second.delegate->dispatchShowEvent();
235 } 251 }
236 252
237 void NotificationManager::OnDidShowPersistent(int request_id, bool success) { 253 void NotificationManager::OnDidShowPersistent(int request_id, bool success) {
238 blink::WebNotificationShowCallbacks* callbacks = 254 blink::WebNotificationShowCallbacks* callbacks =
239 pending_show_notification_requests_.Lookup(request_id); 255 pending_show_notification_requests_.Lookup(request_id);
240 DCHECK(callbacks); 256 DCHECK(callbacks);
241 257
242 if (!callbacks) 258 if (!callbacks)
243 return; 259 return;
244 260
245 if (success) 261 if (success)
246 callbacks->onSuccess(); 262 callbacks->onSuccess();
247 else 263 else
248 callbacks->onError(); 264 callbacks->onError();
249 265
250 pending_show_notification_requests_.Remove(request_id); 266 pending_show_notification_requests_.Remove(request_id);
251 } 267 }
252 268
253 void NotificationManager::OnDidClose(int notification_id) { 269 void NotificationManager::OnDidClose(int notification_id) {
254 const auto& iter = active_page_notifications_.find(notification_id); 270 const auto& iter = active_page_notifications_.find(notification_id);
255 if (iter == active_page_notifications_.end()) 271 if (iter == active_page_notifications_.end())
256 return; 272 return;
257 273
258 iter->second->dispatchCloseEvent(); 274 iter->second.delegate->dispatchCloseEvent();
275
259 active_page_notifications_.erase(iter); 276 active_page_notifications_.erase(iter);
260 } 277 }
261 278
262 void NotificationManager::OnDidClick(int notification_id) { 279 void NotificationManager::OnDidClick(int notification_id) {
263 const auto& iter = active_page_notifications_.find(notification_id); 280 const auto& iter = active_page_notifications_.find(notification_id);
264 if (iter == active_page_notifications_.end()) 281 if (iter == active_page_notifications_.end())
265 return; 282 return;
266 283
267 iter->second->dispatchClickEvent(); 284 iter->second.delegate->dispatchClickEvent();
268 } 285 }
269 286
270 void NotificationManager::OnDidGetNotifications( 287 void NotificationManager::OnDidGetNotifications(
271 int request_id, 288 int request_id,
272 const std::vector<PersistentNotificationInfo>& notification_infos) { 289 const std::vector<PersistentNotificationInfo>& notification_infos) {
273 blink::WebNotificationGetCallbacks* callbacks = 290 blink::WebNotificationGetCallbacks* callbacks =
274 pending_get_notification_requests_.Lookup(request_id); 291 pending_get_notification_requests_.Lookup(request_id);
275 DCHECK(callbacks); 292 DCHECK(callbacks);
276 if (!callbacks) 293 if (!callbacks)
277 return; 294 return;
278 295
279 blink::WebVector<blink::WebPersistentNotificationInfo> notifications( 296 blink::WebVector<blink::WebPersistentNotificationInfo> notifications(
280 notification_infos.size()); 297 notification_infos.size());
281 298
282 for (size_t i = 0; i < notification_infos.size(); ++i) { 299 for (size_t i = 0; i < notification_infos.size(); ++i) {
283 blink::WebPersistentNotificationInfo web_notification_info; 300 blink::WebPersistentNotificationInfo web_notification_info;
284 web_notification_info.persistentId = notification_infos[i].first; 301 web_notification_info.notificationId =
302 blink::WebString::fromUTF8(notification_infos[i].first);
285 web_notification_info.data = 303 web_notification_info.data =
286 ToWebNotificationData(notification_infos[i].second); 304 ToWebNotificationData(notification_infos[i].second);
287 305
288 notifications[i] = web_notification_info; 306 notifications[i] = web_notification_info;
289 } 307 }
290 308
291 callbacks->onSuccess(notifications); 309 callbacks->onSuccess(notifications);
292 310
293 pending_get_notification_requests_.Remove(request_id); 311 pending_get_notification_requests_.Remove(request_id);
294 } 312 }
295 313
296 } // namespace content 314 } // namespace content
OLDNEW
« no previous file with comments | « content/child/notifications/notification_manager.h ('k') | content/common/platform_notification_messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698