| 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/child/push_messaging/push_provider.h" | 5 #include "content/child/push_messaging/push_provider.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "base/threading/thread_local.h" | 10 #include "base/threading/thread_local.h" |
| 11 #include "content/child/push_messaging/push_dispatcher.h" | 11 #include "content/child/push_messaging/push_dispatcher.h" |
| 12 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 12 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
| 13 #include "content/child/thread_safe_sender.h" | 13 #include "content/child/thread_safe_sender.h" |
| 14 #include "content/child/worker_task_runner.h" | 14 #include "content/child/worker_task_runner.h" |
| 15 #include "content/common/push_messaging_messages.h" | 15 #include "content/common/push_messaging_messages.h" |
| 16 #include "third_party/WebKit/public/platform/WebString.h" | 16 #include "third_party/WebKit/public/platform/WebString.h" |
| 17 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubsc
ription.h" | 17 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubsc
ription.h" |
| 18 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubsc
riptionOptions.h" | 18 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubsc
riptionOptions.h" |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 int CurrentWorkerId() { | 23 int CurrentWorkerId() { |
| 24 return WorkerTaskRunner::Instance()->CurrentWorkerId(); | 24 return WorkerTaskRunner::Instance()->CurrentWorkerId(); |
| 25 } | 25 } |
| 26 | 26 |
| 27 // Returns the id of the given |service_worker_registration|, which | 27 // Returns the id of the given |service_worker_registration|, which |
| 28 // is only available on the implementation of the interface. | 28 // is only available on the implementation of the interface. |
| 29 int64 GetServiceWorkerRegistrationId( | 29 int64_t GetServiceWorkerRegistrationId( |
| 30 blink::WebServiceWorkerRegistration* service_worker_registration) { | 30 blink::WebServiceWorkerRegistration* service_worker_registration) { |
| 31 return static_cast<WebServiceWorkerRegistrationImpl*>( | 31 return static_cast<WebServiceWorkerRegistrationImpl*>( |
| 32 service_worker_registration)->registration_id(); | 32 service_worker_registration)->registration_id(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 GURL GetServiceWorkerRegistrationOrigin( |
| 36 blink::WebServiceWorkerRegistration* service_worker_registration) { |
| 37 blink::WebURL scope = static_cast<WebServiceWorkerRegistrationImpl*>( |
| 38 service_worker_registration)->scope(); |
| 39 |
| 40 return GURL(scope).GetOrigin(); |
| 41 } |
| 42 |
| 35 } // namespace | 43 } // namespace |
| 36 | 44 |
| 37 static base::LazyInstance<base::ThreadLocalPointer<PushProvider>>::Leaky | 45 static base::LazyInstance<base::ThreadLocalPointer<PushProvider>>::Leaky |
| 38 g_push_provider_tls = LAZY_INSTANCE_INITIALIZER; | 46 g_push_provider_tls = LAZY_INSTANCE_INITIALIZER; |
| 39 | 47 |
| 40 PushProvider::PushProvider(ThreadSafeSender* thread_safe_sender, | 48 PushProvider::PushProvider(ThreadSafeSender* thread_safe_sender, |
| 41 PushDispatcher* push_dispatcher) | 49 PushDispatcher* push_dispatcher) |
| 42 : thread_safe_sender_(thread_safe_sender), | 50 : thread_safe_sender_(thread_safe_sender), |
| 43 push_dispatcher_(push_dispatcher) { | 51 push_dispatcher_(push_dispatcher) { |
| 44 g_push_provider_tls.Pointer()->Set(this); | 52 g_push_provider_tls.Pointer()->Set(this); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 66 } | 74 } |
| 67 | 75 |
| 68 void PushProvider::subscribe( | 76 void PushProvider::subscribe( |
| 69 blink::WebServiceWorkerRegistration* service_worker_registration, | 77 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 70 const blink::WebPushSubscriptionOptions& options, | 78 const blink::WebPushSubscriptionOptions& options, |
| 71 blink::WebPushSubscriptionCallbacks* callbacks) { | 79 blink::WebPushSubscriptionCallbacks* callbacks) { |
| 72 DCHECK(service_worker_registration); | 80 DCHECK(service_worker_registration); |
| 73 DCHECK(callbacks); | 81 DCHECK(callbacks); |
| 74 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); | 82 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); |
| 75 subscription_callbacks_.AddWithID(callbacks, request_id); | 83 subscription_callbacks_.AddWithID(callbacks, request_id); |
| 76 int64 service_worker_registration_id = | 84 int64_t service_worker_registration_id = |
| 77 GetServiceWorkerRegistrationId(service_worker_registration); | 85 GetServiceWorkerRegistrationId(service_worker_registration); |
| 78 thread_safe_sender_->Send(new PushMessagingHostMsg_SubscribeFromWorker( | 86 thread_safe_sender_->Send(new PushMessagingHostMsg_SubscribeFromWorker( |
| 79 request_id, service_worker_registration_id, options.userVisibleOnly)); | 87 request_id, service_worker_registration_id, options.userVisibleOnly)); |
| 80 } | 88 } |
| 81 | 89 |
| 82 void PushProvider::unsubscribe( | 90 void PushProvider::unsubscribe( |
| 83 blink::WebServiceWorkerRegistration* service_worker_registration, | 91 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 84 blink::WebPushUnsubscribeCallbacks* callbacks) { | 92 blink::WebPushUnsubscribeCallbacks* callbacks) { |
| 85 DCHECK(service_worker_registration); | 93 DCHECK(service_worker_registration); |
| 86 DCHECK(callbacks); | 94 DCHECK(callbacks); |
| 87 | 95 |
| 88 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); | 96 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); |
| 89 unsubscribe_callbacks_.AddWithID(callbacks, request_id); | 97 unsubscribe_callbacks_.AddWithID(callbacks, request_id); |
| 90 | 98 |
| 91 int64 service_worker_registration_id = | 99 int64_t service_worker_registration_id = |
| 92 GetServiceWorkerRegistrationId(service_worker_registration); | 100 GetServiceWorkerRegistrationId(service_worker_registration); |
| 93 thread_safe_sender_->Send(new PushMessagingHostMsg_Unsubscribe( | 101 thread_safe_sender_->Send(new PushMessagingHostMsg_Unsubscribe( |
| 94 request_id, service_worker_registration_id)); | 102 request_id, service_worker_registration_id)); |
| 95 } | 103 } |
| 96 | 104 |
| 97 void PushProvider::getSubscription( | 105 void PushProvider::getSubscription( |
| 98 blink::WebServiceWorkerRegistration* service_worker_registration, | 106 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 99 blink::WebPushSubscriptionCallbacks* callbacks) { | 107 blink::WebPushSubscriptionCallbacks* callbacks) { |
| 100 DCHECK(service_worker_registration); | 108 DCHECK(service_worker_registration); |
| 101 DCHECK(callbacks); | 109 DCHECK(callbacks); |
| 102 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); | 110 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); |
| 103 subscription_callbacks_.AddWithID(callbacks, request_id); | 111 subscription_callbacks_.AddWithID(callbacks, request_id); |
| 104 int64 service_worker_registration_id = | 112 GURL origin = GetServiceWorkerRegistrationOrigin(service_worker_registration); |
| 113 int64_t service_worker_registration_id = |
| 105 GetServiceWorkerRegistrationId(service_worker_registration); | 114 GetServiceWorkerRegistrationId(service_worker_registration); |
| 106 thread_safe_sender_->Send(new PushMessagingHostMsg_GetRegistration( | 115 thread_safe_sender_->Send(new PushMessagingHostMsg_GetRegistration( |
| 107 request_id, service_worker_registration_id)); | 116 request_id, origin, service_worker_registration_id)); |
| 108 } | 117 } |
| 109 | 118 |
| 110 void PushProvider::getPermissionStatus( | 119 void PushProvider::getPermissionStatus( |
| 111 blink::WebServiceWorkerRegistration* service_worker_registration, | 120 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 112 const blink::WebPushSubscriptionOptions& options, | 121 const blink::WebPushSubscriptionOptions& options, |
| 113 blink::WebPushPermissionStatusCallbacks* callbacks) { | 122 blink::WebPushPermissionStatusCallbacks* callbacks) { |
| 114 DCHECK(service_worker_registration); | 123 DCHECK(service_worker_registration); |
| 115 DCHECK(callbacks); | 124 DCHECK(callbacks); |
| 116 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); | 125 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); |
| 117 permission_status_callbacks_.AddWithID(callbacks, request_id); | 126 permission_status_callbacks_.AddWithID(callbacks, request_id); |
| 118 int64 service_worker_registration_id = | 127 int64_t service_worker_registration_id = |
| 119 GetServiceWorkerRegistrationId(service_worker_registration); | 128 GetServiceWorkerRegistrationId(service_worker_registration); |
| 120 thread_safe_sender_->Send(new PushMessagingHostMsg_GetPermissionStatus( | 129 thread_safe_sender_->Send(new PushMessagingHostMsg_GetPermissionStatus( |
| 121 request_id, service_worker_registration_id, options.userVisibleOnly)); | 130 request_id, service_worker_registration_id, options.userVisibleOnly)); |
| 122 } | 131 } |
| 123 | 132 |
| 124 bool PushProvider::OnMessageReceived(const IPC::Message& message) { | 133 bool PushProvider::OnMessageReceived(const IPC::Message& message) { |
| 125 bool handled = true; | 134 bool handled = true; |
| 126 IPC_BEGIN_MESSAGE_MAP(PushProvider, message) | 135 IPC_BEGIN_MESSAGE_MAP(PushProvider, message) |
| 127 IPC_MESSAGE_HANDLER(PushMessagingMsg_SubscribeFromWorkerSuccess, | 136 IPC_MESSAGE_HANDLER(PushMessagingMsg_SubscribeFromWorkerSuccess, |
| 128 OnSubscribeFromWorkerSuccess); | 137 OnSubscribeFromWorkerSuccess); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 141 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusError, | 150 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusError, |
| 142 OnGetPermissionStatusError); | 151 OnGetPermissionStatusError); |
| 143 IPC_MESSAGE_UNHANDLED(handled = false) | 152 IPC_MESSAGE_UNHANDLED(handled = false) |
| 144 IPC_END_MESSAGE_MAP() | 153 IPC_END_MESSAGE_MAP() |
| 145 | 154 |
| 146 return handled; | 155 return handled; |
| 147 } | 156 } |
| 148 | 157 |
| 149 void PushProvider::OnSubscribeFromWorkerSuccess( | 158 void PushProvider::OnSubscribeFromWorkerSuccess( |
| 150 int request_id, | 159 int request_id, |
| 151 const GURL& endpoint) { | 160 const GURL& endpoint, |
| 161 const std::vector<uint8_t>& curve25519dh) { |
| 152 blink::WebPushSubscriptionCallbacks* callbacks = | 162 blink::WebPushSubscriptionCallbacks* callbacks = |
| 153 subscription_callbacks_.Lookup(request_id); | 163 subscription_callbacks_.Lookup(request_id); |
| 154 if (!callbacks) | 164 if (!callbacks) |
| 155 return; | 165 return; |
| 156 | 166 |
| 157 scoped_ptr<blink::WebPushSubscription> subscription( | 167 scoped_ptr<blink::WebPushSubscription> subscription( |
| 158 new blink::WebPushSubscription(endpoint)); | 168 new blink::WebPushSubscription(endpoint, curve25519dh)); |
| 159 callbacks->onSuccess(subscription.release()); | 169 callbacks->onSuccess(subscription.release()); |
| 160 | 170 |
| 161 subscription_callbacks_.Remove(request_id); | 171 subscription_callbacks_.Remove(request_id); |
| 162 } | 172 } |
| 163 | 173 |
| 164 void PushProvider::OnSubscribeFromWorkerError(int request_id, | 174 void PushProvider::OnSubscribeFromWorkerError(int request_id, |
| 165 PushRegistrationStatus status) { | 175 PushRegistrationStatus status) { |
| 166 blink::WebPushSubscriptionCallbacks* callbacks = | 176 blink::WebPushSubscriptionCallbacks* callbacks = |
| 167 subscription_callbacks_.Lookup(request_id); | 177 subscription_callbacks_.Lookup(request_id); |
| 168 if (!callbacks) | 178 if (!callbacks) |
| (...skipping 29 matching lines...) Expand all Loading... |
| 198 | 208 |
| 199 scoped_ptr<blink::WebPushError> error(new blink::WebPushError( | 209 scoped_ptr<blink::WebPushError> error(new blink::WebPushError( |
| 200 error_type, blink::WebString::fromUTF8(error_message))); | 210 error_type, blink::WebString::fromUTF8(error_message))); |
| 201 callbacks->onError(error.release()); | 211 callbacks->onError(error.release()); |
| 202 | 212 |
| 203 unsubscribe_callbacks_.Remove(request_id); | 213 unsubscribe_callbacks_.Remove(request_id); |
| 204 } | 214 } |
| 205 | 215 |
| 206 void PushProvider::OnGetRegistrationSuccess( | 216 void PushProvider::OnGetRegistrationSuccess( |
| 207 int request_id, | 217 int request_id, |
| 208 const GURL& endpoint) { | 218 const GURL& endpoint, |
| 219 const std::vector<uint8_t>& curve25519dh) { |
| 209 blink::WebPushSubscriptionCallbacks* callbacks = | 220 blink::WebPushSubscriptionCallbacks* callbacks = |
| 210 subscription_callbacks_.Lookup(request_id); | 221 subscription_callbacks_.Lookup(request_id); |
| 211 if (!callbacks) | 222 if (!callbacks) |
| 212 return; | 223 return; |
| 213 | 224 |
| 214 scoped_ptr<blink::WebPushSubscription> subscription( | 225 scoped_ptr<blink::WebPushSubscription> subscription( |
| 215 new blink::WebPushSubscription(endpoint)); | 226 new blink::WebPushSubscription(endpoint, curve25519dh)); |
| 216 callbacks->onSuccess(subscription.release()); | 227 callbacks->onSuccess(subscription.release()); |
| 217 | 228 |
| 218 subscription_callbacks_.Remove(request_id); | 229 subscription_callbacks_.Remove(request_id); |
| 219 } | 230 } |
| 220 | 231 |
| 221 void PushProvider::OnGetRegistrationError( | 232 void PushProvider::OnGetRegistrationError( |
| 222 int request_id, | 233 int request_id, |
| 223 PushGetRegistrationStatus status) { | 234 PushGetRegistrationStatus status) { |
| 224 blink::WebPushSubscriptionCallbacks* callbacks = | 235 blink::WebPushSubscriptionCallbacks* callbacks = |
| 225 subscription_callbacks_.Lookup(request_id); | 236 subscription_callbacks_.Lookup(request_id); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 | 273 |
| 263 scoped_ptr<blink::WebPushError> web_error(new blink::WebPushError( | 274 scoped_ptr<blink::WebPushError> web_error(new blink::WebPushError( |
| 264 error, blink::WebString::fromUTF8(error_message))); | 275 error, blink::WebString::fromUTF8(error_message))); |
| 265 | 276 |
| 266 callbacks->onError(web_error.release()); | 277 callbacks->onError(web_error.release()); |
| 267 | 278 |
| 268 permission_status_callbacks_.Remove(request_id); | 279 permission_status_callbacks_.Remove(request_id); |
| 269 } | 280 } |
| 270 | 281 |
| 271 } // namespace content | 282 } // namespace content |
| OLD | NEW |