[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" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 12 | #include "base/lazy_instance.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 13 | #include "base/logging.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame^] | 14 | #include "base/macros.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 15 | #include "base/metrics/histogram.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 16 | #include "base/stl_util.h" |
[email protected] | 10994d13 | 2013-06-11 07:16:18 | [diff] [blame] | 17 | #include "base/strings/string_util.h" |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 18 | #include "base/synchronization/waitable_event.h" |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame^] | 19 | #include "build/build_config.h" |
zhenw | c5db2a0 | 2015-12-11 23:31:43 | [diff] [blame] | 20 | #include "components/tracing/tracing_switches.h" |
[email protected] | 83ab4a28 | 2012-07-12 18:19:45 | [diff] [blame] | 21 | #include "content/browser/histogram_message_filter.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 22 | #include "content/browser/loader/resource_message_filter.h" |
petrcermak | 29bd405 | 2015-09-24 08:28:48 | [diff] [blame] | 23 | #include "content/browser/memory/memory_message_filter.h" |
[email protected] | 33047f1 | 2011-12-01 23:20:20 | [diff] [blame] | 24 | #include "content/browser/profiler_message_filter.h" |
[email protected] | 3a85b1f | 2013-02-01 04:47:40 | [diff] [blame] | 25 | #include "content/browser/tracing/trace_message_filter.h" |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 26 | #include "content/common/child_process_host_impl.h" |
John Abd-El-Malek | fbada2c | 2015-11-24 06:38:15 | [diff] [blame] | 27 | #include "content/common/child_process_messages.h" |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 28 | #include "content/public/browser/browser_child_process_host_delegate.h" |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 29 | #include "content/public/browser/browser_child_process_observer.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 30 | #include "content/public/browser/browser_thread.h" |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 31 | #include "content/public/browser/child_process_data.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 32 | #include "content/public/browser/content_browser_client.h" |
[email protected] | c08950d2 | 2011-10-13 22:20:29 | [diff] [blame] | 33 | #include "content/public/common/content_switches.h" |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 34 | #include "content/public/common/process_type.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 35 | #include "content/public/common/result_codes.h" |
erikchen | d8a3d902 | 2015-10-22 20:02:02 | [diff] [blame] | 36 | #include "ipc/attachment_broker.h" |
| 37 | #include "ipc/attachment_broker_privileged.h" |
John Abd-El-Malek | fbada2c | 2015-11-24 06:38:15 | [diff] [blame] | 38 | #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 39 | |
[email protected] | f167520 | 2012-07-09 15:18:00 | [diff] [blame] | 40 | #if defined(OS_MACOSX) |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 41 | #include "content/browser/mach_broker_mac.h" |
[email protected] | 3838ca1 | 2011-11-02 14:37:15 | [diff] [blame] | 42 | #endif |
[email protected] | a3a7e2c | 2011-09-16 23:07:05 | [diff] [blame] | 43 | |
jam | 1288a4e | 2015-12-09 02:11:53 | [diff] [blame] | 44 | |
| 45 | #if defined(MOJO_SHELL_CLIENT) |
| 46 | #include "content/browser/mojo/mojo_shell_client_host.h" |
| 47 | #include "content/common/mojo/mojo_shell_connection_impl.h" |
| 48 | #endif |
| 49 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 50 | namespace content { |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 51 | namespace { |
| 52 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 53 | static base::LazyInstance<BrowserChildProcessHostImpl::BrowserChildProcessList> |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 54 | g_child_process_list = LAZY_INSTANCE_INITIALIZER; |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 55 | |
brettw | f2a90a2 | 2015-06-03 23:42:54 | [diff] [blame] | 56 | base::LazyInstance<base::ObserverList<BrowserChildProcessObserver>> |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 57 | g_observers = LAZY_INSTANCE_INITIALIZER; |
| 58 | |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 59 | void NotifyProcessLaunchedAndConnected(const ChildProcessData& data) { |
| 60 | FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(), |
| 61 | BrowserChildProcessLaunchedAndConnected(data)); |
| 62 | } |
| 63 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 64 | void NotifyProcessHostConnected(const ChildProcessData& data) { |
| 65 | FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(), |
| 66 | BrowserChildProcessHostConnected(data)); |
| 67 | } |
| 68 | |
| 69 | void NotifyProcessHostDisconnected(const ChildProcessData& data) { |
| 70 | FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(), |
| 71 | BrowserChildProcessHostDisconnected(data)); |
| 72 | } |
| 73 | |
wfh | 22e2f4a2 | 2015-04-28 22:39:21 | [diff] [blame] | 74 | void NotifyProcessCrashed(const ChildProcessData& data, int exit_code) { |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 75 | FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(), |
wfh | 22e2f4a2 | 2015-04-28 22:39:21 | [diff] [blame] | 76 | BrowserChildProcessCrashed(data, exit_code)); |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 77 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 78 | |
wfh | f000329 | 2015-08-18 22:16:46 | [diff] [blame] | 79 | void NotifyProcessKilled(const ChildProcessData& data, int exit_code) { |
| 80 | FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(), |
| 81 | BrowserChildProcessKilled(data, exit_code)); |
| 82 | } |
| 83 | |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 84 | } // namespace |
| 85 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 86 | BrowserChildProcessHost* BrowserChildProcessHost::Create( |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 87 | content::ProcessType process_type, |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 88 | BrowserChildProcessHostDelegate* delegate) { |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 89 | return new BrowserChildProcessHostImpl(process_type, delegate); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 90 | } |
| 91 | |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 92 | BrowserChildProcessHost* BrowserChildProcessHost::FromID(int child_process_id) { |
| 93 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 94 | BrowserChildProcessHostImpl::BrowserChildProcessList* process_list = |
| 95 | g_child_process_list.Pointer(); |
| 96 | for (BrowserChildProcessHostImpl* host : *process_list) { |
| 97 | if (host->GetData().id == child_process_id) |
| 98 | return host; |
| 99 | } |
| 100 | return nullptr; |
| 101 | } |
| 102 | |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 103 | #if defined(OS_MACOSX) |
erikchen | cd3e0e7b | 2015-10-01 22:53:51 | [diff] [blame] | 104 | base::PortProvider* BrowserChildProcessHost::GetPortProvider() { |
[email protected] | 458433c | 2012-02-29 23:43:39 | [diff] [blame] | 105 | return MachBroker::GetInstance(); |
| 106 | } |
| 107 | #endif |
| 108 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 109 | // static |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 110 | BrowserChildProcessHostImpl::BrowserChildProcessList* |
| 111 | BrowserChildProcessHostImpl::GetIterator() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 112 | return g_child_process_list.Pointer(); |
| 113 | } |
| 114 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 115 | // static |
| 116 | void BrowserChildProcessHostImpl::AddObserver( |
| 117 | BrowserChildProcessObserver* observer) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 118 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 119 | g_observers.Get().AddObserver(observer); |
| 120 | } |
| 121 | |
| 122 | // static |
| 123 | void BrowserChildProcessHostImpl::RemoveObserver( |
| 124 | BrowserChildProcessObserver* observer) { |
| 125 | // TODO(phajdan.jr): Check thread after fixing http://crbug.com/167126. |
| 126 | g_observers.Get().RemoveObserver(observer); |
| 127 | } |
| 128 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 129 | BrowserChildProcessHostImpl::BrowserChildProcessHostImpl( |
vadimt | 379d7fe | 2015-04-01 00:09:35 | [diff] [blame] | 130 | content::ProcessType process_type, |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 131 | BrowserChildProcessHostDelegate* delegate) |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 132 | : data_(process_type), |
[email protected] | 9dd9015 | 2013-08-02 22:09:13 | [diff] [blame] | 133 | delegate_(delegate), |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 134 | power_monitor_message_broadcaster_(this), |
| 135 | is_channel_connected_(false) { |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 136 | data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId(); |
[email protected] | 4306c379 | 2011-12-02 01:57:53 | [diff] [blame] | 137 | |
erikchen | d8a3d902 | 2015-10-22 20:02:02 | [diff] [blame] | 138 | #if USE_ATTACHMENT_BROKER |
| 139 | // Construct the privileged attachment broker early in the life cycle of a |
| 140 | // child process. This ensures that when a test is being run in one of the |
| 141 | // single process modes, the global attachment broker is the privileged |
| 142 | // attachment broker, rather than an unprivileged attachment broker. |
| 143 | #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 144 | IPC::AttachmentBrokerPrivileged::CreateBrokerIfNeeded( |
| 145 | MachBroker::GetInstance()); |
| 146 | #else |
| 147 | IPC::AttachmentBrokerPrivileged::CreateBrokerIfNeeded(); |
| 148 | #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 149 | #endif // USE_ATTACHMENT_BROKER |
| 150 | |
[email protected] | 4734d0b | 2011-12-03 07:10:44 | [diff] [blame] | 151 | child_process_host_.reset(ChildProcessHost::Create(this)); |
ssid | 72c5d682 | 2015-06-26 15:51:42 | [diff] [blame] | 152 | AddFilter(new TraceMessageFilter(data_.id)); |
[email protected] | ba780c1 | 2013-10-01 17:07:06 | [diff] [blame] | 153 | AddFilter(new ProfilerMessageFilter(process_type)); |
| 154 | AddFilter(new HistogramMessageFilter); |
chrisha | da753d47 | 2015-12-08 00:57:13 | [diff] [blame] | 155 | AddFilter(new MemoryMessageFilter(this, process_type)); |
[email protected] | 38917fe | 2011-04-06 17:29:12 | [diff] [blame] | 156 | |
| 157 | g_child_process_list.Get().push_back(this); |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 158 | GetContentClient()->browser()->BrowserChildProcessHostCreated(this); |
[email protected] | 30c4c67a | 2014-07-22 02:33:55 | [diff] [blame] | 159 | |
| 160 | power_monitor_message_broadcaster_.Init(); |
[email protected] | 38917fe | 2011-04-06 17:29:12 | [diff] [blame] | 161 | } |
| 162 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 163 | BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() { |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 164 | g_child_process_list.Get().remove(this); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // static |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 168 | void BrowserChildProcessHostImpl::TerminateAll() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 169 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 170 | // Make a copy since the BrowserChildProcessHost dtor mutates the original |
| 171 | // list. |
| 172 | BrowserChildProcessList copy = g_child_process_list.Get(); |
[email protected] | 5998d9d | 2012-03-06 04:25:26 | [diff] [blame] | 173 | for (BrowserChildProcessList::iterator it = copy.begin(); |
| 174 | it != copy.end(); ++it) { |
| 175 | delete (*it)->delegate(); // ~*HostDelegate deletes *HostImpl. |
| 176 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 179 | void BrowserChildProcessHostImpl::Launch( |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 180 | SandboxedProcessLauncherDelegate* delegate, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 181 | base::CommandLine* cmd_line, |
| 182 | bool terminate_on_shutdown) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 183 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | b80f6843 | 2011-05-02 17:22:30 | [diff] [blame] | 184 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 185 | GetContentClient()->browser()->AppendExtraCommandLineSwitches( |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 186 | cmd_line, data_.id); |
[email protected] | b80f6843 | 2011-05-02 17:22:30 | [diff] [blame] | 187 | |
[email protected] | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 188 | const base::CommandLine& browser_command_line = |
| 189 | *base::CommandLine::ForCurrentProcess(); |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 190 | static const char* kForwardSwitches[] = { |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 191 | switches::kDisableLogging, |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 192 | switches::kEnableLogging, |
[email protected] | 65f0ff7 | 2014-06-20 20:32:15 | [diff] [blame] | 193 | switches::kIPCConnectionTimeout, |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 194 | switches::kLoggingLevel, |
[email protected] | 2bf64a9 | 2013-07-11 23:10:40 | [diff] [blame] | 195 | switches::kTraceToConsole, |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 196 | switches::kV, |
| 197 | switches::kVModule, |
jam | 76bcf0c | 2015-10-02 21:01:28 | [diff] [blame] | 198 | "use-new-edk", // TODO(use_chrome_edk): temporary. |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 199 | }; |
| 200 | cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches, |
| 201 | arraysize(kForwardSwitches)); |
[email protected] | 719a205 | 2012-07-30 21:00:43 | [diff] [blame] | 202 | |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 203 | child_process_.reset(new ChildProcessLauncher( |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 204 | delegate, |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 205 | cmd_line, |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 206 | data_.id, |
sievers | 954e37a | 2015-03-28 01:50:24 | [diff] [blame] | 207 | this, |
| 208 | terminate_on_shutdown)); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 211 | const ChildProcessData& BrowserChildProcessHostImpl::GetData() const { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 212 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 213 | return data_; |
| 214 | } |
| 215 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 216 | ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 217 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 218 | return child_process_host_.get(); |
| 219 | } |
| 220 | |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 221 | const base::Process& BrowserChildProcessHostImpl::GetProcess() const { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 222 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 223 | DCHECK(child_process_.get()) |
| 224 | << "Requesting a child process handle before launching."; |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 225 | DCHECK(child_process_->GetProcess().IsValid()) |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 226 | << "Requesting a child process handle before launch has completed OK."; |
rvargas | 5779b38 | 2014-11-18 20:44:11 | [diff] [blame] | 227 | return child_process_->GetProcess(); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 230 | void BrowserChildProcessHostImpl::SetName(const base::string16& name) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 231 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 232 | data_.name = name; |
| 233 | } |
| 234 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 235 | void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 236 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 237 | data_.handle = handle; |
| 238 | } |
| 239 | |
amistry | faa231a4 | 2015-05-20 01:49:12 | [diff] [blame] | 240 | ServiceRegistry* BrowserChildProcessHostImpl::GetServiceRegistry() { |
| 241 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 242 | return delegate_->GetServiceRegistry(); |
| 243 | } |
| 244 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 245 | void BrowserChildProcessHostImpl::ForceShutdown() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 246 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 247 | g_child_process_list.Get().remove(this); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 248 | child_process_host_->ForceShutdown(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 249 | } |
| 250 | |
[email protected] | d36860d | 2013-05-31 00:04:21 | [diff] [blame] | 251 | void BrowserChildProcessHostImpl::SetBackgrounded(bool backgrounded) { |
| 252 | child_process_->SetProcessBackgrounded(backgrounded); |
| 253 | } |
| 254 | |
[email protected] | ba780c1 | 2013-10-01 17:07:06 | [diff] [blame] | 255 | void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter* filter) { |
| 256 | child_process_host_->AddFilter(filter->GetFilter()); |
| 257 | } |
| 258 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 259 | void BrowserChildProcessHostImpl::NotifyProcessInstanceCreated( |
| 260 | const ChildProcessData& data) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 261 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 262 | FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(), |
| 263 | BrowserChildProcessInstanceCreated(data)); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 264 | } |
| 265 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 266 | void BrowserChildProcessHostImpl::HistogramBadMessageTerminated( |
| 267 | int process_type) { |
| 268 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.BadMessgeTerminated", process_type, |
| 269 | PROCESS_TYPE_MAX); |
| 270 | } |
| 271 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 272 | base::TerminationStatus BrowserChildProcessHostImpl::GetTerminationStatus( |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 273 | bool known_dead, int* exit_code) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 274 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 275 | if (!child_process_) // If the delegate doesn't use Launch() helper. |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 276 | return base::GetTerminationStatus(data_.handle, exit_code); |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 277 | return child_process_->GetChildTerminationStatus(known_dead, |
[email protected] | c7691de | 2012-12-06 08:31:51 | [diff] [blame] | 278 | exit_code); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 281 | bool BrowserChildProcessHostImpl::OnMessageReceived( |
| 282 | const IPC::Message& message) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 283 | return delegate_->OnMessageReceived(message); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 284 | } |
| 285 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame^] | 286 | void BrowserChildProcessHostImpl::OnChannelConnected(int32_t peer_pid) { |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 287 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 288 | |
| 289 | is_channel_connected_ = true; |
| 290 | |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 291 | #if defined(OS_WIN) |
| 292 | // From this point onward, the exit of the child process is detected by an |
| 293 | // error on the IPC channel. |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 294 | early_exit_watcher_.StopWatching(); |
| 295 | #endif |
| 296 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 297 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 298 | base::Bind(&NotifyProcessHostConnected, data_)); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 299 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 300 | delegate_->OnChannelConnected(peer_pid); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 301 | |
| 302 | if (IsProcessLaunched()) { |
| 303 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 304 | base::Bind(&NotifyProcessLaunchedAndConnected, |
| 305 | data_)); |
| 306 | } |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 307 | } |
| 308 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 309 | void BrowserChildProcessHostImpl::OnChannelError() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 310 | delegate_->OnChannelError(); |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 311 | } |
| 312 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 313 | void BrowserChildProcessHostImpl::OnBadMessageReceived( |
| 314 | const IPC::Message& message) { |
| 315 | HistogramBadMessageTerminated(data_.process_type); |
avi | 83883c8 | 2014-12-23 00:08:49 | [diff] [blame] | 316 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
[email protected] | e44d134 | 2014-05-16 21:29:33 | [diff] [blame] | 317 | switches::kDisableKillAfterBadIPC)) { |
| 318 | return; |
| 319 | } |
jamescook | da250581 | 2015-03-20 18:01:18 | [diff] [blame] | 320 | LOG(ERROR) << "Terminating child process for bad IPC message of type " |
| 321 | << message.type(); |
wfh | af32d0b | 2015-04-07 02:21:36 | [diff] [blame] | 322 | |
| 323 | // Create a memory dump. This will contain enough stack frames to work out |
| 324 | // what the bad message was. |
| 325 | base::debug::DumpWithoutCrashing(); |
| 326 | |
rvargas | 486b2f56 | 2015-03-18 01:36:33 | [diff] [blame] | 327 | child_process_->GetProcess().Terminate(RESULT_CODE_KILLED_BAD_MESSAGE, false); |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 330 | bool BrowserChildProcessHostImpl::CanShutdown() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 331 | return delegate_->CanShutdown(); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 332 | } |
| 333 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 334 | void BrowserChildProcessHostImpl::OnChildDisconnected() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 335 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
jbauman | 319402e | 2014-10-21 17:52:21 | [diff] [blame] | 336 | #if defined(OS_WIN) |
| 337 | // OnChildDisconnected may be called without OnChannelConnected, so stop the |
| 338 | // early exit watcher so GetTerminationStatus can close the process handle. |
| 339 | early_exit_watcher_.StopWatching(); |
| 340 | #endif |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 341 | if (child_process_.get() || data_.handle) { |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 342 | int exit_code; |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 343 | base::TerminationStatus status = GetTerminationStatus( |
| 344 | true /* known_dead */, &exit_code); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 345 | switch (status) { |
| 346 | case base::TERMINATION_STATUS_PROCESS_CRASHED: |
| 347 | case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: { |
| 348 | delegate_->OnProcessCrashed(exit_code); |
wfh | 22e2f4a2 | 2015-04-28 22:39:21 | [diff] [blame] | 349 | BrowserThread::PostTask( |
| 350 | BrowserThread::UI, FROM_HERE, |
| 351 | base::Bind(&NotifyProcessCrashed, data_, exit_code)); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 352 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2", |
| 353 | data_.process_type, |
| 354 | PROCESS_TYPE_MAX); |
| 355 | break; |
| 356 | } |
derekjchow | 1b0f3bb | 2015-07-30 18:51:10 | [diff] [blame] | 357 | #if defined(OS_ANDROID) |
| 358 | case base::TERMINATION_STATUS_OOM_PROTECTED: |
| 359 | #endif |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 360 | #if defined(OS_CHROMEOS) |
| 361 | case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM: |
| 362 | #endif |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 363 | case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: { |
| 364 | delegate_->OnProcessCrashed(exit_code); |
wfh | f000329 | 2015-08-18 22:16:46 | [diff] [blame] | 365 | BrowserThread::PostTask( |
| 366 | BrowserThread::UI, FROM_HERE, |
| 367 | base::Bind(&NotifyProcessKilled, data_, exit_code)); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 368 | // Report that this child process was killed. |
| 369 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2", |
| 370 | data_.process_type, |
| 371 | PROCESS_TYPE_MAX); |
| 372 | break; |
| 373 | } |
| 374 | case base::TERMINATION_STATUS_STILL_RUNNING: { |
| 375 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2", |
| 376 | data_.process_type, |
| 377 | PROCESS_TYPE_MAX); |
| 378 | } |
| 379 | default: |
| 380 | break; |
[email protected] | 12fed81 | 2011-09-16 05:47:29 | [diff] [blame] | 381 | } |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 382 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2", |
| 383 | data_.process_type, |
| 384 | PROCESS_TYPE_MAX); |
oshima | 62022572 | 2015-06-04 19:45:27 | [diff] [blame] | 385 | #if defined(OS_CHROMEOS) |
| 386 | if (status == base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM) { |
| 387 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2.OOM", |
| 388 | data_.process_type, |
| 389 | PROCESS_TYPE_MAX); |
| 390 | } |
| 391 | #endif |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 392 | } |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 393 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 394 | base::Bind(&NotifyProcessHostDisconnected, data_)); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 395 | delete delegate_; // Will delete us |
[email protected] | a3a7e2c | 2011-09-16 23:07:05 | [diff] [blame] | 396 | } |
| 397 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 398 | bool BrowserChildProcessHostImpl::Send(IPC::Message* message) { |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 399 | return child_process_host_->Send(message); |
| 400 | } |
| 401 | |
[email protected] | fa01e47 | 2014-02-11 14:45:35 | [diff] [blame] | 402 | void BrowserChildProcessHostImpl::OnProcessLaunchFailed() { |
| 403 | delegate_->OnProcessLaunchFailed(); |
| 404 | delete delegate_; // Will delete us |
| 405 | } |
| 406 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 407 | void BrowserChildProcessHostImpl::OnProcessLaunched() { |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 408 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 409 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 410 | const base::Process& process = child_process_->GetProcess(); |
| 411 | DCHECK(process.IsValid()); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 412 | |
John Abd-El-Malek | fbada2c | 2015-11-24 06:38:15 | [diff] [blame] | 413 | if (base::CommandLine::ForCurrentProcess()->HasSwitch("use-new-edk")) { |
jam | 1288a4e | 2015-12-09 02:11:53 | [diff] [blame] | 414 | mojo::embedder::ScopedPlatformHandle client_pipe; |
| 415 | #if defined(MOJO_SHELL_CLIENT) |
| 416 | if (IsRunningInMojoShell()) { |
| 417 | client_pipe = RegisterProcessWithBroker(process.Pid()); |
| 418 | } else |
| 419 | #endif |
| 420 | { |
| 421 | client_pipe = mojo::embedder::ChildProcessLaunched(process.Handle()); |
| 422 | } |
John Abd-El-Malek | 362d0c77 | 2015-11-24 21:29:26 | [diff] [blame] | 423 | Send(new ChildProcessMsg_SetMojoParentPipeHandle( |
jam | 3c8c50c | 2015-11-25 18:48:29 | [diff] [blame] | 424 | IPC::GetFileHandleForProcess( |
| 425 | #if defined(OS_WIN) |
| 426 | client_pipe.release().handle, |
| 427 | #else |
| 428 | client_pipe.release().fd, |
| 429 | #endif |
| 430 | process.Handle(), true))); |
John Abd-El-Malek | fbada2c | 2015-11-24 06:38:15 | [diff] [blame] | 431 | } |
| 432 | |
jam | 3c8c50c | 2015-11-25 18:48:29 | [diff] [blame] | 433 | #if defined(OS_WIN) |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 434 | // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the |
| 435 | // child process exits. This watcher is stopped once the IPC channel is |
| 436 | // connected and the exit of the child process is detecter by an error on the |
| 437 | // IPC channel thereafter. |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 438 | DCHECK(!early_exit_watcher_.GetWatchedObject()); |
jam | 7fab108 | 2015-09-29 02:26:18 | [diff] [blame] | 439 | early_exit_watcher_.StartWatchingOnce(process.Handle(), this); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 440 | #endif |
| 441 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 442 | // TODO(rvargas) crbug.com/417532: Don't store a handle. |
| 443 | data_.handle = process.Handle(); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 444 | delegate_->OnProcessLaunched(); |
afakhry | 9cc7900 | 2015-10-22 19:25:15 | [diff] [blame] | 445 | |
| 446 | if (is_channel_connected_) { |
| 447 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 448 | base::Bind(&NotifyProcessLaunchedAndConnected, |
| 449 | data_)); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | bool BrowserChildProcessHostImpl::IsProcessLaunched() const { |
| 454 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 455 | |
| 456 | return child_process_.get() && child_process_->GetProcess().IsValid(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 457 | } |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 458 | |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 459 | #if defined(OS_WIN) |
| 460 | |
rvargas | 079d184 | 2014-10-17 22:32:16 | [diff] [blame] | 461 | void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) { |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 462 | OnChildDisconnected(); |
| 463 | } |
| 464 | |
| 465 | #endif |
| 466 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 467 | } // namespace content |