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/infobar_service.h"
15 #include "chrome/common/channel_info.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "chrome/grit/locale_settings.h"
18 #include "components/infobars/core/confirm_infobar_delegate.h"
19 #include "components/infobars/core/infobar.h"
20 #include "components/version_info/version_info.h"
21 #include "content/public/browser/browser_child_process_host_iterator.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/child_process_data.h"
24 #include "content/public/browser/notification_details.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/plugin_service.h"
27 #include "content/public/browser/render_process_host.h"
28 #include "content/public/common/process_type.h"
29 #include "content/public/common/result_codes.h"
30 #include "grit/theme_resources.h"
31 #include "ui/base/l10n/l10n_util.h"
34 #include "base/win/scoped_handle.h"
35 #include "chrome/browser/hang_monitor/hang_crash_dump_win.h"
43 // OwnedHandleVector ----------------------------------------------------------
45 class OwnedHandleVector
{
47 typedef std::vector
<HANDLE
> Handles
;
51 Handles
* data() { return &data_
; }
56 DISALLOW_COPY_AND_ASSIGN(OwnedHandleVector
);
59 OwnedHandleVector::OwnedHandleVector() {
62 OwnedHandleVector::~OwnedHandleVector() {
63 for (Handles::iterator iter
= data_
.begin(); iter
!= data_
.end(); ++iter
)
68 // Helpers --------------------------------------------------------------------
70 const char kDumpChildProcessesSequenceName
[] = "DumpChildProcesses";
72 void DumpBrowserInBlockingPool() {
73 CrashDumpForHangDebugging(::GetCurrentProcess());
76 void DumpRenderersInBlockingPool(OwnedHandleVector
* renderer_handles
) {
77 for (OwnedHandleVector::Handles::const_iterator iter
=
78 renderer_handles
->data()->begin();
79 iter
!= renderer_handles
->data()->end(); ++iter
) {
80 CrashDumpForHangDebugging(*iter
);
84 void DumpAndTerminatePluginInBlockingPool(
85 base::win::ScopedHandle
* plugin_handle
) {
86 CrashDumpAndTerminateHungChildProcess(plugin_handle
->Get());
89 #endif // defined(OS_WIN)
91 // Called on the I/O thread to actually kill the plugin with the given child
92 // ID. We specifically don't want this to be a member function since if the
93 // user chooses to kill the plugin, we want to kill it even if they close the
96 // Be careful with the child_id. It's supplied by the renderer which might be
98 void KillPluginOnIOThread(int child_id
) {
99 content::BrowserChildProcessHostIterator
iter(
100 content::PROCESS_TYPE_PPAPI_PLUGIN
);
101 while (!iter
.Done()) {
102 const content::ChildProcessData
& data
= iter
.GetData();
103 if (data
.id
== child_id
) {
105 HANDLE handle
= NULL
;
106 HANDLE current_process
= ::GetCurrentProcess();
107 ::DuplicateHandle(current_process
, data
.handle
, current_process
, &handle
,
108 0, FALSE
, DUPLICATE_SAME_ACCESS
);
109 // Run it in blocking pool so that it won't block the I/O thread. Besides,
110 // we would like to make sure that it happens after dumping renderers.
111 content::BrowserThread::PostBlockingPoolSequencedTask(
112 kDumpChildProcessesSequenceName
, FROM_HERE
,
113 base::Bind(&DumpAndTerminatePluginInBlockingPool
,
114 base::Owned(new base::win::ScopedHandle(handle
))));
116 base::Process process
=
117 base::Process::DeprecatedGetProcessFromHandle(data
.handle
);
118 process
.Terminate(content::RESULT_CODE_HUNG
, false);
124 // Ignore the case where we didn't find the plugin, it may have terminated
125 // before this function could run.
131 // HungPluginInfoBarDelegate --------------------------------------------------
133 class HungPluginInfoBarDelegate
: public ConfirmInfoBarDelegate
{
135 // Creates a hung plugin infobar and delegate and adds the infobar to
136 // |infobar_service|. Returns the infobar if it was successfully added.
137 static infobars::InfoBar
* Create(InfoBarService
* infobar_service
,
138 HungPluginTabHelper
* helper
,
140 const base::string16
& plugin_name
);
143 HungPluginInfoBarDelegate(HungPluginTabHelper
* helper
,
145 const base::string16
& plugin_name
);
146 ~HungPluginInfoBarDelegate() override
;
148 // ConfirmInfoBarDelegate:
149 int GetIconId() const override
;
150 base::string16
GetMessageText() const override
;
151 int GetButtons() const override
;
152 base::string16
GetButtonLabel(InfoBarButton button
) const override
;
153 bool Accept() override
;
155 HungPluginTabHelper
* helper_
;
156 int plugin_child_id_
;
158 base::string16 message_
;
159 base::string16 button_text_
;
163 infobars::InfoBar
* HungPluginInfoBarDelegate::Create(
164 InfoBarService
* infobar_service
,
165 HungPluginTabHelper
* helper
,
167 const base::string16
& plugin_name
) {
168 return infobar_service
->AddInfoBar(infobar_service
->CreateConfirmInfoBar(
169 scoped_ptr
<ConfirmInfoBarDelegate
>(new HungPluginInfoBarDelegate(
170 helper
, plugin_child_id
, plugin_name
))));
173 HungPluginInfoBarDelegate::HungPluginInfoBarDelegate(
174 HungPluginTabHelper
* helper
,
176 const base::string16
& plugin_name
)
177 : ConfirmInfoBarDelegate(),
179 plugin_child_id_(plugin_child_id
),
180 message_(l10n_util::GetStringFUTF16(
181 IDS_BROWSER_HANGMONITOR_PLUGIN_INFOBAR
, plugin_name
)),
182 button_text_(l10n_util::GetStringUTF16(
183 IDS_BROWSER_HANGMONITOR_PLUGIN_INFOBAR_KILLBUTTON
)) {
186 HungPluginInfoBarDelegate::~HungPluginInfoBarDelegate() {
189 int HungPluginInfoBarDelegate::GetIconId() const {
190 return IDR_INFOBAR_PLUGIN_CRASHED
;
193 base::string16
HungPluginInfoBarDelegate::GetMessageText() const {
197 int HungPluginInfoBarDelegate::GetButtons() const {
201 base::string16
HungPluginInfoBarDelegate::GetButtonLabel(
202 InfoBarButton button
) const {
206 bool HungPluginInfoBarDelegate::Accept() {
207 helper_
->KillPlugin(plugin_child_id_
);
212 // HungPluginTabHelper::PluginState -------------------------------------------
214 // Per-plugin state (since there could be more than one plugin hung). The
215 // integer key is the child process ID of the plugin process. This maintains
216 // the state for all plugins on this page that are currently hung, whether or
217 // not we're currently showing the infobar.
218 struct HungPluginTabHelper::PluginState
{
219 // Initializes the plugin state to be a hung plugin.
220 PluginState(const base::FilePath
& p
, const base::string16
& n
);
226 // Possibly-null if we're not showing an infobar right now.
227 infobars::InfoBar
* infobar
;
229 // Time to delay before re-showing the infobar for a hung plugin. This is
230 // increased each time the user cancels it.
231 base::TimeDelta next_reshow_delay
;
233 // Handles calling the helper when the infobar should be re-shown.
237 // Initial delay in seconds before re-showing the hung plugin message.
238 static const int kInitialReshowDelaySec
;
240 // Since the scope of the timer manages our callback, this struct should
242 DISALLOW_COPY_AND_ASSIGN(PluginState
);
246 const int HungPluginTabHelper::PluginState::kInitialReshowDelaySec
= 10;
248 HungPluginTabHelper::PluginState::PluginState(const base::FilePath
& p
,
249 const base::string16
& n
)
253 next_reshow_delay(base::TimeDelta::FromSeconds(kInitialReshowDelaySec
)),
254 timer(false, false) {
257 HungPluginTabHelper::PluginState::~PluginState() {
261 // HungPluginTabHelper --------------------------------------------------------
263 DEFINE_WEB_CONTENTS_USER_DATA_KEY(HungPluginTabHelper
);
265 HungPluginTabHelper::HungPluginTabHelper(content::WebContents
* contents
)
266 : content::WebContentsObserver(contents
) {
267 registrar_
.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED
,
268 content::NotificationService::AllSources());
271 HungPluginTabHelper::~HungPluginTabHelper() {
274 void HungPluginTabHelper::PluginCrashed(const base::FilePath
& plugin_path
,
275 base::ProcessId plugin_pid
) {
276 // TODO(brettw) ideally this would take the child process ID. When we do this
277 // for NaCl plugins, we'll want to know exactly which process it was since
278 // the path won't be useful.
279 InfoBarService
* infobar_service
=
280 InfoBarService::FromWebContents(web_contents());
281 if (!infobar_service
)
284 // For now, just do a brute-force search to see if we have this plugin. Since
285 // we'll normally have 0 or 1, this is fast.
286 for (PluginStateMap::iterator i
= hung_plugins_
.begin();
287 i
!= hung_plugins_
.end(); ++i
) {
288 if (i
->second
->path
== plugin_path
) {
289 if (i
->second
->infobar
)
290 infobar_service
->RemoveInfoBar(i
->second
->infobar
);
291 hung_plugins_
.erase(i
);
297 void HungPluginTabHelper::PluginHungStatusChanged(
299 const base::FilePath
& plugin_path
,
301 InfoBarService
* infobar_service
=
302 InfoBarService::FromWebContents(web_contents());
303 if (!infobar_service
)
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_service
->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 infobars::InfoBar
* infobar
=
332 content::Details
<infobars::InfoBar::RemovedDetails
>(details
)->first
;
333 for (PluginStateMap::iterator i
= hung_plugins_
.begin();
334 i
!= hung_plugins_
.end(); ++i
) {
335 PluginState
* state
= i
->second
.get();
336 if (state
->infobar
== infobar
) {
337 state
->infobar
= NULL
;
339 // Schedule the timer to re-show the infobar if the plugin continues to be
341 state
->timer
.Start(FROM_HERE
, state
->next_reshow_delay
,
342 base::Bind(&HungPluginTabHelper::OnReshowTimer
,
343 base::Unretained(this),
346 // Next time we do this, delay it twice as long to avoid being annoying.
347 state
->next_reshow_delay
*= 2;
353 void HungPluginTabHelper::KillPlugin(int child_id
) {
355 // Dump renderers that are sending or receiving pepper messages, in order to
356 // diagnose inter-process deadlocks.
357 // Only do that on the Canary channel, for 20% of pepper plugin hangs.
358 if (base::RandInt(0, 100) < 20) {
359 version_info::Channel channel
= chrome::GetChannel();
360 if (channel
== version_info::Channel::CANARY
) {
361 scoped_ptr
<OwnedHandleVector
> renderer_handles(new OwnedHandleVector
);
362 HANDLE current_process
= ::GetCurrentProcess();
363 content::RenderProcessHost::iterator renderer_iter
=
364 content::RenderProcessHost::AllHostsIterator();
365 for (; !renderer_iter
.IsAtEnd(); renderer_iter
.Advance()) {
366 content::RenderProcessHost
* host
= renderer_iter
.GetCurrentValue();
367 HANDLE handle
= NULL
;
368 ::DuplicateHandle(current_process
, host
->GetHandle(), current_process
,
369 &handle
, 0, FALSE
, DUPLICATE_SAME_ACCESS
);
370 renderer_handles
->data()->push_back(handle
);
372 // If there are a lot of renderer processes, it is likely that we will
373 // generate too many crash dumps. They might not all be uploaded/recorded
374 // due to our crash dump uploading restrictions. So we just don't generate
375 // renderer crash dumps in that case.
376 if (renderer_handles
->data()->size() > 0 &&
377 renderer_handles
->data()->size() < 4) {
378 content::BrowserThread::PostBlockingPoolSequencedTask(
379 kDumpChildProcessesSequenceName
, FROM_HERE
,
380 base::Bind(&DumpBrowserInBlockingPool
));
381 content::BrowserThread::PostBlockingPoolSequencedTask(
382 kDumpChildProcessesSequenceName
, FROM_HERE
,
383 base::Bind(&DumpRenderersInBlockingPool
,
384 base::Owned(renderer_handles
.release())));
390 PluginStateMap::iterator found
= hung_plugins_
.find(child_id
);
391 DCHECK(found
!= hung_plugins_
.end());
393 content::BrowserThread::PostTask(content::BrowserThread::IO
,
395 base::Bind(&KillPluginOnIOThread
, child_id
));
396 CloseBar(found
->second
.get());
399 void HungPluginTabHelper::OnReshowTimer(int child_id
) {
400 // The timer should have been cancelled if the record isn't in our map
402 PluginStateMap::iterator found
= hung_plugins_
.find(child_id
);
403 DCHECK(found
!= hung_plugins_
.end());
404 DCHECK(!found
->second
->infobar
);
405 ShowBar(child_id
, found
->second
.get());
408 void HungPluginTabHelper::ShowBar(int child_id
, PluginState
* state
) {
409 InfoBarService
* infobar_service
=
410 InfoBarService::FromWebContents(web_contents());
411 if (!infobar_service
)
414 DCHECK(!state
->infobar
);
415 state
->infobar
= HungPluginInfoBarDelegate::Create(infobar_service
, this,
416 child_id
, state
->name
);
419 void HungPluginTabHelper::CloseBar(PluginState
* state
) {
420 InfoBarService
* infobar_service
=
421 InfoBarService::FromWebContents(web_contents());
422 if (infobar_service
&& state
->infobar
) {
423 infobar_service
->RemoveInfoBar(state
->infobar
);
424 state
->infobar
= NULL
;