| 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 14 matching lines...) Expand all Loading... |
| 25 #include "content/child/thread_safe_sender.h" | 25 #include "content/child/thread_safe_sender.h" |
| 26 #include "content/child/webmessageportchannel_impl.h" | 26 #include "content/child/webmessageportchannel_impl.h" |
| 27 #include "content/child/worker_task_runner.h" | 27 #include "content/child/worker_task_runner.h" |
| 28 #include "content/common/devtools_messages.h" | 28 #include "content/common/devtools_messages.h" |
| 29 #include "content/common/message_port_messages.h" | 29 #include "content/common/message_port_messages.h" |
| 30 #include "content/common/mojo/service_registry_impl.h" | 30 #include "content/common/mojo/service_registry_impl.h" |
| 31 #include "content/common/service_worker/embedded_worker_messages.h" | 31 #include "content/common/service_worker/embedded_worker_messages.h" |
| 32 #include "content/common/service_worker/service_worker_messages.h" | 32 #include "content/common/service_worker/service_worker_messages.h" |
| 33 #include "content/public/common/referrer.h" | 33 #include "content/public/common/referrer.h" |
| 34 #include "content/public/renderer/document_state.h" | 34 #include "content/public/renderer/document_state.h" |
| 35 #include "content/renderer/background_sync/background_sync_client_impl.h" |
| 35 #include "content/renderer/devtools/devtools_agent.h" | 36 #include "content/renderer/devtools/devtools_agent.h" |
| 36 #include "content/renderer/render_thread_impl.h" | 37 #include "content/renderer/render_thread_impl.h" |
| 37 #include "content/renderer/service_worker/embedded_worker_dispatcher.h" | 38 #include "content/renderer/service_worker/embedded_worker_dispatcher.h" |
| 38 #include "content/renderer/service_worker/service_worker_type_util.h" | 39 #include "content/renderer/service_worker/service_worker_type_util.h" |
| 39 #include "ipc/ipc_message.h" | 40 #include "ipc/ipc_message.h" |
| 40 #include "ipc/ipc_message_macros.h" | 41 #include "ipc/ipc_message_macros.h" |
| 41 #include "third_party/WebKit/public/platform/WebCrossOriginServiceWorkerClient.h
" | 42 #include "third_party/WebKit/public/platform/WebCrossOriginServiceWorkerClient.h
" |
| 42 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h" | 43 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h" |
| 43 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" | 44 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" |
| 44 #include "third_party/WebKit/public/platform/WebServiceWorkerClientQueryOptions.
h" | 45 #include "third_party/WebKit/public/platform/WebServiceWorkerClientQueryOptions.
h" |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 // worker thread. | 174 // worker thread. |
| 174 struct ServiceWorkerContextClient::WorkerContextData { | 175 struct ServiceWorkerContextClient::WorkerContextData { |
| 175 using ClientsCallbacksMap = | 176 using ClientsCallbacksMap = |
| 176 IDMap<blink::WebServiceWorkerClientsCallbacks, IDMapOwnPointer>; | 177 IDMap<blink::WebServiceWorkerClientsCallbacks, IDMapOwnPointer>; |
| 177 using ClaimClientsCallbacksMap = | 178 using ClaimClientsCallbacksMap = |
| 178 IDMap<blink::WebServiceWorkerClientsClaimCallbacks, IDMapOwnPointer>; | 179 IDMap<blink::WebServiceWorkerClientsClaimCallbacks, IDMapOwnPointer>; |
| 179 using ClientCallbacksMap = | 180 using ClientCallbacksMap = |
| 180 IDMap<blink::WebServiceWorkerClientCallbacks, IDMapOwnPointer>; | 181 IDMap<blink::WebServiceWorkerClientCallbacks, IDMapOwnPointer>; |
| 181 using SkipWaitingCallbacksMap = | 182 using SkipWaitingCallbacksMap = |
| 182 IDMap<blink::WebServiceWorkerSkipWaitingCallbacks, IDMapOwnPointer>; | 183 IDMap<blink::WebServiceWorkerSkipWaitingCallbacks, IDMapOwnPointer>; |
| 184 using SyncEventCallbacksMap = |
| 185 IDMap<const mojo::Callback<void(ServiceWorkerEventStatus)>, |
| 186 IDMapOwnPointer>; |
| 183 | 187 |
| 184 explicit WorkerContextData(ServiceWorkerContextClient* owner) | 188 explicit WorkerContextData(ServiceWorkerContextClient* owner) |
| 185 : weak_factory(owner), proxy_weak_factory(owner->proxy_) {} | 189 : weak_factory(owner), proxy_weak_factory(owner->proxy_) {} |
| 186 | 190 |
| 187 ~WorkerContextData() { | 191 ~WorkerContextData() { |
| 188 DCHECK(thread_checker.CalledOnValidThread()); | 192 DCHECK(thread_checker.CalledOnValidThread()); |
| 189 } | 193 } |
| 190 | 194 |
| 191 // Pending callbacks for GetClientDocuments(). | 195 // Pending callbacks for GetClientDocuments(). |
| 192 ClientsCallbacksMap clients_callbacks; | 196 ClientsCallbacksMap clients_callbacks; |
| 193 | 197 |
| 194 // Pending callbacks for OpenWindow() and FocusClient(). | 198 // Pending callbacks for OpenWindow() and FocusClient(). |
| 195 ClientCallbacksMap client_callbacks; | 199 ClientCallbacksMap client_callbacks; |
| 196 | 200 |
| 197 // Pending callbacks for SkipWaiting(). | 201 // Pending callbacks for SkipWaiting(). |
| 198 SkipWaitingCallbacksMap skip_waiting_callbacks; | 202 SkipWaitingCallbacksMap skip_waiting_callbacks; |
| 199 | 203 |
| 200 // Pending callbacks for ClaimClients(). | 204 // Pending callbacks for ClaimClients(). |
| 201 ClaimClientsCallbacksMap claim_clients_callbacks; | 205 ClaimClientsCallbacksMap claim_clients_callbacks; |
| 202 | 206 |
| 207 // Pending callbacks for Background Sync Events |
| 208 SyncEventCallbacksMap sync_event_callbacks; |
| 209 |
| 203 ServiceRegistryImpl service_registry; | 210 ServiceRegistryImpl service_registry; |
| 204 | 211 |
| 205 base::ThreadChecker thread_checker; | 212 base::ThreadChecker thread_checker; |
| 206 base::WeakPtrFactory<ServiceWorkerContextClient> weak_factory; | 213 base::WeakPtrFactory<ServiceWorkerContextClient> weak_factory; |
| 207 base::WeakPtrFactory<blink::WebServiceWorkerContextProxy> proxy_weak_factory; | 214 base::WeakPtrFactory<blink::WebServiceWorkerContextProxy> proxy_weak_factory; |
| 208 }; | 215 }; |
| 209 | 216 |
| 210 ServiceWorkerContextClient* | 217 ServiceWorkerContextClient* |
| 211 ServiceWorkerContextClient::ThreadSpecificInstance() { | 218 ServiceWorkerContextClient::ThreadSpecificInstance() { |
| 212 return g_worker_client_tls.Pointer()->Get(); | 219 return g_worker_client_tls.Pointer()->Get(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 241 void ServiceWorkerContextClient::OnMessageReceived( | 248 void ServiceWorkerContextClient::OnMessageReceived( |
| 242 int thread_id, | 249 int thread_id, |
| 243 int embedded_worker_id, | 250 int embedded_worker_id, |
| 244 const IPC::Message& message) { | 251 const IPC::Message& message) { |
| 245 CHECK_EQ(embedded_worker_id_, embedded_worker_id); | 252 CHECK_EQ(embedded_worker_id_, embedded_worker_id); |
| 246 bool handled = true; | 253 bool handled = true; |
| 247 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) | 254 IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) |
| 248 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) | 255 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_ActivateEvent, OnActivateEvent) |
| 249 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent) | 256 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_FetchEvent, OnFetchEvent) |
| 250 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) | 257 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_InstallEvent, OnInstallEvent) |
| 251 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_SyncEvent, OnSyncEvent) | |
| 252 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent, | 258 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_NotificationClickEvent, |
| 253 OnNotificationClickEvent) | 259 OnNotificationClickEvent) |
| 254 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) | 260 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_PushEvent, OnPushEvent) |
| 255 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_GeofencingEvent, OnGeofencingEvent) | 261 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_GeofencingEvent, OnGeofencingEvent) |
| 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, |
| 260 OnSendStashedMessagePorts) | 266 OnSendStashedMessagePorts) |
| 261 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) | 267 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClients, OnDidGetClients) |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |
| 352 // Register Mojo services. | 358 // Register Mojo services. |
| 353 context_->service_registry.ServiceRegistry::AddService( | 359 context_->service_registry.ServiceRegistry::AddService( |
| 354 base::Bind(&ServicePortDispatcherImpl::Create, | 360 base::Bind(&ServicePortDispatcherImpl::Create, |
| 355 context_->proxy_weak_factory.GetWeakPtr())); | 361 context_->proxy_weak_factory.GetWeakPtr())); |
| 362 context_->service_registry.ServiceRegistry::AddService( |
| 363 base::Bind(&BackgroundSyncClientImpl::Create)); |
| 356 | 364 |
| 357 SetRegistrationInServiceWorkerGlobalScope(); | 365 SetRegistrationInServiceWorkerGlobalScope(); |
| 358 | 366 |
| 359 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded( | 367 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded( |
| 360 embedded_worker_id_, | 368 embedded_worker_id_, |
| 361 WorkerTaskRunner::Instance()->CurrentWorkerId(), | 369 WorkerTaskRunner::Instance()->CurrentWorkerId(), |
| 362 provider_context_->provider_id())); | 370 provider_context_->provider_id())); |
| 363 | 371 |
| 364 TRACE_EVENT_ASYNC_STEP_INTO0( | 372 TRACE_EVENT_ASYNC_STEP_INTO0( |
| 365 "ServiceWorker", | 373 "ServiceWorker", |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 void ServiceWorkerContextClient::didHandlePushEvent( | 499 void ServiceWorkerContextClient::didHandlePushEvent( |
| 492 int request_id, | 500 int request_id, |
| 493 blink::WebServiceWorkerEventResult result) { | 501 blink::WebServiceWorkerEventResult result) { |
| 494 Send(new ServiceWorkerHostMsg_PushEventFinished( | 502 Send(new ServiceWorkerHostMsg_PushEventFinished( |
| 495 GetRoutingID(), request_id, result)); | 503 GetRoutingID(), request_id, result)); |
| 496 } | 504 } |
| 497 | 505 |
| 498 void ServiceWorkerContextClient::didHandleSyncEvent( | 506 void ServiceWorkerContextClient::didHandleSyncEvent( |
| 499 int request_id, | 507 int request_id, |
| 500 blink::WebServiceWorkerEventResult result) { | 508 blink::WebServiceWorkerEventResult result) { |
| 501 Send(new ServiceWorkerHostMsg_SyncEventFinished(GetRoutingID(), request_id, | 509 const SyncCallback* callback = |
| 502 result)); | 510 context_->sync_event_callbacks.Lookup(request_id); |
| 511 if (!callback) |
| 512 return; |
| 513 if (result == blink::WebServiceWorkerEventResultCompleted) { |
| 514 callback->Run(SERVICE_WORKER_EVENT_STATUS_COMPLETED); |
| 515 } else { |
| 516 callback->Run(SERVICE_WORKER_EVENT_STATUS_REJECTED); |
| 517 } |
| 518 context_->sync_event_callbacks.Remove(request_id); |
| 503 } | 519 } |
| 504 | 520 |
| 505 blink::WebServiceWorkerNetworkProvider* | 521 blink::WebServiceWorkerNetworkProvider* |
| 506 ServiceWorkerContextClient::createServiceWorkerNetworkProvider( | 522 ServiceWorkerContextClient::createServiceWorkerNetworkProvider( |
| 507 blink::WebDataSource* data_source) { | 523 blink::WebDataSource* data_source) { |
| 508 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); | 524 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
| 509 | 525 |
| 510 // Create a content::ServiceWorkerNetworkProvider for this data source so | 526 // Create a content::ServiceWorkerNetworkProvider for this data source so |
| 511 // we can observe its requests. | 527 // we can observe its requests. |
| 512 scoped_ptr<ServiceWorkerNetworkProvider> provider( | 528 scoped_ptr<ServiceWorkerNetworkProvider> provider( |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 WebMessagePortChannelImpl* channel_impl = | 622 WebMessagePortChannelImpl* channel_impl = |
| 607 static_cast<WebMessagePortChannelImpl*>(channel); | 623 static_cast<WebMessagePortChannelImpl*>(channel); |
| 608 main_thread_task_runner_->PostTask( | 624 main_thread_task_runner_->PostTask( |
| 609 FROM_HERE, | 625 FROM_HERE, |
| 610 base::Bind(&StashMessagePortOnMainThread, | 626 base::Bind(&StashMessagePortOnMainThread, |
| 611 sender_, GetRoutingID(), | 627 sender_, GetRoutingID(), |
| 612 base::Unretained(channel_impl), | 628 base::Unretained(channel_impl), |
| 613 static_cast<base::string16>(name))); | 629 static_cast<base::string16>(name))); |
| 614 } | 630 } |
| 615 | 631 |
| 632 void ServiceWorkerContextClient::DispatchSyncEvent( |
| 633 const SyncCallback& callback) { |
| 634 TRACE_EVENT0("ServiceWorker", |
| 635 "ServiceWorkerScriptContext::DispatchSyncEvent"); |
| 636 int request_id = |
| 637 context_->sync_event_callbacks.Add(new SyncCallback(callback)); |
| 638 proxy_->dispatchSyncEvent(request_id); |
| 639 } |
| 640 |
| 616 void ServiceWorkerContextClient::Send(IPC::Message* message) { | 641 void ServiceWorkerContextClient::Send(IPC::Message* message) { |
| 617 sender_->Send(message); | 642 sender_->Send(message); |
| 618 } | 643 } |
| 619 | 644 |
| 620 void ServiceWorkerContextClient::SendWorkerStarted() { | 645 void ServiceWorkerContextClient::SendWorkerStarted() { |
| 621 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 646 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
| 622 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 647 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 623 "ServiceWorkerContextClient::StartingWorkerContext", | 648 "ServiceWorkerContextClient::StartingWorkerContext", |
| 624 this); | 649 this); |
| 625 Send(new EmbeddedWorkerHostMsg_WorkerStarted(embedded_worker_id_)); | 650 Send(new EmbeddedWorkerHostMsg_WorkerStarted(embedded_worker_id_)); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 689 webRequest.setMode(GetBlinkFetchRequestMode(request.mode)); | 714 webRequest.setMode(GetBlinkFetchRequestMode(request.mode)); |
| 690 webRequest.setCredentialsMode( | 715 webRequest.setCredentialsMode( |
| 691 GetBlinkFetchCredentialsMode(request.credentials_mode)); | 716 GetBlinkFetchCredentialsMode(request.credentials_mode)); |
| 692 webRequest.setRequestContext( | 717 webRequest.setRequestContext( |
| 693 GetBlinkRequestContext(request.request_context_type)); | 718 GetBlinkRequestContext(request.request_context_type)); |
| 694 webRequest.setFrameType(GetBlinkFrameType(request.frame_type)); | 719 webRequest.setFrameType(GetBlinkFrameType(request.frame_type)); |
| 695 webRequest.setIsReload(request.is_reload); | 720 webRequest.setIsReload(request.is_reload); |
| 696 proxy_->dispatchFetchEvent(request_id, webRequest); | 721 proxy_->dispatchFetchEvent(request_id, webRequest); |
| 697 } | 722 } |
| 698 | 723 |
| 699 void ServiceWorkerContextClient::OnSyncEvent(int request_id) { | |
| 700 TRACE_EVENT0("ServiceWorker", | |
| 701 "ServiceWorkerContextClient::OnSyncEvent"); | |
| 702 proxy_->dispatchSyncEvent(request_id); | |
| 703 } | |
| 704 | |
| 705 void ServiceWorkerContextClient::OnNotificationClickEvent( | 724 void ServiceWorkerContextClient::OnNotificationClickEvent( |
| 706 int request_id, | 725 int request_id, |
| 707 int64_t persistent_notification_id, | 726 int64_t persistent_notification_id, |
| 708 const PlatformNotificationData& notification_data) { | 727 const PlatformNotificationData& notification_data) { |
| 709 TRACE_EVENT0("ServiceWorker", | 728 TRACE_EVENT0("ServiceWorker", |
| 710 "ServiceWorkerContextClient::OnNotificationClickEvent"); | 729 "ServiceWorkerContextClient::OnNotificationClickEvent"); |
| 711 proxy_->dispatchNotificationClickEvent( | 730 proxy_->dispatchNotificationClickEvent( |
| 712 request_id, | 731 request_id, |
| 713 persistent_notification_id, | 732 persistent_notification_id, |
| 714 ToWebNotificationData(notification_data)); | 733 ToWebNotificationData(notification_data)); |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 924 } | 943 } |
| 925 | 944 |
| 926 base::WeakPtr<ServiceWorkerContextClient> | 945 base::WeakPtr<ServiceWorkerContextClient> |
| 927 ServiceWorkerContextClient::GetWeakPtr() { | 946 ServiceWorkerContextClient::GetWeakPtr() { |
| 928 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 947 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
| 929 DCHECK(context_); | 948 DCHECK(context_); |
| 930 return context_->weak_factory.GetWeakPtr(); | 949 return context_->weak_factory.GetWeakPtr(); |
| 931 } | 950 } |
| 932 | 951 |
| 933 } // namespace content | 952 } // namespace content |
| OLD | NEW |