Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/push_messaging_router_impl.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "content/browser/service_worker/service_worker_context_wrapper.h" | |
| 9 #include "content/browser/service_worker/service_worker_registration.h" | |
| 10 #include "content/browser/service_worker/service_worker_storage.h" | |
| 11 #include "content/public/browser/browser_context.h" | |
| 12 #include "content/public/browser/browser_thread.h" | |
| 13 #include "content/public/browser/push_messaging_application_id.h" | |
| 14 #include "content/public/browser/storage_partition.h" | |
| 15 | |
| 16 namespace content { | |
| 17 | |
| 18 // static | |
| 19 scoped_ptr<PushMessagingRouter> PushMessagingRouter::Create( | |
| 20 BrowserContext* browser_context) { | |
| 21 return scoped_ptr<PushMessagingRouter>( | |
| 22 new PushMessagingRouterImpl(browser_context)); | |
| 23 } | |
| 24 | |
| 25 PushMessagingRouterImpl::PushMessagingRouterImpl( | |
|
johnme
2014/07/23 14:25:23
Nit: Would it make sense for the constructor to DC
Michael van Ouwerkerk
2014/07/23 16:57:51
I think it's not necessary for this class, as it's
| |
| 26 BrowserContext* browser_context) | |
| 27 : browser_context_(browser_context), weak_factory_(this) { | |
| 28 } | |
| 29 | |
| 30 PushMessagingRouterImpl::~PushMessagingRouterImpl() { | |
| 31 } | |
| 32 | |
| 33 void PushMessagingRouterImpl::SendMessage( | |
| 34 const PushMessagingApplicationId& application_id, | |
| 35 const std::string& data, | |
| 36 const SendMessageCallback& send_message_callback) { | |
| 37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 38 StoragePartition* partition = BrowserContext::GetStoragePartitionForSite( | |
| 39 browser_context_, application_id.origin); | |
| 40 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = | |
| 41 static_cast<ServiceWorkerContextWrapper*>( | |
| 42 partition->GetServiceWorkerContext()); | |
| 43 BrowserThread::PostTask( | |
| 44 BrowserThread::IO, | |
| 45 FROM_HERE, | |
| 46 base::Bind(&PushMessagingRouterImpl::FindServiceWorkerRegistration, | |
| 47 weak_factory_.GetWeakPtr(), | |
| 48 application_id, | |
| 49 data, | |
| 50 send_message_callback, | |
| 51 service_worker_context)); | |
| 52 } | |
| 53 | |
| 54 void PushMessagingRouterImpl::FindServiceWorkerRegistration( | |
| 55 const PushMessagingApplicationId& application_id, | |
| 56 const std::string& data, | |
| 57 const SendMessageCallback& send_message_callback, | |
| 58 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { | |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 60 scoped_refptr<ServiceWorkerRegistration> service_worker_registration = | |
| 61 service_worker_context->context()->GetLiveRegistration( | |
| 62 application_id.service_worker_registration_id); | |
| 63 if (service_worker_registration) { | |
| 64 DoSendMessage(data, send_message_callback, service_worker_registration); | |
| 65 } else { | |
| 66 // If there is not currently a live registration, try to find one in | |
| 67 // storage. This path runs e.g. when the browser was not previously running | |
| 68 // and it was woken up for the purpose of delivering the push message. | |
| 69 service_worker_context->context()->storage()->FindRegistrationForId( | |
| 70 application_id.service_worker_registration_id, | |
| 71 application_id.origin, | |
| 72 base::Bind( | |
| 73 &PushMessagingRouterImpl::FindServiceWorkerRegistrationCallback, | |
| 74 weak_factory_.GetWeakPtr(), | |
| 75 application_id, | |
| 76 data, | |
| 77 send_message_callback)); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 void PushMessagingRouterImpl::FindServiceWorkerRegistrationCallback( | |
| 82 const PushMessagingApplicationId& application_id, | |
|
johnme
2014/07/23 14:25:23
application_id appears to be unused.
Michael van Ouwerkerk
2014/07/23 16:57:51
Done.
| |
| 83 const std::string& data, | |
| 84 const SendMessageCallback& send_message_callback, | |
| 85 ServiceWorkerStatusCode service_worker_status, | |
| 86 const scoped_refptr<ServiceWorkerRegistration>& | |
| 87 service_worker_registration) { | |
| 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 89 if (service_worker_status == SERVICE_WORKER_OK) { | |
| 90 DoSendMessage(data, send_message_callback, service_worker_registration); | |
| 91 } else { | |
| 92 // TODO(mvanouwerkerk): UMA logging. | |
| 93 BrowserThread::PostTask( | |
| 94 BrowserThread::UI, | |
| 95 FROM_HERE, | |
| 96 base::Bind( | |
| 97 send_message_callback, | |
| 98 PUSH_MESSAGING_STATUS_MESSAGE_DELIVERY_FAILED_NO_SERVICE_WORKER)); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 void PushMessagingRouterImpl::DoSendMessage( | |
|
johnme
2014/07/23 14:25:23
Nit: let's add a DCHECK(BrowserThread::CurrentlyOn
Michael van Ouwerkerk
2014/07/23 16:57:51
Done.
| |
| 103 const std::string& data, | |
| 104 const SendMessageCallback& send_message_callback, | |
| 105 const scoped_refptr<ServiceWorkerRegistration>& | |
| 106 service_worker_registration) { | |
| 107 // Hold on to the service worker registration in the callback to keep it alive | |
| 108 // until the callback dies. Otherwise the registration could be released when | |
| 109 // this method returns. | |
|
johnme
2014/07/23 14:25:23
Nit: s/returns/returns (before the event is delive
Michael van Ouwerkerk
2014/07/23 16:57:51
Done.
| |
| 110 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = | |
| 111 base::Bind(&PushMessagingRouterImpl::SendMessageEnd, | |
| 112 weak_factory_.GetWeakPtr(), | |
| 113 send_message_callback, | |
| 114 service_worker_registration); | |
| 115 service_worker_registration->active_version()->DispatchPushEvent( | |
| 116 dispatch_event_callback, data); | |
| 117 } | |
| 118 | |
| 119 void PushMessagingRouterImpl::SendMessageEnd( | |
| 120 const SendMessageCallback& send_message_callback, | |
| 121 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | |
| 122 ServiceWorkerStatusCode service_worker_status) { | |
| 123 // TODO(mvanouwerkerk): UMA logging. | |
| 124 PushMessagingStatus push_messaging_status = | |
| 125 service_worker_status == SERVICE_WORKER_OK | |
| 126 ? PUSH_MESSAGING_STATUS_OK | |
| 127 : PUSH_MESSAGING_STATUS_MESSAGE_DELIVERY_FAILED_SERVICE_WORKER_ERROR; | |
| 128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
|
johnme
2014/07/23 14:25:23
Nit: move to top of method?
Michael van Ouwerkerk
2014/07/23 16:57:51
Done.
| |
| 129 BrowserThread::PostTask( | |
| 130 BrowserThread::UI, | |
| 131 FROM_HERE, | |
| 132 base::Bind(send_message_callback, push_messaging_status)); | |
| 133 } | |
| 134 | |
| 135 } // namespace content | |
| OLD | NEW |