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 child_process_host_
->AddFilter(new TraceMessageFilter
);
104 child_process_host_
->AddFilter(new ProfilerMessageFilter(process_type
));
105 child_process_host_
->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::SetName(const string16
& name
) {
196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
200 void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle
) {
201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
202 data_
.handle
= handle
;
205 void BrowserChildProcessHostImpl::ForceShutdown() {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
207 g_child_process_list
.Get().remove(this);
208 child_process_host_
->ForceShutdown();
211 void BrowserChildProcessHostImpl::SetBackgrounded(bool backgrounded
) {
212 child_process_
->SetProcessBackgrounded(backgrounded
);
215 void BrowserChildProcessHostImpl::SetTerminateChildOnShutdown(
216 bool terminate_on_shutdown
) {
217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
218 child_process_
->SetTerminateChildOnShutdown(terminate_on_shutdown
);
221 void BrowserChildProcessHostImpl::NotifyProcessInstanceCreated(
222 const ChildProcessData
& data
) {
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
224 FOR_EACH_OBSERVER(BrowserChildProcessObserver
, g_observers
.Get(),
225 BrowserChildProcessInstanceCreated(data
));
228 base::TerminationStatus
BrowserChildProcessHostImpl::GetTerminationStatus(
229 bool known_dead
, int* exit_code
) {
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
231 if (!child_process_
) // If the delegate doesn't use Launch() helper.
232 return base::GetTerminationStatus(data_
.handle
, exit_code
);
233 return child_process_
->GetChildTerminationStatus(known_dead
,
237 bool BrowserChildProcessHostImpl::OnMessageReceived(
238 const IPC::Message
& message
) {
239 return delegate_
->OnMessageReceived(message
);
242 void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid
) {
244 // From this point onward, the exit of the child process is detected by an
245 // error on the IPC channel.
246 DeleteProcessWaitableEvent(early_exit_watcher_
.GetWatchedEvent());
247 early_exit_watcher_
.StopWatching();
250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
251 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
252 base::Bind(&NotifyProcessHostConnected
, data_
));
254 delegate_
->OnChannelConnected(peer_pid
);
257 void BrowserChildProcessHostImpl::OnChannelError() {
258 delegate_
->OnChannelError();
261 bool BrowserChildProcessHostImpl::CanShutdown() {
262 return delegate_
->CanShutdown();
265 void BrowserChildProcessHostImpl::OnChildDisconnected() {
266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
267 if (child_process_
.get() || data_
.handle
) {
268 DCHECK(data_
.handle
!= base::kNullProcessHandle
);
270 base::TerminationStatus status
= GetTerminationStatus(
271 true /* known_dead */, &exit_code
);
273 case base::TERMINATION_STATUS_PROCESS_CRASHED
:
274 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION
: {
275 delegate_
->OnProcessCrashed(exit_code
);
276 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
277 base::Bind(&NotifyProcessCrashed
, data_
));
278 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2",
283 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED
: {
284 delegate_
->OnProcessCrashed(exit_code
);
285 // Report that this child process was killed.
286 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2",
291 case base::TERMINATION_STATUS_STILL_RUNNING
: {
292 UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2",
299 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2",
303 BrowserThread::PostTask(BrowserThread::UI
, FROM_HERE
,
304 base::Bind(&NotifyProcessHostDisconnected
, data_
));
305 delete delegate_
; // Will delete us
308 bool BrowserChildProcessHostImpl::Send(IPC::Message
* message
) {
309 return child_process_host_
->Send(message
);
312 void BrowserChildProcessHostImpl::OnProcessLaunched() {
313 base::ProcessHandle handle
= child_process_
->GetHandle();
315 delete delegate_
; // Will delete us
320 // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the
321 // child process exits. This watcher is stopped once the IPC channel is
322 // connected and the exit of the child process is detecter by an error on the
323 // IPC channel thereafter.
324 DCHECK(!early_exit_watcher_
.GetWatchedEvent());
325 early_exit_watcher_
.StartWatching(
326 new base::WaitableEvent(handle
),
327 base::Bind(&BrowserChildProcessHostImpl::OnProcessExitedEarly
,
328 base::Unretained(this)));
331 data_
.handle
= handle
;
332 delegate_
->OnProcessLaunched();
337 void BrowserChildProcessHostImpl::DeleteProcessWaitableEvent(
338 base::WaitableEvent
* event
) {
342 // The WaitableEvent does not own the process handle so ensure it does not
349 void BrowserChildProcessHostImpl::OnProcessExitedEarly(
350 base::WaitableEvent
* event
) {
351 DeleteProcessWaitableEvent(event
);
352 OnChildDisconnected();
357 } // namespace content