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/memory/scoped_vector.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/platform_util.h"
11 #include "chrome/browser/ui/browser_finder.h"
12 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
13 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
14 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/logging_chrome.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "components/constrained_window/constrained_window_views.h"
19 #include "components/favicon/content/content_favicon_driver.h"
20 #include "components/web_modal/web_contents_modal_dialog_host.h"
21 #include "content/public/browser/render_process_host.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/common/result_codes.h"
25 #include "grit/theme_resources.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/gfx/canvas.h"
29 #include "ui/views/controls/button/label_button.h"
30 #include "ui/views/controls/image_view.h"
31 #include "ui/views/controls/label.h"
32 #include "ui/views/layout/grid_layout.h"
33 #include "ui/views/layout/layout_constants.h"
34 #include "ui/views/widget/widget.h"
35 #include "ui/views/window/client_view.h"
38 #include "chrome/browser/hang_monitor/hang_crash_dump_win.h"
39 #include "chrome/browser/profiles/profile.h"
40 #include "chrome/browser/shell_integration.h"
41 #include "ui/base/win/shell.h"
42 #include "ui/views/win/hwnd_util.h"
46 #include "ui/aura/window.h"
49 using content::WebContents
;
51 HungRendererDialogView
* HungRendererDialogView::g_instance_
= NULL
;
53 ///////////////////////////////////////////////////////////////////////////////
54 // HungPagesTableModel, public:
56 HungPagesTableModel::HungPagesTableModel(Delegate
* delegate
)
61 HungPagesTableModel::~HungPagesTableModel() {
64 content::RenderProcessHost
* HungPagesTableModel::GetRenderProcessHost() {
65 return tab_observers_
.empty() ? NULL
:
66 tab_observers_
[0]->web_contents()->GetRenderProcessHost();
69 content::RenderViewHost
* HungPagesTableModel::GetRenderViewHost() {
70 return tab_observers_
.empty() ? NULL
:
71 tab_observers_
[0]->web_contents()->GetRenderViewHost();
74 void HungPagesTableModel::InitForWebContents(WebContents
* hung_contents
) {
75 tab_observers_
.clear();
77 // Force hung_contents to be first.
79 tab_observers_
.push_back(new WebContentsObserverImpl(this,
82 for (TabContentsIterator it
; !it
.done(); it
.Next()) {
83 if (*it
!= hung_contents
&&
84 it
->GetRenderProcessHost() == hung_contents
->GetRenderProcessHost())
85 tab_observers_
.push_back(new WebContentsObserverImpl(this, *it
));
88 // The world is different.
90 observer_
->OnModelChanged();
93 ///////////////////////////////////////////////////////////////////////////////
94 // HungPagesTableModel, ui::TableModel implementation:
96 int HungPagesTableModel::RowCount() {
97 return static_cast<int>(tab_observers_
.size());
100 base::string16
HungPagesTableModel::GetText(int row
, int column_id
) {
101 DCHECK(row
>= 0 && row
< RowCount());
102 base::string16 title
= tab_observers_
[row
]->web_contents()->GetTitle();
104 title
= CoreTabHelper::GetDefaultTitle();
105 // TODO(xji): Consider adding a special case if the title text is a URL,
106 // since those should always have LTR directionality. Please refer to
107 // http://crbug.com/6726 for more information.
108 base::i18n::AdjustStringForLocaleDirection(&title
);
112 gfx::ImageSkia
HungPagesTableModel::GetIcon(int row
) {
113 DCHECK(row
>= 0 && row
< RowCount());
114 return favicon::ContentFaviconDriver::FromWebContents(
115 tab_observers_
[row
]->web_contents())
120 void HungPagesTableModel::SetObserver(ui::TableModelObserver
* observer
) {
121 observer_
= observer
;
124 void HungPagesTableModel::GetGroupRange(int model_index
,
125 views::GroupRange
* range
) {
128 range
->length
= RowCount();
131 void HungPagesTableModel::TabDestroyed(WebContentsObserverImpl
* tab
) {
132 // Clean up tab_observers_ and notify our observer.
133 TabObservers::iterator i
= std::find(
134 tab_observers_
.begin(), tab_observers_
.end(), tab
);
135 DCHECK(i
!= tab_observers_
.end());
136 int index
= static_cast<int>(i
- tab_observers_
.begin());
137 tab_observers_
.erase(i
);
139 observer_
->OnItemsRemoved(index
, 1);
141 // Notify the delegate.
142 delegate_
->TabDestroyed();
143 // WARNING: we've likely been deleted.
146 HungPagesTableModel::WebContentsObserverImpl::WebContentsObserverImpl(
147 HungPagesTableModel
* model
, WebContents
* tab
)
148 : content::WebContentsObserver(tab
),
152 void HungPagesTableModel::WebContentsObserverImpl::RenderProcessGone(
153 base::TerminationStatus status
) {
154 model_
->TabDestroyed(this);
157 void HungPagesTableModel::WebContentsObserverImpl::WebContentsDestroyed() {
158 model_
->TabDestroyed(this);
161 ///////////////////////////////////////////////////////////////////////////////
162 // HungRendererDialogView
165 gfx::ImageSkia
* HungRendererDialogView::frozen_icon_
= NULL
;
167 // The dimensions of the hung pages list table view, in pixels.
168 static const int kTableViewWidth
= 300;
169 static const int kTableViewHeight
= 100;
171 // Padding space in pixels between frozen icon to the info label, hung pages
172 // list table view and the Kill pages button.
173 static const int kCentralColumnPadding
=
174 views::kUnrelatedControlLargeHorizontalSpacing
;
176 ///////////////////////////////////////////////////////////////////////////////
177 // HungRendererDialogView, public:
180 HungRendererDialogView
* HungRendererDialogView::Create(
181 gfx::NativeWindow context
) {
183 g_instance_
= new HungRendererDialogView
;
184 views::DialogDelegate::CreateDialogWidget(g_instance_
, context
, NULL
);
190 HungRendererDialogView
* HungRendererDialogView::GetInstance() {
195 void HungRendererDialogView::Show(WebContents
* contents
) {
196 if (logging::DialogsAreSuppressed())
199 gfx::NativeWindow window
=
200 platform_util::GetTopLevel(contents
->GetNativeView());
201 #if defined(USE_AURA)
202 // Don't show the dialog if there is no root window for the renderer, because
203 // it's invisible to the user (happens when the renderer is for prerendering
205 if (!window
->GetRootWindow())
208 HungRendererDialogView
* view
= HungRendererDialogView::Create(window
);
209 view
->ShowForWebContents(contents
);
213 void HungRendererDialogView::Hide(WebContents
* contents
) {
214 if (!logging::DialogsAreSuppressed() && HungRendererDialogView::GetInstance())
215 HungRendererDialogView::GetInstance()->EndForWebContents(contents
);
219 bool HungRendererDialogView::IsFrameActive(WebContents
* contents
) {
220 gfx::NativeWindow window
=
221 platform_util::GetTopLevel(contents
->GetNativeView());
222 return platform_util::IsWindowActive(window
);
226 void HungRendererDialogView::KillRendererProcess(
227 content::RenderProcessHost
* rph
) {
229 // Try to generate a crash report for the hung process.
230 CrashDumpAndTerminateHungChildProcess(rph
->GetHandle());
232 rph
->Shutdown(content::RESULT_CODE_HUNG
, false);
237 HungRendererDialogView::HungRendererDialogView()
238 : hung_pages_table_(NULL
),
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
);
294 void HungRendererDialogView::EndForWebContents(WebContents
* contents
) {
296 if (hung_pages_table_model_
->RowCount() == 0 ||
297 hung_pages_table_model_
->GetRenderProcessHost() ==
298 contents
->GetRenderProcessHost()) {
299 GetWidget()->Close();
300 // Close is async, make sure we drop our references to the tab immediately
301 // (it may be going away).
302 hung_pages_table_model_
->InitForWebContents(NULL
);
306 ///////////////////////////////////////////////////////////////////////////////
307 // HungRendererDialogView, views::DialogDelegate implementation:
309 base::string16
HungRendererDialogView::GetWindowTitle() const {
310 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_TITLE
);
313 void HungRendererDialogView::WindowClosing() {
314 // We are going to be deleted soon, so make sure our instance is destroyed.
318 int HungRendererDialogView::GetDialogButtons() const {
319 // We specifically don't want a CANCEL button here because that code path is
320 // also called when the window is closed by the user clicking the X button in
321 // the window's titlebar, and also if we call Window::Close. Rather, we want
322 // the OK button to wait for responsiveness (and close the dialog) and our
323 // additional button (which we create) to kill the process (which will result
324 // in the dialog being destroyed).
325 return ui::DIALOG_BUTTON_OK
;
328 base::string16
HungRendererDialogView::GetDialogButtonLabel(
329 ui::DialogButton button
) const {
330 if (button
== ui::DIALOG_BUTTON_OK
)
331 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_WAIT
);
332 return views::DialogDelegateView::GetDialogButtonLabel(button
);
335 views::View
* HungRendererDialogView::CreateExtraView() {
336 DCHECK(!kill_button_
);
337 kill_button_
= new views::LabelButton(this,
338 l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_END
));
339 kill_button_
->SetStyle(views::Button::STYLE_BUTTON
);
343 bool HungRendererDialogView::Accept(bool window_closing
) {
344 // Don't do anything if we're being called only because the dialog is being
345 // destroyed and we don't supply a Cancel function...
349 // Start waiting again for responsiveness.
350 if (hung_pages_table_model_
->GetRenderViewHost())
351 hung_pages_table_model_
->GetRenderViewHost()->RestartHangMonitorTimeout();
356 bool HungRendererDialogView::UseNewStyleForThisDialog() const {
358 // Use the old dialog style without Aero glass, otherwise the dialog will be
359 // visually constrained to browser window bounds. See http://crbug.com/323278
360 return ui::win::IsAeroGlassEnabled();
362 return views::DialogDelegateView::UseNewStyleForThisDialog();
366 ///////////////////////////////////////////////////////////////////////////////
367 // HungRendererDialogView, views::ButtonListener implementation:
369 void HungRendererDialogView::ButtonPressed(
370 views::Button
* sender
, const ui::Event
& event
) {
371 if (sender
== kill_button_
&&
372 hung_pages_table_model_
->GetRenderProcessHost()) {
373 KillRendererProcess(hung_pages_table_model_
->GetRenderProcessHost());
377 ///////////////////////////////////////////////////////////////////////////////
378 // HungRendererDialogView, HungPagesTableModel::Delegate overrides:
380 void HungRendererDialogView::TabDestroyed() {
381 GetWidget()->Close();
384 ///////////////////////////////////////////////////////////////////////////////
385 // HungRendererDialogView, views::View overrides:
387 void HungRendererDialogView::ViewHierarchyChanged(
388 const ViewHierarchyChangedDetails
& details
) {
389 views::DialogDelegateView::ViewHierarchyChanged(details
);
390 if (!initialized_
&& details
.is_add
&& details
.child
== this && GetWidget())
394 ///////////////////////////////////////////////////////////////////////////////
395 // HungRendererDialogView, private:
397 void HungRendererDialogView::Init() {
398 views::ImageView
* frozen_icon_view
= new views::ImageView
;
399 frozen_icon_view
->SetImage(frozen_icon_
);
401 views::Label
* info_label
= new views::Label(
402 l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER
));
403 info_label
->SetMultiLine(true);
404 info_label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
406 hung_pages_table_model_
.reset(new HungPagesTableModel(this));
407 std::vector
<ui::TableColumn
> columns
;
408 columns
.push_back(ui::TableColumn());
409 hung_pages_table_
= new views::TableView(
410 hung_pages_table_model_
.get(), columns
, views::ICON_AND_TEXT
, true);
411 hung_pages_table_
->SetGrouper(hung_pages_table_model_
.get());
413 using views::GridLayout
;
414 using views::ColumnSet
;
416 GridLayout
* layout
= GridLayout::CreatePanel(this);
417 SetLayoutManager(layout
);
419 const int double_column_set_id
= 0;
420 ColumnSet
* column_set
= layout
->AddColumnSet(double_column_set_id
);
421 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::LEADING
, 0,
422 GridLayout::FIXED
, frozen_icon_
->width(), 0);
423 column_set
->AddPaddingColumn(0, kCentralColumnPadding
);
424 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
425 GridLayout::USE_PREF
, 0, 0);
427 layout
->StartRow(0, double_column_set_id
);
428 layout
->AddView(frozen_icon_view
, 1, 3);
429 // Add the label with a preferred width of 1, this way it doesn't effect the
430 // overall preferred size of the dialog.
432 info_label
, 1, 1, GridLayout::FILL
, GridLayout::LEADING
, 1, 0);
434 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
436 layout
->StartRow(0, double_column_set_id
);
437 layout
->SkipColumns(1);
438 layout
->AddView(hung_pages_table_
->CreateParentIfNecessary(), 1, 1,
439 views::GridLayout::FILL
,
440 views::GridLayout::FILL
, kTableViewWidth
, kTableViewHeight
);
446 void HungRendererDialogView::InitClass() {
447 static bool initialized
= false;
449 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
450 frozen_icon_
= rb
.GetImageSkiaNamed(IDR_FROZEN_TAB_ICON
);