Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / child / push_messaging / push_provider.cc
blob23033b5820d76617cf177a8f7dc8739fff6cc2d8
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_RegisterFromWorker(
79 request_id, service_worker_registration_id, options.userVisible));
82 void PushProvider::registerPushMessaging(
83 blink::WebServiceWorkerRegistration* service_worker_registration,
84 blink::WebPushSubscriptionCallbacks* callbacks) {
85 subscribe(service_worker_registration,
86 blink::WebPushSubscriptionOptions(),
87 callbacks);
90 void PushProvider::unsubscribe(
91 blink::WebServiceWorkerRegistration* service_worker_registration,
92 blink::WebPushUnsubscribeCallbacks* callbacks) {
93 DCHECK(service_worker_registration);
94 DCHECK(callbacks);
96 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId());
97 unsubscribe_callbacks_.AddWithID(callbacks, request_id);
99 int64 service_worker_registration_id =
100 GetServiceWorkerRegistrationId(service_worker_registration);
101 thread_safe_sender_->Send(new PushMessagingHostMsg_Unregister(
102 request_id, service_worker_registration_id));
105 void PushProvider::unregister(
106 blink::WebServiceWorkerRegistration* service_worker_registration,
107 blink::WebPushUnsubscribeCallbacks* callbacks) {
108 unsubscribe(service_worker_registration, callbacks);
111 void PushProvider::getSubscription(
112 blink::WebServiceWorkerRegistration* service_worker_registration,
113 blink::WebPushSubscriptionCallbacks* callbacks) {
114 DCHECK(service_worker_registration);
115 DCHECK(callbacks);
116 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId());
117 subscription_callbacks_.AddWithID(callbacks, request_id);
118 int64 service_worker_registration_id =
119 GetServiceWorkerRegistrationId(service_worker_registration);
120 thread_safe_sender_->Send(new PushMessagingHostMsg_GetRegistration(
121 request_id, service_worker_registration_id));
124 void PushProvider::getRegistration(
125 blink::WebServiceWorkerRegistration* service_worker_registration,
126 blink::WebPushSubscriptionCallbacks* callbacks) {
127 getSubscription(service_worker_registration, callbacks);
130 void PushProvider::getPermissionStatus(
131 blink::WebServiceWorkerRegistration* service_worker_registration,
132 const blink::WebPushSubscriptionOptions& options,
133 blink::WebPushPermissionStatusCallbacks* callbacks) {
134 DCHECK(service_worker_registration);
135 DCHECK(callbacks);
136 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId());
137 permission_status_callbacks_.AddWithID(callbacks, request_id);
138 int64 service_worker_registration_id =
139 GetServiceWorkerRegistrationId(service_worker_registration);
140 thread_safe_sender_->Send(new PushMessagingHostMsg_GetPermissionStatus(
141 request_id, service_worker_registration_id, options.userVisible));
144 void PushProvider::getPermissionStatus(
145 blink::WebServiceWorkerRegistration* service_worker_registration,
146 blink::WebPushPermissionStatusCallbacks* callbacks) {
147 getPermissionStatus(service_worker_registration,
148 blink::WebPushSubscriptionOptions(),
149 callbacks);
152 bool PushProvider::OnMessageReceived(const IPC::Message& message) {
153 bool handled = true;
154 IPC_BEGIN_MESSAGE_MAP(PushProvider, message)
155 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromWorkerSuccess,
156 OnRegisterFromWorkerSuccess);
157 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromWorkerError,
158 OnRegisterFromWorkerError);
159 IPC_MESSAGE_HANDLER(PushMessagingMsg_UnregisterSuccess,
160 OnUnregisterSuccess);
161 IPC_MESSAGE_HANDLER(PushMessagingMsg_UnregisterError,
162 OnUnregisterError);
163 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetRegistrationSuccess,
164 OnGetRegistrationSuccess);
165 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetRegistrationError,
166 OnGetRegistrationError);
167 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusSuccess,
168 OnGetPermissionStatusSuccess);
169 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusError,
170 OnGetPermissionStatusError);
171 IPC_MESSAGE_UNHANDLED(handled = false)
172 IPC_END_MESSAGE_MAP()
174 return handled;
177 void PushProvider::OnRegisterFromWorkerSuccess(
178 int request_id,
179 const GURL& endpoint,
180 const std::string& registration_id) {
181 blink::WebPushSubscriptionCallbacks* callbacks =
182 subscription_callbacks_.Lookup(request_id);
183 if (!callbacks)
184 return;
186 scoped_ptr<blink::WebPushSubscription> subscription(
187 new blink::WebPushSubscription(
188 blink::WebString::fromUTF8(endpoint.spec()),
189 blink::WebString::fromUTF8(registration_id)));
190 callbacks->onSuccess(subscription.release());
192 subscription_callbacks_.Remove(request_id);
195 void PushProvider::OnRegisterFromWorkerError(int request_id,
196 PushRegistrationStatus status) {
197 blink::WebPushSubscriptionCallbacks* callbacks =
198 subscription_callbacks_.Lookup(request_id);
199 if (!callbacks)
200 return;
202 scoped_ptr<blink::WebPushError> error(new blink::WebPushError(
203 blink::WebPushError::ErrorTypeAbort,
204 blink::WebString::fromUTF8(PushRegistrationStatusToString(status))));
205 callbacks->onError(error.release());
207 subscription_callbacks_.Remove(request_id);
210 void PushProvider::OnUnregisterSuccess(int request_id, bool did_unregister) {
211 blink::WebPushUnsubscribeCallbacks* callbacks =
212 unsubscribe_callbacks_.Lookup(request_id);
213 if (!callbacks)
214 return;
216 callbacks->onSuccess(&did_unregister);
218 unsubscribe_callbacks_.Remove(request_id);
221 void PushProvider::OnUnregisterError(
222 int request_id,
223 blink::WebPushError::ErrorType error_type,
224 const std::string& error_message) {
225 blink::WebPushUnsubscribeCallbacks* callbacks =
226 unsubscribe_callbacks_.Lookup(request_id);
227 if (!callbacks)
228 return;
230 scoped_ptr<blink::WebPushError> error(new blink::WebPushError(
231 error_type, blink::WebString::fromUTF8(error_message)));
232 callbacks->onError(error.release());
234 unsubscribe_callbacks_.Remove(request_id);
237 void PushProvider::OnGetRegistrationSuccess(
238 int request_id,
239 const GURL& endpoint,
240 const std::string& registration_id) {
241 blink::WebPushSubscriptionCallbacks* callbacks =
242 subscription_callbacks_.Lookup(request_id);
243 if (!callbacks)
244 return;
246 scoped_ptr<blink::WebPushSubscription> subscription(
247 new blink::WebPushSubscription(
248 blink::WebString::fromUTF8(endpoint.spec()),
249 blink::WebString::fromUTF8(registration_id)));
250 callbacks->onSuccess(subscription.release());
252 subscription_callbacks_.Remove(request_id);
255 void PushProvider::OnGetRegistrationError(
256 int request_id,
257 PushGetRegistrationStatus status) {
258 blink::WebPushSubscriptionCallbacks* callbacks =
259 subscription_callbacks_.Lookup(request_id);
260 if (!callbacks)
261 return;
263 // We are only expecting an error if we can't find a registration.
264 callbacks->onSuccess(nullptr);
266 subscription_callbacks_.Remove(request_id);
269 void PushProvider::OnGetPermissionStatusSuccess(
270 int request_id,
271 blink::WebPushPermissionStatus status) {
272 blink::WebPushPermissionStatusCallbacks* callbacks =
273 permission_status_callbacks_.Lookup(request_id);
274 if (!callbacks)
275 return;
277 callbacks->onSuccess(&status);
279 permission_status_callbacks_.Remove(request_id);
282 void PushProvider::OnGetPermissionStatusError(int request_id) {
283 blink::WebPushPermissionStatusCallbacks* callbacks =
284 permission_status_callbacks_.Lookup(request_id);
285 if (!callbacks)
286 return;
288 callbacks->onError();
290 permission_status_callbacks_.Remove(request_id);
293 } // namespace content