| 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 ServiceWorkerVersion::ServiceWorkerVersion( | 391 ServiceWorkerVersion::ServiceWorkerVersion( |
| 370 ServiceWorkerRegistration* registration, | 392 ServiceWorkerRegistration* registration, |
| 371 const GURL& script_url, | 393 const GURL& script_url, |
| 372 int64 version_id, | 394 int64 version_id, |
| 373 base::WeakPtr<ServiceWorkerContextCore> context) | 395 base::WeakPtr<ServiceWorkerContextCore> context) |
| (...skipping 411 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 785 MessagePortMessageFilter* filter = | 807 MessagePortMessageFilter* filter = |
| 786 embedded_worker_->message_port_message_filter(); | 808 embedded_worker_->message_port_message_filter(); |
| 787 std::vector<int> new_routing_ids; | 809 std::vector<int> new_routing_ids; |
| 788 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); | 810 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); |
| 789 ServiceWorkerStatusCode status = | 811 ServiceWorkerStatusCode status = |
| 790 embedded_worker_->SendMessage(ServiceWorkerMsg_CrossOriginMessageToWorker( | 812 embedded_worker_->SendMessage(ServiceWorkerMsg_CrossOriginMessageToWorker( |
| 791 client, message, sent_message_ports, new_routing_ids)); | 813 client, message, sent_message_ports, new_routing_ids)); |
| 792 RunSoon(base::Bind(callback, status)); | 814 RunSoon(base::Bind(callback, status)); |
| 793 } | 815 } |
| 794 | 816 |
| 817 void ServiceWorkerVersion::SendStashedMessagePorts( |
| 818 const std::vector<TransferredMessagePort>& stashed_message_ports, |
| 819 const std::vector<base::string16>& port_names, |
| 820 const SendStashedPortsCallback& callback) { |
| 821 if (running_status() != RUNNING) { |
| 822 // Schedule calling this method after starting the worker. |
| 823 StartWorker(base::Bind( |
| 824 &RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), |
| 825 base::Bind(&RunErrorSendStashedPortsCallback, callback), |
| 826 base::Bind(&self::SendStashedMessagePorts, weak_factory_.GetWeakPtr(), |
| 827 stashed_message_ports, port_names, callback))); |
| 828 return; |
| 829 } |
| 830 |
| 831 MessagePortMessageFilter* filter = |
| 832 embedded_worker_->message_port_message_filter(); |
| 833 std::vector<int> new_routing_ids(stashed_message_ports.size()); |
| 834 for (size_t i = 0; i < stashed_message_ports.size(); ++i) |
| 835 new_routing_ids[i] = filter->GetNextRoutingID(); |
| 836 |
| 837 ServiceWorkerStatusCode status = |
| 838 embedded_worker_->SendMessage(ServiceWorkerMsg_SendStashedMessagePorts( |
| 839 stashed_message_ports, new_routing_ids, port_names)); |
| 840 RunSoon(base::Bind(callback, status, new_routing_ids)); |
| 841 } |
| 842 |
| 795 void ServiceWorkerVersion::AddControllee( | 843 void ServiceWorkerVersion::AddControllee( |
| 796 ServiceWorkerProviderHost* provider_host) { | 844 ServiceWorkerProviderHost* provider_host) { |
| 797 const std::string& uuid = provider_host->client_uuid(); | 845 const std::string& uuid = provider_host->client_uuid(); |
| 798 CHECK(!provider_host->client_uuid().empty()); | 846 CHECK(!provider_host->client_uuid().empty()); |
| 799 DCHECK(!ContainsKey(controllee_map_, uuid)); | 847 DCHECK(!ContainsKey(controllee_map_, uuid)); |
| 800 controllee_map_[uuid] = provider_host; | 848 controllee_map_[uuid] = provider_host; |
| 801 // Keep the worker alive a bit longer right after a new controllee is added. | 849 // Keep the worker alive a bit longer right after a new controllee is added. |
| 802 RestartTick(&idle_time_); | 850 RestartTick(&idle_time_); |
| 803 } | 851 } |
| 804 | 852 |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1039 OnClearCachedMetadata) | 1087 OnClearCachedMetadata) |
| 1040 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, | 1088 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, |
| 1041 OnPostMessageToClient) | 1089 OnPostMessageToClient) |
| 1042 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, | 1090 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, |
| 1043 OnFocusClient) | 1091 OnFocusClient) |
| 1044 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, | 1092 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, |
| 1045 OnSkipWaiting) | 1093 OnSkipWaiting) |
| 1046 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, | 1094 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClaimClients, |
| 1047 OnClaimClients) | 1095 OnClaimClients) |
| 1048 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_Pong, OnPongFromWorker) | 1096 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_Pong, OnPongFromWorker) |
| 1097 IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_StashMessagePort, |
| 1098 OnStashMessagePort) |
| 1049 IPC_MESSAGE_UNHANDLED(handled = false) | 1099 IPC_MESSAGE_UNHANDLED(handled = false) |
| 1050 IPC_END_MESSAGE_MAP() | 1100 IPC_END_MESSAGE_MAP() |
| 1051 return handled; | 1101 return handled; |
| 1052 } | 1102 } |
| 1053 | 1103 |
| 1054 void ServiceWorkerVersion::OnStartSentAndScriptEvaluated( | 1104 void ServiceWorkerVersion::OnStartSentAndScriptEvaluated( |
| 1055 ServiceWorkerStatusCode status) { | 1105 ServiceWorkerStatusCode status) { |
| 1056 if (status != SERVICE_WORKER_OK) { | 1106 if (status != SERVICE_WORKER_OK) { |
| 1057 RunCallbacks(this, &start_callbacks_, | 1107 RunCallbacks(this, &start_callbacks_, |
| 1058 DeduceStartWorkerFailureReason(status)); | 1108 DeduceStartWorkerFailureReason(status)); |
| (...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1514 | 1564 |
| 1515 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( | 1565 embedded_worker_->SendMessage(ServiceWorkerMsg_ClaimClientsError( |
| 1516 request_id, blink::WebServiceWorkerError::ErrorTypeAbort, | 1566 request_id, blink::WebServiceWorkerError::ErrorTypeAbort, |
| 1517 base::ASCIIToUTF16(kClaimClientsShutdownErrorMesage))); | 1567 base::ASCIIToUTF16(kClaimClientsShutdownErrorMesage))); |
| 1518 } | 1568 } |
| 1519 | 1569 |
| 1520 void ServiceWorkerVersion::OnPongFromWorker() { | 1570 void ServiceWorkerVersion::OnPongFromWorker() { |
| 1521 ClearTick(&ping_time_); | 1571 ClearTick(&ping_time_); |
| 1522 } | 1572 } |
| 1523 | 1573 |
| 1574 void ServiceWorkerVersion::OnStashMessagePort(int message_port_id, |
| 1575 const base::string16& name) { |
| 1576 // Just abort if we are shutting down. |
| 1577 if (!context_) |
| 1578 return; |
| 1579 |
| 1580 ServiceWorkerRegistration* registration = |
| 1581 context_->GetLiveRegistration(registration_id_); |
| 1582 if (!registration) |
| 1583 return; |
| 1584 |
| 1585 // TODO(mek): Figure out a way to avoid this round-trip through the UI thread. |
| 1586 BrowserThread::PostTaskAndReplyWithResult( |
| 1587 BrowserThread::UI, FROM_HERE, |
| 1588 base::Bind(&GetStashedPortManagerOnUIThread, |
| 1589 make_scoped_refptr(context_->wrapper())), |
| 1590 base::Bind(&StashPortImpl, make_scoped_refptr(this), message_port_id, |
| 1591 name)); |
| 1592 } |
| 1593 |
| 1524 void ServiceWorkerVersion::DidEnsureLiveRegistrationForStartWorker( | 1594 void ServiceWorkerVersion::DidEnsureLiveRegistrationForStartWorker( |
| 1525 bool pause_after_download, | 1595 bool pause_after_download, |
| 1526 const StatusCallback& callback, | 1596 const StatusCallback& callback, |
| 1527 ServiceWorkerStatusCode status, | 1597 ServiceWorkerStatusCode status, |
| 1528 const scoped_refptr<ServiceWorkerRegistration>& protect) { | 1598 const scoped_refptr<ServiceWorkerRegistration>& protect) { |
| 1529 if (status != SERVICE_WORKER_OK) { | 1599 if (status != SERVICE_WORKER_OK) { |
| 1530 RecordStartWorkerResult(status); | 1600 RecordStartWorkerResult(status); |
| 1531 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED)); | 1601 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED)); |
| 1532 return; | 1602 return; |
| 1533 } | 1603 } |
| (...skipping 344 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1878 return SERVICE_WORKER_ERROR_ABORT; | 1948 return SERVICE_WORKER_ERROR_ABORT; |
| 1879 default: | 1949 default: |
| 1880 return SERVICE_WORKER_ERROR_NETWORK; | 1950 return SERVICE_WORKER_ERROR_NETWORK; |
| 1881 } | 1951 } |
| 1882 } | 1952 } |
| 1883 | 1953 |
| 1884 return default_code; | 1954 return default_code; |
| 1885 } | 1955 } |
| 1886 | 1956 |
| 1887 } // namespace content | 1957 } // namespace content |
| OLD | NEW |