| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/browser/service_worker/service_worker_version.h" | 5 #include "content/browser/service_worker/service_worker_version.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "content/browser/bad_message.h" | 14 #include "content/browser/bad_message.h" |
| 15 #include "content/browser/child_process_security_policy_impl.h" | 15 #include "content/browser/child_process_security_policy_impl.h" |
| 16 #include "content/browser/message_port_message_filter.h" | 16 #include "content/browser/message_port_message_filter.h" |
| 17 #include "content/browser/message_port_service.h" | 17 #include "content/browser/message_port_service.h" |
| 18 #include "content/browser/service_worker/embedded_worker_instance.h" | 18 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 19 #include "content/browser/service_worker/embedded_worker_registry.h" | 19 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 20 #include "content/browser/service_worker/service_worker_context_core.h" | 20 #include "content/browser/service_worker/service_worker_context_core.h" |
| 21 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 21 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 22 #include "content/browser/service_worker/service_worker_metrics.h" | 22 #include "content/browser/service_worker/service_worker_metrics.h" |
| 23 #include "content/browser/service_worker/service_worker_registration.h" | 23 #include "content/browser/service_worker/service_worker_registration.h" |
| 24 #include "content/browser/service_worker/service_worker_utils.h" | 24 #include "content/browser/service_worker/service_worker_utils.h" |
| 25 #include "content/browser/service_worker/stashed_port_manager.h" |
| 25 #include "content/browser/storage_partition_impl.h" | 26 #include "content/browser/storage_partition_impl.h" |
| 26 #include "content/common/service_worker/service_worker_messages.h" | 27 #include "content/common/service_worker/service_worker_messages.h" |
| 27 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 28 #include "content/public/browser/content_browser_client.h" | 29 #include "content/public/browser/content_browser_client.h" |
| 29 #include "content/public/browser/page_navigator.h" | 30 #include "content/public/browser/page_navigator.h" |
| 30 #include "content/public/browser/render_frame_host.h" | 31 #include "content/public/browser/render_frame_host.h" |
| 31 #include "content/public/browser/render_process_host.h" | 32 #include "content/public/browser/render_process_host.h" |
| 32 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
| 33 #include "content/public/browser/web_contents_observer.h" | 34 #include "content/public/browser/web_contents_observer.h" |
| 34 #include "content/public/common/child_process_host.h" | 35 #include "content/public/common/child_process_host.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 } | 155 } |
| 155 callback.Run(status); | 156 callback.Run(status); |
| 156 } | 157 } |
| 157 | 158 |
| 158 void RunErrorCrossOriginConnectCallback( | 159 void RunErrorCrossOriginConnectCallback( |
| 159 const ServiceWorkerVersion::CrossOriginConnectCallback& callback, | 160 const ServiceWorkerVersion::CrossOriginConnectCallback& callback, |
| 160 ServiceWorkerStatusCode status) { | 161 ServiceWorkerStatusCode status) { |
| 161 callback.Run(status, false /* accept_connection */); | 162 callback.Run(status, false /* accept_connection */); |
| 162 } | 163 } |
| 163 | 164 |
| 165 void RunErrorSendStashedPortsCallback( |
| 166 const ServiceWorkerVersion::SendStashedPortsCallback& callback, |
| 167 ServiceWorkerStatusCode status) { |
| 168 callback.Run(status, std::vector<int>()); |
| 169 } |
| 170 |
| 164 using WindowOpenedCallback = base::Callback<void(int, int)>; | 171 using WindowOpenedCallback = base::Callback<void(int, int)>; |
| 165 | 172 |
| 166 // The WindowOpenedObserver class is a WebContentsObserver that will wait for a | 173 // The WindowOpenedObserver class is a WebContentsObserver that will wait for a |
| 167 // new Window's WebContents to be initialized, run the |callback| passed to its | 174 // new Window's WebContents to be initialized, run the |callback| passed to its |
| 168 // constructor then self destroy. | 175 // constructor then self destroy. |
| 169 // The callback will receive the process and frame ids. If something went wrong | 176 // The callback will receive the process and frame ids. If something went wrong |
| 170 // those will be (kInvalidUniqueID, MSG_ROUTING_NONE). | 177 // those will be (kInvalidUniqueID, MSG_ROUTING_NONE). |
| 171 // The callback will be called in the IO thread. | 178 // The callback will be called in the IO thread. |
| 172 class WindowOpenedObserver : public WebContentsObserver { | 179 class WindowOpenedObserver : public WebContentsObserver { |
| 173 public: | 180 public: |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 case ServiceWorkerVersion::ACTIVATED: | 361 case ServiceWorkerVersion::ACTIVATED: |
| 355 return true; | 362 return true; |
| 356 case ServiceWorkerVersion::REDUNDANT: | 363 case ServiceWorkerVersion::REDUNDANT: |
| 357 NOTREACHED() << "Cannot use REDUNDANT here."; | 364 NOTREACHED() << "Cannot use REDUNDANT here."; |
| 358 return false; | 365 return false; |
| 359 } | 366 } |
| 360 NOTREACHED() << "Unexpected status: " << status; | 367 NOTREACHED() << "Unexpected status: " << status; |
| 361 return false; | 368 return false; |
| 362 } | 369 } |
| 363 | 370 |
| 371 scoped_refptr<StashedPortManager> GetStashedPortManagerOnUIThread( |
| 372 const scoped_refptr<ServiceWorkerContextWrapper>& context_wrapper) { |
| 373 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 374 return context_wrapper->storage_partition()->GetStashedPortManager(); |
| 375 } |
| 376 |
| 377 void StashPortImpl( |
| 378 const scoped_refptr<ServiceWorkerVersion>& service_worker, |
| 379 int message_port_id, |
| 380 const base::string16& name, |
| 381 const scoped_refptr<StashedPortManager>& stashed_port_manager) { |
| 382 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 383 stashed_port_manager->AddPort(service_worker.get(), message_port_id, name); |
| 384 } |
| 385 |
| 364 } // namespace | 386 } // namespace |
| 365 | 387 |
| 366 const int ServiceWorkerVersion::kStartWorkerTimeoutMinutes = 5; | 388 const int ServiceWorkerVersion::kStartWorkerTimeoutMinutes = 5; |
| 367 const int ServiceWorkerVersion::kRequestTimeoutMinutes = 5; | 389 const int ServiceWorkerVersion::kRequestTimeoutMinutes = 5; |
| 368 | 390 |
| 369 class ServiceWorkerVersion::ServiceWorkerEventMetrics { | 391 class ServiceWorkerVersion::ServiceWorkerEventMetrics { |
| 370 public: | 392 public: |
| 371 ServiceWorkerEventMetrics() {} | 393 ServiceWorkerEventMetrics() {} |
| 372 ~ServiceWorkerEventMetrics() { | 394 ~ServiceWorkerEventMetrics() { |
| 373 ServiceWorkerMetrics::RecordEventStatus(fired_events, handled_events); | 395 ServiceWorkerMetrics::RecordEventStatus(fired_events, handled_events); |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 805 MessagePortMessageFilter* filter = | 827 MessagePortMessageFilter* filter = |
| 806 embedded_worker_->message_port_message_filter(); | 828 embedded_worker_->message_port_message_filter(); |
| 807 std::vector<int> new_routing_ids; | 829 std::vector<int> new_routing_ids; |
| 808 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); | 830 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); |
| 809 ServiceWorkerStatusCode status = | 831 ServiceWorkerStatusCode status = |
| 810 embedded_worker_->SendMessage(ServiceWorkerMsg_CrossOriginMessageToWorker( | 832 embedded_worker_->SendMessage(ServiceWorkerMsg_CrossOriginMessageToWorker( |
| 811 client, message, sent_message_ports, new_routing_ids)); | 833 client, message, sent_message_ports, new_routing_ids)); |
| 812 RunSoon(base::Bind(callback, status)); | 834 RunSoon(base::Bind(callback, status)); |
| 813 } | 835 } |
| 814 | 836 |
| 837 void ServiceWorkerVersion::SendStashedMessagePorts( |
| 838 const std::vector<TransferredMessagePort>& stashed_message_ports, |
| 839 const std::vector<base::string16>& port_names, |
| 840 const SendStashedPortsCallback& callback) { |
| 841 if (running_status() != RUNNING) { |
| 842 // Schedule calling this method after starting the worker. |
| 843 StartWorker(base::Bind( |
| 844 &RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), |
| 845 base::Bind(&RunErrorSendStashedPortsCallback, callback), |
| 846 base::Bind(&self::SendStashedMessagePorts, weak_factory_.GetWeakPtr(), |
| 847 stashed_message_ports, port_names, callback))); |
| 848 return; |
| 849 } |
| 850 |
| 851 MessagePortMessageFilter* filter = |
| 852 embedded_worker_->message_port_message_filter(); |
| 853 std::vector<int> new_routing_ids(stashed_message_ports.size()); |
| 854 for (size_t i = 0; i < stashed_message_ports.size(); ++i) |
| 855 new_routing_ids[i] = filter->GetNextRoutingID(); |
| 856 |
| 857 ServiceWorkerStatusCode status = |
| 858 embedded_worker_->SendMessage(ServiceWorkerMsg_SendStashedMessagePorts( |
| 859 stashed_message_ports, new_routing_ids, port_names)); |
| 860 RunSoon(base::Bind(callback, status, new_routing_ids)); |
| 861 } |
| 862 |
| 815 void ServiceWorkerVersion::AddControllee( | 863 void ServiceWorkerVersion::AddControllee( |
| 816 ServiceWorkerProviderHost* provider_host) { | 864 ServiceWorkerProviderHost* provider_host) { |
| 817 const std::string& uuid = provider_host->client_uuid(); | 865 const std::string& uuid = provider_host->client_uuid(); |
| 818 CHECK(!provider_host->client_uuid().empty()); | 866 CHECK(!provider_host->client_uuid().empty()); |
| 819 DCHECK(!ContainsKey(controllee_map_, uuid)); | 867 DCHECK(!ContainsKey(controllee_map_, uuid)); |
| 820 controllee_map_[uuid] = provider_host; | 868 controllee_map_[uuid] = provider_host; |
| 821 // Keep the worker alive a bit longer right after a new controllee is added. | 869 // Keep the worker alive a bit longer right after a new controllee is added. |
| 822 RestartTick(&idle_time_); | 870 RestartTick(&idle_time_); |
| 823 } | 871 } |
| 824 | 872 |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1059 OnClearCachedMetadata) | 1107 OnClearCachedMetadata) |
| 1060 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, | 1108 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, |
| 1061 OnPostMessageToClient) | 1109 OnPostMessageToClient) |
| 1062 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, | 1110 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, |
| 1063 OnFocusClient) | 1111 OnFocusClient) |
| 1064 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, | 1112 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, |
| 1065 OnSkipWaiting) | 1113 OnSkipWaiting) |
| 1066 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, | 1114 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, |
| 1067 OnClaimClients) | 1115 OnClaimClients) |
| 1068 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_Pong, OnPongFromWorker) | 1116 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_Pong, OnPongFromWorker) |
| 1117 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_StashMessagePort, |
| 1118 OnStashMessagePort) |
| 1069 IPC_MESSAGE_UNHANDLED(handled = false) | 1119 IPC_MESSAGE_UNHANDLED(handled = false) |
| 1070 IPC_END_MESSAGE_MAP() | 1120 IPC_END_MESSAGE_MAP() |
| 1071 return handled; | 1121 return handled; |
| 1072 } | 1122 } |
| 1073 | 1123 |
| 1074 void ServiceWorkerVersion::OnStartSentAndScriptEvaluated( | 1124 void ServiceWorkerVersion::OnStartSentAndScriptEvaluated( |
| 1075 ServiceWorkerStatusCode status) { | 1125 ServiceWorkerStatusCode status) { |
| 1076 if (status != SERVICE_WORKER_OK) { | 1126 if (status != SERVICE_WORKER_OK) { |
| 1077 RunCallbacks(this, &start_callbacks_, | 1127 RunCallbacks(this, &start_callbacks_, |
| 1078 DeduceStartWorkerFailureReason(status)); | 1128 DeduceStartWorkerFailureReason(status)); |
| (...skipping 459 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1538 | 1588 |
| 1539 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( | 1589 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( |
| 1540 request_id, blink::WebServiceWorkerError::ErrorTypeAbort, | 1590 request_id, blink::WebServiceWorkerError::ErrorTypeAbort, |
| 1541 base::ASCIIToUTF16(kClaimClientsShutdownErrorMesage))); | 1591 base::ASCIIToUTF16(kClaimClientsShutdownErrorMesage))); |
| 1542 } | 1592 } |
| 1543 | 1593 |
| 1544 void ServiceWorkerVersion::OnPongFromWorker() { | 1594 void ServiceWorkerVersion::OnPongFromWorker() { |
| 1545 ClearTick(&ping_time_); | 1595 ClearTick(&ping_time_); |
| 1546 } | 1596 } |
| 1547 | 1597 |
| 1598 void ServiceWorkerVersion::OnStashMessagePort(int message_port_id, |
| 1599 const base::string16& name) { |
| 1600 // Just abort if we are shutting down. |
| 1601 if (!context_) |
| 1602 return; |
| 1603 |
| 1604 ServiceWorkerRegistration* registration = |
| 1605 context_->GetLiveRegistration(registration_id_); |
| 1606 if (!registration) |
| 1607 return; |
| 1608 |
| 1609 // TODO(mek): Figure out a way to avoid this round-trip through the UI thread. |
| 1610 BrowserThread::PostTaskAndReplyWithResult( |
| 1611 BrowserThread::UI, FROM_HERE, |
| 1612 base::Bind(&GetStashedPortManagerOnUIThread, |
| 1613 make_scoped_refptr(context_->wrapper())), |
| 1614 base::Bind(&StashPortImpl, make_scoped_refptr(this), message_port_id, |
| 1615 name)); |
| 1616 } |
| 1617 |
| 1548 void ServiceWorkerVersion::DidEnsureLiveRegistrationForStartWorker( | 1618 void ServiceWorkerVersion::DidEnsureLiveRegistrationForStartWorker( |
| 1549 bool pause_after_download, | 1619 bool pause_after_download, |
| 1550 const StatusCallback& callback, | 1620 const StatusCallback& callback, |
| 1551 ServiceWorkerStatusCode status, | 1621 ServiceWorkerStatusCode status, |
| 1552 const scoped_refptr<ServiceWorkerRegistration>& protect) { | 1622 const scoped_refptr<ServiceWorkerRegistration>& protect) { |
| 1553 if (status != SERVICE_WORKER_OK) { | 1623 if (status != SERVICE_WORKER_OK) { |
| 1554 RecordStartWorkerResult(status); | 1624 RecordStartWorkerResult(status); |
| 1555 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED)); | 1625 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED)); |
| 1556 return; | 1626 return; |
| 1557 } | 1627 } |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1902 return SERVICE_WORKER_ERROR_ABORT; | 1972 return SERVICE_WORKER_ERROR_ABORT; |
| 1903 default: | 1973 default: |
| 1904 return SERVICE_WORKER_ERROR_NETWORK; | 1974 return SERVICE_WORKER_ERROR_NETWORK; |
| 1905 } | 1975 } |
| 1906 } | 1976 } |
| 1907 | 1977 |
| 1908 return default_code; | 1978 return default_code; |
| 1909 } | 1979 } |
| 1910 | 1980 |
| 1911 } // namespace content | 1981 } // namespace content |
| OLD | NEW |