Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / browser_child_process_host_impl.cc
blob684e0dbc6460556bbf79f4998266ebf6eaf4ac3e
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/debug/dump_without_crashing.h"
11 #include "base/files/file_path.h"
12 #include "base/lazy_instance.h"
13 #include "base/logging.h"
14 #include "base/metrics/histogram.h"
15 #include "base/profiler/scoped_tracker.h"
16 #include "base/stl_util.h"
17 #include "base/strings/string_util.h"
18 #include "base/synchronization/waitable_event.h"
19 #include "content/browser/histogram_message_filter.h"
20 #include "content/browser/loader/resource_message_filter.h"
21 #include "content/browser/profiler_message_filter.h"
22 #include "content/browser/tracing/trace_message_filter.h"
23 #include "content/common/child_process_host_impl.h"
24 #include "content/public/browser/browser_child_process_host_delegate.h"
25 #include "content/public/browser/browser_child_process_observer.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/child_process_data.h"
28 #include "content/public/browser/content_browser_client.h"
29 #include "content/public/common/content_switches.h"
30 #include "content/public/common/process_type.h"
31 #include "content/public/common/result_codes.h"
33 #if defined(OS_MACOSX)
34 #include "content/browser/mach_broker_mac.h"
35 #endif
37 namespace content {
38 namespace {
40 static base::LazyInstance<BrowserChildProcessHostImpl::BrowserChildProcessList>
41 g_child_process_list = LAZY_INSTANCE_INITIALIZER;
43 base::LazyInstance<ObserverList<BrowserChildProcessObserver> >
44 g_observers = LAZY_INSTANCE_INITIALIZER;
46 void NotifyProcessHostConnected(const ChildProcessData& data) {
47 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
48 BrowserChildProcessHostConnected(data));
51 void NotifyProcessHostDisconnected(const ChildProcessData& data) {
52 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
53 BrowserChildProcessHostDisconnected(data));
56 void NotifyProcessCrashed(const ChildProcessData& data) {
57 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
58 BrowserChildProcessCrashed(data));
61 } // namespace
63 BrowserChildProcessHost* BrowserChildProcessHost::Create(
64 content::ProcessType process_type,
65 BrowserChildProcessHostDelegate* delegate) {
66 return new BrowserChildProcessHostImpl(process_type, delegate);
69 #if defined(OS_MACOSX)
70 base::ProcessMetrics::PortProvider* BrowserChildProcessHost::GetPortProvider() {
71 return MachBroker::GetInstance();
73 #endif
75 // static
76 BrowserChildProcessHostImpl::BrowserChildProcessList*
77 BrowserChildProcessHostImpl::GetIterator() {
78 return g_child_process_list.Pointer();
81 // static
82 void BrowserChildProcessHostImpl::AddObserver(
83 BrowserChildProcessObserver* observer) {
84 DCHECK_CURRENTLY_ON(BrowserThread::UI);
85 g_observers.Get().AddObserver(observer);
88 // static
89 void BrowserChildProcessHostImpl::RemoveObserver(
90 BrowserChildProcessObserver* observer) {
91 // TODO(phajdan.jr): Check thread after fixing http://crbug.com/167126.
92 g_observers.Get().RemoveObserver(observer);
95 BrowserChildProcessHostImpl::BrowserChildProcessHostImpl(
96 content::ProcessType process_type,
97 BrowserChildProcessHostDelegate* delegate)
98 : data_(process_type),
99 delegate_(delegate),
100 power_monitor_message_broadcaster_(this) {
101 data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId();
103 child_process_host_.reset(ChildProcessHost::Create(this));
104 AddFilter(new TraceMessageFilter);
105 AddFilter(new ProfilerMessageFilter(process_type));
106 AddFilter(new HistogramMessageFilter);
108 g_child_process_list.Get().push_back(this);
109 GetContentClient()->browser()->BrowserChildProcessHostCreated(this);
111 power_monitor_message_broadcaster_.Init();
114 BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() {
115 g_child_process_list.Get().remove(this);
118 // static
119 void BrowserChildProcessHostImpl::TerminateAll() {
120 DCHECK_CURRENTLY_ON(BrowserThread::IO);
121 // Make a copy since the BrowserChildProcessHost dtor mutates the original
122 // list.
123 BrowserChildProcessList copy = g_child_process_list.Get();
124 for (BrowserChildProcessList::iterator it = copy.begin();
125 it != copy.end(); ++it) {
126 delete (*it)->delegate(); // ~*HostDelegate deletes *HostImpl.
130 void BrowserChildProcessHostImpl::Launch(
131 SandboxedProcessLauncherDelegate* delegate,
132 base::CommandLine* cmd_line,
133 bool terminate_on_shutdown) {
134 DCHECK_CURRENTLY_ON(BrowserThread::IO);
136 GetContentClient()->browser()->AppendExtraCommandLineSwitches(
137 cmd_line, data_.id);
139 const base::CommandLine& browser_command_line =
140 *base::CommandLine::ForCurrentProcess();
141 static const char* kForwardSwitches[] = {
142 switches::kDisableLogging,
143 switches::kEnableLogging,
144 switches::kIPCConnectionTimeout,
145 switches::kLoggingLevel,
146 switches::kTraceToConsole,
147 switches::kV,
148 switches::kVModule,
150 cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches,
151 arraysize(kForwardSwitches));
153 child_process_.reset(new ChildProcessLauncher(
154 delegate,
155 cmd_line,
156 data_.id,
157 this,
158 terminate_on_shutdown));
161 const ChildProcessData& BrowserChildProcessHostImpl::GetData() const {
162 DCHECK_CURRENTLY_ON(BrowserThread::IO);
163 return data_;
166 ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const {
167 DCHECK_CURRENTLY_ON(BrowserThread::IO);
168 return child_process_host_.get();
171 const base::Process& BrowserChildProcessHostImpl::GetProcess() const {
172 DCHECK_CURRENTLY_ON(BrowserThread::IO);
173 DCHECK(child_process_.get())
174 << "Requesting a child process handle before launching.";
175 DCHECK(child_process_->GetProcess().IsValid())
176 << "Requesting a child process handle before launch has completed OK.";
177 return child_process_->GetProcess();
180 void BrowserChildProcessHostImpl::SetName(const base::string16& name) {
181 DCHECK_CURRENTLY_ON(BrowserThread::IO);
182 data_.name = name;
185 void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle) {
186 DCHECK_CURRENTLY_ON(BrowserThread::IO);
187 data_.handle = handle;
190 void BrowserChildProcessHostImpl::ForceShutdown() {
191 DCHECK_CURRENTLY_ON(BrowserThread::IO);
192 g_child_process_list.Get().remove(this);
193 child_process_host_->ForceShutdown();
196 void BrowserChildProcessHostImpl::SetBackgrounded(bool backgrounded) {
197 child_process_->SetProcessBackgrounded(backgrounded);
200 void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter* filter) {
201 child_process_host_->AddFilter(filter->GetFilter());
204 void BrowserChildProcessHostImpl::NotifyProcessInstanceCreated(
205 const ChildProcessData& data) {
206 DCHECK_CURRENTLY_ON(BrowserThread::UI);
207 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
208 BrowserChildProcessInstanceCreated(data));
211 void BrowserChildProcessHostImpl::HistogramBadMessageTerminated(
212 int process_type) {
213 UMA_HISTOGRAM_ENUMERATION("ChildProcess.BadMessgeTerminated", process_type,
214 PROCESS_TYPE_MAX);
217 base::TerminationStatus BrowserChildProcessHostImpl::GetTerminationStatus(
218 bool known_dead, int* exit_code) {
219 DCHECK_CURRENTLY_ON(BrowserThread::IO);
220 if (!child_process_) // If the delegate doesn't use Launch() helper.
221 return base::GetTerminationStatus(data_.handle, exit_code);
222 return child_process_->GetChildTerminationStatus(known_dead,
223 exit_code);
226 bool BrowserChildProcessHostImpl::OnMessageReceived(
227 const IPC::Message& message) {
228 return delegate_->OnMessageReceived(message);
231 void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid) {
232 #if defined(OS_WIN)
233 // From this point onward, the exit of the child process is detected by an
234 // error on the IPC channel.
235 early_exit_watcher_.StopWatching();
236 #endif
238 DCHECK_CURRENTLY_ON(BrowserThread::IO);
239 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
240 base::Bind(&NotifyProcessHostConnected, data_));
242 delegate_->OnChannelConnected(peer_pid);
245 void BrowserChildProcessHostImpl::OnChannelError() {
246 delegate_->OnChannelError();
249 void BrowserChildProcessHostImpl::OnBadMessageReceived(
250 const IPC::Message& message) {
251 HistogramBadMessageTerminated(data_.process_type);
252 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
253 switches::kDisableKillAfterBadIPC)) {
254 return;
256 LOG(ERROR) << "Terminating child process for bad IPC message of type "
257 << message.type();
259 // Create a memory dump. This will contain enough stack frames to work out
260 // what the bad message was.
261 base::debug::DumpWithoutCrashing();
263 child_process_->GetProcess().Terminate(RESULT_CODE_KILLED_BAD_MESSAGE, false);
266 bool BrowserChildProcessHostImpl::CanShutdown() {
267 return delegate_->CanShutdown();
270 void BrowserChildProcessHostImpl::OnChildDisconnected() {
271 DCHECK_CURRENTLY_ON(BrowserThread::IO);
272 #if defined(OS_WIN)
273 // OnChildDisconnected may be called without OnChannelConnected, so stop the
274 // early exit watcher so GetTerminationStatus can close the process handle.
275 early_exit_watcher_.StopWatching();
276 #endif
277 if (child_process_.get() || data_.handle) {
278 int exit_code;
279 base::TerminationStatus status = GetTerminationStatus(
280 true /* known_dead */, &exit_code);
281 switch (status) {
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",
288 data_.process_type,
289 PROCESS_TYPE_MAX);
290 break;
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",
296 data_.process_type,
297 PROCESS_TYPE_MAX);
298 break;
300 case base::TERMINATION_STATUS_STILL_RUNNING: {
301 UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2",
302 data_.process_type,
303 PROCESS_TYPE_MAX);
305 default:
306 break;
308 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2",
309 data_.process_type,
310 PROCESS_TYPE_MAX);
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::OnProcessLaunchFailed() {
322 delegate_->OnProcessLaunchFailed();
323 delete delegate_; // Will delete us
326 void BrowserChildProcessHostImpl::OnProcessLaunched() {
327 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/465841
328 // is fixed.
329 tracked_objects::ScopedTracker tracking_profile(
330 FROM_HERE_WITH_EXPLICIT_FUNCTION(
331 "465841 BrowserChildProcessHostImpl::OnProcessLaunched"));
332 const base::Process& process = child_process_->GetProcess();
333 DCHECK(process.IsValid());
335 #if defined(OS_WIN)
336 // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the
337 // child process exits. This watcher is stopped once the IPC channel is
338 // connected and the exit of the child process is detecter by an error on the
339 // IPC channel thereafter.
340 DCHECK(!early_exit_watcher_.GetWatchedObject());
341 early_exit_watcher_.StartWatching(process.Handle(), this);
342 #endif
344 // TODO(rvargas) crbug.com/417532: Don't store a handle.
345 data_.handle = process.Handle();
346 delegate_->OnProcessLaunched();
349 #if defined(OS_WIN)
351 void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) {
352 OnChildDisconnected();
355 #endif
357 } // namespace content