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