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
3 // found in the LICENSE file.
5 #include "content/browser/notifications/notification_event_dispatcher_impl.h"
7 #include "base/callback.h"
8 #include "content/browser/notifications/platform_notification_context_impl.h"
9 #include "content/browser/service_worker/service_worker_context_wrapper.h"
10 #include "content/browser/service_worker/service_worker_registration.h"
11 #include "content/browser/service_worker/service_worker_storage.h"
12 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/notification_database_data.h"
15 #include "content/public/browser/storage_partition.h"
16 #include "content/public/common/platform_notification_data.h"
21 using NotificationClickDispatchCompleteCallback
=
22 NotificationEventDispatcher::NotificationClickDispatchCompleteCallback
;
24 // To be called when the notificationclick event has finished executing. Will
25 // post a task to call |dispatch_complete_callback| on the UI thread.
26 void NotificationClickEventFinished(
27 const NotificationClickDispatchCompleteCallback
& dispatch_complete_callback
,
28 const scoped_refptr
<ServiceWorkerRegistration
>& service_worker_registration
,
29 ServiceWorkerStatusCode service_worker_status
) {
30 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
32 PersistentNotificationStatus status
= PERSISTENT_NOTIFICATION_STATUS_SUCCESS
;
33 switch (service_worker_status
) {
34 case SERVICE_WORKER_OK
:
35 // Success status was initialized above.
37 case SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED
:
38 status
= PERSISTENT_NOTIFICATION_STATUS_EVENT_WAITUNTIL_REJECTED
;
40 case SERVICE_WORKER_ERROR_FAILED
:
41 case SERVICE_WORKER_ERROR_ABORT
:
42 case SERVICE_WORKER_ERROR_START_WORKER_FAILED
:
43 case SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND
:
44 case SERVICE_WORKER_ERROR_NOT_FOUND
:
45 case SERVICE_WORKER_ERROR_EXISTS
:
46 case SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED
:
47 case SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED
:
48 case SERVICE_WORKER_ERROR_IPC_FAILED
:
49 case SERVICE_WORKER_ERROR_NETWORK
:
50 case SERVICE_WORKER_ERROR_SECURITY
:
51 case SERVICE_WORKER_ERROR_STATE
:
52 case SERVICE_WORKER_ERROR_TIMEOUT
:
53 case SERVICE_WORKER_ERROR_SCRIPT_EVALUATE_FAILED
:
54 case SERVICE_WORKER_ERROR_DISK_CACHE
:
55 case SERVICE_WORKER_ERROR_REDUNDANT
:
56 case SERVICE_WORKER_ERROR_MAX_VALUE
:
57 status
= PERSISTENT_NOTIFICATION_STATUS_SERVICE_WORKER_ERROR
;
61 BrowserThread::PostTask(BrowserThread::UI
,
63 base::Bind(dispatch_complete_callback
, status
));
66 // Dispatches the notificationclick on |service_worker_registration| if the
67 // registration was available. Must be called on the IO thread.
68 void DispatchNotificationClickEventOnRegistration(
69 const NotificationDatabaseData
& notification_database_data
,
71 const NotificationClickDispatchCompleteCallback
& dispatch_complete_callback
,
72 ServiceWorkerStatusCode service_worker_status
,
73 const scoped_refptr
<ServiceWorkerRegistration
>&
74 service_worker_registration
) {
75 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
76 if (service_worker_status
== SERVICE_WORKER_OK
) {
77 base::Callback
<void(ServiceWorkerStatusCode
)> dispatch_event_callback
=
78 base::Bind(&NotificationClickEventFinished
,
79 dispatch_complete_callback
,
80 service_worker_registration
);
82 service_worker_registration
->active_version()->
83 DispatchNotificationClickEvent(
84 dispatch_event_callback
,
85 notification_database_data
.notification_id
,
86 notification_database_data
.notification_data
,
91 PersistentNotificationStatus status
= PERSISTENT_NOTIFICATION_STATUS_SUCCESS
;
92 switch (service_worker_status
) {
93 case SERVICE_WORKER_ERROR_NOT_FOUND
:
94 status
= PERSISTENT_NOTIFICATION_STATUS_NO_SERVICE_WORKER
;
96 case SERVICE_WORKER_ERROR_FAILED
:
97 case SERVICE_WORKER_ERROR_ABORT
:
98 case SERVICE_WORKER_ERROR_START_WORKER_FAILED
:
99 case SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND
:
100 case SERVICE_WORKER_ERROR_EXISTS
:
101 case SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED
:
102 case SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED
:
103 case SERVICE_WORKER_ERROR_IPC_FAILED
:
104 case SERVICE_WORKER_ERROR_NETWORK
:
105 case SERVICE_WORKER_ERROR_SECURITY
:
106 case SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED
:
107 case SERVICE_WORKER_ERROR_STATE
:
108 case SERVICE_WORKER_ERROR_TIMEOUT
:
109 case SERVICE_WORKER_ERROR_SCRIPT_EVALUATE_FAILED
:
110 case SERVICE_WORKER_ERROR_DISK_CACHE
:
111 case SERVICE_WORKER_ERROR_REDUNDANT
:
112 case SERVICE_WORKER_ERROR_MAX_VALUE
:
113 status
= PERSISTENT_NOTIFICATION_STATUS_SERVICE_WORKER_ERROR
;
115 case SERVICE_WORKER_OK
:
120 BrowserThread::PostTask(BrowserThread::UI
,
122 base::Bind(dispatch_complete_callback
, status
));
125 // Finds the ServiceWorkerRegistration associated with the |origin| and
126 // |service_worker_registration_id|. Must be called on the IO thread.
127 void FindServiceWorkerRegistration(
130 const NotificationClickDispatchCompleteCallback
& dispatch_complete_callback
,
131 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
,
133 const NotificationDatabaseData
& notification_database_data
) {
134 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
136 BrowserThread::PostTask(
139 base::Bind(dispatch_complete_callback
,
140 PERSISTENT_NOTIFICATION_STATUS_DATABASE_ERROR
));
144 service_worker_context
->FindRegistrationForId(
145 notification_database_data
.service_worker_registration_id
,
147 base::Bind(&DispatchNotificationClickEventOnRegistration
,
148 notification_database_data
, action_index
,
149 dispatch_complete_callback
));
152 // Reads the data associated with the |persistent_notification_id| belonging to
153 // |origin| from the notification context.
154 void ReadNotificationDatabaseData(
155 int64_t persistent_notification_id
,
158 const NotificationClickDispatchCompleteCallback
& dispatch_complete_callback
,
159 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
,
160 scoped_refptr
<PlatformNotificationContextImpl
> notification_context
) {
161 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
162 notification_context
->ReadNotificationData(
163 persistent_notification_id
,
165 base::Bind(&FindServiceWorkerRegistration
,
166 origin
, action_index
, dispatch_complete_callback
,
167 service_worker_context
));
173 NotificationEventDispatcher
* NotificationEventDispatcher::GetInstance() {
174 return NotificationEventDispatcherImpl::GetInstance();
177 NotificationEventDispatcherImpl
*
178 NotificationEventDispatcherImpl::GetInstance() {
179 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
180 return Singleton
<NotificationEventDispatcherImpl
>::get();
183 NotificationEventDispatcherImpl::NotificationEventDispatcherImpl() {}
185 NotificationEventDispatcherImpl::~NotificationEventDispatcherImpl() {}
187 void NotificationEventDispatcherImpl::DispatchNotificationClickEvent(
188 BrowserContext
* browser_context
,
189 int64_t persistent_notification_id
,
192 const NotificationClickDispatchCompleteCallback
&
193 dispatch_complete_callback
) {
194 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
195 DCHECK_GT(persistent_notification_id
, 0);
196 DCHECK(origin
.is_valid());
198 StoragePartition
* partition
=
199 BrowserContext::GetStoragePartitionForSite(browser_context
, origin
);
201 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
=
202 static_cast<ServiceWorkerContextWrapper
*>(
203 partition
->GetServiceWorkerContext());
204 scoped_refptr
<PlatformNotificationContextImpl
> notification_context
=
205 static_cast<PlatformNotificationContextImpl
*>(
206 partition
->GetPlatformNotificationContext());
208 BrowserThread::PostTask(
211 base::Bind(&ReadNotificationDatabaseData
,
212 persistent_notification_id
,
215 dispatch_complete_callback
,
216 service_worker_context
,
217 notification_context
));
220 } // namespace content