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

Side by Side Diff: content/browser/push_messaging/push_messaging_router.cc

Issue 1579413004: Move push event dispatching out of ServiceWorkerVersion. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix DispatchSimpleEvent comment Created 4 years, 11 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
OLDNEW
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_messages.h"
13 #include "content/common/service_worker/service_worker_status_code.h" 14 #include "content/common/service_worker/service_worker_status_code.h"
14 #include "content/public/browser/browser_context.h" 15 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/storage_partition.h" 17 #include "content/public/browser/storage_partition.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) {
26 DCHECK_CURRENTLY_ON(BrowserThread::IO);
25 BrowserThread::PostTask( 27 BrowserThread::PostTask(
26 BrowserThread::UI, FROM_HERE, 28 BrowserThread::UI, FROM_HERE,
27 base::Bind(deliver_message_callback, delivery_status)); 29 base::Bind(deliver_message_callback, delivery_status));
28 } 30 }
29 31
30 } // namespace 32 } // namespace
31 33
32 // static 34 // static
33 void PushMessagingRouter::DeliverMessage( 35 void PushMessagingRouter::DeliverMessage(
34 BrowserContext* browser_context, 36 BrowserContext* browser_context,
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 return; 82 return;
81 } 83 }
82 84
83 ServiceWorkerVersion* version = service_worker_registration->active_version(); 85 ServiceWorkerVersion* version = service_worker_registration->active_version();
84 DCHECK(version); 86 DCHECK(version);
85 87
86 // Hold on to the service worker registration in the callback to keep it 88 // Hold on to the service worker registration in the callback to keep it
87 // alive until the callback dies. Otherwise the registration could be 89 // alive until the callback dies. Otherwise the registration could be
88 // released when this method returns - before the event is delivered to the 90 // released when this method returns - before the event is delivered to the
89 // service worker. 91 // service worker.
90 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = 92 version->RunAfterStartWorker(
93 base::Bind(&PushMessagingRouter::DeliverMessageToWorker,
94 make_scoped_refptr(version), service_worker_registration, data,
95 deliver_message_callback),
91 base::Bind(&PushMessagingRouter::DeliverMessageEnd, 96 base::Bind(&PushMessagingRouter::DeliverMessageEnd,
92 deliver_message_callback, service_worker_registration); 97 deliver_message_callback, service_worker_registration));
93
94 version->DispatchPushEvent(dispatch_event_callback, data);
95 } 98 }
96 99
97 // static 100 // static
101 void PushMessagingRouter::DeliverMessageToWorker(
102 const scoped_refptr<ServiceWorkerVersion>& service_worker,
103 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration,
104 const std::string& data,
105 const DeliverMessageCallback& deliver_message_callback) {
106 DCHECK_CURRENTLY_ON(BrowserThread::IO);
107 int request_id = service_worker->StartRequest(
108 ServiceWorkerMetrics::EventType::PUSH,
109 base::Bind(&PushMessagingRouter::DeliverMessageEnd,
110 deliver_message_callback, service_worker_registration));
111 service_worker->DispatchSimpleEvent<ServiceWorkerHostMsg_PushEventFinished>(
112 request_id, ServiceWorkerMsg_PushEvent(request_id, data));
113 }
114
115 // static
98 void PushMessagingRouter::DeliverMessageEnd( 116 void PushMessagingRouter::DeliverMessageEnd(
99 const DeliverMessageCallback& deliver_message_callback, 117 const DeliverMessageCallback& deliver_message_callback,
100 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, 118 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration,
101 ServiceWorkerStatusCode service_worker_status) { 119 ServiceWorkerStatusCode service_worker_status) {
102 DCHECK_CURRENTLY_ON(BrowserThread::IO); 120 DCHECK_CURRENTLY_ON(BrowserThread::IO);
103 // TODO(mvanouwerkerk): UMA logging. 121 // TODO(mvanouwerkerk): UMA logging.
104 PushDeliveryStatus delivery_status = 122 PushDeliveryStatus delivery_status =
105 PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR; 123 PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR;
106 switch (service_worker_status) { 124 switch (service_worker_status) {
107 case SERVICE_WORKER_OK: 125 case SERVICE_WORKER_OK:
(...skipping 25 matching lines...) Expand all
133 case SERVICE_WORKER_ERROR_MAX_VALUE: 151 case SERVICE_WORKER_ERROR_MAX_VALUE:
134 NOTREACHED() << "Got unexpected error code: " << service_worker_status 152 NOTREACHED() << "Got unexpected error code: " << service_worker_status
135 << " " << ServiceWorkerStatusToString(service_worker_status); 153 << " " << ServiceWorkerStatusToString(service_worker_status);
136 delivery_status = PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR; 154 delivery_status = PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR;
137 break; 155 break;
138 } 156 }
139 RunDeliverCallback(deliver_message_callback, delivery_status); 157 RunDeliverCallback(deliver_message_callback, delivery_status);
140 } 158 }
141 159
142 } // namespace content 160 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/push_messaging/push_messaging_router.h ('k') | content/browser/service_worker/service_worker_version.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698