| 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/strings/string_number_conversions.h" |
| 9 #include "content/browser/notifications/platform_notification_context_impl.h" |
| 8 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 9 #include "content/browser/service_worker/service_worker_registration.h" | 11 #include "content/browser/service_worker/service_worker_registration.h" |
| 10 #include "content/browser/service_worker/service_worker_storage.h" | 12 #include "content/browser/service_worker/service_worker_storage.h" |
| 11 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
| 12 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/notification_database_data.h" |
| 13 #include "content/public/browser/storage_partition.h" | 16 #include "content/public/browser/storage_partition.h" |
| 14 #include "content/public/common/platform_notification_data.h" | 17 #include "content/public/common/platform_notification_data.h" |
| 15 | 18 |
| 16 namespace content { | 19 namespace content { |
| 17 namespace { | 20 namespace { |
| 18 | 21 |
| 19 using NotificationClickDispatchCompleteCallback = | 22 using NotificationClickDispatchCompleteCallback = |
| 20 NotificationEventDispatcher::NotificationClickDispatchCompleteCallback; | 23 NotificationEventDispatcher::NotificationClickDispatchCompleteCallback; |
| 21 | 24 |
| 22 // To be called when the notificationclick event has finished executing. Will | 25 // To be called when the notificationclick event has finished executing. Will |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 } | 57 } |
| 55 | 58 |
| 56 BrowserThread::PostTask(BrowserThread::UI, | 59 BrowserThread::PostTask(BrowserThread::UI, |
| 57 FROM_HERE, | 60 FROM_HERE, |
| 58 base::Bind(dispatch_complete_callback, status)); | 61 base::Bind(dispatch_complete_callback, status)); |
| 59 } | 62 } |
| 60 | 63 |
| 61 // Dispatches the notificationclick on |service_worker_registration| if the | 64 // Dispatches the notificationclick on |service_worker_registration| if the |
| 62 // registration was available. Must be called on the IO thread. | 65 // registration was available. Must be called on the IO thread. |
| 63 void DispatchNotificationClickEventOnRegistration( | 66 void DispatchNotificationClickEventOnRegistration( |
| 64 const std::string& notification_id, | 67 const NotificationDatabaseData& notification_database_data, |
| 65 const PlatformNotificationData& notification_data, | |
| 66 const NotificationClickDispatchCompleteCallback& dispatch_complete_callback, | 68 const NotificationClickDispatchCompleteCallback& dispatch_complete_callback, |
| 67 ServiceWorkerStatusCode service_worker_status, | 69 ServiceWorkerStatusCode service_worker_status, |
| 68 const scoped_refptr<ServiceWorkerRegistration>& | 70 const scoped_refptr<ServiceWorkerRegistration>& |
| 69 service_worker_registration) { | 71 service_worker_registration) { |
| 70 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 72 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 71 if (service_worker_status == SERVICE_WORKER_OK) { | 73 if (service_worker_status == SERVICE_WORKER_OK) { |
| 72 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = | 74 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = |
| 73 base::Bind(&NotificationClickEventFinished, | 75 base::Bind(&NotificationClickEventFinished, |
| 74 dispatch_complete_callback, | 76 dispatch_complete_callback, |
| 75 service_worker_registration); | 77 service_worker_registration); |
| 76 service_worker_registration->active_version() | 78 |
| 77 ->DispatchNotificationClickEvent(dispatch_event_callback, | 79 // TODO(peter): Pass the persistent notification id as an int64_t rather |
| 78 notification_id, | 80 // than as a string. This depends on the Blink API being updated. |
| 79 notification_data); | 81 std::string persistent_notification_id_string = |
| 82 base::Int64ToString(notification_database_data.notification_id); |
| 83 |
| 84 service_worker_registration->active_version()-> |
| 85 DispatchNotificationClickEvent( |
| 86 dispatch_event_callback, |
| 87 persistent_notification_id_string, |
| 88 notification_database_data.notification_data); |
| 80 return; | 89 return; |
| 81 } | 90 } |
| 82 | 91 |
| 83 PersistentNotificationStatus status = PERSISTENT_NOTIFICATION_STATUS_SUCCESS; | 92 PersistentNotificationStatus status = PERSISTENT_NOTIFICATION_STATUS_SUCCESS; |
| 84 switch (service_worker_status) { | 93 switch (service_worker_status) { |
| 85 case SERVICE_WORKER_ERROR_NOT_FOUND: | 94 case SERVICE_WORKER_ERROR_NOT_FOUND: |
| 86 status = PERSISTENT_NOTIFICATION_STATUS_NO_SERVICE_WORKER; | 95 status = PERSISTENT_NOTIFICATION_STATUS_NO_SERVICE_WORKER; |
| 87 break; | 96 break; |
| 88 case SERVICE_WORKER_ERROR_FAILED: | 97 case SERVICE_WORKER_ERROR_FAILED: |
| 89 case SERVICE_WORKER_ERROR_ABORT: | 98 case SERVICE_WORKER_ERROR_ABORT: |
| (...skipping 18 matching lines...) Expand all Loading... |
| 108 | 117 |
| 109 BrowserThread::PostTask(BrowserThread::UI, | 118 BrowserThread::PostTask(BrowserThread::UI, |
| 110 FROM_HERE, | 119 FROM_HERE, |
| 111 base::Bind(dispatch_complete_callback, status)); | 120 base::Bind(dispatch_complete_callback, status)); |
| 112 } | 121 } |
| 113 | 122 |
| 114 // Finds the ServiceWorkerRegistration associated with the |origin| and | 123 // Finds the ServiceWorkerRegistration associated with the |origin| and |
| 115 // |service_worker_registration_id|. Must be called on the IO thread. | 124 // |service_worker_registration_id|. Must be called on the IO thread. |
| 116 void FindServiceWorkerRegistration( | 125 void FindServiceWorkerRegistration( |
| 117 const GURL& origin, | 126 const GURL& origin, |
| 118 int64 service_worker_registration_id, | |
| 119 const std::string& notification_id, | |
| 120 const PlatformNotificationData& notification_data, | |
| 121 const NotificationClickDispatchCompleteCallback& dispatch_complete_callback, | 127 const NotificationClickDispatchCompleteCallback& dispatch_complete_callback, |
| 122 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { | 128 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, |
| 129 bool success, |
| 130 const NotificationDatabaseData& notification_database_data) { |
| 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 131 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 132 if (!success) { |
| 133 BrowserThread::PostTask( |
| 134 BrowserThread::UI, |
| 135 FROM_HERE, |
| 136 base::Bind(dispatch_complete_callback, |
| 137 PERSISTENT_NOTIFICATION_STATUS_DATABASE_ERROR)); |
| 138 return; |
| 139 } |
| 140 |
| 124 service_worker_context->context()->storage()->FindRegistrationForId( | 141 service_worker_context->context()->storage()->FindRegistrationForId( |
| 125 service_worker_registration_id, | 142 notification_database_data.service_worker_registration_id, |
| 126 origin, | 143 origin, |
| 127 base::Bind(&DispatchNotificationClickEventOnRegistration, | 144 base::Bind(&DispatchNotificationClickEventOnRegistration, |
| 128 notification_id, | 145 notification_database_data, |
| 129 notification_data, | |
| 130 dispatch_complete_callback)); | 146 dispatch_complete_callback)); |
| 131 } | 147 } |
| 132 | 148 |
| 149 // Reads the data associated with the |persistent_notification_id| belonging to |
| 150 // |origin| from the notification context. |
| 151 void ReadNotificationDatabaseData( |
| 152 int64_t persistent_notification_id, |
| 153 const GURL& origin, |
| 154 const NotificationClickDispatchCompleteCallback& dispatch_complete_callback, |
| 155 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, |
| 156 scoped_refptr<PlatformNotificationContextImpl> notification_context) { |
| 157 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 158 notification_context->ReadNotificationData( |
| 159 persistent_notification_id, |
| 160 origin, |
| 161 base::Bind(&FindServiceWorkerRegistration, |
| 162 origin, dispatch_complete_callback, service_worker_context)); |
| 163 } |
| 164 |
| 133 } // namespace | 165 } // namespace |
| 134 | 166 |
| 135 // static | 167 // static |
| 136 NotificationEventDispatcher* NotificationEventDispatcher::GetInstance() { | 168 NotificationEventDispatcher* NotificationEventDispatcher::GetInstance() { |
| 137 return NotificationEventDispatcherImpl::GetInstance(); | 169 return NotificationEventDispatcherImpl::GetInstance(); |
| 138 } | 170 } |
| 139 | 171 |
| 140 NotificationEventDispatcherImpl* | 172 NotificationEventDispatcherImpl* |
| 141 NotificationEventDispatcherImpl::GetInstance() { | 173 NotificationEventDispatcherImpl::GetInstance() { |
| 142 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 174 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 143 return Singleton<NotificationEventDispatcherImpl>::get(); | 175 return Singleton<NotificationEventDispatcherImpl>::get(); |
| 144 } | 176 } |
| 145 | 177 |
| 146 NotificationEventDispatcherImpl::NotificationEventDispatcherImpl() {} | 178 NotificationEventDispatcherImpl::NotificationEventDispatcherImpl() {} |
| 147 | 179 |
| 148 NotificationEventDispatcherImpl::~NotificationEventDispatcherImpl() {} | 180 NotificationEventDispatcherImpl::~NotificationEventDispatcherImpl() {} |
| 149 | 181 |
| 150 void NotificationEventDispatcherImpl::DispatchNotificationClickEvent( | 182 void NotificationEventDispatcherImpl::DispatchNotificationClickEvent( |
| 151 BrowserContext* browser_context, | 183 BrowserContext* browser_context, |
| 184 int64_t persistent_notification_id, |
| 152 const GURL& origin, | 185 const GURL& origin, |
| 153 int64 service_worker_registration_id, | |
| 154 const std::string& notification_id, | |
| 155 const PlatformNotificationData& notification_data, | |
| 156 const NotificationClickDispatchCompleteCallback& | 186 const NotificationClickDispatchCompleteCallback& |
| 157 dispatch_complete_callback) { | 187 dispatch_complete_callback) { |
| 158 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 188 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 189 DCHECK_GT(persistent_notification_id, 0); |
| 190 DCHECK(origin.is_valid()); |
| 159 | 191 |
| 160 StoragePartition* partition = | 192 StoragePartition* partition = |
| 161 BrowserContext::GetStoragePartitionForSite(browser_context, origin); | 193 BrowserContext::GetStoragePartitionForSite(browser_context, origin); |
| 194 |
| 162 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = | 195 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = |
| 163 static_cast<ServiceWorkerContextWrapper*>( | 196 static_cast<ServiceWorkerContextWrapper*>( |
| 164 partition->GetServiceWorkerContext()); | 197 partition->GetServiceWorkerContext()); |
| 198 scoped_refptr<PlatformNotificationContextImpl> notification_context = |
| 199 static_cast<PlatformNotificationContextImpl*>( |
| 200 partition->GetPlatformNotificationContext()); |
| 201 |
| 165 BrowserThread::PostTask( | 202 BrowserThread::PostTask( |
| 166 BrowserThread::IO, | 203 BrowserThread::IO, |
| 167 FROM_HERE, | 204 FROM_HERE, |
| 168 base::Bind(&FindServiceWorkerRegistration, | 205 base::Bind(&ReadNotificationDatabaseData, |
| 206 persistent_notification_id, |
| 169 origin, | 207 origin, |
| 170 service_worker_registration_id, | |
| 171 notification_id, | |
| 172 notification_data, | |
| 173 dispatch_complete_callback, | 208 dispatch_complete_callback, |
| 174 service_worker_context)); | 209 service_worker_context, |
| 210 notification_context)); |
| 175 } | 211 } |
| 176 | 212 |
| 177 } // namespace content | 213 } // namespace content |
| OLD | NEW |