OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/renderer/service_worker/service_worker_context_client.h" | 5 #include "content/renderer/service_worker/service_worker_context_client.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 #include "content/child/service_worker/service_worker_provider_context.h" | 24 #include "content/child/service_worker/service_worker_provider_context.h" |
25 #include "content/child/service_worker/service_worker_registration_handle_refere nce.h" | 25 #include "content/child/service_worker/service_worker_registration_handle_refere nce.h" |
26 #include "content/child/service_worker/web_service_worker_impl.h" | 26 #include "content/child/service_worker/web_service_worker_impl.h" |
27 #include "content/child/service_worker/web_service_worker_provider_impl.h" | 27 #include "content/child/service_worker/web_service_worker_provider_impl.h" |
28 #include "content/child/service_worker/web_service_worker_registration_impl.h" | 28 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
29 #include "content/child/thread_safe_sender.h" | 29 #include "content/child/thread_safe_sender.h" |
30 #include "content/child/webmessageportchannel_impl.h" | 30 #include "content/child/webmessageportchannel_impl.h" |
31 #include "content/common/devtools_messages.h" | 31 #include "content/common/devtools_messages.h" |
32 #include "content/common/message_port_messages.h" | 32 #include "content/common/message_port_messages.h" |
33 #include "content/common/service_worker/embedded_worker_messages.h" | 33 #include "content/common/service_worker/embedded_worker_messages.h" |
34 #include "content/common/service_worker/fetch_event_dispatcher.mojom.h" | |
34 #include "content/common/service_worker/service_worker_messages.h" | 35 #include "content/common/service_worker/service_worker_messages.h" |
36 #include "content/common/service_worker/service_worker_status_code.h" | |
35 #include "content/public/common/push_event_payload.h" | 37 #include "content/public/common/push_event_payload.h" |
36 #include "content/public/common/referrer.h" | 38 #include "content/public/common/referrer.h" |
37 #include "content/public/renderer/content_renderer_client.h" | 39 #include "content/public/renderer/content_renderer_client.h" |
38 #include "content/public/renderer/document_state.h" | 40 #include "content/public/renderer/document_state.h" |
39 #include "content/renderer/background_sync/background_sync_client_impl.h" | 41 #include "content/renderer/background_sync/background_sync_client_impl.h" |
40 #include "content/renderer/devtools/devtools_agent.h" | 42 #include "content/renderer/devtools/devtools_agent.h" |
41 #include "content/renderer/render_thread_impl.h" | 43 #include "content/renderer/render_thread_impl.h" |
42 #include "content/renderer/service_worker/embedded_worker_dispatcher.h" | 44 #include "content/renderer/service_worker/embedded_worker_dispatcher.h" |
43 #include "content/renderer/service_worker/service_worker_type_util.h" | 45 #include "content/renderer/service_worker/service_worker_type_util.h" |
44 #include "ipc/ipc_message.h" | 46 #include "ipc/ipc_message.h" |
45 #include "ipc/ipc_message_macros.h" | 47 #include "ipc/ipc_message_macros.h" |
46 #include "mojo/public/cpp/bindings/interface_request.h" | 48 #include "mojo/public/cpp/bindings/interface_request.h" |
49 #include "mojo/public/cpp/bindings/strong_binding.h" | |
47 #include "services/shell/public/cpp/interface_provider.h" | 50 #include "services/shell/public/cpp/interface_provider.h" |
48 #include "services/shell/public/cpp/interface_registry.h" | 51 #include "services/shell/public/cpp/interface_registry.h" |
49 #include "third_party/WebKit/public/platform/URLConversion.h" | 52 #include "third_party/WebKit/public/platform/URLConversion.h" |
50 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h" | 53 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h" |
51 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" | 54 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" |
52 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" | 55 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" |
53 #include "third_party/WebKit/public/platform/WebString.h" | 56 #include "third_party/WebKit/public/platform/WebString.h" |
54 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegi stration.h" | 57 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegi stration.h" |
55 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati onData.h" | 58 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati onData.h" |
56 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerClientQueryOptions.h" | 59 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerClientQueryOptions.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
173 using ClaimClientsCallbacksMap = | 176 using ClaimClientsCallbacksMap = |
174 IDMap<blink::WebServiceWorkerClientsClaimCallbacks, IDMapOwnPointer>; | 177 IDMap<blink::WebServiceWorkerClientsClaimCallbacks, IDMapOwnPointer>; |
175 using ClientCallbacksMap = | 178 using ClientCallbacksMap = |
176 IDMap<blink::WebServiceWorkerClientCallbacks, IDMapOwnPointer>; | 179 IDMap<blink::WebServiceWorkerClientCallbacks, IDMapOwnPointer>; |
177 using SkipWaitingCallbacksMap = | 180 using SkipWaitingCallbacksMap = |
178 IDMap<blink::WebServiceWorkerSkipWaitingCallbacks, IDMapOwnPointer>; | 181 IDMap<blink::WebServiceWorkerSkipWaitingCallbacks, IDMapOwnPointer>; |
179 using SyncEventCallbacksMap = | 182 using SyncEventCallbacksMap = |
180 IDMap<const base::Callback<void(blink::mojom::ServiceWorkerEventStatus, | 183 IDMap<const base::Callback<void(blink::mojom::ServiceWorkerEventStatus, |
181 base::Time /* dispatch_event_time */)>, | 184 base::Time /* dispatch_event_time */)>, |
182 IDMapOwnPointer>; | 185 IDMapOwnPointer>; |
186 using FetchEventCallbacksMap = | |
187 IDMap<const base::Callback<void(ServiceWorkerStatusCode, | |
188 base::Time /* dispatch_event_time */)>, | |
189 IDMapOwnPointer>; | |
183 | 190 |
184 explicit WorkerContextData(ServiceWorkerContextClient* owner) | 191 explicit WorkerContextData(ServiceWorkerContextClient* owner) |
185 : weak_factory(owner), proxy_weak_factory(owner->proxy_) {} | 192 : weak_factory(owner), proxy_weak_factory(owner->proxy_) {} |
186 | 193 |
187 ~WorkerContextData() { | 194 ~WorkerContextData() { |
188 DCHECK(thread_checker.CalledOnValidThread()); | 195 DCHECK(thread_checker.CalledOnValidThread()); |
189 } | 196 } |
190 | 197 |
191 // Pending callbacks for GetClientDocuments(). | 198 // Pending callbacks for GetClientDocuments(). |
192 ClientsCallbacksMap clients_callbacks; | 199 ClientsCallbacksMap clients_callbacks; |
193 | 200 |
194 // Pending callbacks for OpenWindow() and FocusClient(). | 201 // Pending callbacks for OpenWindow() and FocusClient(). |
195 ClientCallbacksMap client_callbacks; | 202 ClientCallbacksMap client_callbacks; |
196 | 203 |
197 // Pending callbacks for SkipWaiting(). | 204 // Pending callbacks for SkipWaiting(). |
198 SkipWaitingCallbacksMap skip_waiting_callbacks; | 205 SkipWaitingCallbacksMap skip_waiting_callbacks; |
199 | 206 |
200 // Pending callbacks for ClaimClients(). | 207 // Pending callbacks for ClaimClients(). |
201 ClaimClientsCallbacksMap claim_clients_callbacks; | 208 ClaimClientsCallbacksMap claim_clients_callbacks; |
202 | 209 |
203 // Pending callbacks for Background Sync Events | 210 // Pending callbacks for Background Sync Events |
204 SyncEventCallbacksMap sync_event_callbacks; | 211 SyncEventCallbacksMap sync_event_callbacks; |
205 | 212 |
213 // Pending callbacks for Fetch Events | |
214 FetchEventCallbacksMap fetch_event_callbacks; | |
215 | |
206 shell::InterfaceRegistry interface_registry; | 216 shell::InterfaceRegistry interface_registry; |
207 shell::InterfaceProvider remote_interfaces; | 217 shell::InterfaceProvider remote_interfaces; |
208 | 218 |
209 base::ThreadChecker thread_checker; | 219 base::ThreadChecker thread_checker; |
210 base::WeakPtrFactory<ServiceWorkerContextClient> weak_factory; | 220 base::WeakPtrFactory<ServiceWorkerContextClient> weak_factory; |
211 base::WeakPtrFactory<blink::WebServiceWorkerContextProxy> proxy_weak_factory; | 221 base::WeakPtrFactory<blink::WebServiceWorkerContextProxy> proxy_weak_factory; |
212 }; | 222 }; |
213 | 223 |
224 class ServiceWorkerContextClient::FetchEventDispatcherImpl | |
225 : public NON_EXPORTED_BASE(mojom::FetchEventDispatcher) { | |
226 public: | |
227 static void Create(mojom::FetchEventDispatcherRequest request) { | |
228 mojo::MakeStrongBinding(base::MakeUnique<FetchEventDispatcherImpl>(), | |
229 std::move(request)); | |
230 } | |
231 | |
232 FetchEventDispatcherImpl() {} | |
233 | |
234 ~FetchEventDispatcherImpl() override {} | |
235 | |
236 void DispatchFetchEvent(int response_id, | |
237 const ServiceWorkerFetchRequest& request, | |
238 const DispatchFetchEventCallback& callback) override { | |
239 ServiceWorkerContextClient* client = | |
240 ServiceWorkerContextClient::ThreadSpecificInstance(); | |
241 if (!client) { | |
242 callback.Run(SERVICE_WORKER_ERROR_ABORT, base::Time::Now()); | |
243 return; | |
244 } | |
245 client->DispatchFetchEvent(response_id, request, callback); | |
246 } | |
247 | |
248 private: | |
249 DISALLOW_COPY_AND_ASSIGN(FetchEventDispatcherImpl); | |
250 }; | |
251 | |
214 ServiceWorkerContextClient* | 252 ServiceWorkerContextClient* |
215 ServiceWorkerContextClient::ThreadSpecificInstance() { | 253 ServiceWorkerContextClient::ThreadSpecificInstance() { |
216 return g_worker_client_tls.Pointer()->Get(); | 254 return g_worker_client_tls.Pointer()->Get(); |
217 } | 255 } |
218 | 256 |
219 ServiceWorkerContextClient::ServiceWorkerContextClient( | 257 ServiceWorkerContextClient::ServiceWorkerContextClient( |
220 int embedded_worker_id, | 258 int embedded_worker_id, |
221 int64_t service_worker_version_id, | 259 int64_t service_worker_version_id, |
222 const GURL& service_worker_scope, | 260 const GURL& service_worker_scope, |
223 const GURL& script_url, | 261 const GURL& script_url, |
(...skipping 21 matching lines...) Expand all Loading... | |
245 void ServiceWorkerContextClient::OnMessageReceived( | 283 void ServiceWorkerContextClient::OnMessageReceived( |
246 int thread_id, | 284 int thread_id, |
247 int embedded_worker_id, | 285 int embedded_worker_id, |
248 const IPC::Message& message) { | 286 const IPC::Message& message) { |
249 CHECK_EQ(embedded_worker_id_, embedded_worker_id); | 287 CHECK_EQ(embedded_worker_id_, embedded_worker_id); |
250 bool handled = true; | 288 bool handled = true; |
251 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) | 289 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) |
252 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) | 290 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) |
253 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ExtendableMessageEvent, | 291 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ExtendableMessageEvent, |
254 OnExtendableMessageEvent) | 292 OnExtendableMessageEvent) |
255 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent) | |
256 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) | 293 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) |
257 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent, | 294 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent, |
258 OnNotificationClickEvent) | 295 OnNotificationClickEvent) |
259 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationCloseEvent, | 296 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationCloseEvent, |
260 OnNotificationCloseEvent) | 297 OnNotificationCloseEvent) |
261 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) | 298 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) |
262 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient) | 299 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient) |
263 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) | 300 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) |
264 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, | 301 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, |
265 OnOpenWindowResponse) | 302 OnOpenWindowResponse) |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
378 ServiceWorkerRegistrationObjectInfo registration_info; | 415 ServiceWorkerRegistrationObjectInfo registration_info; |
379 ServiceWorkerVersionAttributes version_attrs; | 416 ServiceWorkerVersionAttributes version_attrs; |
380 provider_context_->GetAssociatedRegistration(®istration_info, | 417 provider_context_->GetAssociatedRegistration(®istration_info, |
381 &version_attrs); | 418 &version_attrs); |
382 DCHECK_NE(registration_info.registration_id, | 419 DCHECK_NE(registration_info.registration_id, |
383 kInvalidServiceWorkerRegistrationId); | 420 kInvalidServiceWorkerRegistrationId); |
384 | 421 |
385 // Register Mojo interfaces. | 422 // Register Mojo interfaces. |
386 context_->interface_registry.AddInterface( | 423 context_->interface_registry.AddInterface( |
387 base::Bind(&BackgroundSyncClientImpl::Create)); | 424 base::Bind(&BackgroundSyncClientImpl::Create)); |
425 context_->interface_registry.AddInterface( | |
426 base::Bind(&FetchEventDispatcherImpl::Create)); | |
388 | 427 |
389 SetRegistrationInServiceWorkerGlobalScope(registration_info, version_attrs); | 428 SetRegistrationInServiceWorkerGlobalScope(registration_info, version_attrs); |
390 | 429 |
391 Send(new EmbeddedWorkerHostMsg_WorkerThreadStarted( | 430 Send(new EmbeddedWorkerHostMsg_WorkerThreadStarted( |
392 embedded_worker_id_, WorkerThread::GetCurrentId(), | 431 embedded_worker_id_, WorkerThread::GetCurrentId(), |
393 provider_context_->provider_id())); | 432 provider_context_->provider_id())); |
394 | 433 |
395 TRACE_EVENT_ASYNC_STEP_INTO0( | 434 TRACE_EVENT_ASYNC_STEP_INTO0( |
396 "ServiceWorker", | 435 "ServiceWorker", |
397 "ServiceWorkerContextClient::StartingWorkerContext", | 436 "ServiceWorkerContextClient::StartingWorkerContext", |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
545 web_response.cacheStorageCacheName().utf8(), cors_exposed_header_names); | 584 web_response.cacheStorageCacheName().utf8(), cors_exposed_header_names); |
546 Send(new ServiceWorkerHostMsg_FetchEventResponse( | 585 Send(new ServiceWorkerHostMsg_FetchEventResponse( |
547 GetRoutingID(), response_id, SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, | 586 GetRoutingID(), response_id, SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, |
548 response, base::Time::FromDoubleT(event_dispatch_time))); | 587 response, base::Time::FromDoubleT(event_dispatch_time))); |
549 } | 588 } |
550 | 589 |
551 void ServiceWorkerContextClient::didHandleFetchEvent( | 590 void ServiceWorkerContextClient::didHandleFetchEvent( |
552 int event_finish_id, | 591 int event_finish_id, |
553 blink::WebServiceWorkerEventResult result, | 592 blink::WebServiceWorkerEventResult result, |
554 double event_dispatch_time) { | 593 double event_dispatch_time) { |
555 Send(new ServiceWorkerHostMsg_FetchEventFinished( | 594 const FetchCallback* callback = |
556 GetRoutingID(), event_finish_id, result, | 595 context_->fetch_event_callbacks.Lookup(event_finish_id); |
557 base::Time::FromDoubleT(event_dispatch_time))); | 596 if (!callback) |
597 return; | |
598 | |
599 callback->Run(result == blink::WebServiceWorkerEventResultCompleted | |
600 ? SERVICE_WORKER_OK | |
601 : SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED, | |
602 base::Time::FromDoubleT(event_dispatch_time)); | |
603 context_->fetch_event_callbacks.Remove(event_finish_id); | |
558 } | 604 } |
559 | 605 |
560 void ServiceWorkerContextClient::didHandleNotificationClickEvent( | 606 void ServiceWorkerContextClient::didHandleNotificationClickEvent( |
561 int request_id, | 607 int request_id, |
562 blink::WebServiceWorkerEventResult result, | 608 blink::WebServiceWorkerEventResult result, |
563 double event_dispatch_time) { | 609 double event_dispatch_time) { |
564 Send(new ServiceWorkerHostMsg_NotificationClickEventFinished( | 610 Send(new ServiceWorkerHostMsg_NotificationClickEventFinished( |
565 GetRoutingID(), request_id, result, | 611 GetRoutingID(), request_id, result, |
566 base::Time::FromDoubleT(event_dispatch_time))); | 612 base::Time::FromDoubleT(event_dispatch_time))); |
567 } | 613 } |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
787 request_id, params.message, params.source_origin, ports, | 833 request_id, params.message, params.source_origin, ports, |
788 WebServiceWorkerImpl::CreateHandle(worker)); | 834 WebServiceWorkerImpl::CreateHandle(worker)); |
789 } | 835 } |
790 | 836 |
791 void ServiceWorkerContextClient::OnInstallEvent(int request_id) { | 837 void ServiceWorkerContextClient::OnInstallEvent(int request_id) { |
792 TRACE_EVENT0("ServiceWorker", | 838 TRACE_EVENT0("ServiceWorker", |
793 "ServiceWorkerContextClient::OnInstallEvent"); | 839 "ServiceWorkerContextClient::OnInstallEvent"); |
794 proxy_->dispatchInstallEvent(request_id); | 840 proxy_->dispatchInstallEvent(request_id); |
795 } | 841 } |
796 | 842 |
797 void ServiceWorkerContextClient::OnFetchEvent( | 843 void ServiceWorkerContextClient::DispatchFetchEvent( |
798 int response_id, | 844 int response_id, |
799 int event_finish_id, | 845 const ServiceWorkerFetchRequest& request, |
800 const ServiceWorkerFetchRequest& request) { | 846 const FetchCallback& callback) { |
801 blink::WebServiceWorkerRequest webRequest; | 847 blink::WebServiceWorkerRequest webRequest; |
802 TRACE_EVENT0("ServiceWorker", | 848 TRACE_EVENT0("ServiceWorker", |
803 "ServiceWorkerContextClient::OnFetchEvent"); | 849 "ServiceWorkerContextClient::DispatchFetchEvent"); |
850 int event_finish_id = | |
dcheng
2016/10/04 07:19:53
Nit: I don't have a better idea, but I found it a
horo
2016/10/04 12:16:40
I'm thinking about changing all to fetch_event_id.
| |
851 context_->fetch_event_callbacks.Add(new FetchCallback(callback)); | |
852 | |
804 webRequest.setURL(blink::WebURL(request.url)); | 853 webRequest.setURL(blink::WebURL(request.url)); |
805 webRequest.setMethod(blink::WebString::fromUTF8(request.method)); | 854 webRequest.setMethod(blink::WebString::fromUTF8(request.method)); |
806 for (ServiceWorkerHeaderMap::const_iterator it = request.headers.begin(); | 855 for (ServiceWorkerHeaderMap::const_iterator it = request.headers.begin(); |
807 it != request.headers.end(); | 856 it != request.headers.end(); |
808 ++it) { | 857 ++it) { |
809 webRequest.setHeader(blink::WebString::fromUTF8(it->first), | 858 webRequest.setHeader(blink::WebString::fromUTF8(it->first), |
810 blink::WebString::fromUTF8(it->second)); | 859 blink::WebString::fromUTF8(it->second)); |
811 } | 860 } |
812 if (!request.blob_uuid.empty()) { | 861 if (!request.blob_uuid.empty()) { |
813 webRequest.setBlob(blink::WebString::fromUTF8(request.blob_uuid), | 862 webRequest.setBlob(blink::WebString::fromUTF8(request.blob_uuid), |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1055 } | 1104 } |
1056 | 1105 |
1057 base::WeakPtr<ServiceWorkerContextClient> | 1106 base::WeakPtr<ServiceWorkerContextClient> |
1058 ServiceWorkerContextClient::GetWeakPtr() { | 1107 ServiceWorkerContextClient::GetWeakPtr() { |
1059 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 1108 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
1060 DCHECK(context_); | 1109 DCHECK(context_); |
1061 return context_->weak_factory.GetWeakPtr(); | 1110 return context_->weak_factory.GetWeakPtr(); |
1062 } | 1111 } |
1063 | 1112 |
1064 } // namespace content | 1113 } // namespace content |
OLD | NEW |