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

Side by Side 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, 4 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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"
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:/
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(
26 BrowserContext* browser_context)
27 : browser_context_(browser_context), weak_factory_(this) {
28 }
29
30 PushMessagingRouterImpl::~PushMessagingRouterImpl() {
31 }
32
33 void PushMessagingRouterImpl::DeliverMessage(
34 const PushMessagingApplicationId& application_id,
35 const std::string& data,
36 const DeliverMessageCallback& deliver_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 deliver_message_callback,
51 service_worker_context));
52 }
53
54 void PushMessagingRouterImpl::FindServiceWorkerRegistration(
55 const PushMessagingApplicationId& application_id,
56 const std::string& data,
57 const DeliverMessageCallback& deliver_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(
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.
62 application_id.service_worker_registration_id);
63 if (service_worker_registration) {
64 DoDeliverMessage(
65 data, deliver_message_callback, service_worker_registration);
66 } else {
67 // If there is not currently a live registration, try to find one in
68 // storage. This path runs e.g. when the browser was not previously running
69 // and it was woken up for the purpose of delivering the push message.
70 service_worker_context->context()->storage()->FindRegistrationForId(
71 application_id.service_worker_registration_id,
72 application_id.origin,
73 base::Bind(
74 &PushMessagingRouterImpl::FindServiceWorkerRegistrationCallback,
75 weak_factory_.GetWeakPtr(),
76 data,
77 deliver_message_callback));
78 }
79 }
80
81 void PushMessagingRouterImpl::FindServiceWorkerRegistrationCallback(
82 const std::string& data,
83 const DeliverMessageCallback& deliver_message_callback,
84 ServiceWorkerStatusCode service_worker_status,
85 const scoped_refptr<ServiceWorkerRegistration>&
86 service_worker_registration) {
87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
88 if (service_worker_status == SERVICE_WORKER_OK) {
89 DoDeliverMessage(
90 data, deliver_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 deliver_message_callback,
98 PUSH_MESSAGING_STATUS_MESSAGE_DELIVERY_FAILED_NO_SERVICE_WORKER));
99 }
100 }
101
102 void PushMessagingRouterImpl::DoDeliverMessage(
103 const std::string& data,
104 const DeliverMessageCallback& deliver_message_callback,
105 const scoped_refptr<ServiceWorkerRegistration>&
106 service_worker_registration) {
107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
108 // 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
109 // until the callback dies. Otherwise the registration could be released when
110 // this method returns - before the event is delivered to the service worker.
111 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback =
112 base::Bind(&PushMessagingRouterImpl::DeliverMessageEnd,
113 weak_factory_.GetWeakPtr(),
114 deliver_message_callback,
115 service_worker_registration);
116 service_worker_registration->active_version()->DispatchPushEvent(
117 dispatch_event_callback, data);
118 }
119
120 void PushMessagingRouterImpl::DeliverMessageEnd(
121 const DeliverMessageCallback& deliver_message_callback,
122 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration,
123 ServiceWorkerStatusCode service_worker_status) {
124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
125 // TODO(mvanouwerkerk): UMA logging.
126 PushMessagingStatus push_messaging_status =
127 service_worker_status == SERVICE_WORKER_OK
128 ? PUSH_MESSAGING_STATUS_OK
129 : PUSH_MESSAGING_STATUS_MESSAGE_DELIVERY_FAILED_SERVICE_WORKER_ERROR;
130 BrowserThread::PostTask(
131 BrowserThread::UI,
132 FROM_HERE,
133 base::Bind(deliver_message_callback, push_messaging_status));
134 }
135
136 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698