This source file includes following definitions.
- dialog_
- RenderProcessGone
- WebContentsDestroyed
- contents_
- Init
- ShowForWebContents
- Hide
- EndForWebContents
- OnResponse
- ShowHungRendererDialog
- HideHungRendererDialog
#include "chrome/browser/ui/browser_dialogs.h"
#include <gtk/gtk.h>
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/favicon/favicon_tab_helper.h"
#include "chrome/browser/ui/gtk/gtk_util.h"
#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
#include "chrome/common/logging_chrome.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/common/result_codes.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/gtk/gtk_hig_constants.h"
#include "ui/base/gtk/gtk_signal.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/gtk_util.h"
#include "ui/gfx/image/image.h"
using content::WebContents;
namespace {
class HungRendererDialogGtk {
public:
HungRendererDialogGtk();
~HungRendererDialogGtk() {}
void ShowForWebContents(WebContents* hung_contents);
void Hide();
void EndForWebContents(WebContents* hung_contents);
private:
class WebContentsObserverImpl : public content::WebContentsObserver {
public:
WebContentsObserverImpl(HungRendererDialogGtk* dialog,
WebContents* contents)
: content::WebContentsObserver(contents),
dialog_(dialog) {
}
virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE {
dialog_->Hide();
}
virtual void WebContentsDestroyed(WebContents* tab) OVERRIDE {
dialog_->Hide();
}
private:
HungRendererDialogGtk* dialog_;
DISALLOW_COPY_AND_ASSIGN(WebContentsObserverImpl);
};
enum {
COL_FAVICON,
COL_TITLE,
COL_COUNT,
};
void Init();
CHROMEGTK_CALLBACK_1(HungRendererDialogGtk, void, OnResponse, int);
GtkDialog* dialog_;
GtkListStore* model_;
WebContents* contents_;
scoped_ptr<WebContentsObserverImpl> contents_observer_;
DISALLOW_COPY_AND_ASSIGN(HungRendererDialogGtk);
};
HungRendererDialogGtk* g_instance = NULL;
const int kKillPagesButtonResponse = 1;
HungRendererDialogGtk::HungRendererDialogGtk()
: dialog_(NULL), model_(NULL), contents_(NULL) {
Init();
}
void HungRendererDialogGtk::Init() {
dialog_ = GTK_DIALOG(gtk_dialog_new_with_buttons(
l10n_util::GetStringUTF8(IDS_BROWSER_HANGMONITOR_RENDERER_TITLE).c_str(),
NULL,
GTK_DIALOG_NO_SEPARATOR,
l10n_util::GetStringUTF8(IDS_BROWSER_HANGMONITOR_RENDERER_END).c_str(),
kKillPagesButtonResponse,
l10n_util::GetStringUTF8(IDS_BROWSER_HANGMONITOR_RENDERER_WAIT).c_str(),
GTK_RESPONSE_OK,
NULL));
gtk_dialog_set_default_response(dialog_, GTK_RESPONSE_OK);
g_signal_connect(dialog_, "delete-event",
G_CALLBACK(gtk_widget_hide_on_delete), NULL);
g_signal_connect(dialog_, "response", G_CALLBACK(OnResponseThunk), this);
GtkWidget* content_area = gtk_dialog_get_content_area(dialog_);
gtk_box_set_spacing(GTK_BOX(content_area), ui::kContentAreaSpacing);
GtkWidget* hbox = gtk_hbox_new(FALSE, 12);
gtk_box_pack_start(GTK_BOX(content_area), hbox, TRUE, TRUE, 0);
GtkWidget* icon_vbox = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), icon_vbox, FALSE, FALSE, 0);
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
GdkPixbuf* icon_pixbuf = rb.GetNativeImageNamed(
IDR_FROZEN_TAB_ICON).ToGdkPixbuf();
GtkWidget* icon = gtk_image_new_from_pixbuf(icon_pixbuf);
gtk_box_pack_start(GTK_BOX(icon_vbox), icon, FALSE, FALSE, 0);
GtkWidget* vbox = gtk_vbox_new(FALSE, ui::kControlSpacing);
gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
GtkWidget* text = gtk_label_new(
l10n_util::GetStringUTF8(IDS_BROWSER_HANGMONITOR_RENDERER).c_str());
gtk_label_set_line_wrap(GTK_LABEL(text), TRUE);
gtk_box_pack_start(GTK_BOX(vbox), text, FALSE, FALSE, 0);
GtkWidget* scroll_list = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_list),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll_list),
GTK_SHADOW_ETCHED_IN);
gtk_box_pack_start(GTK_BOX(vbox), scroll_list, TRUE, TRUE, 0);
model_ = gtk_list_store_new(COL_COUNT, GDK_TYPE_PIXBUF, G_TYPE_STRING);
GtkWidget* tree_view = gtk_tree_view_new_with_model(
GTK_TREE_MODEL(model_));
g_object_unref(model_);
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree_view), FALSE);
GtkTreeViewColumn* column = gtk_tree_view_column_new();
GtkCellRenderer* renderer = gtk_cell_renderer_pixbuf_new();
gtk_tree_view_column_pack_start(column, renderer, FALSE);
gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", COL_FAVICON);
renderer = gtk_cell_renderer_text_new();
gtk_tree_view_column_pack_start(column, renderer, TRUE);
gtk_tree_view_column_add_attribute(column, renderer, "text", COL_TITLE);
gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), column);
gtk_container_add(GTK_CONTAINER(scroll_list), tree_view);
}
void HungRendererDialogGtk::ShowForWebContents(WebContents* hung_contents) {
DCHECK(hung_contents && dialog_);
contents_ = hung_contents;
contents_observer_.reset(new WebContentsObserverImpl(this, contents_));
gtk_list_store_clear(model_);
GtkTreeIter tree_iter;
for (TabContentsIterator it; !it.done(); it.Next()) {
if (it->GetRenderProcessHost() == hung_contents->GetRenderProcessHost()) {
gtk_list_store_append(model_, &tree_iter);
std::string title = base::UTF16ToUTF8(it->GetTitle());
if (title.empty())
title = base::UTF16ToUTF8(CoreTabHelper::GetDefaultTitle());
FaviconTabHelper* favicon_tab_helper =
FaviconTabHelper::FromWebContents(*it);
SkBitmap favicon = favicon_tab_helper->GetFavicon().AsBitmap();
GdkPixbuf* pixbuf = NULL;
if (favicon.width() > 0)
pixbuf = gfx::GdkPixbufFromSkBitmap(favicon);
gtk_list_store_set(model_, &tree_iter,
COL_FAVICON, pixbuf,
COL_TITLE, title.c_str(),
-1);
if (pixbuf)
g_object_unref(pixbuf);
}
}
gtk_util::ShowDialog(GTK_WIDGET(dialog_));
}
void HungRendererDialogGtk::Hide() {
gtk_widget_hide(GTK_WIDGET(dialog_));
contents_observer_.reset();
contents_ = NULL;
}
void HungRendererDialogGtk::EndForWebContents(WebContents* contents) {
DCHECK(contents);
if (contents_ && contents_->GetRenderProcessHost() ==
contents->GetRenderProcessHost()) {
Hide();
}
}
void HungRendererDialogGtk::OnResponse(GtkWidget* dialog, int response_id) {
DCHECK(g_instance == this);
switch (response_id) {
case kKillPagesButtonResponse:
if (contents_ && contents_->GetRenderProcessHost()) {
base::KillProcess(contents_->GetRenderProcessHost()->GetHandle(),
content::RESULT_CODE_HUNG, false);
}
break;
case GTK_RESPONSE_OK:
case GTK_RESPONSE_DELETE_EVENT:
if (contents_ && contents_->GetRenderViewHost())
contents_->GetRenderViewHost()->RestartHangMonitorTimeout();
break;
default:
NOTREACHED();
}
gtk_widget_destroy(GTK_WIDGET(dialog_));
delete g_instance;
g_instance = NULL;
}
}
namespace chrome {
void ShowHungRendererDialog(WebContents* contents) {
if (!logging::DialogsAreSuppressed()) {
if (!g_instance)
g_instance = new HungRendererDialogGtk();
g_instance->ShowForWebContents(contents);
}
}
void HideHungRendererDialog(WebContents* contents) {
if (!logging::DialogsAreSuppressed() && g_instance)
g_instance->EndForWebContents(contents);
}
}