Chromium Code Reviews| Index: chrome/browser/push_messaging/push_messaging_service_impl.cc |
| diff --git a/chrome/browser/push_messaging/push_messaging_service_impl.cc b/chrome/browser/push_messaging/push_messaging_service_impl.cc |
| index ea47be0113b8326dd4468f3b2eff1fa0b0109647..15271e77535128a851b05749096757c80eb7879a 100644 |
| --- a/chrome/browser/push_messaging/push_messaging_service_impl.cc |
| +++ b/chrome/browser/push_messaging/push_messaging_service_impl.cc |
| @@ -37,6 +37,8 @@ |
| #include "components/rappor/rappor_utils.h" |
| #include "content/public/browser/browser_context.h" |
| #include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/notification_database_data.h" |
| +#include "content/public/browser/platform_notification_context.h" |
| #include "content/public/browser/render_frame_host.h" |
| #include "content/public/browser/service_worker_context.h" |
| #include "content/public/browser/storage_partition.h" |
| @@ -57,6 +59,8 @@ |
| #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| #endif |
| +using content::BrowserThread; |
| + |
| namespace { |
| const int kMaxRegistrations = 1000000; |
| @@ -280,20 +284,56 @@ void PushMessagingServiceImpl::DeliverMessageCallback( |
| void PushMessagingServiceImpl::RequireUserVisibleUX( |
| const GURL& requesting_origin, int64 service_worker_registration_id, |
| const base::Closure& message_handled_closure) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| #if defined(ENABLE_NOTIFICATIONS) |
| // TODO(johnme): Relax this heuristic slightly. |
| - PlatformNotificationServiceImpl* notification_service = |
| - PlatformNotificationServiceImpl::GetInstance(); |
| - // Can't use g_browser_process->notification_ui_manager(), since the test uses |
| - // PlatformNotificationServiceImpl::SetNotificationUIManagerForTesting. |
| - // TODO(peter): Remove the need to use both APIs here once Notification.get() |
| - // is supported. |
| - int notification_count = notification_service->GetNotificationUIManager()-> |
| - GetAllIdsByProfileAndSourceOrigin(profile_, requesting_origin).size(); |
| + scoped_refptr<content::PlatformNotificationContext> notification_context = |
| + content::BrowserContext::GetStoragePartitionForSite( |
| + profile_, requesting_origin)->GetPlatformNotificationContext(); |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind( |
| + &content::PlatformNotificationContext |
| + ::ReadAllNotificationDataForServiceWorkerRegistration, |
| + notification_context, |
| + requesting_origin, service_worker_registration_id, |
| + base::Bind( |
| + &PushMessagingServiceImpl::DidGetNotificationsShowingIOProxy, |
| + weak_factory_.GetWeakPtr(), |
| + requesting_origin, service_worker_registration_id, |
| + message_handled_closure))); |
| +#else |
| + message_handled_closure.Run(); |
| +#endif // defined(ENABLE_NOTIFICATIONS) |
| +} |
| + |
| +void PushMessagingServiceImpl::DidGetNotificationsShowingIOProxy( |
|
Peter Beverloo
2015/04/30 11:11:42
nit: // static
johnme
2015/04/30 13:30:12
Done.
|
| + const base::WeakPtr<PushMessagingServiceImpl>& ui_weak_ptr, |
| + const GURL& requesting_origin, |
| + int64 service_worker_registration_id, |
| + const base::Closure& message_handled_closure, |
| + bool success, |
| + const std::vector<content::NotificationDatabaseData>& data) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&PushMessagingServiceImpl::DidGetNotificationsShowing, |
| + ui_weak_ptr, |
| + requesting_origin, service_worker_registration_id, |
| + message_handled_closure, |
| + success, data)); |
| +} |
| + |
| +void PushMessagingServiceImpl::DidGetNotificationsShowing( |
| + const GURL& requesting_origin, int64 service_worker_registration_id, |
| + const base::Closure& message_handled_closure, |
| + bool success, const std::vector<content::NotificationDatabaseData>& data) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| // TODO(johnme): Hiding an existing notification should also count as a useful |
| // user-visible action done in response to a push message - but make sure that |
| // sending two messages in rapid succession which show then hide a |
| // notification doesn't count. |
| + int notification_count = success ? data.size() : 0; |
| bool notification_shown = notification_count > 0; |
| bool notification_needed = true; |
| @@ -357,9 +397,6 @@ void PushMessagingServiceImpl::RequireUserVisibleUX( |
| content::PUSH_USER_VISIBLE_STATUS_NOT_REQUIRED_AND_NOT_SHOWN); |
| message_handled_closure.Run(); |
| } |
| -#else |
| - message_handled_closure.Run(); |
| -#endif // defined(ENABLE_NOTIFICATIONS) |
| } |
| static void IgnoreResult(bool unused) { |
| @@ -370,6 +407,7 @@ void PushMessagingServiceImpl::DidGetNotificationsShown( |
| bool notification_shown, bool notification_needed, |
| const base::Closure& message_handled_closure, |
| const std::string& data, bool success, bool not_found) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| content::ServiceWorkerContext* service_worker_context = |
| content::BrowserContext::GetStoragePartitionForSite( |
| profile_, requesting_origin)->GetServiceWorkerContext(); |
| @@ -429,13 +467,61 @@ void PushMessagingServiceImpl::DidGetNotificationsShown( |
| notification_data.body = |
| l10n_util::GetStringUTF16(IDS_PUSH_MESSAGING_GENERIC_NOTIFICATION_BODY); |
| notification_data.tag = kPushMessagingForcedNotificationTag; |
| - notification_data.icon = GURL(); // TODO(johnme): Better icon? |
| + notification_data.icon = GURL(); |
| notification_data.silent = true; |
| - PlatformNotificationServiceImpl* notification_service = |
| - PlatformNotificationServiceImpl::GetInstance(); |
| - notification_service->DisplayPersistentNotification( |
| + |
| + content::NotificationDatabaseData database_data; |
| + database_data.origin = requesting_origin; |
| + database_data.service_worker_registration_id = |
| + service_worker_registration_id; |
| + database_data.notification_data = notification_data; |
| + |
| + scoped_refptr<content::PlatformNotificationContext> notification_context = |
| + content::BrowserContext::GetStoragePartitionForSite( |
| + profile_, requesting_origin)->GetPlatformNotificationContext(); |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, FROM_HERE, |
| + base::Bind( |
| + &content::PlatformNotificationContext::WriteNotificationData, |
| + notification_context, |
| + requesting_origin, database_data, |
| + base::Bind(&PushMessagingServiceImpl::DidWriteNotificationDataIOProxy, |
| + weak_factory_.GetWeakPtr(), |
| + requesting_origin, notification_data, |
| + message_handled_closure))); |
| +} |
| + |
| +void PushMessagingServiceImpl::DidWriteNotificationDataIOProxy( |
| + const base::WeakPtr<PushMessagingServiceImpl>& ui_weak_ptr, |
| + const GURL& requesting_origin, |
| + const content::PlatformNotificationData& notification_data, |
| + const base::Closure& message_handled_closure, |
| + bool success, |
| + int64_t persistent_notification_id) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&PushMessagingServiceImpl::DidWriteNotificationData, |
| + ui_weak_ptr, |
| + requesting_origin, notification_data, message_handled_closure, |
| + success, persistent_notification_id)); |
| +} |
| + |
| +void PushMessagingServiceImpl::DidWriteNotificationData( |
| + const GURL& requesting_origin, |
| + const content::PlatformNotificationData& notification_data, |
| + const base::Closure& message_handled_closure, |
| + bool success, |
| + int64_t persistent_notification_id) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + if (!success) { |
| + DLOG(ERROR) << "Writing forced notification to database should rarely fail"; |
|
Peter Beverloo
2015/04/30 11:11:42
nit: s/rarely/not/.
johnme
2015/04/30 13:30:12
Done.
|
| + message_handled_closure.Run(); |
| + return; |
| + } |
| + PlatformNotificationServiceImpl::GetInstance()->DisplayPersistentNotification( |
| profile_, |
| - service_worker_registration_id, |
| + persistent_notification_id, |
| requesting_origin, |
| SkBitmap() /* icon */, |
| notification_data); |