NaCl docs: add sanitizers to GSoC ideas
[chromium-blink-merge.git] / chrome / browser / ui / views / hung_renderer_view.cc
blob5d2503c9bd33a081035fbe228e8d2a0cb34ca187
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_finder.h"
13 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
14 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
15 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
16 #include "chrome/common/chrome_constants.h"
17 #include "chrome/common/logging_chrome.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/constrained_window/constrained_window_views.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/theme_resources.h"
26 #include "ui/aura/window.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/base/resource/resource_bundle.h"
29 #include "ui/gfx/canvas.h"
30 #include "ui/views/controls/button/label_button.h"
31 #include "ui/views/controls/image_view.h"
32 #include "ui/views/controls/label.h"
33 #include "ui/views/layout/grid_layout.h"
34 #include "ui/views/layout/layout_constants.h"
35 #include "ui/views/widget/widget.h"
36 #include "ui/views/window/client_view.h"
38 #if defined(OS_WIN)
39 #include "chrome/browser/hang_monitor/hang_crash_dump_win.h"
40 #include "chrome/browser/profiles/profile.h"
41 #include "chrome/browser/shell_integration.h"
42 #include "ui/base/win/shell.h"
43 #include "ui/views/win/hwnd_util.h"
44 #endif
46 using content::WebContents;
48 HungRendererDialogView* HungRendererDialogView::g_instance_ = NULL;
50 ///////////////////////////////////////////////////////////////////////////////
51 // HungPagesTableModel, public:
53 HungPagesTableModel::HungPagesTableModel(Delegate* delegate)
54 : observer_(NULL),
55 delegate_(delegate) {
58 HungPagesTableModel::~HungPagesTableModel() {
61 content::RenderProcessHost* HungPagesTableModel::GetRenderProcessHost() {
62 return tab_observers_.empty() ? NULL :
63 tab_observers_[0]->web_contents()->GetRenderProcessHost();
66 content::RenderViewHost* HungPagesTableModel::GetRenderViewHost() {
67 return tab_observers_.empty() ? NULL :
68 tab_observers_[0]->web_contents()->GetRenderViewHost();
71 void HungPagesTableModel::InitForWebContents(WebContents* hung_contents) {
72 tab_observers_.clear();
73 if (hung_contents) {
74 // Force hung_contents to be first.
75 if (hung_contents) {
76 tab_observers_.push_back(new WebContentsObserverImpl(this,
77 hung_contents));
79 for (TabContentsIterator it; !it.done(); it.Next()) {
80 if (*it != hung_contents &&
81 it->GetRenderProcessHost() == hung_contents->GetRenderProcessHost())
82 tab_observers_.push_back(new WebContentsObserverImpl(this, *it));
85 // The world is different.
86 if (observer_)
87 observer_->OnModelChanged();
90 ///////////////////////////////////////////////////////////////////////////////
91 // HungPagesTableModel, ui::TableModel implementation:
93 int HungPagesTableModel::RowCount() {
94 return static_cast<int>(tab_observers_.size());
97 base::string16 HungPagesTableModel::GetText(int row, int column_id) {
98 DCHECK(row >= 0 && row < RowCount());
99 base::string16 title = tab_observers_[row]->web_contents()->GetTitle();
100 if (title.empty())
101 title = CoreTabHelper::GetDefaultTitle();
102 // TODO(xji): Consider adding a special case if the title text is a URL,
103 // since those should always have LTR directionality. Please refer to
104 // http://crbug.com/6726 for more information.
105 base::i18n::AdjustStringForLocaleDirection(&title);
106 return title;
109 gfx::ImageSkia HungPagesTableModel::GetIcon(int row) {
110 DCHECK(row >= 0 && row < RowCount());
111 return FaviconTabHelper::FromWebContents(
112 tab_observers_[row]->web_contents())->GetFavicon().AsImageSkia();
115 void HungPagesTableModel::SetObserver(ui::TableModelObserver* observer) {
116 observer_ = observer;
119 void HungPagesTableModel::GetGroupRange(int model_index,
120 views::GroupRange* range) {
121 DCHECK(range);
122 range->start = 0;
123 range->length = RowCount();
126 void HungPagesTableModel::TabDestroyed(WebContentsObserverImpl* tab) {
127 // Clean up tab_observers_ and notify our observer.
128 TabObservers::iterator i = std::find(
129 tab_observers_.begin(), tab_observers_.end(), tab);
130 DCHECK(i != tab_observers_.end());
131 int index = static_cast<int>(i - tab_observers_.begin());
132 tab_observers_.erase(i);
133 if (observer_)
134 observer_->OnItemsRemoved(index, 1);
136 // Notify the delegate.
137 delegate_->TabDestroyed();
138 // WARNING: we've likely been deleted.
141 HungPagesTableModel::WebContentsObserverImpl::WebContentsObserverImpl(
142 HungPagesTableModel* model, WebContents* tab)
143 : content::WebContentsObserver(tab),
144 model_(model) {
147 void HungPagesTableModel::WebContentsObserverImpl::RenderProcessGone(
148 base::TerminationStatus status) {
149 model_->TabDestroyed(this);
152 void HungPagesTableModel::WebContentsObserverImpl::WebContentsDestroyed() {
153 model_->TabDestroyed(this);
156 ///////////////////////////////////////////////////////////////////////////////
157 // HungRendererDialogView
159 // static
160 gfx::ImageSkia* HungRendererDialogView::frozen_icon_ = NULL;
162 // The dimensions of the hung pages list table view, in pixels.
163 static const int kTableViewWidth = 300;
164 static const int kTableViewHeight = 100;
166 // Padding space in pixels between frozen icon to the info label, hung pages
167 // list table view and the Kill pages button.
168 static const int kCentralColumnPadding =
169 views::kUnrelatedControlLargeHorizontalSpacing;
171 ///////////////////////////////////////////////////////////////////////////////
172 // HungRendererDialogView, public:
174 // static
175 HungRendererDialogView* HungRendererDialogView::Create(
176 gfx::NativeWindow context) {
177 if (!g_instance_) {
178 g_instance_ = new HungRendererDialogView;
179 views::DialogDelegate::CreateDialogWidget(g_instance_, context, NULL);
181 return g_instance_;
184 // static
185 HungRendererDialogView* HungRendererDialogView::GetInstance() {
186 return g_instance_;
189 // static
190 void HungRendererDialogView::Show(WebContents* contents) {
191 if (logging::DialogsAreSuppressed())
192 return;
194 gfx::NativeWindow window =
195 platform_util::GetTopLevel(contents->GetNativeView());
196 #if defined(USE_AURA)
197 // Don't show the dialog if there is no root window for the renderer, because
198 // it's invisible to the user (happens when the renderer is for prerendering
199 // for example).
200 if (!window->GetRootWindow())
201 return;
202 #endif
203 HungRendererDialogView* view = HungRendererDialogView::Create(window);
204 view->ShowForWebContents(contents);
207 // static
208 void HungRendererDialogView::Hide(WebContents* contents) {
209 if (!logging::DialogsAreSuppressed() && HungRendererDialogView::GetInstance())
210 HungRendererDialogView::GetInstance()->EndForWebContents(contents);
213 // static
214 bool HungRendererDialogView::IsFrameActive(WebContents* contents) {
215 gfx::NativeWindow window =
216 platform_util::GetTopLevel(contents->GetNativeView());
217 return platform_util::IsWindowActive(window);
220 // static
221 void HungRendererDialogView::KillRendererProcess(
222 content::RenderProcessHost* rph) {
223 #if defined(OS_WIN)
224 // Try to generate a crash report for the hung process.
225 CrashDumpAndTerminateHungChildProcess(rph->GetHandle());
226 #else
227 rph->Shutdown(content::RESULT_CODE_HUNG, false);
228 #endif
232 HungRendererDialogView::HungRendererDialogView()
233 : hung_pages_table_(NULL),
234 kill_button_(NULL),
235 initialized_(false) {
236 InitClass();
239 HungRendererDialogView::~HungRendererDialogView() {
240 hung_pages_table_->SetModel(NULL);
243 void HungRendererDialogView::ShowForWebContents(WebContents* contents) {
244 DCHECK(contents && GetWidget());
246 // Don't show the warning unless the foreground window is the frame, or this
247 // window (but still invisible). If the user has another window or
248 // application selected, activating ourselves is rude.
249 if (!IsFrameActive(contents) &&
250 !platform_util::IsWindowActive(GetWidget()->GetNativeWindow()))
251 return;
253 if (!GetWidget()->IsActive()) {
254 // Place the dialog over content's browser window, similar to modal dialogs.
255 Browser* browser = chrome::FindBrowserWithWebContents(contents);
256 if (browser) {
257 ChromeWebModalDialogManagerDelegate* manager = browser;
258 constrained_window::UpdateWidgetModalDialogPosition(
259 GetWidget(), manager->GetWebContentsModalDialogHost());
262 gfx::NativeWindow window =
263 platform_util::GetTopLevel(contents->GetNativeView());
264 views::Widget* insert_after =
265 views::Widget::GetWidgetForNativeWindow(window);
266 if (insert_after)
267 GetWidget()->StackAboveWidget(insert_after);
269 #if defined(OS_WIN)
270 // Group the hung renderer dialog with the browsers with the same profile.
271 Profile* profile =
272 Profile::FromBrowserContext(contents->GetBrowserContext());
273 ui::win::SetAppIdForWindow(
274 ShellIntegration::GetChromiumModelIdForProfile(profile->GetPath()),
275 views::HWNDForWidget(GetWidget()));
276 #endif
278 // We only do this if the window isn't active (i.e. hasn't been shown yet,
279 // or is currently shown but deactivated for another WebContents). This is
280 // because this window is a singleton, and it's possible another active
281 // renderer may hang while this one is showing, and we don't want to reset
282 // the list of hung pages for a potentially unrelated renderer while this
283 // one is showing.
284 hung_pages_table_model_->InitForWebContents(contents);
285 GetWidget()->Show();
289 void HungRendererDialogView::EndForWebContents(WebContents* contents) {
290 DCHECK(contents);
291 if (hung_pages_table_model_->RowCount() == 0 ||
292 hung_pages_table_model_->GetRenderProcessHost() ==
293 contents->GetRenderProcessHost()) {
294 GetWidget()->Close();
295 // Close is async, make sure we drop our references to the tab immediately
296 // (it may be going away).
297 hung_pages_table_model_->InitForWebContents(NULL);
301 ///////////////////////////////////////////////////////////////////////////////
302 // HungRendererDialogView, views::DialogDelegate implementation:
304 base::string16 HungRendererDialogView::GetWindowTitle() const {
305 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_TITLE);
308 void HungRendererDialogView::WindowClosing() {
309 // We are going to be deleted soon, so make sure our instance is destroyed.
310 g_instance_ = NULL;
313 int HungRendererDialogView::GetDialogButtons() const {
314 // We specifically don't want a CANCEL button here because that code path is
315 // also called when the window is closed by the user clicking the X button in
316 // the window's titlebar, and also if we call Window::Close. Rather, we want
317 // the OK button to wait for responsiveness (and close the dialog) and our
318 // additional button (which we create) to kill the process (which will result
319 // in the dialog being destroyed).
320 return ui::DIALOG_BUTTON_OK;
323 base::string16 HungRendererDialogView::GetDialogButtonLabel(
324 ui::DialogButton button) const {
325 if (button == ui::DIALOG_BUTTON_OK)
326 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_WAIT);
327 return views::DialogDelegateView::GetDialogButtonLabel(button);
330 views::View* HungRendererDialogView::CreateExtraView() {
331 DCHECK(!kill_button_);
332 kill_button_ = new views::LabelButton(this,
333 l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_END));
334 kill_button_->SetStyle(views::Button::STYLE_BUTTON);
335 return kill_button_;
338 bool HungRendererDialogView::Accept(bool window_closing) {
339 // Don't do anything if we're being called only because the dialog is being
340 // destroyed and we don't supply a Cancel function...
341 if (window_closing)
342 return true;
344 // Start waiting again for responsiveness.
345 if (hung_pages_table_model_->GetRenderViewHost())
346 hung_pages_table_model_->GetRenderViewHost()->RestartHangMonitorTimeout();
347 return true;
351 bool HungRendererDialogView::UseNewStyleForThisDialog() const {
352 #if defined(OS_WIN)
353 // Use the old dialog style without Aero glass, otherwise the dialog will be
354 // visually constrained to browser window bounds. See http://crbug.com/323278
355 return ui::win::IsAeroGlassEnabled();
356 #else
357 return views::DialogDelegateView::UseNewStyleForThisDialog();
358 #endif
361 ///////////////////////////////////////////////////////////////////////////////
362 // HungRendererDialogView, views::ButtonListener implementation:
364 void HungRendererDialogView::ButtonPressed(
365 views::Button* sender, const ui::Event& event) {
366 if (sender == kill_button_ &&
367 hung_pages_table_model_->GetRenderProcessHost()) {
368 KillRendererProcess(hung_pages_table_model_->GetRenderProcessHost());
372 ///////////////////////////////////////////////////////////////////////////////
373 // HungRendererDialogView, HungPagesTableModel::Delegate overrides:
375 void HungRendererDialogView::TabDestroyed() {
376 GetWidget()->Close();
379 ///////////////////////////////////////////////////////////////////////////////
380 // HungRendererDialogView, views::View overrides:
382 void HungRendererDialogView::ViewHierarchyChanged(
383 const ViewHierarchyChangedDetails& details) {
384 views::DialogDelegateView::ViewHierarchyChanged(details);
385 if (!initialized_ && details.is_add && details.child == this && GetWidget())
386 Init();
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.
426 layout->AddView(
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);
437 initialized_ = true;
440 // static
441 void HungRendererDialogView::InitClass() {
442 static bool initialized = false;
443 if (!initialized) {
444 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
445 frozen_icon_ = rb.GetImageSkiaNamed(IDR_FROZEN_TAB_ICON);
446 initialized = true;