Add a minor text member to ui::MenuModel.
[chromium-blink-merge.git] / chrome / browser / ui / views / hung_renderer_view.cc
blob7f86a717be4a568d04fcc5a03f60c31de4ae42b8
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 #if defined(OS_WIN) && !defined(USE_AURA)
8 #include <windows.h>
9 #endif
11 #include "base/i18n/rtl.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/favicon/favicon_tab_helper.h"
15 #include "chrome/browser/platform_util.h"
16 #include "chrome/browser/ui/browser_dialogs.h"
17 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
18 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "chrome/common/logging_chrome.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"
40 #if defined(USE_AURA)
41 #include "ui/aura/window.h"
42 #endif
44 using content::WebContents;
46 // These functions allow certain chrome platforms to override the default hung
47 // renderer dialog. For e.g. Chrome on Windows 8 metro
48 bool PlatformShowCustomHungRendererDialog(WebContents* contents);
49 bool PlatformHideCustomHungRendererDialog(WebContents* contents);
51 #if !defined(OS_WIN)
52 bool PlatformShowCustomHungRendererDialog(WebContents* contents) {
53 return false;
56 bool PlatformHideCustomHungRendererDialog(WebContents* contents) {
57 return false;
59 #endif // OS_WIN
61 HungRendererDialogView* HungRendererDialogView::g_instance_ = NULL;
63 ///////////////////////////////////////////////////////////////////////////////
64 // HungPagesTableModel, public:
66 HungPagesTableModel::HungPagesTableModel(Delegate* delegate)
67 : observer_(NULL),
68 delegate_(delegate) {
71 HungPagesTableModel::~HungPagesTableModel() {
74 content::RenderProcessHost* HungPagesTableModel::GetRenderProcessHost() {
75 return tab_observers_.empty() ? NULL :
76 tab_observers_[0]->web_contents()->GetRenderProcessHost();
79 content::RenderViewHost* HungPagesTableModel::GetRenderViewHost() {
80 return tab_observers_.empty() ? NULL :
81 tab_observers_[0]->web_contents()->GetRenderViewHost();
84 void HungPagesTableModel::InitForWebContents(WebContents* hung_contents) {
85 tab_observers_.clear();
86 if (hung_contents) {
87 // Force hung_contents to be first.
88 if (hung_contents) {
89 tab_observers_.push_back(new WebContentsObserverImpl(this,
90 hung_contents));
92 for (TabContentsIterator it; !it.done(); it.Next()) {
93 if (*it != hung_contents &&
94 it->GetRenderProcessHost() == hung_contents->GetRenderProcessHost())
95 tab_observers_.push_back(new WebContentsObserverImpl(this, *it));
98 // The world is different.
99 if (observer_)
100 observer_->OnModelChanged();
103 ///////////////////////////////////////////////////////////////////////////////
104 // HungPagesTableModel, ui::TableModel implementation:
106 int HungPagesTableModel::RowCount() {
107 return static_cast<int>(tab_observers_.size());
110 string16 HungPagesTableModel::GetText(int row, int column_id) {
111 DCHECK(row >= 0 && row < RowCount());
112 string16 title = tab_observers_[row]->web_contents()->GetTitle();
113 if (title.empty())
114 title = CoreTabHelper::GetDefaultTitle();
115 // TODO(xji): Consider adding a special case if the title text is a URL,
116 // since those should always have LTR directionality. Please refer to
117 // http://crbug.com/6726 for more information.
118 base::i18n::AdjustStringForLocaleDirection(&title);
119 return title;
122 gfx::ImageSkia HungPagesTableModel::GetIcon(int row) {
123 DCHECK(row >= 0 && row < RowCount());
124 return FaviconTabHelper::FromWebContents(
125 tab_observers_[row]->web_contents())->GetFavicon().AsImageSkia();
128 void HungPagesTableModel::SetObserver(ui::TableModelObserver* observer) {
129 observer_ = observer;
132 void HungPagesTableModel::GetGroupRange(int model_index,
133 views::GroupRange* range) {
134 DCHECK(range);
135 range->start = 0;
136 range->length = RowCount();
139 void HungPagesTableModel::TabDestroyed(WebContentsObserverImpl* tab) {
140 // Clean up tab_observers_ and notify our observer.
141 TabObservers::iterator i = std::find(
142 tab_observers_.begin(), tab_observers_.end(), tab);
143 DCHECK(i != tab_observers_.end());
144 int index = static_cast<int>(i - tab_observers_.begin());
145 tab_observers_.erase(i);
146 if (observer_)
147 observer_->OnItemsRemoved(index, 1);
149 // Notify the delegate.
150 delegate_->TabDestroyed();
151 // WARNING: we've likely been deleted.
154 HungPagesTableModel::WebContentsObserverImpl::WebContentsObserverImpl(
155 HungPagesTableModel* model, WebContents* tab)
156 : content::WebContentsObserver(tab),
157 model_(model) {
160 void HungPagesTableModel::WebContentsObserverImpl::RenderProcessGone(
161 base::TerminationStatus status) {
162 model_->TabDestroyed(this);
165 void HungPagesTableModel::WebContentsObserverImpl::WebContentsDestroyed(
166 WebContents* tab) {
167 model_->TabDestroyed(this);
170 ///////////////////////////////////////////////////////////////////////////////
171 // HungRendererDialogView
173 // static
174 gfx::ImageSkia* HungRendererDialogView::frozen_icon_ = NULL;
176 // The distance in pixels from the top of the relevant contents to place the
177 // warning window.
178 static const int kOverlayContentsOffsetY = 50;
180 // The dimensions of the hung pages list table view, in pixels.
181 static const int kTableViewWidth = 300;
182 static const int kTableViewHeight = 100;
184 // Padding space in pixels between frozen icon to the info label, hung pages
185 // list table view and the Kill pages button.
186 static const int kCentralColumnPadding =
187 views::kUnrelatedControlLargeHorizontalSpacing;
189 ///////////////////////////////////////////////////////////////////////////////
190 // HungRendererDialogView, public:
192 // static
193 HungRendererDialogView* HungRendererDialogView::Create(
194 gfx::NativeView context) {
195 if (!g_instance_) {
196 g_instance_ = new HungRendererDialogView;
197 views::DialogDelegate::CreateDialogWidget(g_instance_, context, NULL);
199 return g_instance_;
202 // static
203 HungRendererDialogView* HungRendererDialogView::GetInstance() {
204 return g_instance_;
207 // static
208 bool HungRendererDialogView::IsFrameActive(WebContents* contents) {
209 gfx::NativeView frame_view =
210 platform_util::GetTopLevel(contents->GetView()->GetNativeView());
211 return platform_util::IsWindowActive(frame_view);
214 #if !defined(OS_WIN)
215 // static
216 void HungRendererDialogView::KillRendererProcess(
217 base::ProcessHandle process_handle) {
218 base::KillProcess(process_handle, content::RESULT_CODE_HUNG, false);
220 #endif // OS_WIN
223 HungRendererDialogView::HungRendererDialogView()
224 : hung_pages_table_(NULL),
225 kill_button_(NULL),
226 initialized_(false) {
227 InitClass();
230 HungRendererDialogView::~HungRendererDialogView() {
231 hung_pages_table_->SetModel(NULL);
234 void HungRendererDialogView::ShowForWebContents(WebContents* contents) {
235 DCHECK(contents && GetWidget());
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;
244 if (!GetWidget()->IsActive()) {
245 gfx::Rect bounds = GetDisplayBounds(contents);
247 gfx::NativeView frame_view =
248 platform_util::GetTopLevel(contents->GetView()->GetNativeView());
250 views::Widget* insert_after =
251 views::Widget::GetWidgetForNativeView(frame_view);
252 GetWidget()->SetBoundsConstrained(bounds);
253 if (insert_after)
254 GetWidget()->StackAboveWidget(insert_after);
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();
267 void 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);
279 ///////////////////////////////////////////////////////////////////////////////
280 // HungRendererDialogView, views::DialogDelegate implementation:
282 string16 HungRendererDialogView::GetWindowTitle() const {
283 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_TITLE);
286 void HungRendererDialogView::WindowClosing() {
287 // We are going to be deleted soon, so make sure our instance is destroyed.
288 g_instance_ = NULL;
291 int 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;
301 string16 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);
308 views::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_;
316 bool 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;
322 // Start waiting again for responsiveness.
323 if (hung_pages_table_model_->GetRenderViewHost())
324 hung_pages_table_model_->GetRenderViewHost()->RestartHangMonitorTimeout();
325 return true;
328 ///////////////////////////////////////////////////////////////////////////////
329 // HungRendererDialogView, views::ButtonListener implementation:
331 void HungRendererDialogView::ButtonPressed(
332 views::Button* sender, const ui::Event& event) {
333 if (sender == kill_button_ &&
334 hung_pages_table_model_->GetRenderProcessHost()) {
336 base::ProcessHandle process_handle =
337 hung_pages_table_model_->GetRenderProcessHost()->GetHandle();
339 KillRendererProcess(process_handle);
343 ///////////////////////////////////////////////////////////////////////////////
344 // HungRendererDialogView, HungPagesTableModel::Delegate overrides:
346 void HungRendererDialogView::TabDestroyed() {
347 GetWidget()->Close();
350 ///////////////////////////////////////////////////////////////////////////////
351 // HungRendererDialogView, views::View overrides:
353 void HungRendererDialogView::ViewHierarchyChanged(
354 const ViewHierarchyChangedDetails& details) {
355 if (!initialized_ && details.is_add && details.child == this && GetWidget())
356 Init();
359 ///////////////////////////////////////////////////////////////////////////////
360 // HungRendererDialogView, private:
362 void HungRendererDialogView::Init() {
363 views::ImageView* frozen_icon_view = new views::ImageView;
364 frozen_icon_view->SetImage(frozen_icon_);
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);
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());
378 using views::GridLayout;
379 using views::ColumnSet;
381 GridLayout* layout = GridLayout::CreatePanel(this);
382 SetLayoutManager(layout);
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);
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);
399 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
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);
407 initialized_ = true;
410 gfx::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();
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());
430 // static
431 void 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;
440 namespace chrome {
442 void ShowHungRendererDialog(WebContents* contents) {
443 if (!logging::DialogsAreSuppressed() &&
444 !PlatformShowCustomHungRendererDialog(contents)) {
445 HungRendererDialogView* view = HungRendererDialogView::Create(
446 platform_util::GetTopLevel(contents->GetView()->GetNativeView()));
447 view->ShowForWebContents(contents);
451 void HideHungRendererDialog(WebContents* contents) {
452 if (!logging::DialogsAreSuppressed() &&
453 !PlatformHideCustomHungRendererDialog(contents) &&
454 HungRendererDialogView::GetInstance())
455 HungRendererDialogView::GetInstance()->EndForWebContents(contents);
458 } // namespace chrome