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
3 // found in the LICENSE file.
5 #include "content/child/push_messaging/push_provider.h"
7 #include "base/lazy_instance.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/stl_util.h"
10 #include "base/threading/thread_local.h"
11 #include "content/child/push_messaging/push_dispatcher.h"
12 #include "content/child/service_worker/web_service_worker_registration_impl.h"
13 #include "content/child/thread_safe_sender.h"
14 #include "content/child/worker_task_runner.h"
15 #include "content/common/push_messaging_messages.h"
16 #include "third_party/WebKit/public/platform/WebString.h"
17 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubscription.h"
18 #include "third_party/WebKit/public/platform/modules/push_messaging/WebPushSubscriptionOptions.h"
23 int CurrentWorkerId() {
24 return WorkerTaskRunner::Instance()->CurrentWorkerId();
27 // Returns the id of the given |service_worker_registration|, which
28 // is only available on the implementation of the interface.
29 int64_t GetServiceWorkerRegistrationId(
30 blink::WebServiceWorkerRegistration
* service_worker_registration
) {
31 return static_cast<WebServiceWorkerRegistrationImpl
*>(
32 service_worker_registration
)->registration_id();
37 static base::LazyInstance
<base::ThreadLocalPointer
<PushProvider
>>::Leaky
38 g_push_provider_tls
= LAZY_INSTANCE_INITIALIZER
;
40 PushProvider::PushProvider(ThreadSafeSender
* thread_safe_sender
,
41 PushDispatcher
* push_dispatcher
)
42 : thread_safe_sender_(thread_safe_sender
),
43 push_dispatcher_(push_dispatcher
) {
44 g_push_provider_tls
.Pointer()->Set(this);
47 PushProvider::~PushProvider() {
48 g_push_provider_tls
.Pointer()->Set(nullptr);
51 PushProvider
* PushProvider::ThreadSpecificInstance(
52 ThreadSafeSender
* thread_safe_sender
,
53 PushDispatcher
* push_dispatcher
) {
54 if (g_push_provider_tls
.Pointer()->Get())
55 return g_push_provider_tls
.Pointer()->Get();
57 PushProvider
* provider
=
58 new PushProvider(thread_safe_sender
, push_dispatcher
);
59 if (CurrentWorkerId())
60 WorkerTaskRunner::Instance()->AddStopObserver(provider
);
64 void PushProvider::OnWorkerRunLoopStopped() {
68 void PushProvider::subscribe(
69 blink::WebServiceWorkerRegistration
* service_worker_registration
,
70 const blink::WebPushSubscriptionOptions
& options
,
71 blink::WebPushSubscriptionCallbacks
* callbacks
) {
72 DCHECK(service_worker_registration
);
74 int request_id
= push_dispatcher_
->GenerateRequestId(CurrentWorkerId());
75 subscription_callbacks_
.AddWithID(callbacks
, request_id
);
76 int64_t service_worker_registration_id
=
77 GetServiceWorkerRegistrationId(service_worker_registration
);
78 thread_safe_sender_
->Send(new PushMessagingHostMsg_SubscribeFromWorker(
79 request_id
, service_worker_registration_id
, options
.userVisibleOnly
));
82 void PushProvider::unsubscribe(
83 blink::WebServiceWorkerRegistration
* service_worker_registration
,
84 blink::WebPushUnsubscribeCallbacks
* callbacks
) {
85 DCHECK(service_worker_registration
);
88 int request_id
= push_dispatcher_
->GenerateRequestId(CurrentWorkerId());
89 unsubscribe_callbacks_
.AddWithID(callbacks
, request_id
);
91 int64_t service_worker_registration_id
=
92 GetServiceWorkerRegistrationId(service_worker_registration
);
93 thread_safe_sender_
->Send(new PushMessagingHostMsg_Unsubscribe(
94 request_id
, service_worker_registration_id
));
97 void PushProvider::getSubscription(
98 blink::WebServiceWorkerRegistration
* service_worker_registration
,
99 blink::WebPushSubscriptionCallbacks
* callbacks
) {
100 DCHECK(service_worker_registration
);
102 int request_id
= push_dispatcher_
->GenerateRequestId(CurrentWorkerId());
103 subscription_callbacks_
.AddWithID(callbacks
, request_id
);
104 int64_t service_worker_registration_id
=
105 GetServiceWorkerRegistrationId(service_worker_registration
);
106 thread_safe_sender_
->Send(new PushMessagingHostMsg_GetRegistration(
107 request_id
, service_worker_registration_id
));
110 void PushProvider::getPermissionStatus(
111 blink::WebServiceWorkerRegistration
* service_worker_registration
,
112 const blink::WebPushSubscriptionOptions
& options
,
113 blink::WebPushPermissionStatusCallbacks
* callbacks
) {
114 DCHECK(service_worker_registration
);
116 int request_id
= push_dispatcher_
->GenerateRequestId(CurrentWorkerId());
117 permission_status_callbacks_
.AddWithID(callbacks
, request_id
);
118 int64_t service_worker_registration_id
=
119 GetServiceWorkerRegistrationId(service_worker_registration
);
120 thread_safe_sender_
->Send(new PushMessagingHostMsg_GetPermissionStatus(
121 request_id
, service_worker_registration_id
, options
.userVisibleOnly
));
124 bool PushProvider::OnMessageReceived(const IPC::Message
& message
) {
126 IPC_BEGIN_MESSAGE_MAP(PushProvider
, message
)
127 IPC_MESSAGE_HANDLER(PushMessagingMsg_SubscribeFromWorkerSuccess
,
128 OnSubscribeFromWorkerSuccess
);
129 IPC_MESSAGE_HANDLER(PushMessagingMsg_SubscribeFromWorkerError
,
130 OnSubscribeFromWorkerError
);
131 IPC_MESSAGE_HANDLER(PushMessagingMsg_UnsubscribeSuccess
,
132 OnUnsubscribeSuccess
);
133 IPC_MESSAGE_HANDLER(PushMessagingMsg_UnsubscribeError
,
135 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetRegistrationSuccess
,
136 OnGetRegistrationSuccess
);
137 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetRegistrationError
,
138 OnGetRegistrationError
);
139 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusSuccess
,
140 OnGetPermissionStatusSuccess
);
141 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusError
,
142 OnGetPermissionStatusError
);
143 IPC_MESSAGE_UNHANDLED(handled
= false)
144 IPC_END_MESSAGE_MAP()
149 void PushProvider::OnSubscribeFromWorkerSuccess(
151 const GURL
& endpoint
,
152 const std::vector
<uint8_t>& curve25519dh
) {
153 blink::WebPushSubscriptionCallbacks
* callbacks
=
154 subscription_callbacks_
.Lookup(request_id
);
158 scoped_ptr
<blink::WebPushSubscription
> subscription(
159 new blink::WebPushSubscription(endpoint
, curve25519dh
));
160 callbacks
->onSuccess(subscription
.release());
162 subscription_callbacks_
.Remove(request_id
);
165 void PushProvider::OnSubscribeFromWorkerError(int request_id
,
166 PushRegistrationStatus status
) {
167 blink::WebPushSubscriptionCallbacks
* callbacks
=
168 subscription_callbacks_
.Lookup(request_id
);
172 scoped_ptr
<blink::WebPushError
> error(new blink::WebPushError(
173 blink::WebPushError::ErrorTypeAbort
,
174 blink::WebString::fromUTF8(PushRegistrationStatusToString(status
))));
175 callbacks
->onError(error
.release());
177 subscription_callbacks_
.Remove(request_id
);
180 void PushProvider::OnUnsubscribeSuccess(int request_id
, bool did_unsubscribe
) {
181 blink::WebPushUnsubscribeCallbacks
* callbacks
=
182 unsubscribe_callbacks_
.Lookup(request_id
);
186 callbacks
->onSuccess(&did_unsubscribe
);
188 unsubscribe_callbacks_
.Remove(request_id
);
191 void PushProvider::OnUnsubscribeError(
193 blink::WebPushError::ErrorType error_type
,
194 const std::string
& error_message
) {
195 blink::WebPushUnsubscribeCallbacks
* callbacks
=
196 unsubscribe_callbacks_
.Lookup(request_id
);
200 scoped_ptr
<blink::WebPushError
> error(new blink::WebPushError(
201 error_type
, blink::WebString::fromUTF8(error_message
)));
202 callbacks
->onError(error
.release());
204 unsubscribe_callbacks_
.Remove(request_id
);
207 void PushProvider::OnGetRegistrationSuccess(
209 const GURL
& endpoint
,
210 const std::vector
<uint8_t>& curve25519dh
) {
211 blink::WebPushSubscriptionCallbacks
* callbacks
=
212 subscription_callbacks_
.Lookup(request_id
);
216 scoped_ptr
<blink::WebPushSubscription
> subscription(
217 new blink::WebPushSubscription(endpoint
, curve25519dh
));
218 callbacks
->onSuccess(subscription
.release());
220 subscription_callbacks_
.Remove(request_id
);
223 void PushProvider::OnGetRegistrationError(
225 PushGetRegistrationStatus status
) {
226 blink::WebPushSubscriptionCallbacks
* callbacks
=
227 subscription_callbacks_
.Lookup(request_id
);
231 // We are only expecting an error if we can't find a registration.
232 callbacks
->onSuccess(nullptr);
234 subscription_callbacks_
.Remove(request_id
);
237 void PushProvider::OnGetPermissionStatusSuccess(
239 blink::WebPushPermissionStatus status
) {
240 blink::WebPushPermissionStatusCallbacks
* callbacks
=
241 permission_status_callbacks_
.Lookup(request_id
);
245 callbacks
->onSuccess(&status
);
247 permission_status_callbacks_
.Remove(request_id
);
250 void PushProvider::OnGetPermissionStatusError(
252 blink::WebPushError::ErrorType error
) {
253 blink::WebPushPermissionStatusCallbacks
* callbacks
=
254 permission_status_callbacks_
.Lookup(request_id
);
258 std::string error_message
;
259 if (error
== blink::WebPushError::ErrorTypeNotSupported
) {
261 "Push subscriptions that don't enable userVisibleOnly are not "
265 scoped_ptr
<blink::WebPushError
> web_error(new blink::WebPushError(
266 error
, blink::WebString::fromUTF8(error_message
)));
268 callbacks
->onError(web_error
.release());
270 permission_status_callbacks_
.Remove(request_id
);
273 } // namespace content