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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 DCHECK(service_worker_registration); | 70 DCHECK(service_worker_registration); |
72 DCHECK(callbacks); | 71 DCHECK(callbacks); |
73 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); | 72 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); |
74 registration_callbacks_.AddWithID(callbacks, request_id); | 73 registration_callbacks_.AddWithID(callbacks, request_id); |
75 int64 service_worker_registration_id = | 74 int64 service_worker_registration_id = |
76 GetServiceWorkerRegistrationId(service_worker_registration); | 75 GetServiceWorkerRegistrationId(service_worker_registration); |
77 thread_safe_sender_->Send(new PushMessagingHostMsg_RegisterFromWorker( | 76 thread_safe_sender_->Send(new PushMessagingHostMsg_RegisterFromWorker( |
78 request_id, service_worker_registration_id)); | 77 request_id, service_worker_registration_id)); |
79 } | 78 } |
80 | 79 |
| 80 void PushProvider::unregister( |
| 81 blink::WebServiceWorkerRegistration* service_worker_registration, |
| 82 blink::WebPushUnregisterCallbacks* callbacks) { |
| 83 DCHECK(service_worker_registration); |
| 84 DCHECK(callbacks); |
| 85 |
| 86 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); |
| 87 unregister_callbacks_.AddWithID(callbacks, request_id); |
| 88 |
| 89 int64 service_worker_registration_id = |
| 90 GetServiceWorkerRegistrationId(service_worker_registration); |
| 91 thread_safe_sender_->Send(new PushMessagingHostMsg_Unregister( |
| 92 request_id, service_worker_registration_id)); |
| 93 } |
| 94 |
81 void PushProvider::getPermissionStatus( | 95 void PushProvider::getPermissionStatus( |
82 blink::WebServiceWorkerRegistration* service_worker_registration, | 96 blink::WebServiceWorkerRegistration* service_worker_registration, |
83 blink::WebPushPermissionStatusCallbacks* callbacks) { | 97 blink::WebPushPermissionStatusCallbacks* callbacks) { |
84 DCHECK(service_worker_registration); | 98 DCHECK(service_worker_registration); |
85 DCHECK(callbacks); | 99 DCHECK(callbacks); |
86 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); | 100 int request_id = push_dispatcher_->GenerateRequestId(CurrentWorkerId()); |
87 permission_status_callbacks_.AddWithID(callbacks, request_id); | 101 permission_status_callbacks_.AddWithID(callbacks, request_id); |
88 int64 service_worker_registration_id = | 102 int64 service_worker_registration_id = |
89 GetServiceWorkerRegistrationId(service_worker_registration); | 103 GetServiceWorkerRegistrationId(service_worker_registration); |
90 thread_safe_sender_->Send(new PushMessagingHostMsg_GetPermissionStatus( | 104 thread_safe_sender_->Send(new PushMessagingHostMsg_GetPermissionStatus( |
91 request_id, service_worker_registration_id)); | 105 request_id, service_worker_registration_id)); |
92 } | 106 } |
93 | 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); |
| 115 IPC_MESSAGE_HANDLER(PushMessagingMsg_UnregisterSuccess, |
| 116 OnUnregisterSuccess); |
| 117 IPC_MESSAGE_HANDLER(PushMessagingMsg_UnregisterError, |
| 118 OnUnregisterError); |
101 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusSuccess, | 119 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusSuccess, |
102 OnGetPermissionStatusSuccess); | 120 OnGetPermissionStatusSuccess); |
103 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusError, | 121 IPC_MESSAGE_HANDLER(PushMessagingMsg_GetPermissionStatusError, |
104 OnGetPermissionStatusError); | 122 OnGetPermissionStatusError); |
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 |
(...skipping 23 matching lines...) Expand all Loading... |
134 return; | 152 return; |
135 | 153 |
136 scoped_ptr<blink::WebPushError> error(new blink::WebPushError( | 154 scoped_ptr<blink::WebPushError> error(new blink::WebPushError( |
137 blink::WebPushError::ErrorTypeAbort, | 155 blink::WebPushError::ErrorTypeAbort, |
138 blink::WebString::fromUTF8(PushRegistrationStatusToString(status)))); | 156 blink::WebString::fromUTF8(PushRegistrationStatusToString(status)))); |
139 callbacks->onError(error.release()); | 157 callbacks->onError(error.release()); |
140 | 158 |
141 registration_callbacks_.Remove(request_id); | 159 registration_callbacks_.Remove(request_id); |
142 } | 160 } |
143 | 161 |
| 162 void PushProvider::OnUnregisterSuccess(int request_id, bool did_unregister) { |
| 163 blink::WebPushUnregisterCallbacks* callbacks = |
| 164 unregister_callbacks_.Lookup(request_id); |
| 165 if (!callbacks) |
| 166 return; |
| 167 |
| 168 callbacks->onSuccess(&did_unregister); |
| 169 |
| 170 unregister_callbacks_.Remove(request_id); |
| 171 } |
| 172 |
| 173 void PushProvider::OnUnregisterError( |
| 174 int request_id, |
| 175 blink::WebPushError::ErrorType error_type, |
| 176 const std::string& error_message) { |
| 177 blink::WebPushUnregisterCallbacks* callbacks = |
| 178 unregister_callbacks_.Lookup(request_id); |
| 179 if (!callbacks) |
| 180 return; |
| 181 |
| 182 scoped_ptr<blink::WebPushError> error(new blink::WebPushError( |
| 183 error_type, blink::WebString::fromUTF8(error_message))); |
| 184 callbacks->onError(error.release()); |
| 185 |
| 186 unregister_callbacks_.Remove(request_id); |
| 187 } |
| 188 |
144 void PushProvider::OnGetPermissionStatusSuccess( | 189 void PushProvider::OnGetPermissionStatusSuccess( |
145 int request_id, | 190 int request_id, |
146 blink::WebPushPermissionStatus status) { | 191 blink::WebPushPermissionStatus status) { |
147 blink::WebPushPermissionStatusCallbacks* callbacks = | 192 blink::WebPushPermissionStatusCallbacks* callbacks = |
148 permission_status_callbacks_.Lookup(request_id); | 193 permission_status_callbacks_.Lookup(request_id); |
149 if (!callbacks) | 194 if (!callbacks) |
150 return; | 195 return; |
151 | 196 |
152 callbacks->onSuccess(&status); | 197 callbacks->onSuccess(&status); |
153 | 198 |
154 permission_status_callbacks_.Remove(request_id); | 199 permission_status_callbacks_.Remove(request_id); |
155 } | 200 } |
156 | 201 |
157 void PushProvider::OnGetPermissionStatusError(int request_id) { | 202 void PushProvider::OnGetPermissionStatusError(int request_id) { |
158 blink::WebPushPermissionStatusCallbacks* callbacks = | 203 blink::WebPushPermissionStatusCallbacks* callbacks = |
159 permission_status_callbacks_.Lookup(request_id); | 204 permission_status_callbacks_.Lookup(request_id); |
160 if (!callbacks) | 205 if (!callbacks) |
161 return; | 206 return; |
162 | 207 |
163 callbacks->onError(); | 208 callbacks->onError(); |
164 | 209 |
165 permission_status_callbacks_.Remove(request_id); | 210 permission_status_callbacks_.Remove(request_id); |
166 } | 211 } |
167 | 212 |
168 } // namespace content | 213 } // namespace content |
OLD | NEW |