cleanup: Use ExtensionRegistryObserver in Uber
[chromium-blink-merge.git] / content / browser / browser_child_process_host_impl.cc
blob6ace7de4aba1f452b409a7e5532d1ae14e4d4e14
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/profiler/scoped_tracker.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_CURRENTLY_ON(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);
110 power_monitor_message_broadcaster_.Init();
113 BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() {
114 g_child_process_list.Get().remove(this);
117 // static
118 void BrowserChildProcessHostImpl::TerminateAll() {
119 DCHECK_CURRENTLY_ON(BrowserThread::IO);
120 // Make a copy since the BrowserChildProcessHost dtor mutates the original
121 // list.
122 BrowserChildProcessList copy = g_child_process_list.Get();
123 for (BrowserChildProcessList::iterator it = copy.begin();
124 it != copy.end(); ++it) {
125 delete (*it)->delegate(); // ~*HostDelegate deletes *HostImpl.
129 void BrowserChildProcessHostImpl::Launch(
130 SandboxedProcessLauncherDelegate* delegate,
131 base::CommandLine* cmd_line) {
132 DCHECK_CURRENTLY_ON(BrowserThread::IO);
134 GetContentClient()->browser()->AppendExtraCommandLineSwitches(
135 cmd_line, data_.id);
137 const base::CommandLine& browser_command_line =
138 *base::CommandLine::ForCurrentProcess();
139 static const char* kForwardSwitches[] = {
140 switches::kDisableLogging,
141 switches::kEnableLogging,
142 switches::kIPCConnectionTimeout,
143 switches::kLoggingLevel,
144 switches::kTraceToConsole,
145 switches::kV,
146 switches::kVModule,
148 cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches,
149 arraysize(kForwardSwitches));
151 child_process_.reset(new ChildProcessLauncher(
152 delegate,
153 cmd_line,
154 data_.id,
155 this));
158 const ChildProcessData& BrowserChildProcessHostImpl::GetData() const {
159 DCHECK_CURRENTLY_ON(BrowserThread::IO);
160 return data_;
163 ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const {
164 DCHECK_CURRENTLY_ON(BrowserThread::IO);
165 return child_process_host_.get();
168 const base::Process& BrowserChildProcessHostImpl::GetProcess() const {
169 DCHECK_CURRENTLY_ON(BrowserThread::IO);
170 DCHECK(child_process_.get())
171 << "Requesting a child process handle before launching.";
172 DCHECK(child_process_->GetProcess().IsValid())
173 << "Requesting a child process handle before launch has completed OK.";
174 return child_process_->GetProcess();
177 void BrowserChildProcessHostImpl::SetName(const base::string16& name) {
178 DCHECK_CURRENTLY_ON(BrowserThread::IO);
179 data_.name = name;
182 void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle) {
183 DCHECK_CURRENTLY_ON(BrowserThread::IO);
184 data_.handle = handle;
187 void BrowserChildProcessHostImpl::ForceShutdown() {
188 DCHECK_CURRENTLY_ON(BrowserThread::IO);
189 g_child_process_list.Get().remove(this);
190 child_process_host_->ForceShutdown();
193 void BrowserChildProcessHostImpl::SetBackgrounded(bool backgrounded) {
194 child_process_->SetProcessBackgrounded(backgrounded);
197 void BrowserChildProcessHostImpl::SetTerminateChildOnShutdown(
198 bool terminate_on_shutdown) {
199 DCHECK_CURRENTLY_ON(BrowserThread::IO);
200 child_process_->SetTerminateChildOnShutdown(terminate_on_shutdown);
203 void BrowserChildProcessHostImpl::AddFilter(BrowserMessageFilter* filter) {
204 child_process_host_->AddFilter(filter->GetFilter());
207 void BrowserChildProcessHostImpl::NotifyProcessInstanceCreated(
208 const ChildProcessData& data) {
209 DCHECK_CURRENTLY_ON(BrowserThread::UI);
210 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
211 BrowserChildProcessInstanceCreated(data));
214 void BrowserChildProcessHostImpl::HistogramBadMessageTerminated(
215 int process_type) {
216 UMA_HISTOGRAM_ENUMERATION("ChildProcess.BadMessgeTerminated", process_type,
217 PROCESS_TYPE_MAX);
220 base::TerminationStatus BrowserChildProcessHostImpl::GetTerminationStatus(
221 bool known_dead, int* exit_code) {
222 DCHECK_CURRENTLY_ON(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(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 early_exit_watcher_.StopWatching();
239 #endif
241 DCHECK_CURRENTLY_ON(BrowserThread::IO);
242 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
243 base::Bind(&NotifyProcessHostConnected, data_));
245 delegate_->OnChannelConnected(peer_pid);
248 void BrowserChildProcessHostImpl::OnChannelError() {
249 delegate_->OnChannelError();
252 void BrowserChildProcessHostImpl::OnBadMessageReceived(
253 const IPC::Message& message) {
254 HistogramBadMessageTerminated(data_.process_type);
255 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
256 switches::kDisableKillAfterBadIPC)) {
257 return;
259 LOG(ERROR) << "Terminating child process for bad IPC message of type "
260 << message.type();
261 child_process_->GetProcess().Terminate(RESULT_CODE_KILLED_BAD_MESSAGE, false);
264 bool BrowserChildProcessHostImpl::CanShutdown() {
265 return delegate_->CanShutdown();
268 void BrowserChildProcessHostImpl::OnChildDisconnected() {
269 DCHECK_CURRENTLY_ON(BrowserThread::IO);
270 #if defined(OS_WIN)
271 // OnChildDisconnected may be called without OnChannelConnected, so stop the
272 // early exit watcher so GetTerminationStatus can close the process handle.
273 early_exit_watcher_.StopWatching();
274 #endif
275 if (child_process_.get() || data_.handle) {
276 int exit_code;
277 base::TerminationStatus status = GetTerminationStatus(
278 true /* known_dead */, &exit_code);
279 switch (status) {
280 case base::TERMINATION_STATUS_PROCESS_CRASHED:
281 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: {
282 delegate_->OnProcessCrashed(exit_code);
283 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
284 base::Bind(&NotifyProcessCrashed, data_));
285 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2",
286 data_.process_type,
287 PROCESS_TYPE_MAX);
288 break;
290 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: {
291 delegate_->OnProcessCrashed(exit_code);
292 // Report that this child process was killed.
293 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2",
294 data_.process_type,
295 PROCESS_TYPE_MAX);
296 break;
298 case base::TERMINATION_STATUS_STILL_RUNNING: {
299 UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2",
300 data_.process_type,
301 PROCESS_TYPE_MAX);
303 default:
304 break;
306 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2",
307 data_.process_type,
308 PROCESS_TYPE_MAX);
310 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
311 base::Bind(&NotifyProcessHostDisconnected, data_));
312 delete delegate_; // Will delete us
315 bool BrowserChildProcessHostImpl::Send(IPC::Message* message) {
316 return child_process_host_->Send(message);
319 void BrowserChildProcessHostImpl::OnProcessLaunchFailed() {
320 delegate_->OnProcessLaunchFailed();
321 delete delegate_; // Will delete us
324 void BrowserChildProcessHostImpl::OnProcessLaunched() {
325 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/465841
326 // is fixed.
327 tracked_objects::ScopedTracker tracking_profile(
328 FROM_HERE_WITH_EXPLICIT_FUNCTION(
329 "465841 BrowserChildProcessHostImpl::OnProcessLaunched"));
330 const base::Process& process = child_process_->GetProcess();
331 DCHECK(process.IsValid());
333 #if defined(OS_WIN)
334 // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the
335 // child process exits. This watcher is stopped once the IPC channel is
336 // connected and the exit of the child process is detecter by an error on the
337 // IPC channel thereafter.
338 DCHECK(!early_exit_watcher_.GetWatchedObject());
339 early_exit_watcher_.StartWatching(process.Handle(), this);
340 #endif
342 // TODO(rvargas) crbug.com/417532: Don't store a handle.
343 data_.handle = process.Handle();
344 delegate_->OnProcessLaunched();
347 #if defined(OS_WIN)
349 void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) {
350 OnChildDisconnected();
353 #endif
355 } // namespace content