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"
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/stl_util.h"
15 #include "base/strings/string_util.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "content/browser/histogram_message_filter.h"
18 #include "content/browser/loader/resource_message_filter.h"
19 #include "content/browser/profiler_message_filter.h"
20 #include "content/browser/tracing/trace_message_filter.h"
21 #include "content/common/child_process_host_impl.h"
22 #include "content/public/browser/browser_child_process_host_delegate.h"
23 #include "content/public/browser/browser_child_process_observer.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/child_process_data.h"
26 #include "content/public/browser/content_browser_client.h"
27 #include "content/public/common/content_switches.h"
28 #include "content/public/common/process_type.h"
29 #include "content/public/common/result_codes.h"
31 #if defined(OS_MACOSX)
32 #include "content/browser/mach_broker_mac.h"
38 static base::LazyInstance
<BrowserChildProcessHostImpl::BrowserChildProcessList
>
39 g_child_process_list
= LAZY_INSTANCE_INITIALIZER
;
41 base::LazyInstance
<ObserverList
<BrowserChildProcessObserver
> >
42 g_observers
= LAZY_INSTANCE_INITIALIZER
;
44 void NotifyProcessHostConnected(const ChildProcessData
& data
) {
45 FOR_EACH_OBSERVER(BrowserChildProcessObserver
, g_observers
.Get(),
46 BrowserChildProcessHostConnected(data
));
49 void NotifyProcessHostDisconnected(const ChildProcessData
& data
) {
50 FOR_EACH_OBSERVER(BrowserChildProcessObserver
, g_observers
.Get(),
51 BrowserChildProcessHostDisconnected(data
));
54 void NotifyProcessCrashed(const ChildProcessData
& data
) {
55 FOR_EACH_OBSERVER(BrowserChildProcessObserver
, g_observers
.Get(),
56 BrowserChildProcessCrashed(data
));
61 BrowserChildProcessHost
* BrowserChildProcessHost::Create(
63 BrowserChildProcessHostDelegate
* delegate
) {
64 return new BrowserChildProcessHostImpl(process_type
, delegate
);
67 #if defined(OS_MACOSX)
68 base::ProcessMetrics::PortProvider
* BrowserChildProcessHost::GetPortProvider() {
69 return MachBroker::GetInstance();
74 BrowserChildProcessHostImpl::BrowserChildProcessList
*
75 BrowserChildProcessHostImpl::GetIterator() {
76 return g_child_process_list
.Pointer();
80 void BrowserChildProcessHostImpl::AddObserver(
81 BrowserChildProcessObserver
* observer
) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
83 g_observers
.Get().AddObserver(observer
);
87 void BrowserChildProcessHostImpl::RemoveObserver(
88 BrowserChildProcessObserver
* observer
) {
89 // TODO(phajdan.jr): Check thread after fixing http://crbug.com/167126.
90 g_observers
.Get().RemoveObserver(observer
);
93 BrowserChildProcessHostImpl::BrowserChildProcessHostImpl(
95 BrowserChildProcessHostDelegate
* delegate
)
96 : data_(process_type
),
98 power_monitor_message_broadcaster_(this) {
99 data_
.id
= ChildProcessHostImpl::GenerateChildProcessUniqueId();
101 child_process_host_
.reset(ChildProcessHost::Create(this));
102 AddFilter(new TraceMessageFilter
);
103 AddFilter(new ProfilerMessageFilter(process_type
));
104 AddFilter(new HistogramMessageFilter
);
106 g_child_process_list
.Get().push_back(this);
107 GetContentClient()->browser()->BrowserChildProcessHostCreated(this);
109 power_monitor_message_broadcaster_
.Init();
112 BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() {
113 g_child_process_list
.Get().remove(this);
117 void BrowserChildProcessHostImpl::TerminateAll() {
118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
119 // Make a copy since the BrowserChildProcessHost dtor mutates the original
121 BrowserChildProcessList copy
= g_child_process_list
.Get();
122 for (BrowserChildProcessList::iterator it
= copy
.begin();
123 it
!= copy
.end(); ++it
) {
124 delete (*it
)->delegate(); // ~*HostDelegate deletes *HostImpl.
128 void BrowserChildProcessHostImpl::Launch(
129 SandboxedProcessLauncherDelegate
* delegate
,
130 base::CommandLine
* cmd_line
) {
131 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
133 GetContentClient()->browser()->AppendExtraCommandLineSwitches(
136 const base::CommandLine
& browser_command_line
=
137 *base::CommandLine::ForCurrentProcess();
138 static const char* kForwardSwitches
[] = {
139 switches::kDisableLogging
,
140 switches::kEnableLogging
,
141 switches::kIPCConnectionTimeout
,
142 switches::kLoggingLevel
,
143 switches::kTraceToConsole
,
147 cmd_line
->CopySwitchesFrom(browser_command_line
, kForwardSwitches
,
148 arraysize(kForwardSwitches
));
150 child_process_
.reset(new ChildProcessLauncher(
157 const ChildProcessData
& BrowserChildProcessHostImpl::GetData() const {
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
162 ChildProcessHost
* BrowserChildProcessHostImpl::GetHost() const {
163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
164 return child_process_host_
.get();
167 const base::Process
& BrowserChildProcessHostImpl::GetProcess() const {
168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
169 DCHECK(child_process_
.get())
170 << "Requesting a child process handle before launching.";
171 DCHECK(child_process_
->GetProcess().IsValid())
172 << "Requesting a child process handle before launch has completed OK.";
173 return child_process_
->GetProcess();
176 void BrowserChildProcessHostImpl::SetName(const base::string16
& name
) {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
181 void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle
) {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
183 data_
.handle
= handle
;
186 void BrowserChildProcessHostImpl::ForceShutdown() {
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
188 g_child_process_list
.Get().remove(this);
189 child_process_host_
->ForceShutdown();
192 void BrowserChildProcessHostImpl::SetBackgrounded(bool backgrounded
) {
193 child_process_
->SetProcessBackgrounded(backgrounded
);
196 void BrowserChildProcessHostImpl::SetTerminateChildOnShutdown(
197 bool terminate_on_shutdown
) {
198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
199 child_process_
->SetTerminateChildOnShutdown(terminate_on_shutdown
);
202 void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter
* filter
) {
203 child_process_host_
->AddFilter(filter
->GetFilter());
206 void BrowserChildProcessHostImpl::NotifyProcessInstanceCreated(
207 const ChildProcessData
& data
) {
208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
209 FOR_EACH_OBSERVER(BrowserChildProcessObserver
, g_observers
.Get(),
210 BrowserChildProcessInstanceCreated(data
));
213 void BrowserChildProcessHostImpl::HistogramBadMessageTerminated(
215 UMA_HISTOGRAM_ENUMERATION("ChildProcess.BadMessgeTerminated", process_type
,
219 base::TerminationStatus
BrowserChildProcessHostImpl::GetTerminationStatus(
220 bool known_dead
, int* exit_code
) {
221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
222 if (!child_process_
) // If the delegate doesn't use Launch() helper.
223 return base::GetTerminationStatus(data_
.handle
, exit_code
);
224 return child_process_
->GetChildTerminationStatus(known_dead
,
228 bool BrowserChildProcessHostImpl::OnMessageReceived(
229 const IPC::Message
& message
) {
230 return delegate_
->OnMessageReceived(message
);
233 void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid
) {
235 // From this point onward, the exit of the child process is detected by an
236 // error on the IPC channel.
237 early_exit_watcher_
.StopWatching();
240 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
241 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
242 base::Bind(&NotifyProcessHostConnected
, data_
));
244 delegate_
->OnChannelConnected(peer_pid
);
247 void BrowserChildProcessHostImpl::OnChannelError() {
248 delegate_
->OnChannelError();
251 void BrowserChildProcessHostImpl::OnBadMessageReceived(
252 const IPC::Message
& message
) {
253 HistogramBadMessageTerminated(data_
.process_type
);
254 if (CommandLine::ForCurrentProcess()->HasSwitch(
255 switches::kDisableKillAfterBadIPC
)) {
258 base::KillProcess(child_process_
->GetProcess().Handle(),
259 RESULT_CODE_KILLED_BAD_MESSAGE
, false);
262 bool BrowserChildProcessHostImpl::CanShutdown() {
263 return delegate_
->CanShutdown();
266 void BrowserChildProcessHostImpl::OnChildDisconnected() {
267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
269 // OnChildDisconnected may be called without OnChannelConnected, so stop the
270 // early exit watcher so GetTerminationStatus can close the process handle.
271 early_exit_watcher_
.StopWatching();
273 if (child_process_
.get() || data_
.handle
) {
275 base::TerminationStatus status
= GetTerminationStatus(
276 true /* known_dead */, &exit_code
);
278 case base::TERMINATION_STATUS_PROCESS_CRASHED
:
279 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION
: {
280 delegate_
->OnProcessCrashed(exit_code
);
281 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
282 base::Bind(&NotifyProcessCrashed
, data_
));
283 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2",
288 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED
: {
289 delegate_
->OnProcessCrashed(exit_code
);
290 // Report that this child process was killed.
291 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2",
296 case base::TERMINATION_STATUS_STILL_RUNNING
: {
297 UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2",
304 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2",
308 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
309 base::Bind(&NotifyProcessHostDisconnected
, data_
));
310 delete delegate_
; // Will delete us
313 bool BrowserChildProcessHostImpl::Send(IPC::Message
* message
) {
314 return child_process_host_
->Send(message
);
317 void BrowserChildProcessHostImpl::OnProcessLaunchFailed() {
318 delegate_
->OnProcessLaunchFailed();
319 delete delegate_
; // Will delete us
322 void BrowserChildProcessHostImpl::OnProcessLaunched() {
323 const base::Process
& process
= child_process_
->GetProcess();
324 DCHECK(process
.IsValid());
327 // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the
328 // child process exits. This watcher is stopped once the IPC channel is
329 // connected and the exit of the child process is detecter by an error on the
330 // IPC channel thereafter.
331 DCHECK(!early_exit_watcher_
.GetWatchedObject());
332 early_exit_watcher_
.StartWatching(process
.Handle(), this);
335 // TODO(rvargas) crbug.com/417532: Don't store a handle.
336 data_
.handle
= process
.Handle();
337 delegate_
->OnProcessLaunched();
342 void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object
) {
343 OnChildDisconnected();
348 } // namespace content