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/browser/service_worker/embedded_worker_instance.h" | 5 #include "content/browser/service_worker/embedded_worker_instance.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
13 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
14 #include "base/trace_event/trace_event.h" | 14 #include "base/trace_event/trace_event.h" |
15 #include "content/browser/devtools/service_worker_devtools_manager.h" | 15 #include "content/browser/devtools/service_worker_devtools_manager.h" |
16 #include "content/browser/service_worker/embedded_worker_registry.h" | 16 #include "content/browser/service_worker/embedded_worker_registry.h" |
17 #include "content/browser/service_worker/embedded_worker_status.h" | 17 #include "content/browser/service_worker/embedded_worker_status.h" |
18 #include "content/browser/service_worker/service_worker_context_core.h" | 18 #include "content/browser/service_worker/service_worker_context_core.h" |
19 #include "content/common/content_switches_internal.h" | 19 #include "content/common/content_switches_internal.h" |
20 #include "content/common/service_worker/embedded_worker_messages.h" | 20 #include "content/common/service_worker/embedded_worker_messages.h" |
21 #include "content/common/service_worker/embedded_worker_settings.h" | 21 #include "content/common/service_worker/embedded_worker_settings.h" |
22 #include "content/common/service_worker/embedded_worker_setup.mojom.h" | 22 #include "content/common/service_worker/embedded_worker_setup.mojom.h" |
23 #include "content/common/service_worker/service_worker_types.h" | 23 #include "content/common/service_worker/service_worker_types.h" |
24 #include "content/common/service_worker/service_worker_utils.h" | |
24 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
25 #include "content/public/browser/content_browser_client.h" | 26 #include "content/public/browser/content_browser_client.h" |
26 #include "content/public/browser/render_process_host.h" | 27 #include "content/public/browser/render_process_host.h" |
27 #include "content/public/common/child_process_host.h" | 28 #include "content/public/common/child_process_host.h" |
29 #include "content/public/common/content_switches.h" | |
28 #include "ipc/ipc_message.h" | 30 #include "ipc/ipc_message.h" |
29 #include "services/shell/public/cpp/interface_provider.h" | 31 #include "services/shell/public/cpp/interface_provider.h" |
30 #include "services/shell/public/cpp/interface_registry.h" | 32 #include "services/shell/public/cpp/interface_registry.h" |
31 #include "url/gurl.h" | 33 #include "url/gurl.h" |
32 | 34 |
33 namespace content { | 35 namespace content { |
34 | 36 |
35 namespace { | 37 namespace { |
36 | 38 |
37 // When a service worker version's failure count exceeds | 39 // When a service worker version's failure count exceeds |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 // |rph| or its InterfaceProvider may be NULL in unit tests. | 110 // |rph| or its InterfaceProvider may be NULL in unit tests. |
109 if (!rph || !rph->GetRemoteInterfaces()) | 111 if (!rph || !rph->GetRemoteInterfaces()) |
110 return; | 112 return; |
111 mojom::EmbeddedWorkerSetupPtr setup; | 113 mojom::EmbeddedWorkerSetupPtr setup; |
112 rph->GetRemoteInterfaces()->GetInterface(&setup); | 114 rph->GetRemoteInterfaces()->GetInterface(&setup); |
113 setup->ExchangeInterfaceProviders( | 115 setup->ExchangeInterfaceProviders( |
114 thread_id, std::move(remote_interfaces), | 116 thread_id, std::move(remote_interfaces), |
115 mojo::MakeProxy(std::move(exposed_interfaces))); | 117 mojo::MakeProxy(std::move(exposed_interfaces))); |
116 } | 118 } |
117 | 119 |
120 void GetRemoteEmbeddededWorkerInstanceClientOnUIThread( | |
121 int process_id, | |
122 const base::Callback<void(mojom::EmbeddedWorkerInstanceClientPtr)>& | |
123 callback_on_io) { | |
124 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
125 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); | |
126 // |rph| or its InterfaceProvider may be NULL in unit tests. | |
127 if (!rph || !rph->GetRemoteInterfaces()) | |
128 return; | |
129 mojom::EmbeddedWorkerInstanceClientPtr instance_client; | |
130 rph->GetRemoteInterfaces()->GetInterface(&instance_client); | |
Marijn Kruisselbrink
2016/08/23 18:56:08
Rather than creating and binding the InterfacePtr
shimazu
2016/08/25 05:14:52
Thanks, it seems better. Updated.
| |
131 BrowserThread::PostTask( | |
132 BrowserThread::IO, FROM_HERE, | |
133 base::Bind(callback_on_io, base::Passed(&instance_client))); | |
134 } | |
135 | |
118 } // namespace | 136 } // namespace |
119 | 137 |
120 // Lives on IO thread, proxies notifications to DevToolsManager that lives on | 138 // Lives on IO thread, proxies notifications to DevToolsManager that lives on |
121 // UI thread. Owned by EmbeddedWorkerInstance. | 139 // UI thread. Owned by EmbeddedWorkerInstance. |
122 class EmbeddedWorkerInstance::DevToolsProxy : public base::NonThreadSafe { | 140 class EmbeddedWorkerInstance::DevToolsProxy : public base::NonThreadSafe { |
123 public: | 141 public: |
124 DevToolsProxy(int process_id, int agent_route_id) | 142 DevToolsProxy(int process_id, int agent_route_id) |
125 : process_id_(process_id), | 143 : process_id_(process_id), |
126 agent_route_id_(agent_route_id) {} | 144 agent_route_id_(agent_route_id) {} |
127 | 145 |
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
359 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", | 377 TRACE_EVENT_ASYNC_STEP_PAST0("ServiceWorker", |
360 "EmbeddedWorkerInstance::Start", this, | 378 "EmbeddedWorkerInstance::Start", this, |
361 "OnRegisteredToDevToolsManager"); | 379 "OnRegisteredToDevToolsManager"); |
362 | 380 |
363 // Notify the instance that it is registered to the devtools manager. | 381 // Notify the instance that it is registered to the devtools manager. |
364 instance_->OnRegisteredToDevToolsManager( | 382 instance_->OnRegisteredToDevToolsManager( |
365 is_new_process, worker_devtools_agent_route_id, wait_for_debugger); | 383 is_new_process, worker_devtools_agent_route_id, wait_for_debugger); |
366 | 384 |
367 params->worker_devtools_agent_route_id = worker_devtools_agent_route_id; | 385 params->worker_devtools_agent_route_id = worker_devtools_agent_route_id; |
368 params->wait_for_debugger = wait_for_debugger; | 386 params->wait_for_debugger = wait_for_debugger; |
369 SendStartWorker(std::move(params)); | 387 |
388 if (ServiceWorkerUtils::IsMojoForServiceWorkerEnabled()) { | |
389 auto callback = base::Bind( | |
390 &EmbeddedWorkerInstance::OnGotRemoteEmbeddedWorkerInstanceClient, | |
391 instance_->weak_factory_.GetWeakPtr(), base::Passed(¶ms)); | |
392 BrowserThread::PostTask( | |
393 BrowserThread::UI, FROM_HERE, | |
394 base::Bind(&GetRemoteEmbeddededWorkerInstanceClientOnUIThread, | |
395 instance_->process_id(), callback)); | |
396 } else { | |
397 SendStartWorker(std::move(params)); | |
398 } | |
370 } | 399 } |
371 | 400 |
372 void SendStartWorker( | 401 void SendStartWorker( |
373 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params) { | 402 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params) { |
374 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 403 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
375 ServiceWorkerStatusCode status = instance_->registry_->SendStartWorker( | 404 ServiceWorkerStatusCode status = instance_->registry_->SendStartWorker( |
376 std::move(params), instance_->process_id()); | 405 std::move(params), instance_->process_id()); |
377 TRACE_EVENT_ASYNC_STEP_PAST1( | 406 TRACE_EVENT_ASYNC_STEP_PAST1( |
378 "ServiceWorker", "EmbeddedWorkerInstance::Start", this, | 407 "ServiceWorker", "EmbeddedWorkerInstance::Start", this, |
379 "SendStartWorker", "Status", ServiceWorkerStatusToString(status)); | 408 "SendStartWorker", "Status", ServiceWorkerStatusToString(status)); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
558 devtools_proxy_.reset( | 587 devtools_proxy_.reset( |
559 new DevToolsProxy(process_id(), worker_devtools_agent_route_id)); | 588 new DevToolsProxy(process_id(), worker_devtools_agent_route_id)); |
560 } | 589 } |
561 if (wait_for_debugger) { | 590 if (wait_for_debugger) { |
562 // We don't measure the start time when wait_for_debugger flag is set. So | 591 // We don't measure the start time when wait_for_debugger flag is set. So |
563 // we set the NULL time here. | 592 // we set the NULL time here. |
564 step_time_ = base::TimeTicks(); | 593 step_time_ = base::TimeTicks(); |
565 } | 594 } |
566 } | 595 } |
567 | 596 |
597 void EmbeddedWorkerInstance::OnGotRemoteEmbeddedWorkerInstanceClient( | |
598 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | |
599 mojom::EmbeddedWorkerInstanceClientPtr instance_client) { | |
600 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
601 // Bind again for thread hop of the mojo interface pointer | |
602 instance_client_.Bind(instance_client.PassInterface()); | |
603 SendMojoStartWorker(std::move(params)); | |
604 } | |
605 | |
606 void EmbeddedWorkerInstance::SendMojoStartWorker( | |
607 std::unique_ptr<EmbeddedWorkerMsg_StartWorker_Params> params) { | |
608 auto mojo_params = mojom::EmbeddedWorkerStartWorkerParams::New(); | |
609 mojo_params->embedded_worker_id = params->embedded_worker_id; | |
610 mojo_params->service_worker_version_id = params->service_worker_version_id; | |
611 mojo_params->scope = params->scope; | |
612 mojo_params->script_url = params->script_url; | |
613 mojo_params->worker_devtools_agent_route_id = | |
614 params->worker_devtools_agent_route_id; | |
615 mojo_params->pause_after_download = params->pause_after_download; | |
616 mojo_params->wait_for_debugger = params->wait_for_debugger; | |
617 mojo_params->is_installed = params->is_installed; | |
618 | |
619 mojo_params->settings = mojom::EmbeddedWorkerSettings::New(); | |
620 mojo_params->settings->data_saver_enabled = | |
621 params->settings.data_saver_enabled; | |
622 mojo_params->settings->v8_cache_options = | |
623 static_cast<mojom::V8CacheOptions>(params->settings.v8_cache_options); | |
horo
2016/08/24 04:59:16
We should have static_assert() for mojo::V8CacheOp
shimazu
2016/08/25 05:14:52
Done.
| |
624 | |
625 instance_client_->StartWorker(std::move(mojo_params)); | |
626 registry_->AddWorker(process_id(), embedded_worker_id()); | |
627 TRACE_EVENT_ASYNC_STEP_PAST1("ServiceWorker", "EmbeddedWorkerInstance::Start", | |
628 this, "SendStartWorker", "Status", "mojo"); | |
629 OnStartWorkerMessageSent(); | |
630 return; | |
631 } | |
632 | |
568 void EmbeddedWorkerInstance::OnStartWorkerMessageSent() { | 633 void EmbeddedWorkerInstance::OnStartWorkerMessageSent() { |
569 if (!step_time_.is_null()) { | 634 if (!step_time_.is_null()) { |
570 base::TimeDelta duration = UpdateStepTime(); | 635 base::TimeDelta duration = UpdateStepTime(); |
571 if (inflight_start_task_->is_installed()) { | 636 if (inflight_start_task_->is_installed()) { |
572 ServiceWorkerMetrics::RecordTimeToSendStartWorker(duration, | 637 ServiceWorkerMetrics::RecordTimeToSendStartWorker(duration, |
573 start_situation_); | 638 start_situation_); |
574 } | 639 } |
575 } | 640 } |
576 | 641 |
577 starting_phase_ = SENT_START_WORKER; | 642 starting_phase_ = SENT_START_WORKER; |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
885 case SCRIPT_READ_FINISHED: | 950 case SCRIPT_READ_FINISHED: |
886 return "Script read finished"; | 951 return "Script read finished"; |
887 case STARTING_PHASE_MAX_VALUE: | 952 case STARTING_PHASE_MAX_VALUE: |
888 NOTREACHED(); | 953 NOTREACHED(); |
889 } | 954 } |
890 NOTREACHED() << phase; | 955 NOTREACHED() << phase; |
891 return std::string(); | 956 return std::string(); |
892 } | 957 } |
893 | 958 |
894 } // namespace content | 959 } // namespace content |
OLD | NEW |