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/favicon/favicon_tab_helper.h"
11 #include "chrome/browser/platform_util.h"
12 #include "chrome/browser/ui/browser_finder.h"
13 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
14 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
15 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
16 #include "chrome/common/chrome_constants.h"
17 #include "chrome/common/logging_chrome.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/constrained_window/constrained_window_views.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 FaviconTabHelper::FromWebContents(
115 tab_observers_
[row
]->web_contents())->GetFavicon().AsImageSkia();
118 void HungPagesTableModel::SetObserver(ui::TableModelObserver
* observer
) {
119 observer_
= observer
;
122 void HungPagesTableModel::GetGroupRange(int model_index
,
123 views::GroupRange
* range
) {
126 range
->length
= RowCount();
129 void HungPagesTableModel::TabDestroyed(WebContentsObserverImpl
* tab
) {
130 // Clean up tab_observers_ and notify our observer.
131 TabObservers::iterator i
= std::find(
132 tab_observers_
.begin(), tab_observers_
.end(), tab
);
133 DCHECK(i
!= tab_observers_
.end());
134 int index
= static_cast<int>(i
- tab_observers_
.begin());
135 tab_observers_
.erase(i
);
137 observer_
->OnItemsRemoved(index
, 1);
139 // Notify the delegate.
140 delegate_
->TabDestroyed();
141 // WARNING: we've likely been deleted.
144 HungPagesTableModel::WebContentsObserverImpl::WebContentsObserverImpl(
145 HungPagesTableModel
* model
, WebContents
* tab
)
146 : content::WebContentsObserver(tab
),
150 void HungPagesTableModel::WebContentsObserverImpl::RenderProcessGone(
151 base::TerminationStatus status
) {
152 model_
->TabDestroyed(this);
155 void HungPagesTableModel::WebContentsObserverImpl::WebContentsDestroyed() {
156 model_
->TabDestroyed(this);
159 ///////////////////////////////////////////////////////////////////////////////
160 // HungRendererDialogView
163 gfx::ImageSkia
* HungRendererDialogView::frozen_icon_
= NULL
;
165 // The dimensions of the hung pages list table view, in pixels.
166 static const int kTableViewWidth
= 300;
167 static const int kTableViewHeight
= 100;
169 // Padding space in pixels between frozen icon to the info label, hung pages
170 // list table view and the Kill pages button.
171 static const int kCentralColumnPadding
=
172 views::kUnrelatedControlLargeHorizontalSpacing
;
174 ///////////////////////////////////////////////////////////////////////////////
175 // HungRendererDialogView, public:
178 HungRendererDialogView
* HungRendererDialogView::Create(
179 gfx::NativeWindow context
) {
181 g_instance_
= new HungRendererDialogView
;
182 views::DialogDelegate::CreateDialogWidget(g_instance_
, context
, NULL
);
188 HungRendererDialogView
* HungRendererDialogView::GetInstance() {
193 void HungRendererDialogView::Show(WebContents
* contents
) {
194 if (logging::DialogsAreSuppressed())
197 gfx::NativeWindow window
=
198 platform_util::GetTopLevel(contents
->GetNativeView());
199 #if defined(USE_AURA)
200 // Don't show the dialog if there is no root window for the renderer, because
201 // it's invisible to the user (happens when the renderer is for prerendering
203 if (!window
->GetRootWindow())
206 HungRendererDialogView
* view
= HungRendererDialogView::Create(window
);
207 view
->ShowForWebContents(contents
);
211 void HungRendererDialogView::Hide(WebContents
* contents
) {
212 if (!logging::DialogsAreSuppressed() && HungRendererDialogView::GetInstance())
213 HungRendererDialogView::GetInstance()->EndForWebContents(contents
);
217 bool HungRendererDialogView::IsFrameActive(WebContents
* contents
) {
218 gfx::NativeWindow window
=
219 platform_util::GetTopLevel(contents
->GetNativeView());
220 return platform_util::IsWindowActive(window
);
224 void HungRendererDialogView::KillRendererProcess(
225 content::RenderProcessHost
* rph
) {
227 // Try to generate a crash report for the hung process.
228 CrashDumpAndTerminateHungChildProcess(rph
->GetHandle());
230 rph
->Shutdown(content::RESULT_CODE_HUNG
, false);
235 HungRendererDialogView::HungRendererDialogView()
236 : hung_pages_table_(NULL
),
238 initialized_(false) {
242 HungRendererDialogView::~HungRendererDialogView() {
243 hung_pages_table_
->SetModel(NULL
);
246 void HungRendererDialogView::ShowForWebContents(WebContents
* contents
) {
247 DCHECK(contents
&& GetWidget());
249 // Don't show the warning unless the foreground window is the frame, or this
250 // window (but still invisible). If the user has another window or
251 // application selected, activating ourselves is rude.
252 if (!IsFrameActive(contents
) &&
253 !platform_util::IsWindowActive(GetWidget()->GetNativeWindow()))
256 if (!GetWidget()->IsActive()) {
257 // Place the dialog over content's browser window, similar to modal dialogs.
258 Browser
* browser
= chrome::FindBrowserWithWebContents(contents
);
260 ChromeWebModalDialogManagerDelegate
* manager
= browser
;
261 constrained_window::UpdateWidgetModalDialogPosition(
262 GetWidget(), manager
->GetWebContentsModalDialogHost());
265 gfx::NativeWindow window
=
266 platform_util::GetTopLevel(contents
->GetNativeView());
267 views::Widget
* insert_after
=
268 views::Widget::GetWidgetForNativeWindow(window
);
270 GetWidget()->StackAboveWidget(insert_after
);
273 // Group the hung renderer dialog with the browsers with the same profile.
275 Profile::FromBrowserContext(contents
->GetBrowserContext());
276 ui::win::SetAppIdForWindow(
277 ShellIntegration::GetChromiumModelIdForProfile(profile
->GetPath()),
278 views::HWNDForWidget(GetWidget()));
281 // We only do this if the window isn't active (i.e. hasn't been shown yet,
282 // or is currently shown but deactivated for another WebContents). This is
283 // because this window is a singleton, and it's possible another active
284 // renderer may hang while this one is showing, and we don't want to reset
285 // the list of hung pages for a potentially unrelated renderer while this
287 hung_pages_table_model_
->InitForWebContents(contents
);
292 void HungRendererDialogView::EndForWebContents(WebContents
* contents
) {
294 if (hung_pages_table_model_
->RowCount() == 0 ||
295 hung_pages_table_model_
->GetRenderProcessHost() ==
296 contents
->GetRenderProcessHost()) {
297 GetWidget()->Close();
298 // Close is async, make sure we drop our references to the tab immediately
299 // (it may be going away).
300 hung_pages_table_model_
->InitForWebContents(NULL
);
304 ///////////////////////////////////////////////////////////////////////////////
305 // HungRendererDialogView, views::DialogDelegate implementation:
307 base::string16
HungRendererDialogView::GetWindowTitle() const {
308 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_TITLE
);
311 void HungRendererDialogView::WindowClosing() {
312 // We are going to be deleted soon, so make sure our instance is destroyed.
316 int HungRendererDialogView::GetDialogButtons() const {
317 // We specifically don't want a CANCEL button here because that code path is
318 // also called when the window is closed by the user clicking the X button in
319 // the window's titlebar, and also if we call Window::Close. Rather, we want
320 // the OK button to wait for responsiveness (and close the dialog) and our
321 // additional button (which we create) to kill the process (which will result
322 // in the dialog being destroyed).
323 return ui::DIALOG_BUTTON_OK
;
326 base::string16
HungRendererDialogView::GetDialogButtonLabel(
327 ui::DialogButton button
) const {
328 if (button
== ui::DIALOG_BUTTON_OK
)
329 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_WAIT
);
330 return views::DialogDelegateView::GetDialogButtonLabel(button
);
333 views::View
* HungRendererDialogView::CreateExtraView() {
334 DCHECK(!kill_button_
);
335 kill_button_
= new views::LabelButton(this,
336 l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_END
));
337 kill_button_
->SetStyle(views::Button::STYLE_BUTTON
);
341 bool HungRendererDialogView::Accept(bool window_closing
) {
342 // Don't do anything if we're being called only because the dialog is being
343 // destroyed and we don't supply a Cancel function...
347 // Start waiting again for responsiveness.
348 if (hung_pages_table_model_
->GetRenderViewHost())
349 hung_pages_table_model_
->GetRenderViewHost()->RestartHangMonitorTimeout();
354 bool HungRendererDialogView::UseNewStyleForThisDialog() const {
356 // Use the old dialog style without Aero glass, otherwise the dialog will be
357 // visually constrained to browser window bounds. See http://crbug.com/323278
358 return ui::win::IsAeroGlassEnabled();
360 return views::DialogDelegateView::UseNewStyleForThisDialog();
364 ///////////////////////////////////////////////////////////////////////////////
365 // HungRendererDialogView, views::ButtonListener implementation:
367 void HungRendererDialogView::ButtonPressed(
368 views::Button
* sender
, const ui::Event
& event
) {
369 if (sender
== kill_button_
&&
370 hung_pages_table_model_
->GetRenderProcessHost()) {
371 KillRendererProcess(hung_pages_table_model_
->GetRenderProcessHost());
375 ///////////////////////////////////////////////////////////////////////////////
376 // HungRendererDialogView, HungPagesTableModel::Delegate overrides:
378 void HungRendererDialogView::TabDestroyed() {
379 GetWidget()->Close();
382 ///////////////////////////////////////////////////////////////////////////////
383 // HungRendererDialogView, views::View overrides:
385 void HungRendererDialogView::ViewHierarchyChanged(
386 const ViewHierarchyChangedDetails
& details
) {
387 views::DialogDelegateView::ViewHierarchyChanged(details
);
388 if (!initialized_
&& details
.is_add
&& details
.child
== this && GetWidget())
392 ///////////////////////////////////////////////////////////////////////////////
393 // HungRendererDialogView, private:
395 void HungRendererDialogView::Init() {
396 views::ImageView
* frozen_icon_view
= new views::ImageView
;
397 frozen_icon_view
->SetImage(frozen_icon_
);
399 views::Label
* info_label
= new views::Label(
400 l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER
));
401 info_label
->SetMultiLine(true);
402 info_label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
404 hung_pages_table_model_
.reset(new HungPagesTableModel(this));
405 std::vector
<ui::TableColumn
> columns
;
406 columns
.push_back(ui::TableColumn());
407 hung_pages_table_
= new views::TableView(
408 hung_pages_table_model_
.get(), columns
, views::ICON_AND_TEXT
, true);
409 hung_pages_table_
->SetGrouper(hung_pages_table_model_
.get());
411 using views::GridLayout
;
412 using views::ColumnSet
;
414 GridLayout
* layout
= GridLayout::CreatePanel(this);
415 SetLayoutManager(layout
);
417 const int double_column_set_id
= 0;
418 ColumnSet
* column_set
= layout
->AddColumnSet(double_column_set_id
);
419 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::LEADING
, 0,
420 GridLayout::FIXED
, frozen_icon_
->width(), 0);
421 column_set
->AddPaddingColumn(0, kCentralColumnPadding
);
422 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
423 GridLayout::USE_PREF
, 0, 0);
425 layout
->StartRow(0, double_column_set_id
);
426 layout
->AddView(frozen_icon_view
, 1, 3);
427 // Add the label with a preferred width of 1, this way it doesn't effect the
428 // overall preferred size of the dialog.
430 info_label
, 1, 1, GridLayout::FILL
, GridLayout::LEADING
, 1, 0);
432 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
434 layout
->StartRow(0, double_column_set_id
);
435 layout
->SkipColumns(1);
436 layout
->AddView(hung_pages_table_
->CreateParentIfNecessary(), 1, 1,
437 views::GridLayout::FILL
,
438 views::GridLayout::FILL
, kTableViewWidth
, kTableViewHeight
);
444 void HungRendererDialogView::InitClass() {
445 static bool initialized
= false;
447 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
448 frozen_icon_
= rb
.GetImageSkiaNamed(IDR_FROZEN_TAB_ICON
);