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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 8 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
9 #include "content/browser/service_worker/service_worker_registration.h" | 9 #include "content/browser/service_worker/service_worker_registration.h" |
10 #include "content/browser/service_worker/service_worker_storage.h" | 10 #include "content/browser/service_worker/service_worker_storage.h" |
11 #include "content/common/service_worker/service_worker_status_code.h" | 11 #include "content/common/service_worker/service_worker_status_code.h" |
12 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "content/public/browser/storage_partition.h" | 14 #include "content/public/browser/storage_partition.h" |
15 | 15 |
16 namespace content { | 16 namespace content { |
17 | 17 |
18 // static | 18 // static |
19 void PushMessagingRouter::DeliverMessage( | 19 void PushMessagingRouter::DeliverMessage( |
20 BrowserContext* browser_context, | 20 BrowserContext* browser_context, |
21 const GURL& origin, | 21 const GURL& origin, |
22 int64 service_worker_registration_id, | 22 int64 service_worker_registration_id, |
23 const std::string& data, | 23 const std::string& data, |
24 const DeliverMessageCallback& deliver_message_callback) { | 24 const DeliverMessageCallback& deliver_message_callback) { |
25 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 25 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
26 StoragePartition* partition = | 26 StoragePartition* partition = |
27 BrowserContext::GetStoragePartitionForSite(browser_context, origin); | 27 BrowserContext::GetStoragePartitionForSite(browser_context, origin); |
28 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = | 28 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context = |
29 static_cast<ServiceWorkerContextWrapper*>( | 29 static_cast<ServiceWorkerContextWrapper*>( |
30 partition->GetServiceWorkerContext()); | 30 partition->GetServiceWorkerContext()); |
31 BrowserThread::PostTask( | 31 BrowserThread::PostTask( |
32 BrowserThread::IO, | 32 BrowserThread::IO, |
33 FROM_HERE, | 33 FROM_HERE, |
34 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistration, | 34 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistration, |
35 origin, | 35 origin, |
36 service_worker_registration_id, | 36 service_worker_registration_id, |
37 data, | 37 data, |
38 deliver_message_callback, | 38 deliver_message_callback, |
39 service_worker_context)); | 39 service_worker_context)); |
40 } | 40 } |
41 | 41 |
42 // static | 42 // static |
43 void PushMessagingRouter::FindServiceWorkerRegistration( | 43 void PushMessagingRouter::FindServiceWorkerRegistration( |
44 const GURL& origin, | 44 const GURL& origin, |
45 int64 service_worker_registration_id, | 45 int64 service_worker_registration_id, |
46 const std::string& data, | 46 const std::string& data, |
47 const DeliverMessageCallback& deliver_message_callback, | 47 const DeliverMessageCallback& deliver_message_callback, |
48 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { | 48 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context) { |
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 49 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
50 // Try to acquire the registration from storage. If it's already live we'll | 50 // Try to acquire the registration from storage. If it's already live we'll |
51 // receive it right away. If not, it will be revived from storage. | 51 // receive it right away. If not, it will be revived from storage. |
52 service_worker_context->context()->storage()->FindRegistrationForId( | 52 service_worker_context->context()->storage()->FindRegistrationForId( |
53 service_worker_registration_id, | 53 service_worker_registration_id, |
54 origin, | 54 origin, |
55 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistrationCallback, | 55 base::Bind(&PushMessagingRouter::FindServiceWorkerRegistrationCallback, |
56 data, | 56 data, |
57 deliver_message_callback)); | 57 deliver_message_callback)); |
58 } | 58 } |
59 | 59 |
60 // static | 60 // static |
61 void PushMessagingRouter::FindServiceWorkerRegistrationCallback( | 61 void PushMessagingRouter::FindServiceWorkerRegistrationCallback( |
62 const std::string& data, | 62 const std::string& data, |
63 const DeliverMessageCallback& deliver_message_callback, | 63 const DeliverMessageCallback& deliver_message_callback, |
64 ServiceWorkerStatusCode service_worker_status, | 64 ServiceWorkerStatusCode service_worker_status, |
65 const scoped_refptr<ServiceWorkerRegistration>& | 65 const scoped_refptr<ServiceWorkerRegistration>& |
66 service_worker_registration) { | 66 service_worker_registration) { |
67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 67 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
68 if (service_worker_status == SERVICE_WORKER_OK) { | 68 if (service_worker_status == SERVICE_WORKER_OK) { |
69 // Hold on to the service worker registration in the callback to keep it | 69 // Hold on to the service worker registration in the callback to keep it |
70 // alive until the callback dies. Otherwise the registration could be | 70 // alive until the callback dies. Otherwise the registration could be |
71 // released when this method returns - before the event is delivered to the | 71 // released when this method returns - before the event is delivered to the |
72 // service worker. | 72 // service worker. |
73 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = | 73 base::Callback<void(ServiceWorkerStatusCode)> dispatch_event_callback = |
74 base::Bind(&PushMessagingRouter::DeliverMessageEnd, | 74 base::Bind(&PushMessagingRouter::DeliverMessageEnd, |
75 deliver_message_callback, | 75 deliver_message_callback, |
76 service_worker_registration); | 76 service_worker_registration); |
77 service_worker_registration->active_version()->DispatchPushEvent( | 77 service_worker_registration->active_version()->DispatchPushEvent( |
78 dispatch_event_callback, data); | 78 dispatch_event_callback, data); |
79 } else { | 79 } else { |
80 // TODO(mvanouwerkerk): UMA logging. | 80 // TODO(mvanouwerkerk): UMA logging. |
81 BrowserThread::PostTask(BrowserThread::UI, | 81 BrowserThread::PostTask(BrowserThread::UI, |
82 FROM_HERE, | 82 FROM_HERE, |
83 base::Bind(deliver_message_callback, | 83 base::Bind(deliver_message_callback, |
84 PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER)); | 84 PUSH_DELIVERY_STATUS_NO_SERVICE_WORKER)); |
85 } | 85 } |
86 } | 86 } |
87 | 87 |
88 // static | 88 // static |
89 void PushMessagingRouter::DeliverMessageEnd( | 89 void PushMessagingRouter::DeliverMessageEnd( |
90 const DeliverMessageCallback& deliver_message_callback, | 90 const DeliverMessageCallback& deliver_message_callback, |
91 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, | 91 const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
92 ServiceWorkerStatusCode service_worker_status) { | 92 ServiceWorkerStatusCode service_worker_status) { |
93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 93 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
94 // TODO(mvanouwerkerk): UMA logging. | 94 // TODO(mvanouwerkerk): UMA logging. |
95 PushDeliveryStatus delivery_status = | 95 PushDeliveryStatus delivery_status = |
96 PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR; | 96 PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR; |
97 switch (service_worker_status) { | 97 switch (service_worker_status) { |
98 case SERVICE_WORKER_OK: | 98 case SERVICE_WORKER_OK: |
99 delivery_status = PUSH_DELIVERY_STATUS_SUCCESS; | 99 delivery_status = PUSH_DELIVERY_STATUS_SUCCESS; |
100 break; | 100 break; |
101 case SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED: | 101 case SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED: |
102 delivery_status = PUSH_DELIVERY_STATUS_EVENT_WAITUNTIL_REJECTED; | 102 delivery_status = PUSH_DELIVERY_STATUS_EVENT_WAITUNTIL_REJECTED; |
103 break; | 103 break; |
(...skipping 18 matching lines...) Expand all Loading... |
122 delivery_status = PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR; | 122 delivery_status = PUSH_DELIVERY_STATUS_SERVICE_WORKER_ERROR; |
123 break; | 123 break; |
124 } | 124 } |
125 BrowserThread::PostTask( | 125 BrowserThread::PostTask( |
126 BrowserThread::UI, | 126 BrowserThread::UI, |
127 FROM_HERE, | 127 FROM_HERE, |
128 base::Bind(deliver_message_callback, delivery_status)); | 128 base::Bind(deliver_message_callback, delivery_status)); |
129 } | 129 } |
130 | 130 |
131 } // namespace content | 131 } // namespace content |
OLD | NEW |