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 "chrome/browser/ui/hung_plugin_tab_helper.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/process/process.h"
11 #include "base/rand_util.h"
12 #include "build/build_config.h"
13 #include "chrome/browser/chrome_notification_types.h"
14 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
15 #include "chrome/browser/infobars/infobar.h"
16 #include "chrome/browser/infobars/infobar_manager.h"
17 #include "chrome/browser/infobars/infobar_service.h"
18 #include "chrome/common/chrome_version_info.h"
19 #include "content/public/browser/browser_child_process_host_iterator.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/child_process_data.h"
22 #include "content/public/browser/notification_details.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/plugin_service.h"
25 #include "content/public/browser/render_process_host.h"
26 #include "content/public/common/process_type.h"
27 #include "content/public/common/result_codes.h"
28 #include "grit/chromium_strings.h"
29 #include "grit/generated_resources.h"
30 #include "grit/locale_settings.h"
31 #include "grit/theme_resources.h"
32 #include "ui/base/l10n/l10n_util.h"
35 #include "base/win/scoped_handle.h"
36 #include "chrome/browser/hang_monitor/hang_crash_dump_win.h"
44 // OwnedHandleVector ----------------------------------------------------------
46 class OwnedHandleVector
{
48 typedef std::vector
<HANDLE
> Handles
;
52 Handles
* data() { return &data_
; }
57 DISALLOW_COPY_AND_ASSIGN(OwnedHandleVector
);
60 OwnedHandleVector::OwnedHandleVector() {
63 OwnedHandleVector::~OwnedHandleVector() {
64 for (Handles::iterator iter
= data_
.begin(); iter
!= data_
.end(); ++iter
)
69 // Helpers --------------------------------------------------------------------
71 const char kDumpChildProcessesSequenceName
[] = "DumpChildProcesses";
73 void DumpBrowserInBlockingPool() {
74 CrashDumpForHangDebugging(::GetCurrentProcess());
77 void DumpRenderersInBlockingPool(OwnedHandleVector
* renderer_handles
) {
78 for (OwnedHandleVector::Handles::const_iterator iter
=
79 renderer_handles
->data()->begin();
80 iter
!= renderer_handles
->data()->end(); ++iter
) {
81 CrashDumpForHangDebugging(*iter
);
85 void DumpAndTerminatePluginInBlockingPool(
86 base::win::ScopedHandle
* plugin_handle
) {
87 CrashDumpAndTerminateHungChildProcess(plugin_handle
->Get());
90 #endif // defined(OS_WIN)
92 // Called on the I/O thread to actually kill the plugin with the given child
93 // ID. We specifically don't want this to be a member function since if the
94 // user chooses to kill the plugin, we want to kill it even if they close the
97 // Be careful with the child_id. It's supplied by the renderer which might be
99 void KillPluginOnIOThread(int child_id
) {
100 content::BrowserChildProcessHostIterator
iter(
101 content::PROCESS_TYPE_PPAPI_PLUGIN
);
102 while (!iter
.Done()) {
103 const content::ChildProcessData
& data
= iter
.GetData();
104 if (data
.id
== child_id
) {
106 HANDLE handle
= NULL
;
107 HANDLE current_process
= ::GetCurrentProcess();
108 ::DuplicateHandle(current_process
, data
.handle
, current_process
, &handle
,
109 0, FALSE
, DUPLICATE_SAME_ACCESS
);
110 // Run it in blocking pool so that it won't block the I/O thread. Besides,
111 // we would like to make sure that it happens after dumping renderers.
112 content::BrowserThread::PostBlockingPoolSequencedTask(
113 kDumpChildProcessesSequenceName
, FROM_HERE
,
114 base::Bind(&DumpAndTerminatePluginInBlockingPool
,
115 base::Owned(new base::win::ScopedHandle(handle
))));
117 base::KillProcess(data
.handle
, content::RESULT_CODE_HUNG
, false);
123 // Ignore the case where we didn't find the plugin, it may have terminated
124 // before this function could run.
130 // HungPluginInfoBarDelegate --------------------------------------------------
132 class HungPluginInfoBarDelegate
: public ConfirmInfoBarDelegate
{
134 // Creates a hung plugin infobar and delegate and adds the infobar to
135 // |infobar_service|. Returns the infobar if it was successfully added.
136 static InfoBar
* Create(InfoBarService
* infobar_service
,
137 HungPluginTabHelper
* helper
,
139 const base::string16
& plugin_name
);
142 HungPluginInfoBarDelegate(HungPluginTabHelper
* helper
,
144 const base::string16
& plugin_name
);
145 virtual ~HungPluginInfoBarDelegate();
147 // ConfirmInfoBarDelegate:
148 virtual int GetIconID() const OVERRIDE
;
149 virtual base::string16
GetMessageText() const OVERRIDE
;
150 virtual int GetButtons() const OVERRIDE
;
151 virtual base::string16
GetButtonLabel(InfoBarButton button
) const OVERRIDE
;
152 virtual bool Accept() OVERRIDE
;
154 HungPluginTabHelper
* helper_
;
155 int plugin_child_id_
;
157 base::string16 message_
;
158 base::string16 button_text_
;
162 InfoBar
* HungPluginInfoBarDelegate::Create(InfoBarService
* infobar_service
,
163 HungPluginTabHelper
* helper
,
165 const base::string16
& plugin_name
) {
166 return infobar_service
->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
167 scoped_ptr
<ConfirmInfoBarDelegate
>(new HungPluginInfoBarDelegate(
168 helper
, plugin_child_id
, plugin_name
))));
171 HungPluginInfoBarDelegate::HungPluginInfoBarDelegate(
172 HungPluginTabHelper
* helper
,
174 const base::string16
& plugin_name
)
175 : ConfirmInfoBarDelegate(),
177 plugin_child_id_(plugin_child_id
),
178 message_(l10n_util::GetStringFUTF16(
179 IDS_BROWSER_HANGMONITOR_PLUGIN_INFOBAR
, plugin_name
)),
180 button_text_(l10n_util::GetStringUTF16(
181 IDS_BROWSER_HANGMONITOR_PLUGIN_INFOBAR_KILLBUTTON
)) {
184 HungPluginInfoBarDelegate::~HungPluginInfoBarDelegate() {
187 int HungPluginInfoBarDelegate::GetIconID() const {
188 return IDR_INFOBAR_PLUGIN_CRASHED
;
191 base::string16
HungPluginInfoBarDelegate::GetMessageText() const {
195 int HungPluginInfoBarDelegate::GetButtons() const {
199 base::string16
HungPluginInfoBarDelegate::GetButtonLabel(
200 InfoBarButton button
) const {
204 bool HungPluginInfoBarDelegate::Accept() {
205 helper_
->KillPlugin(plugin_child_id_
);
210 // HungPluginTabHelper::PluginState -------------------------------------------
212 // Per-plugin state (since there could be more than one plugin hung). The
213 // integer key is the child process ID of the plugin process. This maintains
214 // the state for all plugins on this page that are currently hung, whether or
215 // not we're currently showing the infobar.
216 struct HungPluginTabHelper::PluginState
{
217 // Initializes the plugin state to be a hung plugin.
218 PluginState(const base::FilePath
& p
, const base::string16
& n
);
224 // Possibly-null if we're not showing an infobar right now.
227 // Time to delay before re-showing the infobar for a hung plugin. This is
228 // increased each time the user cancels it.
229 base::TimeDelta next_reshow_delay
;
231 // Handles calling the helper when the infobar should be re-shown.
235 // Initial delay in seconds before re-showing the hung plugin message.
236 static const int kInitialReshowDelaySec
;
238 // Since the scope of the timer manages our callback, this struct should
240 DISALLOW_COPY_AND_ASSIGN(PluginState
);
244 const int HungPluginTabHelper::PluginState::kInitialReshowDelaySec
= 10;
246 HungPluginTabHelper::PluginState::PluginState(const base::FilePath
& p
,
247 const base::string16
& n
)
251 next_reshow_delay(base::TimeDelta::FromSeconds(kInitialReshowDelaySec
)),
252 timer(false, false) {
255 HungPluginTabHelper::PluginState::~PluginState() {
259 // HungPluginTabHelper --------------------------------------------------------
261 DEFINE_WEB_CONTENTS_USER_DATA_KEY(HungPluginTabHelper
);
263 HungPluginTabHelper::HungPluginTabHelper(content::WebContents
* contents
)
264 : content::WebContentsObserver(contents
) {
265 registrar_
.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
,
266 content::NotificationService::AllSources());
269 HungPluginTabHelper::~HungPluginTabHelper() {
272 void HungPluginTabHelper::PluginCrashed(const base::FilePath
& plugin_path
,
273 base::ProcessId plugin_pid
) {
274 // TODO(brettw) ideally this would take the child process ID. When we do this
275 // for NaCl plugins, we'll want to know exactly which process it was since
276 // the path won't be useful.
277 InfoBarService
* infobar_service
=
278 InfoBarService::FromWebContents(web_contents());
279 if (!infobar_service
)
282 InfoBarManager
* infobar_manager
= infobar_service
->infobar_manager();
283 // For now, just do a brute-force search to see if we have this plugin. Since
284 // we'll normally have 0 or 1, this is fast.
285 for (PluginStateMap::iterator i
= hung_plugins_
.begin();
286 i
!= hung_plugins_
.end(); ++i
) {
287 if (i
->second
->path
== plugin_path
) {
288 if (i
->second
->infobar
)
289 infobar_manager
->RemoveInfoBar(i
->second
->infobar
);
290 hung_plugins_
.erase(i
);
296 void HungPluginTabHelper::PluginHungStatusChanged(
298 const base::FilePath
& plugin_path
,
300 InfoBarService
* infobar_service
=
301 InfoBarService::FromWebContents(web_contents());
302 if (!infobar_service
)
305 InfoBarManager
* infobar_manager
= infobar_service
->infobar_manager();
306 PluginStateMap::iterator found
= hung_plugins_
.find(plugin_child_id
);
307 if (found
!= hung_plugins_
.end()) {
309 // Hung plugin became un-hung, close the infobar and delete our info.
310 if (found
->second
->infobar
)
311 infobar_manager
->RemoveInfoBar(found
->second
->infobar
);
312 hung_plugins_
.erase(found
);
317 base::string16 plugin_name
=
318 content::PluginService::GetInstance()->GetPluginDisplayNameByPath(
321 linked_ptr
<PluginState
> state(new PluginState(plugin_path
, plugin_name
));
322 hung_plugins_
[plugin_child_id
] = state
;
323 ShowBar(plugin_child_id
, state
.get());
326 void HungPluginTabHelper::Observe(
328 const content::NotificationSource
& source
,
329 const content::NotificationDetails
& details
) {
330 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
, type
);
331 InfoBar
* infobar
= content::Details
<InfoBar::RemovedDetails
>(details
)->first
;
332 for (PluginStateMap::iterator i
= hung_plugins_
.begin();
333 i
!= hung_plugins_
.end(); ++i
) {
334 PluginState
* state
= i
->second
.get();
335 if (state
->infobar
== infobar
) {
336 state
->infobar
= NULL
;
338 // Schedule the timer to re-show the infobar if the plugin continues to be
340 state
->timer
.Start(FROM_HERE
, state
->next_reshow_delay
,
341 base::Bind(&HungPluginTabHelper::OnReshowTimer
,
342 base::Unretained(this),
345 // Next time we do this, delay it twice as long to avoid being annoying.
346 state
->next_reshow_delay
*= 2;
352 void HungPluginTabHelper::KillPlugin(int child_id
) {
354 // Dump renderers that are sending or receiving pepper messages, in order to
355 // diagnose inter-process deadlocks.
356 // Only do that on the Canary channel, for 20% of pepper plugin hangs.
357 if (base::RandInt(0, 100) < 20) {
358 chrome::VersionInfo::Channel channel
= chrome::VersionInfo::GetChannel();
359 if (channel
== chrome::VersionInfo::CHANNEL_CANARY
) {
360 scoped_ptr
<OwnedHandleVector
> renderer_handles(new OwnedHandleVector
);
361 HANDLE current_process
= ::GetCurrentProcess();
362 content::RenderProcessHost::iterator renderer_iter
=
363 content::RenderProcessHost::AllHostsIterator();
364 for (; !renderer_iter
.IsAtEnd(); renderer_iter
.Advance()) {
365 content::RenderProcessHost
* host
= renderer_iter
.GetCurrentValue();
366 HANDLE handle
= NULL
;
367 ::DuplicateHandle(current_process
, host
->GetHandle(), current_process
,
368 &handle
, 0, FALSE
, DUPLICATE_SAME_ACCESS
);
369 renderer_handles
->data()->push_back(handle
);
371 // If there are a lot of renderer processes, it is likely that we will
372 // generate too many crash dumps. They might not all be uploaded/recorded
373 // due to our crash dump uploading restrictions. So we just don't generate
374 // renderer crash dumps in that case.
375 if (renderer_handles
->data()->size() > 0 &&
376 renderer_handles
->data()->size() < 4) {
377 content::BrowserThread::PostBlockingPoolSequencedTask(
378 kDumpChildProcessesSequenceName
, FROM_HERE
,
379 base::Bind(&DumpBrowserInBlockingPool
));
380 content::BrowserThread::PostBlockingPoolSequencedTask(
381 kDumpChildProcessesSequenceName
, FROM_HERE
,
382 base::Bind(&DumpRenderersInBlockingPool
,
383 base::Owned(renderer_handles
.release())));
389 PluginStateMap::iterator found
= hung_plugins_
.find(child_id
);
390 DCHECK(found
!= hung_plugins_
.end());
392 content::BrowserThread::PostTask(content::BrowserThread::IO
,
394 base::Bind(&KillPluginOnIOThread
, child_id
));
395 CloseBar(found
->second
.get());
398 void HungPluginTabHelper::OnReshowTimer(int child_id
) {
399 // The timer should have been cancelled if the record isn't in our map
401 PluginStateMap::iterator found
= hung_plugins_
.find(child_id
);
402 DCHECK(found
!= hung_plugins_
.end());
403 DCHECK(!found
->second
->infobar
);
404 ShowBar(child_id
, found
->second
.get());
407 void HungPluginTabHelper::ShowBar(int child_id
, PluginState
* state
) {
408 InfoBarService
* infobar_service
=
409 InfoBarService::FromWebContents(web_contents());
410 if (!infobar_service
)
413 DCHECK(!state
->infobar
);
414 state
->infobar
= HungPluginInfoBarDelegate::Create(infobar_service
, this,
415 child_id
, state
->name
);
418 void HungPluginTabHelper::CloseBar(PluginState
* state
) {
419 InfoBarService
* infobar_service
=
420 InfoBarService::FromWebContents(web_contents());
421 if (infobar_service
&& state
->infobar
) {
422 infobar_service
->infobar_manager()->RemoveInfoBar(state
->infobar
);
423 state
->infobar
= NULL
;