Chromium Code Reviews| 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" |
| 11 #include "content/browser/service_worker/service_worker_registration.h" | 11 #include "content/browser/service_worker/service_worker_registration.h" |
| 12 #include "content/browser/service_worker/service_worker_storage.h" | 12 #include "content/browser/service_worker/service_worker_storage.h" |
| 13 #include "content/common/service_worker/service_worker_messages.h" | |
| 13 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
| 14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/notification_database_data.h" | 16 #include "content/public/browser/notification_database_data.h" |
| 16 #include "content/public/browser/storage_partition.h" | 17 #include "content/public/browser/storage_partition.h" |
| 17 #include "content/public/common/platform_notification_data.h" | 18 #include "content/public/common/platform_notification_data.h" |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 using NotificationClickDispatchCompleteCallback = | 23 using NotificationClickDispatchCompleteCallback = |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 case SERVICE_WORKER_ERROR_DISALLOWED: | 66 case SERVICE_WORKER_ERROR_DISALLOWED: |
| 66 case SERVICE_WORKER_ERROR_MAX_VALUE: | 67 case SERVICE_WORKER_ERROR_MAX_VALUE: |
| 67 status = PERSISTENT_NOTIFICATION_STATUS_SERVICE_WORKER_ERROR; | 68 status = PERSISTENT_NOTIFICATION_STATUS_SERVICE_WORKER_ERROR; |
| 68 break; | 69 break; |
| 69 } | 70 } |
| 70 | 71 |
| 71 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 72 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 72 base::Bind(dispatch_complete_callback, status)); | 73 base::Bind(dispatch_complete_callback, status)); |
| 73 } | 74 } |
| 74 | 75 |
| 76 void OnNotificationClickEventReply( | |
|
Michael van Ouwerkerk
2016/01/15 16:44:03
nit: add some documentation like the other functio
Marijn Kruisselbrink
2016/01/15 23:48:49
Done
| |
| 77 const scoped_refptr<ServiceWorkerVersion>& service_worker, | |
| 78 const base::Callback<void(ServiceWorkerStatusCode)>& callback, | |
|
johnme
2016/01/15 16:52:58
Nit: may as well use `const ServiceWorkerVersion::
Marijn Kruisselbrink
2016/01/15 23:48:49
Agreed; not sure why the code in DispatchNotificat
| |
| 79 int request_id) { | |
|
Michael van Ouwerkerk
2016/01/15 16:44:03
nit: DCHECK_CURRENTLY_ON like all other functions
Marijn Kruisselbrink
2016/01/15 23:48:49
Done
| |
| 80 TRACE_EVENT1("ServiceWorker", | |
| 81 "NotificationEventDispatcherImpl::OnNotificationClickEventReply", | |
| 82 "Request id", request_id); | |
| 83 service_worker->FinishRequest(request_id); | |
|
johnme
2016/01/15 16:52:58
Please NOTREACHED() if this returns false.
Marijn Kruisselbrink
2016/01/15 23:48:49
Done
| |
| 84 callback.Run(SERVICE_WORKER_OK); | |
|
johnme
2016/01/15 16:52:58
The previous code did the following:
scoped_refpt
Marijn Kruisselbrink
2016/01/15 23:48:49
If I understand it correctly, the old code made su
johnme
2016/01/18 16:31:05
You're right - the (somewhat awkward) pattern is f
| |
| 85 } | |
| 86 | |
| 87 void DispatchNotificationClickEventOnWorker( | |
|
Michael van Ouwerkerk
2016/01/15 16:44:03
nit: add some documentation like the other functio
Marijn Kruisselbrink
2016/01/15 23:48:49
Done
| |
| 88 const scoped_refptr<ServiceWorkerVersion>& service_worker, | |
| 89 const NotificationDatabaseData& notification_database_data, | |
| 90 int action_index, | |
| 91 const base::Callback<void(ServiceWorkerStatusCode)>& callback) { | |
|
Michael van Ouwerkerk
2016/01/15 16:44:03
nit: DCHECK_CURRENTLY_ON like all other functions
johnme
2016/01/15 16:52:58
Ditto `const ServiceWorkerVersion::StatusCallback&
Marijn Kruisselbrink
2016/01/15 23:48:49
Done
| |
| 92 int request_id = service_worker->StartRequest( | |
| 93 ServiceWorkerMetrics::EventType::NOTIFICATION_CLICK, callback); | |
| 94 service_worker | |
| 95 ->DispatchEvent<ServiceWorkerHostMsg_NotificationClickEventFinished>( | |
| 96 request_id, | |
| 97 ServiceWorkerMsg_NotificationClickEvent( | |
| 98 request_id, notification_database_data.notification_id, | |
| 99 notification_database_data.notification_data, action_index), | |
| 100 base::Bind(&OnNotificationClickEventReply, service_worker, callback)); | |
| 101 } | |
| 102 | |
| 75 // Dispatches the notificationclick on |service_worker_registration| if the | 103 // Dispatches the notificationclick on |service_worker_registration| if the |
| 76 // registration was available. Must be called on the IO thread. | 104 // registration was available. Must be called on the IO thread. |
| 77 void DispatchNotificationClickEventOnRegistration( | 105 void DispatchNotificationClickEventOnRegistration( |
| 78 const NotificationDatabaseData& notification_database_data, | 106 const NotificationDatabaseData& notification_database_data, |
| 79 int action_index, | 107 int action_index, |
| 80 const NotificationClickDispatchCompleteCallback& dispatch_complete_callback, | 108 const NotificationClickDispatchCompleteCallback& dispatch_complete_callback, |
| 81 ServiceWorkerStatusCode service_worker_status, | 109 ServiceWorkerStatusCode service_worker_status, |
| 82 const scoped_refptr<ServiceWorkerRegistration>& | 110 const scoped_refptr<ServiceWorkerRegistration>& |
| 83 service_worker_registration) { | 111 service_worker_registration) { |
| 84 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 112 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 85 #if defined(OS_ANDROID) | 113 #if defined(OS_ANDROID) |
| 86 // This LOG(INFO) deliberately exists to help track down the cause of | 114 // This LOG(INFO) deliberately exists to help track down the cause of |
| 87 // https://crbug.com/534537, where notifications sometimes do not react to | 115 // https://crbug.com/534537, where notifications sometimes do not react to |
| 88 // the user clicking on them. It should be removed once that's fixed. | 116 // the user clicking on them. It should be removed once that's fixed. |
| 89 LOG(INFO) << "Trying to dispatch notification for SW with status: " | 117 LOG(INFO) << "Trying to dispatch notification for SW with status: " |
| 90 << service_worker_status << " action_index: " << action_index; | 118 << service_worker_status << " action_index: " << action_index; |
| 91 #endif | 119 #endif |
| 92 if (service_worker_status == SERVICE_WORKER_OK) { | 120 if (service_worker_status == SERVICE_WORKER_OK) { |
| 93 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = | 121 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = |
| 94 base::Bind(&NotificationClickEventFinished, dispatch_complete_callback, | 122 base::Bind(&NotificationClickEventFinished, dispatch_complete_callback, |
| 95 service_worker_registration); | 123 service_worker_registration); |
| 96 | 124 |
| 97 DCHECK(service_worker_registration->active_version()); | 125 DCHECK(service_worker_registration->active_version()); |
| 98 service_worker_registration->active_version() | 126 service_worker_registration->active_version()->RunAfterStartWorker( |
|
johnme
2016/01/15 16:52:58
Is it ok to call RunAfterStartWorker (and hence St
Marijn Kruisselbrink
2016/01/15 23:48:49
Good question; I don't think the side effects will
| |
| 99 ->DispatchNotificationClickEvent( | 127 dispatch_event_callback, |
|
Michael van Ouwerkerk
2016/01/15 16:44:03
nit: I would expect the error callback argument to
Marijn Kruisselbrink
2016/01/15 23:48:49
Changed this order in https://codereview.chromium.
| |
| 100 dispatch_event_callback, notification_database_data.notification_id, | 128 base::Bind( |
| 101 notification_database_data.notification_data, action_index); | 129 &DispatchNotificationClickEventOnWorker, |
| 130 make_scoped_refptr(service_worker_registration->active_version()), | |
| 131 notification_database_data, action_index, dispatch_event_callback)); | |
| 102 return; | 132 return; |
| 103 } | 133 } |
| 104 | 134 |
| 105 PersistentNotificationStatus status = PERSISTENT_NOTIFICATION_STATUS_SUCCESS; | 135 PersistentNotificationStatus status = PERSISTENT_NOTIFICATION_STATUS_SUCCESS; |
| 106 switch (service_worker_status) { | 136 switch (service_worker_status) { |
| 107 case SERVICE_WORKER_ERROR_NOT_FOUND: | 137 case SERVICE_WORKER_ERROR_NOT_FOUND: |
| 108 status = PERSISTENT_NOTIFICATION_STATUS_NO_SERVICE_WORKER; | 138 status = PERSISTENT_NOTIFICATION_STATUS_NO_SERVICE_WORKER; |
| 109 break; | 139 break; |
| 110 case SERVICE_WORKER_ERROR_FAILED: | 140 case SERVICE_WORKER_ERROR_FAILED: |
| 111 case SERVICE_WORKER_ERROR_ABORT: | 141 case SERVICE_WORKER_ERROR_ABORT: |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 224 partition->GetPlatformNotificationContext()); | 254 partition->GetPlatformNotificationContext()); |
| 225 | 255 |
| 226 BrowserThread::PostTask( | 256 BrowserThread::PostTask( |
| 227 BrowserThread::IO, FROM_HERE, | 257 BrowserThread::IO, FROM_HERE, |
| 228 base::Bind(&ReadNotificationDatabaseData, persistent_notification_id, | 258 base::Bind(&ReadNotificationDatabaseData, persistent_notification_id, |
| 229 origin, action_index, dispatch_complete_callback, | 259 origin, action_index, dispatch_complete_callback, |
| 230 service_worker_context, notification_context)); | 260 service_worker_context, notification_context)); |
| 231 } | 261 } |
| 232 | 262 |
| 233 } // namespace content | 263 } // namespace content |
| OLD | NEW |