hung_renderer_view.cc revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
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. 4 5#include "chrome/browser/ui/views/hung_renderer_view.h" 6 7#if defined(OS_WIN) && !defined(USE_AURA) 8#include <windows.h> 9#endif 10 11#include "base/i18n/rtl.h" 12#include "base/memory/scoped_vector.h" 13#include "base/process_util.h" 14#include "base/utf_string_conversions.h" 15#include "chrome/browser/favicon/favicon_tab_helper.h" 16#include "chrome/browser/platform_util.h" 17#include "chrome/browser/ui/browser_dialogs.h" 18#include "chrome/browser/ui/tab_contents/core_tab_helper.h" 19#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" 20#include "chrome/common/chrome_constants.h" 21#include "chrome/common/logging_chrome.h" 22#include "content/public/browser/render_process_host.h" 23#include "content/public/browser/render_view_host.h" 24#include "content/public/browser/web_contents.h" 25#include "content/public/browser/web_contents_view.h" 26#include "content/public/common/result_codes.h" 27#include "grit/chromium_strings.h" 28#include "grit/generated_resources.h" 29#include "grit/theme_resources.h" 30#include "ui/base/l10n/l10n_util.h" 31#include "ui/base/resource/resource_bundle.h" 32#include "ui/gfx/canvas.h" 33#include "ui/views/controls/button/label_button.h" 34#include "ui/views/controls/image_view.h" 35#include "ui/views/controls/label.h" 36#include "ui/views/layout/grid_layout.h" 37#include "ui/views/layout/layout_constants.h" 38#include "ui/views/widget/widget.h" 39#include "ui/views/window/client_view.h" 40 41#if defined(USE_AURA) 42#include "ui/aura/window.h" 43#endif 44 45using content::WebContents; 46 47// These functions allow certain chrome platforms to override the default hung 48// renderer dialog. For e.g. Chrome on Windows 8 metro 49bool PlatformShowCustomHungRendererDialog(WebContents* contents); 50bool PlatformHideCustomHungRendererDialog(WebContents* contents); 51 52#if !defined(OS_WIN) 53bool PlatformShowCustomHungRendererDialog(WebContents* contents) { 54 return false; 55} 56 57bool PlatformHideCustomHungRendererDialog(WebContents* contents) { 58 return false; 59} 60#endif // OS_WIN 61 62HungRendererDialogView* HungRendererDialogView::g_instance_ = NULL; 63 64/////////////////////////////////////////////////////////////////////////////// 65// HungPagesTableModel, public: 66 67HungPagesTableModel::HungPagesTableModel(Delegate* delegate) 68 : observer_(NULL), 69 delegate_(delegate) { 70} 71 72HungPagesTableModel::~HungPagesTableModel() { 73} 74 75content::RenderProcessHost* HungPagesTableModel::GetRenderProcessHost() { 76 return tab_observers_.empty() ? NULL : 77 tab_observers_[0]->web_contents()->GetRenderProcessHost(); 78} 79 80content::RenderViewHost* HungPagesTableModel::GetRenderViewHost() { 81 return tab_observers_.empty() ? NULL : 82 tab_observers_[0]->web_contents()->GetRenderViewHost(); 83} 84 85void HungPagesTableModel::InitForWebContents(WebContents* hung_contents) { 86 tab_observers_.clear(); 87 if (hung_contents) { 88 // Force hung_contents to be first. 89 if (hung_contents) { 90 tab_observers_.push_back(new WebContentsObserverImpl(this, 91 hung_contents)); 92 } 93 for (TabContentsIterator it; !it.done(); it.Next()) { 94 if (*it != hung_contents && 95 it->GetRenderProcessHost() == hung_contents->GetRenderProcessHost()) 96 tab_observers_.push_back(new WebContentsObserverImpl(this, *it)); 97 } 98 } 99 // The world is different. 100 if (observer_) 101 observer_->OnModelChanged(); 102} 103 104/////////////////////////////////////////////////////////////////////////////// 105// HungPagesTableModel, ui::TableModel implementation: 106 107int HungPagesTableModel::RowCount() { 108 return static_cast<int>(tab_observers_.size()); 109} 110 111string16 HungPagesTableModel::GetText(int row, int column_id) { 112 DCHECK(row >= 0 && row < RowCount()); 113 string16 title = tab_observers_[row]->web_contents()->GetTitle(); 114 if (title.empty()) 115 title = CoreTabHelper::GetDefaultTitle(); 116 // TODO(xji): Consider adding a special case if the title text is a URL, 117 // since those should always have LTR directionality. Please refer to 118 // http://crbug.com/6726 for more information. 119 base::i18n::AdjustStringForLocaleDirection(&title); 120 return title; 121} 122 123gfx::ImageSkia HungPagesTableModel::GetIcon(int row) { 124 DCHECK(row >= 0 && row < RowCount()); 125 return FaviconTabHelper::FromWebContents( 126 tab_observers_[row]->web_contents())->GetFavicon().AsImageSkia(); 127} 128 129void HungPagesTableModel::SetObserver(ui::TableModelObserver* observer) { 130 observer_ = observer; 131} 132 133void HungPagesTableModel::GetGroupRange(int model_index, 134 views::GroupRange* range) { 135 DCHECK(range); 136 range->start = 0; 137 range->length = RowCount(); 138} 139 140void HungPagesTableModel::TabDestroyed(WebContentsObserverImpl* tab) { 141 // Clean up tab_observers_ and notify our observer. 142 TabObservers::iterator i = std::find( 143 tab_observers_.begin(), tab_observers_.end(), tab); 144 DCHECK(i != tab_observers_.end()); 145 int index = static_cast<int>(i - tab_observers_.begin()); 146 tab_observers_.erase(i); 147 if (observer_) 148 observer_->OnItemsRemoved(index, 1); 149 150 // Notify the delegate. 151 delegate_->TabDestroyed(); 152 // WARNING: we've likely been deleted. 153} 154 155HungPagesTableModel::WebContentsObserverImpl::WebContentsObserverImpl( 156 HungPagesTableModel* model, WebContents* tab) 157 : content::WebContentsObserver(tab), 158 model_(model) { 159} 160 161void HungPagesTableModel::WebContentsObserverImpl::RenderViewGone( 162 base::TerminationStatus status) { 163 model_->TabDestroyed(this); 164} 165 166void HungPagesTableModel::WebContentsObserverImpl::WebContentsDestroyed( 167 WebContents* tab) { 168 model_->TabDestroyed(this); 169} 170 171/////////////////////////////////////////////////////////////////////////////// 172// HungRendererDialogView 173 174// static 175gfx::ImageSkia* HungRendererDialogView::frozen_icon_ = NULL; 176 177// The distance in pixels from the top of the relevant contents to place the 178// warning window. 179static const int kOverlayContentsOffsetY = 50; 180 181// The dimensions of the hung pages list table view, in pixels. 182static const int kTableViewWidth = 300; 183static const int kTableViewHeight = 100; 184 185// Padding space in pixels between frozen icon to the info label, hung pages 186// list table view and the Kill pages button. 187static const int kCentralColumnPadding = 188 views::kUnrelatedControlLargeHorizontalSpacing; 189 190/////////////////////////////////////////////////////////////////////////////// 191// HungRendererDialogView, public: 192 193// static 194HungRendererDialogView* HungRendererDialogView::Create() { 195 if (!g_instance_) { 196 g_instance_ = new HungRendererDialogView; 197 views::Widget::CreateWindow(g_instance_); 198 } 199 return g_instance_; 200} 201 202// static 203HungRendererDialogView* HungRendererDialogView::GetInstance() { 204 return g_instance_; 205} 206 207// static 208bool HungRendererDialogView::IsFrameActive(WebContents* contents) { 209 gfx::NativeView frame_view = 210 platform_util::GetTopLevel(contents->GetView()->GetNativeView()); 211 return platform_util::IsWindowActive(frame_view); 212} 213 214#if !defined(OS_WIN) 215// static 216void HungRendererDialogView::KillRendererProcess( 217 base::ProcessHandle process_handle) { 218 base::KillProcess(process_handle, content::RESULT_CODE_HUNG, false); 219} 220#endif // OS_WIN 221 222 223HungRendererDialogView::HungRendererDialogView() 224 : hung_pages_table_(NULL), 225 kill_button_(NULL), 226 initialized_(false) { 227 InitClass(); 228} 229 230HungRendererDialogView::~HungRendererDialogView() { 231 hung_pages_table_->SetModel(NULL); 232} 233 234void HungRendererDialogView::ShowForWebContents(WebContents* contents) { 235 DCHECK(contents && GetWidget()); 236 237 // Don't show the warning unless the foreground window is the frame, or this 238 // window (but still invisible). If the user has another window or 239 // application selected, activating ourselves is rude. 240 if (!IsFrameActive(contents) && 241 !platform_util::IsWindowActive(GetWidget()->GetNativeWindow())) 242 return; 243 244 if (!GetWidget()->IsActive()) { 245 gfx::Rect bounds = GetDisplayBounds(contents); 246 247 gfx::NativeView frame_view = 248 platform_util::GetTopLevel(contents->GetView()->GetNativeView()); 249 250 views::Widget* insert_after = 251 views::Widget::GetWidgetForNativeView(frame_view); 252 GetWidget()->SetBoundsConstrained(bounds); 253 if (insert_after) 254 GetWidget()->StackAboveWidget(insert_after); 255 256 // We only do this if the window isn't active (i.e. hasn't been shown yet, 257 // or is currently shown but deactivated for another WebContents). This is 258 // because this window is a singleton, and it's possible another active 259 // renderer may hang while this one is showing, and we don't want to reset 260 // the list of hung pages for a potentially unrelated renderer while this 261 // one is showing. 262 hung_pages_table_model_->InitForWebContents(contents); 263 GetWidget()->Show(); 264 } 265} 266 267void HungRendererDialogView::EndForWebContents(WebContents* contents) { 268 DCHECK(contents); 269 if (hung_pages_table_model_->RowCount() == 0 || 270 hung_pages_table_model_->GetRenderProcessHost() == 271 contents->GetRenderProcessHost()) { 272 GetWidget()->Close(); 273 // Close is async, make sure we drop our references to the tab immediately 274 // (it may be going away). 275 hung_pages_table_model_->InitForWebContents(NULL); 276 } 277} 278 279/////////////////////////////////////////////////////////////////////////////// 280// HungRendererDialogView, views::DialogDelegate implementation: 281 282string16 HungRendererDialogView::GetWindowTitle() const { 283 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_TITLE); 284} 285 286void HungRendererDialogView::WindowClosing() { 287 // We are going to be deleted soon, so make sure our instance is destroyed. 288 g_instance_ = NULL; 289} 290 291int HungRendererDialogView::GetDialogButtons() const { 292 // We specifically don't want a CANCEL button here because that code path is 293 // also called when the window is closed by the user clicking the X button in 294 // the window's titlebar, and also if we call Window::Close. Rather, we want 295 // the OK button to wait for responsiveness (and close the dialog) and our 296 // additional button (which we create) to kill the process (which will result 297 // in the dialog being destroyed). 298 return ui::DIALOG_BUTTON_OK; 299} 300 301string16 HungRendererDialogView::GetDialogButtonLabel( 302 ui::DialogButton button) const { 303 if (button == ui::DIALOG_BUTTON_OK) 304 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_WAIT); 305 return views::DialogDelegateView::GetDialogButtonLabel(button); 306} 307 308views::View* HungRendererDialogView::CreateExtraView() { 309 DCHECK(!kill_button_); 310 kill_button_ = new views::LabelButton(this, 311 l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_END)); 312 kill_button_->SetStyle(views::Button::STYLE_NATIVE_TEXTBUTTON); 313 return kill_button_; 314} 315 316bool HungRendererDialogView::Accept(bool window_closing) { 317 // Don't do anything if we're being called only because the dialog is being 318 // destroyed and we don't supply a Cancel function... 319 if (window_closing) 320 return true; 321 322 // Start waiting again for responsiveness. 323 if (hung_pages_table_model_->GetRenderViewHost()) 324 hung_pages_table_model_->GetRenderViewHost()->RestartHangMonitorTimeout(); 325 return true; 326} 327 328/////////////////////////////////////////////////////////////////////////////// 329// HungRendererDialogView, views::ButtonListener implementation: 330 331void HungRendererDialogView::ButtonPressed( 332 views::Button* sender, const ui::Event& event) { 333 if (sender == kill_button_ && 334 hung_pages_table_model_->GetRenderProcessHost()) { 335 336 base::ProcessHandle process_handle = 337 hung_pages_table_model_->GetRenderProcessHost()->GetHandle(); 338 339 KillRendererProcess(process_handle); 340 } 341} 342 343/////////////////////////////////////////////////////////////////////////////// 344// HungRendererDialogView, HungPagesTableModel::Delegate overrides: 345 346void HungRendererDialogView::TabDestroyed() { 347 GetWidget()->Close(); 348} 349 350/////////////////////////////////////////////////////////////////////////////// 351// HungRendererDialogView, views::View overrides: 352 353void HungRendererDialogView::ViewHierarchyChanged( 354 const ViewHierarchyChangedDetails& details) { 355 if (!initialized_ && details.is_add && details.child == this && GetWidget()) 356 Init(); 357} 358 359/////////////////////////////////////////////////////////////////////////////// 360// HungRendererDialogView, private: 361 362void HungRendererDialogView::Init() { 363 views::ImageView* frozen_icon_view = new views::ImageView; 364 frozen_icon_view->SetImage(frozen_icon_); 365 366 views::Label* info_label = new views::Label( 367 l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER)); 368 info_label->SetMultiLine(true); 369 info_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); 370 371 hung_pages_table_model_.reset(new HungPagesTableModel(this)); 372 std::vector<ui::TableColumn> columns; 373 columns.push_back(ui::TableColumn()); 374 hung_pages_table_ = new views::TableView( 375 hung_pages_table_model_.get(), columns, views::ICON_AND_TEXT, true); 376 hung_pages_table_->SetGrouper(hung_pages_table_model_.get()); 377 378 using views::GridLayout; 379 using views::ColumnSet; 380 381 GridLayout* layout = GridLayout::CreatePanel(this); 382 SetLayoutManager(layout); 383 384 const int double_column_set_id = 0; 385 ColumnSet* column_set = layout->AddColumnSet(double_column_set_id); 386 column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 0, 387 GridLayout::FIXED, frozen_icon_->width(), 0); 388 column_set->AddPaddingColumn(0, kCentralColumnPadding); 389 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1, 390 GridLayout::USE_PREF, 0, 0); 391 392 layout->StartRow(0, double_column_set_id); 393 layout->AddView(frozen_icon_view, 1, 3); 394 // Add the label with a preferred width of 1, this way it doesn't effect the 395 // overall preferred size of the dialog. 396 layout->AddView( 397 info_label, 1, 1, GridLayout::FILL, GridLayout::LEADING, 1, 0); 398 399 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing); 400 401 layout->StartRow(0, double_column_set_id); 402 layout->SkipColumns(1); 403 layout->AddView(hung_pages_table_->CreateParentIfNecessary(), 1, 1, 404 views::GridLayout::FILL, 405 views::GridLayout::FILL, kTableViewWidth, kTableViewHeight); 406 407 initialized_ = true; 408} 409 410gfx::Rect HungRendererDialogView::GetDisplayBounds( 411 WebContents* contents) { 412#if defined(USE_AURA) 413 gfx::Rect contents_bounds( 414 contents->GetView()->GetNativeView()->GetBoundsInRootWindow()); 415#elif defined(OS_WIN) 416 HWND contents_hwnd = contents->GetView()->GetNativeView(); 417 RECT contents_bounds_rect; 418 GetWindowRect(contents_hwnd, &contents_bounds_rect); 419 gfx::Rect contents_bounds(contents_bounds_rect); 420#endif 421 gfx::Rect window_bounds = GetWidget()->GetWindowBoundsInScreen(); 422 423 int window_x = contents_bounds.x() + 424 (contents_bounds.width() - window_bounds.width()) / 2; 425 int window_y = contents_bounds.y() + kOverlayContentsOffsetY; 426 return gfx::Rect(window_x, window_y, window_bounds.width(), 427 window_bounds.height()); 428} 429 430// static 431void HungRendererDialogView::InitClass() { 432 static bool initialized = false; 433 if (!initialized) { 434 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 435 frozen_icon_ = rb.GetImageSkiaNamed(IDR_FROZEN_TAB_ICON); 436 initialized = true; 437 } 438} 439 440namespace chrome { 441 442void ShowHungRendererDialog(WebContents* contents) { 443 if (!logging::DialogsAreSuppressed() && 444 !PlatformShowCustomHungRendererDialog(contents)) { 445 HungRendererDialogView* view = HungRendererDialogView::Create(); 446 view->ShowForWebContents(contents); 447 } 448} 449 450void HideHungRendererDialog(WebContents* contents) { 451 if (!logging::DialogsAreSuppressed() && 452 !PlatformHideCustomHungRendererDialog(contents) && 453 HungRendererDialogView::GetInstance()) 454 HungRendererDialogView::GetInstance()->EndForWebContents(contents); 455} 456 457} // namespace chrome 458