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