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 ~HungPagesTableModel() override;
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 int RowCount() override;
52 base::string16 GetText(int row, int column_id) override;
53 gfx::ImageSkia GetIcon(int row) override;
54 void SetObserver(ui::TableModelObserver* observer) override;
56 // Overridden from views::TableGrouper:
57 void GetGroupRange(int model_index, views::GroupRange* range) override;
60 // Used to track a single WebContents. If the WebContents is destroyed
61 // TabDestroyed() is invoked on the model.
62 class WebContentsObserverImpl : public content::WebContentsObserver {
64 WebContentsObserverImpl(HungPagesTableModel* model,
65 content::WebContents* tab);
67 FaviconTabHelper* favicon_tab_helper() {
68 return FaviconTabHelper::FromWebContents(web_contents());
71 // WebContentsObserver overrides:
72 void RenderProcessGone(base::TerminationStatus status) override;
73 void WebContentsDestroyed() override;
76 HungPagesTableModel* model_;
78 DISALLOW_COPY_AND_ASSIGN(WebContentsObserverImpl);
81 // Invoked when a WebContents is destroyed. Cleans up |tab_observers_| and
82 // notifies the observer and delegate.
83 void TabDestroyed(WebContentsObserverImpl* tab);
85 typedef ScopedVector<WebContentsObserverImpl> TabObservers;
86 TabObservers tab_observers_;
88 ui::TableModelObserver* observer_;
91 DISALLOW_COPY_AND_ASSIGN(HungPagesTableModel);
94 // This class displays a dialog which contains information about a hung
96 class HungRendererDialogView : public views::DialogDelegateView,
97 public views::ButtonListener,
98 public HungPagesTableModel::Delegate {
100 // Factory function for creating an instance of the HungRendererDialogView
101 // class. At any given point only one instance can be active.
102 static HungRendererDialogView* Create(gfx::NativeView context);
104 // Returns a pointer to the singleton instance if any.
105 static HungRendererDialogView* GetInstance();
107 // Platform specific function to kill the renderer process identified by the
109 static void KillRendererProcess(base::ProcessHandle process_handle);
111 // Returns true if the frame is in the foreground.
112 static bool IsFrameActive(content::WebContents* contents);
114 virtual void ShowForWebContents(content::WebContents* contents);
115 virtual void EndForWebContents(content::WebContents* contents);
117 // views::DialogDelegateView overrides:
118 base::string16 GetWindowTitle() const override;
119 void WindowClosing() override;
120 int GetDialogButtons() const override;
121 base::string16 GetDialogButtonLabel(ui::DialogButton button) const override;
122 views::View* CreateExtraView() override;
123 bool Accept(bool window_closing) override;
124 bool UseNewStyleForThisDialog() const override;
126 // views::ButtonListener overrides:
127 void ButtonPressed(views::Button* sender, const ui::Event& event) override;
129 // HungPagesTableModel::Delegate overrides:
130 void TabDestroyed() override;
133 HungRendererDialogView();
134 ~HungRendererDialogView() override;
136 // views::View overrides:
137 void ViewHierarchyChanged(
138 const ViewHierarchyChangedDetails& details) override;
140 static HungRendererDialogView* g_instance_;
143 // Initialize the controls in this dialog.
146 static void InitClass();
148 // Controls within the dialog box.
149 views::TableView* hung_pages_table_;
151 // The extra button inserted into the ClientView to kill the errant process.
152 views::LabelButton* kill_button_;
154 // The model that provides the contents of the table that shows a list of
155 // pages affected by the hang.
156 scoped_ptr<HungPagesTableModel> hung_pages_table_model_;
158 // Whether or not we've created controls for ourself.
161 // An amusing icon image.
162 static gfx::ImageSkia* frozen_icon_;
164 DISALLOW_COPY_AND_ASSIGN(HungRendererDialogView);
167 #endif // CHROME_BROWSER_UI_VIEWS_HUNG_RENDERER_VIEW_H_