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 // Called when a ServiceWorkerHostMsg_NotificationClickEventFinished message is |
| 77 // received in response to an event that was dispatched. |
| 78 void OnNotificationClickEventReply( |
| 79 const scoped_refptr<ServiceWorkerVersion>& service_worker, |
| 80 const ServiceWorkerVersion::StatusCallback& callback, |
| 81 int request_id) { |
| 82 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 83 TRACE_EVENT1("ServiceWorker", |
| 84 "NotificationEventDispatcherImpl::OnNotificationClickEventReply", |
| 85 "Request id", request_id); |
| 86 if (!service_worker->FinishRequest(request_id)) |
| 87 NOTREACHED() << "Should only receive one reply per event"; |
| 88 callback.Run(SERVICE_WORKER_OK); |
| 89 } |
| 90 |
| 91 // Dispatches the notificationclick event on |service_worker|. Most be called on |
| 92 // the IO thread, and with the worker running. |
| 93 void DispatchNotificationClickEventOnWorker( |
| 94 const scoped_refptr<ServiceWorkerVersion>& service_worker, |
| 95 const NotificationDatabaseData& notification_database_data, |
| 96 int action_index, |
| 97 const ServiceWorkerVersion::StatusCallback& callback) { |
| 98 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 99 int request_id = service_worker->StartRequest( |
| 100 ServiceWorkerMetrics::EventType::NOTIFICATION_CLICK, callback); |
| 101 service_worker |
| 102 ->DispatchEvent<ServiceWorkerHostMsg_NotificationClickEventFinished>( |
| 103 request_id, |
| 104 ServiceWorkerMsg_NotificationClickEvent( |
| 105 request_id, notification_database_data.notification_id, |
| 106 notification_database_data.notification_data, action_index), |
| 107 base::Bind(&OnNotificationClickEventReply, service_worker, callback)); |
| 108 } |
| 109 |
75 // Dispatches the notificationclick on |service_worker_registration| if the | 110 // Dispatches the notificationclick on |service_worker_registration| if the |
76 // registration was available. Must be called on the IO thread. | 111 // registration was available. Must be called on the IO thread. |
77 void DispatchNotificationClickEventOnRegistration( | 112 void DispatchNotificationClickEventOnRegistration( |
78 const NotificationDatabaseData& notification_database_data, | 113 const NotificationDatabaseData& notification_database_data, |
79 int action_index, | 114 int action_index, |
80 const NotificationClickDispatchCompleteCallback& dispatch_complete_callback, | 115 const NotificationClickDispatchCompleteCallback& dispatch_complete_callback, |
81 ServiceWorkerStatusCode service_worker_status, | 116 ServiceWorkerStatusCode service_worker_status, |
82 const scoped_refptr<ServiceWorkerRegistration>& | 117 const scoped_refptr<ServiceWorkerRegistration>& |
83 service_worker_registration) { | 118 service_worker_registration) { |
84 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 119 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
85 #if defined(OS_ANDROID) | 120 #if defined(OS_ANDROID) |
86 // This LOG(INFO) deliberately exists to help track down the cause of | 121 // This LOG(INFO) deliberately exists to help track down the cause of |
87 // https://crbug.com/534537, where notifications sometimes do not react to | 122 // 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. | 123 // the user clicking on them. It should be removed once that's fixed. |
89 LOG(INFO) << "Trying to dispatch notification for SW with status: " | 124 LOG(INFO) << "Trying to dispatch notification for SW with status: " |
90 << service_worker_status << " action_index: " << action_index; | 125 << service_worker_status << " action_index: " << action_index; |
91 #endif | 126 #endif |
92 if (service_worker_status == SERVICE_WORKER_OK) { | 127 if (service_worker_status == SERVICE_WORKER_OK) { |
93 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = | 128 ServiceWorkerVersion::StatusCallback dispatch_event_callback = |
94 base::Bind(&NotificationClickEventFinished, dispatch_complete_callback, | 129 base::Bind(&NotificationClickEventFinished, dispatch_complete_callback, |
95 service_worker_registration); | 130 service_worker_registration); |
96 | 131 |
97 DCHECK(service_worker_registration->active_version()); | 132 DCHECK(service_worker_registration->active_version()); |
98 service_worker_registration->active_version() | 133 service_worker_registration->active_version()->RunAfterStartWorker( |
99 ->DispatchNotificationClickEvent( | 134 dispatch_event_callback, |
100 dispatch_event_callback, notification_database_data.notification_id, | 135 base::Bind( |
101 notification_database_data.notification_data, action_index); | 136 &DispatchNotificationClickEventOnWorker, |
| 137 make_scoped_refptr(service_worker_registration->active_version()), |
| 138 notification_database_data, action_index, dispatch_event_callback)); |
102 return; | 139 return; |
103 } | 140 } |
104 | 141 |
105 PersistentNotificationStatus status = PERSISTENT_NOTIFICATION_STATUS_SUCCESS; | 142 PersistentNotificationStatus status = PERSISTENT_NOTIFICATION_STATUS_SUCCESS; |
106 switch (service_worker_status) { | 143 switch (service_worker_status) { |
107 case SERVICE_WORKER_ERROR_NOT_FOUND: | 144 case SERVICE_WORKER_ERROR_NOT_FOUND: |
108 status = PERSISTENT_NOTIFICATION_STATUS_NO_SERVICE_WORKER; | 145 status = PERSISTENT_NOTIFICATION_STATUS_NO_SERVICE_WORKER; |
109 break; | 146 break; |
110 case SERVICE_WORKER_ERROR_FAILED: | 147 case SERVICE_WORKER_ERROR_FAILED: |
111 case SERVICE_WORKER_ERROR_ABORT: | 148 case SERVICE_WORKER_ERROR_ABORT: |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 partition->GetPlatformNotificationContext()); | 261 partition->GetPlatformNotificationContext()); |
225 | 262 |
226 BrowserThread::PostTask( | 263 BrowserThread::PostTask( |
227 BrowserThread::IO, FROM_HERE, | 264 BrowserThread::IO, FROM_HERE, |
228 base::Bind(&ReadNotificationDatabaseData, persistent_notification_id, | 265 base::Bind(&ReadNotificationDatabaseData, persistent_notification_id, |
229 origin, action_index, dispatch_complete_callback, | 266 origin, action_index, dispatch_complete_callback, |
230 service_worker_context, notification_context)); | 267 service_worker_context, notification_context)); |
231 } | 268 } |
232 | 269 |
233 } // namespace content | 270 } // namespace content |
OLD | NEW |