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/renderer/service_worker/service_worker_script_context.h" | 5 #include "content/renderer/service_worker/service_worker_script_context.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "content/child/service_worker/web_cross_origin_service_worker_client_im
pl.h" |
10 #include "content/child/thread_safe_sender.h" | 11 #include "content/child/thread_safe_sender.h" |
11 #include "content/child/webmessageportchannel_impl.h" | 12 #include "content/child/webmessageportchannel_impl.h" |
| 13 #include "content/common/message_port_messages.h" |
12 #include "content/common/service_worker/service_worker_messages.h" | 14 #include "content/common/service_worker/service_worker_messages.h" |
13 #include "content/renderer/service_worker/embedded_worker_context_client.h" | 15 #include "content/renderer/service_worker/embedded_worker_context_client.h" |
14 #include "ipc/ipc_message.h" | 16 #include "ipc/ipc_message.h" |
15 #include "third_party/WebKit/public/platform/WebNotificationData.h" | 17 #include "third_party/WebKit/public/platform/WebNotificationData.h" |
16 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" | 18 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" |
17 #include "third_party/WebKit/public/platform/WebServiceWorkerRequest.h" | 19 #include "third_party/WebKit/public/platform/WebServiceWorkerRequest.h" |
18 #include "third_party/WebKit/public/platform/WebString.h" | 20 #include "third_party/WebKit/public/platform/WebString.h" |
19 #include "third_party/WebKit/public/platform/WebURL.h" | 21 #include "third_party/WebKit/public/platform/WebURL.h" |
20 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h" | 22 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h" |
21 #include "third_party/WebKit/public/web/WebServiceWorkerContextProxy.h" | 23 #include "third_party/WebKit/public/web/WebServiceWorkerContextProxy.h" |
22 | 24 |
23 namespace content { | 25 namespace content { |
24 | 26 |
25 namespace { | 27 namespace { |
26 | 28 |
27 void SendPostMessageToDocumentOnMainThread( | 29 void SendPostMessageToDocumentOnMainThread( |
28 ThreadSafeSender* sender, | 30 ThreadSafeSender* sender, |
29 int routing_id, | 31 int routing_id, |
30 int client_id, | 32 int client_id, |
31 const base::string16& message, | 33 const base::string16& message, |
32 scoped_ptr<blink::WebMessagePortChannelArray> channels) { | 34 scoped_ptr<blink::WebMessagePortChannelArray> channels) { |
33 sender->Send(new ServiceWorkerHostMsg_PostMessageToDocument( | 35 sender->Send(new ServiceWorkerHostMsg_PostMessageToDocument( |
34 routing_id, client_id, message, | 36 routing_id, client_id, message, |
35 WebMessagePortChannelImpl::ExtractMessagePortIDs(channels.release()))); | 37 WebMessagePortChannelImpl::ExtractMessagePortIDs(channels.release()))); |
36 } | 38 } |
37 | 39 |
| 40 void SendCrossOriginMessageOnMainThread( |
| 41 ThreadSafeSender* sender, |
| 42 int message_port_id, |
| 43 const base::string16& message, |
| 44 scoped_ptr<blink::WebMessagePortChannelArray> channels) { |
| 45 sender->Send(new MessagePortHostMsg_PostMessage( |
| 46 message_port_id, message, |
| 47 WebMessagePortChannelImpl::ExtractMessagePortIDs(channels.release()))); |
| 48 } |
| 49 |
38 blink::WebURLRequest::FetchRequestMode GetBlinkFetchRequestMode( | 50 blink::WebURLRequest::FetchRequestMode GetBlinkFetchRequestMode( |
39 FetchRequestMode mode) { | 51 FetchRequestMode mode) { |
40 return static_cast<blink::WebURLRequest::FetchRequestMode>(mode); | 52 return static_cast<blink::WebURLRequest::FetchRequestMode>(mode); |
41 } | 53 } |
42 | 54 |
43 blink::WebURLRequest::FetchCredentialsMode GetBlinkFetchCredentialsMode( | 55 blink::WebURLRequest::FetchCredentialsMode GetBlinkFetchCredentialsMode( |
44 FetchCredentialsMode credentials_mode) { | 56 FetchCredentialsMode credentials_mode) { |
45 return static_cast<blink::WebURLRequest::FetchCredentialsMode>( | 57 return static_cast<blink::WebURLRequest::FetchCredentialsMode>( |
46 credentials_mode); | 58 credentials_mode); |
47 } | 59 } |
(...skipping 24 matching lines...) Expand all Loading... |
72 void ServiceWorkerScriptContext::OnMessageReceived( | 84 void ServiceWorkerScriptContext::OnMessageReceived( |
73 const IPC::Message& message) { | 85 const IPC::Message& message) { |
74 bool handled = true; | 86 bool handled = true; |
75 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerScriptContext, message) | 87 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerScriptContext, message) |
76 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) | 88 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) |
77 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent) | 89 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent) |
78 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) | 90 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) |
79 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SyncEvent, OnSyncEvent) | 91 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SyncEvent, OnSyncEvent) |
80 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) | 92 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) |
81 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_GeofencingEvent, OnGeofencingEvent) | 93 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_GeofencingEvent, OnGeofencingEvent) |
| 94 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginConnectEvent, |
| 95 OnCrossOriginConnectEvent) |
| 96 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginMessageToWorker, |
| 97 OnCrossOriginMessageToWorker) |
82 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToWorker, OnPostMessage) | 98 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToWorker, OnPostMessage) |
83 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClientDocuments, | 99 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClientDocuments, |
84 OnDidGetClientDocuments) | 100 OnDidGetClientDocuments) |
85 IPC_MESSAGE_UNHANDLED(handled = false) | 101 IPC_MESSAGE_UNHANDLED(handled = false) |
86 IPC_END_MESSAGE_MAP() | 102 IPC_END_MESSAGE_MAP() |
87 | 103 |
88 // TODO(gavinp): Would it be preferable to put an AddListener() method to | 104 // TODO(gavinp): Would it be preferable to put an AddListener() method to |
89 // EmbeddedWorkerContextClient? | 105 // EmbeddedWorkerContextClient? |
90 if (!handled) | 106 if (!handled) |
91 handled = cache_storage_dispatcher_->OnMessageReceived(message); | 107 handled = cache_storage_dispatcher_->OnMessageReceived(message); |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 | 170 |
155 Send(new ServiceWorkerHostMsg_PushEventFinished( | 171 Send(new ServiceWorkerHostMsg_PushEventFinished( |
156 GetRoutingID(), request_id, result)); | 172 GetRoutingID(), request_id, result)); |
157 } | 173 } |
158 | 174 |
159 void ServiceWorkerScriptContext::DidHandleSyncEvent(int request_id) { | 175 void ServiceWorkerScriptContext::DidHandleSyncEvent(int request_id) { |
160 Send(new ServiceWorkerHostMsg_SyncEventFinished( | 176 Send(new ServiceWorkerHostMsg_SyncEventFinished( |
161 GetRoutingID(), request_id)); | 177 GetRoutingID(), request_id)); |
162 } | 178 } |
163 | 179 |
| 180 void ServiceWorkerScriptContext::DidHandleCrossOriginConnectEvent( |
| 181 int request_id, |
| 182 bool allow_connect) { |
| 183 Send(new ServiceWorkerHostMsg_CrossOriginConnectEventFinished( |
| 184 GetRoutingID(), request_id, allow_connect)); |
| 185 } |
| 186 |
164 void ServiceWorkerScriptContext::GetClientDocuments( | 187 void ServiceWorkerScriptContext::GetClientDocuments( |
165 blink::WebServiceWorkerClientsCallbacks* callbacks) { | 188 blink::WebServiceWorkerClientsCallbacks* callbacks) { |
166 DCHECK(callbacks); | 189 DCHECK(callbacks); |
167 int request_id = pending_clients_callbacks_.Add(callbacks); | 190 int request_id = pending_clients_callbacks_.Add(callbacks); |
168 Send(new ServiceWorkerHostMsg_GetClientDocuments( | 191 Send(new ServiceWorkerHostMsg_GetClientDocuments( |
169 GetRoutingID(), request_id)); | 192 GetRoutingID(), request_id)); |
170 } | 193 } |
171 | 194 |
172 void ServiceWorkerScriptContext::PostMessageToDocument( | 195 void ServiceWorkerScriptContext::PostMessageToDocument( |
173 int client_id, | 196 int client_id, |
174 const base::string16& message, | 197 const base::string16& message, |
175 scoped_ptr<blink::WebMessagePortChannelArray> channels) { | 198 scoped_ptr<blink::WebMessagePortChannelArray> channels) { |
176 // This may send channels for MessagePorts, and all internal book-keeping | 199 // This may send channels for MessagePorts, and all internal book-keeping |
177 // messages for MessagePort (e.g. QueueMessages) are sent from main thread | 200 // messages for MessagePort (e.g. QueueMessages) are sent from main thread |
178 // (with thread hopping), so we need to do the same thread hopping here not | 201 // (with thread hopping), so we need to do the same thread hopping here not |
179 // to overtake those messages. | 202 // to overtake those messages. |
180 embedded_context_->main_thread_proxy()->PostTask( | 203 embedded_context_->main_thread_proxy()->PostTask( |
181 FROM_HERE, | 204 FROM_HERE, |
182 base::Bind(&SendPostMessageToDocumentOnMainThread, | 205 base::Bind(&SendPostMessageToDocumentOnMainThread, |
183 make_scoped_refptr(embedded_context_->thread_safe_sender()), | 206 make_scoped_refptr(embedded_context_->thread_safe_sender()), |
184 GetRoutingID(), client_id, message, base::Passed(&channels))); | 207 GetRoutingID(), client_id, message, base::Passed(&channels))); |
185 } | 208 } |
186 | 209 |
| 210 void ServiceWorkerScriptContext::PostCrossOriginMessage( |
| 211 WebCrossOriginServiceWorkerClientImpl* client, |
| 212 const base::string16& message, |
| 213 scoped_ptr<blink::WebMessagePortChannelArray> channels) { |
| 214 // This may send channels for MessagePorts, and all internal book-keeping |
| 215 // messages for MessagePort (e.g. QueueMessages) are sent from main thread |
| 216 // (with thread hopping), so we need to do the same thread hopping here not |
| 217 // to overtake those messages. |
| 218 embedded_context_->main_thread_proxy()->PostTask( |
| 219 FROM_HERE, |
| 220 base::Bind(&SendCrossOriginMessageOnMainThread, |
| 221 make_scoped_refptr(embedded_context_->thread_safe_sender()), |
| 222 client->message_port_id(), message, base::Passed(&channels))); |
| 223 } |
| 224 |
187 void ServiceWorkerScriptContext::Send(IPC::Message* message) { | 225 void ServiceWorkerScriptContext::Send(IPC::Message* message) { |
188 embedded_context_->Send(message); | 226 embedded_context_->Send(message); |
189 } | 227 } |
190 | 228 |
191 int ServiceWorkerScriptContext::GetRoutingID() const { | 229 int ServiceWorkerScriptContext::GetRoutingID() const { |
192 return embedded_context_->embedded_worker_id(); | 230 return embedded_context_->embedded_worker_id(); |
193 } | 231 } |
194 | 232 |
195 void ServiceWorkerScriptContext::OnActivateEvent(int request_id) { | 233 void ServiceWorkerScriptContext::OnActivateEvent(int request_id) { |
196 TRACE_EVENT0("ServiceWorker", | 234 TRACE_EVENT0("ServiceWorker", |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 const std::string& region_id, | 311 const std::string& region_id, |
274 const blink::WebCircularGeofencingRegion& region) { | 312 const blink::WebCircularGeofencingRegion& region) { |
275 TRACE_EVENT0("ServiceWorker", | 313 TRACE_EVENT0("ServiceWorker", |
276 "ServiceWorkerScriptContext::OnGeofencingEvent"); | 314 "ServiceWorkerScriptContext::OnGeofencingEvent"); |
277 proxy_->dispatchGeofencingEvent( | 315 proxy_->dispatchGeofencingEvent( |
278 request_id, event_type, blink::WebString::fromUTF8(region_id), region); | 316 request_id, event_type, blink::WebString::fromUTF8(region_id), region); |
279 Send(new ServiceWorkerHostMsg_GeofencingEventFinished(GetRoutingID(), | 317 Send(new ServiceWorkerHostMsg_GeofencingEventFinished(GetRoutingID(), |
280 request_id)); | 318 request_id)); |
281 } | 319 } |
282 | 320 |
| 321 void ServiceWorkerScriptContext::OnCrossOriginConnectEvent( |
| 322 int request_id, |
| 323 const CrossOriginServiceWorkerClient& client) { |
| 324 TRACE_EVENT0("ServiceWorker", |
| 325 "ServiceWorkerScriptContext::OnCrossOriginConnectEvent"); |
| 326 // base::MessageLoopProxy* loop_proxy = |
| 327 // embedded_context_->main_thread_proxy(); |
| 328 blink::WebCrossOriginServiceWorkerClient* web_client = |
| 329 new WebCrossOriginServiceWorkerClientImpl(client); |
| 330 proxy_->dispatchCrossOriginConnectEvent(request_id, web_client); |
| 331 } |
| 332 |
| 333 void ServiceWorkerScriptContext::OnCrossOriginMessageToWorker( |
| 334 const CrossOriginServiceWorkerClient& client, |
| 335 const base::string16& message, |
| 336 const std::vector<int>& sent_message_port_ids, |
| 337 const std::vector<int>& new_routing_ids) { |
| 338 TRACE_EVENT0("ServiceWorker", |
| 339 "ServiceWorkerScriptContext::OnCrossOriginMessageToWorker"); |
| 340 std::vector<WebMessagePortChannelImpl*> ports; |
| 341 if (!sent_message_port_ids.empty()) { |
| 342 base::MessageLoopProxy* loop_proxy = embedded_context_->main_thread_proxy(); |
| 343 ports.resize(sent_message_port_ids.size()); |
| 344 for (size_t i = 0; i < sent_message_port_ids.size(); ++i) { |
| 345 ports[i] = new WebMessagePortChannelImpl( |
| 346 new_routing_ids[i], sent_message_port_ids[i], loop_proxy); |
| 347 } |
| 348 } |
| 349 |
| 350 blink::WebCrossOriginServiceWorkerClient* web_client = |
| 351 new WebCrossOriginServiceWorkerClientImpl(client); |
| 352 proxy_->dispatchCrossOriginMessageEvent(web_client, message, ports); |
| 353 } |
| 354 |
283 void ServiceWorkerScriptContext::OnPostMessage( | 355 void ServiceWorkerScriptContext::OnPostMessage( |
284 const base::string16& message, | 356 const base::string16& message, |
285 const std::vector<int>& sent_message_port_ids, | 357 const std::vector<int>& sent_message_port_ids, |
286 const std::vector<int>& new_routing_ids) { | 358 const std::vector<int>& new_routing_ids) { |
287 TRACE_EVENT0("ServiceWorker", | 359 TRACE_EVENT0("ServiceWorker", |
288 "ServiceWorkerScriptContext::OnPostEvent"); | 360 "ServiceWorkerScriptContext::OnPostEvent"); |
289 std::vector<WebMessagePortChannelImpl*> ports; | 361 std::vector<WebMessagePortChannelImpl*> ports; |
290 if (!sent_message_port_ids.empty()) { | 362 if (!sent_message_port_ids.empty()) { |
291 base::MessageLoopProxy* loop_proxy = embedded_context_->main_thread_proxy(); | 363 base::MessageLoopProxy* loop_proxy = embedded_context_->main_thread_proxy(); |
292 ports.resize(sent_message_port_ids.size()); | 364 ports.resize(sent_message_port_ids.size()); |
(...skipping 23 matching lines...) Expand all Loading... |
316 return; | 388 return; |
317 } | 389 } |
318 scoped_ptr<blink::WebServiceWorkerClientsInfo> info( | 390 scoped_ptr<blink::WebServiceWorkerClientsInfo> info( |
319 new blink::WebServiceWorkerClientsInfo); | 391 new blink::WebServiceWorkerClientsInfo); |
320 info->clientIDs = client_ids; | 392 info->clientIDs = client_ids; |
321 callbacks->onSuccess(info.release()); | 393 callbacks->onSuccess(info.release()); |
322 pending_clients_callbacks_.Remove(request_id); | 394 pending_clients_callbacks_.Remove(request_id); |
323 } | 395 } |
324 | 396 |
325 } // namespace content | 397 } // namespace content |
OLD | NEW |