OLD | NEW |
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/browser/notifications/notification_event_dispatcher_impl.h" | 5 #include "content/browser/notifications/notification_event_dispatcher_impl.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/optional.h" | 8 #include "base/optional.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "content/browser/notifications/notification_message_filter.h" |
10 #include "content/browser/notifications/platform_notification_context_impl.h" | 11 #include "content/browser/notifications/platform_notification_context_impl.h" |
| 12 #include "content/browser/renderer_host/render_process_host_impl.h" |
11 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 13 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
12 #include "content/browser/service_worker/service_worker_registration.h" | 14 #include "content/browser/service_worker/service_worker_registration.h" |
13 #include "content/browser/service_worker/service_worker_storage.h" | 15 #include "content/browser/service_worker/service_worker_storage.h" |
| 16 #include "content/common/platform_notification_messages.h" |
14 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
15 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/render_process_host.h" |
16 #include "content/public/browser/storage_partition.h" | 20 #include "content/public/browser/storage_partition.h" |
17 #include "content/public/common/platform_notification_data.h" | 21 #include "content/public/common/platform_notification_data.h" |
18 | 22 |
19 namespace content { | 23 namespace content { |
20 namespace { | 24 namespace { |
21 | 25 |
22 using NotificationDispatchCompleteCallback = | 26 using NotificationDispatchCompleteCallback = |
23 NotificationEventDispatcher::NotificationDispatchCompleteCallback; | 27 NotificationEventDispatcher::NotificationDispatchCompleteCallback; |
24 using NotificationOperationCallback = | 28 using NotificationOperationCallback = |
25 base::Callback<void(const ServiceWorkerRegistration*, | 29 base::Callback<void(const ServiceWorkerRegistration*, |
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 NotificationEventDispatcher* NotificationEventDispatcher::GetInstance() { | 353 NotificationEventDispatcher* NotificationEventDispatcher::GetInstance() { |
350 return NotificationEventDispatcherImpl::GetInstance(); | 354 return NotificationEventDispatcherImpl::GetInstance(); |
351 } | 355 } |
352 | 356 |
353 NotificationEventDispatcherImpl* | 357 NotificationEventDispatcherImpl* |
354 NotificationEventDispatcherImpl::GetInstance() { | 358 NotificationEventDispatcherImpl::GetInstance() { |
355 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 359 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
356 return base::Singleton<NotificationEventDispatcherImpl>::get(); | 360 return base::Singleton<NotificationEventDispatcherImpl>::get(); |
357 } | 361 } |
358 | 362 |
359 NotificationEventDispatcherImpl::NotificationEventDispatcherImpl() {} | 363 NotificationEventDispatcherImpl::NotificationEventDispatcherImpl() = default; |
360 | 364 NotificationEventDispatcherImpl::~NotificationEventDispatcherImpl() = default; |
361 NotificationEventDispatcherImpl::~NotificationEventDispatcherImpl() {} | |
362 | 365 |
363 void NotificationEventDispatcherImpl::DispatchNotificationClickEvent( | 366 void NotificationEventDispatcherImpl::DispatchNotificationClickEvent( |
364 BrowserContext* browser_context, | 367 BrowserContext* browser_context, |
365 const std::string& notification_id, | 368 const std::string& notification_id, |
366 const GURL& origin, | 369 const GURL& origin, |
367 int action_index, | 370 int action_index, |
368 const base::NullableString16& reply, | 371 const base::NullableString16& reply, |
369 const NotificationDispatchCompleteCallback& dispatch_complete_callback) { | 372 const NotificationDispatchCompleteCallback& dispatch_complete_callback) { |
370 DispatchNotificationEvent( | 373 DispatchNotificationEvent( |
371 browser_context, notification_id, origin, | 374 browser_context, notification_id, origin, |
372 base::Bind(&DoDispatchNotificationClickEvent, action_index, reply, | 375 base::Bind(&DoDispatchNotificationClickEvent, action_index, reply, |
373 dispatch_complete_callback), | 376 dispatch_complete_callback), |
374 dispatch_complete_callback); | 377 dispatch_complete_callback); |
375 } | 378 } |
376 | 379 |
377 void NotificationEventDispatcherImpl::DispatchNotificationCloseEvent( | 380 void NotificationEventDispatcherImpl::DispatchNotificationCloseEvent( |
378 BrowserContext* browser_context, | 381 BrowserContext* browser_context, |
379 const std::string& notification_id, | 382 const std::string& notification_id, |
380 const GURL& origin, | 383 const GURL& origin, |
381 bool by_user, | 384 bool by_user, |
382 const NotificationDispatchCompleteCallback& dispatch_complete_callback) { | 385 const NotificationDispatchCompleteCallback& dispatch_complete_callback) { |
383 DispatchNotificationEvent( | 386 DispatchNotificationEvent( |
384 browser_context, notification_id, origin, | 387 browser_context, notification_id, origin, |
385 base::Bind(&DoDispatchNotificationCloseEvent, notification_id, by_user, | 388 base::Bind(&DoDispatchNotificationCloseEvent, notification_id, by_user, |
386 dispatch_complete_callback), | 389 dispatch_complete_callback), |
387 dispatch_complete_callback); | 390 dispatch_complete_callback); |
388 } | 391 } |
389 | 392 |
| 393 void NotificationEventDispatcherImpl::RegisterNonPersistentNotification( |
| 394 const std::string& notification_id, |
| 395 int renderer_id, |
| 396 int non_persistent_id) { |
| 397 renderer_ids_[notification_id] = renderer_id; |
| 398 non_persistent_ids_[notification_id] = non_persistent_id; |
| 399 } |
| 400 |
| 401 void NotificationEventDispatcherImpl::DispatchNonPersistentShowEvent( |
| 402 const std::string& notification_id) { |
| 403 if (!renderer_ids_.count(notification_id)) |
| 404 return; |
| 405 DCHECK(non_persistent_ids_.count(notification_id)); |
| 406 |
| 407 RenderProcessHost* sender = |
| 408 RenderProcessHost::FromID(renderer_ids_[notification_id]); |
| 409 if (!sender) |
| 410 return; |
| 411 |
| 412 sender->Send(new PlatformNotificationMsg_DidShow( |
| 413 non_persistent_ids_[notification_id])); |
| 414 } |
| 415 |
| 416 void NotificationEventDispatcherImpl::DispatchNonPersistentClickEvent( |
| 417 const std::string& notification_id) { |
| 418 if (!renderer_ids_.count(notification_id)) |
| 419 return; |
| 420 DCHECK(non_persistent_ids_.count(notification_id)); |
| 421 |
| 422 RenderProcessHost* sender = |
| 423 RenderProcessHost::FromID(renderer_ids_[notification_id]); |
| 424 |
| 425 // This can happen when a notification is clicked by the user but the |
| 426 // renderer does not exist any more, for example because the tab has been |
| 427 // closed. |
| 428 if (!sender) |
| 429 return; |
| 430 sender->Send(new PlatformNotificationMsg_DidClick( |
| 431 non_persistent_ids_[notification_id])); |
| 432 } |
| 433 |
| 434 void NotificationEventDispatcherImpl::DispatchNonPersistentCloseEvent( |
| 435 const std::string& notification_id) { |
| 436 if (!renderer_ids_.count(notification_id)) |
| 437 return; |
| 438 DCHECK(non_persistent_ids_.count(notification_id)); |
| 439 |
| 440 RenderProcessHost* sender = |
| 441 RenderProcessHost::FromID(renderer_ids_[notification_id]); |
| 442 |
| 443 // This can happen when a notification is closed by the user but the |
| 444 // renderer does not exist any more, for example because the tab has been |
| 445 // closed. |
| 446 if (!sender) |
| 447 return; |
| 448 sender->Send(new PlatformNotificationMsg_DidClose( |
| 449 non_persistent_ids_[notification_id])); |
| 450 |
| 451 static_cast<RenderProcessHostImpl*>(sender) |
| 452 ->notification_message_filter() |
| 453 ->DidCloseNotification(notification_id); |
| 454 } |
| 455 |
| 456 void NotificationEventDispatcherImpl::RendererGone(int renderer_id) { |
| 457 for (auto iter = renderer_ids_.begin(); iter != renderer_ids_.end();) { |
| 458 if (iter->second == renderer_id) { |
| 459 non_persistent_ids_.erase(iter->first); |
| 460 iter = renderer_ids_.erase(iter); |
| 461 } else { |
| 462 iter++; |
| 463 } |
| 464 } |
| 465 } |
| 466 |
390 } // namespace content | 467 } // namespace content |
OLD | NEW |