blob: 4da3510befd2474e14921a7cb80caeda87a94bde [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]32b76ef2010-07-26 23:08:247#pragma once
[email protected]616ed5a2008-11-21 22:27:248
[email protected]616ed5a2008-11-21 22:27:249#include "base/basictypes.h"
[email protected]24492412010-08-15 19:00:1910#include "base/string16.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 {
[email protected]c8865482009-07-23 20:40:1051 WARNING_TYPE,
[email protected]96d1ed0c2010-08-14 16:33:5052 PAGE_ACTION_TYPE,
[email protected]c8865482009-07-23 20:40:1053 };
54
[email protected]616ed5a2008-11-21 22:27:2455 // Returns true if the supplied |delegate| is equal to this one. Equality is
56 // left to the implementation to define. This function is called by the
57 // TabContents when determining whether or not a delegate should be added
58 // because a matching one already exists. If this function returns true, the
59 // TabContents will not add the new delegate because it considers one to
60 // already be present.
61 virtual bool EqualsDelegate(InfoBarDelegate* delegate) const {
62 return false;
63 }
64
65 // Returns true if the InfoBar should be closed automatically after the page
[email protected]f86a07022008-11-25 01:06:0566 // is navigated. The default behavior is to return true if the page is
67 // navigated somewhere else or reloaded.
68 virtual bool ShouldExpire(
69 const NavigationController::LoadCommittedDetails& details) const;
[email protected]616ed5a2008-11-21 22:27:2470
[email protected]c8865482009-07-23 20:40:1071 // Called when the user clicks on the close button to dismiss the infobar.
72 virtual void InfoBarDismissed() {}
73
[email protected]616ed5a2008-11-21 22:27:2474 // Called after the InfoBar is closed. The delegate is free to delete itself
75 // at this point.
76 virtual void InfoBarClosed() {}
77
78 // Called to create the InfoBar. Implementation of this method is
79 // platform-specific.
80 virtual InfoBar* CreateInfoBar() = 0;
81
[email protected]358fbd52009-04-10 19:26:3282 // 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 { return NULL; }
85
[email protected]5855df72008-12-03 01:36:0086 // Returns a pointer to the AlertInfoBarDelegate interface, if implemented.
[email protected]616ed5a2008-11-21 22:27:2487 virtual AlertInfoBarDelegate* AsAlertInfoBarDelegate() {
88 return NULL;
89 }
90
[email protected]5855df72008-12-03 01:36:0091 // Returns a pointer to the LinkInfoBarDelegate interface, if implemented.
92 virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate() {
93 return NULL;
94 }
95
[email protected]616ed5a2008-11-21 22:27:2496 // Returns a pointer to the ConfirmInfoBarDelegate interface, if implemented.
97 virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate() {
98 return NULL;
99 }
[email protected]f86a07022008-11-25 01:06:05100
[email protected]47e4c8792009-10-09 20:52:48101 // Returns a pointer to the ThemeInstalledInfoBarDelegate interface, if
[email protected]7a691a962009-07-28 22:57:22102 // implemented.
[email protected]47e4c8792009-10-09 20:52:48103 virtual ThemeInstalledInfoBarDelegate* AsThemePreviewInfobarDelegate() {
[email protected]7a691a962009-07-28 22:57:22104 return NULL;
105 }
106
[email protected]fb53e5d2010-01-27 19:04:55107 // Returns a pointer to the TranslateInfoBarDelegate interface, if
108 // implemented.
109 virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate() {
110 return NULL;
111 }
112
[email protected]f34e79632010-03-17 02:34:08113 // Returns a pointer to the ExtensionInfoBarDelegate interface, if
114 // implemented.
115 virtual ExtensionInfoBarDelegate* AsExtensionInfoBarDelegate() {
116 return NULL;
117 }
118
[email protected]ad0c2e1b2010-01-30 00:00:10119 // Returns a pointer to the CrashedExtensionInfoBarDelegate interface, if
120 // implemented.
121 virtual CrashedExtensionInfoBarDelegate* AsCrashedExtensionInfoBarDelegate() {
122 return NULL;
123 }
124
[email protected]c8865482009-07-23 20:40:10125 // Returns the type of the infobar. The type determines the appearance (such
126 // as background color) of the infobar.
127 virtual Type GetInfoBarType() {
128 return WARNING_TYPE;
129 }
130
[email protected]f86a07022008-11-25 01:06:05131 protected:
[email protected]5855df72008-12-03 01:36:00132 // Provided to subclasses as a convenience to initialize the state of this
133 // object. If |contents| is non-NULL, its active entry's unique ID will be
134 // stored using StoreActiveEntryUniqueID automatically.
[email protected]f86a07022008-11-25 01:06:05135 explicit InfoBarDelegate(TabContents* contents);
[email protected]f86a07022008-11-25 01:06:05136
[email protected]b6187132009-01-21 23:20:48137 virtual ~InfoBarDelegate() { }
138
[email protected]5855df72008-12-03 01:36:00139 // Store the unique id for the active entry in the specified TabContents, to
140 // be used later upon navigation to determine if this InfoBarDelegate should
141 // be expired from |contents_|.
142 void StoreActiveEntryUniqueID(TabContents* contents);
143
144 private:
[email protected]f86a07022008-11-25 01:06:05145 // The unique id of the active NavigationEntry of the TabContents taht we were
146 // opened for. Used to help expire on navigations.
147 int contents_unique_id_;
148
149 DISALLOW_COPY_AND_ASSIGN(InfoBarDelegate);
[email protected]616ed5a2008-11-21 22:27:24150};
151
152// An interface derived from InfoBarDelegate implemented by objects wishing to
153// control an AlertInfoBar.
154class AlertInfoBarDelegate : public InfoBarDelegate {
155 public:
156 // Returns the message string to be displayed for the InfoBar.
[email protected]e23d3a32010-08-13 19:39:58157 virtual string16 GetMessageText() const = 0;
[email protected]616ed5a2008-11-21 22:27:24158
[email protected]358fbd52009-04-10 19:26:32159 // Overridden from InfoBarDelegate.
[email protected]616ed5a2008-11-21 22:27:24160 virtual SkBitmap* GetIcon() const { return NULL; }
161
162 // Overridden from InfoBarDelegate:
163 virtual bool EqualsDelegate(InfoBarDelegate* delegate) const;
164 virtual InfoBar* CreateInfoBar();
165 virtual AlertInfoBarDelegate* AsAlertInfoBarDelegate() { return this; }
[email protected]f86a07022008-11-25 01:06:05166
167 protected:
168 explicit AlertInfoBarDelegate(TabContents* contents);
169
170 DISALLOW_COPY_AND_ASSIGN(AlertInfoBarDelegate);
[email protected]616ed5a2008-11-21 22:27:24171};
172
173// An interface derived from InfoBarDelegate implemented by objects wishing to
[email protected]5855df72008-12-03 01:36:00174// control a LinkInfoBar.
175class LinkInfoBarDelegate : public InfoBarDelegate {
176 public:
177 // Returns the message string to be displayed in the InfoBar. |link_offset|
178 // is the position where the link should be inserted. If |link_offset| is set
[email protected]e23d3a32010-08-13 19:39:58179 // to string16::npos (it is by default), the link is right aligned within
[email protected]5855df72008-12-03 01:36:00180 // the InfoBar rather than being embedded in the message text.
[email protected]e23d3a32010-08-13 19:39:58181 virtual string16 GetMessageTextWithOffset(size_t* link_offset) const {
182 *link_offset = string16::npos;
183 return string16();
[email protected]5855df72008-12-03 01:36:00184 }
185
186 // Returns the text of the link to be displayed.
[email protected]e23d3a32010-08-13 19:39:58187 virtual string16 GetLinkText() const = 0;
[email protected]5855df72008-12-03 01:36:00188
[email protected]358fbd52009-04-10 19:26:32189 // Overridden from InfoBarDelegate.
[email protected]5855df72008-12-03 01:36:00190 virtual SkBitmap* GetIcon() const { return NULL; }
191
192 // Called when the Link is clicked. The |disposition| specifies how the
193 // resulting document should be loaded (based on the event flags present when
194 // the link was clicked). This function returns true if the InfoBar should be
195 // closed now or false if it should remain until the user explicitly closes
196 // it.
197 virtual bool LinkClicked(WindowOpenDisposition disposition) {
[email protected]f0a51fb52009-03-05 12:46:38198 return true;
[email protected]5855df72008-12-03 01:36:00199 }
200
201 // Overridden from InfoBarDelegate:
202 virtual InfoBar* CreateInfoBar();
203 virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate() {
204 return this;
205 }
206
207 protected:
208 explicit LinkInfoBarDelegate(TabContents* contents);
209
210 DISALLOW_COPY_AND_ASSIGN(LinkInfoBarDelegate);
211};
212
213// An interface derived from InfoBarDelegate implemented by objects wishing to
[email protected]616ed5a2008-11-21 22:27:24214// control a ConfirmInfoBar.
215class ConfirmInfoBarDelegate : public AlertInfoBarDelegate {
216 public:
217 enum InfoBarButton {
218 BUTTON_NONE = 0,
[email protected]362e00412009-05-02 00:40:02219 BUTTON_OK = 1,
220 BUTTON_CANCEL = 2,
221 // Specifies that the OK button should be rendered like a default button.
222 BUTTON_OK_DEFAULT = 4
[email protected]616ed5a2008-11-21 22:27:24223 };
224
225 // Return the buttons to be shown for this InfoBar.
226 virtual int GetButtons() const {
227 return BUTTON_NONE;
228 }
229
230 // Return the label for the specified button. The default implementation
231 // returns "OK" for the OK button and "Cancel" for the Cancel button.
[email protected]e23d3a32010-08-13 19:39:58232 virtual string16 GetButtonLabel(InfoBarButton button) const;
[email protected]616ed5a2008-11-21 22:27:24233
[email protected]28918392010-03-23 07:09:30234 // Return whether or not the specified button needs elevation.
235 virtual bool NeedElevation(InfoBarButton button) const { return false; }
236
[email protected]818b9252008-11-25 01:55:10237 // Called when the OK button is pressed. If the function returns true, the
238 // InfoBarDelegate should be removed from the associated TabContents.
239 virtual bool Accept() { return true; }
[email protected]616ed5a2008-11-21 22:27:24240
[email protected]818b9252008-11-25 01:55:10241 // Called when the Cancel button is pressed. If the function returns true,
242 // the InfoBarDelegate should be removed from the associated TabContents.
243 virtual bool Cancel() { return true; }
[email protected]616ed5a2008-11-21 22:27:24244
[email protected]ff667b932010-03-18 13:48:02245 // Returns the text of the link to be displayed, if any. Otherwise returns
246 // and empty string.
[email protected]e23d3a32010-08-13 19:39:58247 virtual string16 GetLinkText() {
248 return string16();
[email protected]ff667b932010-03-18 13:48:02249 }
250
251 // Called when the Link is clicked. The |disposition| specifies how the
252 // resulting document should be loaded (based on the event flags present when
253 // the link was clicked). This function returns true if the InfoBar should be
254 // closed now or false if it should remain until the user explicitly closes
255 // it.
256 // Will only be called if GetLinkText() returns non-empty string.
257 virtual bool LinkClicked(WindowOpenDisposition disposition) {
258 return true;
259 }
260
[email protected]616ed5a2008-11-21 22:27:24261 // Overridden from InfoBarDelegate:
262 virtual InfoBar* CreateInfoBar();
263 virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate() {
264 return this;
265 }
[email protected]f86a07022008-11-25 01:06:05266
267 protected:
268 explicit ConfirmInfoBarDelegate(TabContents* contents);
269
270 DISALLOW_COPY_AND_ASSIGN(ConfirmInfoBarDelegate);
[email protected]616ed5a2008-11-21 22:27:24271};
272
273// Simple implementations for common use cases ---------------------------------
274
275class SimpleAlertInfoBarDelegate : public AlertInfoBarDelegate {
276 public:
[email protected]938e1f92010-04-01 18:09:42277 // |icon| may be |NULL|.
[email protected]f86a07022008-11-25 01:06:05278 SimpleAlertInfoBarDelegate(TabContents* contents,
[email protected]e23d3a32010-08-13 19:39:58279 const string16& message,
[email protected]938e1f92010-04-01 18:09:42280 SkBitmap* icon,
281 bool auto_expire);
[email protected]616ed5a2008-11-21 22:27:24282
283 // Overridden from AlertInfoBarDelegate:
[email protected]938e1f92010-04-01 18:09:42284 virtual bool ShouldExpire(
285 const NavigationController::LoadCommittedDetails& details) const;
[email protected]e23d3a32010-08-13 19:39:58286 virtual string16 GetMessageText() const;
[email protected]616ed5a2008-11-21 22:27:24287 virtual SkBitmap* GetIcon() const;
288 virtual void InfoBarClosed();
289
290 private:
[email protected]e23d3a32010-08-13 19:39:58291 string16 message_;
[email protected]616ed5a2008-11-21 22:27:24292 SkBitmap* icon_;
[email protected]938e1f92010-04-01 18:09:42293 bool auto_expire_; // Should it expire automatically on navigation?
[email protected]616ed5a2008-11-21 22:27:24294
295 DISALLOW_COPY_AND_ASSIGN(SimpleAlertInfoBarDelegate);
296};
297
[email protected]038d52e12009-10-14 16:53:41298#endif // CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_