blob: f83b84629d440bc446b3d3a982457cf0b2f02782 [file] [log] [blame]
[email protected]02798a982012-01-27 00:45:331// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]d27893f62010-07-03 05:47:422// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]4c01d4992012-01-23 23:33:015#include "content/browser/browser_child_process_host_impl.h"
[email protected]d27893f62010-07-03 05:47:426
[email protected]2b065f82012-10-18 07:11:167#include "base/base_switches.h"
[email protected]4306c3792011-12-02 01:57:538#include "base/bind.h"
[email protected]d27893f62010-07-03 05:47:429#include "base/command_line.h"
Yuzhu Shen32fcad72017-09-14 23:12:3710#include "base/debug/crash_logging.h"
wfhaf32d0b2015-04-07 02:21:3611#include "base/debug/dump_without_crashing.h"
erikchen55edbff2016-05-03 23:53:1712#include "base/feature_list.h"
[email protected]57999812013-02-24 05:40:5213#include "base/files/file_path.h"
[email protected]d3c6c0d72010-12-09 08:15:0414#include "base/lazy_instance.h"
[email protected]d27893f62010-07-03 05:47:4215#include "base/logging.h"
avib7348942015-12-25 20:57:1016#include "base/macros.h"
erikchen55edbff2016-05-03 23:53:1717#include "base/metrics/field_trial.h"
Ilya Sherman1c811db2017-12-14 10:36:1818#include "base/metrics/histogram_functions.h"
asvitkine30330812016-08-30 04:01:0819#include "base/metrics/histogram_macros.h"
bcwhiteb58a5fe2016-08-22 16:25:5620#include "base/metrics/persistent_histogram_allocator.h"
21#include "base/metrics/persistent_memory_allocator.h"
[email protected]7286e3fc2011-07-19 22:13:2422#include "base/stl_util.h"
[email protected]10994d132013-06-11 07:16:1823#include "base/strings/string_util.h"
rockotda9887902016-08-19 20:46:4224#include "base/strings/stringprintf.h"
[email protected]162a9fd2013-03-06 20:47:4925#include "base/synchronization/waitable_event.h"
Eric Seckler8652dcd52018-09-20 10:42:2826#include "base/task/post_task.h"
rockot229fb28e2016-06-16 04:46:1627#include "base/threading/thread_task_runner_handle.h"
Ken Rockot315def72018-11-10 02:16:1028#include "base/token.h"
avib7348942015-12-25 20:57:1029#include "build/build_config.h"
Ehsan Chiniforooshandef633e02018-04-24 02:05:0230#include "components/tracing/common/trace_startup_config.h"
primianoccb26c62016-06-01 21:50:0231#include "components/tracing/common/tracing_switches.h"
Robert Sesek1419427e2017-12-07 15:01:3232#include "content/browser/bad_message.h"
Ehsan Chiniforooshanf192e8a22018-03-27 03:18:2033#include "content/browser/browser_main_loop.h"
Nicholas Verne91bf4a632017-10-12 21:14:2434#include "content/browser/histogram_controller.h"
[email protected]678c0362012-12-05 08:02:4435#include "content/browser/loader/resource_message_filter.h"
benff461632016-10-06 14:27:3336#include "content/browser/service_manager/service_manager_context.h"
Darin Fisherc23ee1a2019-06-05 23:26:3037#include "content/browser/tracing/background_tracing_manager_impl.h"
[email protected]4734d0b2011-12-03 07:10:4438#include "content/common/child_process_host_impl.h"
bend32292b2016-10-07 00:21:5839#include "content/common/service_manager/child_connection.h"
[email protected]4967f792012-01-20 22:14:4040#include "content/public/browser/browser_child_process_host_delegate.h"
[email protected]f4eaf7b92013-02-28 22:00:4041#include "content/public/browser/browser_child_process_observer.h"
Eric Seckler8652dcd52018-09-20 10:42:2842#include "content/public/browser/browser_task_traits.h"
[email protected]57999812013-02-24 05:40:5243#include "content/public/browser/browser_thread.h"
[email protected]4306c3792011-12-02 01:57:5344#include "content/public/browser/child_process_data.h"
[email protected]87f3c082011-10-19 18:07:4445#include "content/public/browser/content_browser_client.h"
ke.he19b5e6a2016-10-27 08:47:0946#include "content/public/common/connection_filter.h"
lawrencewu2c930822016-10-13 13:54:2547#include "content/public/common/content_features.h"
[email protected]c08950d22011-10-13 22:20:2948#include "content/public/common/content_switches.h"
[email protected]f3b357692013-03-22 05:16:1349#include "content/public/common/process_type.h"
[email protected]b39ef1cb2011-10-25 04:46:5550#include "content/public/common/result_codes.h"
jcivelli828cd7f2017-01-18 19:50:4651#include "content/public/common/sandboxed_process_launcher_delegate.h"
ke.he19b5e6a2016-10-27 08:47:0952#include "content/public/common/service_manager_connection.h"
Nicholas Verne91bf4a632017-10-12 21:14:2453#include "mojo/public/cpp/system/platform_handle.h"
Yoichi Osato14073bd2019-06-04 11:06:3754#include "net/websockets/websocket_basic_stream.h"
Yoichi Osato7ce08952019-06-18 02:10:3455#include "net/websockets/websocket_channel.h"
ochang6a8400e2017-05-01 21:28:2456#include "services/service_manager/embedder/switches.h"
Ken Rockota86fa542018-11-13 21:47:1357#include "services/service_manager/public/cpp/constants.h"
[email protected]d27893f62010-07-03 05:47:4258
[email protected]f1675202012-07-09 15:18:0059#if defined(OS_MACOSX)
Robert Sesekde2e713262019-07-15 20:57:5860#include "content/browser/child_process_task_port_provider_mac.h"
[email protected]3838ca12011-11-02 14:37:1561#endif
[email protected]a3a7e2c2011-09-16 23:07:0562
[email protected]130757672012-10-24 00:26:1963namespace content {
[email protected]d27893f62010-07-03 05:47:4264namespace {
65
scottmg5e65e3a2017-03-08 08:48:4666static base::LazyInstance<
67 BrowserChildProcessHostImpl::BrowserChildProcessList>::DestructorAtExit
[email protected]4967f792012-01-20 22:14:4068 g_child_process_list = LAZY_INSTANCE_INITIALIZER;
[email protected]d27893f62010-07-03 05:47:4269
Trent Apteda250ec3ab2018-08-19 08:52:1970base::LazyInstance<base::ObserverList<BrowserChildProcessObserver>::Unchecked>::
Daniel Bratellbfcfe222017-10-17 17:57:0071 DestructorAtExit g_browser_child_process_observers =
72 LAZY_INSTANCE_INITIALIZER;
[email protected]f4eaf7b92013-02-28 22:00:4073
afakhry9cc79002015-10-22 19:25:1574void NotifyProcessLaunchedAndConnected(const ChildProcessData& data) {
Daniel Bratellbfcfe222017-10-17 17:57:0075 for (auto& observer : g_browser_child_process_observers.Get())
ericwilligers254597b2016-10-17 10:32:3176 observer.BrowserChildProcessLaunchedAndConnected(data);
afakhry9cc79002015-10-22 19:25:1577}
78
[email protected]f4eaf7b92013-02-28 22:00:4079void NotifyProcessHostConnected(const ChildProcessData& data) {
Daniel Bratellbfcfe222017-10-17 17:57:0080 for (auto& observer : g_browser_child_process_observers.Get())
ericwilligers254597b2016-10-17 10:32:3181 observer.BrowserChildProcessHostConnected(data);
[email protected]f4eaf7b92013-02-28 22:00:4082}
83
84void NotifyProcessHostDisconnected(const ChildProcessData& data) {
Daniel Bratellbfcfe222017-10-17 17:57:0085 for (auto& observer : g_browser_child_process_observers.Get())
ericwilligers254597b2016-10-17 10:32:3186 observer.BrowserChildProcessHostDisconnected(data);
[email protected]f4eaf7b92013-02-28 22:00:4087}
88
Bo Liue9624612018-04-28 00:39:0889#if !defined(OS_ANDROID)
Bo Liu2a489402018-04-24 23:41:2790void NotifyProcessCrashed(const ChildProcessData& data,
91 const ChildProcessTerminationInfo& info) {
Daniel Bratellbfcfe222017-10-17 17:57:0092 for (auto& observer : g_browser_child_process_observers.Get())
Bo Liu2a489402018-04-24 23:41:2793 observer.BrowserChildProcessCrashed(data, info);
[email protected]4306c3792011-12-02 01:57:5394}
Bo Liue9624612018-04-28 00:39:0895#endif
[email protected]d27893f62010-07-03 05:47:4296
Bo Liu2a489402018-04-24 23:41:2797void NotifyProcessKilled(const ChildProcessData& data,
98 const ChildProcessTerminationInfo& info) {
Daniel Bratellbfcfe222017-10-17 17:57:0099 for (auto& observer : g_browser_child_process_observers.Get())
Bo Liu2a489402018-04-24 23:41:27100 observer.BrowserChildProcessKilled(data, info);
wfhf0003292015-08-18 22:16:46101}
102
[email protected]d27893f62010-07-03 05:47:42103} // namespace
104
[email protected]4967f792012-01-20 22:14:40105BrowserChildProcessHost* BrowserChildProcessHost::Create(
vadimt379d7fe2015-04-01 00:09:35106 content::ProcessType process_type,
[email protected]4967f792012-01-20 22:14:40107 BrowserChildProcessHostDelegate* delegate) {
rockotda9887902016-08-19 20:46:42108 return Create(process_type, delegate, std::string());
amistry6ad1e812016-06-06 05:36:30109}
110
111BrowserChildProcessHost* BrowserChildProcessHost::Create(
112 content::ProcessType process_type,
113 BrowserChildProcessHostDelegate* delegate,
rockotda9887902016-08-19 20:46:42114 const std::string& service_name) {
115 return new BrowserChildProcessHostImpl(process_type, delegate, service_name);
[email protected]4967f792012-01-20 22:14:40116}
117
amistryfaa231a42015-05-20 01:49:12118BrowserChildProcessHost* BrowserChildProcessHost::FromID(int child_process_id) {
119 DCHECK_CURRENTLY_ON(BrowserThread::IO);
120 BrowserChildProcessHostImpl::BrowserChildProcessList* process_list =
121 g_child_process_list.Pointer();
122 for (BrowserChildProcessHostImpl* host : *process_list) {
123 if (host->GetData().id == child_process_id)
124 return host;
125 }
126 return nullptr;
127}
128
[email protected]458433c2012-02-29 23:43:39129#if defined(OS_MACOSX)
erikchencd3e0e7b2015-10-01 22:53:51130base::PortProvider* BrowserChildProcessHost::GetPortProvider() {
Robert Sesekde2e713262019-07-15 20:57:58131 return ChildProcessTaskPortProvider::GetInstance();
[email protected]458433c2012-02-29 23:43:39132}
133#endif
134
[email protected]f4eaf7b92013-02-28 22:00:40135// static
[email protected]4c01d4992012-01-23 23:33:01136BrowserChildProcessHostImpl::BrowserChildProcessList*
Maggie Chen47b8dc1d2018-11-10 00:12:34137BrowserChildProcessHostImpl::GetIterator() {
[email protected]4967f792012-01-20 22:14:40138 return g_child_process_list.Pointer();
139}
140
[email protected]f4eaf7b92013-02-28 22:00:40141// static
142void BrowserChildProcessHostImpl::AddObserver(
143 BrowserChildProcessObserver* observer) {
mostynbfbcdc27a2015-03-13 17:58:52144 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Daniel Bratellbfcfe222017-10-17 17:57:00145 g_browser_child_process_observers.Get().AddObserver(observer);
[email protected]f4eaf7b92013-02-28 22:00:40146}
147
148// static
149void BrowserChildProcessHostImpl::RemoveObserver(
150 BrowserChildProcessObserver* observer) {
151 // TODO(phajdan.jr): Check thread after fixing http://crbug.com/167126.
Daniel Bratellbfcfe222017-10-17 17:57:00152 g_browser_child_process_observers.Get().RemoveObserver(observer);
[email protected]f4eaf7b92013-02-28 22:00:40153}
154
[email protected]4c01d4992012-01-23 23:33:01155BrowserChildProcessHostImpl::BrowserChildProcessHostImpl(
vadimt379d7fe2015-04-01 00:09:35156 content::ProcessType process_type,
amistry6ad1e812016-06-06 05:36:30157 BrowserChildProcessHostDelegate* delegate,
rockotda9887902016-08-19 20:46:42158 const std::string& service_name)
[email protected]f3b357692013-03-22 05:16:13159 : data_(process_type),
[email protected]9dd90152013-08-02 22:09:13160 delegate_(delegate),
sadrul6c5aed8c2017-01-11 23:11:44161 channel_(nullptr),
amistry012289f2016-04-01 00:56:37162 is_channel_connected_(false),
Jeremy Roman3bca4bf2019-07-11 03:41:25163 notify_child_disconnected_(false) {
[email protected]4734d0b2011-12-03 07:10:44164 data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId();
[email protected]4306c3792011-12-02 01:57:53165
Ayu Ishii75cc1062019-01-08 01:27:37166 child_process_host_ = ChildProcessHost::Create(this);
[email protected]38917fe2011-04-06 17:29:12167
168 g_child_process_list.Get().push_back(this);
[email protected]130757672012-10-24 00:26:19169 GetContentClient()->browser()->BrowserChildProcessHostCreated(this);
[email protected]30c4c67a2014-07-22 02:33:55170
rockotda9887902016-08-19 20:46:42171 if (!service_name.empty()) {
172 DCHECK_CURRENTLY_ON(BrowserThread::IO);
Ken Rockota86fa542018-11-13 21:47:13173 child_connection_ = std::make_unique<ChildConnection>(
174 service_manager::Identity(
175 service_name, service_manager::kSystemInstanceGroup,
176 base::Token::CreateRandom(), base::Token::CreateRandom()),
177 &mojo_invitation_, ServiceManagerContext::GetConnectorForIOThread(),
178 base::ThreadTaskRunnerHandle::Get());
Will Harrisedb011f2018-06-01 20:28:44179 data_.metrics_name = service_name;
rockotda9887902016-08-19 20:46:42180 }
bcwhiteb58a5fe2016-08-22 16:25:56181
182 // Create a persistent memory segment for subprocess histograms.
183 CreateMetricsAllocator();
[email protected]38917fe2011-04-06 17:29:12184}
185
[email protected]4c01d4992012-01-23 23:33:01186BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() {
[email protected]d3c6c0d72010-12-09 08:15:04187 g_child_process_list.Get().remove(this);
amistry012289f2016-04-01 00:56:37188
189 if (notify_child_disconnected_) {
Eric Seckler8652dcd52018-09-20 10:42:28190 base::PostTaskWithTraits(
191 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26192 base::BindOnce(&NotifyProcessHostDisconnected, data_.Duplicate()));
amistry012289f2016-04-01 00:56:37193 }
[email protected]d27893f62010-07-03 05:47:42194}
195
196// static
[email protected]4c01d4992012-01-23 23:33:01197void BrowserChildProcessHostImpl::TerminateAll() {
mostynbfbcdc27a2015-03-13 17:58:52198 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]f174efd2012-01-17 18:43:33199 // Make a copy since the BrowserChildProcessHost dtor mutates the original
200 // list.
201 BrowserChildProcessList copy = g_child_process_list.Get();
jdoerrie55ec69d2018-10-08 13:34:46202 for (auto it = copy.begin(); it != copy.end(); ++it) {
[email protected]5998d9d2012-03-06 04:25:26203 delete (*it)->delegate(); // ~*HostDelegate deletes *HostImpl.
204 }
[email protected]d27893f62010-07-03 05:47:42205}
206
erikchen55edbff2016-05-03 23:53:17207// static
lawrencewu0b4964922016-10-25 16:28:14208void BrowserChildProcessHostImpl::CopyFeatureAndFieldTrialFlags(
erikchen55edbff2016-05-03 23:53:17209 base::CommandLine* cmd_line) {
erikchen55edbff2016-05-03 23:53:17210 // If we run base::FieldTrials, we want to pass to their state to the
211 // child process so that it can act in accordance with each state.
lawrencewu5e03cd32016-12-05 16:23:28212 base::FieldTrialList::CopyFieldTrialStateToFlags(
213 switches::kFieldTrialHandle, switches::kEnableFeatures,
214 switches::kDisableFeatures, cmd_line);
erikchen55edbff2016-05-03 23:53:17215}
216
Ehsan Chiniforooshanf192e8a22018-03-27 03:18:20217// static
218void BrowserChildProcessHostImpl::CopyTraceStartupFlags(
219 base::CommandLine* cmd_line) {
Ehsan Chiniforooshandef633e02018-04-24 02:05:02220 if (tracing::TraceStartupConfig::GetInstance()->IsEnabled()) {
Ehsan Chiniforooshanf192e8a22018-03-27 03:18:20221 const auto trace_config =
Ehsan Chiniforooshandef633e02018-04-24 02:05:02222 tracing::TraceStartupConfig::GetInstance()->GetTraceConfig();
Ehsan Chiniforooshanf192e8a22018-03-27 03:18:20223 if (!trace_config.IsArgumentFilterEnabled()) {
224 // The only trace option that we can pass through switches is the record
225 // mode. Other trace options should have the default value.
226 //
227 // TODO(chiniforooshan): Add other trace options to switches if, for
228 // example, they are used in a telemetry test that needs startup trace
229 // events from renderer processes.
230 cmd_line->AppendSwitchASCII(switches::kTraceStartup,
231 trace_config.ToCategoryFilterString());
232 cmd_line->AppendSwitchASCII(
233 switches::kTraceStartupRecordMode,
234 base::trace_event::TraceConfig::TraceRecordModeToStr(
235 trace_config.GetTraceRecordMode()));
236 }
237 }
238}
239
[email protected]4c01d4992012-01-23 23:33:01240void BrowserChildProcessHostImpl::Launch(
jcivellid20cc0b2016-12-22 03:51:41241 std::unique_ptr<SandboxedProcessLauncherDelegate> delegate,
242 std::unique_ptr<base::CommandLine> cmd_line,
sievers954e37a2015-03-28 01:50:24243 bool terminate_on_shutdown) {
jcivellid20cc0b2016-12-22 03:51:41244 GetContentClient()->browser()->AppendExtraCommandLineSwitches(cmd_line.get(),
245 data_.id);
Maggie Chen47b8dc1d2018-11-10 00:12:34246 LaunchWithoutExtraCommandLineSwitches(
247 std::move(delegate), std::move(cmd_line), terminate_on_shutdown);
[email protected]d27893f62010-07-03 05:47:42248}
249
Lucas Furukawa Gadani0d5e7142019-04-18 23:00:50250const ChildProcessData& BrowserChildProcessHostImpl::GetData() {
mostynbfbcdc27a2015-03-13 17:58:52251 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]4967f792012-01-20 22:14:40252 return data_;
253}
254
Lucas Furukawa Gadani0d5e7142019-04-18 23:00:50255ChildProcessHost* BrowserChildProcessHostImpl::GetHost() {
mostynbfbcdc27a2015-03-13 17:58:52256 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]4967f792012-01-20 22:14:40257 return child_process_host_.get();
258}
259
Lucas Furukawa Gadani0d5e7142019-04-18 23:00:50260const base::Process& BrowserChildProcessHostImpl::GetProcess() {
mostynbfbcdc27a2015-03-13 17:58:52261 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]5d84d012010-12-02 17:17:21262 DCHECK(child_process_.get())
263 << "Requesting a child process handle before launching.";
rvargas079d1842014-10-17 22:32:16264 DCHECK(child_process_->GetProcess().IsValid())
[email protected]5d84d012010-12-02 17:17:21265 << "Requesting a child process handle before launch has completed OK.";
rvargas5779b382014-11-18 20:44:11266 return child_process_->GetProcess();
[email protected]5d84d012010-12-02 17:17:21267}
268
Alex Ilin30e7d062019-02-21 12:24:53269std::unique_ptr<base::PersistentMemoryAllocator>
bcwhiteb58a5fe2016-08-22 16:25:56270BrowserChildProcessHostImpl::TakeMetricsAllocator() {
271 return std::move(metrics_allocator_);
272}
273
[email protected]fcf75d42013-12-03 20:11:26274void BrowserChildProcessHostImpl::SetName(const base::string16& name) {
mostynbfbcdc27a2015-03-13 17:58:52275 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]f174efd2012-01-17 18:43:33276 data_.name = name;
277}
278
Will Harrisedb011f2018-06-01 20:28:44279void BrowserChildProcessHostImpl::SetMetricsName(
280 const std::string& metrics_name) {
281 DCHECK_CURRENTLY_ON(BrowserThread::IO);
282 data_.metrics_name = metrics_name;
283}
284
Francois Doray884da022018-10-30 23:45:58285void BrowserChildProcessHostImpl::SetProcess(base::Process process) {
mostynbfbcdc27a2015-03-13 17:58:52286 DCHECK_CURRENTLY_ON(BrowserThread::IO);
Francois Doray884da022018-10-30 23:45:58287 data_.SetProcess(std::move(process));
[email protected]f174efd2012-01-17 18:43:33288}
289
Ken Rockotea1716a02017-05-11 05:49:10290service_manager::mojom::ServiceRequest
291BrowserChildProcessHostImpl::TakeInProcessServiceRequest() {
Ken Rockot026afc32018-06-04 19:19:18292 auto invitation = std::move(mojo_invitation_);
Ken Rockotf4d8a942017-05-13 00:10:37293 return service_manager::mojom::ServiceRequest(
Ken Rockot026afc32018-06-04 19:19:18294 invitation.ExtractMessagePipe(child_connection_->service_token()));
sammc2b0375b2016-12-22 01:26:17295}
296
[email protected]4c01d4992012-01-23 23:33:01297void BrowserChildProcessHostImpl::ForceShutdown() {
mostynbfbcdc27a2015-03-13 17:58:52298 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]d3c6c0d72010-12-09 08:15:04299 g_child_process_list.Get().remove(this);
[email protected]4cb43102011-12-02 20:24:49300 child_process_host_->ForceShutdown();
[email protected]d27893f62010-07-03 05:47:42301}
302
[email protected]ba780c12013-10-01 17:07:06303void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter* filter) {
304 child_process_host_->AddFilter(filter->GetFilter());
305}
306
Maggie Chen47b8dc1d2018-11-10 00:12:34307void BrowserChildProcessHostImpl::LaunchWithoutExtraCommandLineSwitches(
308 std::unique_ptr<SandboxedProcessLauncherDelegate> delegate,
309 std::unique_ptr<base::CommandLine> cmd_line,
310 bool terminate_on_shutdown) {
311 DCHECK_CURRENTLY_ON(BrowserThread::IO);
312 const base::CommandLine& browser_command_line =
313 *base::CommandLine::ForCurrentProcess();
314 static const char* const kForwardSwitches[] = {
Yoichi Osato14073bd2019-06-04 11:06:37315 net::kWebSocketReadBufferSize,
Yoichi Osato7ce08952019-06-18 02:10:34316 net::kWebSocketReceiveQuotaThreshold,
Maggie Chen47b8dc1d2018-11-10 00:12:34317 service_manager::switches::kDisableInProcessStackTraces,
Yue Ru Suna50a80f2019-02-08 05:02:58318 switches::kDisableBestEffortTasks,
Maggie Chen47b8dc1d2018-11-10 00:12:34319 switches::kDisableLogging,
Oystein Eftevaag001c99172019-03-23 00:37:29320 switches::kDisablePerfetto,
Maggie Chen47b8dc1d2018-11-10 00:12:34321 switches::kEnableLogging,
322 switches::kIPCConnectionTimeout,
Francois Doray177da2c2019-06-20 14:14:22323 switches::kLogBestEffortTasks,
Greg Thompson54915832018-12-21 17:11:47324 switches::kLogFile,
Maggie Chen47b8dc1d2018-11-10 00:12:34325 switches::kLoggingLevel,
326 switches::kTraceToConsole,
327 switches::kV,
328 switches::kVModule,
329 };
330 cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches,
331 base::size(kForwardSwitches));
332
333 if (child_connection_) {
334 cmd_line->AppendSwitchASCII(
335 service_manager::switches::kServiceRequestChannelToken,
336 child_connection_->service_token());
Darin Fisherc23ee1a2019-06-05 23:26:30337
338 // Tracing adds too much overhead to the profiling service.
339 if (service_manager::SandboxTypeFromCommandLine(*cmd_line) !=
340 service_manager::SANDBOX_TYPE_PROFILING) {
Darin Fisher0e4fc2e2019-06-13 02:19:18341 BackgroundTracingManagerImpl::ActivateForProcess(
342 data_.id,
343 static_cast<ChildProcessHostImpl*>(child_process_host_.get())
344 ->child_control());
Darin Fisherc23ee1a2019-06-05 23:26:30345 }
Maggie Chen47b8dc1d2018-11-10 00:12:34346 }
347
348 // All processes should have a non-empty metrics name.
349 DCHECK(!data_.metrics_name.empty());
350
351 notify_child_disconnected_ = true;
352 child_process_.reset(new ChildProcessLauncher(
353 std::move(delegate), std::move(cmd_line), data_.id, this,
354 std::move(mojo_invitation_),
355 base::BindRepeating(&BrowserChildProcessHostImpl::OnMojoError,
356 weak_factory_.GetWeakPtr(),
357 base::ThreadTaskRunnerHandle::Get()),
358 terminate_on_shutdown));
359 ShareMetricsAllocatorToProcess();
360}
361
bena06f7d62017-04-04 20:48:52362void BrowserChildProcessHostImpl::BindInterface(
363 const std::string& interface_name,
364 mojo::ScopedMessagePipeHandle interface_pipe) {
rockotda9887902016-08-19 20:46:42365 DCHECK_CURRENTLY_ON(BrowserThread::IO);
366 if (!child_connection_)
bena06f7d62017-04-04 20:48:52367 return;
rockotda9887902016-08-19 20:46:42368
bena06f7d62017-04-04 20:48:52369 child_connection_->BindInterface(interface_name, std::move(interface_pipe));
rockotda9887902016-08-19 20:46:42370}
371
[email protected]ef2f6ba2014-05-15 23:06:07372void BrowserChildProcessHostImpl::HistogramBadMessageTerminated(
Wei-Yin Chen (陳威尹)223326c2017-07-21 02:08:28373 ProcessType process_type) {
[email protected]ef2f6ba2014-05-15 23:06:07374 UMA_HISTOGRAM_ENUMERATION("ChildProcess.BadMessgeTerminated", process_type,
375 PROCESS_TYPE_MAX);
376}
377
Min Qineb961902019-03-16 08:08:22378#if defined(OS_ANDROID)
379void BrowserChildProcessHostImpl::EnableWarmUpConnection() {
380 can_use_warm_up_connection_ = true;
381}
382#endif
383
Bo Liu0d2a2322018-04-19 00:18:09384ChildProcessTerminationInfo BrowserChildProcessHostImpl::GetTerminationInfo(
385 bool known_dead) {
mostynbfbcdc27a2015-03-13 17:58:52386 DCHECK_CURRENTLY_ON(BrowserThread::IO);
Bo Liu0d2a2322018-04-19 00:18:09387 if (!child_process_) {
388 // If the delegate doesn't use Launch() helper.
389 ChildProcessTerminationInfo info;
Francois Doray884da022018-10-30 23:45:58390 info.status = base::GetTerminationStatus(data_.GetProcess().Handle(),
391 &info.exit_code);
Bo Liu0d2a2322018-04-19 00:18:09392 return info;
393 }
394 return child_process_->GetChildTerminationInfo(known_dead);
[email protected]d27893f62010-07-03 05:47:42395}
396
[email protected]4c01d4992012-01-23 23:33:01397bool BrowserChildProcessHostImpl::OnMessageReceived(
398 const IPC::Message& message) {
[email protected]4967f792012-01-20 22:14:40399 return delegate_->OnMessageReceived(message);
[email protected]4cb43102011-12-02 20:24:49400}
401
avib7348942015-12-25 20:57:10402void BrowserChildProcessHostImpl::OnChannelConnected(int32_t peer_pid) {
afakhry9cc79002015-10-22 19:25:15403 DCHECK_CURRENTLY_ON(BrowserThread::IO);
404
405 is_channel_connected_ = true;
amistry012289f2016-04-01 00:56:37406 notify_child_disconnected_ = true;
afakhry9cc79002015-10-22 19:25:15407
Robert Sesekde2e713262019-07-15 20:57:58408#if defined(OS_MACOSX)
409 ChildProcessTaskPortProvider::GetInstance()->OnChildProcessLaunched(
410 peer_pid, static_cast<ChildProcessHostImpl*>(child_process_host_.get())
411 ->child_control());
412#endif
413
[email protected]162a9fd2013-03-06 20:47:49414#if defined(OS_WIN)
415 // From this point onward, the exit of the child process is detected by an
416 // error on the IPC channel.
[email protected]162a9fd2013-03-06 20:47:49417 early_exit_watcher_.StopWatching();
418#endif
419
Eric Seckler8652dcd52018-09-20 10:42:28420 base::PostTaskWithTraits(
421 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26422 base::BindOnce(&NotifyProcessHostConnected, data_.Duplicate()));
[email protected]162a9fd2013-03-06 20:47:49423
[email protected]4967f792012-01-20 22:14:40424 delegate_->OnChannelConnected(peer_pid);
afakhry9cc79002015-10-22 19:25:15425
426 if (IsProcessLaunched()) {
Eric Seckler8652dcd52018-09-20 10:42:28427 base::PostTaskWithTraits(
428 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26429 base::BindOnce(&NotifyProcessLaunchedAndConnected, data_.Duplicate()));
afakhry9cc79002015-10-22 19:25:15430 }
[email protected]4967f792012-01-20 22:14:40431}
432
[email protected]4c01d4992012-01-23 23:33:01433void BrowserChildProcessHostImpl::OnChannelError() {
[email protected]4967f792012-01-20 22:14:40434 delegate_->OnChannelError();
[email protected]0d6e9bd2011-10-18 04:29:16435}
436
[email protected]ef2f6ba2014-05-15 23:06:07437void BrowserChildProcessHostImpl::OnBadMessageReceived(
438 const IPC::Message& message) {
Roman Karaseva43d5b4e2017-12-21 03:06:02439 std::string log_message = "Bad message received of type: ";
440 if (message.IsValid()) {
441 log_message += std::to_string(message.type());
442 } else {
443 log_message += "unknown";
444 }
sadrul73f39a62017-01-11 04:07:24445 TerminateOnBadMessageReceived(log_message);
jbauman278b2cc2016-01-08 22:48:53446}
447
sadrul73f39a62017-01-11 04:07:24448void BrowserChildProcessHostImpl::TerminateOnBadMessageReceived(
449 const std::string& error) {
Wei-Yin Chen (陳威尹)223326c2017-07-21 02:08:28450 HistogramBadMessageTerminated(static_cast<ProcessType>(data_.process_type));
avi83883c82014-12-23 00:08:49451 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]e44d1342014-05-16 21:29:33452 switches::kDisableKillAfterBadIPC)) {
453 return;
454 }
sadrul73f39a62017-01-11 04:07:24455 LOG(ERROR) << "Terminating child process for bad IPC message: " << error;
wfhaf32d0b2015-04-07 02:21:36456 // Create a memory dump. This will contain enough stack frames to work out
457 // what the bad message was.
458 base::debug::DumpWithoutCrashing();
459
Bo Liue06143912018-04-20 20:42:30460 child_process_->Terminate(RESULT_CODE_KILLED_BAD_MESSAGE);
[email protected]ef2f6ba2014-05-15 23:06:07461}
462
sadrul6c5aed8c2017-01-11 23:11:44463void BrowserChildProcessHostImpl::OnChannelInitialized(IPC::Channel* channel) {
464 channel_ = channel;
465}
466
[email protected]4c01d4992012-01-23 23:33:01467void BrowserChildProcessHostImpl::OnChildDisconnected() {
mostynbfbcdc27a2015-03-13 17:58:52468 DCHECK_CURRENTLY_ON(BrowserThread::IO);
jbauman319402e2014-10-21 17:52:21469#if defined(OS_WIN)
470 // OnChildDisconnected may be called without OnChannelConnected, so stop the
471 // early exit watcher so GetTerminationStatus can close the process handle.
472 early_exit_watcher_.StopWatching();
473#endif
Francois Doray884da022018-10-30 23:45:58474 if (child_process_.get() || data_.GetProcess().IsValid()) {
Bo Liu0d2a2322018-04-19 00:18:09475 ChildProcessTerminationInfo info =
476 GetTerminationInfo(true /* known_dead */);
Bo Liue9624612018-04-28 00:39:08477#if defined(OS_ANDROID)
478 delegate_->OnProcessCrashed(info.exit_code);
Eric Seckler8652dcd52018-09-20 10:42:28479 base::PostTaskWithTraits(
480 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26481 base::BindOnce(&NotifyProcessKilled, data_.Duplicate(), info));
Bo Liue9624612018-04-28 00:39:08482#else // OS_ANDROID
Bo Liu0d2a2322018-04-19 00:18:09483 switch (info.status) {
[email protected]6d057a0c2013-07-09 21:12:07484 case base::TERMINATION_STATUS_PROCESS_CRASHED:
485 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: {
Bo Liu0d2a2322018-04-19 00:18:09486 delegate_->OnProcessCrashed(info.exit_code);
Eric Seckler8652dcd52018-09-20 10:42:28487 base::PostTaskWithTraits(
488 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26489 base::BindOnce(&NotifyProcessCrashed, data_.Duplicate(), info));
[email protected]6d057a0c2013-07-09 21:12:07490 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2",
Wei-Yin Chen (陳威尹)3330991f2017-07-27 17:25:57491 static_cast<ProcessType>(data_.process_type),
[email protected]6d057a0c2013-07-09 21:12:07492 PROCESS_TYPE_MAX);
493 break;
494 }
oshima620225722015-06-04 19:45:27495#if defined(OS_CHROMEOS)
496 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
497#endif
[email protected]6d057a0c2013-07-09 21:12:07498 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: {
Bo Liu0d2a2322018-04-19 00:18:09499 delegate_->OnProcessCrashed(info.exit_code);
Eric Seckler8652dcd52018-09-20 10:42:28500 base::PostTaskWithTraits(
501 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26502 base::BindOnce(&NotifyProcessKilled, data_.Duplicate(), info));
[email protected]6d057a0c2013-07-09 21:12:07503 // Report that this child process was killed.
504 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2",
Wei-Yin Chen (陳威尹)3330991f2017-07-27 17:25:57505 static_cast<ProcessType>(data_.process_type),
[email protected]6d057a0c2013-07-09 21:12:07506 PROCESS_TYPE_MAX);
507 break;
508 }
509 case base::TERMINATION_STATUS_STILL_RUNNING: {
510 UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2",
Wei-Yin Chen (陳威尹)3330991f2017-07-27 17:25:57511 static_cast<ProcessType>(data_.process_type),
[email protected]6d057a0c2013-07-09 21:12:07512 PROCESS_TYPE_MAX);
Nico Weber9dbefa62018-01-27 01:01:42513 break;
[email protected]6d057a0c2013-07-09 21:12:07514 }
515 default:
516 break;
[email protected]12fed812011-09-16 05:47:29517 }
Bo Liue9624612018-04-28 00:39:08518#endif // OS_ANDROID
[email protected]6d057a0c2013-07-09 21:12:07519 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2",
Wei-Yin Chen (陳威尹)3330991f2017-07-27 17:25:57520 static_cast<ProcessType>(data_.process_type),
[email protected]6d057a0c2013-07-09 21:12:07521 PROCESS_TYPE_MAX);
oshima620225722015-06-04 19:45:27522#if defined(OS_CHROMEOS)
Bo Liu0d2a2322018-04-19 00:18:09523 if (info.status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM) {
oshima620225722015-06-04 19:45:27524 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2.OOM",
Wei-Yin Chen (陳威尹)3330991f2017-07-27 17:25:57525 static_cast<ProcessType>(data_.process_type),
oshima620225722015-06-04 19:45:27526 PROCESS_TYPE_MAX);
527 }
528#endif
[email protected]d27893f62010-07-03 05:47:42529 }
sadrul6c5aed8c2017-01-11 23:11:44530 channel_ = nullptr;
[email protected]4967f792012-01-20 22:14:40531 delete delegate_; // Will delete us
[email protected]a3a7e2c2011-09-16 23:07:05532}
533
[email protected]4c01d4992012-01-23 23:33:01534bool BrowserChildProcessHostImpl::Send(IPC::Message* message) {
[email protected]4cb43102011-12-02 20:24:49535 return child_process_host_->Send(message);
536}
537
bcwhiteb58a5fe2016-08-22 16:25:56538void BrowserChildProcessHostImpl::CreateMetricsAllocator() {
539 // Create a persistent memory segment for subprocess histograms only if
540 // they're active in the browser.
541 // TODO(bcwhite): Remove this once persistence is always enabled.
542 if (!base::GlobalHistogramAllocator::Get())
543 return;
544
545 // Determine the correct parameters based on the process type.
546 size_t memory_size;
547 base::StringPiece metrics_name;
548 switch (data_.process_type) {
bcwhite2496c83e2016-08-29 17:29:25549 case PROCESS_TYPE_UTILITY:
Matt Menkee5bc4a562017-08-07 23:58:22550 // This needs to be larger for the network service.
551 memory_size = 256 << 10; // 256 KiB
bcwhite2496c83e2016-08-29 17:29:25552 metrics_name = "UtilityMetrics";
553 break;
554
555 case PROCESS_TYPE_ZYGOTE:
bcwhite867f45532017-01-05 17:29:02556 memory_size = 64 << 10; // 64 KiB
bcwhite2496c83e2016-08-29 17:29:25557 metrics_name = "ZygoteMetrics";
558 break;
559
560 case PROCESS_TYPE_SANDBOX_HELPER:
bcwhite867f45532017-01-05 17:29:02561 memory_size = 64 << 10; // 64 KiB
bcwhite2496c83e2016-08-29 17:29:25562 metrics_name = "SandboxHelperMetrics";
563 break;
564
bcwhiteb58a5fe2016-08-22 16:25:56565 case PROCESS_TYPE_GPU:
Sadrul Habib Chowdhury2353ca522018-08-15 15:53:26566 // This needs to be larger for the display-compositor in the gpu process.
567 memory_size = 256 << 10; // 256 KiB
bcwhiteb58a5fe2016-08-22 16:25:56568 metrics_name = "GpuMetrics";
569 break;
570
bcwhite2496c83e2016-08-29 17:29:25571 case PROCESS_TYPE_PPAPI_PLUGIN:
bcwhite867f45532017-01-05 17:29:02572 memory_size = 64 << 10; // 64 KiB
bcwhite2496c83e2016-08-29 17:29:25573 metrics_name = "PpapiPluginMetrics";
574 break;
575
576 case PROCESS_TYPE_PPAPI_BROKER:
bcwhite867f45532017-01-05 17:29:02577 memory_size = 64 << 10; // 64 KiB
bcwhite2496c83e2016-08-29 17:29:25578 metrics_name = "PpapiBrokerMetrics";
579 break;
580
bcwhiteb58a5fe2016-08-22 16:25:56581 default:
582 return;
583 }
584
585 // Create the shared memory segment and attach an allocator to it.
586 // Mapping the memory shouldn't fail but be safe if it does; everything
587 // will continue to work but just as if persistence weren't available.
Alex Ilin30e7d062019-02-21 12:24:53588 base::WritableSharedMemoryRegion shm_region =
589 base::WritableSharedMemoryRegion::Create(memory_size);
590 base::WritableSharedMemoryMapping shm_mapping = shm_region.Map();
591 if (!shm_region.IsValid() || !shm_mapping.IsValid())
bcwhiteb58a5fe2016-08-22 16:25:56592 return;
Alex Ilin30e7d062019-02-21 12:24:53593 metrics_allocator_ =
594 std::make_unique<base::WritableSharedPersistentMemoryAllocator>(
595 std::move(shm_mapping), static_cast<uint64_t>(data_.id),
596 metrics_name);
597 metrics_shared_region_ = std::move(shm_region);
bcwhiteb58a5fe2016-08-22 16:25:56598}
599
600void BrowserChildProcessHostImpl::ShareMetricsAllocatorToProcess() {
601 if (metrics_allocator_) {
Nicholas Verne91bf4a632017-10-12 21:14:24602 HistogramController::GetInstance()->SetHistogramMemory<ChildProcessHost>(
Alex Ilin30e7d062019-02-21 12:24:53603 GetHost(), std::move(metrics_shared_region_));
Nicholas Verne91bf4a632017-10-12 21:14:24604 } else {
605 HistogramController::GetInstance()->SetHistogramMemory<ChildProcessHost>(
Alex Ilin30e7d062019-02-21 12:24:53606 GetHost(), base::WritableSharedMemoryRegion());
bcwhiteb58a5fe2016-08-22 16:25:56607 }
608}
609
wfh3adf87d2016-05-03 23:26:06610void BrowserChildProcessHostImpl::OnProcessLaunchFailed(int error_code) {
611 delegate_->OnProcessLaunchFailed(error_code);
amistry012289f2016-04-01 00:56:37612 notify_child_disconnected_ = false;
[email protected]fa01e472014-02-11 14:45:35613 delete delegate_; // Will delete us
614}
615
Min Qineb961902019-03-16 08:08:22616#if defined(OS_ANDROID)
617bool BrowserChildProcessHostImpl::CanUseWarmUpConnection() {
618 return can_use_warm_up_connection_;
619}
620#endif
621
[email protected]4c01d4992012-01-23 23:33:01622void BrowserChildProcessHostImpl::OnProcessLaunched() {
afakhry9cc79002015-10-22 19:25:15623 DCHECK_CURRENTLY_ON(BrowserThread::IO);
624
rvargas079d1842014-10-17 22:32:16625 const base::Process& process = child_process_->GetProcess();
626 DCHECK(process.IsValid());
[email protected]162a9fd2013-03-06 20:47:49627
rockotfe2fecc2017-01-19 20:16:19628 if (child_connection_)
David Bienvenuf47de5f12019-02-21 19:46:23629 child_connection_->SetProcess(process.Duplicate());
rockotfe2fecc2017-01-19 20:16:19630
jam3c8c50c2015-11-25 18:48:29631#if defined(OS_WIN)
[email protected]162a9fd2013-03-06 20:47:49632 // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the
633 // child process exits. This watcher is stopped once the IPC channel is
634 // connected and the exit of the child process is detecter by an error on the
635 // IPC channel thereafter.
rvargas079d1842014-10-17 22:32:16636 DCHECK(!early_exit_watcher_.GetWatchedObject());
jam7fab1082015-09-29 02:26:18637 early_exit_watcher_.StartWatchingOnce(process.Handle(), this);
[email protected]162a9fd2013-03-06 20:47:49638#endif
639
Francois Doray884da022018-10-30 23:45:58640 data_.SetProcess(process.Duplicate());
[email protected]4967f792012-01-20 22:14:40641 delegate_->OnProcessLaunched();
afakhry9cc79002015-10-22 19:25:15642
643 if (is_channel_connected_) {
Eric Seckler8652dcd52018-09-20 10:42:28644 base::PostTaskWithTraits(
645 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26646 base::BindOnce(&NotifyProcessLaunchedAndConnected, data_.Duplicate()));
afakhry9cc79002015-10-22 19:25:15647 }
648}
649
650bool BrowserChildProcessHostImpl::IsProcessLaunched() const {
651 DCHECK_CURRENTLY_ON(BrowserThread::IO);
652
653 return child_process_.get() && child_process_->GetProcess().IsValid();
[email protected]d27893f62010-07-03 05:47:42654}
[email protected]130757672012-10-24 00:26:19655
rockot229fb28e2016-06-16 04:46:16656// static
657void BrowserChildProcessHostImpl::OnMojoError(
658 base::WeakPtr<BrowserChildProcessHostImpl> process,
659 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
660 const std::string& error) {
661 if (!task_runner->BelongsToCurrentThread()) {
662 task_runner->PostTask(
tzik4fea24af2017-08-23 11:41:47663 FROM_HERE, base::BindOnce(&BrowserChildProcessHostImpl::OnMojoError,
664 process, task_runner, error));
olka91f1d412016-11-21 15:24:53665 return;
rockot229fb28e2016-06-16 04:46:16666 }
667 if (!process)
668 return;
Wei-Yin Chen (陳威尹)223326c2017-07-21 02:08:28669 HistogramBadMessageTerminated(
670 static_cast<ProcessType>(process->data_.process_type));
rockot229fb28e2016-06-16 04:46:16671 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
672 switches::kDisableKillAfterBadIPC)) {
673 return;
674 }
675 LOG(ERROR) << "Terminating child process for bad Mojo message: " << error;
676
Yuzhu Shen32fcad72017-09-14 23:12:37677 // Create a memory dump with the error message captured in a crash key value.
678 // This will make it easy to determine details about what interface call
679 // failed.
Robert Sesek1419427e2017-12-07 15:01:32680 base::debug::ScopedCrashKeyString scoped_error_key(
681 bad_message::GetMojoErrorCrashKey(), error);
rockot229fb28e2016-06-16 04:46:16682 base::debug::DumpWithoutCrashing();
Bo Liue06143912018-04-20 20:42:30683 process->child_process_->Terminate(RESULT_CODE_KILLED_BAD_MESSAGE);
rockot229fb28e2016-06-16 04:46:16684}
685
[email protected]162a9fd2013-03-06 20:47:49686#if defined(OS_WIN)
687
rvargas079d1842014-10-17 22:32:16688void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) {
[email protected]162a9fd2013-03-06 20:47:49689 OnChildDisconnected();
690}
691
692#endif
693
[email protected]130757672012-10-24 00:26:19694} // namespace content