[email protected] | 02798a98 | 2012-01-27 00:45:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 5 | #include "content/browser/browser_child_process_host_impl.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 6 | |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 7 | #include "base/base_switches.h" |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 8 | #include "base/bind.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 9 | #include "base/command_line.h" |
Yuzhu Shen | 32fcad7 | 2017-09-14 23:12:37 | [diff] [blame] | 10 | #include "base/debug/crash_logging.h" |
wfh | af32d0b | 2015-04-07 02:21:36 | [diff] [blame] | 11 | #include "base/debug/dump_without_crashing.h" |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 12 | #include "base/feature_list.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 14 | #include "base/lazy_instance.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 15 | #include "base/logging.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 16 | #include "base/macros.h" |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 17 | #include "base/metrics/field_trial.h" |
Ilya Sherman | 1c811db | 2017-12-14 10:36:18 | [diff] [blame] | 18 | #include "base/metrics/histogram_functions.h" |
asvitkine | 3033081 | 2016-08-30 04:01:08 | [diff] [blame] | 19 | #include "base/metrics/histogram_macros.h" |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 20 | #include "base/metrics/persistent_histogram_allocator.h" |
| 21 | #include "base/metrics/persistent_memory_allocator.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 22 | #include "base/stl_util.h" |
[email protected] | 10994d13 | 2013-06-11 07:16:18 | [diff] [blame] | 23 | #include "base/strings/string_util.h" |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 24 | #include "base/strings/stringprintf.h" |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 25 | #include "base/synchronization/waitable_event.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 26 | #include "base/task/post_task.h" |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 27 | #include "base/threading/thread_task_runner_handle.h" |
Ken Rockot | 315def7 | 2018-11-10 02:16:10 | [diff] [blame] | 28 | #include "base/token.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 29 | #include "build/build_config.h" |
Ehsan Chiniforooshan | def633e0 | 2018-04-24 02:05:02 | [diff] [blame] | 30 | #include "components/tracing/common/trace_startup_config.h" |
primiano | ccb26c6 | 2016-06-01 21:50:02 | [diff] [blame] | 31 | #include "components/tracing/common/tracing_switches.h" |
Robert Sesek | 1419427e | 2017-12-07 15:01:32 | [diff] [blame] | 32 | #include "content/browser/bad_message.h" |
Ehsan Chiniforooshan | f192e8a2 | 2018-03-27 03:18:20 | [diff] [blame] | 33 | #include "content/browser/browser_main_loop.h" |
Nicholas Verne | 91bf4a63 | 2017-10-12 21:14:24 | [diff] [blame] | 34 | #include "content/browser/histogram_controller.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 35 | #include "content/browser/loader/resource_message_filter.h" |
ben | ff46163 | 2016-10-06 14:27:33 | [diff] [blame] | 36 | #include "content/browser/service_manager/service_manager_context.h" |
[email protected] | 3a85b1f | 2013-02-01 04:47:40 | [diff] [blame] | 37 | #include "content/browser/tracing/trace_message_filter.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 38 | #include "content/common/child_process_host_impl.h" |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 39 | #include "content/common/service_manager/child_connection.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 40 | #include "content/public/browser/browser_child_process_host_delegate.h" |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 41 | #include "content/public/browser/browser_child_process_observer.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 42 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 43 | #include "content/public/browser/browser_thread.h" |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 44 | #include "content/public/browser/child_process_data.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 45 | #include "content/public/browser/content_browser_client.h" |
ke.he | 19b5e6a | 2016-10-27 08:47:09 | [diff] [blame] | 46 | #include "content/public/common/connection_filter.h" |
lawrencewu | 2c93082 | 2016-10-13 13:54:25 | [diff] [blame] | 47 | #include "content/public/common/content_features.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 48 | #include "content/public/common/content_switches.h" |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 49 | #include "content/public/common/process_type.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 50 | #include "content/public/common/result_codes.h" |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 51 | #include "content/public/common/sandboxed_process_launcher_delegate.h" |
ke.he | 19b5e6a | 2016-10-27 08:47:09 | [diff] [blame] | 52 | #include "content/public/common/service_manager_connection.h" |
Nicholas Verne | 91bf4a63 | 2017-10-12 21:14:24 | [diff] [blame] | 53 | #include "mojo/public/cpp/system/platform_handle.h" |
ochang | 6a8400e | 2017-05-01 21:28:24 | [diff] [blame] | 54 | #include "services/service_manager/embedder/switches.h" |
Ken Rockot | a86fa54 | 2018-11-13 21:47:13 | [diff] [blame] | 55 | #include "services/service_manager/public/cpp/constants.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 56 | |
[email protected] | f167520 | 2012-07-09 15:18:00 | [diff] [blame] | 57 | #if defined(OS_MACOSX) |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 58 | #include "content/browser/mach_broker_mac.h" |
[email protected] | 3838ca1 | 2011-11-02 14:37:15 | [diff] [blame] | 59 | #endif |
[email protected] | a3a7e2c | 2011-09-16 23:07:05 | [diff] [blame] | 60 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 61 | namespace content { |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 62 | namespace { |
| 63 | |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 64 | static base::LazyInstance< |
| 65 | BrowserChildProcessHostImpl::BrowserChildProcessList>::DestructorAtExit |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 66 | g_child_process_list = LAZY_INSTANCE_INITIALIZER; |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 67 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 68 | base::LazyInstance<base::ObserverList<BrowserChildProcessObserver>::Unchecked>:: |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 69 | DestructorAtExit g_browser_child_process_observers = |
| 70 | LAZY_INSTANCE_INITIALIZER; |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 71 | |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 72 | void NotifyProcessLaunchedAndConnected(const ChildProcessData& data) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 73 | for (auto& observer : g_browser_child_process_observers.Get()) |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 74 | observer.BrowserChildProcessLaunchedAndConnected(data); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 77 | void NotifyProcessHostConnected(const ChildProcessData& data) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 78 | for (auto& observer : g_browser_child_process_observers.Get()) |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 79 | observer.BrowserChildProcessHostConnected(data); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void NotifyProcessHostDisconnected(const ChildProcessData& data) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 83 | for (auto& observer : g_browser_child_process_observers.Get()) |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 84 | observer.BrowserChildProcessHostDisconnected(data); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 85 | } |
| 86 | |
Bo Liu | e962461 | 2018-04-28 00:39:08 | [diff] [blame] | 87 | #if !defined(OS_ANDROID) |
Bo Liu | 2a48940 | 2018-04-24 23:41:27 | [diff] [blame] | 88 | void NotifyProcessCrashed(const ChildProcessData& data, |
| 89 | const ChildProcessTerminationInfo& info) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 90 | for (auto& observer : g_browser_child_process_observers.Get()) |
Bo Liu | 2a48940 | 2018-04-24 23:41:27 | [diff] [blame] | 91 | observer.BrowserChildProcessCrashed(data, info); |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 92 | } |
Bo Liu | e962461 | 2018-04-28 00:39:08 | [diff] [blame] | 93 | #endif |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 94 | |
Bo Liu | 2a48940 | 2018-04-24 23:41:27 | [diff] [blame] | 95 | void NotifyProcessKilled(const ChildProcessData& data, |
| 96 | const ChildProcessTerminationInfo& info) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 97 | for (auto& observer : g_browser_child_process_observers.Get()) |
Bo Liu | 2a48940 | 2018-04-24 23:41:27 | [diff] [blame] | 98 | observer.BrowserChildProcessKilled(data, info); |
wfh | f000329 | 2015-08-18 22:16:46 | [diff] [blame] | 99 | } |
| 100 | |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 101 | } // namespace |
| 102 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 103 | BrowserChildProcessHost* BrowserChildProcessHost::Create( |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 104 | content::ProcessType process_type, |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 105 | BrowserChildProcessHostDelegate* delegate) { |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 106 | return Create(process_type, delegate, std::string()); |
amistry | 6ad1e81 | 2016-06-06 05:36:30 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | BrowserChildProcessHost* BrowserChildProcessHost::Create( |
| 110 | content::ProcessType process_type, |
| 111 | BrowserChildProcessHostDelegate* delegate, |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 112 | const std::string& service_name) { |
| 113 | return new BrowserChildProcessHostImpl(process_type, delegate, service_name); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 114 | } |
| 115 | |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 116 | BrowserChildProcessHost* 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] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 127 | #if defined(OS_MACOSX) |
erikchen | cd3e0e7b | 2015-10-01 22:53:51 | [diff] [blame] | 128 | base::PortProvider* BrowserChildProcessHost::GetPortProvider() { |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 129 | return MachBroker::GetInstance(); |
| 130 | } |
| 131 | #endif |
| 132 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 133 | // static |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 134 | BrowserChildProcessHostImpl::BrowserChildProcessList* |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 135 | BrowserChildProcessHostImpl::GetIterator() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 136 | return g_child_process_list.Pointer(); |
| 137 | } |
| 138 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 139 | // static |
| 140 | void BrowserChildProcessHostImpl::AddObserver( |
| 141 | BrowserChildProcessObserver* observer) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 142 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 143 | g_browser_child_process_observers.Get().AddObserver(observer); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | // static |
| 147 | void BrowserChildProcessHostImpl::RemoveObserver( |
| 148 | BrowserChildProcessObserver* observer) { |
| 149 | // TODO(phajdan.jr): Check thread after fixing http://crbug.com/167126. |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 150 | g_browser_child_process_observers.Get().RemoveObserver(observer); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 151 | } |
| 152 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 153 | BrowserChildProcessHostImpl::BrowserChildProcessHostImpl( |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 154 | content::ProcessType process_type, |
amistry | 6ad1e81 | 2016-06-06 05:36:30 | [diff] [blame] | 155 | BrowserChildProcessHostDelegate* delegate, |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 156 | const std::string& service_name) |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 157 | : data_(process_type), |
[email protected] | 9dd9015 | 2013-08-02 22:09:13 | [diff] [blame] | 158 | delegate_(delegate), |
sadrul | 6c5aed8c | 2017-01-11 23:11:44 | [diff] [blame] | 159 | channel_(nullptr), |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 160 | is_channel_connected_(false), |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 161 | notify_child_disconnected_(false), |
| 162 | weak_factory_(this) { |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 163 | data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId(); |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 164 | |
Ayu Ishii | 75cc106 | 2019-01-08 01:27:37 | [diff] [blame] | 165 | child_process_host_ = ChildProcessHost::Create(this); |
ssid | 72c5d682 | 2015-06-26 15:51:42 | [diff] [blame] | 166 | AddFilter(new TraceMessageFilter(data_.id)); |
[email protected] | 38917fe | 2011-04-06 17:29:12 | [diff] [blame] | 167 | |
| 168 | g_child_process_list.Get().push_back(this); |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 169 | GetContentClient()->browser()->BrowserChildProcessHostCreated(this); |
[email protected] | 30c4c67a | 2014-07-22 02:33:55 | [diff] [blame] | 170 | |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 171 | if (!service_name.empty()) { |
| 172 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
Ken Rockot | a86fa54 | 2018-11-13 21:47:13 | [diff] [blame] | 173 | 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 Harris | edb011f | 2018-06-01 20:28:44 | [diff] [blame] | 179 | data_.metrics_name = service_name; |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 180 | } |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 181 | |
| 182 | // Create a persistent memory segment for subprocess histograms. |
| 183 | CreateMetricsAllocator(); |
[email protected] | 38917fe | 2011-04-06 17:29:12 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 186 | BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() { |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 187 | g_child_process_list.Get().remove(this); |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 188 | |
| 189 | if (notify_child_disconnected_) { |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 190 | base::PostTaskWithTraits( |
| 191 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 192 | base::BindOnce(&NotifyProcessHostDisconnected, data_.Duplicate())); |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 193 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // static |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 197 | void BrowserChildProcessHostImpl::TerminateAll() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 198 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 199 | // Make a copy since the BrowserChildProcessHost dtor mutates the original |
| 200 | // list. |
| 201 | BrowserChildProcessList copy = g_child_process_list.Get(); |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 202 | for (auto it = copy.begin(); it != copy.end(); ++it) { |
[email protected] | 5998d9d | 2012-03-06 04:25:26 | [diff] [blame] | 203 | delete (*it)->delegate(); // ~*HostDelegate deletes *HostImpl. |
| 204 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 205 | } |
| 206 | |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 207 | // static |
lawrencewu | 0b496492 | 2016-10-25 16:28:14 | [diff] [blame] | 208 | void BrowserChildProcessHostImpl::CopyFeatureAndFieldTrialFlags( |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 209 | base::CommandLine* cmd_line) { |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 210 | // 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. |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 212 | base::FieldTrialList::CopyFieldTrialStateToFlags( |
| 213 | switches::kFieldTrialHandle, switches::kEnableFeatures, |
| 214 | switches::kDisableFeatures, cmd_line); |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 215 | } |
| 216 | |
Ehsan Chiniforooshan | f192e8a2 | 2018-03-27 03:18:20 | [diff] [blame] | 217 | // static |
| 218 | void BrowserChildProcessHostImpl::CopyTraceStartupFlags( |
| 219 | base::CommandLine* cmd_line) { |
Ehsan Chiniforooshan | def633e0 | 2018-04-24 02:05:02 | [diff] [blame] | 220 | if (tracing::TraceStartupConfig::GetInstance()->IsEnabled()) { |
Ehsan Chiniforooshan | f192e8a2 | 2018-03-27 03:18:20 | [diff] [blame] | 221 | const auto trace_config = |
Ehsan Chiniforooshan | def633e0 | 2018-04-24 02:05:02 | [diff] [blame] | 222 | tracing::TraceStartupConfig::GetInstance()->GetTraceConfig(); |
Ehsan Chiniforooshan | f192e8a2 | 2018-03-27 03:18:20 | [diff] [blame] | 223 | 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] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 240 | void BrowserChildProcessHostImpl::Launch( |
jcivelli | d20cc0b | 2016-12-22 03:51:41 | [diff] [blame] | 241 | std::unique_ptr<SandboxedProcessLauncherDelegate> delegate, |
| 242 | std::unique_ptr<base::CommandLine> cmd_line, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 243 | bool terminate_on_shutdown) { |
jcivelli | d20cc0b | 2016-12-22 03:51:41 | [diff] [blame] | 244 | GetContentClient()->browser()->AppendExtraCommandLineSwitches(cmd_line.get(), |
| 245 | data_.id); |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 246 | LaunchWithoutExtraCommandLineSwitches( |
| 247 | std::move(delegate), std::move(cmd_line), terminate_on_shutdown); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 248 | } |
| 249 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 250 | const ChildProcessData& BrowserChildProcessHostImpl::GetData() const { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 251 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 252 | return data_; |
| 253 | } |
| 254 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 255 | ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 256 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 257 | return child_process_host_.get(); |
| 258 | } |
| 259 | |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 260 | const base::Process& BrowserChildProcessHostImpl::GetProcess() const { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 261 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 262 | DCHECK(child_process_.get()) |
| 263 | << "Requesting a child process handle before launching."; |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 264 | DCHECK(child_process_->GetProcess().IsValid()) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 265 | << "Requesting a child process handle before launch has completed OK."; |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 266 | return child_process_->GetProcess(); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 267 | } |
| 268 | |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 269 | std::unique_ptr<base::SharedPersistentMemoryAllocator> |
| 270 | BrowserChildProcessHostImpl::TakeMetricsAllocator() { |
| 271 | return std::move(metrics_allocator_); |
| 272 | } |
| 273 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 274 | void BrowserChildProcessHostImpl::SetName(const base::string16& name) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 275 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 276 | data_.name = name; |
| 277 | } |
| 278 | |
Will Harris | edb011f | 2018-06-01 20:28:44 | [diff] [blame] | 279 | void BrowserChildProcessHostImpl::SetMetricsName( |
| 280 | const std::string& metrics_name) { |
| 281 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 282 | data_.metrics_name = metrics_name; |
| 283 | } |
| 284 | |
Francois Doray | 884da02 | 2018-10-30 23:45:58 | [diff] [blame] | 285 | void BrowserChildProcessHostImpl::SetProcess(base::Process process) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 286 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
Francois Doray | 884da02 | 2018-10-30 23:45:58 | [diff] [blame] | 287 | data_.SetProcess(std::move(process)); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 288 | } |
| 289 | |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 290 | service_manager::mojom::ServiceRequest |
| 291 | BrowserChildProcessHostImpl::TakeInProcessServiceRequest() { |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 292 | auto invitation = std::move(mojo_invitation_); |
Ken Rockot | f4d8a94 | 2017-05-13 00:10:37 | [diff] [blame] | 293 | return service_manager::mojom::ServiceRequest( |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 294 | invitation.ExtractMessagePipe(child_connection_->service_token())); |
sammc | 2b0375b | 2016-12-22 01:26:17 | [diff] [blame] | 295 | } |
| 296 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 297 | void BrowserChildProcessHostImpl::ForceShutdown() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 298 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 299 | g_child_process_list.Get().remove(this); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 300 | child_process_host_->ForceShutdown(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 301 | } |
| 302 | |
[email protected] | ba780c1 | 2013-10-01 17:07:06 | [diff] [blame] | 303 | void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter* filter) { |
| 304 | child_process_host_->AddFilter(filter->GetFilter()); |
| 305 | } |
| 306 | |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 307 | void 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 Sun | a50a80f | 2019-02-08 05:02:58 | [diff] [blame^] | 316 | switches::kDisableBestEffortTasks, |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 317 | switches::kDisableLogging, |
| 318 | switches::kEnableLogging, |
Eric Seckler | 9b4153d | 2019-01-28 12:56:55 | [diff] [blame] | 319 | switches::kEnablePerfetto, |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 320 | switches::kIPCConnectionTimeout, |
Greg Thompson | 5491583 | 2018-12-21 17:11:47 | [diff] [blame] | 321 | switches::kLogFile, |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 322 | 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 | |
ben | a06f7d6 | 2017-04-04 20:48:52 | [diff] [blame] | 350 | void BrowserChildProcessHostImpl::BindInterface( |
| 351 | const std::string& interface_name, |
| 352 | mojo::ScopedMessagePipeHandle interface_pipe) { |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 353 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 354 | if (!child_connection_) |
ben | a06f7d6 | 2017-04-04 20:48:52 | [diff] [blame] | 355 | return; |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 356 | |
ben | a06f7d6 | 2017-04-04 20:48:52 | [diff] [blame] | 357 | child_connection_->BindInterface(interface_name, std::move(interface_pipe)); |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 358 | } |
| 359 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 360 | void BrowserChildProcessHostImpl::HistogramBadMessageTerminated( |
Wei-Yin Chen (陳威尹) | 223326c | 2017-07-21 02:08:28 | [diff] [blame] | 361 | ProcessType process_type) { |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 362 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.BadMessgeTerminated", process_type, |
| 363 | PROCESS_TYPE_MAX); |
| 364 | } |
| 365 | |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 366 | ChildProcessTerminationInfo BrowserChildProcessHostImpl::GetTerminationInfo( |
| 367 | bool known_dead) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 368 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 369 | if (!child_process_) { |
| 370 | // If the delegate doesn't use Launch() helper. |
| 371 | ChildProcessTerminationInfo info; |
Francois Doray | 884da02 | 2018-10-30 23:45:58 | [diff] [blame] | 372 | info.status = base::GetTerminationStatus(data_.GetProcess().Handle(), |
| 373 | &info.exit_code); |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 374 | return info; |
| 375 | } |
| 376 | return child_process_->GetChildTerminationInfo(known_dead); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 377 | } |
| 378 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 379 | bool BrowserChildProcessHostImpl::OnMessageReceived( |
| 380 | const IPC::Message& message) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 381 | return delegate_->OnMessageReceived(message); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 382 | } |
| 383 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 384 | void BrowserChildProcessHostImpl::OnChannelConnected(int32_t peer_pid) { |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 385 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 386 | |
| 387 | is_channel_connected_ = true; |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 388 | notify_child_disconnected_ = true; |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 389 | |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 390 | #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] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 393 | early_exit_watcher_.StopWatching(); |
| 394 | #endif |
| 395 | |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 396 | base::PostTaskWithTraits( |
| 397 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 398 | base::BindOnce(&NotifyProcessHostConnected, data_.Duplicate())); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 399 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 400 | delegate_->OnChannelConnected(peer_pid); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 401 | |
| 402 | if (IsProcessLaunched()) { |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 403 | base::PostTaskWithTraits( |
| 404 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 405 | base::BindOnce(&NotifyProcessLaunchedAndConnected, data_.Duplicate())); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 406 | } |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 407 | } |
| 408 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 409 | void BrowserChildProcessHostImpl::OnChannelError() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 410 | delegate_->OnChannelError(); |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 411 | } |
| 412 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 413 | void BrowserChildProcessHostImpl::OnBadMessageReceived( |
| 414 | const IPC::Message& message) { |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame] | 415 | 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 | } |
sadrul | 73f39a6 | 2017-01-11 04:07:24 | [diff] [blame] | 421 | TerminateOnBadMessageReceived(log_message); |
jbauman | 278b2cc | 2016-01-08 22:48:53 | [diff] [blame] | 422 | } |
| 423 | |
sadrul | 73f39a6 | 2017-01-11 04:07:24 | [diff] [blame] | 424 | void BrowserChildProcessHostImpl::TerminateOnBadMessageReceived( |
| 425 | const std::string& error) { |
Wei-Yin Chen (陳威尹) | 223326c | 2017-07-21 02:08:28 | [diff] [blame] | 426 | HistogramBadMessageTerminated(static_cast<ProcessType>(data_.process_type)); |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 427 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | e44d134 | 2014-05-16 21:29:33 | [diff] [blame] | 428 | switches::kDisableKillAfterBadIPC)) { |
| 429 | return; |
| 430 | } |
sadrul | 73f39a6 | 2017-01-11 04:07:24 | [diff] [blame] | 431 | LOG(ERROR) << "Terminating child process for bad IPC message: " << error; |
wfh | af32d0b | 2015-04-07 02:21:36 | [diff] [blame] | 432 | // 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 Liu | e0614391 | 2018-04-20 20:42:30 | [diff] [blame] | 436 | child_process_->Terminate(RESULT_CODE_KILLED_BAD_MESSAGE); |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 437 | } |
| 438 | |
sadrul | 6c5aed8c | 2017-01-11 23:11:44 | [diff] [blame] | 439 | void BrowserChildProcessHostImpl::OnChannelInitialized(IPC::Channel* channel) { |
| 440 | channel_ = channel; |
| 441 | } |
| 442 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 443 | void BrowserChildProcessHostImpl::OnChildDisconnected() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 444 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
jbauman | 319402e | 2014-10-21 17:52:21 | [diff] [blame] | 445 | #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 Doray | 884da02 | 2018-10-30 23:45:58 | [diff] [blame] | 450 | if (child_process_.get() || data_.GetProcess().IsValid()) { |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 451 | ChildProcessTerminationInfo info = |
| 452 | GetTerminationInfo(true /* known_dead */); |
Bo Liu | e962461 | 2018-04-28 00:39:08 | [diff] [blame] | 453 | #if defined(OS_ANDROID) |
| 454 | delegate_->OnProcessCrashed(info.exit_code); |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 455 | base::PostTaskWithTraits( |
| 456 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 457 | base::BindOnce(&NotifyProcessKilled, data_.Duplicate(), info)); |
Bo Liu | e962461 | 2018-04-28 00:39:08 | [diff] [blame] | 458 | #else // OS_ANDROID |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 459 | switch (info.status) { |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 460 | case base::TERMINATION_STATUS_PROCESS_CRASHED: |
| 461 | case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: { |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 462 | delegate_->OnProcessCrashed(info.exit_code); |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 463 | base::PostTaskWithTraits( |
| 464 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 465 | base::BindOnce(&NotifyProcessCrashed, data_.Duplicate(), info)); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 466 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 467 | static_cast<ProcessType>(data_.process_type), |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 468 | PROCESS_TYPE_MAX); |
| 469 | break; |
| 470 | } |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 471 | #if defined(OS_CHROMEOS) |
| 472 | case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM: |
| 473 | #endif |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 474 | case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: { |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 475 | delegate_->OnProcessCrashed(info.exit_code); |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 476 | base::PostTaskWithTraits( |
| 477 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 478 | base::BindOnce(&NotifyProcessKilled, data_.Duplicate(), info)); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 479 | // Report that this child process was killed. |
| 480 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 481 | static_cast<ProcessType>(data_.process_type), |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 482 | PROCESS_TYPE_MAX); |
| 483 | break; |
| 484 | } |
| 485 | case base::TERMINATION_STATUS_STILL_RUNNING: { |
| 486 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 487 | static_cast<ProcessType>(data_.process_type), |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 488 | PROCESS_TYPE_MAX); |
Nico Weber | 9dbefa6 | 2018-01-27 01:01:42 | [diff] [blame] | 489 | break; |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 490 | } |
| 491 | default: |
| 492 | break; |
[email protected] | 12fed81 | 2011-09-16 05:47:29 | [diff] [blame] | 493 | } |
Bo Liu | e962461 | 2018-04-28 00:39:08 | [diff] [blame] | 494 | #endif // OS_ANDROID |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 495 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 496 | static_cast<ProcessType>(data_.process_type), |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 497 | PROCESS_TYPE_MAX); |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 498 | #if defined(OS_CHROMEOS) |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 499 | if (info.status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM) { |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 500 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2.OOM", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 501 | static_cast<ProcessType>(data_.process_type), |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 502 | PROCESS_TYPE_MAX); |
| 503 | } |
| 504 | #endif |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 505 | } |
sadrul | 6c5aed8c | 2017-01-11 23:11:44 | [diff] [blame] | 506 | channel_ = nullptr; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 507 | delete delegate_; // Will delete us |
[email protected] | a3a7e2c | 2011-09-16 23:07:05 | [diff] [blame] | 508 | } |
| 509 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 510 | bool BrowserChildProcessHostImpl::Send(IPC::Message* message) { |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 511 | return child_process_host_->Send(message); |
| 512 | } |
| 513 | |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 514 | void 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) { |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 525 | case PROCESS_TYPE_UTILITY: |
Matt Menke | e5bc4a56 | 2017-08-07 23:58:22 | [diff] [blame] | 526 | // This needs to be larger for the network service. |
| 527 | memory_size = 256 << 10; // 256 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 528 | metrics_name = "UtilityMetrics"; |
| 529 | break; |
| 530 | |
| 531 | case PROCESS_TYPE_ZYGOTE: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 532 | memory_size = 64 << 10; // 64 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 533 | metrics_name = "ZygoteMetrics"; |
| 534 | break; |
| 535 | |
| 536 | case PROCESS_TYPE_SANDBOX_HELPER: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 537 | memory_size = 64 << 10; // 64 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 538 | metrics_name = "SandboxHelperMetrics"; |
| 539 | break; |
| 540 | |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 541 | case PROCESS_TYPE_GPU: |
Sadrul Habib Chowdhury | 2353ca52 | 2018-08-15 15:53:26 | [diff] [blame] | 542 | // This needs to be larger for the display-compositor in the gpu process. |
| 543 | memory_size = 256 << 10; // 256 KiB |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 544 | metrics_name = "GpuMetrics"; |
| 545 | break; |
| 546 | |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 547 | case PROCESS_TYPE_PPAPI_PLUGIN: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 548 | memory_size = 64 << 10; // 64 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 549 | metrics_name = "PpapiPluginMetrics"; |
| 550 | break; |
| 551 | |
| 552 | case PROCESS_TYPE_PPAPI_BROKER: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 553 | memory_size = 64 << 10; // 64 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 554 | metrics_name = "PpapiBrokerMetrics"; |
| 555 | break; |
| 556 | |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 557 | default: |
bcwhite | c600ac1 | 2017-07-12 13:37:13 | [diff] [blame] | 558 | // 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 Sherman | 1c811db | 2017-12-14 10:36:18 | [diff] [blame] | 563 | base::UmaHistogramSparse( |
bcwhite | c600ac1 | 2017-07-12 13:37:13 | [diff] [blame] | 564 | "UMA.SubprocessMetricsProvider.UntrackedProcesses", process_type); |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 565 | 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 | |
| 579 | void BrowserChildProcessHostImpl::ShareMetricsAllocatorToProcess() { |
| 580 | if (metrics_allocator_) { |
Nicholas Verne | 91bf4a63 | 2017-10-12 21:14:24 | [diff] [blame] | 581 | HistogramController::GetInstance()->SetHistogramMemory<ChildProcessHost>( |
| 582 | GetHost(), |
| 583 | mojo::WrapSharedMemoryHandle( |
| 584 | metrics_allocator_->shared_memory()->handle().Duplicate(), |
Ken Rockot | 673ce95 | 2018-01-18 20:59:24 | [diff] [blame] | 585 | metrics_allocator_->shared_memory()->mapped_size(), |
| 586 | mojo::UnwrappedSharedMemoryHandleProtection::kReadWrite)); |
Nicholas Verne | 91bf4a63 | 2017-10-12 21:14:24 | [diff] [blame] | 587 | } else { |
| 588 | HistogramController::GetInstance()->SetHistogramMemory<ChildProcessHost>( |
| 589 | GetHost(), mojo::ScopedSharedBufferHandle()); |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 593 | void BrowserChildProcessHostImpl::OnProcessLaunchFailed(int error_code) { |
| 594 | delegate_->OnProcessLaunchFailed(error_code); |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 595 | notify_child_disconnected_ = false; |
[email protected] | fa01e47 | 2014-02-11 14:45:35 | [diff] [blame] | 596 | delete delegate_; // Will delete us |
| 597 | } |
| 598 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 599 | void BrowserChildProcessHostImpl::OnProcessLaunched() { |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 600 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 601 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 602 | const base::Process& process = child_process_->GetProcess(); |
| 603 | DCHECK(process.IsValid()); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 604 | |
rockot | fe2fecc | 2017-01-19 20:16:19 | [diff] [blame] | 605 | if (child_connection_) |
| 606 | child_connection_->SetProcessHandle(process.Handle()); |
| 607 | |
jam | 3c8c50c | 2015-11-25 18:48:29 | [diff] [blame] | 608 | #if defined(OS_WIN) |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 609 | // 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. |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 613 | DCHECK(!early_exit_watcher_.GetWatchedObject()); |
jam | 7fab108 | 2015-09-29 02:26:18 | [diff] [blame] | 614 | early_exit_watcher_.StartWatchingOnce(process.Handle(), this); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 615 | #endif |
| 616 | |
Francois Doray | 884da02 | 2018-10-30 23:45:58 | [diff] [blame] | 617 | data_.SetProcess(process.Duplicate()); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 618 | delegate_->OnProcessLaunched(); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 619 | |
| 620 | if (is_channel_connected_) { |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 621 | base::PostTaskWithTraits( |
| 622 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 623 | base::BindOnce(&NotifyProcessLaunchedAndConnected, data_.Duplicate())); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 624 | } |
| 625 | } |
| 626 | |
| 627 | bool BrowserChildProcessHostImpl::IsProcessLaunched() const { |
| 628 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 629 | |
| 630 | return child_process_.get() && child_process_->GetProcess().IsValid(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 631 | } |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 632 | |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 633 | // static |
| 634 | void 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( |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 640 | FROM_HERE, base::BindOnce(&BrowserChildProcessHostImpl::OnMojoError, |
| 641 | process, task_runner, error)); |
olka | 91f1d41 | 2016-11-21 15:24:53 | [diff] [blame] | 642 | return; |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 643 | } |
| 644 | if (!process) |
| 645 | return; |
Wei-Yin Chen (陳威尹) | 223326c | 2017-07-21 02:08:28 | [diff] [blame] | 646 | HistogramBadMessageTerminated( |
| 647 | static_cast<ProcessType>(process->data_.process_type)); |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 648 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 649 | switches::kDisableKillAfterBadIPC)) { |
| 650 | return; |
| 651 | } |
| 652 | LOG(ERROR) << "Terminating child process for bad Mojo message: " << error; |
| 653 | |
Yuzhu Shen | 32fcad7 | 2017-09-14 23:12:37 | [diff] [blame] | 654 | // 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 Sesek | 1419427e | 2017-12-07 15:01:32 | [diff] [blame] | 657 | base::debug::ScopedCrashKeyString scoped_error_key( |
| 658 | bad_message::GetMojoErrorCrashKey(), error); |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 659 | base::debug::DumpWithoutCrashing(); |
Bo Liu | e0614391 | 2018-04-20 20:42:30 | [diff] [blame] | 660 | process->child_process_->Terminate(RESULT_CODE_KILLED_BAD_MESSAGE); |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 661 | } |
| 662 | |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 663 | #if defined(OS_WIN) |
| 664 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 665 | void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) { |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 666 | OnChildDisconnected(); |
| 667 | } |
| 668 | |
| 669 | #endif |
| 670 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 671 | } // namespace content |