blob: 23a010b1b0fb406ea32d737ce917aa0e1c8fd07f [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"
[email protected]3a85b1f2013-02-01 04:47:4037#include "content/browser/tracing/trace_message_filter.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"
ochang6a8400e2017-05-01 21:28:2454#include "services/service_manager/embedder/switches.h"
Ken Rockota86fa542018-11-13 21:47:1355#include "services/service_manager/public/cpp/constants.h"
[email protected]d27893f62010-07-03 05:47:4256
[email protected]f1675202012-07-09 15:18:0057#if defined(OS_MACOSX)
[email protected]458433c2012-02-29 23:43:3958#include "content/browser/mach_broker_mac.h"
[email protected]3838ca12011-11-02 14:37:1559#endif
[email protected]a3a7e2c2011-09-16 23:07:0560
[email protected]130757672012-10-24 00:26:1961namespace content {
[email protected]d27893f62010-07-03 05:47:4262namespace {
63
scottmg5e65e3a2017-03-08 08:48:4664static base::LazyInstance<
65 BrowserChildProcessHostImpl::BrowserChildProcessList>::DestructorAtExit
[email protected]4967f792012-01-20 22:14:4066 g_child_process_list = LAZY_INSTANCE_INITIALIZER;
[email protected]d27893f62010-07-03 05:47:4267
Trent Apteda250ec3ab2018-08-19 08:52:1968base::LazyInstance<base::ObserverList<BrowserChildProcessObserver>::Unchecked>::
Daniel Bratellbfcfe222017-10-17 17:57:0069 DestructorAtExit g_browser_child_process_observers =
70 LAZY_INSTANCE_INITIALIZER;
[email protected]f4eaf7b92013-02-28 22:00:4071
afakhry9cc79002015-10-22 19:25:1572void NotifyProcessLaunchedAndConnected(const ChildProcessData& data) {
Daniel Bratellbfcfe222017-10-17 17:57:0073 for (auto& observer : g_browser_child_process_observers.Get())
ericwilligers254597b2016-10-17 10:32:3174 observer.BrowserChildProcessLaunchedAndConnected(data);
afakhry9cc79002015-10-22 19:25:1575}
76
[email protected]f4eaf7b92013-02-28 22:00:4077void NotifyProcessHostConnected(const ChildProcessData& data) {
Daniel Bratellbfcfe222017-10-17 17:57:0078 for (auto& observer : g_browser_child_process_observers.Get())
ericwilligers254597b2016-10-17 10:32:3179 observer.BrowserChildProcessHostConnected(data);
[email protected]f4eaf7b92013-02-28 22:00:4080}
81
82void NotifyProcessHostDisconnected(const ChildProcessData& data) {
Daniel Bratellbfcfe222017-10-17 17:57:0083 for (auto& observer : g_browser_child_process_observers.Get())
ericwilligers254597b2016-10-17 10:32:3184 observer.BrowserChildProcessHostDisconnected(data);
[email protected]f4eaf7b92013-02-28 22:00:4085}
86
Bo Liue9624612018-04-28 00:39:0887#if !defined(OS_ANDROID)
Bo Liu2a489402018-04-24 23:41:2788void NotifyProcessCrashed(const ChildProcessData& data,
89 const ChildProcessTerminationInfo& info) {
Daniel Bratellbfcfe222017-10-17 17:57:0090 for (auto& observer : g_browser_child_process_observers.Get())
Bo Liu2a489402018-04-24 23:41:2791 observer.BrowserChildProcessCrashed(data, info);
[email protected]4306c3792011-12-02 01:57:5392}
Bo Liue9624612018-04-28 00:39:0893#endif
[email protected]d27893f62010-07-03 05:47:4294
Bo Liu2a489402018-04-24 23:41:2795void NotifyProcessKilled(const ChildProcessData& data,
96 const ChildProcessTerminationInfo& info) {
Daniel Bratellbfcfe222017-10-17 17:57:0097 for (auto& observer : g_browser_child_process_observers.Get())
Bo Liu2a489402018-04-24 23:41:2798 observer.BrowserChildProcessKilled(data, info);
wfhf0003292015-08-18 22:16:4699}
100
[email protected]d27893f62010-07-03 05:47:42101} // namespace
102
[email protected]4967f792012-01-20 22:14:40103BrowserChildProcessHost* BrowserChildProcessHost::Create(
vadimt379d7fe2015-04-01 00:09:35104 content::ProcessType process_type,
[email protected]4967f792012-01-20 22:14:40105 BrowserChildProcessHostDelegate* delegate) {
rockotda9887902016-08-19 20:46:42106 return Create(process_type, delegate, std::string());
amistry6ad1e812016-06-06 05:36:30107}
108
109BrowserChildProcessHost* BrowserChildProcessHost::Create(
110 content::ProcessType process_type,
111 BrowserChildProcessHostDelegate* delegate,
rockotda9887902016-08-19 20:46:42112 const std::string& service_name) {
113 return new BrowserChildProcessHostImpl(process_type, delegate, service_name);
[email protected]4967f792012-01-20 22:14:40114}
115
amistryfaa231a42015-05-20 01:49:12116BrowserChildProcessHost* BrowserChildProcessHost::FromID(int child_process_id) {
117 DCHECK_CURRENTLY_ON(BrowserThread::IO);
118 BrowserChildProcessHostImpl::BrowserChildProcessList* process_list =
119 g_child_process_list.Pointer();
120 for (BrowserChildProcessHostImpl* host : *process_list) {
121 if (host->GetData().id == child_process_id)
122 return host;
123 }
124 return nullptr;
125}
126
[email protected]458433c2012-02-29 23:43:39127#if defined(OS_MACOSX)
erikchencd3e0e7b2015-10-01 22:53:51128base::PortProvider* BrowserChildProcessHost::GetPortProvider() {
[email protected]458433c2012-02-29 23:43:39129 return MachBroker::GetInstance();
130}
131#endif
132
[email protected]f4eaf7b92013-02-28 22:00:40133// static
[email protected]4c01d4992012-01-23 23:33:01134BrowserChildProcessHostImpl::BrowserChildProcessList*
Maggie Chen47b8dc1d2018-11-10 00:12:34135BrowserChildProcessHostImpl::GetIterator() {
[email protected]4967f792012-01-20 22:14:40136 return g_child_process_list.Pointer();
137}
138
[email protected]f4eaf7b92013-02-28 22:00:40139// static
140void BrowserChildProcessHostImpl::AddObserver(
141 BrowserChildProcessObserver* observer) {
mostynbfbcdc27a2015-03-13 17:58:52142 DCHECK_CURRENTLY_ON(BrowserThread::UI);
Daniel Bratellbfcfe222017-10-17 17:57:00143 g_browser_child_process_observers.Get().AddObserver(observer);
[email protected]f4eaf7b92013-02-28 22:00:40144}
145
146// static
147void BrowserChildProcessHostImpl::RemoveObserver(
148 BrowserChildProcessObserver* observer) {
149 // TODO(phajdan.jr): Check thread after fixing http://crbug.com/167126.
Daniel Bratellbfcfe222017-10-17 17:57:00150 g_browser_child_process_observers.Get().RemoveObserver(observer);
[email protected]f4eaf7b92013-02-28 22:00:40151}
152
[email protected]4c01d4992012-01-23 23:33:01153BrowserChildProcessHostImpl::BrowserChildProcessHostImpl(
vadimt379d7fe2015-04-01 00:09:35154 content::ProcessType process_type,
amistry6ad1e812016-06-06 05:36:30155 BrowserChildProcessHostDelegate* delegate,
rockotda9887902016-08-19 20:46:42156 const std::string& service_name)
[email protected]f3b357692013-03-22 05:16:13157 : data_(process_type),
[email protected]9dd90152013-08-02 22:09:13158 delegate_(delegate),
sadrul6c5aed8c2017-01-11 23:11:44159 channel_(nullptr),
amistry012289f2016-04-01 00:56:37160 is_channel_connected_(false),
rockot229fb28e2016-06-16 04:46:16161 notify_child_disconnected_(false),
162 weak_factory_(this) {
[email protected]4734d0b2011-12-03 07:10:44163 data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId();
[email protected]4306c3792011-12-02 01:57:53164
Ayu Ishii75cc1062019-01-08 01:27:37165 child_process_host_ = ChildProcessHost::Create(this);
ssid72c5d6822015-06-26 15:51:42166 AddFilter(new TraceMessageFilter(data_.id));
[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
[email protected]4c01d4992012-01-23 23:33:01250const ChildProcessData& BrowserChildProcessHostImpl::GetData() const {
mostynbfbcdc27a2015-03-13 17:58:52251 DCHECK_CURRENTLY_ON(BrowserThread::IO);
[email protected]4967f792012-01-20 22:14:40252 return data_;
253}
254
[email protected]4c01d4992012-01-23 23:33:01255ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const {
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
rvargas5779b382014-11-18 20:44:11260const base::Process& BrowserChildProcessHostImpl::GetProcess() const {
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
bcwhiteb58a5fe2016-08-22 16:25:56269std::unique_ptr<base::SharedPersistentMemoryAllocator>
270BrowserChildProcessHostImpl::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[] = {
315 service_manager::switches::kDisableInProcessStackTraces,
Yue Ru Suna50a80f2019-02-08 05:02:58316 switches::kDisableBestEffortTasks,
Maggie Chen47b8dc1d2018-11-10 00:12:34317 switches::kDisableLogging,
318 switches::kEnableLogging,
Eric Seckler9b4153d2019-01-28 12:56:55319 switches::kEnablePerfetto,
Maggie Chen47b8dc1d2018-11-10 00:12:34320 switches::kIPCConnectionTimeout,
Greg Thompson54915832018-12-21 17:11:47321 switches::kLogFile,
Maggie Chen47b8dc1d2018-11-10 00:12:34322 switches::kLoggingLevel,
323 switches::kTraceToConsole,
324 switches::kV,
325 switches::kVModule,
326 };
327 cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches,
328 base::size(kForwardSwitches));
329
330 if (child_connection_) {
331 cmd_line->AppendSwitchASCII(
332 service_manager::switches::kServiceRequestChannelToken,
333 child_connection_->service_token());
334 }
335
336 // All processes should have a non-empty metrics name.
337 DCHECK(!data_.metrics_name.empty());
338
339 notify_child_disconnected_ = true;
340 child_process_.reset(new ChildProcessLauncher(
341 std::move(delegate), std::move(cmd_line), data_.id, this,
342 std::move(mojo_invitation_),
343 base::BindRepeating(&BrowserChildProcessHostImpl::OnMojoError,
344 weak_factory_.GetWeakPtr(),
345 base::ThreadTaskRunnerHandle::Get()),
346 terminate_on_shutdown));
347 ShareMetricsAllocatorToProcess();
348}
349
bena06f7d62017-04-04 20:48:52350void BrowserChildProcessHostImpl::BindInterface(
351 const std::string& interface_name,
352 mojo::ScopedMessagePipeHandle interface_pipe) {
rockotda9887902016-08-19 20:46:42353 DCHECK_CURRENTLY_ON(BrowserThread::IO);
354 if (!child_connection_)
bena06f7d62017-04-04 20:48:52355 return;
rockotda9887902016-08-19 20:46:42356
bena06f7d62017-04-04 20:48:52357 child_connection_->BindInterface(interface_name, std::move(interface_pipe));
rockotda9887902016-08-19 20:46:42358}
359
[email protected]ef2f6ba2014-05-15 23:06:07360void BrowserChildProcessHostImpl::HistogramBadMessageTerminated(
Wei-Yin Chen (陳威尹)223326c2017-07-21 02:08:28361 ProcessType process_type) {
[email protected]ef2f6ba2014-05-15 23:06:07362 UMA_HISTOGRAM_ENUMERATION("ChildProcess.BadMessgeTerminated", process_type,
363 PROCESS_TYPE_MAX);
364}
365
Bo Liu0d2a2322018-04-19 00:18:09366ChildProcessTerminationInfo BrowserChildProcessHostImpl::GetTerminationInfo(
367 bool known_dead) {
mostynbfbcdc27a2015-03-13 17:58:52368 DCHECK_CURRENTLY_ON(BrowserThread::IO);
Bo Liu0d2a2322018-04-19 00:18:09369 if (!child_process_) {
370 // If the delegate doesn't use Launch() helper.
371 ChildProcessTerminationInfo info;
Francois Doray884da022018-10-30 23:45:58372 info.status = base::GetTerminationStatus(data_.GetProcess().Handle(),
373 &info.exit_code);
Bo Liu0d2a2322018-04-19 00:18:09374 return info;
375 }
376 return child_process_->GetChildTerminationInfo(known_dead);
[email protected]d27893f62010-07-03 05:47:42377}
378
[email protected]4c01d4992012-01-23 23:33:01379bool BrowserChildProcessHostImpl::OnMessageReceived(
380 const IPC::Message& message) {
[email protected]4967f792012-01-20 22:14:40381 return delegate_->OnMessageReceived(message);
[email protected]4cb43102011-12-02 20:24:49382}
383
avib7348942015-12-25 20:57:10384void BrowserChildProcessHostImpl::OnChannelConnected(int32_t peer_pid) {
afakhry9cc79002015-10-22 19:25:15385 DCHECK_CURRENTLY_ON(BrowserThread::IO);
386
387 is_channel_connected_ = true;
amistry012289f2016-04-01 00:56:37388 notify_child_disconnected_ = true;
afakhry9cc79002015-10-22 19:25:15389
[email protected]162a9fd2013-03-06 20:47:49390#if defined(OS_WIN)
391 // From this point onward, the exit of the child process is detected by an
392 // error on the IPC channel.
[email protected]162a9fd2013-03-06 20:47:49393 early_exit_watcher_.StopWatching();
394#endif
395
Eric Seckler8652dcd52018-09-20 10:42:28396 base::PostTaskWithTraits(
397 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26398 base::BindOnce(&NotifyProcessHostConnected, data_.Duplicate()));
[email protected]162a9fd2013-03-06 20:47:49399
[email protected]4967f792012-01-20 22:14:40400 delegate_->OnChannelConnected(peer_pid);
afakhry9cc79002015-10-22 19:25:15401
402 if (IsProcessLaunched()) {
Eric Seckler8652dcd52018-09-20 10:42:28403 base::PostTaskWithTraits(
404 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26405 base::BindOnce(&NotifyProcessLaunchedAndConnected, data_.Duplicate()));
afakhry9cc79002015-10-22 19:25:15406 }
[email protected]4967f792012-01-20 22:14:40407}
408
[email protected]4c01d4992012-01-23 23:33:01409void BrowserChildProcessHostImpl::OnChannelError() {
[email protected]4967f792012-01-20 22:14:40410 delegate_->OnChannelError();
[email protected]0d6e9bd2011-10-18 04:29:16411}
412
[email protected]ef2f6ba2014-05-15 23:06:07413void BrowserChildProcessHostImpl::OnBadMessageReceived(
414 const IPC::Message& message) {
Roman Karaseva43d5b4e2017-12-21 03:06:02415 std::string log_message = "Bad message received of type: ";
416 if (message.IsValid()) {
417 log_message += std::to_string(message.type());
418 } else {
419 log_message += "unknown";
420 }
sadrul73f39a62017-01-11 04:07:24421 TerminateOnBadMessageReceived(log_message);
jbauman278b2cc2016-01-08 22:48:53422}
423
sadrul73f39a62017-01-11 04:07:24424void BrowserChildProcessHostImpl::TerminateOnBadMessageReceived(
425 const std::string& error) {
Wei-Yin Chen (陳威尹)223326c2017-07-21 02:08:28426 HistogramBadMessageTerminated(static_cast<ProcessType>(data_.process_type));
avi83883c82014-12-23 00:08:49427 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
[email protected]e44d1342014-05-16 21:29:33428 switches::kDisableKillAfterBadIPC)) {
429 return;
430 }
sadrul73f39a62017-01-11 04:07:24431 LOG(ERROR) << "Terminating child process for bad IPC message: " << error;
wfhaf32d0b2015-04-07 02:21:36432 // Create a memory dump. This will contain enough stack frames to work out
433 // what the bad message was.
434 base::debug::DumpWithoutCrashing();
435
Bo Liue06143912018-04-20 20:42:30436 child_process_->Terminate(RESULT_CODE_KILLED_BAD_MESSAGE);
[email protected]ef2f6ba2014-05-15 23:06:07437}
438
sadrul6c5aed8c2017-01-11 23:11:44439void BrowserChildProcessHostImpl::OnChannelInitialized(IPC::Channel* channel) {
440 channel_ = channel;
441}
442
[email protected]4c01d4992012-01-23 23:33:01443void BrowserChildProcessHostImpl::OnChildDisconnected() {
mostynbfbcdc27a2015-03-13 17:58:52444 DCHECK_CURRENTLY_ON(BrowserThread::IO);
jbauman319402e2014-10-21 17:52:21445#if defined(OS_WIN)
446 // OnChildDisconnected may be called without OnChannelConnected, so stop the
447 // early exit watcher so GetTerminationStatus can close the process handle.
448 early_exit_watcher_.StopWatching();
449#endif
Francois Doray884da022018-10-30 23:45:58450 if (child_process_.get() || data_.GetProcess().IsValid()) {
Bo Liu0d2a2322018-04-19 00:18:09451 ChildProcessTerminationInfo info =
452 GetTerminationInfo(true /* known_dead */);
Bo Liue9624612018-04-28 00:39:08453#if defined(OS_ANDROID)
454 delegate_->OnProcessCrashed(info.exit_code);
Eric Seckler8652dcd52018-09-20 10:42:28455 base::PostTaskWithTraits(
456 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26457 base::BindOnce(&NotifyProcessKilled, data_.Duplicate(), info));
Bo Liue9624612018-04-28 00:39:08458#else // OS_ANDROID
Bo Liu0d2a2322018-04-19 00:18:09459 switch (info.status) {
[email protected]6d057a0c2013-07-09 21:12:07460 case base::TERMINATION_STATUS_PROCESS_CRASHED:
461 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: {
Bo Liu0d2a2322018-04-19 00:18:09462 delegate_->OnProcessCrashed(info.exit_code);
Eric Seckler8652dcd52018-09-20 10:42:28463 base::PostTaskWithTraits(
464 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26465 base::BindOnce(&NotifyProcessCrashed, data_.Duplicate(), info));
[email protected]6d057a0c2013-07-09 21:12:07466 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2",
Wei-Yin Chen (陳威尹)3330991f2017-07-27 17:25:57467 static_cast<ProcessType>(data_.process_type),
[email protected]6d057a0c2013-07-09 21:12:07468 PROCESS_TYPE_MAX);
469 break;
470 }
oshima620225722015-06-04 19:45:27471#if defined(OS_CHROMEOS)
472 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
473#endif
[email protected]6d057a0c2013-07-09 21:12:07474 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: {
Bo Liu0d2a2322018-04-19 00:18:09475 delegate_->OnProcessCrashed(info.exit_code);
Eric Seckler8652dcd52018-09-20 10:42:28476 base::PostTaskWithTraits(
477 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26478 base::BindOnce(&NotifyProcessKilled, data_.Duplicate(), info));
[email protected]6d057a0c2013-07-09 21:12:07479 // Report that this child process was killed.
480 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2",
Wei-Yin Chen (陳威尹)3330991f2017-07-27 17:25:57481 static_cast<ProcessType>(data_.process_type),
[email protected]6d057a0c2013-07-09 21:12:07482 PROCESS_TYPE_MAX);
483 break;
484 }
485 case base::TERMINATION_STATUS_STILL_RUNNING: {
486 UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2",
Wei-Yin Chen (陳威尹)3330991f2017-07-27 17:25:57487 static_cast<ProcessType>(data_.process_type),
[email protected]6d057a0c2013-07-09 21:12:07488 PROCESS_TYPE_MAX);
Nico Weber9dbefa62018-01-27 01:01:42489 break;
[email protected]6d057a0c2013-07-09 21:12:07490 }
491 default:
492 break;
[email protected]12fed812011-09-16 05:47:29493 }
Bo Liue9624612018-04-28 00:39:08494#endif // OS_ANDROID
[email protected]6d057a0c2013-07-09 21:12:07495 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2",
Wei-Yin Chen (陳威尹)3330991f2017-07-27 17:25:57496 static_cast<ProcessType>(data_.process_type),
[email protected]6d057a0c2013-07-09 21:12:07497 PROCESS_TYPE_MAX);
oshima620225722015-06-04 19:45:27498#if defined(OS_CHROMEOS)
Bo Liu0d2a2322018-04-19 00:18:09499 if (info.status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM) {
oshima620225722015-06-04 19:45:27500 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2.OOM",
Wei-Yin Chen (陳威尹)3330991f2017-07-27 17:25:57501 static_cast<ProcessType>(data_.process_type),
oshima620225722015-06-04 19:45:27502 PROCESS_TYPE_MAX);
503 }
504#endif
[email protected]d27893f62010-07-03 05:47:42505 }
sadrul6c5aed8c2017-01-11 23:11:44506 channel_ = nullptr;
[email protected]4967f792012-01-20 22:14:40507 delete delegate_; // Will delete us
[email protected]a3a7e2c2011-09-16 23:07:05508}
509
[email protected]4c01d4992012-01-23 23:33:01510bool BrowserChildProcessHostImpl::Send(IPC::Message* message) {
[email protected]4cb43102011-12-02 20:24:49511 return child_process_host_->Send(message);
512}
513
bcwhiteb58a5fe2016-08-22 16:25:56514void BrowserChildProcessHostImpl::CreateMetricsAllocator() {
515 // Create a persistent memory segment for subprocess histograms only if
516 // they're active in the browser.
517 // TODO(bcwhite): Remove this once persistence is always enabled.
518 if (!base::GlobalHistogramAllocator::Get())
519 return;
520
521 // Determine the correct parameters based on the process type.
522 size_t memory_size;
523 base::StringPiece metrics_name;
524 switch (data_.process_type) {
bcwhite2496c83e2016-08-29 17:29:25525 case PROCESS_TYPE_UTILITY:
Matt Menkee5bc4a562017-08-07 23:58:22526 // This needs to be larger for the network service.
527 memory_size = 256 << 10; // 256 KiB
bcwhite2496c83e2016-08-29 17:29:25528 metrics_name = "UtilityMetrics";
529 break;
530
531 case PROCESS_TYPE_ZYGOTE:
bcwhite867f45532017-01-05 17:29:02532 memory_size = 64 << 10; // 64 KiB
bcwhite2496c83e2016-08-29 17:29:25533 metrics_name = "ZygoteMetrics";
534 break;
535
536 case PROCESS_TYPE_SANDBOX_HELPER:
bcwhite867f45532017-01-05 17:29:02537 memory_size = 64 << 10; // 64 KiB
bcwhite2496c83e2016-08-29 17:29:25538 metrics_name = "SandboxHelperMetrics";
539 break;
540
bcwhiteb58a5fe2016-08-22 16:25:56541 case PROCESS_TYPE_GPU:
Sadrul Habib Chowdhury2353ca522018-08-15 15:53:26542 // This needs to be larger for the display-compositor in the gpu process.
543 memory_size = 256 << 10; // 256 KiB
bcwhiteb58a5fe2016-08-22 16:25:56544 metrics_name = "GpuMetrics";
545 break;
546
bcwhite2496c83e2016-08-29 17:29:25547 case PROCESS_TYPE_PPAPI_PLUGIN:
bcwhite867f45532017-01-05 17:29:02548 memory_size = 64 << 10; // 64 KiB
bcwhite2496c83e2016-08-29 17:29:25549 metrics_name = "PpapiPluginMetrics";
550 break;
551
552 case PROCESS_TYPE_PPAPI_BROKER:
bcwhite867f45532017-01-05 17:29:02553 memory_size = 64 << 10; // 64 KiB
bcwhite2496c83e2016-08-29 17:29:25554 metrics_name = "PpapiBrokerMetrics";
555 break;
556
bcwhiteb58a5fe2016-08-22 16:25:56557 default:
bcwhitec600ac12017-07-12 13:37:13558 // Report new processes. "Custom" ones are renumbered to 1000+ so that
559 // they won't conflict with any standard ones in the future.
560 int process_type = data_.process_type;
561 if (process_type >= PROCESS_TYPE_CONTENT_END)
562 process_type += 1000 - PROCESS_TYPE_CONTENT_END;
Ilya Sherman1c811db2017-12-14 10:36:18563 base::UmaHistogramSparse(
bcwhitec600ac12017-07-12 13:37:13564 "UMA.SubprocessMetricsProvider.UntrackedProcesses", process_type);
bcwhiteb58a5fe2016-08-22 16:25:56565 return;
566 }
567
568 // Create the shared memory segment and attach an allocator to it.
569 // Mapping the memory shouldn't fail but be safe if it does; everything
570 // will continue to work but just as if persistence weren't available.
571 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory());
572 if (!shm->CreateAndMapAnonymous(memory_size))
573 return;
574 metrics_allocator_.reset(new base::SharedPersistentMemoryAllocator(
575 std::move(shm), static_cast<uint64_t>(data_.id), metrics_name,
576 /*readonly=*/false));
577}
578
579void BrowserChildProcessHostImpl::ShareMetricsAllocatorToProcess() {
580 if (metrics_allocator_) {
Nicholas Verne91bf4a632017-10-12 21:14:24581 HistogramController::GetInstance()->SetHistogramMemory<ChildProcessHost>(
582 GetHost(),
583 mojo::WrapSharedMemoryHandle(
584 metrics_allocator_->shared_memory()->handle().Duplicate(),
Ken Rockot673ce952018-01-18 20:59:24585 metrics_allocator_->shared_memory()->mapped_size(),
586 mojo::UnwrappedSharedMemoryHandleProtection::kReadWrite));
Nicholas Verne91bf4a632017-10-12 21:14:24587 } else {
588 HistogramController::GetInstance()->SetHistogramMemory<ChildProcessHost>(
589 GetHost(), mojo::ScopedSharedBufferHandle());
bcwhiteb58a5fe2016-08-22 16:25:56590 }
591}
592
wfh3adf87d2016-05-03 23:26:06593void BrowserChildProcessHostImpl::OnProcessLaunchFailed(int error_code) {
594 delegate_->OnProcessLaunchFailed(error_code);
amistry012289f2016-04-01 00:56:37595 notify_child_disconnected_ = false;
[email protected]fa01e472014-02-11 14:45:35596 delete delegate_; // Will delete us
597}
598
[email protected]4c01d4992012-01-23 23:33:01599void BrowserChildProcessHostImpl::OnProcessLaunched() {
afakhry9cc79002015-10-22 19:25:15600 DCHECK_CURRENTLY_ON(BrowserThread::IO);
601
rvargas079d1842014-10-17 22:32:16602 const base::Process& process = child_process_->GetProcess();
603 DCHECK(process.IsValid());
[email protected]162a9fd2013-03-06 20:47:49604
rockotfe2fecc2017-01-19 20:16:19605 if (child_connection_)
606 child_connection_->SetProcessHandle(process.Handle());
607
jam3c8c50c2015-11-25 18:48:29608#if defined(OS_WIN)
[email protected]162a9fd2013-03-06 20:47:49609 // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the
610 // child process exits. This watcher is stopped once the IPC channel is
611 // connected and the exit of the child process is detecter by an error on the
612 // IPC channel thereafter.
rvargas079d1842014-10-17 22:32:16613 DCHECK(!early_exit_watcher_.GetWatchedObject());
jam7fab1082015-09-29 02:26:18614 early_exit_watcher_.StartWatchingOnce(process.Handle(), this);
[email protected]162a9fd2013-03-06 20:47:49615#endif
616
Francois Doray884da022018-10-30 23:45:58617 data_.SetProcess(process.Duplicate());
[email protected]4967f792012-01-20 22:14:40618 delegate_->OnProcessLaunched();
afakhry9cc79002015-10-22 19:25:15619
620 if (is_channel_connected_) {
Eric Seckler8652dcd52018-09-20 10:42:28621 base::PostTaskWithTraits(
622 FROM_HERE, {BrowserThread::UI},
Bruce Dawson02f07de2018-07-31 17:28:26623 base::BindOnce(&NotifyProcessLaunchedAndConnected, data_.Duplicate()));
afakhry9cc79002015-10-22 19:25:15624 }
625}
626
627bool BrowserChildProcessHostImpl::IsProcessLaunched() const {
628 DCHECK_CURRENTLY_ON(BrowserThread::IO);
629
630 return child_process_.get() && child_process_->GetProcess().IsValid();
[email protected]d27893f62010-07-03 05:47:42631}
[email protected]130757672012-10-24 00:26:19632
rockot229fb28e2016-06-16 04:46:16633// static
634void BrowserChildProcessHostImpl::OnMojoError(
635 base::WeakPtr<BrowserChildProcessHostImpl> process,
636 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
637 const std::string& error) {
638 if (!task_runner->BelongsToCurrentThread()) {
639 task_runner->PostTask(
tzik4fea24af2017-08-23 11:41:47640 FROM_HERE, base::BindOnce(&BrowserChildProcessHostImpl::OnMojoError,
641 process, task_runner, error));
olka91f1d412016-11-21 15:24:53642 return;
rockot229fb28e2016-06-16 04:46:16643 }
644 if (!process)
645 return;
Wei-Yin Chen (陳威尹)223326c2017-07-21 02:08:28646 HistogramBadMessageTerminated(
647 static_cast<ProcessType>(process->data_.process_type));
rockot229fb28e2016-06-16 04:46:16648 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
649 switches::kDisableKillAfterBadIPC)) {
650 return;
651 }
652 LOG(ERROR) << "Terminating child process for bad Mojo message: " << error;
653
Yuzhu Shen32fcad72017-09-14 23:12:37654 // Create a memory dump with the error message captured in a crash key value.
655 // This will make it easy to determine details about what interface call
656 // failed.
Robert Sesek1419427e2017-12-07 15:01:32657 base::debug::ScopedCrashKeyString scoped_error_key(
658 bad_message::GetMojoErrorCrashKey(), error);
rockot229fb28e2016-06-16 04:46:16659 base::debug::DumpWithoutCrashing();
Bo Liue06143912018-04-20 20:42:30660 process->child_process_->Terminate(RESULT_CODE_KILLED_BAD_MESSAGE);
rockot229fb28e2016-06-16 04:46:16661}
662
[email protected]162a9fd2013-03-06 20:47:49663#if defined(OS_WIN)
664
rvargas079d1842014-10-17 22:32:16665void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) {
[email protected]162a9fd2013-03-06 20:47:49666 OnChildDisconnected();
667}
668
669#endif
670
[email protected]130757672012-10-24 00:26:19671} // namespace content