Refactor FacetManager into a separate file.
[chromium-blink-merge.git] / content / browser / browser_child_process_host_impl.cc
blobe606902637c531f0d45b60f0ebb771bb58b44bc0
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/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"
33 #endif
35 namespace content {
36 namespace {
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));
59 } // namespace
61 BrowserChildProcessHost* BrowserChildProcessHost::Create(
62 int process_type,
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();
71 #endif
73 // static
74 BrowserChildProcessHostImpl::BrowserChildProcessList*
75 BrowserChildProcessHostImpl::GetIterator() {
76 return g_child_process_list.Pointer();
79 // static
80 void BrowserChildProcessHostImpl::AddObserver(
81 BrowserChildProcessObserver* observer) {
82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
83 g_observers.Get().AddObserver(observer);
86 // static
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(
94 int process_type,
95 BrowserChildProcessHostDelegate* delegate)
96 : data_(process_type),
97 delegate_(delegate),
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);
116 // static
117 void BrowserChildProcessHostImpl::TerminateAll() {
118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
119 // Make a copy since the BrowserChildProcessHost dtor mutates the original
120 // list.
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(
134 cmd_line, data_.id);
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,
144 switches::kV,
145 switches::kVModule,
147 cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches,
148 arraysize(kForwardSwitches));
150 child_process_.reset(new ChildProcessLauncher(
151 delegate,
152 cmd_line,
153 data_.id,
154 this));
157 const ChildProcessData& BrowserChildProcessHostImpl::GetData() const {
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
159 return data_;
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));
178 data_.name = name;
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(
214 int process_type) {
215 UMA_HISTOGRAM_ENUMERATION("ChildProcess.BadMessgeTerminated", process_type,
216 PROCESS_TYPE_MAX);
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,
225 exit_code);
228 bool BrowserChildProcessHostImpl::OnMessageReceived(
229 const IPC::Message& message) {
230 return delegate_->OnMessageReceived(message);
233 void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid) {
234 #if defined(OS_WIN)
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();
238 #endif
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 (base::CommandLine::ForCurrentProcess()->HasSwitch(
255 switches::kDisableKillAfterBadIPC)) {
256 return;
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));
268 #if defined(OS_WIN)
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();
272 #endif
273 if (child_process_.get() || data_.handle) {
274 int exit_code;
275 base::TerminationStatus status = GetTerminationStatus(
276 true /* known_dead */, &exit_code);
277 switch (status) {
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",
284 data_.process_type,
285 PROCESS_TYPE_MAX);
286 break;
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",
292 data_.process_type,
293 PROCESS_TYPE_MAX);
294 break;
296 case base::TERMINATION_STATUS_STILL_RUNNING: {
297 UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2",
298 data_.process_type,
299 PROCESS_TYPE_MAX);
301 default:
302 break;
304 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2",
305 data_.process_type,
306 PROCESS_TYPE_MAX);
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());
326 #if defined(OS_WIN)
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);
333 #endif
335 // TODO(rvargas) crbug.com/417532: Don't store a handle.
336 data_.handle = process.Handle();
337 delegate_->OnProcessLaunched();
340 #if defined(OS_WIN)
342 void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) {
343 OnChildDisconnected();
346 #endif
348 } // namespace content