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/views/hung_renderer_view.h"
7 #include "base/i18n/rtl.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/platform_util.h"
10 #include "chrome/browser/ui/browser_finder.h"
11 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
12 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
13 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
14 #include "chrome/common/chrome_constants.h"
15 #include "chrome/common/logging_chrome.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "components/constrained_window/constrained_window_views.h"
18 #include "components/favicon/content/content_favicon_driver.h"
19 #include "components/web_modal/web_contents_modal_dialog_host.h"
20 #include "content/public/browser/render_process_host.h"
21 #include "content/public/browser/render_view_host.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/common/result_codes.h"
24 #include "grit/theme_resources.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/gfx/canvas.h"
28 #include "ui/views/controls/button/label_button.h"
29 #include "ui/views/controls/image_view.h"
30 #include "ui/views/controls/label.h"
31 #include "ui/views/layout/grid_layout.h"
32 #include "ui/views/layout/layout_constants.h"
33 #include "ui/views/widget/widget.h"
34 #include "ui/views/window/client_view.h"
37 #include "chrome/browser/hang_monitor/hang_crash_dump_win.h"
38 #include "chrome/browser/profiles/profile.h"
39 #include "chrome/browser/shell_integration.h"
40 #include "ui/base/win/shell.h"
41 #include "ui/views/win/hwnd_util.h"
45 #include "ui/aura/window.h"
48 using content::WebContents
;
50 HungRendererDialogView
* HungRendererDialogView::g_instance_
= NULL
;
52 ///////////////////////////////////////////////////////////////////////////////
53 // HungPagesTableModel, public:
55 HungPagesTableModel::HungPagesTableModel(Delegate
* delegate
)
60 HungPagesTableModel::~HungPagesTableModel() {
63 content::RenderProcessHost
* HungPagesTableModel::GetRenderProcessHost() {
64 return tab_observers_
.empty() ? NULL
:
65 tab_observers_
[0]->web_contents()->GetRenderProcessHost();
68 content::RenderViewHost
* HungPagesTableModel::GetRenderViewHost() {
69 return tab_observers_
.empty() ? NULL
:
70 tab_observers_
[0]->web_contents()->GetRenderViewHost();
73 void HungPagesTableModel::InitForWebContents(WebContents
* hung_contents
) {
74 tab_observers_
.clear();
76 // Force hung_contents to be first.
78 tab_observers_
.push_back(new WebContentsObserverImpl(this,
81 for (TabContentsIterator it
; !it
.done(); it
.Next()) {
82 if (*it
!= hung_contents
&&
83 it
->GetRenderProcessHost() == hung_contents
->GetRenderProcessHost())
84 tab_observers_
.push_back(new WebContentsObserverImpl(this, *it
));
87 // The world is different.
89 observer_
->OnModelChanged();
92 ///////////////////////////////////////////////////////////////////////////////
93 // HungPagesTableModel, ui::TableModel implementation:
95 int HungPagesTableModel::RowCount() {
96 return static_cast<int>(tab_observers_
.size());
99 base::string16
HungPagesTableModel::GetText(int row
, int column_id
) {
100 DCHECK(row
>= 0 && row
< RowCount());
101 base::string16 title
= tab_observers_
[row
]->web_contents()->GetTitle();
103 title
= CoreTabHelper::GetDefaultTitle();
104 // TODO(xji): Consider adding a special case if the title text is a URL,
105 // since those should always have LTR directionality. Please refer to
106 // http://crbug.com/6726 for more information.
107 base::i18n::AdjustStringForLocaleDirection(&title
);
111 gfx::ImageSkia
HungPagesTableModel::GetIcon(int row
) {
112 DCHECK(row
>= 0 && row
< RowCount());
113 return favicon::ContentFaviconDriver::FromWebContents(
114 tab_observers_
[row
]->web_contents())
119 void HungPagesTableModel::SetObserver(ui::TableModelObserver
* observer
) {
120 observer_
= observer
;
123 void HungPagesTableModel::GetGroupRange(int model_index
,
124 views::GroupRange
* range
) {
127 range
->length
= RowCount();
130 void HungPagesTableModel::TabDestroyed(WebContentsObserverImpl
* tab
) {
131 // Clean up tab_observers_ and notify our observer.
132 TabObservers::iterator i
= std::find(
133 tab_observers_
.begin(), tab_observers_
.end(), tab
);
134 DCHECK(i
!= tab_observers_
.end());
135 int index
= static_cast<int>(i
- tab_observers_
.begin());
136 tab_observers_
.erase(i
);
138 observer_
->OnItemsRemoved(index
, 1);
140 // Notify the delegate.
141 delegate_
->TabDestroyed();
142 // WARNING: we've likely been deleted.
145 HungPagesTableModel::WebContentsObserverImpl::WebContentsObserverImpl(
146 HungPagesTableModel
* model
, WebContents
* tab
)
147 : content::WebContentsObserver(tab
),
151 void HungPagesTableModel::WebContentsObserverImpl::RenderProcessGone(
152 base::TerminationStatus status
) {
153 model_
->TabDestroyed(this);
156 void HungPagesTableModel::WebContentsObserverImpl::WebContentsDestroyed() {
157 model_
->TabDestroyed(this);
160 ///////////////////////////////////////////////////////////////////////////////
161 // HungRendererDialogView
164 gfx::ImageSkia
* HungRendererDialogView::frozen_icon_
= NULL
;
166 // The dimensions of the hung pages list table view, in pixels.
167 static const int kTableViewWidth
= 300;
168 static const int kTableViewHeight
= 100;
170 // Padding space in pixels between frozen icon to the info label, hung pages
171 // list table view and the Kill pages button.
172 static const int kCentralColumnPadding
=
173 views::kUnrelatedControlLargeHorizontalSpacing
;
175 ///////////////////////////////////////////////////////////////////////////////
176 // HungRendererDialogView, public:
179 HungRendererDialogView
* HungRendererDialogView::Create(
180 gfx::NativeWindow context
) {
182 g_instance_
= new HungRendererDialogView
;
183 views::DialogDelegate::CreateDialogWidget(g_instance_
, context
, NULL
);
189 HungRendererDialogView
* HungRendererDialogView::GetInstance() {
194 void HungRendererDialogView::Show(WebContents
* contents
) {
195 if (logging::DialogsAreSuppressed())
198 gfx::NativeWindow window
=
199 platform_util::GetTopLevel(contents
->GetNativeView());
200 #if defined(USE_AURA)
201 // Don't show the dialog if there is no root window for the renderer, because
202 // it's invisible to the user (happens when the renderer is for prerendering
204 if (!window
->GetRootWindow())
207 HungRendererDialogView
* view
= HungRendererDialogView::Create(window
);
208 view
->ShowForWebContents(contents
);
212 void HungRendererDialogView::Hide(WebContents
* contents
) {
213 if (!logging::DialogsAreSuppressed() && HungRendererDialogView::GetInstance())
214 HungRendererDialogView::GetInstance()->EndForWebContents(contents
);
218 bool HungRendererDialogView::IsFrameActive(WebContents
* contents
) {
219 gfx::NativeWindow window
=
220 platform_util::GetTopLevel(contents
->GetNativeView());
221 return platform_util::IsWindowActive(window
);
225 void HungRendererDialogView::KillRendererProcess(
226 content::RenderProcessHost
* rph
) {
228 // Try to generate a crash report for the hung process.
229 CrashDumpAndTerminateHungChildProcess(rph
->GetHandle());
231 rph
->Shutdown(content::RESULT_CODE_HUNG
, false);
236 HungRendererDialogView::HungRendererDialogView()
237 : info_label_(nullptr),
238 hung_pages_table_(nullptr),
239 kill_button_(nullptr),
240 initialized_(false) {
244 HungRendererDialogView::~HungRendererDialogView() {
245 hung_pages_table_
->SetModel(NULL
);
248 void HungRendererDialogView::ShowForWebContents(WebContents
* contents
) {
249 DCHECK(contents
&& GetWidget());
251 // Don't show the warning unless the foreground window is the frame, or this
252 // window (but still invisible). If the user has another window or
253 // application selected, activating ourselves is rude.
254 if (!IsFrameActive(contents
) &&
255 !platform_util::IsWindowActive(GetWidget()->GetNativeWindow()))
258 if (!GetWidget()->IsActive()) {
259 // Place the dialog over content's browser window, similar to modal dialogs.
260 Browser
* browser
= chrome::FindBrowserWithWebContents(contents
);
262 ChromeWebModalDialogManagerDelegate
* manager
= browser
;
263 constrained_window::UpdateWidgetModalDialogPosition(
264 GetWidget(), manager
->GetWebContentsModalDialogHost());
267 gfx::NativeWindow window
=
268 platform_util::GetTopLevel(contents
->GetNativeView());
269 views::Widget
* insert_after
=
270 views::Widget::GetWidgetForNativeWindow(window
);
272 GetWidget()->StackAboveWidget(insert_after
);
275 // Group the hung renderer dialog with the browsers with the same profile.
277 Profile::FromBrowserContext(contents
->GetBrowserContext());
278 ui::win::SetAppIdForWindow(
279 ShellIntegration::GetChromiumModelIdForProfile(profile
->GetPath()),
280 views::HWNDForWidget(GetWidget()));
283 // We only do this if the window isn't active (i.e. hasn't been shown yet,
284 // or is currently shown but deactivated for another WebContents). This is
285 // because this window is a singleton, and it's possible another active
286 // renderer may hang while this one is showing, and we don't want to reset
287 // the list of hung pages for a potentially unrelated renderer while this
289 hung_pages_table_model_
->InitForWebContents(contents
);
291 info_label_
->SetText(
292 l10n_util::GetPluralStringFUTF16(IDS_BROWSER_HANGMONITOR_RENDERER
,
293 hung_pages_table_model_
->RowCount()));
296 // Make Widget ask for the window title again.
297 GetWidget()->UpdateWindowTitle();
303 void HungRendererDialogView::EndForWebContents(WebContents
* contents
) {
305 if (hung_pages_table_model_
->RowCount() == 0 ||
306 hung_pages_table_model_
->GetRenderProcessHost() ==
307 contents
->GetRenderProcessHost()) {
308 GetWidget()->Close();
309 // Close is async, make sure we drop our references to the tab immediately
310 // (it may be going away).
311 hung_pages_table_model_
->InitForWebContents(NULL
);
315 ///////////////////////////////////////////////////////////////////////////////
316 // HungRendererDialogView, views::DialogDelegate implementation:
318 base::string16
HungRendererDialogView::GetWindowTitle() const {
320 return base::string16();
322 return l10n_util::GetPluralStringFUTF16(
323 IDS_BROWSER_HANGMONITOR_RENDERER_TITLE
,
324 hung_pages_table_model_
->RowCount());
327 void HungRendererDialogView::WindowClosing() {
328 // We are going to be deleted soon, so make sure our instance is destroyed.
332 int HungRendererDialogView::GetDialogButtons() const {
333 // We specifically don't want a CANCEL button here because that code path is
334 // also called when the window is closed by the user clicking the X button in
335 // the window's titlebar, and also if we call Window::Close. Rather, we want
336 // the OK button to wait for responsiveness (and close the dialog) and our
337 // additional button (which we create) to kill the process (which will result
338 // in the dialog being destroyed).
339 return ui::DIALOG_BUTTON_OK
;
342 base::string16
HungRendererDialogView::GetDialogButtonLabel(
343 ui::DialogButton button
) const {
344 if (button
== ui::DIALOG_BUTTON_OK
)
345 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_WAIT
);
346 return views::DialogDelegateView::GetDialogButtonLabel(button
);
349 views::View
* HungRendererDialogView::CreateExtraView() {
350 DCHECK(!kill_button_
);
351 kill_button_
= new views::LabelButton(this,
352 l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_END
));
353 kill_button_
->SetStyle(views::Button::STYLE_BUTTON
);
357 bool HungRendererDialogView::Accept(bool window_closing
) {
358 // Don't do anything if we're being called only because the dialog is being
359 // destroyed and we don't supply a Cancel function...
363 // Start waiting again for responsiveness.
364 if (hung_pages_table_model_
->GetRenderViewHost())
365 hung_pages_table_model_
->GetRenderViewHost()->RestartHangMonitorTimeout();
370 bool HungRendererDialogView::UseNewStyleForThisDialog() const {
372 // Use the old dialog style without Aero glass, otherwise the dialog will be
373 // visually constrained to browser window bounds. See http://crbug.com/323278
374 return ui::win::IsAeroGlassEnabled();
376 return views::DialogDelegateView::UseNewStyleForThisDialog();
380 ///////////////////////////////////////////////////////////////////////////////
381 // HungRendererDialogView, views::ButtonListener implementation:
383 void HungRendererDialogView::ButtonPressed(
384 views::Button
* sender
, const ui::Event
& event
) {
385 if (sender
== kill_button_
&&
386 hung_pages_table_model_
->GetRenderProcessHost()) {
387 KillRendererProcess(hung_pages_table_model_
->GetRenderProcessHost());
391 ///////////////////////////////////////////////////////////////////////////////
392 // HungRendererDialogView, HungPagesTableModel::Delegate overrides:
394 void HungRendererDialogView::TabDestroyed() {
395 GetWidget()->Close();
398 ///////////////////////////////////////////////////////////////////////////////
399 // HungRendererDialogView, views::View overrides:
401 void HungRendererDialogView::ViewHierarchyChanged(
402 const ViewHierarchyChangedDetails
& details
) {
403 views::DialogDelegateView::ViewHierarchyChanged(details
);
404 if (!initialized_
&& details
.is_add
&& details
.child
== this && GetWidget())
408 ///////////////////////////////////////////////////////////////////////////////
409 // HungRendererDialogView, private:
411 void HungRendererDialogView::Init() {
412 views::ImageView
* frozen_icon_view
= new views::ImageView
;
413 frozen_icon_view
->SetImage(frozen_icon_
);
415 info_label_
= new views::Label();
416 info_label_
->SetMultiLine(true);
417 info_label_
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
419 hung_pages_table_model_
.reset(new HungPagesTableModel(this));
420 std::vector
<ui::TableColumn
> columns
;
421 columns
.push_back(ui::TableColumn());
422 hung_pages_table_
= new views::TableView(
423 hung_pages_table_model_
.get(), columns
, views::ICON_AND_TEXT
, true);
424 hung_pages_table_
->SetGrouper(hung_pages_table_model_
.get());
426 using views::GridLayout
;
427 using views::ColumnSet
;
429 GridLayout
* layout
= GridLayout::CreatePanel(this);
430 SetLayoutManager(layout
);
432 const int double_column_set_id
= 0;
433 ColumnSet
* column_set
= layout
->AddColumnSet(double_column_set_id
);
434 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::LEADING
, 0,
435 GridLayout::FIXED
, frozen_icon_
->width(), 0);
436 column_set
->AddPaddingColumn(0, kCentralColumnPadding
);
437 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
438 GridLayout::USE_PREF
, 0, 0);
440 layout
->StartRow(0, double_column_set_id
);
441 layout
->AddView(frozen_icon_view
, 1, 3);
442 // Add the label with a preferred width of 1, this way it doesn't affect the
443 // overall preferred size of the dialog.
445 info_label_
, 1, 1, GridLayout::FILL
, GridLayout::LEADING
, 1, 0);
447 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
449 layout
->StartRow(0, double_column_set_id
);
450 layout
->SkipColumns(1);
451 layout
->AddView(hung_pages_table_
->CreateParentIfNecessary(), 1, 1,
452 views::GridLayout::FILL
,
453 views::GridLayout::FILL
, kTableViewWidth
, kTableViewHeight
);
459 void HungRendererDialogView::InitClass() {
460 static bool initialized
= false;
462 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
463 frozen_icon_
= rb
.GetImageSkiaNamed(IDR_FROZEN_TAB_ICON
);