This source file includes following definitions.
- PlatformShowCustomHungRendererDialog
- PlatformHideCustomHungRendererDialog
- delegate_
- GetRenderProcessHost
- GetRenderViewHost
- InitForWebContents
- RowCount
- GetText
- GetIcon
- SetObserver
- GetGroupRange
- TabDestroyed
- model_
- RenderProcessGone
- WebContentsDestroyed
- Create
- GetInstance
- IsFrameActive
- KillRendererProcess
- initialized_
- ShowForWebContents
- EndForWebContents
- GetWindowTitle
- WindowClosing
- GetDialogButtons
- GetDialogButtonLabel
- CreateExtraView
- Accept
- UseNewStyleForThisDialog
- ButtonPressed
- TabDestroyed
- ViewHierarchyChanged
- Init
- InitClass
- ShowHungRendererDialog
- HideHungRendererDialog
#include "chrome/browser/ui/views/hung_renderer_view.h"
#include "base/i18n/rtl.h"
#include "base/memory/scoped_vector.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/favicon/favicon_tab_helper.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
#include "chrome/browser/ui/views/constrained_window_views.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/logging_chrome.h"
#include "components/web_modal/web_contents_modal_dialog_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "content/public/common/result_codes.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/grid_layout.h"
#include "ui/views/layout/layout_constants.h"
#include "ui/views/widget/widget.h"
#include "ui/views/window/client_view.h"
#if defined(OS_WIN)
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/shell_integration.h"
#include "ui/base/win/shell.h"
#include "ui/views/win/hwnd_util.h"
#endif
#if defined(USE_AURA)
#include "ui/aura/window.h"
#endif
#if defined(OS_WIN)
#include "ui/base/win/shell.h"
#endif
using content::WebContents;
bool PlatformShowCustomHungRendererDialog(WebContents* contents);
bool PlatformHideCustomHungRendererDialog(WebContents* contents);
#if !defined(OS_WIN)
bool PlatformShowCustomHungRendererDialog(WebContents* contents) {
return false;
}
bool PlatformHideCustomHungRendererDialog(WebContents* contents) {
return false;
}
#endif
HungRendererDialogView* HungRendererDialogView::g_instance_ = NULL;
HungPagesTableModel::HungPagesTableModel(Delegate* delegate)
: observer_(NULL),
delegate_(delegate) {
}
HungPagesTableModel::~HungPagesTableModel() {
}
content::RenderProcessHost* HungPagesTableModel::GetRenderProcessHost() {
return tab_observers_.empty() ? NULL :
tab_observers_[0]->web_contents()->GetRenderProcessHost();
}
content::RenderViewHost* HungPagesTableModel::GetRenderViewHost() {
return tab_observers_.empty() ? NULL :
tab_observers_[0]->web_contents()->GetRenderViewHost();
}
void HungPagesTableModel::InitForWebContents(WebContents* hung_contents) {
tab_observers_.clear();
if (hung_contents) {
if (hung_contents) {
tab_observers_.push_back(new WebContentsObserverImpl(this,
hung_contents));
}
for (TabContentsIterator it; !it.done(); it.Next()) {
if (*it != hung_contents &&
it->GetRenderProcessHost() == hung_contents->GetRenderProcessHost())
tab_observers_.push_back(new WebContentsObserverImpl(this, *it));
}
}
if (observer_)
observer_->OnModelChanged();
}
int HungPagesTableModel::RowCount() {
return static_cast<int>(tab_observers_.size());
}
base::string16 HungPagesTableModel::GetText(int row, int column_id) {
DCHECK(row >= 0 && row < RowCount());
base::string16 title = tab_observers_[row]->web_contents()->GetTitle();
if (title.empty())
title = CoreTabHelper::GetDefaultTitle();
base::i18n::AdjustStringForLocaleDirection(&title);
return title;
}
gfx::ImageSkia HungPagesTableModel::GetIcon(int row) {
DCHECK(row >= 0 && row < RowCount());
return FaviconTabHelper::FromWebContents(
tab_observers_[row]->web_contents())->GetFavicon().AsImageSkia();
}
void HungPagesTableModel::SetObserver(ui::TableModelObserver* observer) {
observer_ = observer;
}
void HungPagesTableModel::GetGroupRange(int model_index,
views::GroupRange* range) {
DCHECK(range);
range->start = 0;
range->length = RowCount();
}
void HungPagesTableModel::TabDestroyed(WebContentsObserverImpl* tab) {
TabObservers::iterator i = std::find(
tab_observers_.begin(), tab_observers_.end(), tab);
DCHECK(i != tab_observers_.end());
int index = static_cast<int>(i - tab_observers_.begin());
tab_observers_.erase(i);
if (observer_)
observer_->OnItemsRemoved(index, 1);
delegate_->TabDestroyed();
}
HungPagesTableModel::WebContentsObserverImpl::WebContentsObserverImpl(
HungPagesTableModel* model, WebContents* tab)
: content::WebContentsObserver(tab),
model_(model) {
}
void HungPagesTableModel::WebContentsObserverImpl::RenderProcessGone(
base::TerminationStatus status) {
model_->TabDestroyed(this);
}
void HungPagesTableModel::WebContentsObserverImpl::WebContentsDestroyed(
WebContents* tab) {
model_->TabDestroyed(this);
}
gfx::ImageSkia* HungRendererDialogView::frozen_icon_ = NULL;
static const int kTableViewWidth = 300;
static const int kTableViewHeight = 100;
static const int kCentralColumnPadding =
views::kUnrelatedControlLargeHorizontalSpacing;
HungRendererDialogView* HungRendererDialogView::Create(
gfx::NativeView context) {
if (!g_instance_) {
g_instance_ = new HungRendererDialogView;
views::DialogDelegate::CreateDialogWidget(g_instance_, context, NULL);
}
return g_instance_;
}
HungRendererDialogView* HungRendererDialogView::GetInstance() {
return g_instance_;
}
bool HungRendererDialogView::IsFrameActive(WebContents* contents) {
gfx::NativeView frame_view =
platform_util::GetTopLevel(contents->GetView()->GetNativeView());
return platform_util::IsWindowActive(frame_view);
}
#if !defined(OS_WIN)
void HungRendererDialogView::KillRendererProcess(
base::ProcessHandle process_handle) {
base::KillProcess(process_handle, content::RESULT_CODE_HUNG, false);
}
#endif
HungRendererDialogView::HungRendererDialogView()
: hung_pages_table_(NULL),
kill_button_(NULL),
initialized_(false) {
InitClass();
}
HungRendererDialogView::~HungRendererDialogView() {
hung_pages_table_->SetModel(NULL);
}
void HungRendererDialogView::ShowForWebContents(WebContents* contents) {
DCHECK(contents && GetWidget());
if (!IsFrameActive(contents) &&
!platform_util::IsWindowActive(GetWidget()->GetNativeWindow()))
return;
if (!GetWidget()->IsActive()) {
Browser* browser = chrome::FindBrowserWithWebContents(contents);
if (browser) {
ChromeWebModalDialogManagerDelegate* manager = browser;
UpdateBrowserModalDialogPosition(
GetWidget(), manager->GetWebContentsModalDialogHost());
}
gfx::NativeView frame_view =
platform_util::GetTopLevel(contents->GetView()->GetNativeView());
views::Widget* insert_after =
views::Widget::GetWidgetForNativeView(frame_view);
if (insert_after)
GetWidget()->StackAboveWidget(insert_after);
#if defined(OS_WIN)
Profile* profile =
Profile::FromBrowserContext(contents->GetBrowserContext());
ui::win::SetAppIdForWindow(
ShellIntegration::GetChromiumModelIdForProfile(profile->GetPath()),
views::HWNDForWidget(GetWidget()));
#endif
hung_pages_table_model_->InitForWebContents(contents);
GetWidget()->Show();
}
}
void HungRendererDialogView::EndForWebContents(WebContents* contents) {
DCHECK(contents);
if (hung_pages_table_model_->RowCount() == 0 ||
hung_pages_table_model_->GetRenderProcessHost() ==
contents->GetRenderProcessHost()) {
GetWidget()->Close();
hung_pages_table_model_->InitForWebContents(NULL);
}
}
base::string16 HungRendererDialogView::GetWindowTitle() const {
return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_TITLE);
}
void HungRendererDialogView::WindowClosing() {
g_instance_ = NULL;
}
int HungRendererDialogView::GetDialogButtons() const {
return ui::DIALOG_BUTTON_OK;
}
base::string16 HungRendererDialogView::GetDialogButtonLabel(
ui::DialogButton button) const {
if (button == ui::DIALOG_BUTTON_OK)
return l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_WAIT);
return views::DialogDelegateView::GetDialogButtonLabel(button);
}
views::View* HungRendererDialogView::CreateExtraView() {
DCHECK(!kill_button_);
kill_button_ = new views::LabelButton(this,
l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER_END));
kill_button_->SetStyle(views::Button::STYLE_BUTTON);
return kill_button_;
}
bool HungRendererDialogView::Accept(bool window_closing) {
if (window_closing)
return true;
if (hung_pages_table_model_->GetRenderViewHost())
hung_pages_table_model_->GetRenderViewHost()->RestartHangMonitorTimeout();
return true;
}
bool HungRendererDialogView::UseNewStyleForThisDialog() const {
#if defined(OS_WIN)
return ui::win::IsAeroGlassEnabled();
#else
return views::DialogDelegateView::UseNewStyleForThisDialog();
#endif
}
void HungRendererDialogView::ButtonPressed(
views::Button* sender, const ui::Event& event) {
if (sender == kill_button_ &&
hung_pages_table_model_->GetRenderProcessHost()) {
base::ProcessHandle process_handle =
hung_pages_table_model_->GetRenderProcessHost()->GetHandle();
KillRendererProcess(process_handle);
}
}
void HungRendererDialogView::TabDestroyed() {
GetWidget()->Close();
}
void HungRendererDialogView::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) {
if (!initialized_ && details.is_add && details.child == this && GetWidget())
Init();
}
void HungRendererDialogView::Init() {
views::ImageView* frozen_icon_view = new views::ImageView;
frozen_icon_view->SetImage(frozen_icon_);
views::Label* info_label = new views::Label(
l10n_util::GetStringUTF16(IDS_BROWSER_HANGMONITOR_RENDERER));
info_label->SetMultiLine(true);
info_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
hung_pages_table_model_.reset(new HungPagesTableModel(this));
std::vector<ui::TableColumn> columns;
columns.push_back(ui::TableColumn());
hung_pages_table_ = new views::TableView(
hung_pages_table_model_.get(), columns, views::ICON_AND_TEXT, true);
hung_pages_table_->SetGrouper(hung_pages_table_model_.get());
using views::GridLayout;
using views::ColumnSet;
GridLayout* layout = GridLayout::CreatePanel(this);
SetLayoutManager(layout);
const int double_column_set_id = 0;
ColumnSet* column_set = layout->AddColumnSet(double_column_set_id);
column_set->AddColumn(GridLayout::LEADING, GridLayout::LEADING, 0,
GridLayout::FIXED, frozen_icon_->width(), 0);
column_set->AddPaddingColumn(0, kCentralColumnPadding);
column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
GridLayout::USE_PREF, 0, 0);
layout->StartRow(0, double_column_set_id);
layout->AddView(frozen_icon_view, 1, 3);
layout->AddView(
info_label, 1, 1, GridLayout::FILL, GridLayout::LEADING, 1, 0);
layout->AddPaddingRow(0, views::kRelatedControlVerticalSpacing);
layout->StartRow(0, double_column_set_id);
layout->SkipColumns(1);
layout->AddView(hung_pages_table_->CreateParentIfNecessary(), 1, 1,
views::GridLayout::FILL,
views::GridLayout::FILL, kTableViewWidth, kTableViewHeight);
initialized_ = true;
}
void HungRendererDialogView::InitClass() {
static bool initialized = false;
if (!initialized) {
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
frozen_icon_ = rb.GetImageSkiaNamed(IDR_FROZEN_TAB_ICON);
initialized = true;
}
}
namespace chrome {
void ShowHungRendererDialog(WebContents* contents) {
if (!logging::DialogsAreSuppressed() &&
!PlatformShowCustomHungRendererDialog(contents)) {
gfx::NativeView toplevel_view =
platform_util::GetTopLevel(contents->GetView()->GetNativeView());
#if defined(USE_AURA)
if (!toplevel_view->GetRootWindow())
return;
#endif
HungRendererDialogView* view = HungRendererDialogView::Create(
toplevel_view);
view->ShowForWebContents(contents);
}
}
void HideHungRendererDialog(WebContents* contents) {
if (!logging::DialogsAreSuppressed() &&
!PlatformHideCustomHungRendererDialog(contents) &&
HungRendererDialogView::GetInstance())
HungRendererDialogView::GetInstance()->EndForWebContents(contents);
}
}