[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" |
Ken Rockot | 47c47d86 | 2019-09-26 17:38:07 | [diff] [blame] | 22 | #include "base/no_destructor.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 23 | #include "base/stl_util.h" |
[email protected] | 10994d13 | 2013-06-11 07:16:18 | [diff] [blame] | 24 | #include "base/strings/string_util.h" |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 25 | #include "base/strings/stringprintf.h" |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 26 | #include "base/synchronization/waitable_event.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 27 | #include "base/task/post_task.h" |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 28 | #include "base/threading/thread_task_runner_handle.h" |
Ken Rockot | 315def7 | 2018-11-10 02:16:10 | [diff] [blame] | 29 | #include "base/token.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 30 | #include "build/build_config.h" |
Ehsan Chiniforooshan | def633e0 | 2018-04-24 02:05:02 | [diff] [blame] | 31 | #include "components/tracing/common/trace_startup_config.h" |
primiano | ccb26c6 | 2016-06-01 21:50:02 | [diff] [blame] | 32 | #include "components/tracing/common/tracing_switches.h" |
Robert Sesek | 1419427e | 2017-12-07 15:01:32 | [diff] [blame] | 33 | #include "content/browser/bad_message.h" |
Ehsan Chiniforooshan | f192e8a2 | 2018-03-27 03:18:20 | [diff] [blame] | 34 | #include "content/browser/browser_main_loop.h" |
Nicholas Verne | 91bf4a63 | 2017-10-12 21:14:24 | [diff] [blame] | 35 | #include "content/browser/histogram_controller.h" |
ben | ff46163 | 2016-10-06 14:27:33 | [diff] [blame] | 36 | #include "content/browser/service_manager/service_manager_context.h" |
Darin Fisher | c23ee1a | 2019-06-05 23:26:30 | [diff] [blame] | 37 | #include "content/browser/tracing/background_tracing_manager_impl.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" |
John Abd-El-Malek | 252adb9c | 2019-07-30 04:57:24 | [diff] [blame] | 42 | #include "content/public/browser/browser_message_filter.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 43 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 44 | #include "content/public/browser/browser_thread.h" |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 45 | #include "content/public/browser/child_process_data.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 46 | #include "content/public/browser/content_browser_client.h" |
Ken Rockot | d7148c20 | 2019-09-18 18:00:59 | [diff] [blame] | 47 | #include "content/public/browser/resource_coordinator_service.h" |
Ken Rockot | 20cd07b | 2019-10-01 23:01:19 | [diff] [blame] | 48 | #include "content/public/browser/system_connector.h" |
ke.he | 19b5e6a | 2016-10-27 08:47:09 | [diff] [blame] | 49 | #include "content/public/common/connection_filter.h" |
Hans Wennborg | 5ffd139 | 2019-10-16 11:00:02 | [diff] [blame] | 50 | #include "content/public/common/content_client.h" |
lawrencewu | 2c93082 | 2016-10-13 13:54:25 | [diff] [blame] | 51 | #include "content/public/common/content_features.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 52 | #include "content/public/common/content_switches.h" |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 53 | #include "content/public/common/process_type.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 54 | #include "content/public/common/result_codes.h" |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 55 | #include "content/public/common/sandboxed_process_launcher_delegate.h" |
ke.he | 19b5e6a | 2016-10-27 08:47:09 | [diff] [blame] | 56 | #include "content/public/common/service_manager_connection.h" |
Nicholas Verne | 91bf4a63 | 2017-10-12 21:14:24 | [diff] [blame] | 57 | #include "mojo/public/cpp/system/platform_handle.h" |
Yoichi Osato | 14073bd | 2019-06-04 11:06:37 | [diff] [blame] | 58 | #include "net/websockets/websocket_basic_stream.h" |
Yoichi Osato | 7ce0895 | 2019-06-18 02:10:34 | [diff] [blame] | 59 | #include "net/websockets/websocket_channel.h" |
ochang | 6a8400e | 2017-05-01 21:28:24 | [diff] [blame] | 60 | #include "services/service_manager/embedder/switches.h" |
Ken Rockot | a86fa54 | 2018-11-13 21:47:13 | [diff] [blame] | 61 | #include "services/service_manager/public/cpp/constants.h" |
Eric Seckler | 660ffc4 | 2019-09-30 19:00:04 | [diff] [blame] | 62 | #include "services/tracing/public/cpp/trace_startup.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 63 | |
[email protected] | f167520 | 2012-07-09 15:18:00 | [diff] [blame] | 64 | #if defined(OS_MACOSX) |
Robert Sesek | de2e71326 | 2019-07-15 20:57:58 | [diff] [blame] | 65 | #include "content/browser/child_process_task_port_provider_mac.h" |
Ken Rockot | 47c47d86 | 2019-09-26 17:38:07 | [diff] [blame] | 66 | #include "content/browser/sandbox_support_mac_impl.h" |
| 67 | #include "content/common/sandbox_support_mac.mojom.h" |
| 68 | #endif |
| 69 | |
| 70 | #if defined(OS_WIN) |
| 71 | #include "content/browser/renderer_host/dwrite_font_proxy_impl_win.h" |
| 72 | #include "content/public/common/font_cache_dispatcher_win.h" |
| 73 | #include "content/public/common/font_cache_win.mojom.h" |
[email protected] | 3838ca1 | 2011-11-02 14:37:15 | [diff] [blame] | 74 | #endif |
[email protected] | a3a7e2c | 2011-09-16 23:07:05 | [diff] [blame] | 75 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 76 | namespace content { |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 77 | namespace { |
| 78 | |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 79 | static base::LazyInstance< |
| 80 | BrowserChildProcessHostImpl::BrowserChildProcessList>::DestructorAtExit |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 81 | g_child_process_list = LAZY_INSTANCE_INITIALIZER; |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 82 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 83 | base::LazyInstance<base::ObserverList<BrowserChildProcessObserver>::Unchecked>:: |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 84 | DestructorAtExit g_browser_child_process_observers = |
| 85 | LAZY_INSTANCE_INITIALIZER; |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 86 | |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 87 | void NotifyProcessLaunchedAndConnected(const ChildProcessData& data) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 88 | for (auto& observer : g_browser_child_process_observers.Get()) |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 89 | observer.BrowserChildProcessLaunchedAndConnected(data); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 90 | } |
| 91 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 92 | void NotifyProcessHostConnected(const ChildProcessData& data) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 93 | for (auto& observer : g_browser_child_process_observers.Get()) |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 94 | observer.BrowserChildProcessHostConnected(data); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void NotifyProcessHostDisconnected(const ChildProcessData& data) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 98 | for (auto& observer : g_browser_child_process_observers.Get()) |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 99 | observer.BrowserChildProcessHostDisconnected(data); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 100 | } |
| 101 | |
Bo Liu | e962461 | 2018-04-28 00:39:08 | [diff] [blame] | 102 | #if !defined(OS_ANDROID) |
Bo Liu | 2a48940 | 2018-04-24 23:41:27 | [diff] [blame] | 103 | void NotifyProcessCrashed(const ChildProcessData& data, |
| 104 | const ChildProcessTerminationInfo& info) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 105 | for (auto& observer : g_browser_child_process_observers.Get()) |
Bo Liu | 2a48940 | 2018-04-24 23:41:27 | [diff] [blame] | 106 | observer.BrowserChildProcessCrashed(data, info); |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 107 | } |
Bo Liu | e962461 | 2018-04-28 00:39:08 | [diff] [blame] | 108 | #endif |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 109 | |
Bo Liu | 2a48940 | 2018-04-24 23:41:27 | [diff] [blame] | 110 | void NotifyProcessKilled(const ChildProcessData& data, |
| 111 | const ChildProcessTerminationInfo& info) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 112 | for (auto& observer : g_browser_child_process_observers.Get()) |
Bo Liu | 2a48940 | 2018-04-24 23:41:27 | [diff] [blame] | 113 | observer.BrowserChildProcessKilled(data, info); |
wfh | f000329 | 2015-08-18 22:16:46 | [diff] [blame] | 114 | } |
| 115 | |
Ken Rockot | d7148c20 | 2019-09-18 18:00:59 | [diff] [blame] | 116 | memory_instrumentation::mojom::ProcessType GetCoordinatorClientProcessType( |
| 117 | ProcessType process_type) { |
| 118 | switch (process_type) { |
| 119 | case PROCESS_TYPE_RENDERER: |
| 120 | return memory_instrumentation::mojom::ProcessType::RENDERER; |
| 121 | case PROCESS_TYPE_UTILITY: |
| 122 | return memory_instrumentation::mojom::ProcessType::UTILITY; |
| 123 | case PROCESS_TYPE_GPU: |
| 124 | return memory_instrumentation::mojom::ProcessType::GPU; |
| 125 | case PROCESS_TYPE_PPAPI_PLUGIN: |
| 126 | case PROCESS_TYPE_PPAPI_BROKER: |
| 127 | return memory_instrumentation::mojom::ProcessType::PLUGIN; |
| 128 | default: |
| 129 | NOTREACHED(); |
| 130 | return memory_instrumentation::mojom::ProcessType::OTHER; |
| 131 | } |
| 132 | } |
| 133 | |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 134 | } // namespace |
| 135 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 136 | BrowserChildProcessHost* BrowserChildProcessHost::Create( |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 137 | content::ProcessType process_type, |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 138 | BrowserChildProcessHostDelegate* delegate) { |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 139 | return Create(process_type, delegate, std::string()); |
amistry | 6ad1e81 | 2016-06-06 05:36:30 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | BrowserChildProcessHost* BrowserChildProcessHost::Create( |
| 143 | content::ProcessType process_type, |
| 144 | BrowserChildProcessHostDelegate* delegate, |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 145 | const std::string& service_name) { |
| 146 | return new BrowserChildProcessHostImpl(process_type, delegate, service_name); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 147 | } |
| 148 | |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 149 | BrowserChildProcessHost* BrowserChildProcessHost::FromID(int child_process_id) { |
| 150 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 151 | BrowserChildProcessHostImpl::BrowserChildProcessList* process_list = |
| 152 | g_child_process_list.Pointer(); |
| 153 | for (BrowserChildProcessHostImpl* host : *process_list) { |
| 154 | if (host->GetData().id == child_process_id) |
| 155 | return host; |
| 156 | } |
| 157 | return nullptr; |
| 158 | } |
| 159 | |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 160 | #if defined(OS_MACOSX) |
erikchen | cd3e0e7b | 2015-10-01 22:53:51 | [diff] [blame] | 161 | base::PortProvider* BrowserChildProcessHost::GetPortProvider() { |
Robert Sesek | de2e71326 | 2019-07-15 20:57:58 | [diff] [blame] | 162 | return ChildProcessTaskPortProvider::GetInstance(); |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 163 | } |
| 164 | #endif |
| 165 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 166 | // static |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 167 | BrowserChildProcessHostImpl::BrowserChildProcessList* |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 168 | BrowserChildProcessHostImpl::GetIterator() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 169 | return g_child_process_list.Pointer(); |
| 170 | } |
| 171 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 172 | // static |
| 173 | void BrowserChildProcessHostImpl::AddObserver( |
| 174 | BrowserChildProcessObserver* observer) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 175 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 176 | g_browser_child_process_observers.Get().AddObserver(observer); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // static |
| 180 | void BrowserChildProcessHostImpl::RemoveObserver( |
| 181 | BrowserChildProcessObserver* observer) { |
| 182 | // TODO(phajdan.jr): Check thread after fixing http://crbug.com/167126. |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 183 | g_browser_child_process_observers.Get().RemoveObserver(observer); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 184 | } |
| 185 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 186 | BrowserChildProcessHostImpl::BrowserChildProcessHostImpl( |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 187 | content::ProcessType process_type, |
amistry | 6ad1e81 | 2016-06-06 05:36:30 | [diff] [blame] | 188 | BrowserChildProcessHostDelegate* delegate, |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 189 | const std::string& service_name) |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 190 | : data_(process_type), |
[email protected] | 9dd9015 | 2013-08-02 22:09:13 | [diff] [blame] | 191 | delegate_(delegate), |
sadrul | 6c5aed8c | 2017-01-11 23:11:44 | [diff] [blame] | 192 | channel_(nullptr), |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 193 | is_channel_connected_(false), |
Jeremy Roman | 3bca4bf | 2019-07-11 03:41:25 | [diff] [blame] | 194 | notify_child_disconnected_(false) { |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 195 | data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId(); |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 196 | |
Ayu Ishii | 75cc106 | 2019-01-08 01:27:37 | [diff] [blame] | 197 | child_process_host_ = ChildProcessHost::Create(this); |
[email protected] | 38917fe | 2011-04-06 17:29:12 | [diff] [blame] | 198 | |
| 199 | g_child_process_list.Get().push_back(this); |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 200 | GetContentClient()->browser()->BrowserChildProcessHostCreated(this); |
[email protected] | 30c4c67a | 2014-07-22 02:33:55 | [diff] [blame] | 201 | |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 202 | if (!service_name.empty()) { |
| 203 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
Ken Rockot | a86fa54 | 2018-11-13 21:47:13 | [diff] [blame] | 204 | child_connection_ = std::make_unique<ChildConnection>( |
| 205 | service_manager::Identity( |
| 206 | service_name, service_manager::kSystemInstanceGroup, |
| 207 | base::Token::CreateRandom(), base::Token::CreateRandom()), |
| 208 | &mojo_invitation_, ServiceManagerContext::GetConnectorForIOThread(), |
| 209 | base::ThreadTaskRunnerHandle::Get()); |
Will Harris | edb011f | 2018-06-01 20:28:44 | [diff] [blame] | 210 | data_.metrics_name = service_name; |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 211 | } |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 212 | |
| 213 | // Create a persistent memory segment for subprocess histograms. |
| 214 | CreateMetricsAllocator(); |
[email protected] | 38917fe | 2011-04-06 17:29:12 | [diff] [blame] | 215 | } |
| 216 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 217 | BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() { |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 218 | g_child_process_list.Get().remove(this); |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 219 | |
| 220 | if (notify_child_disconnected_) { |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 221 | base::PostTask( |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 222 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 223 | base::BindOnce(&NotifyProcessHostDisconnected, data_.Duplicate())); |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 224 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // static |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 228 | void BrowserChildProcessHostImpl::TerminateAll() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 229 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 230 | // Make a copy since the BrowserChildProcessHost dtor mutates the original |
| 231 | // list. |
| 232 | BrowserChildProcessList copy = g_child_process_list.Get(); |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 233 | for (auto it = copy.begin(); it != copy.end(); ++it) { |
[email protected] | 5998d9d | 2012-03-06 04:25:26 | [diff] [blame] | 234 | delete (*it)->delegate(); // ~*HostDelegate deletes *HostImpl. |
| 235 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 236 | } |
| 237 | |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 238 | // static |
lawrencewu | 0b496492 | 2016-10-25 16:28:14 | [diff] [blame] | 239 | void BrowserChildProcessHostImpl::CopyFeatureAndFieldTrialFlags( |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 240 | base::CommandLine* cmd_line) { |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 241 | // If we run base::FieldTrials, we want to pass to their state to the |
| 242 | // child process so that it can act in accordance with each state. |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 243 | base::FieldTrialList::CopyFieldTrialStateToFlags( |
| 244 | switches::kFieldTrialHandle, switches::kEnableFeatures, |
| 245 | switches::kDisableFeatures, cmd_line); |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 246 | } |
| 247 | |
Ehsan Chiniforooshan | f192e8a2 | 2018-03-27 03:18:20 | [diff] [blame] | 248 | // static |
| 249 | void BrowserChildProcessHostImpl::CopyTraceStartupFlags( |
| 250 | base::CommandLine* cmd_line) { |
Eric Seckler | 660ffc4 | 2019-09-30 19:00:04 | [diff] [blame] | 251 | tracing::PropagateTracingFlagsToChildProcessCmdLine(cmd_line); |
Ehsan Chiniforooshan | f192e8a2 | 2018-03-27 03:18:20 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 254 | void BrowserChildProcessHostImpl::Launch( |
jcivelli | d20cc0b | 2016-12-22 03:51:41 | [diff] [blame] | 255 | std::unique_ptr<SandboxedProcessLauncherDelegate> delegate, |
| 256 | std::unique_ptr<base::CommandLine> cmd_line, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 257 | bool terminate_on_shutdown) { |
jcivelli | d20cc0b | 2016-12-22 03:51:41 | [diff] [blame] | 258 | GetContentClient()->browser()->AppendExtraCommandLineSwitches(cmd_line.get(), |
| 259 | data_.id); |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 260 | LaunchWithoutExtraCommandLineSwitches( |
| 261 | std::move(delegate), std::move(cmd_line), terminate_on_shutdown); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 262 | } |
| 263 | |
Lucas Furukawa Gadani | 0d5e714 | 2019-04-18 23:00:50 | [diff] [blame] | 264 | const ChildProcessData& BrowserChildProcessHostImpl::GetData() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 265 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 266 | return data_; |
| 267 | } |
| 268 | |
Lucas Furukawa Gadani | 0d5e714 | 2019-04-18 23:00:50 | [diff] [blame] | 269 | ChildProcessHost* BrowserChildProcessHostImpl::GetHost() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 270 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 271 | return child_process_host_.get(); |
| 272 | } |
| 273 | |
Lucas Furukawa Gadani | 0d5e714 | 2019-04-18 23:00:50 | [diff] [blame] | 274 | const base::Process& BrowserChildProcessHostImpl::GetProcess() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 275 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 276 | DCHECK(child_process_.get()) |
| 277 | << "Requesting a child process handle before launching."; |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 278 | DCHECK(child_process_->GetProcess().IsValid()) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 279 | << "Requesting a child process handle before launch has completed OK."; |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 280 | return child_process_->GetProcess(); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 281 | } |
| 282 | |
Alex Ilin | 30e7d06 | 2019-02-21 12:24:53 | [diff] [blame] | 283 | std::unique_ptr<base::PersistentMemoryAllocator> |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 284 | BrowserChildProcessHostImpl::TakeMetricsAllocator() { |
| 285 | return std::move(metrics_allocator_); |
| 286 | } |
| 287 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 288 | void BrowserChildProcessHostImpl::SetName(const base::string16& name) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 289 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 290 | data_.name = name; |
| 291 | } |
| 292 | |
Will Harris | edb011f | 2018-06-01 20:28:44 | [diff] [blame] | 293 | void BrowserChildProcessHostImpl::SetMetricsName( |
| 294 | const std::string& metrics_name) { |
| 295 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 296 | data_.metrics_name = metrics_name; |
| 297 | } |
| 298 | |
Francois Doray | 884da02 | 2018-10-30 23:45:58 | [diff] [blame] | 299 | void BrowserChildProcessHostImpl::SetProcess(base::Process process) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 300 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
Francois Doray | 884da02 | 2018-10-30 23:45:58 | [diff] [blame] | 301 | data_.SetProcess(std::move(process)); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 302 | } |
| 303 | |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 304 | service_manager::mojom::ServiceRequest |
| 305 | BrowserChildProcessHostImpl::TakeInProcessServiceRequest() { |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 306 | auto invitation = std::move(mojo_invitation_); |
Ken Rockot | f4d8a94 | 2017-05-13 00:10:37 | [diff] [blame] | 307 | return service_manager::mojom::ServiceRequest( |
Ken Rockot | 026afc3 | 2018-06-04 19:19:18 | [diff] [blame] | 308 | invitation.ExtractMessagePipe(child_connection_->service_token())); |
sammc | 2b0375b | 2016-12-22 01:26:17 | [diff] [blame] | 309 | } |
| 310 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 311 | void BrowserChildProcessHostImpl::ForceShutdown() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 312 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 313 | g_child_process_list.Get().remove(this); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 314 | child_process_host_->ForceShutdown(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 315 | } |
| 316 | |
[email protected] | ba780c1 | 2013-10-01 17:07:06 | [diff] [blame] | 317 | void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter* filter) { |
| 318 | child_process_host_->AddFilter(filter->GetFilter()); |
| 319 | } |
| 320 | |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 321 | void BrowserChildProcessHostImpl::LaunchWithoutExtraCommandLineSwitches( |
| 322 | std::unique_ptr<SandboxedProcessLauncherDelegate> delegate, |
| 323 | std::unique_ptr<base::CommandLine> cmd_line, |
| 324 | bool terminate_on_shutdown) { |
| 325 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 326 | const base::CommandLine& browser_command_line = |
| 327 | *base::CommandLine::ForCurrentProcess(); |
| 328 | static const char* const kForwardSwitches[] = { |
Yoichi Osato | 14073bd | 2019-06-04 11:06:37 | [diff] [blame] | 329 | net::kWebSocketReadBufferSize, |
Yoichi Osato | 7ce0895 | 2019-06-18 02:10:34 | [diff] [blame] | 330 | net::kWebSocketReceiveQuotaThreshold, |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 331 | service_manager::switches::kDisableInProcessStackTraces, |
Yue Ru Sun | a50a80f | 2019-02-08 05:02:58 | [diff] [blame] | 332 | switches::kDisableBestEffortTasks, |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 333 | switches::kDisableLogging, |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 334 | switches::kEnableLogging, |
| 335 | switches::kIPCConnectionTimeout, |
Francois Doray | 177da2c | 2019-06-20 14:14:22 | [diff] [blame] | 336 | switches::kLogBestEffortTasks, |
Greg Thompson | 5491583 | 2018-12-21 17:11:47 | [diff] [blame] | 337 | switches::kLogFile, |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 338 | switches::kLoggingLevel, |
| 339 | switches::kTraceToConsole, |
| 340 | switches::kV, |
| 341 | switches::kVModule, |
| 342 | }; |
| 343 | cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches, |
| 344 | base::size(kForwardSwitches)); |
| 345 | |
| 346 | if (child_connection_) { |
| 347 | cmd_line->AppendSwitchASCII( |
| 348 | service_manager::switches::kServiceRequestChannelToken, |
| 349 | child_connection_->service_token()); |
Darin Fisher | c23ee1a | 2019-06-05 23:26:30 | [diff] [blame] | 350 | |
| 351 | // Tracing adds too much overhead to the profiling service. |
| 352 | if (service_manager::SandboxTypeFromCommandLine(*cmd_line) != |
| 353 | service_manager::SANDBOX_TYPE_PROFILING) { |
Darin Fisher | 0e4fc2e | 2019-06-13 02:19:18 | [diff] [blame] | 354 | BackgroundTracingManagerImpl::ActivateForProcess( |
| 355 | data_.id, |
| 356 | static_cast<ChildProcessHostImpl*>(child_process_host_.get()) |
Ken Rockot | 62fb435 | 2019-07-18 16:03:38 | [diff] [blame] | 357 | ->child_process()); |
Darin Fisher | c23ee1a | 2019-06-05 23:26:30 | [diff] [blame] | 358 | } |
Maggie Chen | 47b8dc1d | 2018-11-10 00:12:34 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | // All processes should have a non-empty metrics name. |
| 362 | DCHECK(!data_.metrics_name.empty()); |
| 363 | |
| 364 | notify_child_disconnected_ = true; |
| 365 | child_process_.reset(new ChildProcessLauncher( |
| 366 | std::move(delegate), std::move(cmd_line), data_.id, this, |
| 367 | std::move(mojo_invitation_), |
| 368 | base::BindRepeating(&BrowserChildProcessHostImpl::OnMojoError, |
| 369 | weak_factory_.GetWeakPtr(), |
| 370 | base::ThreadTaskRunnerHandle::Get()), |
| 371 | terminate_on_shutdown)); |
| 372 | ShareMetricsAllocatorToProcess(); |
| 373 | } |
| 374 | |
ben | a06f7d6 | 2017-04-04 20:48:52 | [diff] [blame] | 375 | void BrowserChildProcessHostImpl::BindInterface( |
| 376 | const std::string& interface_name, |
| 377 | mojo::ScopedMessagePipeHandle interface_pipe) { |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 378 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 379 | if (!child_connection_) |
ben | a06f7d6 | 2017-04-04 20:48:52 | [diff] [blame] | 380 | return; |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 381 | |
ben | a06f7d6 | 2017-04-04 20:48:52 | [diff] [blame] | 382 | child_connection_->BindInterface(interface_name, std::move(interface_pipe)); |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 383 | } |
| 384 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 385 | void BrowserChildProcessHostImpl::HistogramBadMessageTerminated( |
Wei-Yin Chen (陳威尹) | 223326c | 2017-07-21 02:08:28 | [diff] [blame] | 386 | ProcessType process_type) { |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 387 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.BadMessgeTerminated", process_type, |
| 388 | PROCESS_TYPE_MAX); |
| 389 | } |
| 390 | |
Min Qin | eb96190 | 2019-03-16 08:08:22 | [diff] [blame] | 391 | #if defined(OS_ANDROID) |
| 392 | void BrowserChildProcessHostImpl::EnableWarmUpConnection() { |
| 393 | can_use_warm_up_connection_ = true; |
| 394 | } |
| 395 | #endif |
| 396 | |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 397 | ChildProcessTerminationInfo BrowserChildProcessHostImpl::GetTerminationInfo( |
| 398 | bool known_dead) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 399 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 400 | if (!child_process_) { |
| 401 | // If the delegate doesn't use Launch() helper. |
| 402 | ChildProcessTerminationInfo info; |
Francois Doray | 884da02 | 2018-10-30 23:45:58 | [diff] [blame] | 403 | info.status = base::GetTerminationStatus(data_.GetProcess().Handle(), |
| 404 | &info.exit_code); |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 405 | return info; |
| 406 | } |
| 407 | return child_process_->GetChildTerminationInfo(known_dead); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 410 | bool BrowserChildProcessHostImpl::OnMessageReceived( |
| 411 | const IPC::Message& message) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 412 | return delegate_->OnMessageReceived(message); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 413 | } |
| 414 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 415 | void BrowserChildProcessHostImpl::OnChannelConnected(int32_t peer_pid) { |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 416 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 417 | |
| 418 | is_channel_connected_ = true; |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 419 | notify_child_disconnected_ = true; |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 420 | |
Robert Sesek | de2e71326 | 2019-07-15 20:57:58 | [diff] [blame] | 421 | #if defined(OS_MACOSX) |
| 422 | ChildProcessTaskPortProvider::GetInstance()->OnChildProcessLaunched( |
| 423 | peer_pid, static_cast<ChildProcessHostImpl*>(child_process_host_.get()) |
Ken Rockot | 62fb435 | 2019-07-18 16:03:38 | [diff] [blame] | 424 | ->child_process()); |
Robert Sesek | de2e71326 | 2019-07-15 20:57:58 | [diff] [blame] | 425 | #endif |
| 426 | |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 427 | #if defined(OS_WIN) |
| 428 | // From this point onward, the exit of the child process is detected by an |
| 429 | // error on the IPC channel. |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 430 | early_exit_watcher_.StopWatching(); |
| 431 | #endif |
| 432 | |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 433 | base::PostTask( |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 434 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 435 | base::BindOnce(&NotifyProcessHostConnected, data_.Duplicate())); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 436 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 437 | delegate_->OnChannelConnected(peer_pid); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 438 | |
| 439 | if (IsProcessLaunched()) { |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 440 | base::PostTask( |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 441 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 442 | base::BindOnce(&NotifyProcessLaunchedAndConnected, data_.Duplicate())); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 443 | } |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 444 | } |
| 445 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 446 | void BrowserChildProcessHostImpl::OnChannelError() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 447 | delegate_->OnChannelError(); |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 448 | } |
| 449 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 450 | void BrowserChildProcessHostImpl::OnBadMessageReceived( |
| 451 | const IPC::Message& message) { |
Roman Karasev | a43d5b4e | 2017-12-21 03:06:02 | [diff] [blame] | 452 | std::string log_message = "Bad message received of type: "; |
| 453 | if (message.IsValid()) { |
| 454 | log_message += std::to_string(message.type()); |
| 455 | } else { |
| 456 | log_message += "unknown"; |
| 457 | } |
sadrul | 73f39a6 | 2017-01-11 04:07:24 | [diff] [blame] | 458 | TerminateOnBadMessageReceived(log_message); |
jbauman | 278b2cc | 2016-01-08 22:48:53 | [diff] [blame] | 459 | } |
| 460 | |
sadrul | 73f39a6 | 2017-01-11 04:07:24 | [diff] [blame] | 461 | void BrowserChildProcessHostImpl::TerminateOnBadMessageReceived( |
| 462 | const std::string& error) { |
Wei-Yin Chen (陳威尹) | 223326c | 2017-07-21 02:08:28 | [diff] [blame] | 463 | HistogramBadMessageTerminated(static_cast<ProcessType>(data_.process_type)); |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 464 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | e44d134 | 2014-05-16 21:29:33 | [diff] [blame] | 465 | switches::kDisableKillAfterBadIPC)) { |
| 466 | return; |
| 467 | } |
sadrul | 73f39a6 | 2017-01-11 04:07:24 | [diff] [blame] | 468 | LOG(ERROR) << "Terminating child process for bad IPC message: " << error; |
wfh | af32d0b | 2015-04-07 02:21:36 | [diff] [blame] | 469 | // Create a memory dump. This will contain enough stack frames to work out |
| 470 | // what the bad message was. |
| 471 | base::debug::DumpWithoutCrashing(); |
| 472 | |
Bo Liu | e0614391 | 2018-04-20 20:42:30 | [diff] [blame] | 473 | child_process_->Terminate(RESULT_CODE_KILLED_BAD_MESSAGE); |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 474 | } |
| 475 | |
sadrul | 6c5aed8c | 2017-01-11 23:11:44 | [diff] [blame] | 476 | void BrowserChildProcessHostImpl::OnChannelInitialized(IPC::Channel* channel) { |
| 477 | channel_ = channel; |
| 478 | } |
| 479 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 480 | void BrowserChildProcessHostImpl::OnChildDisconnected() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 481 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
jbauman | 319402e | 2014-10-21 17:52:21 | [diff] [blame] | 482 | #if defined(OS_WIN) |
| 483 | // OnChildDisconnected may be called without OnChannelConnected, so stop the |
| 484 | // early exit watcher so GetTerminationStatus can close the process handle. |
| 485 | early_exit_watcher_.StopWatching(); |
| 486 | #endif |
Francois Doray | 884da02 | 2018-10-30 23:45:58 | [diff] [blame] | 487 | if (child_process_.get() || data_.GetProcess().IsValid()) { |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 488 | ChildProcessTerminationInfo info = |
| 489 | GetTerminationInfo(true /* known_dead */); |
Bo Liu | e962461 | 2018-04-28 00:39:08 | [diff] [blame] | 490 | #if defined(OS_ANDROID) |
| 491 | delegate_->OnProcessCrashed(info.exit_code); |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 492 | base::PostTask( |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 493 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 494 | base::BindOnce(&NotifyProcessKilled, data_.Duplicate(), info)); |
Bo Liu | e962461 | 2018-04-28 00:39:08 | [diff] [blame] | 495 | #else // OS_ANDROID |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 496 | switch (info.status) { |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 497 | case base::TERMINATION_STATUS_PROCESS_CRASHED: |
| 498 | case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: { |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 499 | delegate_->OnProcessCrashed(info.exit_code); |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 500 | base::PostTask( |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 501 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 502 | base::BindOnce(&NotifyProcessCrashed, data_.Duplicate(), info)); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 503 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 504 | static_cast<ProcessType>(data_.process_type), |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 505 | PROCESS_TYPE_MAX); |
| 506 | break; |
| 507 | } |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 508 | #if defined(OS_CHROMEOS) |
| 509 | case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM: |
| 510 | #endif |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 511 | case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: { |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 512 | delegate_->OnProcessCrashed(info.exit_code); |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 513 | base::PostTask( |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 514 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 515 | base::BindOnce(&NotifyProcessKilled, data_.Duplicate(), info)); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 516 | // Report that this child process was killed. |
| 517 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 518 | static_cast<ProcessType>(data_.process_type), |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 519 | PROCESS_TYPE_MAX); |
| 520 | break; |
| 521 | } |
| 522 | case base::TERMINATION_STATUS_STILL_RUNNING: { |
| 523 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 524 | static_cast<ProcessType>(data_.process_type), |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 525 | PROCESS_TYPE_MAX); |
Nico Weber | 9dbefa6 | 2018-01-27 01:01:42 | [diff] [blame] | 526 | break; |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 527 | } |
| 528 | default: |
| 529 | break; |
[email protected] | 12fed81 | 2011-09-16 05:47:29 | [diff] [blame] | 530 | } |
Bo Liu | e962461 | 2018-04-28 00:39:08 | [diff] [blame] | 531 | #endif // OS_ANDROID |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 532 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 533 | static_cast<ProcessType>(data_.process_type), |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 534 | PROCESS_TYPE_MAX); |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 535 | #if defined(OS_CHROMEOS) |
Bo Liu | 0d2a232 | 2018-04-19 00:18:09 | [diff] [blame] | 536 | if (info.status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM) { |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 537 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2.OOM", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 538 | static_cast<ProcessType>(data_.process_type), |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 539 | PROCESS_TYPE_MAX); |
| 540 | } |
| 541 | #endif |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 542 | } |
sadrul | 6c5aed8c | 2017-01-11 23:11:44 | [diff] [blame] | 543 | channel_ = nullptr; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 544 | delete delegate_; // Will delete us |
[email protected] | a3a7e2c | 2011-09-16 23:07:05 | [diff] [blame] | 545 | } |
| 546 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 547 | bool BrowserChildProcessHostImpl::Send(IPC::Message* message) { |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 548 | return child_process_host_->Send(message); |
| 549 | } |
| 550 | |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 551 | void BrowserChildProcessHostImpl::CreateMetricsAllocator() { |
| 552 | // Create a persistent memory segment for subprocess histograms only if |
| 553 | // they're active in the browser. |
| 554 | // TODO(bcwhite): Remove this once persistence is always enabled. |
| 555 | if (!base::GlobalHistogramAllocator::Get()) |
| 556 | return; |
| 557 | |
| 558 | // Determine the correct parameters based on the process type. |
| 559 | size_t memory_size; |
| 560 | base::StringPiece metrics_name; |
| 561 | switch (data_.process_type) { |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 562 | case PROCESS_TYPE_UTILITY: |
Matt Menke | e5bc4a56 | 2017-08-07 23:58:22 | [diff] [blame] | 563 | // This needs to be larger for the network service. |
| 564 | memory_size = 256 << 10; // 256 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 565 | metrics_name = "UtilityMetrics"; |
| 566 | break; |
| 567 | |
| 568 | case PROCESS_TYPE_ZYGOTE: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 569 | memory_size = 64 << 10; // 64 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 570 | metrics_name = "ZygoteMetrics"; |
| 571 | break; |
| 572 | |
| 573 | case PROCESS_TYPE_SANDBOX_HELPER: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 574 | memory_size = 64 << 10; // 64 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 575 | metrics_name = "SandboxHelperMetrics"; |
| 576 | break; |
| 577 | |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 578 | case PROCESS_TYPE_GPU: |
Sadrul Habib Chowdhury | 2353ca52 | 2018-08-15 15:53:26 | [diff] [blame] | 579 | // This needs to be larger for the display-compositor in the gpu process. |
| 580 | memory_size = 256 << 10; // 256 KiB |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 581 | metrics_name = "GpuMetrics"; |
| 582 | break; |
| 583 | |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 584 | case PROCESS_TYPE_PPAPI_PLUGIN: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 585 | memory_size = 64 << 10; // 64 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 586 | metrics_name = "PpapiPluginMetrics"; |
| 587 | break; |
| 588 | |
| 589 | case PROCESS_TYPE_PPAPI_BROKER: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 590 | memory_size = 64 << 10; // 64 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 591 | metrics_name = "PpapiBrokerMetrics"; |
| 592 | break; |
| 593 | |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 594 | default: |
| 595 | return; |
| 596 | } |
| 597 | |
| 598 | // Create the shared memory segment and attach an allocator to it. |
| 599 | // Mapping the memory shouldn't fail but be safe if it does; everything |
| 600 | // will continue to work but just as if persistence weren't available. |
Alex Ilin | 30e7d06 | 2019-02-21 12:24:53 | [diff] [blame] | 601 | base::WritableSharedMemoryRegion shm_region = |
| 602 | base::WritableSharedMemoryRegion::Create(memory_size); |
| 603 | base::WritableSharedMemoryMapping shm_mapping = shm_region.Map(); |
| 604 | if (!shm_region.IsValid() || !shm_mapping.IsValid()) |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 605 | return; |
Alex Ilin | 30e7d06 | 2019-02-21 12:24:53 | [diff] [blame] | 606 | metrics_allocator_ = |
| 607 | std::make_unique<base::WritableSharedPersistentMemoryAllocator>( |
| 608 | std::move(shm_mapping), static_cast<uint64_t>(data_.id), |
| 609 | metrics_name); |
| 610 | metrics_shared_region_ = std::move(shm_region); |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | void BrowserChildProcessHostImpl::ShareMetricsAllocatorToProcess() { |
| 614 | if (metrics_allocator_) { |
Nicholas Verne | 91bf4a63 | 2017-10-12 21:14:24 | [diff] [blame] | 615 | HistogramController::GetInstance()->SetHistogramMemory<ChildProcessHost>( |
Alex Ilin | 30e7d06 | 2019-02-21 12:24:53 | [diff] [blame] | 616 | GetHost(), std::move(metrics_shared_region_)); |
Nicholas Verne | 91bf4a63 | 2017-10-12 21:14:24 | [diff] [blame] | 617 | } else { |
| 618 | HistogramController::GetInstance()->SetHistogramMemory<ChildProcessHost>( |
Alex Ilin | 30e7d06 | 2019-02-21 12:24:53 | [diff] [blame] | 619 | GetHost(), base::WritableSharedMemoryRegion()); |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 623 | void BrowserChildProcessHostImpl::OnProcessLaunchFailed(int error_code) { |
| 624 | delegate_->OnProcessLaunchFailed(error_code); |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 625 | notify_child_disconnected_ = false; |
[email protected] | fa01e47 | 2014-02-11 14:45:35 | [diff] [blame] | 626 | delete delegate_; // Will delete us |
| 627 | } |
| 628 | |
Min Qin | eb96190 | 2019-03-16 08:08:22 | [diff] [blame] | 629 | #if defined(OS_ANDROID) |
| 630 | bool BrowserChildProcessHostImpl::CanUseWarmUpConnection() { |
| 631 | return can_use_warm_up_connection_; |
| 632 | } |
| 633 | #endif |
| 634 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 635 | void BrowserChildProcessHostImpl::OnProcessLaunched() { |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 636 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 637 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 638 | const base::Process& process = child_process_->GetProcess(); |
| 639 | DCHECK(process.IsValid()); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 640 | |
rockot | fe2fecc | 2017-01-19 20:16:19 | [diff] [blame] | 641 | if (child_connection_) |
David Bienvenu | f47de5f1 | 2019-02-21 19:46:23 | [diff] [blame] | 642 | child_connection_->SetProcess(process.Duplicate()); |
rockot | fe2fecc | 2017-01-19 20:16:19 | [diff] [blame] | 643 | |
jam | 3c8c50c | 2015-11-25 18:48:29 | [diff] [blame] | 644 | #if defined(OS_WIN) |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 645 | // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the |
| 646 | // child process exits. This watcher is stopped once the IPC channel is |
| 647 | // connected and the exit of the child process is detecter by an error on the |
| 648 | // IPC channel thereafter. |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 649 | DCHECK(!early_exit_watcher_.GetWatchedObject()); |
jam | 7fab108 | 2015-09-29 02:26:18 | [diff] [blame] | 650 | early_exit_watcher_.StartWatchingOnce(process.Handle(), this); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 651 | #endif |
| 652 | |
Francois Doray | 884da02 | 2018-10-30 23:45:58 | [diff] [blame] | 653 | data_.SetProcess(process.Duplicate()); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 654 | delegate_->OnProcessLaunched(); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 655 | |
| 656 | if (is_channel_connected_) { |
Sami Kyostila | 8e4d5a9 | 2019-08-02 12:45:05 | [diff] [blame] | 657 | base::PostTask( |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 658 | FROM_HERE, {BrowserThread::UI}, |
Bruce Dawson | 02f07de | 2018-07-31 17:28:26 | [diff] [blame] | 659 | base::BindOnce(&NotifyProcessLaunchedAndConnected, data_.Duplicate())); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | |
Ken Rockot | d7148c20 | 2019-09-18 18:00:59 | [diff] [blame] | 663 | void BrowserChildProcessHostImpl::RegisterCoordinatorClient( |
| 664 | mojo::PendingReceiver<memory_instrumentation::mojom::Coordinator> receiver, |
| 665 | mojo::PendingRemote<memory_instrumentation::mojom::ClientProcess> |
| 666 | client_process) { |
| 667 | base::PostTask( |
| 668 | FROM_HERE, BrowserThread::UI, |
| 669 | base::BindOnce( |
| 670 | [](mojo::PendingReceiver<memory_instrumentation::mojom::Coordinator> |
| 671 | receiver, |
| 672 | mojo::PendingRemote<memory_instrumentation::mojom::ClientProcess> |
| 673 | client_process, |
| 674 | memory_instrumentation::mojom::ProcessType process_type, |
| 675 | base::ProcessId process_id, |
| 676 | base::Optional<std::string> service_name) { |
| 677 | GetMemoryInstrumentationCoordinatorController() |
| 678 | ->RegisterClientProcess(std::move(receiver), |
| 679 | std::move(client_process), process_type, |
| 680 | process_id, std::move(service_name)); |
| 681 | }, |
| 682 | std::move(receiver), std::move(client_process), |
| 683 | GetCoordinatorClientProcessType( |
| 684 | static_cast<ProcessType>(data_.process_type)), |
| 685 | child_process_->GetProcess().Pid(), delegate_->GetServiceName())); |
| 686 | } |
| 687 | |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 688 | bool BrowserChildProcessHostImpl::IsProcessLaunched() const { |
| 689 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 690 | |
| 691 | return child_process_.get() && child_process_->GetProcess().IsValid(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 692 | } |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 693 | |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 694 | // static |
| 695 | void BrowserChildProcessHostImpl::OnMojoError( |
| 696 | base::WeakPtr<BrowserChildProcessHostImpl> process, |
| 697 | scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 698 | const std::string& error) { |
| 699 | if (!task_runner->BelongsToCurrentThread()) { |
| 700 | task_runner->PostTask( |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 701 | FROM_HERE, base::BindOnce(&BrowserChildProcessHostImpl::OnMojoError, |
| 702 | process, task_runner, error)); |
olka | 91f1d41 | 2016-11-21 15:24:53 | [diff] [blame] | 703 | return; |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 704 | } |
| 705 | if (!process) |
| 706 | return; |
Wei-Yin Chen (陳威尹) | 223326c | 2017-07-21 02:08:28 | [diff] [blame] | 707 | HistogramBadMessageTerminated( |
| 708 | static_cast<ProcessType>(process->data_.process_type)); |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 709 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 710 | switches::kDisableKillAfterBadIPC)) { |
| 711 | return; |
| 712 | } |
| 713 | LOG(ERROR) << "Terminating child process for bad Mojo message: " << error; |
| 714 | |
Yuzhu Shen | 32fcad7 | 2017-09-14 23:12:37 | [diff] [blame] | 715 | // Create a memory dump with the error message captured in a crash key value. |
| 716 | // This will make it easy to determine details about what interface call |
| 717 | // failed. |
Robert Sesek | 1419427e | 2017-12-07 15:01:32 | [diff] [blame] | 718 | base::debug::ScopedCrashKeyString scoped_error_key( |
| 719 | bad_message::GetMojoErrorCrashKey(), error); |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 720 | base::debug::DumpWithoutCrashing(); |
Bo Liu | e0614391 | 2018-04-20 20:42:30 | [diff] [blame] | 721 | process->child_process_->Terminate(RESULT_CODE_KILLED_BAD_MESSAGE); |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 722 | } |
| 723 | |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 724 | #if defined(OS_WIN) |
| 725 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 726 | void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) { |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 727 | OnChildDisconnected(); |
| 728 | } |
| 729 | |
| 730 | #endif |
| 731 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 732 | } // namespace content |