[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / content / child / push_messaging / push_provider.cc
blob927da07baa35fd30984f680cc51c482e0198c973
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"
20 namespace content {
21 namespace {
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 GetServiceWorkerRegistrationId(
30 blink::WebServiceWorkerRegistration* service_worker_registration) {
31 return static_cast<WebServiceWorkerRegistrationImpl*>(
32 service_worker_registration)->registration_id();
35 } // namespace
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);
61 return provider;
64 void PushProvider::OnWorkerRunLoopStopped() {
65 delete this;
68 void PushProvider::subscribe(
69 blink::WebServiceWorkerRegistration* service_worker_registration,
70 const blink::WebPushSubscriptionOptions& options,
71 blink::WebPushSubscriptionCallbacks* callbacks) {
72 DCHECK(service_worker_registration);
73 DCHECK(callbacks);
74 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId());
75 subscription_callbacks_.AddWithID(callbacks, request_id);
76 int64 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);
86 DCHECK(callbacks);
88 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId());
89 unsubscribe_callbacks_.AddWithID(callbacks, request_id);
91 int64 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);
101 DCHECK(callbacks);
102 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId());
103 subscription_callbacks_.AddWithID(callbacks, request_id);
104 int64 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);
115 DCHECK(callbacks);
116 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId());
117 permission_status_callbacks_.AddWithID(callbacks, request_id);
118 int64 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) {
125 bool handled = true;
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,
134 OnUnsubscribeError);
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()
146 return handled;
149 void PushProvider::OnSubscribeFromWorkerSuccess(
150 int request_id,
151 const GURL& endpoint) {
152 blink::WebPushSubscriptionCallbacks* callbacks =
153 subscription_callbacks_.Lookup(request_id);
154 if (!callbacks)
155 return;
157 scoped_ptr<blink::WebPushSubscription> subscription(
158 new blink::WebPushSubscription(endpoint));
159 callbacks->onSuccess(subscription.release());
161 subscription_callbacks_.Remove(request_id);
164 void PushProvider::OnSubscribeFromWorkerError(int request_id,
165 PushRegistrationStatus status) {
166 blink::WebPushSubscriptionCallbacks* callbacks =
167 subscription_callbacks_.Lookup(request_id);
168 if (!callbacks)
169 return;
171 scoped_ptr<blink::WebPushError> error(new blink::WebPushError(
172 blink::WebPushError::ErrorTypeAbort,
173 blink::WebString::fromUTF8(PushRegistrationStatusToString(status))));
174 callbacks->onError(error.release());
176 subscription_callbacks_.Remove(request_id);
179 void PushProvider::OnUnsubscribeSuccess(int request_id, bool did_unsubscribe) {
180 blink::WebPushUnsubscribeCallbacks* callbacks =
181 unsubscribe_callbacks_.Lookup(request_id);
182 if (!callbacks)
183 return;
185 callbacks->onSuccess(&did_unsubscribe);
187 unsubscribe_callbacks_.Remove(request_id);
190 void PushProvider::OnUnsubscribeError(
191 int request_id,
192 blink::WebPushError::ErrorType error_type,
193 const std::string& error_message) {
194 blink::WebPushUnsubscribeCallbacks* callbacks =
195 unsubscribe_callbacks_.Lookup(request_id);
196 if (!callbacks)
197 return;
199 scoped_ptr<blink::WebPushError> error(new blink::WebPushError(
200 error_type, blink::WebString::fromUTF8(error_message)));
201 callbacks->onError(error.release());
203 unsubscribe_callbacks_.Remove(request_id);
206 void PushProvider::OnGetRegistrationSuccess(
207 int request_id,
208 const GURL& endpoint) {
209 blink::WebPushSubscriptionCallbacks* callbacks =
210 subscription_callbacks_.Lookup(request_id);
211 if (!callbacks)
212 return;
214 scoped_ptr<blink::WebPushSubscription> subscription(
215 new blink::WebPushSubscription(endpoint));
216 callbacks->onSuccess(subscription.release());
218 subscription_callbacks_.Remove(request_id);
221 void PushProvider::OnGetRegistrationError(
222 int request_id,
223 PushGetRegistrationStatus status) {
224 blink::WebPushSubscriptionCallbacks* callbacks =
225 subscription_callbacks_.Lookup(request_id);
226 if (!callbacks)
227 return;
229 // We are only expecting an error if we can't find a registration.
230 callbacks->onSuccess(nullptr);
232 subscription_callbacks_.Remove(request_id);
235 void PushProvider::OnGetPermissionStatusSuccess(
236 int request_id,
237 blink::WebPushPermissionStatus status) {
238 blink::WebPushPermissionStatusCallbacks* callbacks =
239 permission_status_callbacks_.Lookup(request_id);
240 if (!callbacks)
241 return;
243 callbacks->onSuccess(&status);
245 permission_status_callbacks_.Remove(request_id);
248 void PushProvider::OnGetPermissionStatusError(
249 int request_id,
250 blink::WebPushError::ErrorType error) {
251 blink::WebPushPermissionStatusCallbacks* callbacks =
252 permission_status_callbacks_.Lookup(request_id);
253 if (!callbacks)
254 return;
256 std::string error_message;
257 if (error == blink::WebPushError::ErrorTypeNotSupported) {
258 error_message =
259 "Push subscriptions that don't enable userVisibleOnly are not "
260 "supported.";
263 scoped_ptr<blink::WebPushError> web_error(new blink::WebPushError(
264 error, blink::WebString::fromUTF8(error_message)));
266 callbacks->onError(web_error.release());
268 permission_status_callbacks_.Remove(request_id);
271 } // namespace content