Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3713)

Unified Diff: content/browser/push_messaging_router_impl.cc

Issue 340773006: Dispatch push event to worker from PushServiceImpl#OnMessage. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address John's comments. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..bbb1ed98663e77094668cad75c4bf3a4ebd93216
--- /dev/null
+++ b/content/browser/push_messaging_router_impl.cc
@@ -0,0 +1,136 @@
+// 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"
michaeln 2014/07/24 02:08:16 missing file? push_messaging_application_id.h
Michael van Ouwerkerk 2014/07/24 14:18:55 It's defined in the patch this depends on: https:/
+#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(
+ BrowserContext* browser_context)
+ : browser_context_(browser_context), weak_factory_(this) {
+}
+
+PushMessagingRouterImpl::~PushMessagingRouterImpl() {
+}
+
+void PushMessagingRouterImpl::DeliverMessage(
+ const PushMessagingApplicationId& application_id,
+ const std::string& data,
+ const DeliverMessageCallback& deliver_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,
+ deliver_message_callback,
+ service_worker_context));
+}
+
+void PushMessagingRouterImpl::FindServiceWorkerRegistration(
+ const PushMessagingApplicationId& application_id,
+ const std::string& data,
+ const DeliverMessageCallback& deliver_message_callback,
+ scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ scoped_refptr<ServiceWorkerRegistration> service_worker_registration =
+ service_worker_context->context()->GetLiveRegistration(
michaeln 2014/07/24 02:08:16 this code path to lookup the 'live' object may not
Michael van Ouwerkerk 2014/07/24 14:18:55 Done. Nice.
+ application_id.service_worker_registration_id);
+ if (service_worker_registration) {
+ DoDeliverMessage(
+ data, deliver_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(),
+ data,
+ deliver_message_callback));
+ }
+}
+
+void PushMessagingRouterImpl::FindServiceWorkerRegistrationCallback(
+ const std::string& data,
+ const DeliverMessageCallback& deliver_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) {
+ DoDeliverMessage(
+ data, deliver_message_callback, service_worker_registration);
+ } else {
+ // TODO(mvanouwerkerk): UMA logging.
+ BrowserThread::PostTask(
+ BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(
+ deliver_message_callback,
+ PUSH_MESSAGING_STATUS_MESSAGE_DELIVERY_FAILED_NO_SERVICE_WORKER));
+ }
+}
+
+void PushMessagingRouterImpl::DoDeliverMessage(
+ const std::string& data,
+ const DeliverMessageCallback& deliver_message_callback,
+ const scoped_refptr<ServiceWorkerRegistration>&
+ service_worker_registration) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ // Hold on to the service worker registration in the callback to keep it alive
michaeln 2014/07/24 02:08:16 nice comment
Michael van Ouwerkerk 2014/07/24 14:18:55 Yeah that was actually a bit of a mean bug. Maybe
michaeln 2014/07/25 00:08:27 Right, maybe the SW side of this should keep thing
+ // until the callback dies. Otherwise the registration could be released when
+ // this method returns - before the event is delivered to the service worker.
+ base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback =
+ base::Bind(&PushMessagingRouterImpl::DeliverMessageEnd,
+ weak_factory_.GetWeakPtr(),
+ deliver_message_callback,
+ service_worker_registration);
+ service_worker_registration->active_version()->DispatchPushEvent(
+ dispatch_event_callback, data);
+}
+
+void PushMessagingRouterImpl::DeliverMessageEnd(
+ const DeliverMessageCallback& deliver_message_callback,
+ const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration,
+ ServiceWorkerStatusCode service_worker_status) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ // 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;
+ BrowserThread::PostTask(
+ BrowserThread::UI,
+ FROM_HERE,
+ base::Bind(deliver_message_callback, push_messaging_status));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698