Remove render view id from the audio input and output, part two!
[chromium-blink-merge.git] / content / browser / browser_child_process_host_impl.cc
blob08486fc7fa4c51c6284a25a35ae38b7d80f654c8
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/browser_child_process_host_impl.h"
7 #include "base/base_switches.h"
8 #include "base/bind.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/metrics/histogram.h"
14 #include "base/profiler/scoped_tracker.h"
15 #include "base/stl_util.h"
16 #include "base/strings/string_util.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "content/browser/histogram_message_filter.h"
19 #include "content/browser/loader/resource_message_filter.h"
20 #include "content/browser/profiler_message_filter.h"
21 #include "content/browser/tracing/trace_message_filter.h"
22 #include "content/common/child_process_host_impl.h"
23 #include "content/public/browser/browser_child_process_host_delegate.h"
24 #include "content/public/browser/browser_child_process_observer.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/child_process_data.h"
27 #include "content/public/browser/content_browser_client.h"
28 #include "content/public/common/content_switches.h"
29 #include "content/public/common/process_type.h"
30 #include "content/public/common/result_codes.h"
32 #if defined(OS_MACOSX)
33 #include "content/browser/mach_broker_mac.h"
34 #endif
36 namespace content {
37 namespace {
39 static base::LazyInstance<BrowserChildProcessHostImpl::BrowserChildProcessList>
40 g_child_process_list = LAZY_INSTANCE_INITIALIZER;
42 base::LazyInstance<ObserverList<BrowserChildProcessObserver> >
43 g_observers = LAZY_INSTANCE_INITIALIZER;
45 void NotifyProcessHostConnected(const ChildProcessData& data) {
46 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
47 BrowserChildProcessHostConnected(data));
50 void NotifyProcessHostDisconnected(const ChildProcessData& data) {
51 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
52 BrowserChildProcessHostDisconnected(data));
55 void NotifyProcessCrashed(const ChildProcessData& data) {
56 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
57 BrowserChildProcessCrashed(data));
60 } // namespace
62 BrowserChildProcessHost* BrowserChildProcessHost::Create(
63 content::ProcessType process_type,
64 BrowserChildProcessHostDelegate* delegate) {
65 return new BrowserChildProcessHostImpl(process_type, delegate);
68 #if defined(OS_MACOSX)
69 base::ProcessMetrics::PortProvider* BrowserChildProcessHost::GetPortProvider() {
70 return MachBroker::GetInstance();
72 #endif
74 // static
75 BrowserChildProcessHostImpl::BrowserChildProcessList*
76 BrowserChildProcessHostImpl::GetIterator() {
77 return g_child_process_list.Pointer();
80 // static
81 void BrowserChildProcessHostImpl::AddObserver(
82 BrowserChildProcessObserver* observer) {
83 DCHECK_CURRENTLY_ON(BrowserThread::UI);
84 g_observers.Get().AddObserver(observer);
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);
94 BrowserChildProcessHostImpl::BrowserChildProcessHostImpl(
95 content::ProcessType process_type,
96 BrowserChildProcessHostDelegate* delegate)
97 : data_(process_type),
98 delegate_(delegate),
99 power_monitor_message_broadcaster_(this) {
100 data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId();
102 child_process_host_.reset(ChildProcessHost::Create(this));
103 AddFilter(new TraceMessageFilter);
104 AddFilter(new ProfilerMessageFilter(process_type));
105 AddFilter(new HistogramMessageFilter);
107 g_child_process_list.Get().push_back(this);
108 GetContentClient()->browser()->BrowserChildProcessHostCreated(this);
110 power_monitor_message_broadcaster_.Init();
113 BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() {
114 g_child_process_list.Get().remove(this);
117 // static
118 void BrowserChildProcessHostImpl::TerminateAll() {
119 DCHECK_CURRENTLY_ON(BrowserThread::IO);
120 // Make a copy since the BrowserChildProcessHost dtor mutates the original
121 // list.
122 BrowserChildProcessList copy = g_child_process_list.Get();
123 for (BrowserChildProcessList::iterator it = copy.begin();
124 it != copy.end(); ++it) {
125 delete (*it)->delegate(); // ~*HostDelegate deletes *HostImpl.
129 void BrowserChildProcessHostImpl::Launch(
130 SandboxedProcessLauncherDelegate* delegate,
131 base::CommandLine* cmd_line,
132 bool terminate_on_shutdown) {
133 DCHECK_CURRENTLY_ON(BrowserThread::IO);
135 GetContentClient()->browser()->AppendExtraCommandLineSwitches(
136 cmd_line, data_.id);
138 const base::CommandLine& browser_command_line =
139 *base::CommandLine::ForCurrentProcess();
140 static const char* kForwardSwitches[] = {
141 switches::kDisableLogging,
142 switches::kEnableLogging,
143 switches::kIPCConnectionTimeout,
144 switches::kLoggingLevel,
145 switches::kTraceToConsole,
146 switches::kV,
147 switches::kVModule,
149 cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches,
150 arraysize(kForwardSwitches));
152 child_process_.reset(new ChildProcessLauncher(
153 delegate,
154 cmd_line,
155 data_.id,
156 this,
157 terminate_on_shutdown));
160 const ChildProcessData& BrowserChildProcessHostImpl::GetData() const {
161 DCHECK_CURRENTLY_ON(BrowserThread::IO);
162 return data_;
165 ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const {
166 DCHECK_CURRENTLY_ON(BrowserThread::IO);
167 return child_process_host_.get();
170 const base::Process& BrowserChildProcessHostImpl::GetProcess() const {
171 DCHECK_CURRENTLY_ON(BrowserThread::IO);
172 DCHECK(child_process_.get())
173 << "Requesting a child process handle before launching.";
174 DCHECK(child_process_->GetProcess().IsValid())
175 << "Requesting a child process handle before launch has completed OK.";
176 return child_process_->GetProcess();
179 void BrowserChildProcessHostImpl::SetName(const base::string16& name) {
180 DCHECK_CURRENTLY_ON(BrowserThread::IO);
181 data_.name = name;
184 void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle) {
185 DCHECK_CURRENTLY_ON(BrowserThread::IO);
186 data_.handle = handle;
189 void BrowserChildProcessHostImpl::ForceShutdown() {
190 DCHECK_CURRENTLY_ON(BrowserThread::IO);
191 g_child_process_list.Get().remove(this);
192 child_process_host_->ForceShutdown();
195 void BrowserChildProcessHostImpl::SetBackgrounded(bool backgrounded) {
196 child_process_->SetProcessBackgrounded(backgrounded);
199 void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter* filter) {
200 child_process_host_->AddFilter(filter->GetFilter());
203 void BrowserChildProcessHostImpl::NotifyProcessInstanceCreated(
204 const ChildProcessData& data) {
205 DCHECK_CURRENTLY_ON(BrowserThread::UI);
206 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
207 BrowserChildProcessInstanceCreated(data));
210 void BrowserChildProcessHostImpl::HistogramBadMessageTerminated(
211 int process_type) {
212 UMA_HISTOGRAM_ENUMERATION("ChildProcess.BadMessgeTerminated", process_type,
213 PROCESS_TYPE_MAX);
216 base::TerminationStatus BrowserChildProcessHostImpl::GetTerminationStatus(
217 bool known_dead, int* exit_code) {
218 DCHECK_CURRENTLY_ON(BrowserThread::IO);
219 if (!child_process_) // If the delegate doesn't use Launch() helper.
220 return base::GetTerminationStatus(data_.handle, exit_code);
221 return child_process_->GetChildTerminationStatus(known_dead,
222 exit_code);
225 bool BrowserChildProcessHostImpl::OnMessageReceived(
226 const IPC::Message& message) {
227 return delegate_->OnMessageReceived(message);
230 void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid) {
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.
234 early_exit_watcher_.StopWatching();
235 #endif
237 DCHECK_CURRENTLY_ON(BrowserThread::IO);
238 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
239 base::Bind(&NotifyProcessHostConnected, data_));
241 delegate_->OnChannelConnected(peer_pid);
244 void BrowserChildProcessHostImpl::OnChannelError() {
245 delegate_->OnChannelError();
248 void BrowserChildProcessHostImpl::OnBadMessageReceived(
249 const IPC::Message& message) {
250 HistogramBadMessageTerminated(data_.process_type);
251 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
252 switches::kDisableKillAfterBadIPC)) {
253 return;
255 LOG(ERROR) << "Terminating child process for bad IPC message of type "
256 << message.type();
257 child_process_->GetProcess().Terminate(RESULT_CODE_KILLED_BAD_MESSAGE, false);
260 bool BrowserChildProcessHostImpl::CanShutdown() {
261 return delegate_->CanShutdown();
264 void BrowserChildProcessHostImpl::OnChildDisconnected() {
265 DCHECK_CURRENTLY_ON(BrowserThread::IO);
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
271 if (child_process_.get() || data_.handle) {
272 int exit_code;
273 base::TerminationStatus status = GetTerminationStatus(
274 true /* known_dead */, &exit_code);
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;
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;
294 case base::TERMINATION_STATUS_STILL_RUNNING: {
295 UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2",
296 data_.process_type,
297 PROCESS_TYPE_MAX);
299 default:
300 break;
302 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2",
303 data_.process_type,
304 PROCESS_TYPE_MAX);
306 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
307 base::Bind(&NotifyProcessHostDisconnected, data_));
308 delete delegate_; // Will delete us
311 bool BrowserChildProcessHostImpl::Send(IPC::Message* message) {
312 return child_process_host_->Send(message);
315 void BrowserChildProcessHostImpl::OnProcessLaunchFailed() {
316 delegate_->OnProcessLaunchFailed();
317 delete delegate_; // Will delete us
320 void BrowserChildProcessHostImpl::OnProcessLaunched() {
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"));
326 const base::Process& process = child_process_->GetProcess();
327 DCHECK(process.IsValid());
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.
334 DCHECK(!early_exit_watcher_.GetWatchedObject());
335 early_exit_watcher_.StartWatching(process.Handle(), this);
336 #endif
338 // TODO(rvargas) crbug.com/417532: Don't store a handle.
339 data_.handle = process.Handle();
340 delegate_->OnProcessLaunched();
343 #if defined(OS_WIN)
345 void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) {
346 OnChildDisconnected();
349 #endif
351 } // namespace content