| 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 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 "Preload Origin Trial token."; | 98 "Preload Origin Trial token."; |
| 101 } | 99 } |
| 102 NOTREACHED(); | 100 NOTREACHED(); |
| 103 return ""; | 101 return ""; |
| 104 } | 102 } |
| 105 | 103 |
| 106 } // namespace | 104 } // namespace |
| 107 | 105 |
| 108 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( | 106 ServiceWorkerDispatcherHost::ServiceWorkerDispatcherHost( |
| 109 int render_process_id, | 107 int render_process_id, |
| 110 MessagePortMessageFilter* message_port_message_filter, | |
| 111 ResourceContext* resource_context) | 108 ResourceContext* resource_context) |
| 112 : BrowserMessageFilter(kFilteredMessageClasses, | 109 : BrowserMessageFilter(kFilteredMessageClasses, |
| 113 arraysize(kFilteredMessageClasses)), | 110 arraysize(kFilteredMessageClasses)), |
| 114 render_process_id_(render_process_id), | 111 render_process_id_(render_process_id), |
| 115 message_port_message_filter_(message_port_message_filter), | |
| 116 resource_context_(resource_context), | 112 resource_context_(resource_context), |
| 117 channel_ready_(false), | 113 channel_ready_(false), |
| 118 weak_factory_(this) { | 114 weak_factory_(this) { |
| 119 AddAssociatedInterface( | 115 AddAssociatedInterface( |
| 120 mojom::ServiceWorkerDispatcherHost::Name_, | 116 mojom::ServiceWorkerDispatcherHost::Name_, |
| 121 base::Bind(&ServiceWorkerDispatcherHost::AddMojoBinding, | 117 base::Bind(&ServiceWorkerDispatcherHost::AddMojoBinding, |
| 122 base::Unretained(this))); | 118 base::Unretained(this))); |
| 123 } | 119 } |
| 124 | 120 |
| 125 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { | 121 ServiceWorkerDispatcherHost::~ServiceWorkerDispatcherHost() { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 136 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 132 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 137 base::Bind(&ServiceWorkerDispatcherHost::Init, this, | 133 base::Bind(&ServiceWorkerDispatcherHost::Init, this, |
| 138 base::RetainedRef(context_wrapper))); | 134 base::RetainedRef(context_wrapper))); |
| 139 return; | 135 return; |
| 140 } | 136 } |
| 141 | 137 |
| 142 context_wrapper_ = context_wrapper; | 138 context_wrapper_ = context_wrapper; |
| 143 if (!GetContext()) | 139 if (!GetContext()) |
| 144 return; | 140 return; |
| 145 GetContext()->embedded_worker_registry()->AddChildProcessSender( | 141 GetContext()->embedded_worker_registry()->AddChildProcessSender( |
| 146 render_process_id_, this, message_port_message_filter_); | 142 render_process_id_, this); |
| 147 } | 143 } |
| 148 | 144 |
| 149 void ServiceWorkerDispatcherHost::OnFilterAdded(IPC::Channel* channel) { | 145 void ServiceWorkerDispatcherHost::OnFilterAdded(IPC::Channel* channel) { |
| 150 TRACE_EVENT0("ServiceWorker", | 146 TRACE_EVENT0("ServiceWorker", |
| 151 "ServiceWorkerDispatcherHost::OnFilterAdded"); | 147 "ServiceWorkerDispatcherHost::OnFilterAdded"); |
| 152 channel_ready_ = true; | 148 channel_ready_ = true; |
| 153 std::vector<std::unique_ptr<IPC::Message>> messages; | 149 std::vector<std::unique_ptr<IPC::Message>> messages; |
| 154 messages.swap(pending_messages_); | 150 messages.swap(pending_messages_); |
| 155 for (auto& message : messages) { | 151 for (auto& message : messages) { |
| 156 BrowserMessageFilter::Send(message.release()); | 152 BrowserMessageFilter::Send(message.release()); |
| (...skipping 776 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 933 registration->id(), registration->pattern().GetOrigin(), value, | 929 registration->id(), registration->pattern().GetOrigin(), value, |
| 934 base::Bind(&ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadHeader, | 930 base::Bind(&ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadHeader, |
| 935 this, thread_id, request_id, registration->id(), value)); | 931 this, thread_id, request_id, registration->id(), value)); |
| 936 } | 932 } |
| 937 | 933 |
| 938 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( | 934 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( |
| 939 int handle_id, | 935 int handle_id, |
| 940 int provider_id, | 936 int provider_id, |
| 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 TRACE_EVENT0("ServiceWorker", | 940 TRACE_EVENT0("ServiceWorker", |
| 945 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); | 941 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); |
| 946 if (!GetContext()) | 942 if (!GetContext()) |
| 947 return; | 943 return; |
| 948 | 944 |
| 949 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); | 945 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); |
| 950 if (!handle) { | 946 if (!handle) { |
| 951 bad_message::ReceivedBadMessage(this, bad_message::SWDH_POST_MESSAGE); | 947 bad_message::ReceivedBadMessage(this, bad_message::SWDH_POST_MESSAGE); |
| 952 return; | 948 return; |
| 953 } | 949 } |
| 954 | 950 |
| 955 ServiceWorkerProviderHost* sender_provider_host = | 951 ServiceWorkerProviderHost* sender_provider_host = |
| 956 GetContext()->GetProviderHost(render_process_id_, provider_id); | 952 GetContext()->GetProviderHost(render_process_id_, provider_id); |
| 957 if (!sender_provider_host) { | 953 if (!sender_provider_host) { |
| 958 // This may occur when destruction of the sender provider overtakes | 954 // This may occur when destruction of the sender provider overtakes |
| 959 // postMessage() because of thread hopping on WebServiceWorkerImpl. | 955 // postMessage() because of thread hopping on WebServiceWorkerImpl. |
| 960 return; | 956 return; |
| 961 } | 957 } |
| 962 | 958 |
| 963 DispatchExtendableMessageEvent( | 959 DispatchExtendableMessageEvent( |
| 964 make_scoped_refptr(handle->version()), message, source_origin, | 960 make_scoped_refptr(handle->version()), message, source_origin, |
| 965 sent_message_ports, sender_provider_host, | 961 sent_message_ports, sender_provider_host, |
| 966 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 962 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 967 } | 963 } |
| 968 | 964 |
| 969 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent( | 965 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent( |
| 970 scoped_refptr<ServiceWorkerVersion> worker, | 966 scoped_refptr<ServiceWorkerVersion> worker, |
| 971 const base::string16& message, | 967 const base::string16& message, |
| 972 const url::Origin& source_origin, | 968 const url::Origin& source_origin, |
| 973 const std::vector<int>& sent_message_ports, | 969 const std::vector<MessagePort>& sent_message_ports, |
| 974 ServiceWorkerProviderHost* sender_provider_host, | 970 ServiceWorkerProviderHost* sender_provider_host, |
| 975 const StatusCallback& callback) { | 971 const StatusCallback& callback) { |
| 976 for (int port : sent_message_ports) | |
| 977 MessagePortService::GetInstance()->HoldMessages(port); | |
| 978 | |
| 979 switch (sender_provider_host->provider_type()) { | 972 switch (sender_provider_host->provider_type()) { |
| 980 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: | 973 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: |
| 981 case SERVICE_WORKER_PROVIDER_FOR_WORKER: | 974 case SERVICE_WORKER_PROVIDER_FOR_WORKER: |
| 982 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: | 975 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: |
| 983 service_worker_client_utils::GetClient( | 976 service_worker_client_utils::GetClient( |
| 984 sender_provider_host, | 977 sender_provider_host, |
| 985 base::Bind(&ServiceWorkerDispatcherHost:: | 978 base::Bind(&ServiceWorkerDispatcherHost:: |
| 986 DispatchExtendableMessageEventInternal< | 979 DispatchExtendableMessageEventInternal< |
| 987 ServiceWorkerClientInfo>, | 980 ServiceWorkerClientInfo>, |
| 988 this, worker, message, source_origin, sent_message_ports, | 981 this, worker, message, source_origin, sent_message_ports, |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1163 | 1156 |
| 1164 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId, | 1157 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId, |
| 1165 provider_id, info, attrs)); | 1158 provider_id, info, attrs)); |
| 1166 } | 1159 } |
| 1167 | 1160 |
| 1168 template <typename SourceInfo> | 1161 template <typename SourceInfo> |
| 1169 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( | 1162 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( |
| 1170 scoped_refptr<ServiceWorkerVersion> worker, | 1163 scoped_refptr<ServiceWorkerVersion> worker, |
| 1171 const base::string16& message, | 1164 const base::string16& message, |
| 1172 const url::Origin& source_origin, | 1165 const url::Origin& source_origin, |
| 1173 const std::vector<int>& sent_message_ports, | 1166 const std::vector<MessagePort>& sent_message_ports, |
| 1174 const base::Optional<base::TimeDelta>& timeout, | 1167 const base::Optional<base::TimeDelta>& timeout, |
| 1175 const StatusCallback& callback, | 1168 const StatusCallback& callback, |
| 1176 const SourceInfo& source_info) { | 1169 const SourceInfo& source_info) { |
| 1177 if (!source_info.IsValid()) { | 1170 if (!source_info.IsValid()) { |
| 1178 DidFailToDispatchExtendableMessageEvent<SourceInfo>( | 1171 DidFailToDispatchExtendableMessageEvent<SourceInfo>( |
| 1179 sent_message_ports, source_info, callback, SERVICE_WORKER_ERROR_FAILED); | 1172 sent_message_ports, source_info, callback, SERVICE_WORKER_ERROR_FAILED); |
| 1180 return; | 1173 return; |
| 1181 } | 1174 } |
| 1182 | 1175 |
| 1183 // If not enough time is left to actually process the event don't even | 1176 // If not enough time is left to actually process the event don't even |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1199 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent< | 1192 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent< |
| 1200 SourceInfo>, | 1193 SourceInfo>, |
| 1201 this, sent_message_ports, source_info, callback)); | 1194 this, sent_message_ports, source_info, callback)); |
| 1202 } | 1195 } |
| 1203 | 1196 |
| 1204 void ServiceWorkerDispatcherHost:: | 1197 void ServiceWorkerDispatcherHost:: |
| 1205 DispatchExtendableMessageEventAfterStartWorker( | 1198 DispatchExtendableMessageEventAfterStartWorker( |
| 1206 scoped_refptr<ServiceWorkerVersion> worker, | 1199 scoped_refptr<ServiceWorkerVersion> worker, |
| 1207 const base::string16& message, | 1200 const base::string16& message, |
| 1208 const url::Origin& source_origin, | 1201 const url::Origin& source_origin, |
| 1209 const std::vector<int>& sent_message_ports, | 1202 const std::vector<MessagePort>& sent_message_ports, |
| 1210 const ExtendableMessageEventSource& source, | 1203 const ExtendableMessageEventSource& source, |
| 1211 const base::Optional<base::TimeDelta>& timeout, | 1204 const base::Optional<base::TimeDelta>& timeout, |
| 1212 const StatusCallback& callback) { | 1205 const StatusCallback& callback) { |
| 1213 int request_id; | 1206 int request_id; |
| 1214 if (timeout) { | 1207 if (timeout) { |
| 1215 request_id = worker->StartRequestWithCustomTimeout( | 1208 request_id = worker->StartRequestWithCustomTimeout( |
| 1216 ServiceWorkerMetrics::EventType::MESSAGE, callback, *timeout, | 1209 ServiceWorkerMetrics::EventType::MESSAGE, callback, *timeout, |
| 1217 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT); | 1210 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT); |
| 1218 } else { | 1211 } else { |
| 1219 request_id = worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, | 1212 request_id = worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, |
| 1220 callback); | 1213 callback); |
| 1221 } | 1214 } |
| 1222 | 1215 |
| 1223 MessagePortMessageFilter* filter = | |
| 1224 worker->embedded_worker()->message_port_message_filter(); | |
| 1225 std::vector<int> new_routing_ids; | |
| 1226 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); | |
| 1227 | |
| 1228 mojom::ExtendableMessageEventPtr event = mojom::ExtendableMessageEvent::New(); | 1216 mojom::ExtendableMessageEventPtr event = mojom::ExtendableMessageEvent::New(); |
| 1229 event->message = message; | 1217 event->message = message; |
| 1230 event->source_origin = source_origin; | 1218 event->source_origin = source_origin; |
| 1231 event->message_ports = sent_message_ports; | 1219 event->message_ports = MessagePort::ReleaseHandles(sent_message_ports); |
| 1232 event->new_routing_ids = new_routing_ids; | |
| 1233 event->source = source; | 1220 event->source = source; |
| 1234 | 1221 |
| 1235 // Hide the client url if the client has a unique origin. | 1222 // Hide the client url if the client has a unique origin. |
| 1236 if (source_origin.unique()) { | 1223 if (source_origin.unique()) { |
| 1237 if (event->source.client_info.IsValid()) | 1224 if (event->source.client_info.IsValid()) |
| 1238 event->source.client_info.url = GURL(); | 1225 event->source.client_info.url = GURL(); |
| 1239 else | 1226 else |
| 1240 event->source.service_worker_info.url = GURL(); | 1227 event->source.service_worker_info.url = GURL(); |
| 1241 } | 1228 } |
| 1242 | 1229 |
| 1243 // |event_dispatcher| is owned by |worker|, once |worker| got destroyed, the | 1230 // |event_dispatcher| is owned by |worker|, once |worker| got destroyed, the |
| 1244 // bound function will never be called, so it is safe to use | 1231 // bound function will never be called, so it is safe to use |
| 1245 // base::Unretained() here. | 1232 // base::Unretained() here. |
| 1246 worker->event_dispatcher()->DispatchExtendableMessageEvent( | 1233 worker->event_dispatcher()->DispatchExtendableMessageEvent( |
| 1247 std::move(event), base::Bind(&ServiceWorkerVersion::OnSimpleEventFinished, | 1234 std::move(event), base::Bind(&ServiceWorkerVersion::OnSimpleEventFinished, |
| 1248 base::Unretained(worker.get()), request_id)); | 1235 base::Unretained(worker.get()), request_id)); |
| 1249 } | 1236 } |
| 1250 | 1237 |
| 1251 template <typename SourceInfo> | 1238 template <typename SourceInfo> |
| 1252 void ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent( | 1239 void ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent( |
| 1253 const std::vector<int>& sent_message_ports, | 1240 const std::vector<MessagePort>& sent_message_ports, |
| 1254 const SourceInfo& source_info, | 1241 const SourceInfo& source_info, |
| 1255 const StatusCallback& callback, | 1242 const StatusCallback& callback, |
| 1256 ServiceWorkerStatusCode status) { | 1243 ServiceWorkerStatusCode status) { |
| 1257 // Transfering the message ports failed, so destroy the ports. | |
| 1258 for (int port : sent_message_ports) | |
| 1259 MessagePortService::GetInstance()->ClosePort(port); | |
| 1260 if (source_info.IsValid()) | 1244 if (source_info.IsValid()) |
| 1261 ReleaseSourceInfo(source_info); | 1245 ReleaseSourceInfo(source_info); |
| 1262 callback.Run(status); | 1246 callback.Run(status); |
| 1263 } | 1247 } |
| 1264 | 1248 |
| 1265 void ServiceWorkerDispatcherHost::ReleaseSourceInfo( | 1249 void ServiceWorkerDispatcherHost::ReleaseSourceInfo( |
| 1266 const ServiceWorkerClientInfo& source_info) { | 1250 const ServiceWorkerClientInfo& source_info) { |
| 1267 // ServiceWorkerClientInfo is just a snapshot of the client. There is no need | 1251 // ServiceWorkerClientInfo is just a snapshot of the client. There is no need |
| 1268 // to do anything for it. | 1252 // to do anything for it. |
| 1269 } | 1253 } |
| (...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1806 if (!handle) { | 1790 if (!handle) { |
| 1807 bad_message::ReceivedBadMessage(this, | 1791 bad_message::ReceivedBadMessage(this, |
| 1808 bad_message::SWDH_TERMINATE_BAD_HANDLE); | 1792 bad_message::SWDH_TERMINATE_BAD_HANDLE); |
| 1809 return; | 1793 return; |
| 1810 } | 1794 } |
| 1811 handle->version()->StopWorker( | 1795 handle->version()->StopWorker( |
| 1812 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1796 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
| 1813 } | 1797 } |
| 1814 | 1798 |
| 1815 } // namespace content | 1799 } // namespace content |
| OLD | NEW |