Chromium Code Reviews| Index: content/browser/push_messaging_router_impl.cc |
| diff --git a/content/browser/push_messaging_router_impl.cc b/content/browser/push_messaging_router_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dd19bcf3fe83bcd88a8c67155b5a9b06eda3f94f |
| --- /dev/null |
| +++ b/content/browser/push_messaging_router_impl.cc |
| @@ -0,0 +1,135 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/push_messaging_router_impl.h" |
| + |
| +#include "base/bind.h" |
| +#include "content/browser/service_worker/service_worker_context_wrapper.h" |
| +#include "content/browser/service_worker/service_worker_registration.h" |
| +#include "content/browser/service_worker/service_worker_storage.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/push_messaging_application_id.h" |
| +#include "content/public/browser/storage_partition.h" |
| + |
| +namespace content { |
| + |
| +// static |
| +scoped_ptr<PushMessagingRouter> PushMessagingRouter::Create( |
| + BrowserContext* browser_context) { |
| + return scoped_ptr<PushMessagingRouter>( |
| + new PushMessagingRouterImpl(browser_context)); |
| +} |
| + |
| +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
|
| + BrowserContext* browser_context) |
| + : browser_context_(browser_context), weak_factory_(this) { |
| +} |
| + |
| +PushMessagingRouterImpl::~PushMessagingRouterImpl() { |
| +} |
| + |
| +void PushMessagingRouterImpl::SendMessage( |
| + const PushMessagingApplicationId& application_id, |
| + const std::string& data, |
| + const SendMessageCallback& send_message_callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + StoragePartition* partition = BrowserContext::GetStoragePartitionForSite( |
| + browser_context_, application_id.origin); |
| + scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = |
| + static_cast<ServiceWorkerContextWrapper*>( |
| + partition->GetServiceWorkerContext()); |
| + BrowserThread::PostTask( |
| + BrowserThread::IO, |
| + FROM_HERE, |
| + base::Bind(&PushMessagingRouterImpl::FindServiceWorkerRegistration, |
| + weak_factory_.GetWeakPtr(), |
| + application_id, |
| + data, |
| + send_message_callback, |
| + service_worker_context)); |
| +} |
| + |
| +void PushMessagingRouterImpl::FindServiceWorkerRegistration( |
| + const PushMessagingApplicationId& application_id, |
| + const std::string& data, |
| + const SendMessageCallback& send_message_callback, |
| + scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + scoped_refptr<ServiceWorkerRegistration> service_worker_registration = |
| + service_worker_context->context()->GetLiveRegistration( |
| + application_id.service_worker_registration_id); |
| + if (service_worker_registration) { |
| + DoSendMessage(data, send_message_callback, service_worker_registration); |
| + } else { |
| + // If there is not currently a live registration, try to find one in |
| + // storage. This path runs e.g. when the browser was not previously running |
| + // and it was woken up for the purpose of delivering the push message. |
| + service_worker_context->context()->storage()->FindRegistrationForId( |
| + application_id.service_worker_registration_id, |
| + application_id.origin, |
| + base::Bind( |
| + &PushMessagingRouterImpl::FindServiceWorkerRegistrationCallback, |
| + weak_factory_.GetWeakPtr(), |
| + application_id, |
| + data, |
| + send_message_callback)); |
| + } |
| +} |
| + |
| +void PushMessagingRouterImpl::FindServiceWorkerRegistrationCallback( |
| + 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.
|
| + const std::string& data, |
| + const SendMessageCallback& send_message_callback, |
| + ServiceWorkerStatusCode service_worker_status, |
| + const scoped_refptr<ServiceWorkerRegistration>& |
| + service_worker_registration) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + if (service_worker_status == SERVICE_WORKER_OK) { |
| + DoSendMessage(data, send_message_callback, service_worker_registration); |
| + } else { |
| + // TODO(mvanouwerkerk): UMA logging. |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind( |
| + send_message_callback, |
| + PUSH_MESSAGING_STATUS_MESSAGE_DELIVERY_FAILED_NO_SERVICE_WORKER)); |
| + } |
| +} |
| + |
| +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.
|
| + const std::string& data, |
| + const SendMessageCallback& send_message_callback, |
| + const scoped_refptr<ServiceWorkerRegistration>& |
| + service_worker_registration) { |
| + // Hold on to the service worker registration in the callback to keep it alive |
| + // until the callback dies. Otherwise the registration could be released when |
| + // 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.
|
| + base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = |
| + base::Bind(&PushMessagingRouterImpl::SendMessageEnd, |
| + weak_factory_.GetWeakPtr(), |
| + send_message_callback, |
| + service_worker_registration); |
| + service_worker_registration->active_version()->DispatchPushEvent( |
| + dispatch_event_callback, data); |
| +} |
| + |
| +void PushMessagingRouterImpl::SendMessageEnd( |
| + const SendMessageCallback& send_message_callback, |
| + const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
| + ServiceWorkerStatusCode service_worker_status) { |
| + // TODO(mvanouwerkerk): UMA logging. |
| + PushMessagingStatus push_messaging_status = |
| + service_worker_status == SERVICE_WORKER_OK |
| + ? PUSH_MESSAGING_STATUS_OK |
| + : PUSH_MESSAGING_STATUS_MESSAGE_DELIVERY_FAILED_SERVICE_WORKER_ERROR; |
| + 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.
|
| + BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(send_message_callback, push_messaging_status)); |
| +} |
| + |
| +} // namespace content |