37264b1c9c0fd7b439fc1d490dc646d359cdce2b
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / hung_renderer_view.h
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.
4
5 #ifndef CHROME_BROWSER_UI_VIEWS_HUNG_RENDERER_VIEW_H_
6 #define CHROME_BROWSER_UI_VIEWS_HUNG_RENDERER_VIEW_H_
7
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"
16
17 namespace content {
18 class WebContents;
19 }
20
21 namespace views {
22 class LabelButton;
23 }
24
25 // Provides functionality to display information about a hung renderer.
26 class HungPagesTableModel : public ui::TableModel, public views::TableGrouper {
27  public:
28   // The Delegate is notified any time a WebContents the model is listening to
29   // is destroyed.
30   class Delegate {
31    public:
32     virtual void TabDestroyed() = 0;
33
34    protected:
35     virtual ~Delegate() {}
36   };
37
38   explicit HungPagesTableModel(Delegate* delegate);
39   ~HungPagesTableModel() override;
40
41   void InitForWebContents(content::WebContents* hung_contents);
42
43   // Returns the first RenderProcessHost, or NULL if there aren't any
44   // WebContents.
45   content::RenderProcessHost* GetRenderProcessHost();
46
47   // Returns the first RenderViewHost, or NULL if there aren't any WebContents.
48   content::RenderViewHost* GetRenderViewHost();
49
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;
55
56   // Overridden from views::TableGrouper:
57   void GetGroupRange(int model_index, views::GroupRange* range) override;
58
59  private:
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 {
63    public:
64     WebContentsObserverImpl(HungPagesTableModel* model,
65                             content::WebContents* tab);
66
67     FaviconTabHelper* favicon_tab_helper() {
68       return FaviconTabHelper::FromWebContents(web_contents());
69     }
70
71     // WebContentsObserver overrides:
72     void RenderProcessGone(base::TerminationStatus status) override;
73     void WebContentsDestroyed() override;
74
75    private:
76     HungPagesTableModel* model_;
77
78     DISALLOW_COPY_AND_ASSIGN(WebContentsObserverImpl);
79   };
80
81   // Invoked when a WebContents is destroyed. Cleans up |tab_observers_| and
82   // notifies the observer and delegate.
83   void TabDestroyed(WebContentsObserverImpl* tab);
84
85   typedef ScopedVector<WebContentsObserverImpl> TabObservers;
86   TabObservers tab_observers_;
87
88   ui::TableModelObserver* observer_;
89   Delegate* delegate_;
90
91   DISALLOW_COPY_AND_ASSIGN(HungPagesTableModel);
92 };
93
94 // This class displays a dialog which contains information about a hung
95 // renderer process.
96 class HungRendererDialogView : public views::DialogDelegateView,
97                                public views::ButtonListener,
98                                public HungPagesTableModel::Delegate {
99  public:
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);
103
104   // Returns a pointer to the singleton instance if any.
105   static HungRendererDialogView* GetInstance();
106
107   // Platform specific function to kill the renderer process identified by the
108   // handle passed in.
109   static void KillRendererProcess(base::ProcessHandle process_handle);
110
111   // Returns true if the frame is in the foreground.
112   static bool IsFrameActive(content::WebContents* contents);
113
114   virtual void ShowForWebContents(content::WebContents* contents);
115   virtual void EndForWebContents(content::WebContents* contents);
116
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;
125
126   // views::ButtonListener overrides:
127   void ButtonPressed(views::Button* sender, const ui::Event& event) override;
128
129   // HungPagesTableModel::Delegate overrides:
130   void TabDestroyed() override;
131
132  protected:
133   HungRendererDialogView();
134   ~HungRendererDialogView() override;
135
136   // views::View overrides:
137   void ViewHierarchyChanged(
138       const ViewHierarchyChangedDetails& details) override;
139
140   static HungRendererDialogView* g_instance_;
141
142  private:
143   // Initialize the controls in this dialog.
144   void Init();
145
146   static void InitClass();
147
148   // Controls within the dialog box.
149   views::TableView* hung_pages_table_;
150
151   // The extra button inserted into the ClientView to kill the errant process.
152   views::LabelButton* kill_button_;
153
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_;
157
158   // Whether or not we've created controls for ourself.
159   bool initialized_;
160
161   // An amusing icon image.
162   static gfx::ImageSkia* frozen_icon_;
163
164   DISALLOW_COPY_AND_ASSIGN(HungRendererDialogView);
165 };
166
167 #endif  // CHROME_BROWSER_UI_VIEWS_HUNG_RENDERER_VIEW_H_