Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / browser / browser_child_process_host_impl.cc
blobe1365f994bdd87735631bcc43bfca26123122c20
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/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"
34 #endif
36 namespace content {
37 namespace {
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));
60 } // namespace
62 BrowserChildProcessHost* BrowserChildProcessHost::Create(
63 int process_type,
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();
72 #endif
74 // static
75 BrowserChildProcessHostImpl::BrowserChildProcessList*
76 BrowserChildProcessHostImpl::GetIterator() {
77 return g_child_process_list.Pointer();
80 // static
81 void BrowserChildProcessHostImpl::AddObserver(
82 BrowserChildProcessObserver* observer) {
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
84 g_observers.Get().AddObserver(observer);
87 // static
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(
95 int process_type,
96 BrowserChildProcessHostDelegate* delegate)
97 : data_(process_type),
98 delegate_(delegate),
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);
114 #if defined(OS_WIN)
115 DeleteProcessWaitableEvent(early_exit_watcher_.GetWatchedEvent());
116 #endif
119 // static
120 void BrowserChildProcessHostImpl::TerminateAll() {
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
122 // Make a copy since the BrowserChildProcessHost dtor mutates the original
123 // list.
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(
132 SandboxedProcessLauncherDelegate* delegate,
133 CommandLine* cmd_line) {
134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
136 GetContentClient()->browser()->AppendExtraCommandLineSwitches(
137 cmd_line, data_.id);
139 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
140 static const char* kForwardSwitches[] = {
141 switches::kDisableLogging,
142 switches::kEnableLogging,
143 switches::kLoggingLevel,
144 switches::kTraceToConsole,
145 switches::kV,
146 switches::kVModule,
147 #if defined(OS_WIN)
148 switches::kEnableHighResolutionTime,
149 #endif
151 cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches,
152 arraysize(kForwardSwitches));
154 child_process_.reset(new ChildProcessLauncher(
155 delegate,
156 cmd_line,
157 data_.id,
158 this));
161 const ChildProcessData& BrowserChildProcessHostImpl::GetData() const {
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
163 return data_;
166 ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const {
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
168 return child_process_host_.get();
171 base::ProcessHandle BrowserChildProcessHostImpl::GetHandle() const {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
173 DCHECK(child_process_.get())
174 << "Requesting a child process handle before launching.";
175 DCHECK(child_process_->GetHandle())
176 << "Requesting a child process handle before launch has completed OK.";
177 return child_process_->GetHandle();
180 void BrowserChildProcessHostImpl::SetNaClDebugStubPort(int port) {
181 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
182 data_.nacl_debug_stub_port = port;
185 void BrowserChildProcessHostImpl::SetName(const base::string16& name) {
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
187 data_.name = name;
190 void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle) {
191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
192 data_.handle = handle;
195 void BrowserChildProcessHostImpl::ForceShutdown() {
196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
197 g_child_process_list.Get().remove(this);
198 child_process_host_->ForceShutdown();
201 void BrowserChildProcessHostImpl::SetBackgrounded(bool backgrounded) {
202 child_process_->SetProcessBackgrounded(backgrounded);
205 void BrowserChildProcessHostImpl::SetTerminateChildOnShutdown(
206 bool terminate_on_shutdown) {
207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
208 child_process_->SetTerminateChildOnShutdown(terminate_on_shutdown);
211 void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter* filter) {
212 child_process_host_->AddFilter(filter->GetFilter());
215 void BrowserChildProcessHostImpl::NotifyProcessInstanceCreated(
216 const ChildProcessData& data) {
217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
218 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
219 BrowserChildProcessInstanceCreated(data));
222 base::TerminationStatus BrowserChildProcessHostImpl::GetTerminationStatus(
223 bool known_dead, int* exit_code) {
224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
225 if (!child_process_) // If the delegate doesn't use Launch() helper.
226 return base::GetTerminationStatus(data_.handle, exit_code);
227 return child_process_->GetChildTerminationStatus(known_dead,
228 exit_code);
231 bool BrowserChildProcessHostImpl::OnMessageReceived(
232 const IPC::Message& message) {
233 return delegate_->OnMessageReceived(message);
236 void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid) {
237 #if defined(OS_WIN)
238 // From this point onward, the exit of the child process is detected by an
239 // error on the IPC channel.
240 DeleteProcessWaitableEvent(early_exit_watcher_.GetWatchedEvent());
241 early_exit_watcher_.StopWatching();
242 #endif
244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
245 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
246 base::Bind(&NotifyProcessHostConnected, data_));
248 delegate_->OnChannelConnected(peer_pid);
251 void BrowserChildProcessHostImpl::OnChannelError() {
252 delegate_->OnChannelError();
255 bool BrowserChildProcessHostImpl::CanShutdown() {
256 return delegate_->CanShutdown();
259 void BrowserChildProcessHostImpl::OnChildDisconnected() {
260 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
261 if (child_process_.get() || data_.handle) {
262 DCHECK(data_.handle != base::kNullProcessHandle);
263 int exit_code;
264 base::TerminationStatus status = GetTerminationStatus(
265 true /* known_dead */, &exit_code);
266 switch (status) {
267 case base::TERMINATION_STATUS_PROCESS_CRASHED:
268 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: {
269 delegate_->OnProcessCrashed(exit_code);
270 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
271 base::Bind(&NotifyProcessCrashed, data_));
272 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2",
273 data_.process_type,
274 PROCESS_TYPE_MAX);
275 break;
277 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: {
278 delegate_->OnProcessCrashed(exit_code);
279 // Report that this child process was killed.
280 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2",
281 data_.process_type,
282 PROCESS_TYPE_MAX);
283 break;
285 case base::TERMINATION_STATUS_STILL_RUNNING: {
286 UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2",
287 data_.process_type,
288 PROCESS_TYPE_MAX);
290 default:
291 break;
293 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2",
294 data_.process_type,
295 PROCESS_TYPE_MAX);
297 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
298 base::Bind(&NotifyProcessHostDisconnected, data_));
299 delete delegate_; // Will delete us
302 bool BrowserChildProcessHostImpl::Send(IPC::Message* message) {
303 return child_process_host_->Send(message);
306 void BrowserChildProcessHostImpl::OnProcessLaunchFailed() {
307 delegate_->OnProcessLaunchFailed();
308 delete delegate_; // Will delete us
311 void BrowserChildProcessHostImpl::OnProcessLaunched() {
312 base::ProcessHandle handle = child_process_->GetHandle();
313 if (!handle) {
314 delete delegate_; // Will delete us
315 return;
318 #if defined(OS_WIN)
319 // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the
320 // child process exits. This watcher is stopped once the IPC channel is
321 // connected and the exit of the child process is detecter by an error on the
322 // IPC channel thereafter.
323 DCHECK(!early_exit_watcher_.GetWatchedEvent());
324 early_exit_watcher_.StartWatching(
325 new base::WaitableEvent(handle),
326 base::Bind(&BrowserChildProcessHostImpl::OnProcessExitedEarly,
327 base::Unretained(this)));
328 #endif
330 data_.handle = handle;
331 delegate_->OnProcessLaunched();
334 #if defined(OS_WIN)
336 void BrowserChildProcessHostImpl::DeleteProcessWaitableEvent(
337 base::WaitableEvent* event) {
338 if (!event)
339 return;
341 // The WaitableEvent does not own the process handle so ensure it does not
342 // close it.
343 event->Release();
345 delete event;
348 void BrowserChildProcessHostImpl::OnProcessExitedEarly(
349 base::WaitableEvent* event) {
350 DeleteProcessWaitableEvent(event);
351 OnChildDisconnected();
354 #endif
356 } // namespace content