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 777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
934 registration->id(), registration->pattern().GetOrigin(), value, | 930 registration->id(), registration->pattern().GetOrigin(), value, |
935 base::Bind(&ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadHeader, | 931 base::Bind(&ServiceWorkerDispatcherHost::DidUpdateNavigationPreloadHeader, |
936 this, thread_id, request_id, registration->id(), value)); | 932 this, thread_id, request_id, registration->id(), value)); |
937 } | 933 } |
938 | 934 |
939 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( | 935 void ServiceWorkerDispatcherHost::OnPostMessageToWorker( |
940 int handle_id, | 936 int handle_id, |
941 int provider_id, | 937 int provider_id, |
942 const base::string16& message, | 938 const base::string16& message, |
943 const url::Origin& source_origin, | 939 const url::Origin& source_origin, |
944 const std::vector<int>& sent_message_ports) { | 940 const std::vector<MessagePort>& sent_message_ports) { |
945 TRACE_EVENT0("ServiceWorker", | 941 TRACE_EVENT0("ServiceWorker", |
946 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); | 942 "ServiceWorkerDispatcherHost::OnPostMessageToWorker"); |
947 if (!GetContext()) | 943 if (!GetContext()) |
948 return; | 944 return; |
949 | 945 |
950 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); | 946 ServiceWorkerHandle* handle = handles_.Lookup(handle_id); |
951 if (!handle) { | 947 if (!handle) { |
952 bad_message::ReceivedBadMessage(this, bad_message::SWDH_POST_MESSAGE); | 948 bad_message::ReceivedBadMessage(this, bad_message::SWDH_POST_MESSAGE); |
953 return; | 949 return; |
954 } | 950 } |
955 | 951 |
956 ServiceWorkerProviderHost* sender_provider_host = | 952 ServiceWorkerProviderHost* sender_provider_host = |
957 GetContext()->GetProviderHost(render_process_id_, provider_id); | 953 GetContext()->GetProviderHost(render_process_id_, provider_id); |
958 if (!sender_provider_host) { | 954 if (!sender_provider_host) { |
959 // This may occur when destruction of the sender provider overtakes | 955 // This may occur when destruction of the sender provider overtakes |
960 // postMessage() because of thread hopping on WebServiceWorkerImpl. | 956 // postMessage() because of thread hopping on WebServiceWorkerImpl. |
961 return; | 957 return; |
962 } | 958 } |
963 | 959 |
964 DispatchExtendableMessageEvent( | 960 DispatchExtendableMessageEvent( |
965 make_scoped_refptr(handle->version()), message, source_origin, | 961 make_scoped_refptr(handle->version()), message, source_origin, |
966 sent_message_ports, sender_provider_host, | 962 sent_message_ports, sender_provider_host, |
967 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 963 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
968 } | 964 } |
969 | 965 |
970 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent( | 966 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEvent( |
971 scoped_refptr<ServiceWorkerVersion> worker, | 967 scoped_refptr<ServiceWorkerVersion> worker, |
972 const base::string16& message, | 968 const base::string16& message, |
973 const url::Origin& source_origin, | 969 const url::Origin& source_origin, |
974 const std::vector<int>& sent_message_ports, | 970 const std::vector<MessagePort>& sent_message_ports, |
975 ServiceWorkerProviderHost* sender_provider_host, | 971 ServiceWorkerProviderHost* sender_provider_host, |
976 const StatusCallback& callback) { | 972 const StatusCallback& callback) { |
977 for (int port : sent_message_ports) | |
978 MessagePortService::GetInstance()->HoldMessages(port); | |
979 | |
980 switch (sender_provider_host->provider_type()) { | 973 switch (sender_provider_host->provider_type()) { |
981 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: | 974 case SERVICE_WORKER_PROVIDER_FOR_WINDOW: |
982 case SERVICE_WORKER_PROVIDER_FOR_WORKER: | 975 case SERVICE_WORKER_PROVIDER_FOR_WORKER: |
983 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: | 976 case SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER: |
984 service_worker_client_utils::GetClient( | 977 service_worker_client_utils::GetClient( |
985 sender_provider_host, | 978 sender_provider_host, |
986 base::Bind(&ServiceWorkerDispatcherHost:: | 979 base::Bind(&ServiceWorkerDispatcherHost:: |
987 DispatchExtendableMessageEventInternal< | 980 DispatchExtendableMessageEventInternal< |
988 ServiceWorkerClientInfo>, | 981 ServiceWorkerClientInfo>, |
989 this, worker, message, source_origin, sent_message_ports, | 982 this, worker, message, source_origin, sent_message_ports, |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1164 | 1157 |
1165 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId, | 1158 Send(new ServiceWorkerMsg_AssociateRegistration(kDocumentMainThreadId, |
1166 provider_id, info, attrs)); | 1159 provider_id, info, attrs)); |
1167 } | 1160 } |
1168 | 1161 |
1169 template <typename SourceInfo> | 1162 template <typename SourceInfo> |
1170 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( | 1163 void ServiceWorkerDispatcherHost::DispatchExtendableMessageEventInternal( |
1171 scoped_refptr<ServiceWorkerVersion> worker, | 1164 scoped_refptr<ServiceWorkerVersion> worker, |
1172 const base::string16& message, | 1165 const base::string16& message, |
1173 const url::Origin& source_origin, | 1166 const url::Origin& source_origin, |
1174 const std::vector<int>& sent_message_ports, | 1167 const std::vector<MessagePort>& sent_message_ports, |
1175 const base::Optional<base::TimeDelta>& timeout, | 1168 const base::Optional<base::TimeDelta>& timeout, |
1176 const StatusCallback& callback, | 1169 const StatusCallback& callback, |
1177 const SourceInfo& source_info) { | 1170 const SourceInfo& source_info) { |
1178 if (!source_info.IsValid()) { | 1171 if (!source_info.IsValid()) { |
1179 DidFailToDispatchExtendableMessageEvent<SourceInfo>( | 1172 DidFailToDispatchExtendableMessageEvent<SourceInfo>( |
1180 sent_message_ports, source_info, callback, SERVICE_WORKER_ERROR_FAILED); | 1173 sent_message_ports, source_info, callback, SERVICE_WORKER_ERROR_FAILED); |
1181 return; | 1174 return; |
1182 } | 1175 } |
1183 | 1176 |
1184 // If not enough time is left to actually process the event don't even | 1177 // If not enough time is left to actually process the event don't even |
(...skipping 15 matching lines...) Expand all Loading... |
1200 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent< | 1193 &ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent< |
1201 SourceInfo>, | 1194 SourceInfo>, |
1202 this, sent_message_ports, source_info, callback)); | 1195 this, sent_message_ports, source_info, callback)); |
1203 } | 1196 } |
1204 | 1197 |
1205 void ServiceWorkerDispatcherHost:: | 1198 void ServiceWorkerDispatcherHost:: |
1206 DispatchExtendableMessageEventAfterStartWorker( | 1199 DispatchExtendableMessageEventAfterStartWorker( |
1207 scoped_refptr<ServiceWorkerVersion> worker, | 1200 scoped_refptr<ServiceWorkerVersion> worker, |
1208 const base::string16& message, | 1201 const base::string16& message, |
1209 const url::Origin& source_origin, | 1202 const url::Origin& source_origin, |
1210 const std::vector<int>& sent_message_ports, | 1203 const std::vector<MessagePort>& sent_message_ports, |
1211 const ExtendableMessageEventSource& source, | 1204 const ExtendableMessageEventSource& source, |
1212 const base::Optional<base::TimeDelta>& timeout, | 1205 const base::Optional<base::TimeDelta>& timeout, |
1213 const StatusCallback& callback) { | 1206 const StatusCallback& callback) { |
1214 int request_id; | 1207 int request_id; |
1215 if (timeout) { | 1208 if (timeout) { |
1216 request_id = worker->StartRequestWithCustomTimeout( | 1209 request_id = worker->StartRequestWithCustomTimeout( |
1217 ServiceWorkerMetrics::EventType::MESSAGE, callback, *timeout, | 1210 ServiceWorkerMetrics::EventType::MESSAGE, callback, *timeout, |
1218 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT); | 1211 ServiceWorkerVersion::CONTINUE_ON_TIMEOUT); |
1219 } else { | 1212 } else { |
1220 request_id = worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, | 1213 request_id = worker->StartRequest(ServiceWorkerMetrics::EventType::MESSAGE, |
1221 callback); | 1214 callback); |
1222 } | 1215 } |
1223 | 1216 |
1224 MessagePortMessageFilter* filter = | |
1225 worker->embedded_worker()->message_port_message_filter(); | |
1226 std::vector<int> new_routing_ids; | |
1227 filter->UpdateMessagePortsWithNewRoutes(sent_message_ports, &new_routing_ids); | |
1228 | |
1229 mojom::ExtendableMessageEventPtr event = mojom::ExtendableMessageEvent::New(); | 1217 mojom::ExtendableMessageEventPtr event = mojom::ExtendableMessageEvent::New(); |
1230 event->message = message; | 1218 event->message = message; |
1231 event->source_origin = source_origin; | 1219 event->source_origin = source_origin; |
1232 event->message_ports = sent_message_ports; | 1220 event->message_ports = MessagePort::ReleaseHandles(sent_message_ports); |
1233 event->new_routing_ids = new_routing_ids; | |
1234 event->source = source; | 1221 event->source = source; |
1235 | 1222 |
1236 // Hide the client url if the client has a unique origin. | 1223 // Hide the client url if the client has a unique origin. |
1237 if (source_origin.unique()) { | 1224 if (source_origin.unique()) { |
1238 if (event->source.client_info.IsValid()) | 1225 if (event->source.client_info.IsValid()) |
1239 event->source.client_info.url = GURL(); | 1226 event->source.client_info.url = GURL(); |
1240 else | 1227 else |
1241 event->source.service_worker_info.url = GURL(); | 1228 event->source.service_worker_info.url = GURL(); |
1242 } | 1229 } |
1243 | 1230 |
1244 worker->event_dispatcher()->DispatchExtendableMessageEvent( | 1231 worker->event_dispatcher()->DispatchExtendableMessageEvent( |
1245 std::move(event), worker->CreateSimpleEventCallback(request_id)); | 1232 std::move(event), worker->CreateSimpleEventCallback(request_id)); |
1246 } | 1233 } |
1247 | 1234 |
1248 template <typename SourceInfo> | 1235 template <typename SourceInfo> |
1249 void ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent( | 1236 void ServiceWorkerDispatcherHost::DidFailToDispatchExtendableMessageEvent( |
1250 const std::vector<int>& sent_message_ports, | 1237 const std::vector<MessagePort>& sent_message_ports, |
1251 const SourceInfo& source_info, | 1238 const SourceInfo& source_info, |
1252 const StatusCallback& callback, | 1239 const StatusCallback& callback, |
1253 ServiceWorkerStatusCode status) { | 1240 ServiceWorkerStatusCode status) { |
1254 // Transfering the message ports failed, so destroy the ports. | |
1255 for (int port : sent_message_ports) | |
1256 MessagePortService::GetInstance()->ClosePort(port); | |
1257 if (source_info.IsValid()) | 1241 if (source_info.IsValid()) |
1258 ReleaseSourceInfo(source_info); | 1242 ReleaseSourceInfo(source_info); |
1259 callback.Run(status); | 1243 callback.Run(status); |
1260 } | 1244 } |
1261 | 1245 |
1262 void ServiceWorkerDispatcherHost::ReleaseSourceInfo( | 1246 void ServiceWorkerDispatcherHost::ReleaseSourceInfo( |
1263 const ServiceWorkerClientInfo& source_info) { | 1247 const ServiceWorkerClientInfo& source_info) { |
1264 // ServiceWorkerClientInfo is just a snapshot of the client. There is no need | 1248 // ServiceWorkerClientInfo is just a snapshot of the client. There is no need |
1265 // to do anything for it. | 1249 // to do anything for it. |
1266 } | 1250 } |
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1813 if (!handle) { | 1797 if (!handle) { |
1814 bad_message::ReceivedBadMessage(this, | 1798 bad_message::ReceivedBadMessage(this, |
1815 bad_message::SWDH_TERMINATE_BAD_HANDLE); | 1799 bad_message::SWDH_TERMINATE_BAD_HANDLE); |
1816 return; | 1800 return; |
1817 } | 1801 } |
1818 handle->version()->StopWorker( | 1802 handle->version()->StopWorker( |
1819 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); | 1803 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback)); |
1820 } | 1804 } |
1821 | 1805 |
1822 } // namespace content | 1806 } // namespace content |
OLD | NEW |