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/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/service_worker/embedded_worker_messages.h" | 29 #include "content/common/service_worker/embedded_worker_messages.h" |
30 #include "content/common/service_worker/service_worker_messages.h" | 30 #include "content/common/service_worker/service_worker_messages.h" |
31 #include "content/public/common/referrer.h" | 31 #include "content/public/common/referrer.h" |
32 #include "content/public/renderer/document_state.h" | 32 #include "content/public/renderer/document_state.h" |
33 #include "content/renderer/devtools/devtools_agent.h" | 33 #include "content/renderer/devtools/devtools_agent.h" |
34 #include "content/renderer/render_thread_impl.h" | 34 #include "content/renderer/render_thread_impl.h" |
35 #include "content/renderer/service_worker/embedded_worker_dispatcher.h" | 35 #include "content/renderer/service_worker/embedded_worker_impl.h" |
36 #include "content/renderer/service_worker/service_worker_type_util.h" | 36 #include "content/renderer/service_worker/service_worker_type_util.h" |
37 #include "ipc/ipc_message.h" | 37 #include "ipc/ipc_message.h" |
38 #include "ipc/ipc_message_macros.h" | 38 #include "ipc/ipc_message_macros.h" |
39 #include "third_party/WebKit/public/platform/WebCrossOriginServiceWorkerClient.h
" | 39 #include "third_party/WebKit/public/platform/WebCrossOriginServiceWorkerClient.h
" |
40 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h" | 40 #include "third_party/WebKit/public/platform/WebMessagePortChannel.h" |
41 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" | 41 #include "third_party/WebKit/public/platform/WebReferrerPolicy.h" |
42 #include "third_party/WebKit/public/platform/WebServiceWorkerClientQueryOptions.
h" | 42 #include "third_party/WebKit/public/platform/WebServiceWorkerClientQueryOptions.
h" |
43 #include "third_party/WebKit/public/platform/WebServiceWorkerRequest.h" | 43 #include "third_party/WebKit/public/platform/WebServiceWorkerRequest.h" |
44 #include "third_party/WebKit/public/platform/WebServiceWorkerResponse.h" | 44 #include "third_party/WebKit/public/platform/WebServiceWorkerResponse.h" |
45 #include "third_party/WebKit/public/platform/WebString.h" | 45 #include "third_party/WebKit/public/platform/WebString.h" |
46 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onData.h" | 46 #include "third_party/WebKit/public/platform/modules/notifications/WebNotificati
onData.h" |
47 #include "third_party/WebKit/public/web/WebDataSource.h" | 47 #include "third_party/WebKit/public/web/WebDataSource.h" |
48 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h" | 48 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h" |
49 #include "third_party/WebKit/public/web/WebServiceWorkerContextProxy.h" | 49 #include "third_party/WebKit/public/web/WebServiceWorkerContextProxy.h" |
50 #include "third_party/WebKit/public/web/WebServiceWorkerNetworkProvider.h" | 50 #include "third_party/WebKit/public/web/WebServiceWorkerNetworkProvider.h" |
51 | 51 |
52 namespace content { | 52 namespace content { |
53 | 53 |
54 namespace { | 54 namespace { |
55 | 55 |
56 // For now client must be a per-thread instance. | 56 // For now client must be a per-thread instance. |
57 base::LazyInstance<base::ThreadLocalPointer<ServiceWorkerContextClient>>:: | 57 base::LazyInstance<base::ThreadLocalPointer<ServiceWorkerContextClient>>:: |
58 Leaky g_worker_client_tls = LAZY_INSTANCE_INITIALIZER; | 58 Leaky g_worker_client_tls = LAZY_INSTANCE_INITIALIZER; |
59 | 59 |
60 void CallWorkerContextDestroyedOnMainThread(int embedded_worker_id) { | |
61 if (!RenderThreadImpl::current() || | |
62 !RenderThreadImpl::current()->embedded_worker_dispatcher()) | |
63 return; | |
64 RenderThreadImpl::current()->embedded_worker_dispatcher()-> | |
65 WorkerContextDestroyed(embedded_worker_id); | |
66 } | |
67 | |
68 // We store an instance of this class in the "extra data" of the WebDataSource | 60 // We store an instance of this class in the "extra data" of the WebDataSource |
69 // and attach a ServiceWorkerNetworkProvider to it as base::UserData. | 61 // and attach a ServiceWorkerNetworkProvider to it as base::UserData. |
70 // (see createServiceWorkerNetworkProvider). | 62 // (see createServiceWorkerNetworkProvider). |
71 class DataSourceExtraData | 63 class DataSourceExtraData |
72 : public blink::WebDataSource::ExtraData, | 64 : public blink::WebDataSource::ExtraData, |
73 public base::SupportsUserData { | 65 public base::SupportsUserData { |
74 public: | 66 public: |
75 DataSourceExtraData() {} | 67 DataSourceExtraData() {} |
76 virtual ~DataSourceExtraData() {} | 68 virtual ~DataSourceExtraData() {} |
77 }; | 69 }; |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 base::WeakPtrFactory<ServiceWorkerContextClient> weak_factory; | 194 base::WeakPtrFactory<ServiceWorkerContextClient> weak_factory; |
203 }; | 195 }; |
204 | 196 |
205 ServiceWorkerContextClient* | 197 ServiceWorkerContextClient* |
206 ServiceWorkerContextClient::ThreadSpecificInstance() { | 198 ServiceWorkerContextClient::ThreadSpecificInstance() { |
207 return g_worker_client_tls.Pointer()->Get(); | 199 return g_worker_client_tls.Pointer()->Get(); |
208 } | 200 } |
209 | 201 |
210 ServiceWorkerContextClient::ServiceWorkerContextClient( | 202 ServiceWorkerContextClient::ServiceWorkerContextClient( |
211 int embedded_worker_id, | 203 int embedded_worker_id, |
| 204 EmbeddedWorkerImpl* embedded_worker, |
212 int64 service_worker_version_id, | 205 int64 service_worker_version_id, |
213 const GURL& service_worker_scope, | 206 const GURL& service_worker_scope, |
214 const GURL& script_url, | 207 const GURL& script_url, |
215 int worker_devtools_agent_route_id) | 208 int worker_devtools_agent_route_id) |
216 : embedded_worker_id_(embedded_worker_id), | 209 : embedded_worker_id_(embedded_worker_id), |
| 210 embedded_worker_(embedded_worker), |
217 service_worker_version_id_(service_worker_version_id), | 211 service_worker_version_id_(service_worker_version_id), |
218 service_worker_scope_(service_worker_scope), | 212 service_worker_scope_(service_worker_scope), |
219 script_url_(script_url), | 213 script_url_(script_url), |
220 worker_devtools_agent_route_id_(worker_devtools_agent_route_id), | 214 worker_devtools_agent_route_id_(worker_devtools_agent_route_id), |
221 sender_(ChildThreadImpl::current()->thread_safe_sender()), | 215 sender_(ChildThreadImpl::current()->thread_safe_sender()), |
222 main_thread_task_runner_(RenderThreadImpl::current()->GetTaskRunner()), | 216 main_thread_task_runner_(RenderThreadImpl::current()->GetTaskRunner()), |
223 proxy_(nullptr) { | 217 proxy_(nullptr) { |
224 TRACE_EVENT_ASYNC_BEGIN0("ServiceWorker", | 218 TRACE_EVENT_ASYNC_BEGIN0("ServiceWorker", |
225 "ServiceWorkerContextClient::StartingWorkerContext", | 219 "ServiceWorkerContextClient::StartingWorkerContext", |
226 this); | 220 this); |
227 TRACE_EVENT_ASYNC_STEP_INTO0( | 221 TRACE_EVENT_ASYNC_STEP_INTO0( |
228 "ServiceWorker", | 222 "ServiceWorker", "ServiceWorkerContextClient::StartingWorkerContext", |
229 "ServiceWorkerContextClient::StartingWorkerContext", | 223 this, "PrepareWorker"); |
230 this, | |
231 "PrepareWorker"); | |
232 } | 224 } |
233 | 225 |
234 ServiceWorkerContextClient::~ServiceWorkerContextClient() {} | 226 ServiceWorkerContextClient::~ServiceWorkerContextClient() {} |
235 | 227 |
236 void ServiceWorkerContextClient::OnMessageReceived( | 228 void ServiceWorkerContextClient::OnMessageReceived( |
237 int thread_id, | 229 int thread_id, |
238 int embedded_worker_id, | 230 int embedded_worker_id, |
239 const IPC::Message& message) { | 231 const IPC::Message& message) { |
240 CHECK_EQ(embedded_worker_id_, embedded_worker_id); | 232 CHECK_EQ(embedded_worker_id_, embedded_worker_id); |
241 bool handled = true; | 233 bool handled = true; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 | 309 |
318 void ServiceWorkerContextClient::workerReadyForInspection() { | 310 void ServiceWorkerContextClient::workerReadyForInspection() { |
319 Send(new EmbeddedWorkerHostMsg_WorkerReadyForInspection(embedded_worker_id_)); | 311 Send(new EmbeddedWorkerHostMsg_WorkerReadyForInspection(embedded_worker_id_)); |
320 } | 312 } |
321 | 313 |
322 void ServiceWorkerContextClient::workerContextFailedToStart() { | 314 void ServiceWorkerContextClient::workerContextFailedToStart() { |
323 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); | 315 DCHECK(main_thread_task_runner_->RunsTasksOnCurrentThread()); |
324 DCHECK(!proxy_); | 316 DCHECK(!proxy_); |
325 | 317 |
326 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoadFailed(embedded_worker_id_)); | 318 Send(new EmbeddedWorkerHostMsg_WorkerScriptLoadFailed(embedded_worker_id_)); |
327 | 319 embedded_worker_->WorkerContextDestroyed(); |
328 RenderThreadImpl::current()->embedded_worker_dispatcher()-> | |
329 WorkerContextDestroyed(embedded_worker_id_); | |
330 } | 320 } |
331 | 321 |
332 void ServiceWorkerContextClient::workerContextStarted( | 322 void ServiceWorkerContextClient::workerContextStarted( |
333 blink::WebServiceWorkerContextProxy* proxy) { | 323 blink::WebServiceWorkerContextProxy* proxy) { |
334 DCHECK(!worker_task_runner_.get()); | 324 DCHECK(!worker_task_runner_.get()); |
335 DCHECK_NE(0, WorkerTaskRunner::Instance()->CurrentWorkerId()); | 325 DCHECK_NE(0, WorkerTaskRunner::Instance()->CurrentWorkerId()); |
336 worker_task_runner_ = base::ThreadTaskRunnerHandle::Get(); | 326 worker_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
337 // g_worker_client_tls.Pointer()->Get() could return NULL if this context | 327 // g_worker_client_tls.Pointer()->Get() could return NULL if this context |
338 // gets deleted before workerContextStarted() is called. | 328 // gets deleted before workerContextStarted() is called. |
339 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL); | 329 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 // same thread. | 372 // same thread. |
383 context_.reset(); | 373 context_.reset(); |
384 | 374 |
385 // This also lets the message filter stop dispatching messages to | 375 // This also lets the message filter stop dispatching messages to |
386 // this client. | 376 // this client. |
387 g_worker_client_tls.Pointer()->Set(NULL); | 377 g_worker_client_tls.Pointer()->Set(NULL); |
388 } | 378 } |
389 | 379 |
390 void ServiceWorkerContextClient::workerContextDestroyed() { | 380 void ServiceWorkerContextClient::workerContextDestroyed() { |
391 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL); | 381 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL); |
392 | 382 embedded_worker_->WorkerContextDestroyed(); |
393 // Now we should be able to free the WebEmbeddedWorker container on the | |
394 // main thread. | |
395 main_thread_task_runner_->PostTask( | |
396 FROM_HERE, | |
397 base::Bind(&CallWorkerContextDestroyedOnMainThread, | |
398 embedded_worker_id_)); | |
399 } | 383 } |
400 | 384 |
401 void ServiceWorkerContextClient::reportException( | 385 void ServiceWorkerContextClient::reportException( |
402 const blink::WebString& error_message, | 386 const blink::WebString& error_message, |
403 int line_number, | 387 int line_number, |
404 int column_number, | 388 int column_number, |
405 const blink::WebString& source_url) { | 389 const blink::WebString& source_url) { |
406 Send(new EmbeddedWorkerHostMsg_ReportException( | 390 Send(new EmbeddedWorkerHostMsg_ReportException( |
407 embedded_worker_id_, | 391 embedded_worker_id_, |
408 error_message, | 392 error_message, |
(...skipping 562 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
971 } | 955 } |
972 | 956 |
973 base::WeakPtr<ServiceWorkerContextClient> | 957 base::WeakPtr<ServiceWorkerContextClient> |
974 ServiceWorkerContextClient::GetWeakPtr() { | 958 ServiceWorkerContextClient::GetWeakPtr() { |
975 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); | 959 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread()); |
976 DCHECK(context_); | 960 DCHECK(context_); |
977 return context_->weak_factory.GetWeakPtr(); | 961 return context_->weak_factory.GetWeakPtr(); |
978 } | 962 } |
979 | 963 |
980 } // namespace content | 964 } // namespace content |
OLD | NEW |