OLD | NEW |
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_messages.h" |
14 #include "content/common/service_worker/service_worker_status_code.h" | 14 #include "content/common/service_worker/service_worker_status_code.h" |
15 #include "content/public/browser/browser_context.h" | 15 #include "content/public/browser/browser_context.h" |
16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/storage_partition.h" | 17 #include "content/public/browser/storage_partition.h" |
| 18 #include "content/public/common/push_event_payload.h" |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 void RunDeliverCallback( | 24 void RunDeliverCallback( |
24 const PushMessagingRouter::DeliverMessageCallback& deliver_message_callback, | 25 const PushMessagingRouter::DeliverMessageCallback& deliver_message_callback, |
25 PushDeliveryStatus delivery_status) { | 26 PushDeliveryStatus delivery_status) { |
26 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 27 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
27 BrowserThread::PostTask( | 28 BrowserThread::PostTask( |
28 BrowserThread::UI, FROM_HERE, | 29 BrowserThread::UI, FROM_HERE, |
29 base::Bind(deliver_message_callback, delivery_status)); | 30 base::Bind(deliver_message_callback, delivery_status)); |
30 } | 31 } |
31 | 32 |
32 } // namespace | 33 } // namespace |
33 | 34 |
34 // static | 35 // static |
35 void PushMessagingRouter::DeliverMessage( | 36 void PushMessagingRouter::DeliverMessage( |
36 BrowserContext* browser_context, | 37 BrowserContext* browser_context, |
37 const GURL& origin, | 38 const GURL& origin, |
38 int64_t service_worker_registration_id, | 39 int64_t service_worker_registration_id, |
39 const std::string& data, | 40 const PushEventPayload& payload, |
40 const DeliverMessageCallback& deliver_message_callback) { | 41 const DeliverMessageCallback& deliver_message_callback) { |
41 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 42 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
42 StoragePartition* partition = | 43 StoragePartition* partition = |
43 BrowserContext::GetStoragePartitionForSite(browser_context, origin); | 44 BrowserContext::GetStoragePartitionForSite(browser_context, origin); |
44 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = | 45 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = |
45 static_cast<ServiceWorkerContextWrapper*>( | 46 static_cast<ServiceWorkerContextWrapper*>( |
46 partition->GetServiceWorkerContext()); | 47 partition->GetServiceWorkerContext()); |
47 BrowserThread::PostTask( | 48 BrowserThread::PostTask( |
48 BrowserThread::IO, FROM_HERE, | 49 BrowserThread::IO, FROM_HERE, |
49 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistration, origin, | 50 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistration, origin, |
50 service_worker_registration_id, data, deliver_message_callback, | 51 service_worker_registration_id, payload, |
51 service_worker_context)); | 52 deliver_message_callback, service_worker_context)); |
52 } | 53 } |
53 | 54 |
54 // static | 55 // static |
55 void PushMessagingRouter::FindServiceWorkerRegistration( | 56 void PushMessagingRouter::FindServiceWorkerRegistration( |
56 const GURL& origin, | 57 const GURL& origin, |
57 int64_t service_worker_registration_id, | 58 int64_t service_worker_registration_id, |
58 const std::string& data, | 59 const PushEventPayload& payload, |
59 const DeliverMessageCallback& deliver_message_callback, | 60 const DeliverMessageCallback& deliver_message_callback, |
60 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { | 61 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { |
61 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 62 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
62 // Try to acquire the registration from storage. If it's already live we'll | 63 // Try to acquire the registration from storage. If it's already live we'll |
63 // receive it right away. If not, it will be revived from storage. | 64 // receive it right away. If not, it will be revived from storage. |
64 service_worker_context->FindReadyRegistrationForId( | 65 service_worker_context->FindReadyRegistrationForId( |
65 service_worker_registration_id, origin, | 66 service_worker_registration_id, origin, |
66 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistrationCallback, | 67 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistrationCallback, |
67 data, deliver_message_callback)); | 68 payload, deliver_message_callback)); |
68 } | 69 } |
69 | 70 |
70 // static | 71 // static |
71 void PushMessagingRouter::FindServiceWorkerRegistrationCallback( | 72 void PushMessagingRouter::FindServiceWorkerRegistrationCallback( |
72 const std::string& data, | 73 const PushEventPayload& payload, |
73 const DeliverMessageCallback& deliver_message_callback, | 74 const DeliverMessageCallback& deliver_message_callback, |
74 ServiceWorkerStatusCode service_worker_status, | 75 ServiceWorkerStatusCode service_worker_status, |
75 const scoped_refptr<ServiceWorkerRegistration>& | 76 const scoped_refptr<ServiceWorkerRegistration>& |
76 service_worker_registration) { | 77 service_worker_registration) { |
77 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
78 // TODO(mvanouwerkerk): UMA logging. | 79 // TODO(mvanouwerkerk): UMA logging. |
79 if (service_worker_status != SERVICE_WORKER_OK) { | 80 if (service_worker_status != SERVICE_WORKER_OK) { |
80 RunDeliverCallback(deliver_message_callback, | 81 RunDeliverCallback(deliver_message_callback, |
81 PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER); | 82 PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER); |
82 return; | 83 return; |
83 } | 84 } |
84 | 85 |
85 ServiceWorkerVersion* version = service_worker_registration->active_version(); | 86 ServiceWorkerVersion* version = service_worker_registration->active_version(); |
86 DCHECK(version); | 87 DCHECK(version); |
87 | 88 |
88 // Hold on to the service worker registration in the callback to keep it | 89 // Hold on to the service worker registration in the callback to keep it |
89 // alive until the callback dies. Otherwise the registration could be | 90 // alive until the callback dies. Otherwise the registration could be |
90 // released when this method returns - before the event is delivered to the | 91 // released when this method returns - before the event is delivered to the |
91 // service worker. | 92 // service worker. |
92 version->RunAfterStartWorker( | 93 version->RunAfterStartWorker( |
93 base::Bind(&PushMessagingRouter::DeliverMessageToWorker, | 94 base::Bind(&PushMessagingRouter::DeliverMessageToWorker, |
94 make_scoped_refptr(version), service_worker_registration, data, | 95 make_scoped_refptr(version), service_worker_registration, |
95 deliver_message_callback), | 96 payload, deliver_message_callback), |
96 base::Bind(&PushMessagingRouter::DeliverMessageEnd, | 97 base::Bind(&PushMessagingRouter::DeliverMessageEnd, |
97 deliver_message_callback, service_worker_registration)); | 98 deliver_message_callback, service_worker_registration)); |
98 } | 99 } |
99 | 100 |
100 // static | 101 // static |
101 void PushMessagingRouter::DeliverMessageToWorker( | 102 void PushMessagingRouter::DeliverMessageToWorker( |
102 const scoped_refptr<ServiceWorkerVersion>& service_worker, | 103 const scoped_refptr<ServiceWorkerVersion>& service_worker, |
103 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | 104 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
104 const std::string& data, | 105 const PushEventPayload& payload, |
105 const DeliverMessageCallback& deliver_message_callback) { | 106 const DeliverMessageCallback& deliver_message_callback) { |
106 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 107 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
107 int request_id = service_worker->StartRequest( | 108 int request_id = service_worker->StartRequest( |
108 ServiceWorkerMetrics::EventType::PUSH, | 109 ServiceWorkerMetrics::EventType::PUSH, |
109 base::Bind(&PushMessagingRouter::DeliverMessageEnd, | 110 base::Bind(&PushMessagingRouter::DeliverMessageEnd, |
110 deliver_message_callback, service_worker_registration)); | 111 deliver_message_callback, service_worker_registration)); |
111 service_worker->DispatchSimpleEvent<ServiceWorkerHostMsg_PushEventFinished>( | 112 service_worker->DispatchSimpleEvent<ServiceWorkerHostMsg_PushEventFinished>( |
112 request_id, ServiceWorkerMsg_PushEvent(request_id, data)); | 113 request_id, ServiceWorkerMsg_PushEvent(request_id, payload)); |
113 } | 114 } |
114 | 115 |
115 // static | 116 // static |
116 void PushMessagingRouter::DeliverMessageEnd( | 117 void PushMessagingRouter::DeliverMessageEnd( |
117 const DeliverMessageCallback& deliver_message_callback, | 118 const DeliverMessageCallback& deliver_message_callback, |
118 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | 119 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
119 ServiceWorkerStatusCode service_worker_status) { | 120 ServiceWorkerStatusCode service_worker_status) { |
120 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 121 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
121 // TODO(mvanouwerkerk): UMA logging. | 122 // TODO(mvanouwerkerk): UMA logging. |
122 PushDeliveryStatus delivery_status = | 123 PushDeliveryStatus delivery_status = |
(...skipping 28 matching lines...) Expand all Loading... |
151 case SERVICE_WORKER_ERROR_MAX_VALUE: | 152 case SERVICE_WORKER_ERROR_MAX_VALUE: |
152 NOTREACHED() << "Got unexpected error code: " << service_worker_status | 153 NOTREACHED() << "Got unexpected error code: " << service_worker_status |
153 << " " << ServiceWorkerStatusToString(service_worker_status); | 154 << " " << ServiceWorkerStatusToString(service_worker_status); |
154 delivery_status = PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR; | 155 delivery_status = PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR; |
155 break; | 156 break; |
156 } | 157 } |
157 RunDeliverCallback(deliver_message_callback, delivery_status); | 158 RunDeliverCallback(deliver_message_callback, delivery_status); |
158 } | 159 } |
159 | 160 |
160 } // namespace content | 161 } // namespace content |
OLD | NEW |