| 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 "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "content/browser/notifications/platform_notification_context_impl.h" | 9 #include "content/browser/notifications/platform_notification_context_impl.h" |
| 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 return; | 171 return; |
| 172 } | 172 } |
| 173 | 173 |
| 174 service_worker_context->FindReadyRegistrationForId( | 174 service_worker_context->FindReadyRegistrationForId( |
| 175 notification_database_data.service_worker_registration_id, origin, | 175 notification_database_data.service_worker_registration_id, origin, |
| 176 base::Bind(&DispatchNotificationEventOnRegistration, | 176 base::Bind(&DispatchNotificationEventOnRegistration, |
| 177 notification_database_data, notification_context, | 177 notification_database_data, notification_context, |
| 178 notification_action_callback, dispatch_error_callback)); | 178 notification_action_callback, dispatch_error_callback)); |
| 179 } | 179 } |
| 180 | 180 |
| 181 // Reads the data associated with the |persistent_notification_id| belonging to | 181 // Reads the data associated with the |notification_id| belonging to |origin| |
| 182 // |origin| from the notification context. | 182 // from the notification context. |
| 183 void ReadNotificationDatabaseData( | 183 void ReadNotificationDatabaseData( |
| 184 int64_t persistent_notification_id, | 184 const std::string& notification_id, |
| 185 const GURL& origin, | 185 const GURL& origin, |
| 186 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context, | 186 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context, |
| 187 const scoped_refptr<PlatformNotificationContext>& notification_context, | 187 const scoped_refptr<PlatformNotificationContext>& notification_context, |
| 188 const NotificationOperationCallback& notification_read_callback, | 188 const NotificationOperationCallback& notification_read_callback, |
| 189 const NotificationDispatchCompleteCallback& dispatch_error_callback) { | 189 const NotificationDispatchCompleteCallback& dispatch_error_callback) { |
| 190 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 190 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 191 notification_context->ReadNotificationData( | 191 notification_context->ReadNotificationData( |
| 192 persistent_notification_id, origin, | 192 notification_id, origin, |
| 193 base::Bind(&FindServiceWorkerRegistration, origin, service_worker_context, | 193 base::Bind(&FindServiceWorkerRegistration, origin, service_worker_context, |
| 194 notification_context, notification_read_callback, | 194 notification_context, notification_read_callback, |
| 195 dispatch_error_callback)); | 195 dispatch_error_callback)); |
| 196 } | 196 } |
| 197 | 197 |
| 198 // ----------------------------------------------------------------------------- | 198 // ----------------------------------------------------------------------------- |
| 199 | 199 |
| 200 // Dispatches the notificationclick event on |service_worker|. Must be called on | 200 // Dispatches the notificationclick event on |service_worker|. Must be called on |
| 201 // the IO thread, and with the worker running. | 201 // the IO thread, and with the worker running. |
| 202 void DispatchNotificationClickEventOnWorker( | 202 void DispatchNotificationClickEventOnWorker( |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 } | 248 } |
| 249 NotificationEventFinished( | 249 NotificationEventFinished( |
| 250 dispatch_complete_callback, | 250 dispatch_complete_callback, |
| 251 success ? PERSISTENT_NOTIFICATION_STATUS_SUCCESS | 251 success ? PERSISTENT_NOTIFICATION_STATUS_SUCCESS |
| 252 : PERSISTENT_NOTIFICATION_STATUS_DATABASE_ERROR); | 252 : PERSISTENT_NOTIFICATION_STATUS_DATABASE_ERROR); |
| 253 } | 253 } |
| 254 | 254 |
| 255 // Called when the persistent notification close event has been handled | 255 // Called when the persistent notification close event has been handled |
| 256 // to remove the notification from the database. | 256 // to remove the notification from the database. |
| 257 void DeleteNotificationDataFromDatabase( | 257 void DeleteNotificationDataFromDatabase( |
| 258 const int64_t notification_id, | 258 const std::string& notification_id, |
| 259 const GURL& origin, | 259 const GURL& origin, |
| 260 const scoped_refptr<PlatformNotificationContext>& notification_context, | 260 const scoped_refptr<PlatformNotificationContext>& notification_context, |
| 261 const NotificationDispatchCompleteCallback& dispatch_complete_callback, | 261 const NotificationDispatchCompleteCallback& dispatch_complete_callback, |
| 262 ServiceWorkerStatusCode status_code) { | 262 ServiceWorkerStatusCode status_code) { |
| 263 notification_context->DeleteNotificationData( | 263 notification_context->DeleteNotificationData( |
| 264 notification_id, origin, | 264 notification_id, origin, |
| 265 base::Bind(&OnPersistentNotificationDataDeleted, status_code, | 265 base::Bind(&OnPersistentNotificationDataDeleted, status_code, |
| 266 dispatch_complete_callback)); | 266 dispatch_complete_callback)); |
| 267 } | 267 } |
| 268 | 268 |
| 269 // Dispatches the notificationclose event on |service_worker|. Must be called on | 269 // Dispatches the notificationclose event on |service_worker|. Must be called on |
| 270 // the IO thread, and with the worker running. | 270 // the IO thread, and with the worker running. |
| 271 void DispatchNotificationCloseEventOnWorker( | 271 void DispatchNotificationCloseEventOnWorker( |
| 272 const scoped_refptr<ServiceWorkerVersion>& service_worker, | 272 const scoped_refptr<ServiceWorkerVersion>& service_worker, |
| 273 const NotificationDatabaseData& notification_database_data, | 273 const NotificationDatabaseData& notification_database_data, |
| 274 const ServiceWorkerVersion::StatusCallback& callback) { | 274 const ServiceWorkerVersion::StatusCallback& callback) { |
| 275 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 275 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 276 int request_id = service_worker->StartRequest( | 276 int request_id = service_worker->StartRequest( |
| 277 ServiceWorkerMetrics::EventType::NOTIFICATION_CLOSE, callback); | 277 ServiceWorkerMetrics::EventType::NOTIFICATION_CLOSE, callback); |
| 278 service_worker->DispatchSimpleEvent< | 278 service_worker->DispatchSimpleEvent< |
| 279 ServiceWorkerHostMsg_NotificationCloseEventFinished>( | 279 ServiceWorkerHostMsg_NotificationCloseEventFinished>( |
| 280 request_id, ServiceWorkerMsg_NotificationCloseEvent( | 280 request_id, ServiceWorkerMsg_NotificationCloseEvent( |
| 281 request_id, notification_database_data.notification_id, | 281 request_id, notification_database_data.notification_id, |
| 282 notification_database_data.notification_data)); | 282 notification_database_data.notification_data)); |
| 283 } | 283 } |
| 284 | 284 |
| 285 // Actually dispatches the notification close event on the service worker | 285 // Actually dispatches the notification close event on the service worker |
| 286 // registration. | 286 // registration. |
| 287 void DoDispatchNotificationCloseEvent( | 287 void DoDispatchNotificationCloseEvent( |
| 288 const std::string& notification_id, |
| 288 bool by_user, | 289 bool by_user, |
| 289 const NotificationDispatchCompleteCallback& dispatch_complete_callback, | 290 const NotificationDispatchCompleteCallback& dispatch_complete_callback, |
| 290 const scoped_refptr<PlatformNotificationContext>& notification_context, | 291 const scoped_refptr<PlatformNotificationContext>& notification_context, |
| 291 const ServiceWorkerRegistration* service_worker_registration, | 292 const ServiceWorkerRegistration* service_worker_registration, |
| 292 const NotificationDatabaseData& notification_database_data) { | 293 const NotificationDatabaseData& notification_database_data) { |
| 293 const ServiceWorkerVersion::StatusCallback dispatch_event_callback = | 294 const ServiceWorkerVersion::StatusCallback dispatch_event_callback = |
| 294 base::Bind(&DeleteNotificationDataFromDatabase, | 295 base::Bind(&DeleteNotificationDataFromDatabase, notification_id, |
| 295 notification_database_data.notification_id, | |
| 296 notification_database_data.origin, notification_context, | 296 notification_database_data.origin, notification_context, |
| 297 dispatch_complete_callback); | 297 dispatch_complete_callback); |
| 298 if (by_user) { | 298 if (by_user) { |
| 299 service_worker_registration->active_version()->RunAfterStartWorker( | 299 service_worker_registration->active_version()->RunAfterStartWorker( |
| 300 ServiceWorkerMetrics::EventType::NOTIFICATION_CLOSE, | 300 ServiceWorkerMetrics::EventType::NOTIFICATION_CLOSE, |
| 301 base::Bind( | 301 base::Bind( |
| 302 &DispatchNotificationCloseEventOnWorker, | 302 &DispatchNotificationCloseEventOnWorker, |
| 303 make_scoped_refptr(service_worker_registration->active_version()), | 303 make_scoped_refptr(service_worker_registration->active_version()), |
| 304 notification_database_data, dispatch_event_callback), | 304 notification_database_data, dispatch_event_callback), |
| 305 dispatch_event_callback); | 305 dispatch_event_callback); |
| 306 } else { | 306 } else { |
| 307 dispatch_event_callback.Run(ServiceWorkerStatusCode::SERVICE_WORKER_OK); | 307 dispatch_event_callback.Run(ServiceWorkerStatusCode::SERVICE_WORKER_OK); |
| 308 } | 308 } |
| 309 } | 309 } |
| 310 | 310 |
| 311 // Dispatches any notification event. The actual, specific event dispatch should | 311 // Dispatches any notification event. The actual, specific event dispatch should |
| 312 // be done by the |notification_action_callback|. | 312 // be done by the |notification_action_callback|. |
| 313 void DispatchNotificationEvent( | 313 void DispatchNotificationEvent( |
| 314 BrowserContext* browser_context, | 314 BrowserContext* browser_context, |
| 315 int64_t persistent_notification_id, | 315 const std::string& notification_id, |
| 316 const GURL& origin, | 316 const GURL& origin, |
| 317 const NotificationOperationCallbackWithContext& | 317 const NotificationOperationCallbackWithContext& |
| 318 notification_action_callback, | 318 notification_action_callback, |
| 319 const NotificationDispatchCompleteCallback& notification_error_callback) { | 319 const NotificationDispatchCompleteCallback& notification_error_callback) { |
| 320 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 320 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 321 DCHECK_GT(persistent_notification_id, 0); | 321 DCHECK(!notification_id.empty()); |
| 322 DCHECK(origin.is_valid()); | 322 DCHECK(origin.is_valid()); |
| 323 | 323 |
| 324 StoragePartition* partition = | 324 StoragePartition* partition = |
| 325 BrowserContext::GetStoragePartitionForSite(browser_context, origin); | 325 BrowserContext::GetStoragePartitionForSite(browser_context, origin); |
| 326 | 326 |
| 327 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = | 327 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = |
| 328 static_cast<ServiceWorkerContextWrapper*>( | 328 static_cast<ServiceWorkerContextWrapper*>( |
| 329 partition->GetServiceWorkerContext()); | 329 partition->GetServiceWorkerContext()); |
| 330 scoped_refptr<PlatformNotificationContext> notification_context = | 330 scoped_refptr<PlatformNotificationContext> notification_context = |
| 331 partition->GetPlatformNotificationContext(); | 331 partition->GetPlatformNotificationContext(); |
| 332 | 332 |
| 333 BrowserThread::PostTask( | 333 BrowserThread::PostTask( |
| 334 BrowserThread::IO, FROM_HERE, | 334 BrowserThread::IO, FROM_HERE, |
| 335 base::Bind(&ReadNotificationDatabaseData, persistent_notification_id, | 335 base::Bind(&ReadNotificationDatabaseData, notification_id, origin, |
| 336 origin, service_worker_context, notification_context, | 336 service_worker_context, notification_context, |
| 337 base::Bind(notification_action_callback, notification_context), | 337 base::Bind(notification_action_callback, notification_context), |
| 338 notification_error_callback)); | 338 notification_error_callback)); |
| 339 } | 339 } |
| 340 | 340 |
| 341 } // namespace | 341 } // namespace |
| 342 | 342 |
| 343 // static | 343 // static |
| 344 NotificationEventDispatcher* NotificationEventDispatcher::GetInstance() { | 344 NotificationEventDispatcher* NotificationEventDispatcher::GetInstance() { |
| 345 return NotificationEventDispatcherImpl::GetInstance(); | 345 return NotificationEventDispatcherImpl::GetInstance(); |
| 346 } | 346 } |
| 347 | 347 |
| 348 NotificationEventDispatcherImpl* | 348 NotificationEventDispatcherImpl* |
| 349 NotificationEventDispatcherImpl::GetInstance() { | 349 NotificationEventDispatcherImpl::GetInstance() { |
| 350 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 350 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 351 return base::Singleton<NotificationEventDispatcherImpl>::get(); | 351 return base::Singleton<NotificationEventDispatcherImpl>::get(); |
| 352 } | 352 } |
| 353 | 353 |
| 354 NotificationEventDispatcherImpl::NotificationEventDispatcherImpl() {} | 354 NotificationEventDispatcherImpl::NotificationEventDispatcherImpl() {} |
| 355 | 355 |
| 356 NotificationEventDispatcherImpl::~NotificationEventDispatcherImpl() {} | 356 NotificationEventDispatcherImpl::~NotificationEventDispatcherImpl() {} |
| 357 | 357 |
| 358 void NotificationEventDispatcherImpl::DispatchNotificationClickEvent( | 358 void NotificationEventDispatcherImpl::DispatchNotificationClickEvent( |
| 359 BrowserContext* browser_context, | 359 BrowserContext* browser_context, |
| 360 int64_t persistent_notification_id, | 360 const std::string& notification_id, |
| 361 const GURL& origin, | 361 const GURL& origin, |
| 362 int action_index, | 362 int action_index, |
| 363 const NotificationDispatchCompleteCallback& dispatch_complete_callback) { | 363 const NotificationDispatchCompleteCallback& dispatch_complete_callback) { |
| 364 DispatchNotificationEvent( | 364 DispatchNotificationEvent( |
| 365 browser_context, persistent_notification_id, origin, | 365 browser_context, notification_id, origin, |
| 366 base::Bind(&DoDispatchNotificationClickEvent, action_index, | 366 base::Bind(&DoDispatchNotificationClickEvent, action_index, |
| 367 dispatch_complete_callback), | 367 dispatch_complete_callback), |
| 368 dispatch_complete_callback); | 368 dispatch_complete_callback); |
| 369 } | 369 } |
| 370 | 370 |
| 371 void NotificationEventDispatcherImpl::DispatchNotificationCloseEvent( | 371 void NotificationEventDispatcherImpl::DispatchNotificationCloseEvent( |
| 372 BrowserContext* browser_context, | 372 BrowserContext* browser_context, |
| 373 int64_t persistent_notification_id, | 373 const std::string& notification_id, |
| 374 const GURL& origin, | 374 const GURL& origin, |
| 375 bool by_user, | 375 bool by_user, |
| 376 const NotificationDispatchCompleteCallback& dispatch_complete_callback) { | 376 const NotificationDispatchCompleteCallback& dispatch_complete_callback) { |
| 377 DispatchNotificationEvent(browser_context, persistent_notification_id, origin, | 377 DispatchNotificationEvent( |
| 378 base::Bind(&DoDispatchNotificationCloseEvent, | 378 browser_context, notification_id, origin, |
| 379 by_user, dispatch_complete_callback), | 379 base::Bind(&DoDispatchNotificationCloseEvent, notification_id, by_user, |
| 380 dispatch_complete_callback); | 380 dispatch_complete_callback), |
| 381 dispatch_complete_callback); |
| 381 } | 382 } |
| 382 | 383 |
| 383 } // namespace content | 384 } // namespace content |
| OLD | NEW |