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 SetRegistrationInServiceWorkerGlobalScope(); | |
185 | |
179 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded( | 186 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoaded( |
180 embedded_worker_id_, | 187 embedded_worker_id_, |
181 WorkerTaskRunner::Instance()->CurrentWorkerId())); | 188 WorkerTaskRunner::Instance()->CurrentWorkerId())); |
182 | 189 |
183 // Schedule a task to send back WorkerStarted asynchronously, | 190 // Schedule a task to send back WorkerStarted asynchronously, |
184 // so that at the time we send it we can be sure that the worker | 191 // 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. | 192 // script has been evaluated and worker run loop has been started. |
186 worker_task_runner_->PostTask( | 193 worker_task_runner_->PostTask( |
187 FROM_HERE, | 194 FROM_HERE, |
188 base::Bind(&EmbeddedWorkerContextClient::SendWorkerStarted, | 195 base::Bind(&EmbeddedWorkerContextClient::SendWorkerStarted, |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
341 int request_id, | 348 int request_id, |
342 bool accept_connection) { | 349 bool accept_connection) { |
343 DCHECK(script_context_); | 350 DCHECK(script_context_); |
344 script_context_->DidHandleCrossOriginConnectEvent(request_id, | 351 script_context_->DidHandleCrossOriginConnectEvent(request_id, |
345 accept_connection); | 352 accept_connection); |
346 } | 353 } |
347 | 354 |
348 blink::WebServiceWorkerNetworkProvider* | 355 blink::WebServiceWorkerNetworkProvider* |
349 EmbeddedWorkerContextClient::createServiceWorkerNetworkProvider( | 356 EmbeddedWorkerContextClient::createServiceWorkerNetworkProvider( |
350 blink::WebDataSource* data_source) { | 357 blink::WebDataSource* data_source) { |
358 DCHECK(main_thread_proxy_->RunsTasksOnCurrentThread()); | |
359 | |
351 // Create a content::ServiceWorkerNetworkProvider for this data source so | 360 // Create a content::ServiceWorkerNetworkProvider for this data source so |
352 // we can observe its requests. | 361 // we can observe its requests. |
353 scoped_ptr<ServiceWorkerNetworkProvider> provider( | 362 scoped_ptr<ServiceWorkerNetworkProvider> provider( |
354 new ServiceWorkerNetworkProvider(MSG_ROUTING_NONE)); | 363 new ServiceWorkerNetworkProvider(MSG_ROUTING_NONE)); |
364 provider_context_ = provider->context(); | |
355 | 365 |
356 // Tell the network provider about which version to load. | 366 // Tell the network provider about which version to load. |
357 provider->SetServiceWorkerVersionId(service_worker_version_id_); | 367 provider->SetServiceWorkerVersionId(service_worker_version_id_); |
358 | 368 |
359 // The provider is kept around for the lifetime of the DataSource | 369 // The provider is kept around for the lifetime of the DataSource |
360 // and ownership is transferred to the DataSource. | 370 // and ownership is transferred to the DataSource. |
361 DataSourceExtraData* extra_data = new DataSourceExtraData(); | 371 DataSourceExtraData* extra_data = new DataSourceExtraData(); |
362 data_source->setExtraData(extra_data); | 372 data_source->setExtraData(extra_data); |
363 ServiceWorkerNetworkProvider::AttachToDocumentState( | 373 ServiceWorkerNetworkProvider::AttachToDocumentState( |
364 extra_data, provider.Pass()); | 374 extra_data, provider.Pass()); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
408 } | 418 } |
409 | 419 |
410 void EmbeddedWorkerContextClient::SendWorkerStarted() { | 420 void EmbeddedWorkerContextClient::SendWorkerStarted() { |
411 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 421 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
412 TRACE_EVENT_ASYNC_END0("ServiceWorker", | 422 TRACE_EVENT_ASYNC_END0("ServiceWorker", |
413 "EmbeddedWorkerContextClient::StartingWorkerContext", | 423 "EmbeddedWorkerContextClient::StartingWorkerContext", |
414 this); | 424 this); |
415 Send(new EmbeddedWorkerHostMsg_WorkerStarted(embedded_worker_id_)); | 425 Send(new EmbeddedWorkerHostMsg_WorkerStarted(embedded_worker_id_)); |
416 } | 426 } |
417 | 427 |
428 void EmbeddedWorkerContextClient::SetRegistrationInServiceWorkerGlobalScope() { | |
429 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | |
430 DCHECK(provider_context_); | |
431 DCHECK(script_context_); | |
432 | |
433 ServiceWorkerDispatcher* dispatcher = | |
434 ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance( | |
435 thread_safe_sender()); | |
436 | |
437 // Register a registration with the dispatcher living on the worker thread. | |
438 scoped_ptr<WebServiceWorkerRegistrationImpl> registration( | |
439 dispatcher->CreateServiceWorkerRegistration( | |
440 provider_context_->registration()->info(), false)); | |
441 | |
442 // Register workers with the dispatcher living on the worker thread. | |
443 ServiceWorkerVersionAttributes attrs = | |
444 provider_context_->GetVersionAttributes(); | |
kinuko
2015/01/22 08:05:44
It feels it's a bit odd to call two locked methods
nhiroki
2015/01/22 09:09:16
Agree. I'll make a followup patch (probably we can
| |
445 registration->SetInstalling( | |
446 dispatcher->GetServiceWorker(attrs.installing, false)); | |
447 registration->SetWaiting( | |
448 dispatcher->GetServiceWorker(attrs.waiting, false)); | |
449 registration->SetActive( | |
450 dispatcher->GetServiceWorker(attrs.active, false)); | |
451 | |
452 script_context_->SetRegistrationInServiceWorkerGlobalScope( | |
453 registration.Pass()); | |
454 } | |
455 | |
418 } // namespace content | 456 } // namespace content |
OLD | NEW |