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
,
70 const NotificationClickDispatchCompleteCallback
& dispatch_complete_callback
,
71 ServiceWorkerStatusCode service_worker_status
,
72 const scoped_refptr
<ServiceWorkerRegistration
>&
73 service_worker_registration
) {
74 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
75 if (service_worker_status
== SERVICE_WORKER_OK
) {
76 base::Callback
<void(ServiceWorkerStatusCode
)> dispatch_event_callback
=
77 base::Bind(&NotificationClickEventFinished
,
78 dispatch_complete_callback
,
79 service_worker_registration
);
81 service_worker_registration
->active_version()->
82 DispatchNotificationClickEvent(
83 dispatch_event_callback
,
84 notification_database_data
.notification_id
,
85 notification_database_data
.notification_data
);
89 PersistentNotificationStatus status
= PERSISTENT_NOTIFICATION_STATUS_SUCCESS
;
90 switch (service_worker_status
) {
91 case SERVICE_WORKER_ERROR_NOT_FOUND
:
92 status
= PERSISTENT_NOTIFICATION_STATUS_NO_SERVICE_WORKER
;
94 case SERVICE_WORKER_ERROR_FAILED
:
95 case SERVICE_WORKER_ERROR_ABORT
:
96 case SERVICE_WORKER_ERROR_START_WORKER_FAILED
:
97 case SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND
:
98 case SERVICE_WORKER_ERROR_EXISTS
:
99 case SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED
:
100 case SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED
:
101 case SERVICE_WORKER_ERROR_IPC_FAILED
:
102 case SERVICE_WORKER_ERROR_NETWORK
:
103 case SERVICE_WORKER_ERROR_SECURITY
:
104 case SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED
:
105 case SERVICE_WORKER_ERROR_STATE
:
106 case SERVICE_WORKER_ERROR_TIMEOUT
:
107 case SERVICE_WORKER_ERROR_SCRIPT_EVALUATE_FAILED
:
108 case SERVICE_WORKER_ERROR_DISK_CACHE
:
109 case SERVICE_WORKER_ERROR_REDUNDANT
:
110 case SERVICE_WORKER_ERROR_MAX_VALUE
:
111 status
= PERSISTENT_NOTIFICATION_STATUS_SERVICE_WORKER_ERROR
;
113 case SERVICE_WORKER_OK
:
118 BrowserThread::PostTask(BrowserThread::UI
,
120 base::Bind(dispatch_complete_callback
, status
));
123 // Finds the ServiceWorkerRegistration associated with the |origin| and
124 // |service_worker_registration_id|. Must be called on the IO thread.
125 void FindServiceWorkerRegistration(
127 const NotificationClickDispatchCompleteCallback
& dispatch_complete_callback
,
128 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
,
130 const NotificationDatabaseData
& notification_database_data
) {
131 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
133 BrowserThread::PostTask(
136 base::Bind(dispatch_complete_callback
,
137 PERSISTENT_NOTIFICATION_STATUS_DATABASE_ERROR
));
141 service_worker_context
->FindRegistrationForId(
142 notification_database_data
.service_worker_registration_id
,
144 base::Bind(&DispatchNotificationClickEventOnRegistration
,
145 notification_database_data
,
146 dispatch_complete_callback
));
149 // Reads the data associated with the |persistent_notification_id| belonging to
150 // |origin| from the notification context.
151 void ReadNotificationDatabaseData(
152 int64_t persistent_notification_id
,
154 const NotificationClickDispatchCompleteCallback
& dispatch_complete_callback
,
155 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
,
156 scoped_refptr
<PlatformNotificationContextImpl
> notification_context
) {
157 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
158 notification_context
->ReadNotificationData(
159 persistent_notification_id
,
161 base::Bind(&FindServiceWorkerRegistration
,
162 origin
, dispatch_complete_callback
, service_worker_context
));
168 NotificationEventDispatcher
* NotificationEventDispatcher::GetInstance() {
169 return NotificationEventDispatcherImpl::GetInstance();
172 NotificationEventDispatcherImpl
*
173 NotificationEventDispatcherImpl::GetInstance() {
174 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
175 return Singleton
<NotificationEventDispatcherImpl
>::get();
178 NotificationEventDispatcherImpl::NotificationEventDispatcherImpl() {}
180 NotificationEventDispatcherImpl::~NotificationEventDispatcherImpl() {}
182 void NotificationEventDispatcherImpl::DispatchNotificationClickEvent(
183 BrowserContext
* browser_context
,
184 int64_t persistent_notification_id
,
186 const NotificationClickDispatchCompleteCallback
&
187 dispatch_complete_callback
) {
188 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
189 DCHECK_GT(persistent_notification_id
, 0);
190 DCHECK(origin
.is_valid());
192 StoragePartition
* partition
=
193 BrowserContext::GetStoragePartitionForSite(browser_context
, origin
);
195 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
=
196 static_cast<ServiceWorkerContextWrapper
*>(
197 partition
->GetServiceWorkerContext());
198 scoped_refptr
<PlatformNotificationContextImpl
> notification_context
=
199 static_cast<PlatformNotificationContextImpl
*>(
200 partition
->GetPlatformNotificationContext());
202 BrowserThread::PostTask(
205 base::Bind(&ReadNotificationDatabaseData
,
206 persistent_notification_id
,
208 dispatch_complete_callback
,
209 service_worker_context
,
210 notification_context
));
213 } // namespace content