| 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 "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "content/child/thread_safe_sender.h" | 24 #include "content/child/thread_safe_sender.h" |
| 25 #include "content/child/webmessageportchannel_impl.h" | 25 #include "content/child/webmessageportchannel_impl.h" |
| 26 #include "content/child/worker_task_runner.h" | 26 #include "content/child/worker_task_runner.h" |
| 27 #include "content/common/devtools_messages.h" | 27 #include "content/common/devtools_messages.h" |
| 28 #include "content/common/message_port_messages.h" | 28 #include "content/common/message_port_messages.h" |
| 29 #include "content/common/mojo/service_registry_impl.h" | 29 #include "content/common/mojo/service_registry_impl.h" |
| 30 #include "content/common/service_worker/embedded_worker_messages.h" | 30 #include "content/common/service_worker/embedded_worker_messages.h" |
| 31 #include "content/common/service_worker/service_worker_messages.h" | 31 #include "content/common/service_worker/service_worker_messages.h" |
| 32 #include "content/public/common/referrer.h" | 32 #include "content/public/common/referrer.h" |
| 33 #include "content/public/renderer/document_state.h" | 33 #include "content/public/renderer/document_state.h" |
| 34 #include "content/renderer/background_sync/background_sync_client_impl.h" |
| 34 #include "content/renderer/devtools/devtools_agent.h" | 35 #include "content/renderer/devtools/devtools_agent.h" |
| 35 #include "content/renderer/render_thread_impl.h" | 36 #include "content/renderer/render_thread_impl.h" |
| 36 #include "content/renderer/service_worker/embedded_worker_dispatcher.h" | 37 #include "content/renderer/service_worker/embedded_worker_dispatcher.h" |
| 37 #include "content/renderer/service_worker/service_worker_type_util.h" | 38 #include "content/renderer/service_worker/service_worker_type_util.h" |
| 38 #include "ipc/ipc_message.h" | 39 #include "ipc/ipc_message.h" |
| 39 #include "ipc/ipc_message_macros.h" | 40 #include "ipc/ipc_message_macros.h" |
| 40 #include "third_party/WebKit/public/platform/WebCrossOriginServiceWorkerClient.h
" | 41 #include "third_party/WebKit/public/platform/WebCrossOriginServiceWorkerClient.h
" |
| 41 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h" | 42 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h" |
| 42 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" | 43 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" |
| 43 #include "third_party/WebKit/public/platform/WebServiceWorkerClientQueryOptions.
h" | 44 #include "third_party/WebKit/public/platform/WebServiceWorkerClientQueryOptions.
h" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 // worker thread. | 173 // worker thread. |
| 173 struct ServiceWorkerContextClient::WorkerContextData { | 174 struct ServiceWorkerContextClient::WorkerContextData { |
| 174 using ClientsCallbacksMap = | 175 using ClientsCallbacksMap = |
| 175 IDMap<blink::WebServiceWorkerClientsCallbacks, IDMapOwnPointer>; | 176 IDMap<blink::WebServiceWorkerClientsCallbacks, IDMapOwnPointer>; |
| 176 using ClaimClientsCallbacksMap = | 177 using ClaimClientsCallbacksMap = |
| 177 IDMap<blink::WebServiceWorkerClientsClaimCallbacks, IDMapOwnPointer>; | 178 IDMap<blink::WebServiceWorkerClientsClaimCallbacks, IDMapOwnPointer>; |
| 178 using ClientCallbacksMap = | 179 using ClientCallbacksMap = |
| 179 IDMap<blink::WebServiceWorkerClientCallbacks, IDMapOwnPointer>; | 180 IDMap<blink::WebServiceWorkerClientCallbacks, IDMapOwnPointer>; |
| 180 using SkipWaitingCallbacksMap = | 181 using SkipWaitingCallbacksMap = |
| 181 IDMap<blink::WebServiceWorkerSkipWaitingCallbacks, IDMapOwnPointer>; | 182 IDMap<blink::WebServiceWorkerSkipWaitingCallbacks, IDMapOwnPointer>; |
| 183 using SyncEventCallbacksMap = |
| 184 IDMap<const mojo::Callback<void(ServiceWorkerEventStatus)>, |
| 185 IDMapOwnPointer>; |
| 182 | 186 |
| 183 explicit WorkerContextData(ServiceWorkerContextClient* owner) | 187 explicit WorkerContextData(ServiceWorkerContextClient* owner) |
| 184 : weak_factory(owner) {} | 188 : weak_factory(owner) {} |
| 185 | 189 |
| 186 ~WorkerContextData() { | 190 ~WorkerContextData() { |
| 187 DCHECK(thread_checker.CalledOnValidThread()); | 191 DCHECK(thread_checker.CalledOnValidThread()); |
| 188 } | 192 } |
| 189 | 193 |
| 190 // Pending callbacks for GetClientDocuments(). | 194 // Pending callbacks for GetClientDocuments(). |
| 191 ClientsCallbacksMap clients_callbacks; | 195 ClientsCallbacksMap clients_callbacks; |
| 192 | 196 |
| 193 // Pending callbacks for OpenWindow() and FocusClient(). | 197 // Pending callbacks for OpenWindow() and FocusClient(). |
| 194 ClientCallbacksMap client_callbacks; | 198 ClientCallbacksMap client_callbacks; |
| 195 | 199 |
| 196 // Pending callbacks for SkipWaiting(). | 200 // Pending callbacks for SkipWaiting(). |
| 197 SkipWaitingCallbacksMap skip_waiting_callbacks; | 201 SkipWaitingCallbacksMap skip_waiting_callbacks; |
| 198 | 202 |
| 199 // Pending callbacks for ClaimClients(). | 203 // Pending callbacks for ClaimClients(). |
| 200 ClaimClientsCallbacksMap claim_clients_callbacks; | 204 ClaimClientsCallbacksMap claim_clients_callbacks; |
| 201 | 205 |
| 206 // Pending callbacks for Background Sync Events |
| 207 SyncEventCallbacksMap sync_event_callbacks; |
| 208 |
| 202 ServiceRegistryImpl service_registry; | 209 ServiceRegistryImpl service_registry; |
| 203 | 210 |
| 204 base::ThreadChecker thread_checker; | 211 base::ThreadChecker thread_checker; |
| 205 base::WeakPtrFactory<ServiceWorkerContextClient> weak_factory; | 212 base::WeakPtrFactory<ServiceWorkerContextClient> weak_factory; |
| 206 }; | 213 }; |
| 207 | 214 |
| 208 ServiceWorkerContextClient* | 215 ServiceWorkerContextClient* |
| 209 ServiceWorkerContextClient::ThreadSpecificInstance() { | 216 ServiceWorkerContextClient::ThreadSpecificInstance() { |
| 210 return g_worker_client_tls.Pointer()->Get(); | 217 return g_worker_client_tls.Pointer()->Get(); |
| 211 } | 218 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 239 void ServiceWorkerContextClient::OnMessageReceived( | 246 void ServiceWorkerContextClient::OnMessageReceived( |
| 240 int thread_id, | 247 int thread_id, |
| 241 int embedded_worker_id, | 248 int embedded_worker_id, |
| 242 const IPC::Message& message) { | 249 const IPC::Message& message) { |
| 243 CHECK_EQ(embedded_worker_id_, embedded_worker_id); | 250 CHECK_EQ(embedded_worker_id_, embedded_worker_id); |
| 244 bool handled = true; | 251 bool handled = true; |
| 245 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) | 252 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) |
| 246 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) | 253 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) |
| 247 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent) | 254 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent) |
| 248 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) | 255 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) |
| 249 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SyncEvent, OnSyncEvent) | |
| 250 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent, | 256 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent, |
| 251 OnNotificationClickEvent) | 257 OnNotificationClickEvent) |
| 252 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) | 258 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) |
| 253 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_GeofencingEvent, OnGeofencingEvent) | 259 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_GeofencingEvent, OnGeofencingEvent) |
| 254 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginConnectEvent, | 260 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginConnectEvent, |
| 255 OnCrossOriginConnectEvent) | 261 OnCrossOriginConnectEvent) |
| 256 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToWorker, OnPostMessage) | 262 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_MessageToWorker, OnPostMessage) |
| 257 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginMessageToWorker, | 263 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_CrossOriginMessageToWorker, |
| 258 OnCrossOriginMessageToWorker) | 264 OnCrossOriginMessageToWorker) |
| 259 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SendStashedMessagePorts, | 265 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SendStashedMessagePorts, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL); | 348 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL); |
| 343 DCHECK(!proxy_); | 349 DCHECK(!proxy_); |
| 344 g_worker_client_tls.Pointer()->Set(this); | 350 g_worker_client_tls.Pointer()->Set(this); |
| 345 proxy_ = proxy; | 351 proxy_ = proxy; |
| 346 | 352 |
| 347 // Initialize pending callback maps. This needs to be freed on the | 353 // Initialize pending callback maps. This needs to be freed on the |
| 348 // same thread before the worker context goes away in | 354 // same thread before the worker context goes away in |
| 349 // willDestroyWorkerContext. | 355 // willDestroyWorkerContext. |
| 350 context_.reset(new WorkerContextData(this)); | 356 context_.reset(new WorkerContextData(this)); |
| 351 | 357 |
| 358 // Register Mojo services. |
| 359 context_->service_registry.ServiceRegistry::AddService( |
| 360 base::Bind(&BackgroundSyncClientImpl::Create)); |
| 361 |
| 352 SetRegistrationInServiceWorkerGlobalScope(); | 362 SetRegistrationInServiceWorkerGlobalScope(); |
| 353 | 363 |
| 354 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded( | 364 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded( |
| 355 embedded_worker_id_, | 365 embedded_worker_id_, |
| 356 WorkerTaskRunner::Instance()->CurrentWorkerId(), | 366 WorkerTaskRunner::Instance()->CurrentWorkerId(), |
| 357 provider_context_->provider_id())); | 367 provider_context_->provider_id())); |
| 358 | 368 |
| 359 TRACE_EVENT_ASYNC_STEP_INTO0( | 369 TRACE_EVENT_ASYNC_STEP_INTO0( |
| 360 "ServiceWorker", | 370 "ServiceWorker", |
| 361 "ServiceWorkerContextClient::StartingWorkerContext", | 371 "ServiceWorkerContextClient::StartingWorkerContext", |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 486 void ServiceWorkerContextClient::didHandlePushEvent( | 496 void ServiceWorkerContextClient::didHandlePushEvent( |
| 487 int request_id, | 497 int request_id, |
| 488 blink::WebServiceWorkerEventResult result) { | 498 blink::WebServiceWorkerEventResult result) { |
| 489 Send(new ServiceWorkerHostMsg_PushEventFinished( | 499 Send(new ServiceWorkerHostMsg_PushEventFinished( |
| 490 GetRoutingID(), request_id, result)); | 500 GetRoutingID(), request_id, result)); |
| 491 } | 501 } |
| 492 | 502 |
| 493 void ServiceWorkerContextClient::didHandleSyncEvent( | 503 void ServiceWorkerContextClient::didHandleSyncEvent( |
| 494 int request_id, | 504 int request_id, |
| 495 blink::WebServiceWorkerEventResult result) { | 505 blink::WebServiceWorkerEventResult result) { |
| 496 Send(new ServiceWorkerHostMsg_SyncEventFinished(GetRoutingID(), request_id, | 506 const SyncCallback* callback = |
| 497 result)); | 507 context_->sync_event_callbacks.Lookup(request_id); |
| 508 if (!callback) |
| 509 return; |
| 510 if (result == blink::WebServiceWorkerEventResultCompleted) { |
| 511 callback->Run(SERVICE_WORKER_EVENT_STATUS_COMPLETED); |
| 512 } else { |
| 513 callback->Run(SERVICE_WORKER_EVENT_STATUS_REJECTED); |
| 514 } |
| 515 context_->sync_event_callbacks.Remove(request_id); |
| 498 } | 516 } |
| 499 | 517 |
| 500 void ServiceWorkerContextClient::didHandleCrossOriginConnectEvent( | 518 void ServiceWorkerContextClient::didHandleCrossOriginConnectEvent( |
| 501 int request_id, | 519 int request_id, |
| 502 bool accept_connection) { | 520 bool accept_connection) { |
| 503 Send(new ServiceWorkerHostMsg_CrossOriginConnectEventFinished( | 521 Send(new ServiceWorkerHostMsg_CrossOriginConnectEventFinished( |
| 504 GetRoutingID(), request_id, accept_connection)); | 522 GetRoutingID(), request_id, accept_connection)); |
| 505 } | 523 } |
| 506 | 524 |
| 507 blink::WebServiceWorkerNetworkProvider* | 525 blink::WebServiceWorkerNetworkProvider* |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 WebMessagePortChannelImpl* channel_impl = | 626 WebMessagePortChannelImpl* channel_impl = |
| 609 static_cast<WebMessagePortChannelImpl*>(channel); | 627 static_cast<WebMessagePortChannelImpl*>(channel); |
| 610 main_thread_task_runner_->PostTask( | 628 main_thread_task_runner_->PostTask( |
| 611 FROM_HERE, | 629 FROM_HERE, |
| 612 base::Bind(&StashMessagePortOnMainThread, | 630 base::Bind(&StashMessagePortOnMainThread, |
| 613 sender_, GetRoutingID(), | 631 sender_, GetRoutingID(), |
| 614 base::Unretained(channel_impl), | 632 base::Unretained(channel_impl), |
| 615 static_cast<base::string16>(name))); | 633 static_cast<base::string16>(name))); |
| 616 } | 634 } |
| 617 | 635 |
| 636 void ServiceWorkerContextClient::DispatchSyncEvent( |
| 637 const SyncCallback& callback) { |
| 638 TRACE_EVENT0("ServiceWorker", |
| 639 "ServiceWorkerScriptContext::DispatchSyncEvent"); |
| 640 int request_id = |
| 641 context_->sync_event_callbacks.Add(new SyncCallback(callback)); |
| 642 proxy_->dispatchSyncEvent(request_id); |
| 643 } |
| 644 |
| 618 void ServiceWorkerContextClient::Send(IPC::Message* message) { | 645 void ServiceWorkerContextClient::Send(IPC::Message* message) { |
| 619 sender_->Send(message); | 646 sender_->Send(message); |
| 620 } | 647 } |
| 621 | 648 |
| 622 void ServiceWorkerContextClient::SendWorkerStarted() { | 649 void ServiceWorkerContextClient::SendWorkerStarted() { |
| 623 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 650 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
| 624 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 651 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 625 "ServiceWorkerContextClient::StartingWorkerContext", | 652 "ServiceWorkerContextClient::StartingWorkerContext", |
| 626 this); | 653 this); |
| 627 Send(new EmbeddedWorkerHostMsg_WorkerStarted(embedded_worker_id_)); | 654 Send(new EmbeddedWorkerHostMsg_WorkerStarted(embedded_worker_id_)); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 webRequest.setMode(GetBlinkFetchRequestMode(request.mode)); | 718 webRequest.setMode(GetBlinkFetchRequestMode(request.mode)); |
| 692 webRequest.setCredentialsMode( | 719 webRequest.setCredentialsMode( |
| 693 GetBlinkFetchCredentialsMode(request.credentials_mode)); | 720 GetBlinkFetchCredentialsMode(request.credentials_mode)); |
| 694 webRequest.setRequestContext( | 721 webRequest.setRequestContext( |
| 695 GetBlinkRequestContext(request.request_context_type)); | 722 GetBlinkRequestContext(request.request_context_type)); |
| 696 webRequest.setFrameType(GetBlinkFrameType(request.frame_type)); | 723 webRequest.setFrameType(GetBlinkFrameType(request.frame_type)); |
| 697 webRequest.setIsReload(request.is_reload); | 724 webRequest.setIsReload(request.is_reload); |
| 698 proxy_->dispatchFetchEvent(request_id, webRequest); | 725 proxy_->dispatchFetchEvent(request_id, webRequest); |
| 699 } | 726 } |
| 700 | 727 |
| 701 void ServiceWorkerContextClient::OnSyncEvent(int request_id) { | |
| 702 TRACE_EVENT0("ServiceWorker", | |
| 703 "ServiceWorkerContextClient::OnSyncEvent"); | |
| 704 proxy_->dispatchSyncEvent(request_id); | |
| 705 } | |
| 706 | |
| 707 void ServiceWorkerContextClient::OnNotificationClickEvent( | 728 void ServiceWorkerContextClient::OnNotificationClickEvent( |
| 708 int request_id, | 729 int request_id, |
| 709 int64_t persistent_notification_id, | 730 int64_t persistent_notification_id, |
| 710 const PlatformNotificationData& notification_data) { | 731 const PlatformNotificationData& notification_data) { |
| 711 TRACE_EVENT0("ServiceWorker", | 732 TRACE_EVENT0("ServiceWorker", |
| 712 "ServiceWorkerContextClient::OnNotificationClickEvent"); | 733 "ServiceWorkerContextClient::OnNotificationClickEvent"); |
| 713 proxy_->dispatchNotificationClickEvent( | 734 proxy_->dispatchNotificationClickEvent( |
| 714 request_id, | 735 request_id, |
| 715 persistent_notification_id, | 736 persistent_notification_id, |
| 716 ToWebNotificationData(notification_data)); | 737 ToWebNotificationData(notification_data)); |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 938 } | 959 } |
| 939 | 960 |
| 940 base::WeakPtr<ServiceWorkerContextClient> | 961 base::WeakPtr<ServiceWorkerContextClient> |
| 941 ServiceWorkerContextClient::GetWeakPtr() { | 962 ServiceWorkerContextClient::GetWeakPtr() { |
| 942 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 963 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
| 943 DCHECK(context_); | 964 DCHECK(context_); |
| 944 return context_->weak_factory.GetWeakPtr(); | 965 return context_->weak_factory.GetWeakPtr(); |
| 945 } | 966 } |
| 946 | 967 |
| 947 } // namespace content | 968 } // namespace content |
| OLD | NEW |