[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / content / child / push_messaging / push_provider.cc
blobd503f507dd4c02b7413c1c2b54e155d4a47a69ad
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_t 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_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);
86 DCHECK(callbacks);
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);
101 DCHECK(callbacks);
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);
115 DCHECK(callbacks);
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) {
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 const std::vector<uint8_t>& curve25519dh) {
153 blink::WebPushSubscriptionCallbacks* callbacks =
154 subscription_callbacks_.Lookup(request_id);
155 if (!callbacks)
156 return;
158 callbacks->onSuccess(blink::adoptWebPtr(
159 new blink::WebPushSubscription(endpoint, curve25519dh)));
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 callbacks->onError(blink::WebPushError(
172 blink::WebPushError::ErrorTypeAbort,
173 blink::WebString::fromUTF8(PushRegistrationStatusToString(status))));
175 subscription_callbacks_.Remove(request_id);
178 void PushProvider::OnUnsubscribeSuccess(int request_id, bool did_unsubscribe) {
179 blink::WebPushUnsubscribeCallbacks* callbacks =
180 unsubscribe_callbacks_.Lookup(request_id);
181 if (!callbacks)
182 return;
184 callbacks->onSuccess(did_unsubscribe);
186 unsubscribe_callbacks_.Remove(request_id);
189 void PushProvider::OnUnsubscribeError(
190 int request_id,
191 blink::WebPushError::ErrorType error_type,
192 const std::string& error_message) {
193 blink::WebPushUnsubscribeCallbacks* callbacks =
194 unsubscribe_callbacks_.Lookup(request_id);
195 if (!callbacks)
196 return;
198 callbacks->onError(blink::WebPushError(
199 error_type, blink::WebString::fromUTF8(error_message)));
201 unsubscribe_callbacks_.Remove(request_id);
204 void PushProvider::OnGetRegistrationSuccess(
205 int request_id,
206 const GURL& endpoint,
207 const std::vector<uint8_t>& curve25519dh) {
208 blink::WebPushSubscriptionCallbacks* callbacks =
209 subscription_callbacks_.Lookup(request_id);
210 if (!callbacks)
211 return;
213 callbacks->onSuccess(blink::adoptWebPtr(
214 new blink::WebPushSubscription(endpoint, curve25519dh)));
216 subscription_callbacks_.Remove(request_id);
219 void PushProvider::OnGetRegistrationError(
220 int request_id,
221 PushGetRegistrationStatus status) {
222 blink::WebPushSubscriptionCallbacks* callbacks =
223 subscription_callbacks_.Lookup(request_id);
224 if (!callbacks)
225 return;
227 // We are only expecting an error if we can't find a registration.
228 callbacks->onSuccess(nullptr);
230 subscription_callbacks_.Remove(request_id);
233 void PushProvider::OnGetPermissionStatusSuccess(
234 int request_id,
235 blink::WebPushPermissionStatus status) {
236 blink::WebPushPermissionStatusCallbacks* callbacks =
237 permission_status_callbacks_.Lookup(request_id);
238 if (!callbacks)
239 return;
241 callbacks->onSuccess(status);
243 permission_status_callbacks_.Remove(request_id);
246 void PushProvider::OnGetPermissionStatusError(
247 int request_id,
248 blink::WebPushError::ErrorType error) {
249 blink::WebPushPermissionStatusCallbacks* callbacks =
250 permission_status_callbacks_.Lookup(request_id);
251 if (!callbacks)
252 return;
254 std::string error_message;
255 if (error == blink::WebPushError::ErrorTypeNotSupported) {
256 error_message =
257 "Push subscriptions that don't enable userVisibleOnly are not "
258 "supported.";
261 callbacks->onError(blink::WebPushError(
262 error, blink::WebString::fromUTF8(error_message)));
264 permission_status_callbacks_.Remove(request_id);
267 } // namespace content