[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" |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 14 | #include "base/path_service.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( |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 63 | int 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) { |
| 83 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 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( |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 95 | int 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] | 38917fe | 2011-04-06 17:29:12 | [diff] [blame] | 109 | } |
| 110 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 111 | BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() { |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 112 | g_child_process_list.Get().remove(this); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 113 | |
| 114 | #if defined(OS_WIN) |
| 115 | DeleteProcessWaitableEvent(early_exit_watcher_.GetWatchedEvent()); |
| 116 | #endif |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // static |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 120 | void BrowserChildProcessHostImpl::TerminateAll() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 121 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 122 | // Make a copy since the BrowserChildProcessHost dtor mutates the original |
| 123 | // list. |
| 124 | BrowserChildProcessList copy = g_child_process_list.Get(); |
[email protected] | 5998d9d | 2012-03-06 04:25:26 | [diff] [blame] | 125 | for (BrowserChildProcessList::iterator it = copy.begin(); |
| 126 | it != copy.end(); ++it) { |
| 127 | delete (*it)->delegate(); // ~*HostDelegate deletes *HostImpl. |
| 128 | } |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 129 | } |
| 130 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 131 | void BrowserChildProcessHostImpl::Launch( |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 132 | #if defined(OS_WIN) |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 133 | SandboxedProcessLauncherDelegate* delegate, |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 134 | #elif defined(OS_POSIX) |
| 135 | bool use_zygote, |
[email protected] | b345c48 | 2013-08-30 18:00:39 | [diff] [blame] | 136 | const base::EnvironmentMap& environ, |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 137 | #endif |
| 138 | CommandLine* cmd_line) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 139 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | b80f6843 | 2011-05-02 17:22:30 | [diff] [blame] | 140 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 141 | GetContentClient()->browser()->AppendExtraCommandLineSwitches( |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 142 | cmd_line, data_.id); |
[email protected] | b80f6843 | 2011-05-02 17:22:30 | [diff] [blame] | 143 | |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 144 | const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); |
| 145 | static const char* kForwardSwitches[] = { |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 146 | switches::kDisableLogging, |
| 147 | switches::kEnableDCHECK, |
| 148 | switches::kEnableLogging, |
| 149 | switches::kLoggingLevel, |
[email protected] | 2bf64a9 | 2013-07-11 23:10:40 | [diff] [blame] | 150 | switches::kTraceToConsole, |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 151 | switches::kV, |
| 152 | switches::kVModule, |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 153 | #if defined(OS_POSIX) |
| 154 | switches::kChildCleanExit, |
| 155 | #endif |
[email protected] | c8e8cb94 | 2013-09-10 23:36:08 | [diff] [blame] | 156 | #if defined(OS_WIN) |
| 157 | switches::kEnableHighResolutionTime, |
| 158 | #endif |
[email protected] | 2b065f8 | 2012-10-18 07:11:16 | [diff] [blame] | 159 | }; |
| 160 | cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches, |
| 161 | arraysize(kForwardSwitches)); |
[email protected] | 719a205 | 2012-07-30 21:00:43 | [diff] [blame] | 162 | |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 163 | child_process_.reset(new ChildProcessLauncher( |
| 164 | #if defined(OS_WIN) |
[email protected] | 34f4868 | 2013-03-20 00:30:18 | [diff] [blame] | 165 | delegate, |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 166 | #elif defined(OS_POSIX) |
| 167 | use_zygote, |
| 168 | environ, |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 169 | child_process_host_->TakeClientFileDescriptor(), |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 170 | #endif |
| 171 | cmd_line, |
[email protected] | 40da3e0c | 2012-10-24 22:03:38 | [diff] [blame] | 172 | data_.id, |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 173 | this)); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 176 | const ChildProcessData& BrowserChildProcessHostImpl::GetData() const { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 177 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 178 | return data_; |
| 179 | } |
| 180 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 181 | ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 182 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 183 | return child_process_host_.get(); |
| 184 | } |
| 185 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 186 | base::ProcessHandle BrowserChildProcessHostImpl::GetHandle() const { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 187 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 5d84d01 | 2010-12-02 17:17:21 | [diff] [blame] | 188 | DCHECK(child_process_.get()) |
| 189 | << "Requesting a child process handle before launching."; |
| 190 | DCHECK(child_process_->GetHandle()) |
| 191 | << "Requesting a child process handle before launch has completed OK."; |
| 192 | return child_process_->GetHandle(); |
| 193 | } |
| 194 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 195 | void BrowserChildProcessHostImpl::SetName(const string16& name) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 196 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 197 | data_.name = name; |
| 198 | } |
| 199 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 200 | void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 201 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | f174efd | 2012-01-17 18:43:33 | [diff] [blame] | 202 | data_.handle = handle; |
| 203 | } |
| 204 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 205 | void BrowserChildProcessHostImpl::ForceShutdown() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 206 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 207 | g_child_process_list.Get().remove(this); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 208 | child_process_host_->ForceShutdown(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | d36860d | 2013-05-31 00:04:21 | [diff] [blame] | 211 | void BrowserChildProcessHostImpl::SetBackgrounded(bool backgrounded) { |
| 212 | child_process_->SetProcessBackgrounded(backgrounded); |
| 213 | } |
| 214 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 215 | void BrowserChildProcessHostImpl::SetTerminateChildOnShutdown( |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 216 | bool terminate_on_shutdown) { |
| 217 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 358cb8e | 2011-05-25 02:12:45 | [diff] [blame] | 218 | child_process_->SetTerminateChildOnShutdown(terminate_on_shutdown); |
| 219 | } |
| 220 | |
[email protected] | ba780c1 | 2013-10-01 17:07:06 | [diff] [blame^] | 221 | void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter* filter) { |
| 222 | child_process_host_->AddFilter(filter->GetFilter()); |
| 223 | } |
| 224 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 225 | void BrowserChildProcessHostImpl::NotifyProcessInstanceCreated( |
| 226 | const ChildProcessData& data) { |
| 227 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 228 | FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(), |
| 229 | BrowserChildProcessInstanceCreated(data)); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 230 | } |
| 231 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 232 | base::TerminationStatus BrowserChildProcessHostImpl::GetTerminationStatus( |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 233 | bool known_dead, int* exit_code) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 234 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 59383c78 | 2013-04-17 16:43:27 | [diff] [blame] | 235 | if (!child_process_) // If the delegate doesn't use Launch() helper. |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 236 | return base::GetTerminationStatus(data_.handle, exit_code); |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 237 | return child_process_->GetChildTerminationStatus(known_dead, |
[email protected] | c7691de | 2012-12-06 08:31:51 | [diff] [blame] | 238 | exit_code); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 239 | } |
| 240 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 241 | bool BrowserChildProcessHostImpl::OnMessageReceived( |
| 242 | const IPC::Message& message) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 243 | return delegate_->OnMessageReceived(message); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 244 | } |
| 245 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 246 | void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid) { |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 247 | #if defined(OS_WIN) |
| 248 | // From this point onward, the exit of the child process is detected by an |
| 249 | // error on the IPC channel. |
| 250 | DeleteProcessWaitableEvent(early_exit_watcher_.GetWatchedEvent()); |
| 251 | early_exit_watcher_.StopWatching(); |
| 252 | #endif |
| 253 | |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 254 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 255 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 256 | base::Bind(&NotifyProcessHostConnected, data_)); |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 257 | |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 258 | delegate_->OnChannelConnected(peer_pid); |
| 259 | } |
| 260 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 261 | void BrowserChildProcessHostImpl::OnChannelError() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 262 | delegate_->OnChannelError(); |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 263 | } |
| 264 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 265 | bool BrowserChildProcessHostImpl::CanShutdown() { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 266 | return delegate_->CanShutdown(); |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 267 | } |
| 268 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 269 | void BrowserChildProcessHostImpl::OnChildDisconnected() { |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 270 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 271 | if (child_process_.get() || data_.handle) { |
| 272 | DCHECK(data_.handle != base::kNullProcessHandle); |
| 273 | int exit_code; |
[email protected] | 547603d | 2013-08-27 17:59:19 | [diff] [blame] | 274 | base::TerminationStatus status = GetTerminationStatus( |
| 275 | true /* known_dead */, &exit_code); |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 276 | switch (status) { |
| 277 | case base::TERMINATION_STATUS_PROCESS_CRASHED: |
| 278 | case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: { |
| 279 | delegate_->OnProcessCrashed(exit_code); |
| 280 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 281 | base::Bind(&NotifyProcessCrashed, data_)); |
| 282 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2", |
| 283 | data_.process_type, |
| 284 | PROCESS_TYPE_MAX); |
| 285 | break; |
| 286 | } |
| 287 | case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: { |
| 288 | delegate_->OnProcessCrashed(exit_code); |
| 289 | // Report that this child process was killed. |
| 290 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2", |
| 291 | data_.process_type, |
| 292 | PROCESS_TYPE_MAX); |
| 293 | break; |
| 294 | } |
| 295 | case base::TERMINATION_STATUS_STILL_RUNNING: { |
| 296 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2", |
| 297 | data_.process_type, |
| 298 | PROCESS_TYPE_MAX); |
| 299 | } |
| 300 | default: |
| 301 | break; |
[email protected] | 12fed81 | 2011-09-16 05:47:29 | [diff] [blame] | 302 | } |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 303 | UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2", |
| 304 | data_.process_type, |
| 305 | PROCESS_TYPE_MAX); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 306 | } |
[email protected] | f4eaf7b9 | 2013-02-28 22:00:40 | [diff] [blame] | 307 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 308 | base::Bind(&NotifyProcessHostDisconnected, data_)); |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 309 | delete delegate_; // Will delete us |
[email protected] | a3a7e2c | 2011-09-16 23:07:05 | [diff] [blame] | 310 | } |
| 311 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 312 | bool BrowserChildProcessHostImpl::Send(IPC::Message* message) { |
[email protected] | 4cb4310 | 2011-12-02 20:24:49 | [diff] [blame] | 313 | return child_process_host_->Send(message); |
| 314 | } |
| 315 | |
[email protected] | 4c01d499 | 2012-01-23 23:33:01 | [diff] [blame] | 316 | void BrowserChildProcessHostImpl::OnProcessLaunched() { |
[email protected] | d8c7006 | 2013-04-24 00:22:34 | [diff] [blame] | 317 | base::ProcessHandle handle = child_process_->GetHandle(); |
| 318 | if (!handle) { |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 319 | delete delegate_; // Will delete us |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 320 | return; |
| 321 | } |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 322 | |
| 323 | #if defined(OS_WIN) |
| 324 | // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the |
| 325 | // child process exits. This watcher is stopped once the IPC channel is |
| 326 | // connected and the exit of the child process is detecter by an error on the |
| 327 | // IPC channel thereafter. |
| 328 | DCHECK(!early_exit_watcher_.GetWatchedEvent()); |
| 329 | early_exit_watcher_.StartWatching( |
[email protected] | d8c7006 | 2013-04-24 00:22:34 | [diff] [blame] | 330 | new base::WaitableEvent(handle), |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 331 | base::Bind(&BrowserChildProcessHostImpl::OnProcessExitedEarly, |
| 332 | base::Unretained(this))); |
| 333 | #endif |
| 334 | |
[email protected] | d8c7006 | 2013-04-24 00:22:34 | [diff] [blame] | 335 | data_.handle = handle; |
[email protected] | 4967f79 | 2012-01-20 22:14:40 | [diff] [blame] | 336 | delegate_->OnProcessLaunched(); |
[email protected] | d27893f6 | 2010-07-03 05:47:42 | [diff] [blame] | 337 | } |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 338 | |
[email protected] | 162a9fd | 2013-03-06 20:47:49 | [diff] [blame] | 339 | #if defined(OS_WIN) |
| 340 | |
| 341 | void BrowserChildProcessHostImpl::DeleteProcessWaitableEvent( |
| 342 | base::WaitableEvent* event) { |
| 343 | if (!event) |
| 344 | return; |
| 345 | |
| 346 | // The WaitableEvent does not own the process handle so ensure it does not |
| 347 | // close it. |
| 348 | event->Release(); |
| 349 | |
| 350 | delete event; |
| 351 | } |
| 352 | |
| 353 | void BrowserChildProcessHostImpl::OnProcessExitedEarly( |
| 354 | base::WaitableEvent* event) { |
| 355 | DeleteProcessWaitableEvent(event); |
| 356 | OnChildDisconnected(); |
| 357 | } |
| 358 | |
| 359 | #endif |
| 360 | |
[email protected] | 13075767 | 2012-10-24 00:26:19 | [diff] [blame] | 361 | } // namespace content |