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

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: 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) {
25 BrowserThread::PostTask( 26 BrowserThread::PostTask(
johnme 2016/01/18 19:06:19 Nit: While you're touching this file, please add a
Marijn Kruisselbrink 2016/01/20 01:06:31 Done
26 BrowserThread::UI, FROM_HERE, 27 BrowserThread::UI, FROM_HERE,
27 base::Bind(deliver_message_callback, delivery_status)); 28 base::Bind(deliver_message_callback, delivery_status));
28 } 29 }
29 30
31 void OnPushEventResponse(scoped_refptr<ServiceWorkerVersion> service_worker,
johnme 2016/01/18 19:06:19 [see comment in .h first] Please add DCHECK_CURRE
Marijn Kruisselbrink 2016/01/20 01:06:31 Done
32 base::Callback<void(ServiceWorkerStatusCode)> callback,
33 int request_id,
34 blink::WebServiceWorkerEventResult result) {
35 TRACE_EVENT1("ServiceWorker", "PushMessagingRouter::OnPushEventResponse",
36 "Request id", request_id);
37
38 service_worker->FinishRequest(request_id);
johnme 2016/01/18 19:06:19 [see comment in .h first] Please apply equivalent
Marijn Kruisselbrink 2016/01/20 01:06:31 Done
39
40 ServiceWorkerStatusCode status = SERVICE_WORKER_OK;
41 if (result == blink::WebServiceWorkerEventResultRejected)
42 status = SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED;
43 callback.Run(status);
44 }
45
30 } // namespace 46 } // namespace
31 47
32 // static 48 // static
33 void PushMessagingRouter::DeliverMessage( 49 void PushMessagingRouter::DeliverMessage(
34 BrowserContext* browser_context, 50 BrowserContext* browser_context,
35 const GURL& origin, 51 const GURL& origin,
36 int64_t service_worker_registration_id, 52 int64_t service_worker_registration_id,
37 const std::string& data, 53 const std::string& data,
38 const DeliverMessageCallback& deliver_message_callback) { 54 const DeliverMessageCallback& deliver_message_callback) {
39 DCHECK_CURRENTLY_ON(BrowserThread::UI); 55 DCHECK_CURRENTLY_ON(BrowserThread::UI);
40 StoragePartition* partition = 56 StoragePartition* partition =
41 BrowserContext::GetStoragePartitionForSite(browser_context, origin); 57 BrowserContext::GetStoragePartitionForSite(browser_context, origin);
42 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = 58 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context =
43 static_cast<ServiceWorkerContextWrapper*>( 59 static_cast<ServiceWorkerContextWrapper*>(
44 partition->GetServiceWorkerContext()); 60 partition->GetServiceWorkerContext());
45 BrowserThread::PostTask( 61 BrowserThread::PostTask(
46 BrowserThread::IO, FROM_HERE, 62 BrowserThread::IO, FROM_HERE,
47 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistration, origin, 63 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistration, origin,
48 service_worker_registration_id, data, deliver_message_callback, 64 service_worker_registration_id, data, deliver_message_callback,
49 service_worker_context)); 65 service_worker_context));
50 } 66 }
51 67
52 // static 68 // static
69 void PushMessagingRouter::DeliverMessageToWorker(
johnme 2016/01/18 19:06:19 [see comment in .h first] Nit: please move this b
Marijn Kruisselbrink 2016/01/20 01:06:31 That would go against the chromium coding style: "
70 const scoped_refptr<ServiceWorkerVersion>& service_worker,
71 const std::string& data,
72 const base::Callback<void(ServiceWorkerStatusCode)>& callback) {
73 DCHECK_CURRENTLY_ON(BrowserThread::IO);
74 if (service_worker->running_status() != ServiceWorkerVersion::RUNNING) {
johnme 2016/01/18 19:06:19 [see comment in .h first] Nit: In https://coderev
Marijn Kruisselbrink 2016/01/20 01:06:31 The reason for the difference was to keep the chan
75 service_worker->RunAfterStartWorker(
76 callback, base::Bind(&PushMessagingRouter::DeliverMessageToWorker,
77 service_worker, data, callback));
78 return;
79 }
80
81 int request_id = service_worker->StartRequest(
82 ServiceWorkerMetrics::EventType::PUSH, callback);
83 service_worker->DispatchEvent<ServiceWorkerHostMsg_PushEventFinished>(
84 request_id, ServiceWorkerMsg_PushEvent(request_id, data),
85 base::Bind(&OnPushEventResponse, service_worker, callback));
86 }
87
88 // static
53 void PushMessagingRouter::FindServiceWorkerRegistration( 89 void PushMessagingRouter::FindServiceWorkerRegistration(
54 const GURL& origin, 90 const GURL& origin,
55 int64_t service_worker_registration_id, 91 int64_t service_worker_registration_id,
56 const std::string& data, 92 const std::string& data,
57 const DeliverMessageCallback& deliver_message_callback, 93 const DeliverMessageCallback& deliver_message_callback,
58 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { 94 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) {
59 DCHECK_CURRENTLY_ON(BrowserThread::IO); 95 DCHECK_CURRENTLY_ON(BrowserThread::IO);
60 // Try to acquire the registration from storage. If it's already live we'll 96 // Try to acquire the registration from storage. If it's already live we'll
61 // receive it right away. If not, it will be revived from storage. 97 // receive it right away. If not, it will be revived from storage.
62 service_worker_context->FindReadyRegistrationForId( 98 service_worker_context->FindReadyRegistrationForId(
(...skipping 20 matching lines...) Expand all
83 ServiceWorkerVersion* version = service_worker_registration->active_version(); 119 ServiceWorkerVersion* version = service_worker_registration->active_version();
84 DCHECK(version); 120 DCHECK(version);
85 121
86 // Hold on to the service worker registration in the callback to keep it 122 // Hold on to the service worker registration in the callback to keep it
87 // alive until the callback dies. Otherwise the registration could be 123 // alive until the callback dies. Otherwise the registration could be
88 // released when this method returns - before the event is delivered to the 124 // released when this method returns - before the event is delivered to the
89 // service worker. 125 // service worker.
90 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = 126 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback =
91 base::Bind(&PushMessagingRouter::DeliverMessageEnd, 127 base::Bind(&PushMessagingRouter::DeliverMessageEnd,
92 deliver_message_callback, service_worker_registration); 128 deliver_message_callback, service_worker_registration);
93 129 DeliverMessageToWorker(version, data, dispatch_event_callback);
94 version->DispatchPushEvent(dispatch_event_callback, data);
95 } 130 }
96 131
97 // static 132 // static
98 void PushMessagingRouter::DeliverMessageEnd( 133 void PushMessagingRouter::DeliverMessageEnd(
99 const DeliverMessageCallback& deliver_message_callback, 134 const DeliverMessageCallback& deliver_message_callback,
100 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, 135 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration,
101 ServiceWorkerStatusCode service_worker_status) { 136 ServiceWorkerStatusCode service_worker_status) {
102 DCHECK_CURRENTLY_ON(BrowserThread::IO); 137 DCHECK_CURRENTLY_ON(BrowserThread::IO);
103 // TODO(mvanouwerkerk): UMA logging. 138 // TODO(mvanouwerkerk): UMA logging.
104 PushDeliveryStatus delivery_status = 139 PushDeliveryStatus delivery_status =
(...skipping 27 matching lines...) Expand all
132 case SERVICE_WORKER_ERROR_MAX_VALUE: 167 case SERVICE_WORKER_ERROR_MAX_VALUE:
133 NOTREACHED() << "Got unexpected error code: " << service_worker_status 168 NOTREACHED() << "Got unexpected error code: " << service_worker_status
134 << " " << ServiceWorkerStatusToString(service_worker_status); 169 << " " << ServiceWorkerStatusToString(service_worker_status);
135 delivery_status = PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR; 170 delivery_status = PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR;
136 break; 171 break;
137 } 172 }
138 RunDeliverCallback(deliver_message_callback, delivery_status); 173 RunDeliverCallback(deliver_message_callback, delivery_status);
139 } 174 }
140 175
141 } // namespace content 176 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698