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