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_dialogs.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
15 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
16 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
17 #include "chrome/browser/ui/views/constrained_window_views.h"
18 #include "chrome/common/chrome_constants.h"
19 #include "chrome/common/logging_chrome.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/browser/web_contents_view.h"
25 #include "content/public/common/result_codes.h"
26 #include "grit/chromium_strings.h"
27 #include "grit/generated_resources.h"
28 #include "grit/theme_resources.h"
29 #include "ui/base/l10n/l10n_util.h"
30 #include "ui/base/resource/resource_bundle.h"
31 #include "ui/gfx/canvas.h"
32 #include "ui/views/controls/button/label_button.h"
33 #include "ui/views/controls/image_view.h"
34 #include "ui/views/controls/label.h"
35 #include "ui/views/layout/grid_layout.h"
36 #include "ui/views/layout/layout_constants.h"
37 #include "ui/views/widget/widget.h"
38 #include "ui/views/window/client_view.h"
41 #include "chrome/browser/profiles/profile.h"
42 #include "chrome/browser/shell_integration.h"
43 #include "ui/base/win/shell.h"
44 #include "ui/views/win/hwnd_util.h"
48 #include "ui/aura/window.h"
52 #include "ui/base/win/shell.h"
55 using content::WebContents
;
57 // These functions allow certain chrome platforms to override the default hung
58 // renderer dialog. For e.g. Chrome on Windows 8 metro
59 bool PlatformShowCustomHungRendererDialog(WebContents
* contents
);
60 bool PlatformHideCustomHungRendererDialog(WebContents
* contents
);
63 bool PlatformShowCustomHungRendererDialog(WebContents
* contents
) {
67 bool PlatformHideCustomHungRendererDialog(WebContents
* contents
) {
72 HungRendererDialogView
* HungRendererDialogView::g_instance_
= NULL
;
74 ///////////////////////////////////////////////////////////////////////////////
75 // HungPagesTableModel, public:
77 HungPagesTableModel::HungPagesTableModel(Delegate
* delegate
)
82 HungPagesTableModel::~HungPagesTableModel() {
85 content::RenderProcessHost
* HungPagesTableModel::GetRenderProcessHost() {
86 return tab_observers_
.empty() ? NULL
:
87 tab_observers_
[0]->web_contents()->GetRenderProcessHost();
90 content::RenderViewHost
* HungPagesTableModel::GetRenderViewHost() {
91 return tab_observers_
.empty() ? NULL
:
92 tab_observers_
[0]->web_contents()->GetRenderViewHost();
95 void HungPagesTableModel::InitForWebContents(WebContents
* hung_contents
) {
96 tab_observers_
.clear();
98 // Force hung_contents to be first.
100 tab_observers_
.push_back(new WebContentsObserverImpl(this,
103 for (TabContentsIterator it
; !it
.done(); it
.Next()) {
104 if (*it
!= hung_contents
&&
105 it
->GetRenderProcessHost() == hung_contents
->GetRenderProcessHost())
106 tab_observers_
.push_back(new WebContentsObserverImpl(this, *it
));
109 // The world is different.
111 observer_
->OnModelChanged();
114 ///////////////////////////////////////////////////////////////////////////////
115 // HungPagesTableModel, ui::TableModel implementation:
117 int HungPagesTableModel::RowCount() {
118 return static_cast<int>(tab_observers_
.size());
121 base::string16
HungPagesTableModel::GetText(int row
, int column_id
) {
122 DCHECK(row
>= 0 && row
< RowCount());
123 base::string16 title
= tab_observers_
[row
]->web_contents()->GetTitle();
125 title
= CoreTabHelper::GetDefaultTitle();
126 // TODO(xji): Consider adding a special case if the title text is a URL,
127 // since those should always have LTR directionality. Please refer to
128 // http://crbug.com/6726 for more information.
129 base::i18n::AdjustStringForLocaleDirection(&title
);
133 gfx::ImageSkia
HungPagesTableModel::GetIcon(int row
) {
134 DCHECK(row
>= 0 && row
< RowCount());
135 return FaviconTabHelper::FromWebContents(
136 tab_observers_
[row
]->web_contents())->GetFavicon().AsImageSkia();
139 void HungPagesTableModel::SetObserver(ui::TableModelObserver
* observer
) {
140 observer_
= observer
;
143 void HungPagesTableModel::GetGroupRange(int model_index
,
144 views::GroupRange
* range
) {
147 range
->length
= RowCount();
150 void HungPagesTableModel::TabDestroyed(WebContentsObserverImpl
* tab
) {
151 // Clean up tab_observers_ and notify our observer.
152 TabObservers::iterator i
= std::find(
153 tab_observers_
.begin(), tab_observers_
.end(), tab
);
154 DCHECK(i
!= tab_observers_
.end());
155 int index
= static_cast<int>(i
- tab_observers_
.begin());
156 tab_observers_
.erase(i
);
158 observer_
->OnItemsRemoved(index
, 1);
160 // Notify the delegate.
161 delegate_
->TabDestroyed();
162 // WARNING: we've likely been deleted.
165 HungPagesTableModel::WebContentsObserverImpl::WebContentsObserverImpl(
166 HungPagesTableModel
* model
, WebContents
* tab
)
167 : content::WebContentsObserver(tab
),
171 void HungPagesTableModel::WebContentsObserverImpl::RenderProcessGone(
172 base::TerminationStatus status
) {
173 model_
->TabDestroyed(this);
176 void HungPagesTableModel::WebContentsObserverImpl::WebContentsDestroyed(
178 model_
->TabDestroyed(this);
181 ///////////////////////////////////////////////////////////////////////////////
182 // HungRendererDialogView
185 gfx::ImageSkia
* HungRendererDialogView::frozen_icon_
= NULL
;
187 // The dimensions of the hung pages list table view, in pixels.
188 static const int kTableViewWidth
= 300;
189 static const int kTableViewHeight
= 100;
191 // Padding space in pixels between frozen icon to the info label, hung pages
192 // list table view and the Kill pages button.
193 static const int kCentralColumnPadding
=
194 views::kUnrelatedControlLargeHorizontalSpacing
;
196 ///////////////////////////////////////////////////////////////////////////////
197 // HungRendererDialogView, public:
200 HungRendererDialogView
* HungRendererDialogView::Create(
201 gfx::NativeView context
) {
203 g_instance_
= new HungRendererDialogView
;
204 views::DialogDelegate::CreateDialogWidget(g_instance_
, context
, NULL
);
210 HungRendererDialogView
* HungRendererDialogView::GetInstance() {
215 bool HungRendererDialogView::IsFrameActive(WebContents
* contents
) {
216 gfx::NativeView frame_view
=
217 platform_util::GetTopLevel(contents
->GetView()->GetNativeView());
218 return platform_util::IsWindowActive(frame_view
);
223 void HungRendererDialogView::KillRendererProcess(
224 base::ProcessHandle process_handle
) {
225 base::KillProcess(process_handle
, content::RESULT_CODE_HUNG
, false);
230 HungRendererDialogView::HungRendererDialogView()
231 : hung_pages_table_(NULL
),
233 initialized_(false) {
237 HungRendererDialogView::~HungRendererDialogView() {
238 hung_pages_table_
->SetModel(NULL
);
241 void HungRendererDialogView::ShowForWebContents(WebContents
* contents
) {
242 DCHECK(contents
&& GetWidget());
244 // Don't show the warning unless the foreground window is the frame, or this
245 // window (but still invisible). If the user has another window or
246 // application selected, activating ourselves is rude.
247 if (!IsFrameActive(contents
) &&
248 !platform_util::IsWindowActive(GetWidget()->GetNativeWindow()))
251 if (!GetWidget()->IsActive()) {
252 // Place the dialog over content's browser window, similar to modal dialogs.
253 Browser
* browser
= chrome::FindBrowserWithWebContents(contents
);
255 ChromeWebModalDialogManagerDelegate
* manager
= browser
;
256 UpdateBrowserModalDialogPosition(
257 GetWidget(), manager
->GetWebContentsModalDialogHost());
260 gfx::NativeView frame_view
=
261 platform_util::GetTopLevel(contents
->GetView()->GetNativeView());
262 views::Widget
* insert_after
=
263 views::Widget::GetWidgetForNativeView(frame_view
);
265 GetWidget()->StackAboveWidget(insert_after
);
268 // Group the hung renderer dialog with the browsers with the same profile.
270 Profile::FromBrowserContext(contents
->GetBrowserContext());
271 ui::win::SetAppIdForWindow(
272 ShellIntegration::GetChromiumModelIdForProfile(profile
->GetPath()),
273 views::HWNDForWidget(GetWidget()));
276 // We only do this if the window isn't active (i.e. hasn't been shown yet,
277 // or is currently shown but deactivated for another WebContents). This is
278 // because this window is a singleton, and it's possible another active
279 // renderer may hang while this one is showing, and we don't want to reset
280 // the list of hung pages for a potentially unrelated renderer while this
282 hung_pages_table_model_
->InitForWebContents(contents
);
287 void HungRendererDialogView::EndForWebContents(WebContents
* contents
) {
289 if (hung_pages_table_model_
->RowCount() == 0 ||
290 hung_pages_table_model_
->GetRenderProcessHost() ==
291 contents
->GetRenderProcessHost()) {
292 GetWidget()->Close();
293 // Close is async, make sure we drop our references to the tab immediately
294 // (it may be going away).
295 hung_pages_table_model_
->InitForWebContents(NULL
);
299 ///////////////////////////////////////////////////////////////////////////////
300 // HungRendererDialogView, views::DialogDelegate implementation:
302 base::string16
HungRendererDialogView::GetWindowTitle() const {
303 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_TITLE
);
306 void HungRendererDialogView::WindowClosing() {
307 // We are going to be deleted soon, so make sure our instance is destroyed.
311 int HungRendererDialogView::GetDialogButtons() const {
312 // We specifically don't want a CANCEL button here because that code path is
313 // also called when the window is closed by the user clicking the X button in
314 // the window's titlebar, and also if we call Window::Close. Rather, we want
315 // the OK button to wait for responsiveness (and close the dialog) and our
316 // additional button (which we create) to kill the process (which will result
317 // in the dialog being destroyed).
318 return ui::DIALOG_BUTTON_OK
;
321 base::string16
HungRendererDialogView::GetDialogButtonLabel(
322 ui::DialogButton button
) const {
323 if (button
== ui::DIALOG_BUTTON_OK
)
324 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_WAIT
);
325 return views::DialogDelegateView::GetDialogButtonLabel(button
);
328 views::View
* HungRendererDialogView::CreateExtraView() {
329 DCHECK(!kill_button_
);
330 kill_button_
= new views::LabelButton(this,
331 l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_END
));
332 kill_button_
->SetStyle(views::Button::STYLE_BUTTON
);
336 bool HungRendererDialogView::Accept(bool window_closing
) {
337 // Don't do anything if we're being called only because the dialog is being
338 // destroyed and we don't supply a Cancel function...
342 // Start waiting again for responsiveness.
343 if (hung_pages_table_model_
->GetRenderViewHost())
344 hung_pages_table_model_
->GetRenderViewHost()->RestartHangMonitorTimeout();
349 bool HungRendererDialogView::UseNewStyleForThisDialog() const {
351 // Use the old dialog style without Aero glass, otherwise the dialog will be
352 // visually constrained to browser window bounds. See http://crbug.com/323278
353 return ui::win::IsAeroGlassEnabled();
355 return views::DialogDelegateView::UseNewStyleForThisDialog();
358 ///////////////////////////////////////////////////////////////////////////////
359 // HungRendererDialogView, views::ButtonListener implementation:
361 void HungRendererDialogView::ButtonPressed(
362 views::Button
* sender
, const ui::Event
& event
) {
363 if (sender
== kill_button_
&&
364 hung_pages_table_model_
->GetRenderProcessHost()) {
366 base::ProcessHandle process_handle
=
367 hung_pages_table_model_
->GetRenderProcessHost()->GetHandle();
369 KillRendererProcess(process_handle
);
373 ///////////////////////////////////////////////////////////////////////////////
374 // HungRendererDialogView, HungPagesTableModel::Delegate overrides:
376 void HungRendererDialogView::TabDestroyed() {
377 GetWidget()->Close();
380 ///////////////////////////////////////////////////////////////////////////////
381 // HungRendererDialogView, views::View overrides:
383 void HungRendererDialogView::ViewHierarchyChanged(
384 const ViewHierarchyChangedDetails
& details
) {
385 if (!initialized_
&& details
.is_add
&& details
.child
== this && GetWidget())
389 ///////////////////////////////////////////////////////////////////////////////
390 // HungRendererDialogView, private:
392 void HungRendererDialogView::Init() {
393 views::ImageView
* frozen_icon_view
= new views::ImageView
;
394 frozen_icon_view
->SetImage(frozen_icon_
);
396 views::Label
* info_label
= new views::Label(
397 l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER
));
398 info_label
->SetMultiLine(true);
399 info_label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
401 hung_pages_table_model_
.reset(new HungPagesTableModel(this));
402 std::vector
<ui::TableColumn
> columns
;
403 columns
.push_back(ui::TableColumn());
404 hung_pages_table_
= new views::TableView(
405 hung_pages_table_model_
.get(), columns
, views::ICON_AND_TEXT
, true);
406 hung_pages_table_
->SetGrouper(hung_pages_table_model_
.get());
408 using views::GridLayout
;
409 using views::ColumnSet
;
411 GridLayout
* layout
= GridLayout::CreatePanel(this);
412 SetLayoutManager(layout
);
414 const int double_column_set_id
= 0;
415 ColumnSet
* column_set
= layout
->AddColumnSet(double_column_set_id
);
416 column_set
->AddColumn(GridLayout::LEADING
, GridLayout::LEADING
, 0,
417 GridLayout::FIXED
, frozen_icon_
->width(), 0);
418 column_set
->AddPaddingColumn(0, kCentralColumnPadding
);
419 column_set
->AddColumn(GridLayout::FILL
, GridLayout::FILL
, 1,
420 GridLayout::USE_PREF
, 0, 0);
422 layout
->StartRow(0, double_column_set_id
);
423 layout
->AddView(frozen_icon_view
, 1, 3);
424 // Add the label with a preferred width of 1, this way it doesn't effect the
425 // overall preferred size of the dialog.
427 info_label
, 1, 1, GridLayout::FILL
, GridLayout::LEADING
, 1, 0);
429 layout
->AddPaddingRow(0, views::kRelatedControlVerticalSpacing
);
431 layout
->StartRow(0, double_column_set_id
);
432 layout
->SkipColumns(1);
433 layout
->AddView(hung_pages_table_
->CreateParentIfNecessary(), 1, 1,
434 views::GridLayout::FILL
,
435 views::GridLayout::FILL
, kTableViewWidth
, kTableViewHeight
);
441 void HungRendererDialogView::InitClass() {
442 static bool initialized
= false;
444 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
445 frozen_icon_
= rb
.GetImageSkiaNamed(IDR_FROZEN_TAB_ICON
);
452 void ShowHungRendererDialog(WebContents
* contents
) {
453 if (!logging::DialogsAreSuppressed() &&
454 !PlatformShowCustomHungRendererDialog(contents
)) {
455 gfx::NativeView toplevel_view
=
456 platform_util::GetTopLevel(contents
->GetView()->GetNativeView());
457 #if defined(USE_AURA)
458 // Don't show the dialog if there is no root window for the renderer,
459 // because it's invisible to the user (happens when the renderer is for
460 // prerendering for example).
461 if (!toplevel_view
->GetRootWindow())
464 HungRendererDialogView
* view
= HungRendererDialogView::Create(
466 view
->ShowForWebContents(contents
);
470 void HideHungRendererDialog(WebContents
* contents
) {
471 if (!logging::DialogsAreSuppressed() &&
472 !PlatformHideCustomHungRendererDialog(contents
) &&
473 HungRendererDialogView::GetInstance())
474 HungRendererDialogView::GetInstance()->EndForWebContents(contents
);
477 } // namespace chrome