[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" |
fdoray | ba12142 | 2016-12-23 19:51:48 | [diff] [blame] | 17 | #include "base/memory/ptr_util.h" |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 18 | #include "base/metrics/field_trial.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" |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 26 | #include "base/threading/thread_task_runner_handle.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 27 | #include "build/build_config.h" |
primiano | ccb26c6 | 2016-06-01 21:50:02 | [diff] [blame] | 28 | #include "components/tracing/common/tracing_switches.h" |
Robert Sesek | 1419427e | 2017-12-07 15:01:32 | [diff] [blame^] | 29 | #include "content/browser/bad_message.h" |
Nicholas Verne | 91bf4a63 | 2017-10-12 21:14:24 | [diff] [blame] | 30 | #include "content/browser/histogram_controller.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 31 | #include "content/browser/loader/resource_message_filter.h" |
ben | ff46163 | 2016-10-06 14:27:33 | [diff] [blame] | 32 | #include "content/browser/service_manager/service_manager_context.h" |
[email protected] | 3a85b1f | 2013-02-01 04:47:40 | [diff] [blame] | 33 | #include "content/browser/tracing/trace_message_filter.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 34 | #include "content/common/child_process_host_impl.h" |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 35 | #include "content/common/service_manager/child_connection.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 36 | #include "content/public/browser/browser_child_process_host_delegate.h" |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 37 | #include "content/public/browser/browser_child_process_observer.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 38 | #include "content/public/browser/browser_thread.h" |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 39 | #include "content/public/browser/child_process_data.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 40 | #include "content/public/browser/content_browser_client.h" |
ke.he | 19b5e6a | 2016-10-27 08:47:09 | [diff] [blame] | 41 | #include "content/public/common/connection_filter.h" |
lawrencewu | 2c93082 | 2016-10-13 13:54:25 | [diff] [blame] | 42 | #include "content/public/common/content_features.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 43 | #include "content/public/common/content_switches.h" |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 44 | #include "content/public/common/mojo_channel_switches.h" |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 45 | #include "content/public/common/process_type.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 46 | #include "content/public/common/result_codes.h" |
jcivelli | 828cd7f | 2017-01-18 19:50:46 | [diff] [blame] | 47 | #include "content/public/common/sandboxed_process_launcher_delegate.h" |
ke.he | 19b5e6a | 2016-10-27 08:47:09 | [diff] [blame] | 48 | #include "content/public/common/service_manager_connection.h" |
rockot | c637caf9b | 2016-02-10 09:57:08 | [diff] [blame] | 49 | #include "mojo/edk/embedder/embedder.h" |
Nicholas Verne | 91bf4a63 | 2017-10-12 21:14:24 | [diff] [blame] | 50 | #include "mojo/public/cpp/system/platform_handle.h" |
ochang | 6a8400e | 2017-05-01 21:28:24 | [diff] [blame] | 51 | #include "services/service_manager/embedder/switches.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 52 | |
[email protected] | f167520 | 2012-07-09 15:18:00 | [diff] [blame] | 53 | #if defined(OS_MACOSX) |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 54 | #include "content/browser/mach_broker_mac.h" |
[email protected] | 3838ca1 | 2011-11-02 14:37:15 | [diff] [blame] | 55 | #endif |
[email protected] | a3a7e2c | 2011-09-16 23:07:05 | [diff] [blame] | 56 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 57 | namespace content { |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 58 | namespace { |
| 59 | |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 60 | static base::LazyInstance< |
| 61 | BrowserChildProcessHostImpl::BrowserChildProcessList>::DestructorAtExit |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 62 | g_child_process_list = LAZY_INSTANCE_INITIALIZER; |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 63 | |
scottmg | 5e65e3a | 2017-03-08 08:48:46 | [diff] [blame] | 64 | base::LazyInstance<base::ObserverList<BrowserChildProcessObserver>>:: |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 65 | DestructorAtExit g_browser_child_process_observers = |
| 66 | LAZY_INSTANCE_INITIALIZER; |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 67 | |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 68 | void NotifyProcessLaunchedAndConnected(const ChildProcessData& data) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 69 | for (auto& observer : g_browser_child_process_observers.Get()) |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 70 | observer.BrowserChildProcessLaunchedAndConnected(data); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 71 | } |
| 72 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 73 | void NotifyProcessHostConnected(const ChildProcessData& data) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 74 | for (auto& observer : g_browser_child_process_observers.Get()) |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 75 | observer.BrowserChildProcessHostConnected(data); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void NotifyProcessHostDisconnected(const ChildProcessData& data) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 79 | for (auto& observer : g_browser_child_process_observers.Get()) |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 80 | observer.BrowserChildProcessHostDisconnected(data); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 81 | } |
| 82 | |
wfh | 22e2f4a2 | 2015-04-28 22:39:21 | [diff] [blame] | 83 | void NotifyProcessCrashed(const ChildProcessData& data, int exit_code) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 84 | for (auto& observer : g_browser_child_process_observers.Get()) |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 85 | observer.BrowserChildProcessCrashed(data, exit_code); |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 86 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 87 | |
wfh | f000329 | 2015-08-18 22:16:46 | [diff] [blame] | 88 | void NotifyProcessKilled(const ChildProcessData& data, int exit_code) { |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 89 | for (auto& observer : g_browser_child_process_observers.Get()) |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 90 | observer.BrowserChildProcessKilled(data, exit_code); |
wfh | f000329 | 2015-08-18 22:16:46 | [diff] [blame] | 91 | } |
| 92 | |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 93 | } // namespace |
| 94 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 95 | BrowserChildProcessHost* BrowserChildProcessHost::Create( |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 96 | content::ProcessType process_type, |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 97 | BrowserChildProcessHostDelegate* delegate) { |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 98 | return Create(process_type, delegate, std::string()); |
amistry | 6ad1e81 | 2016-06-06 05:36:30 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | BrowserChildProcessHost* BrowserChildProcessHost::Create( |
| 102 | content::ProcessType process_type, |
| 103 | BrowserChildProcessHostDelegate* delegate, |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 104 | const std::string& service_name) { |
| 105 | return new BrowserChildProcessHostImpl(process_type, delegate, service_name); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 106 | } |
| 107 | |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 108 | BrowserChildProcessHost* BrowserChildProcessHost::FromID(int child_process_id) { |
| 109 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 110 | BrowserChildProcessHostImpl::BrowserChildProcessList* process_list = |
| 111 | g_child_process_list.Pointer(); |
| 112 | for (BrowserChildProcessHostImpl* host : *process_list) { |
| 113 | if (host->GetData().id == child_process_id) |
| 114 | return host; |
| 115 | } |
| 116 | return nullptr; |
| 117 | } |
| 118 | |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 119 | #if defined(OS_MACOSX) |
erikchen | cd3e0e7b | 2015-10-01 22:53:51 | [diff] [blame] | 120 | base::PortProvider* BrowserChildProcessHost::GetPortProvider() { |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 121 | return MachBroker::GetInstance(); |
| 122 | } |
| 123 | #endif |
| 124 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 125 | // static |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 126 | BrowserChildProcessHostImpl::BrowserChildProcessList* |
| 127 | BrowserChildProcessHostImpl::GetIterator() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 128 | return g_child_process_list.Pointer(); |
| 129 | } |
| 130 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 131 | // static |
| 132 | void BrowserChildProcessHostImpl::AddObserver( |
| 133 | BrowserChildProcessObserver* observer) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 134 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 135 | g_browser_child_process_observers.Get().AddObserver(observer); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // static |
| 139 | void BrowserChildProcessHostImpl::RemoveObserver( |
| 140 | BrowserChildProcessObserver* observer) { |
| 141 | // TODO(phajdan.jr): Check thread after fixing http://crbug.com/167126. |
Daniel Bratell | bfcfe22 | 2017-10-17 17:57:00 | [diff] [blame] | 142 | g_browser_child_process_observers.Get().RemoveObserver(observer); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 145 | BrowserChildProcessHostImpl::BrowserChildProcessHostImpl( |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 146 | content::ProcessType process_type, |
amistry | 6ad1e81 | 2016-06-06 05:36:30 | [diff] [blame] | 147 | BrowserChildProcessHostDelegate* delegate, |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 148 | const std::string& service_name) |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 149 | : data_(process_type), |
[email protected] | 9dd9015 | 2013-08-02 22:09:13 | [diff] [blame] | 150 | delegate_(delegate), |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 151 | broker_client_invitation_(new mojo::edk::OutgoingBrokerClientInvitation), |
sadrul | 6c5aed8c | 2017-01-11 23:11:44 | [diff] [blame] | 152 | channel_(nullptr), |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 153 | is_channel_connected_(false), |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 154 | notify_child_disconnected_(false), |
| 155 | weak_factory_(this) { |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 156 | data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId(); |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 157 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 158 | child_process_host_.reset(ChildProcessHost::Create(this)); |
ssid | 72c5d682 | 2015-06-26 15:51:42 | [diff] [blame] | 159 | AddFilter(new TraceMessageFilter(data_.id)); |
[email protected] | 38917fe | 2011-04-06 17:29:12 | [diff] [blame] | 160 | |
| 161 | g_child_process_list.Get().push_back(this); |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 162 | GetContentClient()->browser()->BrowserChildProcessHostCreated(this); |
[email protected] | 30c4c67a | 2014-07-22 02:33:55 | [diff] [blame] | 163 | |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 164 | if (!service_name.empty()) { |
| 165 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
ben | 03ac5d01 | 2017-04-28 05:12:59 | [diff] [blame] | 166 | service_manager::Identity child_identity( |
| 167 | service_name, service_manager::mojom::kInheritUserID, |
| 168 | base::StringPrintf("%d", data_.id)); |
rockot | 1039b82 | 2017-02-09 23:19:41 | [diff] [blame] | 169 | child_connection_.reset( |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 170 | new ChildConnection(child_identity, broker_client_invitation_.get(), |
rockot | 1039b82 | 2017-02-09 23:19:41 | [diff] [blame] | 171 | ServiceManagerContext::GetConnectorForIOThread(), |
| 172 | base::ThreadTaskRunnerHandle::Get())); |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 173 | } |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 174 | |
| 175 | // Create a persistent memory segment for subprocess histograms. |
| 176 | CreateMetricsAllocator(); |
[email protected] | 38917fe | 2011-04-06 17:29:12 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 179 | BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() { |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 180 | g_child_process_list.Get().remove(this); |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 181 | |
| 182 | if (notify_child_disconnected_) { |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 183 | BrowserThread::PostTask( |
| 184 | BrowserThread::UI, FROM_HERE, |
| 185 | base::BindOnce(&NotifyProcessHostDisconnected, data_)); |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 186 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // static |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 190 | void BrowserChildProcessHostImpl::TerminateAll() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 191 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 192 | // Make a copy since the BrowserChildProcessHost dtor mutates the original |
| 193 | // list. |
| 194 | BrowserChildProcessList copy = g_child_process_list.Get(); |
[email protected] | 5998d9d | 2012-03-06 04:25:26 | [diff] [blame] | 195 | for (BrowserChildProcessList::iterator it = copy.begin(); |
| 196 | it != copy.end(); ++it) { |
| 197 | delete (*it)->delegate(); // ~*HostDelegate deletes *HostImpl. |
| 198 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 199 | } |
| 200 | |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 201 | // static |
lawrencewu | 0b496492 | 2016-10-25 16:28:14 | [diff] [blame] | 202 | void BrowserChildProcessHostImpl::CopyFeatureAndFieldTrialFlags( |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 203 | base::CommandLine* cmd_line) { |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 204 | // If we run base::FieldTrials, we want to pass to their state to the |
| 205 | // child process so that it can act in accordance with each state. |
lawrencewu | 5e03cd3 | 2016-12-05 16:23:28 | [diff] [blame] | 206 | base::FieldTrialList::CopyFieldTrialStateToFlags( |
| 207 | switches::kFieldTrialHandle, switches::kEnableFeatures, |
| 208 | switches::kDisableFeatures, cmd_line); |
erikchen | 55edbff | 2016-05-03 23:53:17 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 211 | void BrowserChildProcessHostImpl::Launch( |
jcivelli | d20cc0b | 2016-12-22 03:51:41 | [diff] [blame] | 212 | std::unique_ptr<SandboxedProcessLauncherDelegate> delegate, |
| 213 | std::unique_ptr<base::CommandLine> cmd_line, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 214 | bool terminate_on_shutdown) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 215 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | b80f6843 | 2011-05-02 17:22:30 | [diff] [blame] | 216 | |
jcivelli | d20cc0b | 2016-12-22 03:51:41 | [diff] [blame] | 217 | GetContentClient()->browser()->AppendExtraCommandLineSwitches(cmd_line.get(), |
| 218 | data_.id); |
[email protected] | b80f6843 | 2011-05-02 17:22:30 | [diff] [blame] | 219 | |
[email protected] | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 220 | const base::CommandLine& browser_command_line = |
| 221 | *base::CommandLine::ForCurrentProcess(); |
thestig | 418c505 | 2016-04-11 19:39:19 | [diff] [blame] | 222 | static const char* const kForwardSwitches[] = { |
ochang | 6a8400e | 2017-05-01 21:28:24 | [diff] [blame] | 223 | service_manager::switches::kDisableInProcessStackTraces, |
| 224 | switches::kDisableLogging, |
| 225 | switches::kEnableLogging, |
| 226 | switches::kIPCConnectionTimeout, |
| 227 | switches::kLoggingLevel, |
| 228 | switches::kTraceToConsole, |
| 229 | switches::kV, |
| 230 | switches::kVModule, |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 231 | }; |
| 232 | cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches, |
| 233 | arraysize(kForwardSwitches)); |
[email protected] | 719a205 | 2012-07-30 21:00:43 | [diff] [blame] | 234 | |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 235 | if (child_connection_) { |
ben | d32292b | 2016-10-07 00:21:58 | [diff] [blame] | 236 | cmd_line->AppendSwitchASCII(switches::kServiceRequestChannelToken, |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 237 | child_connection_->service_token()); |
| 238 | } |
| 239 | |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 240 | DCHECK(broker_client_invitation_); |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 241 | notify_child_disconnected_ = true; |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 242 | child_process_.reset(new ChildProcessLauncher( |
rockot | 1039b82 | 2017-02-09 23:19:41 | [diff] [blame] | 243 | std::move(delegate), std::move(cmd_line), data_.id, this, |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 244 | std::move(broker_client_invitation_), |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 245 | base::Bind(&BrowserChildProcessHostImpl::OnMojoError, |
| 246 | weak_factory_.GetWeakPtr(), |
| 247 | base::ThreadTaskRunnerHandle::Get()), |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 248 | terminate_on_shutdown)); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 249 | } |
| 250 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 251 | const ChildProcessData& BrowserChildProcessHostImpl::GetData() const { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 252 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 253 | return data_; |
| 254 | } |
| 255 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 256 | ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 257 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 258 | return child_process_host_.get(); |
| 259 | } |
| 260 | |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 261 | const base::Process& BrowserChildProcessHostImpl::GetProcess() const { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 262 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 263 | DCHECK(child_process_.get()) |
| 264 | << "Requesting a child process handle before launching."; |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 265 | DCHECK(child_process_->GetProcess().IsValid()) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 266 | << "Requesting a child process handle before launch has completed OK."; |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 267 | return child_process_->GetProcess(); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 268 | } |
| 269 | |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 270 | std::unique_ptr<base::SharedPersistentMemoryAllocator> |
| 271 | BrowserChildProcessHostImpl::TakeMetricsAllocator() { |
| 272 | return std::move(metrics_allocator_); |
| 273 | } |
| 274 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 275 | void BrowserChildProcessHostImpl::SetName(const base::string16& name) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 276 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 277 | data_.name = name; |
| 278 | } |
| 279 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 280 | void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 281 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 282 | data_.handle = handle; |
| 283 | } |
| 284 | |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 285 | service_manager::mojom::ServiceRequest |
| 286 | BrowserChildProcessHostImpl::TakeInProcessServiceRequest() { |
| 287 | DCHECK(broker_client_invitation_); |
| 288 | auto invitation = std::move(broker_client_invitation_); |
Ken Rockot | f4d8a94 | 2017-05-13 00:10:37 | [diff] [blame] | 289 | return service_manager::mojom::ServiceRequest( |
Ken Rockot | ea1716a0 | 2017-05-11 05:49:10 | [diff] [blame] | 290 | invitation->ExtractInProcessMessagePipe( |
| 291 | child_connection_->service_token())); |
sammc | 2b0375b | 2016-12-22 01:26:17 | [diff] [blame] | 292 | } |
| 293 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 294 | void BrowserChildProcessHostImpl::ForceShutdown() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 295 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 296 | g_child_process_list.Get().remove(this); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 297 | child_process_host_->ForceShutdown(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 298 | } |
| 299 | |
[email protected] | ba780c1 | 2013-10-01 17:07:06 | [diff] [blame] | 300 | void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter* filter) { |
| 301 | child_process_host_->AddFilter(filter->GetFilter()); |
| 302 | } |
| 303 | |
ben | a06f7d6 | 2017-04-04 20:48:52 | [diff] [blame] | 304 | void BrowserChildProcessHostImpl::BindInterface( |
| 305 | const std::string& interface_name, |
| 306 | mojo::ScopedMessagePipeHandle interface_pipe) { |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 307 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 308 | if (!child_connection_) |
ben | a06f7d6 | 2017-04-04 20:48:52 | [diff] [blame] | 309 | return; |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 310 | |
ben | a06f7d6 | 2017-04-04 20:48:52 | [diff] [blame] | 311 | child_connection_->BindInterface(interface_name, std::move(interface_pipe)); |
rockot | da988790 | 2016-08-19 20:46:42 | [diff] [blame] | 312 | } |
| 313 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 314 | void BrowserChildProcessHostImpl::HistogramBadMessageTerminated( |
Wei-Yin Chen (陳威尹) | 223326c | 2017-07-21 02:08:28 | [diff] [blame] | 315 | ProcessType process_type) { |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 316 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.BadMessgeTerminated", process_type, |
| 317 | PROCESS_TYPE_MAX); |
| 318 | } |
| 319 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 320 | base::TerminationStatus BrowserChildProcessHostImpl::GetTerminationStatus( |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 321 | bool known_dead, int* exit_code) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 322 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 323 | if (!child_process_) // If the delegate doesn't use Launch() helper. |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 324 | return base::GetTerminationStatus(data_.handle, exit_code); |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 325 | return child_process_->GetChildTerminationStatus(known_dead, |
[email protected] | c7691de | 2012-12-06 08:31:51 | [diff] [blame] | 326 | exit_code); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 327 | } |
| 328 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 329 | bool BrowserChildProcessHostImpl::OnMessageReceived( |
| 330 | const IPC::Message& message) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 331 | return delegate_->OnMessageReceived(message); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 332 | } |
| 333 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 334 | void BrowserChildProcessHostImpl::OnChannelConnected(int32_t peer_pid) { |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 335 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 336 | |
| 337 | is_channel_connected_ = true; |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 338 | notify_child_disconnected_ = true; |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 339 | |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 340 | #if defined(OS_WIN) |
| 341 | // From this point onward, the exit of the child process is detected by an |
| 342 | // error on the IPC channel. |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 343 | early_exit_watcher_.StopWatching(); |
| 344 | #endif |
| 345 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 346 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 347 | base::BindOnce(&NotifyProcessHostConnected, data_)); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 348 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 349 | delegate_->OnChannelConnected(peer_pid); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 350 | |
| 351 | if (IsProcessLaunched()) { |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 352 | ShareMetricsAllocatorToProcess(); |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 353 | BrowserThread::PostTask( |
| 354 | BrowserThread::UI, FROM_HERE, |
| 355 | base::BindOnce(&NotifyProcessLaunchedAndConnected, data_)); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 356 | } |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 357 | } |
| 358 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 359 | void BrowserChildProcessHostImpl::OnChannelError() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 360 | delegate_->OnChannelError(); |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 361 | } |
| 362 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 363 | void BrowserChildProcessHostImpl::OnBadMessageReceived( |
| 364 | const IPC::Message& message) { |
sadrul | 73f39a6 | 2017-01-11 04:07:24 | [diff] [blame] | 365 | std::string log_message = |
| 366 | base::StringPrintf("Bad message received of type: %u", message.type()); |
| 367 | TerminateOnBadMessageReceived(log_message); |
jbauman | 278b2cc | 2016-01-08 22:48:53 | [diff] [blame] | 368 | } |
| 369 | |
sadrul | 73f39a6 | 2017-01-11 04:07:24 | [diff] [blame] | 370 | void BrowserChildProcessHostImpl::TerminateOnBadMessageReceived( |
| 371 | const std::string& error) { |
Wei-Yin Chen (陳威尹) | 223326c | 2017-07-21 02:08:28 | [diff] [blame] | 372 | HistogramBadMessageTerminated(static_cast<ProcessType>(data_.process_type)); |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 373 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | e44d134 | 2014-05-16 21:29:33 | [diff] [blame] | 374 | switches::kDisableKillAfterBadIPC)) { |
| 375 | return; |
| 376 | } |
sadrul | 73f39a6 | 2017-01-11 04:07:24 | [diff] [blame] | 377 | LOG(ERROR) << "Terminating child process for bad IPC message: " << error; |
wfh | af32d0b | 2015-04-07 02:21:36 | [diff] [blame] | 378 | // Create a memory dump. This will contain enough stack frames to work out |
| 379 | // what the bad message was. |
| 380 | base::debug::DumpWithoutCrashing(); |
| 381 | |
rvargas | 486b2f56 | 2015-03-18 01:36:33 | [diff] [blame] | 382 | child_process_->GetProcess().Terminate(RESULT_CODE_KILLED_BAD_MESSAGE, false); |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 383 | } |
| 384 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 385 | bool BrowserChildProcessHostImpl::CanShutdown() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 386 | return delegate_->CanShutdown(); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 387 | } |
| 388 | |
sadrul | 6c5aed8c | 2017-01-11 23:11:44 | [diff] [blame] | 389 | void BrowserChildProcessHostImpl::OnChannelInitialized(IPC::Channel* channel) { |
| 390 | channel_ = channel; |
| 391 | } |
| 392 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 393 | void BrowserChildProcessHostImpl::OnChildDisconnected() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 394 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
jbauman | 319402e | 2014-10-21 17:52:21 | [diff] [blame] | 395 | #if defined(OS_WIN) |
| 396 | // OnChildDisconnected may be called without OnChannelConnected, so stop the |
| 397 | // early exit watcher so GetTerminationStatus can close the process handle. |
| 398 | early_exit_watcher_.StopWatching(); |
| 399 | #endif |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 400 | if (child_process_.get() || data_.handle) { |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 401 | int exit_code; |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 402 | base::TerminationStatus status = GetTerminationStatus( |
| 403 | true /* known_dead */, &exit_code); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 404 | switch (status) { |
| 405 | case base::TERMINATION_STATUS_PROCESS_CRASHED: |
| 406 | case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: { |
| 407 | delegate_->OnProcessCrashed(exit_code); |
wfh | 22e2f4a2 | 2015-04-28 22:39:21 | [diff] [blame] | 408 | BrowserThread::PostTask( |
| 409 | BrowserThread::UI, FROM_HERE, |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 410 | base::BindOnce(&NotifyProcessCrashed, data_, exit_code)); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 411 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 412 | static_cast<ProcessType>(data_.process_type), |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 413 | PROCESS_TYPE_MAX); |
| 414 | break; |
| 415 | } |
derekjchow | 1b0f3bb | 2015-07-30 18:51:10 | [diff] [blame] | 416 | #if defined(OS_ANDROID) |
| 417 | case base::TERMINATION_STATUS_OOM_PROTECTED: |
| 418 | #endif |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 419 | #if defined(OS_CHROMEOS) |
| 420 | case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM: |
| 421 | #endif |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 422 | case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: { |
| 423 | delegate_->OnProcessCrashed(exit_code); |
wfh | f000329 | 2015-08-18 22:16:46 | [diff] [blame] | 424 | BrowserThread::PostTask( |
| 425 | BrowserThread::UI, FROM_HERE, |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 426 | base::BindOnce(&NotifyProcessKilled, data_, exit_code)); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 427 | // Report that this child process was killed. |
| 428 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 429 | static_cast<ProcessType>(data_.process_type), |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 430 | PROCESS_TYPE_MAX); |
| 431 | break; |
| 432 | } |
| 433 | case base::TERMINATION_STATUS_STILL_RUNNING: { |
| 434 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 435 | static_cast<ProcessType>(data_.process_type), |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 436 | PROCESS_TYPE_MAX); |
| 437 | } |
| 438 | default: |
| 439 | break; |
[email protected] | 12fed81 | 2011-09-16 05:47:29 | [diff] [blame] | 440 | } |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 441 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 442 | static_cast<ProcessType>(data_.process_type), |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 443 | PROCESS_TYPE_MAX); |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 444 | #if defined(OS_CHROMEOS) |
| 445 | if (status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM) { |
| 446 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2.OOM", |
Wei-Yin Chen (陳威尹) | 3330991f | 2017-07-27 17:25:57 | [diff] [blame] | 447 | static_cast<ProcessType>(data_.process_type), |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 448 | PROCESS_TYPE_MAX); |
| 449 | } |
| 450 | #endif |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 451 | } |
sadrul | 6c5aed8c | 2017-01-11 23:11:44 | [diff] [blame] | 452 | channel_ = nullptr; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 453 | delete delegate_; // Will delete us |
[email protected] | a3a7e2c | 2011-09-16 23:07:05 | [diff] [blame] | 454 | } |
| 455 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 456 | bool BrowserChildProcessHostImpl::Send(IPC::Message* message) { |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 457 | return child_process_host_->Send(message); |
| 458 | } |
| 459 | |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 460 | void BrowserChildProcessHostImpl::CreateMetricsAllocator() { |
| 461 | // Create a persistent memory segment for subprocess histograms only if |
| 462 | // they're active in the browser. |
| 463 | // TODO(bcwhite): Remove this once persistence is always enabled. |
| 464 | if (!base::GlobalHistogramAllocator::Get()) |
| 465 | return; |
| 466 | |
| 467 | // Determine the correct parameters based on the process type. |
| 468 | size_t memory_size; |
| 469 | base::StringPiece metrics_name; |
| 470 | switch (data_.process_type) { |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 471 | case PROCESS_TYPE_UTILITY: |
Matt Menke | e5bc4a56 | 2017-08-07 23:58:22 | [diff] [blame] | 472 | // This needs to be larger for the network service. |
| 473 | memory_size = 256 << 10; // 256 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 474 | metrics_name = "UtilityMetrics"; |
| 475 | break; |
| 476 | |
| 477 | case PROCESS_TYPE_ZYGOTE: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 478 | memory_size = 64 << 10; // 64 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 479 | metrics_name = "ZygoteMetrics"; |
| 480 | break; |
| 481 | |
| 482 | case PROCESS_TYPE_SANDBOX_HELPER: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 483 | memory_size = 64 << 10; // 64 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 484 | metrics_name = "SandboxHelperMetrics"; |
| 485 | break; |
| 486 | |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 487 | case PROCESS_TYPE_GPU: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 488 | memory_size = 64 << 10; // 64 KiB |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 489 | metrics_name = "GpuMetrics"; |
| 490 | break; |
| 491 | |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 492 | case PROCESS_TYPE_PPAPI_PLUGIN: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 493 | memory_size = 64 << 10; // 64 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 494 | metrics_name = "PpapiPluginMetrics"; |
| 495 | break; |
| 496 | |
| 497 | case PROCESS_TYPE_PPAPI_BROKER: |
bcwhite | 867f4553 | 2017-01-05 17:29:02 | [diff] [blame] | 498 | memory_size = 64 << 10; // 64 KiB |
bcwhite | 2496c83e | 2016-08-29 17:29:25 | [diff] [blame] | 499 | metrics_name = "PpapiBrokerMetrics"; |
| 500 | break; |
| 501 | |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 502 | default: |
bcwhite | c600ac1 | 2017-07-12 13:37:13 | [diff] [blame] | 503 | // Report new processes. "Custom" ones are renumbered to 1000+ so that |
| 504 | // they won't conflict with any standard ones in the future. |
| 505 | int process_type = data_.process_type; |
| 506 | if (process_type >= PROCESS_TYPE_CONTENT_END) |
| 507 | process_type += 1000 - PROCESS_TYPE_CONTENT_END; |
| 508 | UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 509 | "UMA.SubprocessMetricsProvider.UntrackedProcesses", process_type); |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 510 | return; |
| 511 | } |
| 512 | |
| 513 | // Create the shared memory segment and attach an allocator to it. |
| 514 | // Mapping the memory shouldn't fail but be safe if it does; everything |
| 515 | // will continue to work but just as if persistence weren't available. |
| 516 | std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); |
| 517 | if (!shm->CreateAndMapAnonymous(memory_size)) |
| 518 | return; |
| 519 | metrics_allocator_.reset(new base::SharedPersistentMemoryAllocator( |
| 520 | std::move(shm), static_cast<uint64_t>(data_.id), metrics_name, |
| 521 | /*readonly=*/false)); |
| 522 | } |
| 523 | |
| 524 | void BrowserChildProcessHostImpl::ShareMetricsAllocatorToProcess() { |
| 525 | if (metrics_allocator_) { |
Nicholas Verne | 91bf4a63 | 2017-10-12 21:14:24 | [diff] [blame] | 526 | HistogramController::GetInstance()->SetHistogramMemory<ChildProcessHost>( |
| 527 | GetHost(), |
| 528 | mojo::WrapSharedMemoryHandle( |
| 529 | metrics_allocator_->shared_memory()->handle().Duplicate(), |
| 530 | metrics_allocator_->shared_memory()->mapped_size(), false)); |
| 531 | } else { |
| 532 | HistogramController::GetInstance()->SetHistogramMemory<ChildProcessHost>( |
| 533 | GetHost(), mojo::ScopedSharedBufferHandle()); |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
wfh | 3adf87d | 2016-05-03 23:26:06 | [diff] [blame] | 537 | void BrowserChildProcessHostImpl::OnProcessLaunchFailed(int error_code) { |
| 538 | delegate_->OnProcessLaunchFailed(error_code); |
amistry | 012289f | 2016-04-01 00:56:37 | [diff] [blame] | 539 | notify_child_disconnected_ = false; |
[email protected] | fa01e47 | 2014-02-11 14:45:35 | [diff] [blame] | 540 | delete delegate_; // Will delete us |
| 541 | } |
| 542 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 543 | void BrowserChildProcessHostImpl::OnProcessLaunched() { |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 544 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 545 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 546 | const base::Process& process = child_process_->GetProcess(); |
| 547 | DCHECK(process.IsValid()); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 548 | |
rockot | fe2fecc | 2017-01-19 20:16:19 | [diff] [blame] | 549 | if (child_connection_) |
| 550 | child_connection_->SetProcessHandle(process.Handle()); |
| 551 | |
jam | 3c8c50c | 2015-11-25 18:48:29 | [diff] [blame] | 552 | #if defined(OS_WIN) |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 553 | // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the |
| 554 | // child process exits. This watcher is stopped once the IPC channel is |
| 555 | // connected and the exit of the child process is detecter by an error on the |
| 556 | // IPC channel thereafter. |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 557 | DCHECK(!early_exit_watcher_.GetWatchedObject()); |
jam | 7fab108 | 2015-09-29 02:26:18 | [diff] [blame] | 558 | early_exit_watcher_.StartWatchingOnce(process.Handle(), this); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 559 | #endif |
| 560 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 561 | // TODO(rvargas) crbug.com/417532: Don't store a handle. |
| 562 | data_.handle = process.Handle(); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 563 | delegate_->OnProcessLaunched(); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 564 | |
| 565 | if (is_channel_connected_) { |
bcwhite | b58a5fe | 2016-08-22 16:25:56 | [diff] [blame] | 566 | ShareMetricsAllocatorToProcess(); |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 567 | BrowserThread::PostTask( |
| 568 | BrowserThread::UI, FROM_HERE, |
| 569 | base::BindOnce(&NotifyProcessLaunchedAndConnected, data_)); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | bool BrowserChildProcessHostImpl::IsProcessLaunched() const { |
| 574 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 575 | |
| 576 | return child_process_.get() && child_process_->GetProcess().IsValid(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 577 | } |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 578 | |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 579 | // static |
| 580 | void BrowserChildProcessHostImpl::OnMojoError( |
| 581 | base::WeakPtr<BrowserChildProcessHostImpl> process, |
| 582 | scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 583 | const std::string& error) { |
| 584 | if (!task_runner->BelongsToCurrentThread()) { |
| 585 | task_runner->PostTask( |
tzik | 4fea24af | 2017-08-23 11:41:47 | [diff] [blame] | 586 | FROM_HERE, base::BindOnce(&BrowserChildProcessHostImpl::OnMojoError, |
| 587 | process, task_runner, error)); |
olka | 91f1d41 | 2016-11-21 15:24:53 | [diff] [blame] | 588 | return; |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 589 | } |
| 590 | if (!process) |
| 591 | return; |
Wei-Yin Chen (陳威尹) | 223326c | 2017-07-21 02:08:28 | [diff] [blame] | 592 | HistogramBadMessageTerminated( |
| 593 | static_cast<ProcessType>(process->data_.process_type)); |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 594 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 595 | switches::kDisableKillAfterBadIPC)) { |
| 596 | return; |
| 597 | } |
| 598 | LOG(ERROR) << "Terminating child process for bad Mojo message: " << error; |
| 599 | |
Yuzhu Shen | 32fcad7 | 2017-09-14 23:12:37 | [diff] [blame] | 600 | // Create a memory dump with the error message captured in a crash key value. |
| 601 | // This will make it easy to determine details about what interface call |
| 602 | // failed. |
Robert Sesek | 1419427e | 2017-12-07 15:01:32 | [diff] [blame^] | 603 | base::debug::ScopedCrashKeyString scoped_error_key( |
| 604 | bad_message::GetMojoErrorCrashKey(), error); |
rockot | 229fb28e | 2016-06-16 04:46:16 | [diff] [blame] | 605 | base::debug::DumpWithoutCrashing(); |
| 606 | process->child_process_->GetProcess().Terminate( |
| 607 | RESULT_CODE_KILLED_BAD_MESSAGE, false); |
| 608 | } |
| 609 | |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 610 | #if defined(OS_WIN) |
| 611 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 612 | void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) { |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 613 | OnChildDisconnected(); |
| 614 | } |
| 615 | |
| 616 | #endif |
| 617 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 618 | } // namespace content |