[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" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 11 | #include "base/lazy_instance.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 13 | #include "base/metrics/histogram.h" |
erikchen | 8054a8c | 2015-03-11 02:34:29 | [diff] [blame] | 14 | #include "base/profiler/scoped_tracker.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 15 | #include "base/stl_util.h" |
[email protected] | 10994d13 | 2013-06-11 07:16:18 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 17 | #include "base/synchronization/waitable_event.h" |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 18 | #include "content/browser/histogram_message_filter.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 19 | #include "content/browser/loader/resource_message_filter.h" |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 20 | #include "content/browser/profiler_message_filter.h" |
[email protected] | 3a85b1f | 2013-02-01 04:47:40 | [diff] [blame] | 21 | #include "content/browser/tracing/trace_message_filter.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 22 | #include "content/common/child_process_host_impl.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 23 | #include "content/public/browser/browser_child_process_host_delegate.h" |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 24 | #include "content/public/browser/browser_child_process_observer.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 25 | #include "content/public/browser/browser_thread.h" |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 26 | #include "content/public/browser/child_process_data.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 27 | #include "content/public/browser/content_browser_client.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 28 | #include "content/public/common/content_switches.h" |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 29 | #include "content/public/common/process_type.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 30 | #include "content/public/common/result_codes.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 31 | |
[email protected] | f167520 | 2012-07-09 15:18:00 | [diff] [blame] | 32 | #if defined(OS_MACOSX) |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 33 | #include "content/browser/mach_broker_mac.h" |
[email protected] | 3838ca1 | 2011-11-02 14:37:15 | [diff] [blame] | 34 | #endif |
[email protected] | a3a7e2c | 2011-09-16 23:07:05 | [diff] [blame] | 35 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 36 | namespace content { |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 37 | namespace { |
| 38 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 39 | static base::LazyInstance<BrowserChildProcessHostImpl::BrowserChildProcessList> |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 40 | g_child_process_list = LAZY_INSTANCE_INITIALIZER; |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 41 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 42 | base::LazyInstance<ObserverList<BrowserChildProcessObserver> > |
| 43 | g_observers = LAZY_INSTANCE_INITIALIZER; |
| 44 | |
| 45 | void NotifyProcessHostConnected(const ChildProcessData& data) { |
| 46 | FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(), |
| 47 | BrowserChildProcessHostConnected(data)); |
| 48 | } |
| 49 | |
| 50 | void NotifyProcessHostDisconnected(const ChildProcessData& data) { |
| 51 | FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(), |
| 52 | BrowserChildProcessHostDisconnected(data)); |
| 53 | } |
| 54 | |
| 55 | void NotifyProcessCrashed(const ChildProcessData& data) { |
| 56 | FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(), |
| 57 | BrowserChildProcessCrashed(data)); |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 58 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 59 | |
| 60 | } // namespace |
| 61 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 62 | BrowserChildProcessHost* BrowserChildProcessHost::Create( |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 63 | content::ProcessType process_type, |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 64 | BrowserChildProcessHostDelegate* delegate) { |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 65 | return new BrowserChildProcessHostImpl(process_type, delegate); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 68 | #if defined(OS_MACOSX) |
| 69 | base::ProcessMetrics::PortProvider* BrowserChildProcessHost::GetPortProvider() { |
| 70 | return MachBroker::GetInstance(); |
| 71 | } |
| 72 | #endif |
| 73 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 74 | // static |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 75 | BrowserChildProcessHostImpl::BrowserChildProcessList* |
| 76 | BrowserChildProcessHostImpl::GetIterator() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 77 | return g_child_process_list.Pointer(); |
| 78 | } |
| 79 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 80 | // static |
| 81 | void BrowserChildProcessHostImpl::AddObserver( |
| 82 | BrowserChildProcessObserver* observer) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 83 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 84 | g_observers.Get().AddObserver(observer); |
| 85 | } |
| 86 | |
| 87 | // static |
| 88 | void BrowserChildProcessHostImpl::RemoveObserver( |
| 89 | BrowserChildProcessObserver* observer) { |
| 90 | // TODO(phajdan.jr): Check thread after fixing http://crbug.com/167126. |
| 91 | g_observers.Get().RemoveObserver(observer); |
| 92 | } |
| 93 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 94 | BrowserChildProcessHostImpl::BrowserChildProcessHostImpl( |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 95 | content::ProcessType process_type, |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 96 | BrowserChildProcessHostDelegate* delegate) |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 97 | : data_(process_type), |
[email protected] | 9dd9015 | 2013-08-02 22:09:13 | [diff] [blame] | 98 | delegate_(delegate), |
| 99 | power_monitor_message_broadcaster_(this) { |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 100 | data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId(); |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 101 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 102 | child_process_host_.reset(ChildProcessHost::Create(this)); |
[email protected] | ba780c1 | 2013-10-01 17:07:06 | [diff] [blame] | 103 | AddFilter(new TraceMessageFilter); |
| 104 | AddFilter(new ProfilerMessageFilter(process_type)); |
| 105 | AddFilter(new HistogramMessageFilter); |
[email protected] | 38917fe | 2011-04-06 17:29:12 | [diff] [blame] | 106 | |
| 107 | g_child_process_list.Get().push_back(this); |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 108 | GetContentClient()->browser()->BrowserChildProcessHostCreated(this); |
[email protected] | 30c4c67a | 2014-07-22 02:33:55 | [diff] [blame] | 109 | |
| 110 | power_monitor_message_broadcaster_.Init(); |
[email protected] | 38917fe | 2011-04-06 17:29:12 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 113 | BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() { |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 114 | g_child_process_list.Get().remove(this); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // static |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 118 | void BrowserChildProcessHostImpl::TerminateAll() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 119 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 120 | // Make a copy since the BrowserChildProcessHost dtor mutates the original |
| 121 | // list. |
| 122 | BrowserChildProcessList copy = g_child_process_list.Get(); |
[email protected] | 5998d9d | 2012-03-06 04:25:26 | [diff] [blame] | 123 | for (BrowserChildProcessList::iterator it = copy.begin(); |
| 124 | it != copy.end(); ++it) { |
| 125 | delete (*it)->delegate(); // ~*HostDelegate deletes *HostImpl. |
| 126 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 129 | void BrowserChildProcessHostImpl::Launch( |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 130 | SandboxedProcessLauncherDelegate* delegate, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 131 | base::CommandLine* cmd_line, |
| 132 | bool terminate_on_shutdown) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 133 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | b80f6843 | 2011-05-02 17:22:30 | [diff] [blame] | 134 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 135 | GetContentClient()->browser()->AppendExtraCommandLineSwitches( |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 136 | cmd_line, data_.id); |
[email protected] | b80f6843 | 2011-05-02 17:22:30 | [diff] [blame] | 137 | |
[email protected] | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 138 | const base::CommandLine& browser_command_line = |
| 139 | *base::CommandLine::ForCurrentProcess(); |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 140 | static const char* kForwardSwitches[] = { |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 141 | switches::kDisableLogging, |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 142 | switches::kEnableLogging, |
[email protected] | 65f0ff7 | 2014-06-20 20:32:15 | [diff] [blame] | 143 | switches::kIPCConnectionTimeout, |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 144 | switches::kLoggingLevel, |
[email protected] | 2bf64a9 | 2013-07-11 23:10:40 | [diff] [blame] | 145 | switches::kTraceToConsole, |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 146 | switches::kV, |
| 147 | switches::kVModule, |
| 148 | }; |
| 149 | cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches, |
| 150 | arraysize(kForwardSwitches)); |
[email protected] | 719a205 | 2012-07-30 21:00:43 | [diff] [blame] | 151 | |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 152 | child_process_.reset(new ChildProcessLauncher( |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 153 | delegate, |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 154 | cmd_line, |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 155 | data_.id, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 156 | this, |
| 157 | terminate_on_shutdown)); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 160 | const ChildProcessData& BrowserChildProcessHostImpl::GetData() const { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 161 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 162 | return data_; |
| 163 | } |
| 164 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 165 | ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 166 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 167 | return child_process_host_.get(); |
| 168 | } |
| 169 | |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 170 | const base::Process& BrowserChildProcessHostImpl::GetProcess() const { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 171 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 172 | DCHECK(child_process_.get()) |
| 173 | << "Requesting a child process handle before launching."; |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 174 | DCHECK(child_process_->GetProcess().IsValid()) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 175 | << "Requesting a child process handle before launch has completed OK."; |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 176 | return child_process_->GetProcess(); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 179 | void BrowserChildProcessHostImpl::SetName(const base::string16& name) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 180 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 181 | data_.name = name; |
| 182 | } |
| 183 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 184 | void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 185 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 186 | data_.handle = handle; |
| 187 | } |
| 188 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 189 | void BrowserChildProcessHostImpl::ForceShutdown() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 190 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 191 | g_child_process_list.Get().remove(this); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 192 | child_process_host_->ForceShutdown(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 193 | } |
| 194 | |
[email protected] | d36860d | 2013-05-31 00:04:21 | [diff] [blame] | 195 | void BrowserChildProcessHostImpl::SetBackgrounded(bool backgrounded) { |
| 196 | child_process_->SetProcessBackgrounded(backgrounded); |
| 197 | } |
| 198 | |
[email protected] | ba780c1 | 2013-10-01 17:07:06 | [diff] [blame] | 199 | void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter* filter) { |
| 200 | child_process_host_->AddFilter(filter->GetFilter()); |
| 201 | } |
| 202 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 203 | void BrowserChildProcessHostImpl::NotifyProcessInstanceCreated( |
| 204 | const ChildProcessData& data) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 205 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 206 | FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(), |
| 207 | BrowserChildProcessInstanceCreated(data)); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 210 | void BrowserChildProcessHostImpl::HistogramBadMessageTerminated( |
| 211 | int process_type) { |
| 212 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.BadMessgeTerminated", process_type, |
| 213 | PROCESS_TYPE_MAX); |
| 214 | } |
| 215 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 216 | base::TerminationStatus BrowserChildProcessHostImpl::GetTerminationStatus( |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 217 | bool known_dead, int* exit_code) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 218 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 219 | if (!child_process_) // If the delegate doesn't use Launch() helper. |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 220 | return base::GetTerminationStatus(data_.handle, exit_code); |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 221 | return child_process_->GetChildTerminationStatus(known_dead, |
[email protected] | c7691de | 2012-12-06 08:31:51 | [diff] [blame] | 222 | exit_code); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 225 | bool BrowserChildProcessHostImpl::OnMessageReceived( |
| 226 | const IPC::Message& message) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 227 | return delegate_->OnMessageReceived(message); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 230 | void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid) { |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 231 | #if defined(OS_WIN) |
| 232 | // From this point onward, the exit of the child process is detected by an |
| 233 | // error on the IPC channel. |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 234 | early_exit_watcher_.StopWatching(); |
| 235 | #endif |
| 236 | |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 237 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 238 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 239 | base::Bind(&NotifyProcessHostConnected, data_)); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 240 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 241 | delegate_->OnChannelConnected(peer_pid); |
| 242 | } |
| 243 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 244 | void BrowserChildProcessHostImpl::OnChannelError() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 245 | delegate_->OnChannelError(); |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 246 | } |
| 247 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 248 | void BrowserChildProcessHostImpl::OnBadMessageReceived( |
| 249 | const IPC::Message& message) { |
| 250 | HistogramBadMessageTerminated(data_.process_type); |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 251 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | e44d134 | 2014-05-16 21:29:33 | [diff] [blame] | 252 | switches::kDisableKillAfterBadIPC)) { |
| 253 | return; |
| 254 | } |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 255 | LOG(ERROR) << "Terminating child process for bad IPC message of type " |
| 256 | << message.type(); |
rvargas | 486b2f56 | 2015-03-18 01:36:33 | [diff] [blame] | 257 | child_process_->GetProcess().Terminate(RESULT_CODE_KILLED_BAD_MESSAGE, false); |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 258 | } |
| 259 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 260 | bool BrowserChildProcessHostImpl::CanShutdown() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 261 | return delegate_->CanShutdown(); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 262 | } |
| 263 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 264 | void BrowserChildProcessHostImpl::OnChildDisconnected() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 265 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
jbauman | 319402e | 2014-10-21 17:52:21 | [diff] [blame] | 266 | #if defined(OS_WIN) |
| 267 | // OnChildDisconnected may be called without OnChannelConnected, so stop the |
| 268 | // early exit watcher so GetTerminationStatus can close the process handle. |
| 269 | early_exit_watcher_.StopWatching(); |
| 270 | #endif |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 271 | if (child_process_.get() || data_.handle) { |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 272 | int exit_code; |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 273 | base::TerminationStatus status = GetTerminationStatus( |
| 274 | true /* known_dead */, &exit_code); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 275 | switch (status) { |
| 276 | case base::TERMINATION_STATUS_PROCESS_CRASHED: |
| 277 | case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: { |
| 278 | delegate_->OnProcessCrashed(exit_code); |
| 279 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 280 | base::Bind(&NotifyProcessCrashed, data_)); |
| 281 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2", |
| 282 | data_.process_type, |
| 283 | PROCESS_TYPE_MAX); |
| 284 | break; |
| 285 | } |
| 286 | case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: { |
| 287 | delegate_->OnProcessCrashed(exit_code); |
| 288 | // Report that this child process was killed. |
| 289 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2", |
| 290 | data_.process_type, |
| 291 | PROCESS_TYPE_MAX); |
| 292 | break; |
| 293 | } |
| 294 | case base::TERMINATION_STATUS_STILL_RUNNING: { |
| 295 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2", |
| 296 | data_.process_type, |
| 297 | PROCESS_TYPE_MAX); |
| 298 | } |
| 299 | default: |
| 300 | break; |
[email protected] | 12fed81 | 2011-09-16 05:47:29 | [diff] [blame] | 301 | } |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 302 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2", |
| 303 | data_.process_type, |
| 304 | PROCESS_TYPE_MAX); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 305 | } |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 306 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 307 | base::Bind(&NotifyProcessHostDisconnected, data_)); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 308 | delete delegate_; // Will delete us |
[email protected] | a3a7e2c | 2011-09-16 23:07:05 | [diff] [blame] | 309 | } |
| 310 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 311 | bool BrowserChildProcessHostImpl::Send(IPC::Message* message) { |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 312 | return child_process_host_->Send(message); |
| 313 | } |
| 314 | |
[email protected] | fa01e47 | 2014-02-11 14:45:35 | [diff] [blame] | 315 | void BrowserChildProcessHostImpl::OnProcessLaunchFailed() { |
| 316 | delegate_->OnProcessLaunchFailed(); |
| 317 | delete delegate_; // Will delete us |
| 318 | } |
| 319 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 320 | void BrowserChildProcessHostImpl::OnProcessLaunched() { |
erikchen | 8054a8c | 2015-03-11 02:34:29 | [diff] [blame] | 321 | // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/465841 |
| 322 | // is fixed. |
| 323 | tracked_objects::ScopedTracker tracking_profile( |
| 324 | FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 325 | "465841 BrowserChildProcessHostImpl::OnProcessLaunched")); |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 326 | const base::Process& process = child_process_->GetProcess(); |
| 327 | DCHECK(process.IsValid()); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 328 | |
| 329 | #if defined(OS_WIN) |
| 330 | // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the |
| 331 | // child process exits. This watcher is stopped once the IPC channel is |
| 332 | // connected and the exit of the child process is detecter by an error on the |
| 333 | // IPC channel thereafter. |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 334 | DCHECK(!early_exit_watcher_.GetWatchedObject()); |
| 335 | early_exit_watcher_.StartWatching(process.Handle(), this); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 336 | #endif |
| 337 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 338 | // TODO(rvargas) crbug.com/417532: Don't store a handle. |
| 339 | data_.handle = process.Handle(); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 340 | delegate_->OnProcessLaunched(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 341 | } |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 342 | |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 343 | #if defined(OS_WIN) |
| 344 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 345 | void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) { |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 346 | OnChildDisconnected(); |
| 347 | } |
| 348 | |
| 349 | #endif |
| 350 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 351 | } // namespace content |