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/WebPushError.h" | |
17 #include "third_party/WebKit/public/platform/WebPushRegistration.h" | 16 #include "third_party/WebKit/public/platform/WebPushRegistration.h" |
18 #include "third_party/WebKit/public/platform/WebString.h" | 17 #include "third_party/WebKit/public/platform/WebString.h" |
19 | 18 |
20 namespace content { | 19 namespace content { |
21 namespace { | 20 namespace { |
22 | 21 |
23 int CurrentWorkerId() { | 22 int CurrentWorkerId() { |
24 return WorkerTaskRunner::Instance()->CurrentWorkerId(); | 23 return WorkerTaskRunner::Instance()->CurrentWorkerId(); |
25 } | 24 } |
26 | 25 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 DCHECK(service_worker_registration); | 83 DCHECK(service_worker_registration); |
85 DCHECK(callbacks); | 84 DCHECK(callbacks); |
86 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); | 85 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); |
87 permission_status_callbacks_.AddWithID(callbacks, request_id); | 86 permission_status_callbacks_.AddWithID(callbacks, request_id); |
88 int64 service_worker_registration_id = | 87 int64 service_worker_registration_id = |
89 GetServiceWorkerRegistrationId(service_worker_registration); | 88 GetServiceWorkerRegistrationId(service_worker_registration); |
90 thread_safe_sender_->Send(new PushMessagingHostMsg_GetPermissionStatus( | 89 thread_safe_sender_->Send(new PushMessagingHostMsg_GetPermissionStatus( |
91 request_id, service_worker_registration_id)); | 90 request_id, service_worker_registration_id)); |
92 } | 91 } |
93 | 92 |
| 93 void PushProvider::unregister( |
| 94 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 95 blink::WebPushUnregisterCallbacks* callbacks) { |
| 96 DCHECK(service_worker_registration); |
| 97 DCHECK(callbacks); |
| 98 |
| 99 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); |
| 100 unregister_callbacks_.AddWithID(callbacks, request_id); |
| 101 |
| 102 int64 service_worker_registration_id = |
| 103 GetServiceWorkerRegistrationId(service_worker_registration); |
| 104 thread_safe_sender_->Send(new PushMessagingHostMsg_Unregister( |
| 105 request_id, service_worker_registration_id)); |
| 106 } |
| 107 |
94 bool PushProvider::OnMessageReceived(const IPC::Message& message) { | 108 bool PushProvider::OnMessageReceived(const IPC::Message& message) { |
95 bool handled = true; | 109 bool handled = true; |
96 IPC_BEGIN_MESSAGE_MAP(PushProvider, message) | 110 IPC_BEGIN_MESSAGE_MAP(PushProvider, message) |
97 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromWorkerSuccess, | 111 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromWorkerSuccess, |
98 OnRegisterFromWorkerSuccess); | 112 OnRegisterFromWorkerSuccess); |
99 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromWorkerError, | 113 IPC_MESSAGE_HANDLER(PushMessagingMsg_RegisterFromWorkerError, |
100 OnRegisterFromWorkerError); | 114 OnRegisterFromWorkerError); |
101 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusSuccess, | 115 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusSuccess, |
102 OnGetPermissionStatusSuccess); | 116 OnGetPermissionStatusSuccess); |
103 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusError, | 117 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusError, |
104 OnGetPermissionStatusError); | 118 OnGetPermissionStatusError); |
| 119 IPC_MESSAGE_HANDLER(PushMessagingMsg_UnregisterSuccess, |
| 120 OnUnregisterSuccess); |
| 121 IPC_MESSAGE_HANDLER(PushMessagingMsg_UnregisterError, |
| 122 OnUnregisterError); |
105 IPC_MESSAGE_UNHANDLED(handled = false) | 123 IPC_MESSAGE_UNHANDLED(handled = false) |
106 IPC_END_MESSAGE_MAP() | 124 IPC_END_MESSAGE_MAP() |
107 | 125 |
108 return handled; | 126 return handled; |
109 } | 127 } |
110 | 128 |
111 void PushProvider::OnRegisterFromWorkerSuccess( | 129 void PushProvider::OnRegisterFromWorkerSuccess( |
112 int request_id, | 130 int request_id, |
113 const GURL& endpoint, | 131 const GURL& endpoint, |
114 const std::string& registration_id) { | 132 const std::string& registration_id) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 blink::WebPushPermissionStatusCallbacks* callbacks = | 176 blink::WebPushPermissionStatusCallbacks* callbacks = |
159 permission_status_callbacks_.Lookup(request_id); | 177 permission_status_callbacks_.Lookup(request_id); |
160 if (!callbacks) | 178 if (!callbacks) |
161 return; | 179 return; |
162 | 180 |
163 callbacks->onError(); | 181 callbacks->onError(); |
164 | 182 |
165 permission_status_callbacks_.Remove(request_id); | 183 permission_status_callbacks_.Remove(request_id); |
166 } | 184 } |
167 | 185 |
| 186 void PushProvider::OnUnregisterSuccess(int request_id, bool did_unregister) { |
| 187 blink::WebPushUnregisterCallbacks* callbacks = |
| 188 unregister_callbacks_.Lookup(request_id); |
| 189 if (!callbacks) |
| 190 return; |
| 191 |
| 192 callbacks->onSuccess(&did_unregister); |
| 193 |
| 194 unregister_callbacks_.Remove(request_id); |
| 195 } |
| 196 |
| 197 void PushProvider::OnUnregisterError( |
| 198 int request_id, |
| 199 blink::WebPushError::ErrorType error_type, |
| 200 const std::string& error_message) { |
| 201 blink::WebPushUnregisterCallbacks* callbacks = |
| 202 unregister_callbacks_.Lookup(request_id); |
| 203 if (!callbacks) |
| 204 return; |
| 205 |
| 206 scoped_ptr<blink::WebPushError> error(new blink::WebPushError( |
| 207 error_type, blink::WebString::fromUTF8(error_message))); |
| 208 callbacks->onError(error.release()); |
| 209 |
| 210 unregister_callbacks_.Remove(request_id); |
| 211 } |
| 212 |
168 } // namespace content | 213 } // namespace content |
OLD | NEW |