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