peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "content/browser/notifications/notification_event_dispatcher_impl.h" |
| 6 | |
| 7 | #include "base/callback.h" |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 8 | #include "base/strings/string_number_conversions.h" |
| 9 | #include "content/browser/notifications/platform_notification_context_impl.h" |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 10 | #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 11 | #include "content/browser/service_worker/service_worker_registration.h" |
| 12 | #include "content/browser/service_worker/service_worker_storage.h" |
| 13 | #include "content/public/browser/browser_context.h" |
| 14 | #include "content/public/browser/browser_thread.h" |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 15 | #include "content/public/browser/notification_database_data.h" |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 16 | #include "content/public/browser/storage_partition.h" |
peter | 7329c2c | 2014-12-12 14:42:59 | [diff] [blame] | 17 | #include "content/public/common/platform_notification_data.h" |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 18 | |
| 19 | namespace content { |
| 20 | namespace { |
| 21 | |
| 22 | using NotificationClickDispatchCompleteCallback = |
| 23 | NotificationEventDispatcher::NotificationClickDispatchCompleteCallback; |
| 24 | |
| 25 | // To be called when the notificationclick event has finished executing. Will |
| 26 | // post a task to call |dispatch_complete_callback| on the UI thread. |
| 27 | void NotificationClickEventFinished( |
| 28 | const NotificationClickDispatchCompleteCallback& dispatch_complete_callback, |
| 29 | const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
| 30 | ServiceWorkerStatusCode service_worker_status) { |
| 31 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 32 | |
| 33 | PersistentNotificationStatus status = PERSISTENT_NOTIFICATION_STATUS_SUCCESS; |
| 34 | switch (service_worker_status) { |
| 35 | case SERVICE_WORKER_OK: |
| 36 | // Success status was initialized above. |
| 37 | break; |
| 38 | case SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED: |
| 39 | status = PERSISTENT_NOTIFICATION_STATUS_EVENT_WAITUNTIL_REJECTED; |
| 40 | break; |
| 41 | case SERVICE_WORKER_ERROR_FAILED: |
| 42 | case SERVICE_WORKER_ERROR_ABORT: |
| 43 | case SERVICE_WORKER_ERROR_START_WORKER_FAILED: |
| 44 | case SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND: |
| 45 | case SERVICE_WORKER_ERROR_NOT_FOUND: |
| 46 | case SERVICE_WORKER_ERROR_EXISTS: |
| 47 | case SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED: |
| 48 | case SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED: |
| 49 | case SERVICE_WORKER_ERROR_IPC_FAILED: |
| 50 | case SERVICE_WORKER_ERROR_NETWORK: |
| 51 | case SERVICE_WORKER_ERROR_SECURITY: |
xiang.long | afc4f16f | 2015-02-04 07:04:57 | [diff] [blame] | 52 | case SERVICE_WORKER_ERROR_STATE: |
falken | 292e84a | 2015-03-05 16:54:25 | [diff] [blame] | 53 | case SERVICE_WORKER_ERROR_TIMEOUT: |
| 54 | case SERVICE_WORKER_ERROR_MAX_VALUE: |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 55 | status = PERSISTENT_NOTIFICATION_STATUS_SERVICE_WORKER_ERROR; |
| 56 | break; |
| 57 | } |
| 58 | |
| 59 | BrowserThread::PostTask(BrowserThread::UI, |
| 60 | FROM_HERE, |
| 61 | base::Bind(dispatch_complete_callback, status)); |
| 62 | } |
| 63 | |
| 64 | // Dispatches the notificationclick on |service_worker_registration| if the |
| 65 | // registration was available. Must be called on the IO thread. |
| 66 | void DispatchNotificationClickEventOnRegistration( |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 67 | const NotificationDatabaseData& notification_database_data, |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 68 | const NotificationClickDispatchCompleteCallback& dispatch_complete_callback, |
| 69 | ServiceWorkerStatusCode service_worker_status, |
| 70 | const scoped_refptr<ServiceWorkerRegistration>& |
| 71 | service_worker_registration) { |
| 72 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 73 | if (service_worker_status == SERVICE_WORKER_OK) { |
| 74 | base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = |
| 75 | base::Bind(&NotificationClickEventFinished, |
| 76 | dispatch_complete_callback, |
| 77 | service_worker_registration); |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 78 | |
| 79 | // TODO(peter): Pass the persistent notification id as an int64_t rather |
| 80 | // than as a string. This depends on the Blink API being updated. |
| 81 | std::string persistent_notification_id_string = |
| 82 | base::Int64ToString(notification_database_data.notification_id); |
| 83 | |
| 84 | service_worker_registration->active_version()-> |
| 85 | DispatchNotificationClickEvent( |
| 86 | dispatch_event_callback, |
| 87 | persistent_notification_id_string, |
| 88 | notification_database_data.notification_data); |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 89 | return; |
| 90 | } |
| 91 | |
| 92 | PersistentNotificationStatus status = PERSISTENT_NOTIFICATION_STATUS_SUCCESS; |
| 93 | switch (service_worker_status) { |
| 94 | case SERVICE_WORKER_ERROR_NOT_FOUND: |
| 95 | status = PERSISTENT_NOTIFICATION_STATUS_NO_SERVICE_WORKER; |
| 96 | break; |
| 97 | case SERVICE_WORKER_ERROR_FAILED: |
| 98 | case SERVICE_WORKER_ERROR_ABORT: |
| 99 | case SERVICE_WORKER_ERROR_START_WORKER_FAILED: |
| 100 | case SERVICE_WORKER_ERROR_PROCESS_NOT_FOUND: |
| 101 | case SERVICE_WORKER_ERROR_EXISTS: |
| 102 | case SERVICE_WORKER_ERROR_INSTALL_WORKER_FAILED: |
| 103 | case SERVICE_WORKER_ERROR_ACTIVATE_WORKER_FAILED: |
| 104 | case SERVICE_WORKER_ERROR_IPC_FAILED: |
| 105 | case SERVICE_WORKER_ERROR_NETWORK: |
| 106 | case SERVICE_WORKER_ERROR_SECURITY: |
| 107 | case SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED: |
xiang.long | afc4f16f | 2015-02-04 07:04:57 | [diff] [blame] | 108 | case SERVICE_WORKER_ERROR_STATE: |
falken | 292e84a | 2015-03-05 16:54:25 | [diff] [blame] | 109 | case SERVICE_WORKER_ERROR_TIMEOUT: |
| 110 | case SERVICE_WORKER_ERROR_MAX_VALUE: |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 111 | status = PERSISTENT_NOTIFICATION_STATUS_SERVICE_WORKER_ERROR; |
| 112 | break; |
| 113 | case SERVICE_WORKER_OK: |
| 114 | NOTREACHED(); |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | BrowserThread::PostTask(BrowserThread::UI, |
| 119 | FROM_HERE, |
| 120 | base::Bind(dispatch_complete_callback, status)); |
| 121 | } |
| 122 | |
| 123 | // Finds the ServiceWorkerRegistration associated with the |origin| and |
| 124 | // |service_worker_registration_id|. Must be called on the IO thread. |
| 125 | void FindServiceWorkerRegistration( |
| 126 | const GURL& origin, |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 127 | const NotificationClickDispatchCompleteCallback& dispatch_complete_callback, |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 128 | scoped_refptr<ServiceWorkerContextWrapper> service_worker_context, |
| 129 | bool success, |
| 130 | const NotificationDatabaseData& notification_database_data) { |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 131 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 132 | if (!success) { |
| 133 | BrowserThread::PostTask( |
| 134 | BrowserThread::UI, |
| 135 | FROM_HERE, |
| 136 | base::Bind(dispatch_complete_callback, |
| 137 | PERSISTENT_NOTIFICATION_STATUS_DATABASE_ERROR)); |
| 138 | return; |
| 139 | } |
| 140 | |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 141 | service_worker_context->context()->storage()->FindRegistrationForId( |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 142 | notification_database_data.service_worker_registration_id, |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 143 | origin, |
| 144 | base::Bind(&DispatchNotificationClickEventOnRegistration, |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 145 | notification_database_data, |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 146 | dispatch_complete_callback)); |
| 147 | } |
| 148 | |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 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, |
| 153 | const GURL& origin, |
| 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, |
| 160 | origin, |
| 161 | base::Bind(&FindServiceWorkerRegistration, |
| 162 | origin, dispatch_complete_callback, service_worker_context)); |
| 163 | } |
| 164 | |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 165 | } // namespace |
| 166 | |
| 167 | // static |
| 168 | NotificationEventDispatcher* NotificationEventDispatcher::GetInstance() { |
| 169 | return NotificationEventDispatcherImpl::GetInstance(); |
| 170 | } |
| 171 | |
| 172 | NotificationEventDispatcherImpl* |
| 173 | NotificationEventDispatcherImpl::GetInstance() { |
| 174 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 175 | return Singleton<NotificationEventDispatcherImpl>::get(); |
| 176 | } |
| 177 | |
| 178 | NotificationEventDispatcherImpl::NotificationEventDispatcherImpl() {} |
| 179 | |
| 180 | NotificationEventDispatcherImpl::~NotificationEventDispatcherImpl() {} |
| 181 | |
| 182 | void NotificationEventDispatcherImpl::DispatchNotificationClickEvent( |
| 183 | BrowserContext* browser_context, |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 184 | int64_t persistent_notification_id, |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 185 | const GURL& origin, |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 186 | const NotificationClickDispatchCompleteCallback& |
| 187 | dispatch_complete_callback) { |
| 188 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 189 | DCHECK_GT(persistent_notification_id, 0); |
| 190 | DCHECK(origin.is_valid()); |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 191 | |
| 192 | StoragePartition* partition = |
| 193 | BrowserContext::GetStoragePartitionForSite(browser_context, origin); |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 194 | |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 195 | scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = |
| 196 | static_cast<ServiceWorkerContextWrapper*>( |
| 197 | partition->GetServiceWorkerContext()); |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 198 | scoped_refptr<PlatformNotificationContextImpl> notification_context = |
| 199 | static_cast<PlatformNotificationContextImpl*>( |
| 200 | partition->GetPlatformNotificationContext()); |
| 201 | |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 202 | BrowserThread::PostTask( |
| 203 | BrowserThread::IO, |
| 204 | FROM_HERE, |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 205 | base::Bind(&ReadNotificationDatabaseData, |
| 206 | persistent_notification_id, |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 207 | origin, |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 208 | dispatch_complete_callback, |
peter | 2ee1f3c | 2015-04-15 13:09:09 | [diff] [blame^] | 209 | service_worker_context, |
| 210 | notification_context)); |
peter | d69823a1 | 2014-12-09 14:06:59 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | } // namespace content |