| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/push_messaging/push_messaging_router.h" | 5 #include "content/browser/push_messaging/push_messaging_router.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 10 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 11 #include "content/browser/service_worker/service_worker_registration.h" | 11 #include "content/browser/service_worker/service_worker_registration.h" |
| 12 #include "content/browser/service_worker/service_worker_storage.h" | 12 #include "content/browser/service_worker/service_worker_storage.h" |
| 13 #include "content/common/service_worker/service_worker_status_code.h" | 13 #include "content/common/service_worker/service_worker_status_code.h" |
| 14 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/storage_partition.h" | 16 #include "content/public/browser/storage_partition.h" |
| 17 #include "content/public/common/push_event_payload.h" |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 void RunDeliverCallback( | 23 void RunDeliverCallback( |
| 23 const PushMessagingRouter::DeliverMessageCallback& deliver_message_callback, | 24 const PushMessagingRouter::DeliverMessageCallback& deliver_message_callback, |
| 24 PushDeliveryStatus delivery_status) { | 25 PushDeliveryStatus delivery_status) { |
| 25 BrowserThread::PostTask( | 26 BrowserThread::PostTask( |
| 26 BrowserThread::UI, FROM_HERE, | 27 BrowserThread::UI, FROM_HERE, |
| 27 base::Bind(deliver_message_callback, delivery_status)); | 28 base::Bind(deliver_message_callback, delivery_status)); |
| 28 } | 29 } |
| 29 | 30 |
| 30 } // namespace | 31 } // namespace |
| 31 | 32 |
| 32 // static | 33 // static |
| 33 void PushMessagingRouter::DeliverMessage( | 34 void PushMessagingRouter::DeliverMessage( |
| 34 BrowserContext* browser_context, | 35 BrowserContext* browser_context, |
| 35 const GURL& origin, | 36 const GURL& origin, |
| 36 int64_t service_worker_registration_id, | 37 int64_t service_worker_registration_id, |
| 37 const std::string& data, | 38 const PushEventPayload& payload, |
| 38 const DeliverMessageCallback& deliver_message_callback) { | 39 const DeliverMessageCallback& deliver_message_callback) { |
| 39 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 40 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 40 StoragePartition* partition = | 41 StoragePartition* partition = |
| 41 BrowserContext::GetStoragePartitionForSite(browser_context, origin); | 42 BrowserContext::GetStoragePartitionForSite(browser_context, origin); |
| 42 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = | 43 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = |
| 43 static_cast<ServiceWorkerContextWrapper*>( | 44 static_cast<ServiceWorkerContextWrapper*>( |
| 44 partition->GetServiceWorkerContext()); | 45 partition->GetServiceWorkerContext()); |
| 45 BrowserThread::PostTask( | 46 BrowserThread::PostTask( |
| 46 BrowserThread::IO, FROM_HERE, | 47 BrowserThread::IO, FROM_HERE, |
| 47 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistration, origin, | 48 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistration, origin, |
| 48 service_worker_registration_id, data, deliver_message_callback, | 49 service_worker_registration_id, payload, |
| 49 service_worker_context)); | 50 deliver_message_callback, service_worker_context)); |
| 50 } | 51 } |
| 51 | 52 |
| 52 // static | 53 // static |
| 53 void PushMessagingRouter::FindServiceWorkerRegistration( | 54 void PushMessagingRouter::FindServiceWorkerRegistration( |
| 54 const GURL& origin, | 55 const GURL& origin, |
| 55 int64_t service_worker_registration_id, | 56 int64_t service_worker_registration_id, |
| 56 const std::string& data, | 57 const PushEventPayload& payload, |
| 57 const DeliverMessageCallback& deliver_message_callback, | 58 const DeliverMessageCallback& deliver_message_callback, |
| 58 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { | 59 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { |
| 59 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 60 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 60 // Try to acquire the registration from storage. If it's already live we'll | 61 // Try to acquire the registration from storage. If it's already live we'll |
| 61 // receive it right away. If not, it will be revived from storage. | 62 // receive it right away. If not, it will be revived from storage. |
| 62 service_worker_context->FindReadyRegistrationForId( | 63 service_worker_context->FindReadyRegistrationForId( |
| 63 service_worker_registration_id, origin, | 64 service_worker_registration_id, origin, |
| 64 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistrationCallback, | 65 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistrationCallback, |
| 65 data, deliver_message_callback)); | 66 payload, deliver_message_callback)); |
| 66 } | 67 } |
| 67 | 68 |
| 68 // static | 69 // static |
| 69 void PushMessagingRouter::FindServiceWorkerRegistrationCallback( | 70 void PushMessagingRouter::FindServiceWorkerRegistrationCallback( |
| 70 const std::string& data, | 71 const PushEventPayload& payload, |
| 71 const DeliverMessageCallback& deliver_message_callback, | 72 const DeliverMessageCallback& deliver_message_callback, |
| 72 ServiceWorkerStatusCode service_worker_status, | 73 ServiceWorkerStatusCode service_worker_status, |
| 73 const scoped_refptr<ServiceWorkerRegistration>& | 74 const scoped_refptr<ServiceWorkerRegistration>& |
| 74 service_worker_registration) { | 75 service_worker_registration) { |
| 75 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 76 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 76 // TODO(mvanouwerkerk): UMA logging. | 77 // TODO(mvanouwerkerk): UMA logging. |
| 77 if (service_worker_status != SERVICE_WORKER_OK) { | 78 if (service_worker_status != SERVICE_WORKER_OK) { |
| 78 RunDeliverCallback(deliver_message_callback, | 79 RunDeliverCallback(deliver_message_callback, |
| 79 PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER); | 80 PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER); |
| 80 return; | 81 return; |
| 81 } | 82 } |
| 82 | 83 |
| 83 ServiceWorkerVersion* version = service_worker_registration->active_version(); | 84 ServiceWorkerVersion* version = service_worker_registration->active_version(); |
| 84 DCHECK(version); | 85 DCHECK(version); |
| 85 | 86 |
| 86 // Hold on to the service worker registration in the callback to keep it | 87 // Hold on to the service worker registration in the callback to keep it |
| 87 // alive until the callback dies. Otherwise the registration could be | 88 // alive until the callback dies. Otherwise the registration could be |
| 88 // released when this method returns - before the event is delivered to the | 89 // released when this method returns - before the event is delivered to the |
| 89 // service worker. | 90 // service worker. |
| 90 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = | 91 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = |
| 91 base::Bind(&PushMessagingRouter::DeliverMessageEnd, | 92 base::Bind(&PushMessagingRouter::DeliverMessageEnd, |
| 92 deliver_message_callback, service_worker_registration); | 93 deliver_message_callback, service_worker_registration); |
| 93 | 94 |
| 94 version->DispatchPushEvent(dispatch_event_callback, data); | 95 version->DispatchPushEvent(dispatch_event_callback, payload); |
| 95 } | 96 } |
| 96 | 97 |
| 97 // static | 98 // static |
| 98 void PushMessagingRouter::DeliverMessageEnd( | 99 void PushMessagingRouter::DeliverMessageEnd( |
| 99 const DeliverMessageCallback& deliver_message_callback, | 100 const DeliverMessageCallback& deliver_message_callback, |
| 100 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | 101 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
| 101 ServiceWorkerStatusCode service_worker_status) { | 102 ServiceWorkerStatusCode service_worker_status) { |
| 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 103 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 103 // TODO(mvanouwerkerk): UMA logging. | 104 // TODO(mvanouwerkerk): UMA logging. |
| 104 PushDeliveryStatus delivery_status = | 105 PushDeliveryStatus delivery_status = |
| (...skipping 27 matching lines...) Expand all Loading... |
| 132 case SERVICE_WORKER_ERROR_MAX_VALUE: | 133 case SERVICE_WORKER_ERROR_MAX_VALUE: |
| 133 NOTREACHED() << "Got unexpected error code: " << service_worker_status | 134 NOTREACHED() << "Got unexpected error code: " << service_worker_status |
| 134 << " " << ServiceWorkerStatusToString(service_worker_status); | 135 << " " << ServiceWorkerStatusToString(service_worker_status); |
| 135 delivery_status = PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR; | 136 delivery_status = PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR; |
| 136 break; | 137 break; |
| 137 } | 138 } |
| 138 RunDeliverCallback(deliver_message_callback, delivery_status); | 139 RunDeliverCallback(deliver_message_callback, delivery_status); |
| 139 } | 140 } |
| 140 | 141 |
| 141 } // namespace content | 142 } // namespace content |
| OLD | NEW |