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 #ifndef CHROME_BROWSER_UI_VIEWS_HUNG_RENDERER_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_HUNG_RENDERER_VIEW_H_
8 #include "base/memory/scoped_vector.h"
9 #include "chrome/browser/favicon/favicon_tab_helper.h"
10 #include "content/public/browser/web_contents_observer.h"
11 #include "ui/base/models/table_model.h"
12 #include "ui/views/controls/button/button.h"
13 #include "ui/views/controls/table/table_grouper.h"
14 #include "ui/views/controls/table/table_view.h"
15 #include "ui/views/window/dialog_delegate.h"
25 // Provides functionality to display information about a hung renderer.
26 class HungPagesTableModel : public ui::TableModel, public views::TableGrouper {
28 // The Delegate is notified any time a WebContents the model is listening to
32 virtual void TabDestroyed() = 0;
35 virtual ~Delegate() {}
38 explicit HungPagesTableModel(Delegate* delegate);
39 virtual ~HungPagesTableModel();
41 void InitForWebContents(content::WebContents* hung_contents);
43 // Returns the first RenderProcessHost, or NULL if there aren't any
45 content::RenderProcessHost* GetRenderProcessHost();
47 // Returns the first RenderViewHost, or NULL if there aren't any WebContents.
48 content::RenderViewHost* GetRenderViewHost();
50 // Overridden from ui::TableModel:
51 virtual int RowCount() OVERRIDE;
52 virtual base::string16 GetText(int row, int column_id) OVERRIDE;
53 virtual gfx::ImageSkia GetIcon(int row) OVERRIDE;
54 virtual void SetObserver(ui::TableModelObserver* observer) OVERRIDE;
56 // Overridden from views::TableGrouper:
57 virtual void GetGroupRange(int model_index,
58 views::GroupRange* range) OVERRIDE;
61 // Used to track a single WebContents. If the WebContents is destroyed
62 // TabDestroyed() is invoked on the model.
63 class WebContentsObserverImpl : public content::WebContentsObserver {
65 WebContentsObserverImpl(HungPagesTableModel* model,
66 content::WebContents* tab);
68 FaviconTabHelper* favicon_tab_helper() {
69 return FaviconTabHelper::FromWebContents(web_contents());
72 // WebContentsObserver overrides:
73 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
74 virtual void WebContentsDestroyed() OVERRIDE;
77 HungPagesTableModel* model_;
79 DISALLOW_COPY_AND_ASSIGN(WebContentsObserverImpl);
82 // Invoked when a WebContents is destroyed. Cleans up |tab_observers_| and
83 // notifies the observer and delegate.
84 void TabDestroyed(WebContentsObserverImpl* tab);
86 typedef ScopedVector<WebContentsObserverImpl> TabObservers;
87 TabObservers tab_observers_;
89 ui::TableModelObserver* observer_;
92 DISALLOW_COPY_AND_ASSIGN(HungPagesTableModel);
95 // This class displays a dialog which contains information about a hung
97 class HungRendererDialogView : public views::DialogDelegateView,
98 public views::ButtonListener,
99 public HungPagesTableModel::Delegate {
101 // Factory function for creating an instance of the HungRendererDialogView
102 // class. At any given point only one instance can be active.
103 static HungRendererDialogView* Create(gfx::NativeView context);
105 // Returns a pointer to the singleton instance if any.
106 static HungRendererDialogView* GetInstance();
108 // Platform specific function to kill the renderer process identified by the
110 static void KillRendererProcess(base::ProcessHandle process_handle);
112 // Returns true if the frame is in the foreground.
113 static bool IsFrameActive(content::WebContents* contents);
115 virtual void ShowForWebContents(content::WebContents* contents);
116 virtual void EndForWebContents(content::WebContents* contents);
118 // views::DialogDelegateView overrides:
119 virtual base::string16 GetWindowTitle() const OVERRIDE;
120 virtual void WindowClosing() OVERRIDE;
121 virtual int GetDialogButtons() const OVERRIDE;
122 virtual base::string16 GetDialogButtonLabel(
123 ui::DialogButton button) const OVERRIDE;
124 virtual views::View* CreateExtraView() OVERRIDE;
125 virtual bool Accept(bool window_closing) OVERRIDE;
126 virtual bool UseNewStyleForThisDialog() const OVERRIDE;
128 // views::ButtonListener overrides:
129 virtual void ButtonPressed(views::Button* sender,
130 const ui::Event& event) OVERRIDE;
132 // HungPagesTableModel::Delegate overrides:
133 virtual void TabDestroyed() OVERRIDE;
136 HungRendererDialogView();
137 virtual ~HungRendererDialogView();
139 // views::View overrides:
140 virtual void ViewHierarchyChanged(
141 const ViewHierarchyChangedDetails& details) OVERRIDE;
143 static HungRendererDialogView* g_instance_;
146 // Initialize the controls in this dialog.
149 static void InitClass();
151 // Controls within the dialog box.
152 views::TableView* hung_pages_table_;
154 // The extra button inserted into the ClientView to kill the errant process.
155 views::LabelButton* kill_button_;
157 // The model that provides the contents of the table that shows a list of
158 // pages affected by the hang.
159 scoped_ptr<HungPagesTableModel> hung_pages_table_model_;
161 // Whether or not we've created controls for ourself.
164 // An amusing icon image.
165 static gfx::ImageSkia* frozen_icon_;
167 DISALLOW_COPY_AND_ASSIGN(HungRendererDialogView);
170 #endif // CHROME_BROWSER_UI_VIEWS_HUNG_RENDERER_VIEW_H_