| 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" |
| 8 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 9 #include "content/browser/notifications/platform_notification_context_impl.h" | 10 #include "content/browser/notifications/platform_notification_context_impl.h" |
| 11 #include "content/browser/notifications/type_converters.h" |
| 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 12 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 11 #include "content/browser/service_worker/service_worker_registration.h" | 13 #include "content/browser/service_worker/service_worker_registration.h" |
| 12 #include "content/browser/service_worker/service_worker_storage.h" | 14 #include "content/browser/service_worker/service_worker_storage.h" |
| 13 #include "content/common/service_worker/service_worker_messages.h" | 15 #include "content/common/notification_service_worker_client.mojom.h" |
| 16 #include "content/common/service_worker/service_worker_type_converters.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" |
| 16 #include "content/public/browser/storage_partition.h" | 19 #include "content/public/browser/storage_partition.h" |
| 17 #include "content/public/common/platform_notification_data.h" | 20 #include "content/public/common/platform_notification_data.h" |
| 21 #include "third_party/WebKit/public/platform/modules/notifications/notification.
mojom.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*, |
| 26 const NotificationDatabaseData&)>; | 30 const NotificationDatabaseData&)>; |
| 27 using NotificationOperationCallbackWithContext = | 31 using NotificationOperationCallbackWithContext = |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 195 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 192 notification_context->ReadNotificationData( | 196 notification_context->ReadNotificationData( |
| 193 persistent_notification_id, origin, | 197 persistent_notification_id, origin, |
| 194 base::Bind(&FindServiceWorkerRegistration, origin, service_worker_context, | 198 base::Bind(&FindServiceWorkerRegistration, origin, service_worker_context, |
| 195 notification_context, notification_read_callback, | 199 notification_context, notification_read_callback, |
| 196 dispatch_error_callback)); | 200 dispatch_error_callback)); |
| 197 } | 201 } |
| 198 | 202 |
| 199 // ----------------------------------------------------------------------------- | 203 // ----------------------------------------------------------------------------- |
| 200 | 204 |
| 205 // Called when a notification event has finished. Marks the request as finished |
| 206 // and will finalize the |callback| with the |status| code. |
| 207 void DidDispatchNotificationEvent( |
| 208 const scoped_refptr<ServiceWorkerVersion>& service_worker, |
| 209 int request_id, |
| 210 const ServiceWorkerVersion::StatusCallback& callback, |
| 211 mojom::ServiceWorkerEventStatus status) { |
| 212 if (!service_worker->FinishRequest( |
| 213 request_id, status == mojom::ServiceWorkerEventStatus::COMPLETED)) { |
| 214 return; |
| 215 } |
| 216 |
| 217 callback.Run(mojo::ConvertTo<ServiceWorkerStatusCode>(status)); |
| 218 } |
| 219 |
| 201 // Dispatches the notificationclick event on |service_worker|. Must be called on | 220 // Dispatches the notificationclick event on |service_worker|. Must be called on |
| 202 // the IO thread, and with the worker running. | 221 // the IO thread, and with the worker running. |
| 203 void DispatchNotificationClickEventOnWorker( | 222 void DispatchNotificationClickEventOnWorker( |
| 204 const scoped_refptr<ServiceWorkerVersion>& service_worker, | 223 const scoped_refptr<ServiceWorkerVersion>& service_worker, |
| 205 const NotificationDatabaseData& notification_database_data, | 224 const NotificationDatabaseData& notification_database_data, |
| 206 int action_index, | 225 int action_index, |
| 207 const ServiceWorkerVersion::StatusCallback& callback) { | 226 const ServiceWorkerVersion::StatusCallback& callback) { |
| 208 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 227 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 209 int request_id = service_worker->StartRequest( | 228 int request_id = service_worker->StartRequest( |
| 210 ServiceWorkerMetrics::EventType::NOTIFICATION_CLICK, callback); | 229 ServiceWorkerMetrics::EventType::NOTIFICATION_CLICK, callback); |
| 211 service_worker->DispatchSimpleEvent< | 230 |
| 212 ServiceWorkerHostMsg_NotificationClickEventFinished>( | 231 blink::mojom::NotificationPtr notification = |
| 213 request_id, | 232 blink::mojom::Notification::From( |
| 214 ServiceWorkerMsg_NotificationClickEvent( | 233 notification_database_data.notification_data); |
| 215 request_id, notification_database_data.notification_id, | 234 |
| 216 notification_database_data.notification_data, action_index)); | 235 // TODO(peter): Use the NotificationIdGenerator for creating the id. |
| 236 notification->id = |
| 237 base::Int64ToString(notification_database_data.notification_id); |
| 238 |
| 239 base::WeakPtr<mojom::NotificationServiceWorkerClient> client = |
| 240 service_worker |
| 241 ->GetMojoServiceForRequest<mojom::NotificationServiceWorkerClient>( |
| 242 request_id); |
| 243 |
| 244 client->Click(std::move(notification), action_index, |
| 245 base::Bind(&DidDispatchNotificationEvent, service_worker, |
| 246 request_id, callback)); |
| 217 } | 247 } |
| 218 | 248 |
| 219 // Dispatches the notification click event on the |service_worker_registration|. | 249 // Dispatches the notification click event on the |service_worker_registration|. |
| 220 void DoDispatchNotificationClickEvent( | 250 void DoDispatchNotificationClickEvent( |
| 221 int action_index, | 251 int action_index, |
| 222 const NotificationDispatchCompleteCallback& dispatch_complete_callback, | 252 const NotificationDispatchCompleteCallback& dispatch_complete_callback, |
| 223 const scoped_refptr<PlatformNotificationContext>& notification_context, | 253 const scoped_refptr<PlatformNotificationContext>& notification_context, |
| 224 const ServiceWorkerRegistration* service_worker_registration, | 254 const ServiceWorkerRegistration* service_worker_registration, |
| 225 const NotificationDatabaseData& notification_database_data) { | 255 const NotificationDatabaseData& notification_database_data) { |
| 226 ServiceWorkerVersion::StatusCallback status_callback = base::Bind( | 256 ServiceWorkerVersion::StatusCallback status_callback = base::Bind( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 | 299 |
| 270 // Dispatches the notificationclose event on |service_worker|. Must be called on | 300 // Dispatches the notificationclose event on |service_worker|. Must be called on |
| 271 // the IO thread, and with the worker running. | 301 // the IO thread, and with the worker running. |
| 272 void DispatchNotificationCloseEventOnWorker( | 302 void DispatchNotificationCloseEventOnWorker( |
| 273 const scoped_refptr<ServiceWorkerVersion>& service_worker, | 303 const scoped_refptr<ServiceWorkerVersion>& service_worker, |
| 274 const NotificationDatabaseData& notification_database_data, | 304 const NotificationDatabaseData& notification_database_data, |
| 275 const ServiceWorkerVersion::StatusCallback& callback) { | 305 const ServiceWorkerVersion::StatusCallback& callback) { |
| 276 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 306 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 277 int request_id = service_worker->StartRequest( | 307 int request_id = service_worker->StartRequest( |
| 278 ServiceWorkerMetrics::EventType::NOTIFICATION_CLOSE, callback); | 308 ServiceWorkerMetrics::EventType::NOTIFICATION_CLOSE, callback); |
| 279 service_worker->DispatchSimpleEvent< | 309 |
| 280 ServiceWorkerHostMsg_NotificationCloseEventFinished>( | 310 blink::mojom::NotificationPtr notification = |
| 281 request_id, ServiceWorkerMsg_NotificationCloseEvent( | 311 blink::mojom::Notification::From( |
| 282 request_id, notification_database_data.notification_id, | 312 notification_database_data.notification_data); |
| 283 notification_database_data.notification_data)); | 313 |
| 314 // TODO(peter): Use the NotificationIdGenerator in this case too. |
| 315 notification->id = |
| 316 base::Int64ToString(notification_database_data.notification_id); |
| 317 |
| 318 base::WeakPtr<mojom::NotificationServiceWorkerClient> client = |
| 319 service_worker |
| 320 ->GetMojoServiceForRequest<mojom::NotificationServiceWorkerClient>( |
| 321 request_id); |
| 322 |
| 323 client->Close(std::move(notification), |
| 324 base::Bind(&DidDispatchNotificationEvent, service_worker, |
| 325 request_id, callback)); |
| 284 } | 326 } |
| 285 | 327 |
| 286 // Actually dispatches the notification close event on the service worker | 328 // Actually dispatches the notification close event on the service worker |
| 287 // registration. | 329 // registration. |
| 288 void DoDispatchNotificationCloseEvent( | 330 void DoDispatchNotificationCloseEvent( |
| 289 bool by_user, | 331 bool by_user, |
| 290 const NotificationDispatchCompleteCallback& dispatch_complete_callback, | 332 const NotificationDispatchCompleteCallback& dispatch_complete_callback, |
| 291 const scoped_refptr<PlatformNotificationContext>& notification_context, | 333 const scoped_refptr<PlatformNotificationContext>& notification_context, |
| 292 const ServiceWorkerRegistration* service_worker_registration, | 334 const ServiceWorkerRegistration* service_worker_registration, |
| 293 const NotificationDatabaseData& notification_database_data) { | 335 const NotificationDatabaseData& notification_database_data) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 const GURL& origin, | 417 const GURL& origin, |
| 376 bool by_user, | 418 bool by_user, |
| 377 const NotificationDispatchCompleteCallback& dispatch_complete_callback) { | 419 const NotificationDispatchCompleteCallback& dispatch_complete_callback) { |
| 378 DispatchNotificationEvent(browser_context, persistent_notification_id, origin, | 420 DispatchNotificationEvent(browser_context, persistent_notification_id, origin, |
| 379 base::Bind(&DoDispatchNotificationCloseEvent, | 421 base::Bind(&DoDispatchNotificationCloseEvent, |
| 380 by_user, dispatch_complete_callback), | 422 by_user, dispatch_complete_callback), |
| 381 dispatch_complete_callback); | 423 dispatch_complete_callback); |
| 382 } | 424 } |
| 383 | 425 |
| 384 } // namespace content | 426 } // namespace content |
| OLD | NEW |