Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / views / hung_renderer_view.cc
blobc0ef3de5646d6fc7a7df3208aaed8d7e5ec827b6
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/platform_util.h"
11 #include "chrome/browser/ui/browser_finder.h"
12 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
13 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
14 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/logging_chrome.h"
17 #include "chrome/grit/generated_resources.h"
18 #include "components/constrained_window/constrained_window_views.h"
19 #include "components/favicon/content/content_favicon_driver.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/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/gfx/canvas.h"
29 #include "ui/views/controls/button/label_button.h"
30 #include "ui/views/controls/image_view.h"
31 #include "ui/views/controls/label.h"
32 #include "ui/views/layout/grid_layout.h"
33 #include "ui/views/layout/layout_constants.h"
34 #include "ui/views/widget/widget.h"
35 #include "ui/views/window/client_view.h"
37 #if defined(OS_WIN)
38 #include "chrome/browser/hang_monitor/hang_crash_dump_win.h"
39 #include "chrome/browser/profiles/profile.h"
40 #include "chrome/browser/shell_integration.h"
41 #include "ui/base/win/shell.h"
42 #include "ui/views/win/hwnd_util.h"
43 #endif
45 #if defined(USE_AURA)
46 #include "ui/aura/window.h"
47 #endif
49 using content::WebContents;
51 HungRendererDialogView* HungRendererDialogView::g_instance_ = NULL;
53 ///////////////////////////////////////////////////////////////////////////////
54 // HungPagesTableModel, public:
56 HungPagesTableModel::HungPagesTableModel(Delegate* delegate)
57 : observer_(NULL),
58 delegate_(delegate) {
61 HungPagesTableModel::~HungPagesTableModel() {
64 content::RenderProcessHost* HungPagesTableModel::GetRenderProcessHost() {
65 return tab_observers_.empty() ? NULL :
66 tab_observers_[0]->web_contents()->GetRenderProcessHost();
69 content::RenderViewHost* HungPagesTableModel::GetRenderViewHost() {
70 return tab_observers_.empty() ? NULL :
71 tab_observers_[0]->web_contents()->GetRenderViewHost();
74 void HungPagesTableModel::InitForWebContents(WebContents* hung_contents) {
75 tab_observers_.clear();
76 if (hung_contents) {
77 // Force hung_contents to be first.
78 if (hung_contents) {
79 tab_observers_.push_back(new WebContentsObserverImpl(this,
80 hung_contents));
82 for (TabContentsIterator it; !it.done(); it.Next()) {
83 if (*it != hung_contents &&
84 it->GetRenderProcessHost() == hung_contents->GetRenderProcessHost())
85 tab_observers_.push_back(new WebContentsObserverImpl(this, *it));
88 // The world is different.
89 if (observer_)
90 observer_->OnModelChanged();
93 ///////////////////////////////////////////////////////////////////////////////
94 // HungPagesTableModel, ui::TableModel implementation:
96 int HungPagesTableModel::RowCount() {
97 return static_cast<int>(tab_observers_.size());
100 base::string16 HungPagesTableModel::GetText(int row, int column_id) {
101 DCHECK(row >= 0 && row < RowCount());
102 base::string16 title = tab_observers_[row]->web_contents()->GetTitle();
103 if (title.empty())
104 title = CoreTabHelper::GetDefaultTitle();
105 // TODO(xji): Consider adding a special case if the title text is a URL,
106 // since those should always have LTR directionality. Please refer to
107 // http://crbug.com/6726 for more information.
108 base::i18n::AdjustStringForLocaleDirection(&title);
109 return title;
112 gfx::ImageSkia HungPagesTableModel::GetIcon(int row) {
113 DCHECK(row >= 0 && row < RowCount());
114 return favicon::ContentFaviconDriver::FromWebContents(
115 tab_observers_[row]->web_contents())
116 ->GetFavicon()
117 .AsImageSkia();
120 void HungPagesTableModel::SetObserver(ui::TableModelObserver* observer) {
121 observer_ = observer;
124 void HungPagesTableModel::GetGroupRange(int model_index,
125 views::GroupRange* range) {
126 DCHECK(range);
127 range->start = 0;
128 range->length = RowCount();
131 void 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);
141 // Notify the delegate.
142 delegate_->TabDestroyed();
143 // WARNING: we've likely been deleted.
146 HungPagesTableModel::WebContentsObserverImpl::WebContentsObserverImpl(
147 HungPagesTableModel* model, WebContents* tab)
148 : content::WebContentsObserver(tab),
149 model_(model) {
152 void HungPagesTableModel::WebContentsObserverImpl::RenderProcessGone(
153 base::TerminationStatus status) {
154 model_->TabDestroyed(this);
157 void HungPagesTableModel::WebContentsObserverImpl::WebContentsDestroyed() {
158 model_->TabDestroyed(this);
161 ///////////////////////////////////////////////////////////////////////////////
162 // HungRendererDialogView
164 // static
165 gfx::ImageSkia* HungRendererDialogView::frozen_icon_ = NULL;
167 // The dimensions of the hung pages list table view, in pixels.
168 static const int kTableViewWidth = 300;
169 static const int kTableViewHeight = 100;
171 // Padding space in pixels between frozen icon to the info label, hung pages
172 // list table view and the Kill pages button.
173 static const int kCentralColumnPadding =
174 views::kUnrelatedControlLargeHorizontalSpacing;
176 ///////////////////////////////////////////////////////////////////////////////
177 // HungRendererDialogView, public:
179 // static
180 HungRendererDialogView* HungRendererDialogView::Create(
181 gfx::NativeWindow context) {
182 if (!g_instance_) {
183 g_instance_ = new HungRendererDialogView;
184 views::DialogDelegate::CreateDialogWidget(g_instance_, context, NULL);
186 return g_instance_;
189 // static
190 HungRendererDialogView* HungRendererDialogView::GetInstance() {
191 return g_instance_;
194 // static
195 void HungRendererDialogView::Show(WebContents* contents) {
196 if (logging::DialogsAreSuppressed())
197 return;
199 gfx::NativeWindow window =
200 platform_util::GetTopLevel(contents->GetNativeView());
201 #if defined(USE_AURA)
202 // Don't show the dialog if there is no root window for the renderer, because
203 // it's invisible to the user (happens when the renderer is for prerendering
204 // for example).
205 if (!window->GetRootWindow())
206 return;
207 #endif
208 HungRendererDialogView* view = HungRendererDialogView::Create(window);
209 view->ShowForWebContents(contents);
212 // static
213 void HungRendererDialogView::Hide(WebContents* contents) {
214 if (!logging::DialogsAreSuppressed() && HungRendererDialogView::GetInstance())
215 HungRendererDialogView::GetInstance()->EndForWebContents(contents);
218 // static
219 bool HungRendererDialogView::IsFrameActive(WebContents* contents) {
220 gfx::NativeWindow window =
221 platform_util::GetTopLevel(contents->GetNativeView());
222 return platform_util::IsWindowActive(window);
225 // static
226 void HungRendererDialogView::KillRendererProcess(
227 content::RenderProcessHost* rph) {
228 #if defined(OS_WIN)
229 // Try to generate a crash report for the hung process.
230 CrashDumpAndTerminateHungChildProcess(rph->GetHandle());
231 #else
232 rph->Shutdown(content::RESULT_CODE_HUNG, false);
233 #endif
237 HungRendererDialogView::HungRendererDialogView()
238 : info_label_(nullptr),
239 hung_pages_table_(nullptr),
240 kill_button_(nullptr),
241 initialized_(false) {
242 InitClass();
245 HungRendererDialogView::~HungRendererDialogView() {
246 hung_pages_table_->SetModel(NULL);
249 void HungRendererDialogView::ShowForWebContents(WebContents* contents) {
250 DCHECK(contents && GetWidget());
252 // Don't show the warning unless the foreground window is the frame, or this
253 // window (but still invisible). If the user has another window or
254 // application selected, activating ourselves is rude.
255 if (!IsFrameActive(contents) &&
256 !platform_util::IsWindowActive(GetWidget()->GetNativeWindow()))
257 return;
259 if (!GetWidget()->IsActive()) {
260 // Place the dialog over content's browser window, similar to modal dialogs.
261 Browser* browser = chrome::FindBrowserWithWebContents(contents);
262 if (browser) {
263 ChromeWebModalDialogManagerDelegate* manager = browser;
264 constrained_window::UpdateWidgetModalDialogPosition(
265 GetWidget(), manager->GetWebContentsModalDialogHost());
268 gfx::NativeWindow window =
269 platform_util::GetTopLevel(contents->GetNativeView());
270 views::Widget* insert_after =
271 views::Widget::GetWidgetForNativeWindow(window);
272 if (insert_after)
273 GetWidget()->StackAboveWidget(insert_after);
275 #if defined(OS_WIN)
276 // Group the hung renderer dialog with the browsers with the same profile.
277 Profile* profile =
278 Profile::FromBrowserContext(contents->GetBrowserContext());
279 ui::win::SetAppIdForWindow(
280 ShellIntegration::GetChromiumModelIdForProfile(profile->GetPath()),
281 views::HWNDForWidget(GetWidget()));
282 #endif
284 // We only do this if the window isn't active (i.e. hasn't been shown yet,
285 // or is currently shown but deactivated for another WebContents). This is
286 // because this window is a singleton, and it's possible another active
287 // renderer may hang while this one is showing, and we don't want to reset
288 // the list of hung pages for a potentially unrelated renderer while this
289 // one is showing.
290 hung_pages_table_model_->InitForWebContents(contents);
292 info_label_->SetText(
293 l10n_util::GetPluralStringFUTF16(IDS_BROWSER_HANGMONITOR_RENDERER,
294 hung_pages_table_model_->RowCount()));
295 Layout();
297 // Make Widget ask for the window title again.
298 GetWidget()->UpdateWindowTitle();
300 GetWidget()->Show();
304 void HungRendererDialogView::EndForWebContents(WebContents* contents) {
305 DCHECK(contents);
306 if (hung_pages_table_model_->RowCount() == 0 ||
307 hung_pages_table_model_->GetRenderProcessHost() ==
308 contents->GetRenderProcessHost()) {
309 GetWidget()->Close();
310 // Close is async, make sure we drop our references to the tab immediately
311 // (it may be going away).
312 hung_pages_table_model_->InitForWebContents(NULL);
316 ///////////////////////////////////////////////////////////////////////////////
317 // HungRendererDialogView, views::DialogDelegate implementation:
319 base::string16 HungRendererDialogView::GetWindowTitle() const {
320 if (!initialized_)
321 return base::string16();
323 return l10n_util::GetPluralStringFUTF16(
324 IDS_BROWSER_HANGMONITOR_RENDERER_TITLE,
325 hung_pages_table_model_->RowCount());
328 void HungRendererDialogView::WindowClosing() {
329 // We are going to be deleted soon, so make sure our instance is destroyed.
330 g_instance_ = NULL;
333 int HungRendererDialogView::GetDialogButtons() const {
334 // We specifically don't want a CANCEL button here because that code path is
335 // also called when the window is closed by the user clicking the X button in
336 // the window's titlebar, and also if we call Window::Close. Rather, we want
337 // the OK button to wait for responsiveness (and close the dialog) and our
338 // additional button (which we create) to kill the process (which will result
339 // in the dialog being destroyed).
340 return ui::DIALOG_BUTTON_OK;
343 base::string16 HungRendererDialogView::GetDialogButtonLabel(
344 ui::DialogButton button) const {
345 if (button == ui::DIALOG_BUTTON_OK)
346 return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_WAIT);
347 return views::DialogDelegateView::GetDialogButtonLabel(button);
350 views::View* HungRendererDialogView::CreateExtraView() {
351 DCHECK(!kill_button_);
352 kill_button_ = new views::LabelButton(this,
353 l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_END));
354 kill_button_->SetStyle(views::Button::STYLE_BUTTON);
355 return kill_button_;
358 bool HungRendererDialogView::Accept(bool window_closing) {
359 // Don't do anything if we're being called only because the dialog is being
360 // destroyed and we don't supply a Cancel function...
361 if (window_closing)
362 return true;
364 // Start waiting again for responsiveness.
365 if (hung_pages_table_model_->GetRenderViewHost())
366 hung_pages_table_model_->GetRenderViewHost()->RestartHangMonitorTimeout();
367 return true;
371 bool HungRendererDialogView::UseNewStyleForThisDialog() const {
372 #if defined(OS_WIN)
373 // Use the old dialog style without Aero glass, otherwise the dialog will be
374 // visually constrained to browser window bounds. See http://crbug.com/323278
375 return ui::win::IsAeroGlassEnabled();
376 #else
377 return views::DialogDelegateView::UseNewStyleForThisDialog();
378 #endif
381 ///////////////////////////////////////////////////////////////////////////////
382 // HungRendererDialogView, views::ButtonListener implementation:
384 void HungRendererDialogView::ButtonPressed(
385 views::Button* sender, const ui::Event& event) {
386 if (sender == kill_button_ &&
387 hung_pages_table_model_->GetRenderProcessHost()) {
388 KillRendererProcess(hung_pages_table_model_->GetRenderProcessHost());
392 ///////////////////////////////////////////////////////////////////////////////
393 // HungRendererDialogView, HungPagesTableModel::Delegate overrides:
395 void HungRendererDialogView::TabDestroyed() {
396 GetWidget()->Close();
399 ///////////////////////////////////////////////////////////////////////////////
400 // HungRendererDialogView, views::View overrides:
402 void HungRendererDialogView::ViewHierarchyChanged(
403 const ViewHierarchyChangedDetails& details) {
404 views::DialogDelegateView::ViewHierarchyChanged(details);
405 if (!initialized_ && details.is_add && details.child == this && GetWidget())
406 Init();
409 ///////////////////////////////////////////////////////////////////////////////
410 // HungRendererDialogView, private:
412 void HungRendererDialogView::Init() {
413 views::ImageView* frozen_icon_view = new views::ImageView;
414 frozen_icon_view->SetImage(frozen_icon_);
416 info_label_ = new views::Label();
417 info_label_->SetMultiLine(true);
418 info_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT);
420 hung_pages_table_model_.reset(new HungPagesTableModel(this));
421 std::vector<ui::TableColumn> columns;
422 columns.push_back(ui::TableColumn());
423 hung_pages_table_ = new views::TableView(
424 hung_pages_table_model_.get(), columns, views::ICON_AND_TEXT, true);
425 hung_pages_table_->SetGrouper(hung_pages_table_model_.get());
427 using views::GridLayout;
428 using views::ColumnSet;
430 GridLayout* layout = GridLayout::CreatePanel(this);
431 SetLayoutManager(layout);
433 const int double_column_set_id = 0;
434 ColumnSet* column_set = layout->AddColumnSet(double_column_set_id);
435 column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 0,
436 GridLayout::FIXED, frozen_icon_->width(), 0);
437 column_set->AddPaddingColumn(0, kCentralColumnPadding);
438 column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
439 GridLayout::USE_PREF, 0, 0);
441 layout->StartRow(0, double_column_set_id);
442 layout->AddView(frozen_icon_view, 1, 3);
443 // Add the label with a preferred width of 1, this way it doesn't affect the
444 // overall preferred size of the dialog.
445 layout->AddView(
446 info_label_, 1, 1, GridLayout::FILL, GridLayout::LEADING, 1, 0);
448 layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
450 layout->StartRow(0, double_column_set_id);
451 layout->SkipColumns(1);
452 layout->AddView(hung_pages_table_->CreateParentIfNecessary(), 1, 1,
453 views::GridLayout::FILL,
454 views::GridLayout::FILL, kTableViewWidth, kTableViewHeight);
456 initialized_ = true;
459 // static
460 void HungRendererDialogView::InitClass() {
461 static bool initialized = false;
462 if (!initialized) {
463 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
464 frozen_icon_ = rb.GetImageSkiaNamed(IDR_FROZEN_TAB_ICON);
465 initialized = true;