| 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_dispatcher_host.h" | 5 #include "content/browser/service_worker/service_worker_dispatcher_host.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/debug/crash_logging.h" | 9 #include "base/debug/crash_logging.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/profiler/scoped_tracker.h" | 13 #include "base/profiler/scoped_tracker.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "base/trace_event/trace_event.h" | 17 #include "base/trace_event/trace_event.h" |
| 18 #include "content/browser/bad_message.h" | 18 #include "content/browser/bad_message.h" |
| 19 #include "content/browser/message_port_message_filter.h" | |
| 20 #include "content/browser/message_port_service.h" | |
| 21 #include "content/browser/service_worker/embedded_worker_registry.h" | 19 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 22 #include "content/browser/service_worker/embedded_worker_status.h" | 20 #include "content/browser/service_worker/embedded_worker_status.h" |
| 23 #include "content/browser/service_worker/service_worker_client_utils.h" | 21 #include "content/browser/service_worker/service_worker_client_utils.h" |
| 24 #include "content/browser/service_worker/service_worker_context_core.h" | 22 #include "content/browser/service_worker/service_worker_context_core.h" |
| 25 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 23 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 26 #include "content/browser/service_worker/service_worker_handle.h" | 24 #include "content/browser/service_worker/service_worker_handle.h" |
| 27 #include "content/browser/service_worker/service_worker_navigation_handle_core.h
" | 25 #include "content/browser/service_worker/service_worker_navigation_handle_core.h
" |
| 28 #include "content/browser/service_worker/service_worker_registration.h" | 26 #include "content/browser/service_worker/service_worker_registration.h" |
| 29 #include "content/browser/service_worker/service_worker_registration_handle.h" | 27 #include "content/browser/service_worker/service_worker_registration_handle.h" |
| 30 #include "content/common/service_worker/embedded_worker_messages.h" | 28 #include "content/common/service_worker/embedded_worker_messages.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 WebContents* GetWebContents(int render_process_id, int render_frame_id) { | 76 WebContents* GetWebContents(int render_process_id, int render_frame_id) { |
| 79 RenderFrameHost* rfh = | 77 RenderFrameHost* rfh = |
| 80 RenderFrameHost::FromID(render_process_id, render_frame_id); | 78 RenderFrameHost::FromID(render_process_id, render_frame_id); |
| 81 return WebContents::FromRenderFrameHost(rfh); | 79 return WebContents::FromRenderFrameHost(rfh); |
| 82 } | 80 } |
| 83 | 81 |
| 84 } // namespace | 82 } // namespace |
| 85 | 83 |
| 86 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( | 84 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( |
| 87 int render_process_id, | 85 int render_process_id, |
| 88 MessagePortMessageFilter* message_port_message_filter, | |
| 89 ResourceContext* resource_context) | 86 ResourceContext* resource_context) |
| 90 : BrowserMessageFilter(kFilteredMessageClasses, | 87 : BrowserMessageFilter(kFilteredMessageClasses, |
| 91 arraysize(kFilteredMessageClasses)), | 88 arraysize(kFilteredMessageClasses)), |
| 92 render_process_id_(render_process_id), | 89 render_process_id_(render_process_id), |
| 93 message_port_message_filter_(message_port_message_filter), | |
| 94 resource_context_(resource_context), | 90 resource_context_(resource_context), |
| 95 channel_ready_(false), | 91 channel_ready_(false), |
| 96 weak_factory_(this) { | 92 weak_factory_(this) { |
| 97 AddAssociatedInterface( | 93 AddAssociatedInterface( |
| 98 mojom::ServiceWorkerDispatcherHost::Name_, | 94 mojom::ServiceWorkerDispatcherHost::Name_, |
| 99 base::Bind(&ServiceWorkerDispatcherHost::AddMojoBinding, | 95 base::Bind(&ServiceWorkerDispatcherHost::AddMojoBinding, |
| 100 base::Unretained(this))); | 96 base::Unretained(this))); |
| 101 } | 97 } |
| 102 | 98 |
| 103 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { | 99 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 110 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 115 base::Bind(&ServiceWorkerDispatcherHost::Init, this, | 111 base::Bind(&ServiceWorkerDispatcherHost::Init, this, |
| 116 base::RetainedRef(context_wrapper))); | 112 base::RetainedRef(context_wrapper))); |
| 117 return; | 113 return; |
| 118 } | 114 } |
| 119 | 115 |
| 120 context_wrapper_ = context_wrapper; | 116 context_wrapper_ = context_wrapper; |
| 121 if (!GetContext()) | 117 if (!GetContext()) |
| 122 return; | 118 return; |
| 123 GetContext()->embedded_worker_registry()->AddChildProcessSender( | 119 GetContext()->embedded_worker_registry()->AddChildProcessSender( |
| 124 render_process_id_, this, message_port_message_filter_); | 120 render_process_id_, this); |
| 125 } | 121 } |
| 126 | 122 |
| 127 void ServiceWorkerDispatcherHost::OnFilterAdded(IPC::Channel* channel) { | 123 void ServiceWorkerDispatcherHost::OnFilterAdded(IPC::Channel* channel) { |
| 128 TRACE_EVENT0("ServiceWorker", | 124 TRACE_EVENT0("ServiceWorker", |
| 129 "ServiceWorkerDispatcherHost::OnFilterAdded"); | 125 "ServiceWorkerDispatcherHost::OnFilterAdded"); |
| 130 channel_ready_ = true; | 126 channel_ready_ = true; |
| 131 std::vector<std::unique_ptr<IPC::Message>> messages; | 127 std::vector<std::unique_ptr<IPC::Message>> messages; |
| 132 messages.swap(pending_messages_); | 128 messages.swap(pending_messages_); |
| 133 for (auto& message : messages) { | 129 for (auto& message : messages) { |
| 134 BrowserMessageFilter::Send(message.release()); | 130 BrowserMessageFilter::Send(message.release()); |
| (...skipping 768 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 903 registration->id(), registration->pattern().GetOrigin(), value, | 899 registration->id(), registration->pattern().GetOrigin(), value, |
| 904 base::Bind(&ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadHeader, | 900 base::Bind(&ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadHeader, |
| 905 this, thread_id, request_id, registration->id(), value)); | 901 this, thread_id, request_id, registration->id(), value)); |
| 906 } | 902 } |
| 907 | 903 |
| 908 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( | 904 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( |
| 909 int handle_id, | 905 int handle_id, |
| 910 int provider_id, | 906 int provider_id, |
| 911 const base::string16& message, | 907 const base::string16& message, |
| 912 const url::Origin& source_origin, | 908 const url::Origin& source_origin, |
| 913 const std::vector<int>& sent_message_ports) { | 909 const std::vector<MessagePort>& sent_message_ports) { |
| 914 TRACE_EVENT0("ServiceWorker", | 910 TRACE_EVENT0("ServiceWorker", |
| 915 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); | 911 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); |
| 916 if (!GetContext()) | 912 if (!GetContext()) |
| 917 return; | 913 return; |
| 918 | 914 |
| 919 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); | 915 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); |
| 920 if (!handle) { | 916 if (!handle) { |
| 921 bad_message::ReceivedBadMessage(this, bad_message::SWDH_POST_MESSAGE); | 917 bad_message::ReceivedBadMessage(this, bad_message::SWDH_POST_MESSAGE); |
| 922 return; | 918 return; |
| 923 } | 919 } |
| 924 | 920 |
| 925 ServiceWorkerProviderHost* sender_provider_host = | 921 ServiceWorkerProviderHost* sender_provider_host = |
| 926 GetContext()->GetProviderHost(render_process_id_, provider_id); | 922 GetContext()->GetProviderHost(render_process_id_, provider_id); |
| 927 if (!sender_provider_host) { | 923 if (!sender_provider_host) { |
| 928 // This may occur when destruction of the sender provider overtakes | 924 // This may occur when destruction of the sender provider overtakes |
| 929 // postMessage() because of thread hopping on WebServiceWorkerImpl. | 925 // postMessage() because of thread hopping on WebServiceWorkerImpl. |
| 930 return; | 926 return; |
| 931 } | 927 } |
| 932 | 928 |
| 933 DispatchExtendableMessageEvent( | 929 DispatchExtendableMessageEvent( |
| 934 make_scoped_refptr(handle->version()), message, source_origin, | 930 make_scoped_refptr(handle->version()), message, source_origin, |
| 935 sent_message_ports, sender_provider_host, | 931 sent_message_ports, sender_provider_host, |
| 936 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 932 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 937 } | 933 } |
| 938 | 934 |
| 939 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent( | 935 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent( |
| 940 scoped_refptr<ServiceWorkerVersion> worker, | 936 scoped_refptr<ServiceWorkerVersion> worker, |
| 941 const base::string16& message, | 937 const base::string16& message, |
| 942 const url::Origin& source_origin, | 938 const url::Origin& source_origin, |
| 943 const std::vector<int>& sent_message_ports, | 939 const std::vector<MessagePort>& sent_message_ports, |
| 944 ServiceWorkerProviderHost* sender_provider_host, | 940 ServiceWorkerProviderHost* sender_provider_host, |
| 945 const StatusCallback& callback) { | 941 const StatusCallback& callback) { |
| 946 for (int port : sent_message_ports) | |
| 947 MessagePortService::GetInstance()->HoldMessages(port); | |
| 948 | |
| 949 switch (sender_provider_host->provider_type()) { | 942 switch (sender_provider_host->provider_type()) { |
| 950 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: | 943 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: |
| 951 case SERVICE_WORKER_PROVIDER_FOR_WORKER: | 944 case SERVICE_WORKER_PROVIDER_FOR_WORKER: |
| 952 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: | 945 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: |
| 953 service_worker_client_utils::GetClient( | 946 service_worker_client_utils::GetClient( |
| 954 sender_provider_host, | 947 sender_provider_host, |
| 955 base::Bind(&ServiceWorkerDispatcherHost:: | 948 base::Bind(&ServiceWorkerDispatcherHost:: |
| 956 DispatchExtendableMessageEventInternal< | 949 DispatchExtendableMessageEventInternal< |
| 957 ServiceWorkerClientInfo>, | 950 ServiceWorkerClientInfo>, |
| 958 this, worker, message, source_origin, sent_message_ports, | 951 this, worker, message, source_origin, sent_message_ports, |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1124 | 1117 |
| 1125 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId, | 1118 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId, |
| 1126 provider_id, info, attrs)); | 1119 provider_id, info, attrs)); |
| 1127 } | 1120 } |
| 1128 | 1121 |
| 1129 template <typename SourceInfo> | 1122 template <typename SourceInfo> |
| 1130 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( | 1123 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( |
| 1131 scoped_refptr<ServiceWorkerVersion> worker, | 1124 scoped_refptr<ServiceWorkerVersion> worker, |
| 1132 const base::string16& message, | 1125 const base::string16& message, |
| 1133 const url::Origin& source_origin, | 1126 const url::Origin& source_origin, |
| 1134 const std::vector<int>& sent_message_ports, | 1127 const std::vector<MessagePort>& sent_message_ports, |
| 1135 const base::Optional<base::TimeDelta>& timeout, | 1128 const base::Optional<base::TimeDelta>& timeout, |
| 1136 const StatusCallback& callback, | 1129 const StatusCallback& callback, |
| 1137 const SourceInfo& source_info) { | 1130 const SourceInfo& source_info) { |
| 1138 if (!source_info.IsValid()) { | 1131 if (!source_info.IsValid()) { |
| 1139 DidFailToDispatchExtendableMessageEvent<SourceInfo>( | 1132 DidFailToDispatchExtendableMessageEvent<SourceInfo>( |
| 1140 sent_message_ports, source_info, callback, SERVICE_WORKER_ERROR_FAILED); | 1133 sent_message_ports, source_info, callback, SERVICE_WORKER_ERROR_FAILED); |
| 1141 return; | 1134 return; |
| 1142 } | 1135 } |
| 1143 | 1136 |
| 1144 // If not enough time is left to actually process the event don't even | 1137 // If not enough time is left to actually process the event don't even |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1160 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent< | 1153 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent< |
| 1161 SourceInfo>, | 1154 SourceInfo>, |
| 1162 this, sent_message_ports, source_info, callback)); | 1155 this, sent_message_ports, source_info, callback)); |
| 1163 } | 1156 } |
| 1164 | 1157 |
| 1165 void ServiceWorkerDispatcherHost:: | 1158 void ServiceWorkerDispatcherHost:: |
| 1166 DispatchExtendableMessageEventAfterStartWorker( | 1159 DispatchExtendableMessageEventAfterStartWorker( |
| 1167 scoped_refptr<ServiceWorkerVersion> worker, | 1160 scoped_refptr<ServiceWorkerVersion> worker, |
| 1168 const base::string16& message, | 1161 const base::string16& message, |
| 1169 const url::Origin& source_origin, | 1162 const url::Origin& source_origin, |
| 1170 const std::vector<int>& sent_message_ports, | 1163 const std::vector<MessagePort>& sent_message_ports, |
| 1171 const ExtendableMessageEventSource& source, | 1164 const ExtendableMessageEventSource& source, |
| 1172 const base::Optional<base::TimeDelta>& timeout, | 1165 const base::Optional<base::TimeDelta>& timeout, |
| 1173 const StatusCallback& callback) { | 1166 const StatusCallback& callback) { |
| 1174 int request_id; | 1167 int request_id; |
| 1175 if (timeout) { | 1168 if (timeout) { |
| 1176 request_id = worker->StartRequestWithCustomTimeout( | 1169 request_id = worker->StartRequestWithCustomTimeout( |
| 1177 ServiceWorkerMetrics::EventType::MESSAGE, callback, *timeout, | 1170 ServiceWorkerMetrics::EventType::MESSAGE, callback, *timeout, |
| 1178 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT); | 1171 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT); |
| 1179 } else { | 1172 } else { |
| 1180 request_id = worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, | 1173 request_id = worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, |
| 1181 callback); | 1174 callback); |
| 1182 } | 1175 } |
| 1183 | 1176 |
| 1184 MessagePortMessageFilter* filter = | |
| 1185 worker->embedded_worker()->message_port_message_filter(); | |
| 1186 std::vector<int> new_routing_ids; | |
| 1187 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); | |
| 1188 | |
| 1189 mojom::ExtendableMessageEventPtr event = mojom::ExtendableMessageEvent::New(); | 1177 mojom::ExtendableMessageEventPtr event = mojom::ExtendableMessageEvent::New(); |
| 1190 event->message = message; | 1178 event->message = message; |
| 1191 event->source_origin = source_origin; | 1179 event->source_origin = source_origin; |
| 1192 event->message_ports = sent_message_ports; | 1180 event->message_ports = MessagePort::ReleaseHandles(sent_message_ports); |
| 1193 event->new_routing_ids = new_routing_ids; | |
| 1194 event->source = source; | 1181 event->source = source; |
| 1195 | 1182 |
| 1196 // Hide the client url if the client has a unique origin. | 1183 // Hide the client url if the client has a unique origin. |
| 1197 if (source_origin.unique()) { | 1184 if (source_origin.unique()) { |
| 1198 if (event->source.client_info.IsValid()) | 1185 if (event->source.client_info.IsValid()) |
| 1199 event->source.client_info.url = GURL(); | 1186 event->source.client_info.url = GURL(); |
| 1200 else | 1187 else |
| 1201 event->source.service_worker_info.url = GURL(); | 1188 event->source.service_worker_info.url = GURL(); |
| 1202 } | 1189 } |
| 1203 | 1190 |
| 1204 // |event_dispatcher| is owned by |worker|, once |worker| got destroyed, the | 1191 // |event_dispatcher| is owned by |worker|, once |worker| got destroyed, the |
| 1205 // bound function will never be called, so it is safe to use | 1192 // bound function will never be called, so it is safe to use |
| 1206 // base::Unretained() here. | 1193 // base::Unretained() here. |
| 1207 worker->event_dispatcher()->DispatchExtendableMessageEvent( | 1194 worker->event_dispatcher()->DispatchExtendableMessageEvent( |
| 1208 std::move(event), base::Bind(&ServiceWorkerVersion::OnSimpleEventFinished, | 1195 std::move(event), base::Bind(&ServiceWorkerVersion::OnSimpleEventFinished, |
| 1209 base::Unretained(worker.get()), request_id)); | 1196 base::Unretained(worker.get()), request_id)); |
| 1210 } | 1197 } |
| 1211 | 1198 |
| 1212 template <typename SourceInfo> | 1199 template <typename SourceInfo> |
| 1213 void ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent( | 1200 void ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent( |
| 1214 const std::vector<int>& sent_message_ports, | 1201 const std::vector<MessagePort>& sent_message_ports, |
| 1215 const SourceInfo& source_info, | 1202 const SourceInfo& source_info, |
| 1216 const StatusCallback& callback, | 1203 const StatusCallback& callback, |
| 1217 ServiceWorkerStatusCode status) { | 1204 ServiceWorkerStatusCode status) { |
| 1218 // Transfering the message ports failed, so destroy the ports. | |
| 1219 for (int port : sent_message_ports) | |
| 1220 MessagePortService::GetInstance()->ClosePort(port); | |
| 1221 if (source_info.IsValid()) | 1205 if (source_info.IsValid()) |
| 1222 ReleaseSourceInfo(source_info); | 1206 ReleaseSourceInfo(source_info); |
| 1223 callback.Run(status); | 1207 callback.Run(status); |
| 1224 } | 1208 } |
| 1225 | 1209 |
| 1226 void ServiceWorkerDispatcherHost::ReleaseSourceInfo( | 1210 void ServiceWorkerDispatcherHost::ReleaseSourceInfo( |
| 1227 const ServiceWorkerClientInfo& source_info) { | 1211 const ServiceWorkerClientInfo& source_info) { |
| 1228 // ServiceWorkerClientInfo is just a snapshot of the client. There is no need | 1212 // ServiceWorkerClientInfo is just a snapshot of the client. There is no need |
| 1229 // to do anything for it. | 1213 // to do anything for it. |
| 1230 } | 1214 } |
| (...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1767 if (!handle) { | 1751 if (!handle) { |
| 1768 bad_message::ReceivedBadMessage(this, | 1752 bad_message::ReceivedBadMessage(this, |
| 1769 bad_message::SWDH_TERMINATE_BAD_HANDLE); | 1753 bad_message::SWDH_TERMINATE_BAD_HANDLE); |
| 1770 return; | 1754 return; |
| 1771 } | 1755 } |
| 1772 handle->version()->StopWorker( | 1756 handle->version()->StopWorker( |
| 1773 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1757 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 1774 } | 1758 } |
| 1775 | 1759 |
| 1776 } // namespace content | 1760 } // namespace content |
| OLD | NEW |