[android_webview] Disable AwSettingsTest broken by Blink roll.
[chromium-blink-merge.git] / content / browser / browser_child_process_host_impl.cc
bloba4f339c68c93753c81634901f317998e70901db3
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/process_util.h"
16 #include "base/stl_util.h"
17 #include "base/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 int 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(BrowserThread::CurrentlyOn(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 int process_type,
97 BrowserChildProcessHostDelegate* delegate)
98 : data_(process_type),
99 delegate_(delegate) {
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);
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 #if defined(OS_WIN)
133 SandboxedProcessLauncherDelegate* delegate,
134 #elif defined(OS_POSIX)
135 bool use_zygote,
136 const base::EnvironmentVector& environ,
137 #endif
138 CommandLine* cmd_line) {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
141 GetContentClient()->browser()->AppendExtraCommandLineSwitches(
142 cmd_line, data_.id);
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::kV,
151 switches::kVModule,
152 #if defined(OS_POSIX)
153 switches::kChildCleanExit,
154 #endif
156 cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches,
157 arraysize(kForwardSwitches));
159 child_process_.reset(new ChildProcessLauncher(
160 #if defined(OS_WIN)
161 delegate,
162 #elif defined(OS_POSIX)
163 use_zygote,
164 environ,
165 child_process_host_->TakeClientFileDescriptor(),
166 #endif
167 cmd_line,
168 data_.id,
169 this));
172 const ChildProcessData& BrowserChildProcessHostImpl::GetData() const {
173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
174 return data_;
177 ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const {
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
179 return child_process_host_.get();
182 base::ProcessHandle BrowserChildProcessHostImpl::GetHandle() const {
183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
184 DCHECK(child_process_.get())
185 << "Requesting a child process handle before launching.";
186 DCHECK(child_process_->GetHandle())
187 << "Requesting a child process handle before launch has completed OK.";
188 return child_process_->GetHandle();
191 void BrowserChildProcessHostImpl::SetName(const string16& name) {
192 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
193 data_.name = name;
196 void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle) {
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
198 data_.handle = handle;
201 void BrowserChildProcessHostImpl::ForceShutdown() {
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
203 g_child_process_list.Get().remove(this);
204 child_process_host_->ForceShutdown();
207 void BrowserChildProcessHostImpl::SetTerminateChildOnShutdown(
208 bool terminate_on_shutdown) {
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
210 child_process_->SetTerminateChildOnShutdown(terminate_on_shutdown);
213 void BrowserChildProcessHostImpl::NotifyProcessInstanceCreated(
214 const ChildProcessData& data) {
215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
216 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
217 BrowserChildProcessInstanceCreated(data));
220 base::TerminationStatus BrowserChildProcessHostImpl::GetTerminationStatus(
221 int* exit_code) {
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
223 if (!child_process_) // If the delegate doesn't use Launch() helper.
224 return base::GetTerminationStatus(data_.handle, exit_code);
225 return child_process_->GetChildTerminationStatus(false /* known_dead */,
226 exit_code);
229 bool BrowserChildProcessHostImpl::OnMessageReceived(
230 const IPC::Message& message) {
231 return delegate_->OnMessageReceived(message);
234 void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid) {
235 #if defined(OS_WIN)
236 // From this point onward, the exit of the child process is detected by an
237 // error on the IPC channel.
238 DeleteProcessWaitableEvent(early_exit_watcher_.GetWatchedEvent());
239 early_exit_watcher_.StopWatching();
240 #endif
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
243 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
244 base::Bind(&NotifyProcessHostConnected, data_));
246 delegate_->OnChannelConnected(peer_pid);
249 void BrowserChildProcessHostImpl::OnChannelError() {
250 delegate_->OnChannelError();
253 bool BrowserChildProcessHostImpl::CanShutdown() {
254 return delegate_->CanShutdown();
257 void BrowserChildProcessHostImpl::OnChildDisconnected() {
258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
259 DCHECK(data_.handle != base::kNullProcessHandle);
260 int exit_code;
261 base::TerminationStatus status = GetTerminationStatus(&exit_code);
262 switch (status) {
263 case base::TERMINATION_STATUS_PROCESS_CRASHED:
264 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: {
265 delegate_->OnProcessCrashed(exit_code);
266 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
267 base::Bind(&NotifyProcessCrashed, data_));
268 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2",
269 data_.process_type,
270 PROCESS_TYPE_MAX);
271 break;
273 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: {
274 delegate_->OnProcessCrashed(exit_code);
275 // Report that this child process was killed.
276 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2",
277 data_.process_type,
278 PROCESS_TYPE_MAX);
279 break;
281 case base::TERMINATION_STATUS_STILL_RUNNING: {
282 UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2",
283 data_.process_type,
284 PROCESS_TYPE_MAX);
286 default:
287 break;
289 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2",
290 data_.process_type,
291 PROCESS_TYPE_MAX);
292 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
293 base::Bind(&NotifyProcessHostDisconnected, data_));
294 delete delegate_; // Will delete us
297 bool BrowserChildProcessHostImpl::Send(IPC::Message* message) {
298 return child_process_host_->Send(message);
301 void BrowserChildProcessHostImpl::OnProcessLaunched() {
302 base::ProcessHandle handle = child_process_->GetHandle();
303 if (!handle) {
304 delete delegate_; // Will delete us
305 return;
308 #if defined(OS_WIN)
309 // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the
310 // child process exits. This watcher is stopped once the IPC channel is
311 // connected and the exit of the child process is detecter by an error on the
312 // IPC channel thereafter.
313 DCHECK(!early_exit_watcher_.GetWatchedEvent());
314 early_exit_watcher_.StartWatching(
315 new base::WaitableEvent(handle),
316 base::Bind(&BrowserChildProcessHostImpl::OnProcessExitedEarly,
317 base::Unretained(this)));
318 #endif
320 data_.handle = handle;
321 delegate_->OnProcessLaunched();
324 #if defined(OS_WIN)
326 void BrowserChildProcessHostImpl::DeleteProcessWaitableEvent(
327 base::WaitableEvent* event) {
328 if (!event)
329 return;
331 // The WaitableEvent does not own the process handle so ensure it does not
332 // close it.
333 event->Release();
335 delete event;
338 void BrowserChildProcessHostImpl::OnProcessExitedEarly(
339 base::WaitableEvent* event) {
340 DeleteProcessWaitableEvent(event);
341 OnChildDisconnected();
344 #endif
346 } // namespace content