blob: 3c8b7145468a767f1784bf83329864444fe36821 [file] [log] [blame]
[email protected]f34e79632010-03-17 02:34:081// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]616ed5a2008-11-21 22:27:242// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]5632b202009-01-16 19:20:565#ifndef CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_
6#define CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_
[email protected]616ed5a2008-11-21 22:27:247
[email protected]d882cf92008-11-21 22:35:118#include <string>
9
[email protected]616ed5a2008-11-21 22:27:2410#include "base/basictypes.h"
[email protected]f3ec7742009-01-15 00:59:1611#include "chrome/browser/tab_contents/navigation_controller.h"
[email protected]e09ba552009-02-05 03:26:2912#include "webkit/glue/window_open_disposition.h"
[email protected]616ed5a2008-11-21 22:27:2413
14class AlertInfoBarDelegate;
15class ConfirmInfoBarDelegate;
[email protected]ad0c2e1b2010-01-30 00:00:1016class CrashedExtensionInfoBarDelegate;
[email protected]f34e79632010-03-17 02:34:0817class ExtensionInfoBarDelegate;
[email protected]fb53e5d2010-01-27 19:04:5518class TranslateInfoBarDelegate;
[email protected]616ed5a2008-11-21 22:27:2419class InfoBar;
[email protected]5855df72008-12-03 01:36:0020class LinkInfoBarDelegate;
[email protected]1db6ff152009-10-12 15:32:0721class SkBitmap;
[email protected]47e4c8792009-10-09 20:52:4822class ThemeInstalledInfoBarDelegate;
[email protected]616ed5a2008-11-21 22:27:2423
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// AlertInfoBarDelegate or ConfirmInfoBarDelegate, or override with your own
28// delegate for your own InfoBar variety.
[email protected]c34099192009-01-16 22:46:5129//
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
[email protected]f0a51fb52009-03-05 12:46:3843// anymore.
[email protected]c34099192009-01-16 22:46:5144// As such, it is generally much safer to dedicate a delegate instance to
45// AddInfoBar!
[email protected]616ed5a2008-11-21 22:27:2446class InfoBarDelegate {
47 public:
[email protected]54bc09252010-01-31 21:45:1748 // The type of the infobar. It controls its appearance, such as its background
[email protected]c8865482009-07-23 20:40:1049 // color.
50 enum Type {
51 INFO_TYPE,
52 WARNING_TYPE,
[email protected]fb53e5d2010-01-27 19:04:5553 ERROR_TYPE,
54 PAGE_ACTION_TYPE
[email protected]c8865482009-07-23 20:40:1055 };
56
[email protected]616ed5a2008-11-21 22:27:2457 // Returns true if the supplied |delegate| is equal to this one. Equality is
58 // left to the implementation to define. This function is called by the
59 // TabContents when determining whether or not a delegate should be added
60 // because a matching one already exists. If this function returns true, the
61 // TabContents will not add the new delegate because it considers one to
62 // already be present.
63 virtual bool EqualsDelegate(InfoBarDelegate* delegate) const {
64 return false;
65 }
66
67 // Returns true if the InfoBar should be closed automatically after the page
[email protected]f86a07022008-11-25 01:06:0568 // is navigated. The default behavior is to return true if the page is
69 // navigated somewhere else or reloaded.
70 virtual bool ShouldExpire(
71 const NavigationController::LoadCommittedDetails& details) const;
[email protected]616ed5a2008-11-21 22:27:2472
[email protected]c8865482009-07-23 20:40:1073 // Called when the user clicks on the close button to dismiss the infobar.
74 virtual void InfoBarDismissed() {}
75
[email protected]616ed5a2008-11-21 22:27:2476 // Called after the InfoBar is closed. The delegate is free to delete itself
77 // at this point.
78 virtual void InfoBarClosed() {}
79
80 // Called to create the InfoBar. Implementation of this method is
81 // platform-specific.
82 virtual InfoBar* CreateInfoBar() = 0;
83
[email protected]358fbd52009-04-10 19:26:3284 // Return the icon to be shown for this InfoBar. If the returned bitmap is
85 // NULL, no icon is shown.
86 virtual SkBitmap* GetIcon() const { return NULL; }
87
[email protected]5855df72008-12-03 01:36:0088 // Returns a pointer to the AlertInfoBarDelegate interface, if implemented.
[email protected]616ed5a2008-11-21 22:27:2489 virtual AlertInfoBarDelegate* AsAlertInfoBarDelegate() {
90 return NULL;
91 }
92
[email protected]5855df72008-12-03 01:36:0093 // Returns a pointer to the LinkInfoBarDelegate interface, if implemented.
94 virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate() {
95 return NULL;
96 }
97
[email protected]616ed5a2008-11-21 22:27:2498 // Returns a pointer to the ConfirmInfoBarDelegate interface, if implemented.
99 virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate() {
100 return NULL;
101 }
[email protected]f86a07022008-11-25 01:06:05102
[email protected]47e4c8792009-10-09 20:52:48103 // Returns a pointer to the ThemeInstalledInfoBarDelegate interface, if
[email protected]7a691a962009-07-28 22:57:22104 // implemented.
[email protected]47e4c8792009-10-09 20:52:48105 virtual ThemeInstalledInfoBarDelegate* AsThemePreviewInfobarDelegate() {
[email protected]7a691a962009-07-28 22:57:22106 return NULL;
107 }
108
[email protected]fb53e5d2010-01-27 19:04:55109 // Returns a pointer to the TranslateInfoBarDelegate interface, if
110 // implemented.
111 virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate() {
112 return NULL;
113 }
114
[email protected]f34e79632010-03-17 02:34:08115 // Returns a pointer to the ExtensionInfoBarDelegate interface, if
116 // implemented.
117 virtual ExtensionInfoBarDelegate* AsExtensionInfoBarDelegate() {
118 return NULL;
119 }
120
[email protected]ad0c2e1b2010-01-30 00:00:10121 // Returns a pointer to the CrashedExtensionInfoBarDelegate interface, if
122 // implemented.
123 virtual CrashedExtensionInfoBarDelegate* AsCrashedExtensionInfoBarDelegate() {
124 return NULL;
125 }
126
[email protected]c8865482009-07-23 20:40:10127 // Returns the type of the infobar. The type determines the appearance (such
128 // as background color) of the infobar.
129 virtual Type GetInfoBarType() {
130 return WARNING_TYPE;
131 }
132
[email protected]f86a07022008-11-25 01:06:05133 protected:
[email protected]5855df72008-12-03 01:36:00134 // Provided to subclasses as a convenience to initialize the state of this
135 // object. If |contents| is non-NULL, its active entry's unique ID will be
136 // stored using StoreActiveEntryUniqueID automatically.
[email protected]f86a07022008-11-25 01:06:05137 explicit InfoBarDelegate(TabContents* contents);
[email protected]f86a07022008-11-25 01:06:05138
[email protected]b6187132009-01-21 23:20:48139 virtual ~InfoBarDelegate() { }
140
[email protected]5855df72008-12-03 01:36:00141 // Store the unique id for the active entry in the specified TabContents, to
142 // be used later upon navigation to determine if this InfoBarDelegate should
143 // be expired from |contents_|.
144 void StoreActiveEntryUniqueID(TabContents* contents);
145
146 private:
[email protected]f86a07022008-11-25 01:06:05147 // The unique id of the active NavigationEntry of the TabContents taht we were
148 // opened for. Used to help expire on navigations.
149 int contents_unique_id_;
150
151 DISALLOW_COPY_AND_ASSIGN(InfoBarDelegate);
[email protected]616ed5a2008-11-21 22:27:24152};
153
154// An interface derived from InfoBarDelegate implemented by objects wishing to
155// control an AlertInfoBar.
156class AlertInfoBarDelegate : public InfoBarDelegate {
157 public:
158 // Returns the message string to be displayed for the InfoBar.
159 virtual std::wstring GetMessageText() const = 0;
160
[email protected]358fbd52009-04-10 19:26:32161 // Overridden from InfoBarDelegate.
[email protected]616ed5a2008-11-21 22:27:24162 virtual SkBitmap* GetIcon() const { return NULL; }
163
164 // Overridden from InfoBarDelegate:
165 virtual bool EqualsDelegate(InfoBarDelegate* delegate) const;
166 virtual InfoBar* CreateInfoBar();
167 virtual AlertInfoBarDelegate* AsAlertInfoBarDelegate() { return this; }
[email protected]f86a07022008-11-25 01:06:05168
169 protected:
170 explicit AlertInfoBarDelegate(TabContents* contents);
171
172 DISALLOW_COPY_AND_ASSIGN(AlertInfoBarDelegate);
[email protected]616ed5a2008-11-21 22:27:24173};
174
175// An interface derived from InfoBarDelegate implemented by objects wishing to
[email protected]5855df72008-12-03 01:36:00176// control a LinkInfoBar.
177class LinkInfoBarDelegate : public InfoBarDelegate {
178 public:
179 // Returns the message string to be displayed in the InfoBar. |link_offset|
180 // is the position where the link should be inserted. If |link_offset| is set
181 // to std::wstring::npos (it is by default), the link is right aligned within
182 // the InfoBar rather than being embedded in the message text.
183 virtual std::wstring GetMessageTextWithOffset(size_t* link_offset) const {
184 *link_offset = std::wstring::npos;
185 return std::wstring();
186 }
187
188 // Returns the text of the link to be displayed.
189 virtual std::wstring GetLinkText() const = 0;
190
[email protected]358fbd52009-04-10 19:26:32191 // Overridden from InfoBarDelegate.
[email protected]5855df72008-12-03 01:36:00192 virtual SkBitmap* GetIcon() const { return NULL; }
193
194 // Called when the Link is clicked. The |disposition| specifies how the
195 // resulting document should be loaded (based on the event flags present when
196 // the link was clicked). This function returns true if the InfoBar should be
197 // closed now or false if it should remain until the user explicitly closes
198 // it.
199 virtual bool LinkClicked(WindowOpenDisposition disposition) {
[email protected]f0a51fb52009-03-05 12:46:38200 return true;
[email protected]5855df72008-12-03 01:36:00201 }
202
203 // Overridden from InfoBarDelegate:
204 virtual InfoBar* CreateInfoBar();
205 virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate() {
206 return this;
207 }
208
209 protected:
210 explicit LinkInfoBarDelegate(TabContents* contents);
211
212 DISALLOW_COPY_AND_ASSIGN(LinkInfoBarDelegate);
213};
214
215// An interface derived from InfoBarDelegate implemented by objects wishing to
[email protected]616ed5a2008-11-21 22:27:24216// control a ConfirmInfoBar.
217class ConfirmInfoBarDelegate : public AlertInfoBarDelegate {
218 public:
219 enum InfoBarButton {
220 BUTTON_NONE = 0,
[email protected]362e00412009-05-02 00:40:02221 BUTTON_OK = 1,
222 BUTTON_CANCEL = 2,
223 // Specifies that the OK button should be rendered like a default button.
224 BUTTON_OK_DEFAULT = 4
[email protected]616ed5a2008-11-21 22:27:24225 };
226
227 // Return the buttons to be shown for this InfoBar.
228 virtual int GetButtons() const {
229 return BUTTON_NONE;
230 }
231
232 // Return the label for the specified button. The default implementation
233 // returns "OK" for the OK button and "Cancel" for the Cancel button.
234 virtual std::wstring GetButtonLabel(InfoBarButton button) const;
235
[email protected]28918392010-03-23 07:09:30236 // Return whether or not the specified button needs elevation.
237 virtual bool NeedElevation(InfoBarButton button) const { return false; }
238
[email protected]818b9252008-11-25 01:55:10239 // Called when the OK button is pressed. If the function returns true, the
240 // InfoBarDelegate should be removed from the associated TabContents.
241 virtual bool Accept() { return true; }
[email protected]616ed5a2008-11-21 22:27:24242
[email protected]818b9252008-11-25 01:55:10243 // Called when the Cancel button is pressed. If the function returns true,
244 // the InfoBarDelegate should be removed from the associated TabContents.
245 virtual bool Cancel() { return true; }
[email protected]616ed5a2008-11-21 22:27:24246
[email protected]ff667b932010-03-18 13:48:02247 // Returns the text of the link to be displayed, if any. Otherwise returns
248 // and empty string.
249 virtual std::wstring GetLinkText() {
250 return std::wstring();
251 }
252
253 // Called when the Link is clicked. The |disposition| specifies how the
254 // resulting document should be loaded (based on the event flags present when
255 // the link was clicked). This function returns true if the InfoBar should be
256 // closed now or false if it should remain until the user explicitly closes
257 // it.
258 // Will only be called if GetLinkText() returns non-empty string.
259 virtual bool LinkClicked(WindowOpenDisposition disposition) {
260 return true;
261 }
262
[email protected]616ed5a2008-11-21 22:27:24263 // Overridden from InfoBarDelegate:
264 virtual InfoBar* CreateInfoBar();
265 virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate() {
266 return this;
267 }
[email protected]f86a07022008-11-25 01:06:05268
269 protected:
270 explicit ConfirmInfoBarDelegate(TabContents* contents);
271
272 DISALLOW_COPY_AND_ASSIGN(ConfirmInfoBarDelegate);
[email protected]616ed5a2008-11-21 22:27:24273};
274
275// Simple implementations for common use cases ---------------------------------
276
277class SimpleAlertInfoBarDelegate : public AlertInfoBarDelegate {
278 public:
[email protected]938e1f92010-04-01 18:09:42279 // |icon| may be |NULL|.
[email protected]f86a07022008-11-25 01:06:05280 SimpleAlertInfoBarDelegate(TabContents* contents,
281 const std::wstring& message,
[email protected]938e1f92010-04-01 18:09:42282 SkBitmap* icon,
283 bool auto_expire);
[email protected]616ed5a2008-11-21 22:27:24284
285 // Overridden from AlertInfoBarDelegate:
[email protected]938e1f92010-04-01 18:09:42286 virtual bool ShouldExpire(
287 const NavigationController::LoadCommittedDetails& details) const;
[email protected]616ed5a2008-11-21 22:27:24288 virtual std::wstring GetMessageText() const;
289 virtual SkBitmap* GetIcon() const;
290 virtual void InfoBarClosed();
291
292 private:
293 std::wstring message_;
294 SkBitmap* icon_;
[email protected]938e1f92010-04-01 18:09:42295 bool auto_expire_; // Should it expire automatically on navigation?
[email protected]616ed5a2008-11-21 22:27:24296
297 DISALLOW_COPY_AND_ASSIGN(SimpleAlertInfoBarDelegate);
298};
299
[email protected]038d52e12009-10-14 16:53:41300#endif // CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_