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_MAX_VALUE
:
56 status
= PERSISTENT_NOTIFICATION_STATUS_SERVICE_WORKER_ERROR
;
60 BrowserThread::PostTask(BrowserThread::UI
,
62 base::Bind(dispatch_complete_callback
, status
));
65 // Dispatches the notificationclick on |service_worker_registration| if the
66 // registration was available. Must be called on the IO thread.
67 void DispatchNotificationClickEventOnRegistration(
68 const NotificationDatabaseData
& notification_database_data
,
69 const NotificationClickDispatchCompleteCallback
& dispatch_complete_callback
,
70 ServiceWorkerStatusCode service_worker_status
,
71 const scoped_refptr
<ServiceWorkerRegistration
>&
72 service_worker_registration
) {
73 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
74 if (service_worker_status
== SERVICE_WORKER_OK
) {
75 base::Callback
<void(ServiceWorkerStatusCode
)> dispatch_event_callback
=
76 base::Bind(&NotificationClickEventFinished
,
77 dispatch_complete_callback
,
78 service_worker_registration
);
80 service_worker_registration
->active_version()->
81 DispatchNotificationClickEvent(
82 dispatch_event_callback
,
83 notification_database_data
.notification_id
,
84 notification_database_data
.notification_data
);
88 PersistentNotificationStatus status
= PERSISTENT_NOTIFICATION_STATUS_SUCCESS
;
89 switch (service_worker_status
) {
90 case SERVICE_WORKER_ERROR_NOT_FOUND
:
91 status
= PERSISTENT_NOTIFICATION_STATUS_NO_SERVICE_WORKER
;
93 case SERVICE_WORKER_ERROR_FAILED
:
94 case SERVICE_WORKER_ERROR_ABORT
:
95 case SERVICE_WORKER_ERROR_START_WORKER_FAILED
:
96 case SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND
:
97 case SERVICE_WORKER_ERROR_EXISTS
:
98 case SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED
:
99 case SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED
:
100 case SERVICE_WORKER_ERROR_IPC_FAILED
:
101 case SERVICE_WORKER_ERROR_NETWORK
:
102 case SERVICE_WORKER_ERROR_SECURITY
:
103 case SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED
:
104 case SERVICE_WORKER_ERROR_STATE
:
105 case SERVICE_WORKER_ERROR_TIMEOUT
:
106 case SERVICE_WORKER_ERROR_SCRIPT_EVALUATE_FAILED
:
107 case SERVICE_WORKER_ERROR_DISK_CACHE
:
108 case SERVICE_WORKER_ERROR_MAX_VALUE
:
109 status
= PERSISTENT_NOTIFICATION_STATUS_SERVICE_WORKER_ERROR
;
111 case SERVICE_WORKER_OK
:
116 BrowserThread::PostTask(BrowserThread::UI
,
118 base::Bind(dispatch_complete_callback
, status
));
121 // Finds the ServiceWorkerRegistration associated with the |origin| and
122 // |service_worker_registration_id|. Must be called on the IO thread.
123 void FindServiceWorkerRegistration(
125 const NotificationClickDispatchCompleteCallback
& dispatch_complete_callback
,
126 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
,
128 const NotificationDatabaseData
& notification_database_data
) {
129 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
131 BrowserThread::PostTask(
134 base::Bind(dispatch_complete_callback
,
135 PERSISTENT_NOTIFICATION_STATUS_DATABASE_ERROR
));
139 service_worker_context
->FindRegistrationForId(
140 notification_database_data
.service_worker_registration_id
,
142 base::Bind(&DispatchNotificationClickEventOnRegistration
,
143 notification_database_data
,
144 dispatch_complete_callback
));
147 // Reads the data associated with the |persistent_notification_id| belonging to
148 // |origin| from the notification context.
149 void ReadNotificationDatabaseData(
150 int64_t persistent_notification_id
,
152 const NotificationClickDispatchCompleteCallback
& dispatch_complete_callback
,
153 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
,
154 scoped_refptr
<PlatformNotificationContextImpl
> notification_context
) {
155 DCHECK_CURRENTLY_ON(BrowserThread::IO
);
156 notification_context
->ReadNotificationData(
157 persistent_notification_id
,
159 base::Bind(&FindServiceWorkerRegistration
,
160 origin
, dispatch_complete_callback
, service_worker_context
));
166 NotificationEventDispatcher
* NotificationEventDispatcher::GetInstance() {
167 return NotificationEventDispatcherImpl::GetInstance();
170 NotificationEventDispatcherImpl
*
171 NotificationEventDispatcherImpl::GetInstance() {
172 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
173 return Singleton
<NotificationEventDispatcherImpl
>::get();
176 NotificationEventDispatcherImpl::NotificationEventDispatcherImpl() {}
178 NotificationEventDispatcherImpl::~NotificationEventDispatcherImpl() {}
180 void NotificationEventDispatcherImpl::DispatchNotificationClickEvent(
181 BrowserContext
* browser_context
,
182 int64_t persistent_notification_id
,
184 const NotificationClickDispatchCompleteCallback
&
185 dispatch_complete_callback
) {
186 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
187 DCHECK_GT(persistent_notification_id
, 0);
188 DCHECK(origin
.is_valid());
190 StoragePartition
* partition
=
191 BrowserContext::GetStoragePartitionForSite(browser_context
, origin
);
193 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context
=
194 static_cast<ServiceWorkerContextWrapper
*>(
195 partition
->GetServiceWorkerContext());
196 scoped_refptr
<PlatformNotificationContextImpl
> notification_context
=
197 static_cast<PlatformNotificationContextImpl
*>(
198 partition
->GetPlatformNotificationContext());
200 BrowserThread::PostTask(
203 base::Bind(&ReadNotificationDatabaseData
,
204 persistent_notification_id
,
206 dispatch_complete_callback
,
207 service_worker_context
,
208 notification_context
));
211 } // namespace content