Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / browser / notifications / notification_event_dispatcher_impl.cc
blob204fea21ae61b7dbf875f1d638151438882572ef
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"
18 namespace content {
19 namespace {
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.
36 break;
37 case SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED:
38 status = PERSISTENT_NOTIFICATION_STATUS_EVENT_WAITUNTIL_REJECTED;
39 break;
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;
58 break;
61 BrowserThread::PostTask(BrowserThread::UI,
62 FROM_HERE,
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 int action_index,
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,
87 action_index);
88 return;
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;
95 break;
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;
114 break;
115 case SERVICE_WORKER_OK:
116 NOTREACHED();
117 break;
120 BrowserThread::PostTask(BrowserThread::UI,
121 FROM_HERE,
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(
128 const GURL& origin,
129 int action_index,
130 const NotificationClickDispatchCompleteCallback& dispatch_complete_callback,
131 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context,
132 bool success,
133 const NotificationDatabaseData& notification_database_data) {
134 DCHECK_CURRENTLY_ON(BrowserThread::IO);
135 if (!success) {
136 BrowserThread::PostTask(
137 BrowserThread::UI,
138 FROM_HERE,
139 base::Bind(dispatch_complete_callback,
140 PERSISTENT_NOTIFICATION_STATUS_DATABASE_ERROR));
141 return;
144 service_worker_context->FindRegistrationForId(
145 notification_database_data.service_worker_registration_id,
146 origin,
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,
156 const GURL& origin,
157 int action_index,
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,
164 origin,
165 base::Bind(&FindServiceWorkerRegistration,
166 origin, action_index, dispatch_complete_callback,
167 service_worker_context));
170 } // namespace
172 // static
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,
190 const GURL& origin,
191 int action_index,
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(
209 BrowserThread::IO,
210 FROM_HERE,
211 base::Bind(&ReadNotificationDatabaseData,
212 persistent_notification_id,
213 origin,
214 action_index,
215 dispatch_complete_callback,
216 service_worker_context,
217 notification_context));
220 } // namespace content