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/path_service.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"
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
));
62 BrowserChildProcessHost
* BrowserChildProcessHost::Create(
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();
75 BrowserChildProcessHostImpl::BrowserChildProcessList
*
76 BrowserChildProcessHostImpl::GetIterator() {
77 return g_child_process_list
.Pointer();
81 void BrowserChildProcessHostImpl::AddObserver(
82 BrowserChildProcessObserver
* observer
) {
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
84 g_observers
.Get().AddObserver(observer
);
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(
96 BrowserChildProcessHostDelegate
* delegate
)
97 : data_(process_type
),
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);
111 BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() {
112 g_child_process_list
.Get().remove(this);
115 DeleteProcessWaitableEvent(early_exit_watcher_
.GetWatchedEvent());
120 void BrowserChildProcessHostImpl::TerminateAll() {
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
122 // Make a copy since the BrowserChildProcessHost dtor mutates the original
124 BrowserChildProcessList copy
= g_child_process_list
.Get();
125 for (BrowserChildProcessList::iterator it
= copy
.begin();
126 it
!= copy
.end(); ++it
) {
127 delete (*it
)->delegate(); // ~*HostDelegate deletes *HostImpl.
131 void BrowserChildProcessHostImpl::Launch(
133 SandboxedProcessLauncherDelegate
* delegate
,
134 #elif defined(OS_POSIX)
136 const base::EnvironmentMap
& environ
,
138 CommandLine
* cmd_line
) {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
141 GetContentClient()->browser()->AppendExtraCommandLineSwitches(
144 const CommandLine
& browser_command_line
= *CommandLine::ForCurrentProcess();
145 static const char* kForwardSwitches
[] = {
146 switches::kDisableLogging
,
147 switches::kEnableDCHECK
,
148 switches::kEnableLogging
,
149 switches::kLoggingLevel
,
150 switches::kTraceToConsole
,
153 #if defined(OS_POSIX)
154 switches::kChildCleanExit
,
157 switches::kEnableHighResolutionTime
,
160 cmd_line
->CopySwitchesFrom(browser_command_line
, kForwardSwitches
,
161 arraysize(kForwardSwitches
));
163 child_process_
.reset(new ChildProcessLauncher(
166 #elif defined(OS_POSIX)
169 child_process_host_
->TakeClientFileDescriptor(),
176 const ChildProcessData
& BrowserChildProcessHostImpl::GetData() const {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
181 ChildProcessHost
* BrowserChildProcessHostImpl::GetHost() const {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
183 return child_process_host_
.get();
186 base::ProcessHandle
BrowserChildProcessHostImpl::GetHandle() const {
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
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();
195 void BrowserChildProcessHostImpl::SetNaClDebugStubPort(int port
) {
196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
197 data_
.nacl_debug_stub_port
= port
;
200 void BrowserChildProcessHostImpl::SetName(const base::string16
& name
) {
201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
205 void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle
) {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
207 data_
.handle
= handle
;
210 void BrowserChildProcessHostImpl::ForceShutdown() {
211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
212 g_child_process_list
.Get().remove(this);
213 child_process_host_
->ForceShutdown();
216 void BrowserChildProcessHostImpl::SetBackgrounded(bool backgrounded
) {
217 child_process_
->SetProcessBackgrounded(backgrounded
);
220 void BrowserChildProcessHostImpl::SetTerminateChildOnShutdown(
221 bool terminate_on_shutdown
) {
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
223 child_process_
->SetTerminateChildOnShutdown(terminate_on_shutdown
);
226 void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter
* filter
) {
227 child_process_host_
->AddFilter(filter
->GetFilter());
230 void BrowserChildProcessHostImpl::NotifyProcessInstanceCreated(
231 const ChildProcessData
& data
) {
232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
233 FOR_EACH_OBSERVER(BrowserChildProcessObserver
, g_observers
.Get(),
234 BrowserChildProcessInstanceCreated(data
));
237 base::TerminationStatus
BrowserChildProcessHostImpl::GetTerminationStatus(
238 bool known_dead
, int* exit_code
) {
239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
240 if (!child_process_
) // If the delegate doesn't use Launch() helper.
241 return base::GetTerminationStatus(data_
.handle
, exit_code
);
242 return child_process_
->GetChildTerminationStatus(known_dead
,
246 bool BrowserChildProcessHostImpl::OnMessageReceived(
247 const IPC::Message
& message
) {
248 return delegate_
->OnMessageReceived(message
);
251 void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid
) {
253 // From this point onward, the exit of the child process is detected by an
254 // error on the IPC channel.
255 DeleteProcessWaitableEvent(early_exit_watcher_
.GetWatchedEvent());
256 early_exit_watcher_
.StopWatching();
259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
260 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
261 base::Bind(&NotifyProcessHostConnected
, data_
));
263 delegate_
->OnChannelConnected(peer_pid
);
266 void BrowserChildProcessHostImpl::OnChannelError() {
267 delegate_
->OnChannelError();
270 bool BrowserChildProcessHostImpl::CanShutdown() {
271 return delegate_
->CanShutdown();
274 void BrowserChildProcessHostImpl::OnChildDisconnected() {
275 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
276 if (child_process_
.get() || data_
.handle
) {
277 DCHECK(data_
.handle
!= base::kNullProcessHandle
);
279 base::TerminationStatus status
= GetTerminationStatus(
280 true /* known_dead */, &exit_code
);
282 case base::TERMINATION_STATUS_PROCESS_CRASHED
:
283 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION
: {
284 delegate_
->OnProcessCrashed(exit_code
);
285 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
286 base::Bind(&NotifyProcessCrashed
, data_
));
287 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2",
292 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED
: {
293 delegate_
->OnProcessCrashed(exit_code
);
294 // Report that this child process was killed.
295 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2",
300 case base::TERMINATION_STATUS_STILL_RUNNING
: {
301 UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2",
308 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2",
312 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
313 base::Bind(&NotifyProcessHostDisconnected
, data_
));
314 delete delegate_
; // Will delete us
317 bool BrowserChildProcessHostImpl::Send(IPC::Message
* message
) {
318 return child_process_host_
->Send(message
);
321 void BrowserChildProcessHostImpl::OnProcessLaunched() {
322 base::ProcessHandle handle
= child_process_
->GetHandle();
324 delete delegate_
; // Will delete us
329 // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the
330 // child process exits. This watcher is stopped once the IPC channel is
331 // connected and the exit of the child process is detecter by an error on the
332 // IPC channel thereafter.
333 DCHECK(!early_exit_watcher_
.GetWatchedEvent());
334 early_exit_watcher_
.StartWatching(
335 new base::WaitableEvent(handle
),
336 base::Bind(&BrowserChildProcessHostImpl::OnProcessExitedEarly
,
337 base::Unretained(this)));
340 data_
.handle
= handle
;
341 delegate_
->OnProcessLaunched();
346 void BrowserChildProcessHostImpl::DeleteProcessWaitableEvent(
347 base::WaitableEvent
* event
) {
351 // The WaitableEvent does not own the process handle so ensure it does not
358 void BrowserChildProcessHostImpl::OnProcessExitedEarly(
359 base::WaitableEvent
* event
) {
360 DeleteProcessWaitableEvent(event
);
361 OnChildDisconnected();
366 } // namespace content