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/notifications/notification_data_conversions.h" | 10 #include "content/child/notifications/notification_data_conversions.h" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 OnNotificationClickEvent) | 95 OnNotificationClickEvent) |
96 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) | 96 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) |
97 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_GeofencingEvent, OnGeofencingEvent) | 97 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_GeofencingEvent, OnGeofencingEvent) |
98 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginConnectEvent, | 98 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginConnectEvent, |
99 OnCrossOriginConnectEvent) | 99 OnCrossOriginConnectEvent) |
100 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToWorker, OnPostMessage) | 100 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToWorker, OnPostMessage) |
101 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginMessageToWorker, | 101 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginMessageToWorker, |
102 OnCrossOriginMessageToWorker) | 102 OnCrossOriginMessageToWorker) |
103 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClientDocuments, | 103 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClientDocuments, |
104 OnDidGetClientDocuments) | 104 OnDidGetClientDocuments) |
| 105 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, |
| 106 OnOpenWindowResponse) |
| 107 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError, |
| 108 OnOpenWindowError) |
105 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse, | 109 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FocusClientResponse, |
106 OnFocusClientResponse) | 110 OnFocusClientResponse) |
107 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidSkipWaiting, OnDidSkipWaiting) | 111 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidSkipWaiting, OnDidSkipWaiting) |
108 IPC_MESSAGE_UNHANDLED(handled = false) | 112 IPC_MESSAGE_UNHANDLED(handled = false) |
109 IPC_END_MESSAGE_MAP() | 113 IPC_END_MESSAGE_MAP() |
110 | 114 |
111 // TODO(gavinp): Would it be preferable to put an AddListener() method to | 115 // TODO(gavinp): Would it be preferable to put an AddListener() method to |
112 // EmbeddedWorkerContextClient? | 116 // EmbeddedWorkerContextClient? |
113 if (!handled) | 117 if (!handled) |
114 handled = cache_storage_dispatcher_->OnMessageReceived(message); | 118 handled = cache_storage_dispatcher_->OnMessageReceived(message); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 } | 201 } |
198 | 202 |
199 void ServiceWorkerScriptContext::GetClientDocuments( | 203 void ServiceWorkerScriptContext::GetClientDocuments( |
200 blink::WebServiceWorkerClientsCallbacks* callbacks) { | 204 blink::WebServiceWorkerClientsCallbacks* callbacks) { |
201 DCHECK(callbacks); | 205 DCHECK(callbacks); |
202 int request_id = pending_clients_callbacks_.Add(callbacks); | 206 int request_id = pending_clients_callbacks_.Add(callbacks); |
203 Send(new ServiceWorkerHostMsg_GetClientDocuments( | 207 Send(new ServiceWorkerHostMsg_GetClientDocuments( |
204 GetRoutingID(), request_id)); | 208 GetRoutingID(), request_id)); |
205 } | 209 } |
206 | 210 |
| 211 void ServiceWorkerScriptContext::OpenWindow( |
| 212 const GURL& url, blink::WebServiceWorkerClientCallbacks* callbacks) { |
| 213 DCHECK(callbacks); |
| 214 int request_id = pending_client_callbacks_.Add(callbacks); |
| 215 Send(new ServiceWorkerHostMsg_OpenWindow(GetRoutingID(), request_id, url)); |
| 216 } |
| 217 |
207 void ServiceWorkerScriptContext::PostMessageToDocument( | 218 void ServiceWorkerScriptContext::PostMessageToDocument( |
208 int client_id, | 219 int client_id, |
209 const base::string16& message, | 220 const base::string16& message, |
210 scoped_ptr<blink::WebMessagePortChannelArray> channels) { | 221 scoped_ptr<blink::WebMessagePortChannelArray> channels) { |
211 // This may send channels for MessagePorts, and all internal book-keeping | 222 // This may send channels for MessagePorts, and all internal book-keeping |
212 // messages for MessagePort (e.g. QueueMessages) are sent from main thread | 223 // messages for MessagePort (e.g. QueueMessages) are sent from main thread |
213 // (with thread hopping), so we need to do the same thread hopping here not | 224 // (with thread hopping), so we need to do the same thread hopping here not |
214 // to overtake those messages. | 225 // to overtake those messages. |
215 embedded_context_->main_thread_proxy()->PostTask( | 226 embedded_context_->main_thread_proxy()->PostTask( |
216 FROM_HERE, | 227 FROM_HERE, |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 new blink::WebServiceWorkerClientsInfo); | 429 new blink::WebServiceWorkerClientsInfo); |
419 blink::WebVector<blink::WebServiceWorkerClientInfo> convertedClients( | 430 blink::WebVector<blink::WebServiceWorkerClientInfo> convertedClients( |
420 clients.size()); | 431 clients.size()); |
421 for (size_t i = 0; i < clients.size(); ++i) | 432 for (size_t i = 0; i < clients.size(); ++i) |
422 convertedClients[i] = clients[i]; | 433 convertedClients[i] = clients[i]; |
423 info->clients.swap(convertedClients); | 434 info->clients.swap(convertedClients); |
424 callbacks->onSuccess(info.release()); | 435 callbacks->onSuccess(info.release()); |
425 pending_clients_callbacks_.Remove(request_id); | 436 pending_clients_callbacks_.Remove(request_id); |
426 } | 437 } |
427 | 438 |
| 439 void ServiceWorkerScriptContext::OnOpenWindowResponse( |
| 440 int request_id, |
| 441 const ServiceWorkerClientInfo& client) { |
| 442 TRACE_EVENT0("ServiceWorker", |
| 443 "ServiceWorkerScriptContext::OnOpenWindowResponse"); |
| 444 blink::WebServiceWorkerClientCallbacks* callbacks = |
| 445 pending_client_callbacks_.Lookup(request_id); |
| 446 if (!callbacks) { |
| 447 NOTREACHED() << "Got stray response: " << request_id; |
| 448 return; |
| 449 } |
| 450 scoped_ptr<blink::WebServiceWorkerClientInfo> web_client; |
| 451 if (!client.IsEmpty()) { |
| 452 DCHECK(client.IsValid()); |
| 453 web_client.reset(new blink::WebServiceWorkerClientInfo(client)); |
| 454 } |
| 455 callbacks->onSuccess(web_client.release()); |
| 456 pending_client_callbacks_.Remove(request_id); |
| 457 } |
| 458 |
| 459 void ServiceWorkerScriptContext::OnOpenWindowError(int request_id) { |
| 460 TRACE_EVENT0("ServiceWorker", |
| 461 "ServiceWorkerScriptContext::OnOpenWindowError"); |
| 462 blink::WebServiceWorkerClientCallbacks* callbacks = |
| 463 pending_client_callbacks_.Lookup(request_id); |
| 464 if (!callbacks) { |
| 465 NOTREACHED() << "Got stray response: " << request_id; |
| 466 return; |
| 467 } |
| 468 scoped_ptr<blink::WebServiceWorkerError> error( |
| 469 new blink::WebServiceWorkerError( |
| 470 blink::WebServiceWorkerError::ErrorTypeUnknown, |
| 471 "Something went wrong while trying to open the window.")); |
| 472 callbacks->onError(error.release()); |
| 473 pending_client_callbacks_.Remove(request_id); |
| 474 } |
| 475 |
428 void ServiceWorkerScriptContext::OnFocusClientResponse(int request_id, | 476 void ServiceWorkerScriptContext::OnFocusClientResponse(int request_id, |
429 bool result) { | 477 bool result) { |
430 TRACE_EVENT0("ServiceWorker", | 478 TRACE_EVENT0("ServiceWorker", |
431 "ServiceWorkerScriptContext::OnFocusClientResponse"); | 479 "ServiceWorkerScriptContext::OnFocusClientResponse"); |
432 blink::WebServiceWorkerClientFocusCallback* callback = | 480 blink::WebServiceWorkerClientFocusCallback* callback = |
433 pending_focus_client_callbacks_.Lookup(request_id); | 481 pending_focus_client_callbacks_.Lookup(request_id); |
434 if (!callback) { | 482 if (!callback) { |
435 NOTREACHED() << "Got stray response: " << request_id; | 483 NOTREACHED() << "Got stray response: " << request_id; |
436 return; | 484 return; |
437 } | 485 } |
438 callback->onSuccess(&result); | 486 callback->onSuccess(&result); |
439 pending_focus_client_callbacks_.Remove(request_id); | 487 pending_focus_client_callbacks_.Remove(request_id); |
440 } | 488 } |
441 | 489 |
442 void ServiceWorkerScriptContext::OnDidSkipWaiting(int request_id) { | 490 void ServiceWorkerScriptContext::OnDidSkipWaiting(int request_id) { |
443 TRACE_EVENT0("ServiceWorker", | 491 TRACE_EVENT0("ServiceWorker", |
444 "ServiceWorkerScriptContext::OnDidSkipWaiting"); | 492 "ServiceWorkerScriptContext::OnDidSkipWaiting"); |
445 blink::WebServiceWorkerSkipWaitingCallbacks* callbacks = | 493 blink::WebServiceWorkerSkipWaitingCallbacks* callbacks = |
446 pending_skip_waiting_callbacks_.Lookup(request_id); | 494 pending_skip_waiting_callbacks_.Lookup(request_id); |
447 if (!callbacks) { | 495 if (!callbacks) { |
448 NOTREACHED() << "Got stray response: " << request_id; | 496 NOTREACHED() << "Got stray response: " << request_id; |
449 return; | 497 return; |
450 } | 498 } |
451 callbacks->onSuccess(); | 499 callbacks->onSuccess(); |
452 pending_skip_waiting_callbacks_.Remove(request_id); | 500 pending_skip_waiting_callbacks_.Remove(request_id); |
453 } | 501 } |
454 | 502 |
455 } // namespace content | 503 } // namespace content |
OLD | NEW |