| 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/renderer/service_worker/embedded_worker_context_client.h" | 5 #include "content/renderer/service_worker/embedded_worker_context_client.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/debug/trace_event.h" | 10 #include "base/debug/trace_event.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/message_loop/message_loop_proxy.h" | 12 #include "base/message_loop/message_loop_proxy.h" |
| 13 #include "base/pickle.h" | 13 #include "base/pickle.h" |
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/threading/thread_local.h" | 16 #include "base/threading/thread_local.h" |
| 17 #include "content/child/request_extra_data.h" | 17 #include "content/child/request_extra_data.h" |
| 18 #include "content/child/service_worker/service_worker_dispatcher.h" |
| 18 #include "content/child/service_worker/service_worker_network_provider.h" | 19 #include "content/child/service_worker/service_worker_network_provider.h" |
| 20 #include "content/child/service_worker/service_worker_provider_context.h" |
| 21 #include "content/child/service_worker/service_worker_registration_handle_refere
nce.h" |
| 22 #include "content/child/service_worker/web_service_worker_impl.h" |
| 23 #include "content/child/service_worker/web_service_worker_registration_impl.h" |
| 19 #include "content/child/thread_safe_sender.h" | 24 #include "content/child/thread_safe_sender.h" |
| 20 #include "content/child/worker_task_runner.h" | 25 #include "content/child/worker_task_runner.h" |
| 21 #include "content/child/worker_thread_task_runner.h" | 26 #include "content/child/worker_thread_task_runner.h" |
| 22 #include "content/common/devtools_messages.h" | 27 #include "content/common/devtools_messages.h" |
| 23 #include "content/common/service_worker/embedded_worker_messages.h" | 28 #include "content/common/service_worker/embedded_worker_messages.h" |
| 24 #include "content/common/service_worker/service_worker_types.h" | 29 #include "content/common/service_worker/service_worker_types.h" |
| 25 #include "content/public/renderer/document_state.h" | 30 #include "content/public/renderer/document_state.h" |
| 26 #include "content/renderer/render_thread_impl.h" | 31 #include "content/renderer/render_thread_impl.h" |
| 27 #include "content/renderer/service_worker/embedded_worker_dispatcher.h" | 32 #include "content/renderer/service_worker/embedded_worker_dispatcher.h" |
| 28 #include "content/renderer/service_worker/service_worker_script_context.h" | 33 #include "content/renderer/service_worker/service_worker_script_context.h" |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 worker_task_runner_ = new WorkerThreadTaskRunner( | 174 worker_task_runner_ = new WorkerThreadTaskRunner( |
| 170 WorkerTaskRunner::Instance()->CurrentWorkerId()); | 175 WorkerTaskRunner::Instance()->CurrentWorkerId()); |
| 171 DCHECK_NE(0, WorkerTaskRunner::Instance()->CurrentWorkerId()); | 176 DCHECK_NE(0, WorkerTaskRunner::Instance()->CurrentWorkerId()); |
| 172 // g_worker_client_tls.Pointer()->Get() could return NULL if this context | 177 // g_worker_client_tls.Pointer()->Get() could return NULL if this context |
| 173 // gets deleted before workerContextStarted() is called. | 178 // gets deleted before workerContextStarted() is called. |
| 174 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL); | 179 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL); |
| 175 DCHECK(!script_context_); | 180 DCHECK(!script_context_); |
| 176 g_worker_client_tls.Pointer()->Set(this); | 181 g_worker_client_tls.Pointer()->Set(this); |
| 177 script_context_.reset(new ServiceWorkerScriptContext(this, proxy)); | 182 script_context_.reset(new ServiceWorkerScriptContext(this, proxy)); |
| 178 | 183 |
| 184 // This can be nullptr on EmbeddedWorkerBrowserTests. |
| 185 // TODO(nhiroki): Remove this workaround. |registration()| should always be |
| 186 // valid other than the test because the registration association message |
| 187 // arrives before starting the worker context. |
| 188 if (provider_context_->registration()) |
| 189 SetRegistrationInServiceWorkerGlobalScope(); |
| 190 |
| 179 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded( | 191 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded( |
| 180 embedded_worker_id_, | 192 embedded_worker_id_, |
| 181 WorkerTaskRunner::Instance()->CurrentWorkerId())); | 193 WorkerTaskRunner::Instance()->CurrentWorkerId())); |
| 182 | 194 |
| 183 // Schedule a task to send back WorkerStarted asynchronously, | 195 // Schedule a task to send back WorkerStarted asynchronously, |
| 184 // so that at the time we send it we can be sure that the worker | 196 // so that at the time we send it we can be sure that the worker |
| 185 // script has been evaluated and worker run loop has been started. | 197 // script has been evaluated and worker run loop has been started. |
| 186 worker_task_runner_->PostTask( | 198 worker_task_runner_->PostTask( |
| 187 FROM_HERE, | 199 FROM_HERE, |
| 188 base::Bind(&EmbeddedWorkerContextClient::SendWorkerStarted, | 200 base::Bind(&EmbeddedWorkerContextClient::SendWorkerStarted, |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 341 int request_id, | 353 int request_id, |
| 342 bool accept_connection) { | 354 bool accept_connection) { |
| 343 DCHECK(script_context_); | 355 DCHECK(script_context_); |
| 344 script_context_->DidHandleCrossOriginConnectEvent(request_id, | 356 script_context_->DidHandleCrossOriginConnectEvent(request_id, |
| 345 accept_connection); | 357 accept_connection); |
| 346 } | 358 } |
| 347 | 359 |
| 348 blink::WebServiceWorkerNetworkProvider* | 360 blink::WebServiceWorkerNetworkProvider* |
| 349 EmbeddedWorkerContextClient::createServiceWorkerNetworkProvider( | 361 EmbeddedWorkerContextClient::createServiceWorkerNetworkProvider( |
| 350 blink::WebDataSource* data_source) { | 362 blink::WebDataSource* data_source) { |
| 363 DCHECK(main_thread_proxy_->RunsTasksOnCurrentThread()); |
| 364 |
| 351 // Create a content::ServiceWorkerNetworkProvider for this data source so | 365 // Create a content::ServiceWorkerNetworkProvider for this data source so |
| 352 // we can observe its requests. | 366 // we can observe its requests. |
| 353 scoped_ptr<ServiceWorkerNetworkProvider> provider( | 367 scoped_ptr<ServiceWorkerNetworkProvider> provider( |
| 354 new ServiceWorkerNetworkProvider(MSG_ROUTING_NONE)); | 368 new ServiceWorkerNetworkProvider(MSG_ROUTING_NONE)); |
| 369 provider_context_ = provider->context(); |
| 355 | 370 |
| 356 // Tell the network provider about which version to load. | 371 // Tell the network provider about which version to load. |
| 357 provider->SetServiceWorkerVersionId(service_worker_version_id_); | 372 provider->SetServiceWorkerVersionId(service_worker_version_id_); |
| 358 | 373 |
| 359 // The provider is kept around for the lifetime of the DataSource | 374 // The provider is kept around for the lifetime of the DataSource |
| 360 // and ownership is transferred to the DataSource. | 375 // and ownership is transferred to the DataSource. |
| 361 DataSourceExtraData* extra_data = new DataSourceExtraData(); | 376 DataSourceExtraData* extra_data = new DataSourceExtraData(); |
| 362 data_source->setExtraData(extra_data); | 377 data_source->setExtraData(extra_data); |
| 363 ServiceWorkerNetworkProvider::AttachToDocumentState( | 378 ServiceWorkerNetworkProvider::AttachToDocumentState( |
| 364 extra_data, provider.Pass()); | 379 extra_data, provider.Pass()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 } | 423 } |
| 409 | 424 |
| 410 void EmbeddedWorkerContextClient::SendWorkerStarted() { | 425 void EmbeddedWorkerContextClient::SendWorkerStarted() { |
| 411 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 426 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
| 412 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 427 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
| 413 "EmbeddedWorkerContextClient::StartingWorkerContext", | 428 "EmbeddedWorkerContextClient::StartingWorkerContext", |
| 414 this); | 429 this); |
| 415 Send(new EmbeddedWorkerHostMsg_WorkerStarted(embedded_worker_id_)); | 430 Send(new EmbeddedWorkerHostMsg_WorkerStarted(embedded_worker_id_)); |
| 416 } | 431 } |
| 417 | 432 |
| 433 void EmbeddedWorkerContextClient::SetRegistrationInServiceWorkerGlobalScope() { |
| 434 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
| 435 DCHECK(provider_context_); |
| 436 DCHECK(script_context_); |
| 437 |
| 438 ServiceWorkerDispatcher* dispatcher = |
| 439 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( |
| 440 thread_safe_sender()); |
| 441 |
| 442 // Register a registration with the dispatcher living on the worker thread. |
| 443 DCHECK(provider_context_->registration()); |
| 444 scoped_ptr<WebServiceWorkerRegistrationImpl> registration( |
| 445 dispatcher->CreateServiceWorkerRegistration( |
| 446 provider_context_->registration()->info(), false)); |
| 447 |
| 448 // Register workers with the dispatcher living on the worker thread. |
| 449 ServiceWorkerVersionAttributes attrs = |
| 450 provider_context_->GetVersionAttributes(); |
| 451 registration->SetInstalling( |
| 452 dispatcher->GetServiceWorker(attrs.installing, false)); |
| 453 registration->SetWaiting( |
| 454 dispatcher->GetServiceWorker(attrs.waiting, false)); |
| 455 registration->SetActive( |
| 456 dispatcher->GetServiceWorker(attrs.active, false)); |
| 457 |
| 458 script_context_->SetRegistrationInServiceWorkerGlobalScope( |
| 459 registration.Pass()); |
| 460 } |
| 461 |
| 418 } // namespace content | 462 } // namespace content |
| OLD | NEW |