OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ | 5 #ifndef CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ |
6 #define CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ | 6 #define CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 // TODO(jam): remove this file when all files have been converted. |
10 #include "base/string16.h" | 10 #include "content/browser/tab_contents/infobar_delegate.h" |
11 #include "chrome/browser/tab_contents/navigation_controller.h" | |
12 #include "webkit/glue/window_open_disposition.h" | |
13 | |
14 class ConfirmInfoBarDelegate; | |
15 class CrashedExtensionInfoBarDelegate; | |
16 class ExtensionInfoBarDelegate; | |
17 class InfoBar; | |
18 class LinkInfoBarDelegate; | |
19 class PluginInstallerInfoBarDelegate; | |
20 class SkBitmap; | |
21 class ThemeInstalledInfoBarDelegate; | |
22 class TranslateInfoBarDelegate; | |
23 | |
24 // An interface implemented by objects wishing to control an InfoBar. | |
25 // Implementing this interface is not sufficient to use an InfoBar, since it | |
26 // does not map to a specific InfoBar type. Instead, you must implement either | |
27 // LinkInfoBarDelegate or ConfirmInfoBarDelegate, or override with your own | |
28 // delegate for your own InfoBar variety. | |
29 // | |
30 // --- WARNING --- | |
31 // When creating your InfoBarDelegate subclass, it is recommended that you | |
32 // design it such that you instantiate a brand new delegate for every call to | |
33 // AddInfoBar, rather than re-using/sharing a delegate object. Otherwise, | |
34 // you need to consider the fact that more than one InfoBar instance can exist | |
35 // and reference the same delegate -- even though it is also true that we only | |
36 // ever fully show one infobar (they don't stack). The dual-references occur | |
37 // because a second InfoBar can be added while the first one is in the process | |
38 // of closing (the animations). This can cause problems because when the first | |
39 // one does finally fully close InfoBarDelegate::InfoBarClosed() is called, | |
40 // and the delegate is free to clean itself up or reset state, which may have | |
41 // fatal consequences for the InfoBar that was in the process of opening (or is | |
42 // now fully opened) -- it is referencing a delegate that may not even exist | |
43 // anymore. | |
44 // As such, it is generally much safer to dedicate a delegate instance to | |
45 // AddInfoBar! | |
46 class InfoBarDelegate { | |
47 public: | |
48 // The type of the infobar. It controls its appearance, such as its background | |
49 // color. | |
50 enum Type { | |
51 WARNING_TYPE, | |
52 PAGE_ACTION_TYPE, | |
53 }; | |
54 | |
55 virtual ~InfoBarDelegate(); | |
56 | |
57 // Called to create the InfoBar. Implementation of this method is | |
58 // platform-specific. | |
59 virtual InfoBar* CreateInfoBar() = 0; | |
60 | |
61 // Returns true if the supplied |delegate| is equal to this one. Equality is | |
62 // left to the implementation to define. This function is called by the | |
63 // TabContents when determining whether or not a delegate should be added | |
64 // because a matching one already exists. If this function returns true, the | |
65 // TabContents will not add the new delegate because it considers one to | |
66 // already be present. | |
67 virtual bool EqualsDelegate(InfoBarDelegate* delegate) const; | |
68 | |
69 // Returns true if the InfoBar should be closed automatically after the page | |
70 // is navigated. The default behavior is to return true if the page is | |
71 // navigated somewhere else or reloaded. | |
72 virtual bool ShouldExpire( | |
73 const NavigationController::LoadCommittedDetails& details) const; | |
74 | |
75 // Called when the user clicks on the close button to dismiss the infobar. | |
76 virtual void InfoBarDismissed(); | |
77 | |
78 // Called after the InfoBar is closed. The delegate is free to delete itself | |
79 // at this point. | |
80 virtual void InfoBarClosed(); | |
81 | |
82 // Return the icon to be shown for this InfoBar. If the returned bitmap is | |
83 // NULL, no icon is shown. | |
84 virtual SkBitmap* GetIcon() const; | |
85 | |
86 // Returns the type of the infobar. The type determines the appearance (such | |
87 // as background color) of the infobar. | |
88 virtual Type GetInfoBarType() const; | |
89 | |
90 // Type-checking downcast routines: | |
91 virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate(); | |
92 virtual CrashedExtensionInfoBarDelegate* AsCrashedExtensionInfoBarDelegate(); | |
93 virtual ExtensionInfoBarDelegate* AsExtensionInfoBarDelegate(); | |
94 virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate(); | |
95 virtual PluginInstallerInfoBarDelegate* AsPluginInstallerInfoBarDelegate(); | |
96 virtual ThemeInstalledInfoBarDelegate* AsThemePreviewInfobarDelegate(); | |
97 virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate(); | |
98 | |
99 protected: | |
100 // Provided to subclasses as a convenience to initialize the state of this | |
101 // object. If |contents| is non-NULL, its active entry's unique ID will be | |
102 // stored using StoreActiveEntryUniqueID automatically. | |
103 explicit InfoBarDelegate(TabContents* contents); | |
104 | |
105 // Store the unique id for the active entry in the specified TabContents, to | |
106 // be used later upon navigation to determine if this InfoBarDelegate should | |
107 // be expired from |contents_|. | |
108 void StoreActiveEntryUniqueID(TabContents* contents); | |
109 | |
110 private: | |
111 // The unique id of the active NavigationEntry of the TabContents that we were | |
112 // opened for. Used to help expire on navigations. | |
113 int contents_unique_id_; | |
114 | |
115 DISALLOW_COPY_AND_ASSIGN(InfoBarDelegate); | |
116 }; | |
117 | |
118 // An interface derived from InfoBarDelegate implemented by objects wishing to | |
119 // control a LinkInfoBar. | |
120 class LinkInfoBarDelegate : public InfoBarDelegate { | |
121 public: | |
122 // Returns the message string to be displayed in the InfoBar. |link_offset| | |
123 // is the position where the link should be inserted. If |link_offset| is set | |
124 // to string16::npos (it is by default), the link is right aligned within | |
125 // the InfoBar rather than being embedded in the message text. | |
126 virtual string16 GetMessageTextWithOffset(size_t* link_offset) const; | |
127 | |
128 // Returns the text of the link to be displayed. | |
129 virtual string16 GetLinkText() const = 0; | |
130 | |
131 // Called when the Link is clicked. The |disposition| specifies how the | |
132 // resulting document should be loaded (based on the event flags present when | |
133 // the link was clicked). This function returns true if the InfoBar should be | |
134 // closed now or false if it should remain until the user explicitly closes | |
135 // it. | |
136 virtual bool LinkClicked(WindowOpenDisposition disposition); | |
137 | |
138 protected: | |
139 explicit LinkInfoBarDelegate(TabContents* contents); | |
140 virtual ~LinkInfoBarDelegate(); | |
141 | |
142 private: | |
143 // InfoBarDelegate: | |
144 virtual InfoBar* CreateInfoBar(); | |
145 virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate(); | |
146 | |
147 DISALLOW_COPY_AND_ASSIGN(LinkInfoBarDelegate); | |
148 }; | |
149 | |
150 // An interface derived from InfoBarDelegate implemented by objects wishing to | |
151 // control a ConfirmInfoBar. | |
152 class ConfirmInfoBarDelegate : public InfoBarDelegate { | |
153 public: | |
154 enum InfoBarButton { | |
155 BUTTON_NONE = 0, | |
156 BUTTON_OK = 1 << 0, | |
157 BUTTON_CANCEL = 1 << 1, | |
158 }; | |
159 | |
160 // Returns the message string to be displayed for the InfoBar. | |
161 virtual string16 GetMessageText() const = 0; | |
162 | |
163 // Return the buttons to be shown for this InfoBar. | |
164 virtual int GetButtons() const; | |
165 | |
166 // Return the label for the specified button. The default implementation | |
167 // returns "OK" for the OK button and "Cancel" for the Cancel button. | |
168 virtual string16 GetButtonLabel(InfoBarButton button) const; | |
169 | |
170 // Return whether or not the specified button needs elevation. | |
171 virtual bool NeedElevation(InfoBarButton button) const; | |
172 | |
173 // Called when the OK button is pressed. If the function returns true, the | |
174 // InfoBarDelegate should be removed from the associated TabContents. | |
175 virtual bool Accept(); | |
176 | |
177 // Called when the Cancel button is pressed. If the function returns true, | |
178 // the InfoBarDelegate should be removed from the associated TabContents. | |
179 virtual bool Cancel(); | |
180 | |
181 // Returns the text of the link to be displayed, if any. Otherwise returns | |
182 // and empty string. | |
183 virtual string16 GetLinkText(); | |
184 | |
185 // Called when the Link is clicked. The |disposition| specifies how the | |
186 // resulting document should be loaded (based on the event flags present when | |
187 // the link was clicked). This function returns true if the InfoBar should be | |
188 // closed now or false if it should remain until the user explicitly closes | |
189 // it. | |
190 // Will only be called if GetLinkText() returns non-empty string. | |
191 virtual bool LinkClicked(WindowOpenDisposition disposition); | |
192 | |
193 protected: | |
194 explicit ConfirmInfoBarDelegate(TabContents* contents); | |
195 virtual ~ConfirmInfoBarDelegate(); | |
196 | |
197 private: | |
198 // InfoBarDelegate: | |
199 virtual InfoBar* CreateInfoBar(); | |
200 virtual bool EqualsDelegate(InfoBarDelegate* delegate) const; | |
201 virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate(); | |
202 | |
203 DISALLOW_COPY_AND_ASSIGN(ConfirmInfoBarDelegate); | |
204 }; | |
205 | |
206 // Simple implementations for common use cases --------------------------------- | |
207 | |
208 class SimpleAlertInfoBarDelegate : public ConfirmInfoBarDelegate { | |
209 public: | |
210 SimpleAlertInfoBarDelegate(TabContents* contents, | |
211 SkBitmap* icon, // May be NULL. | |
212 const string16& message, | |
213 bool auto_expire); | |
214 | |
215 private: | |
216 virtual ~SimpleAlertInfoBarDelegate(); | |
217 | |
218 // ConfirmInfoBarDelegate: | |
219 virtual bool ShouldExpire( | |
220 const NavigationController::LoadCommittedDetails& details) const; | |
221 virtual void InfoBarClosed(); | |
222 virtual SkBitmap* GetIcon() const; | |
223 virtual string16 GetMessageText() const; | |
224 virtual int GetButtons() const; | |
225 | |
226 SkBitmap* icon_; | |
227 string16 message_; | |
228 bool auto_expire_; // Should it expire automatically on navigation? | |
229 | |
230 DISALLOW_COPY_AND_ASSIGN(SimpleAlertInfoBarDelegate); | |
231 }; | |
232 | 11 |
233 #endif // CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ | 12 #endif // CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ |
OLD | NEW |