blob: c070161f4e96fa8de161b456928bfe7487823a33 [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]d882cf92008-11-21 22:35:119#include <string>
10
[email protected]616ed5a2008-11-21 22:27:2411#include "base/basictypes.h"
[email protected]f3ec7742009-01-15 00:59:1612#include "chrome/browser/tab_contents/navigation_controller.h"
[email protected]e09ba552009-02-05 03:26:2913#include "webkit/glue/window_open_disposition.h"
[email protected]616ed5a2008-11-21 22:27:2414
15class AlertInfoBarDelegate;
16class ConfirmInfoBarDelegate;
[email protected]ad0c2e1b2010-01-30 00:00:1017class CrashedExtensionInfoBarDelegate;
[email protected]f34e79632010-03-17 02:34:0818class ExtensionInfoBarDelegate;
[email protected]fb53e5d2010-01-27 19:04:5519class TranslateInfoBarDelegate;
[email protected]616ed5a2008-11-21 22:27:2420class InfoBar;
[email protected]5855df72008-12-03 01:36:0021class LinkInfoBarDelegate;
[email protected]1db6ff152009-10-12 15:32:0722class SkBitmap;
[email protected]47e4c8792009-10-09 20:52:4823class ThemeInstalledInfoBarDelegate;
[email protected]616ed5a2008-11-21 22:27:2424
25// An interface implemented by objects wishing to control an InfoBar.
26// Implementing this interface is not sufficient to use an InfoBar, since it
27// does not map to a specific InfoBar type. Instead, you must implement either
28// AlertInfoBarDelegate or ConfirmInfoBarDelegate, or override with your own
29// delegate for your own InfoBar variety.
[email protected]c34099192009-01-16 22:46:5130//
31// --- WARNING ---
32// When creating your InfoBarDelegate subclass, it is recommended that you
33// design it such that you instantiate a brand new delegate for every call to
34// AddInfoBar, rather than re-using/sharing a delegate object. Otherwise,
35// you need to consider the fact that more than one InfoBar instance can exist
36// and reference the same delegate -- even though it is also true that we only
37// ever fully show one infobar (they don't stack). The dual-references occur
38// because a second InfoBar can be added while the first one is in the process
39// of closing (the animations). This can cause problems because when the first
40// one does finally fully close InfoBarDelegate::InfoBarClosed() is called,
41// and the delegate is free to clean itself up or reset state, which may have
42// fatal consequences for the InfoBar that was in the process of opening (or is
43// now fully opened) -- it is referencing a delegate that may not even exist
[email protected]f0a51fb52009-03-05 12:46:3844// anymore.
[email protected]c34099192009-01-16 22:46:5145// As such, it is generally much safer to dedicate a delegate instance to
46// AddInfoBar!
[email protected]616ed5a2008-11-21 22:27:2447class InfoBarDelegate {
48 public:
[email protected]54bc09252010-01-31 21:45:1749 // The type of the infobar. It controls its appearance, such as its background
[email protected]c8865482009-07-23 20:40:1050 // color.
51 enum Type {
[email protected]d2ce4e32010-07-26 14:40:2252 INFO_TYPE,
[email protected]c8865482009-07-23 20:40:1053 WARNING_TYPE,
[email protected]fb53e5d2010-01-27 19:04:5554 ERROR_TYPE,
55 PAGE_ACTION_TYPE
[email protected]c8865482009-07-23 20:40:1056 };
57
[email protected]616ed5a2008-11-21 22:27:2458 // Returns true if the supplied |delegate| is equal to this one. Equality is
59 // left to the implementation to define. This function is called by the
60 // TabContents when determining whether or not a delegate should be added
61 // because a matching one already exists. If this function returns true, the
62 // TabContents will not add the new delegate because it considers one to
63 // already be present.
64 virtual bool EqualsDelegate(InfoBarDelegate* delegate) const {
65 return false;
66 }
67
68 // Returns true if the InfoBar should be closed automatically after the page
[email protected]f86a07022008-11-25 01:06:0569 // is navigated. The default behavior is to return true if the page is
70 // navigated somewhere else or reloaded.
71 virtual bool ShouldExpire(
72 const NavigationController::LoadCommittedDetails& details) const;
[email protected]616ed5a2008-11-21 22:27:2473
[email protected]c8865482009-07-23 20:40:1074 // Called when the user clicks on the close button to dismiss the infobar.
75 virtual void InfoBarDismissed() {}
76
[email protected]616ed5a2008-11-21 22:27:2477 // Called after the InfoBar is closed. The delegate is free to delete itself
78 // at this point.
79 virtual void InfoBarClosed() {}
80
81 // Called to create the InfoBar. Implementation of this method is
82 // platform-specific.
83 virtual InfoBar* CreateInfoBar() = 0;
84
[email protected]358fbd52009-04-10 19:26:3285 // Return the icon to be shown for this InfoBar. If the returned bitmap is
86 // NULL, no icon is shown.
87 virtual SkBitmap* GetIcon() const { return NULL; }
88
[email protected]5855df72008-12-03 01:36:0089 // Returns a pointer to the AlertInfoBarDelegate interface, if implemented.
[email protected]616ed5a2008-11-21 22:27:2490 virtual AlertInfoBarDelegate* AsAlertInfoBarDelegate() {
91 return NULL;
92 }
93
[email protected]5855df72008-12-03 01:36:0094 // Returns a pointer to the LinkInfoBarDelegate interface, if implemented.
95 virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate() {
96 return NULL;
97 }
98
[email protected]616ed5a2008-11-21 22:27:2499 // Returns a pointer to the ConfirmInfoBarDelegate interface, if implemented.
100 virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate() {
101 return NULL;
102 }
[email protected]f86a07022008-11-25 01:06:05103
[email protected]47e4c8792009-10-09 20:52:48104 // Returns a pointer to the ThemeInstalledInfoBarDelegate interface, if
[email protected]7a691a962009-07-28 22:57:22105 // implemented.
[email protected]47e4c8792009-10-09 20:52:48106 virtual ThemeInstalledInfoBarDelegate* AsThemePreviewInfobarDelegate() {
[email protected]7a691a962009-07-28 22:57:22107 return NULL;
108 }
109
[email protected]fb53e5d2010-01-27 19:04:55110 // Returns a pointer to the TranslateInfoBarDelegate interface, if
111 // implemented.
112 virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate() {
113 return NULL;
114 }
[email protected]fb53e5d2010-01-27 19:04:55115
[email protected]f34e79632010-03-17 02:34:08116 // Returns a pointer to the ExtensionInfoBarDelegate interface, if
117 // implemented.
118 virtual ExtensionInfoBarDelegate* AsExtensionInfoBarDelegate() {
119 return NULL;
120 }
121
[email protected]ad0c2e1b2010-01-30 00:00:10122 // Returns a pointer to the CrashedExtensionInfoBarDelegate interface, if
123 // implemented.
124 virtual CrashedExtensionInfoBarDelegate* AsCrashedExtensionInfoBarDelegate() {
125 return NULL;
126 }
127
[email protected]c8865482009-07-23 20:40:10128 // Returns the type of the infobar. The type determines the appearance (such
129 // as background color) of the infobar.
130 virtual Type GetInfoBarType() {
131 return WARNING_TYPE;
132 }
133
[email protected]f86a07022008-11-25 01:06:05134 protected:
[email protected]5855df72008-12-03 01:36:00135 // Provided to subclasses as a convenience to initialize the state of this
136 // object. If |contents| is non-NULL, its active entry's unique ID will be
137 // stored using StoreActiveEntryUniqueID automatically.
[email protected]f86a07022008-11-25 01:06:05138 explicit InfoBarDelegate(TabContents* contents);
[email protected]f86a07022008-11-25 01:06:05139
[email protected]b6187132009-01-21 23:20:48140 virtual ~InfoBarDelegate() { }
141
[email protected]5855df72008-12-03 01:36:00142 // Store the unique id for the active entry in the specified TabContents, to
143 // be used later upon navigation to determine if this InfoBarDelegate should
144 // be expired from |contents_|.
145 void StoreActiveEntryUniqueID(TabContents* contents);
146
147 private:
[email protected]f86a07022008-11-25 01:06:05148 // The unique id of the active NavigationEntry of the TabContents taht we were
149 // opened for. Used to help expire on navigations.
150 int contents_unique_id_;
151
152 DISALLOW_COPY_AND_ASSIGN(InfoBarDelegate);
[email protected]616ed5a2008-11-21 22:27:24153};
154
155// An interface derived from InfoBarDelegate implemented by objects wishing to
156// control an AlertInfoBar.
157class AlertInfoBarDelegate : public InfoBarDelegate {
158 public:
159 // Returns the message string to be displayed for the InfoBar.
160 virtual std::wstring GetMessageText() const = 0;
161
[email protected]358fbd52009-04-10 19:26:32162 // Overridden from InfoBarDelegate.
[email protected]616ed5a2008-11-21 22:27:24163 virtual SkBitmap* GetIcon() const { return NULL; }
164
165 // Overridden from InfoBarDelegate:
166 virtual bool EqualsDelegate(InfoBarDelegate* delegate) const;
167 virtual InfoBar* CreateInfoBar();
168 virtual AlertInfoBarDelegate* AsAlertInfoBarDelegate() { return this; }
[email protected]f86a07022008-11-25 01:06:05169
170 protected:
171 explicit AlertInfoBarDelegate(TabContents* contents);
172
173 DISALLOW_COPY_AND_ASSIGN(AlertInfoBarDelegate);
[email protected]616ed5a2008-11-21 22:27:24174};
175
176// An interface derived from InfoBarDelegate implemented by objects wishing to
[email protected]5855df72008-12-03 01:36:00177// control a LinkInfoBar.
178class LinkInfoBarDelegate : public InfoBarDelegate {
179 public:
180 // Returns the message string to be displayed in the InfoBar. |link_offset|
181 // is the position where the link should be inserted. If |link_offset| is set
182 // to std::wstring::npos (it is by default), the link is right aligned within
183 // the InfoBar rather than being embedded in the message text.
184 virtual std::wstring GetMessageTextWithOffset(size_t* link_offset) const {
185 *link_offset = std::wstring::npos;
186 return std::wstring();
187 }
188
189 // Returns the text of the link to be displayed.
190 virtual std::wstring GetLinkText() const = 0;
191
[email protected]358fbd52009-04-10 19:26:32192 // Overridden from InfoBarDelegate.
[email protected]5855df72008-12-03 01:36:00193 virtual SkBitmap* GetIcon() const { return NULL; }
194
195 // Called when the Link is clicked. The |disposition| specifies how the
196 // resulting document should be loaded (based on the event flags present when
197 // the link was clicked). This function returns true if the InfoBar should be
198 // closed now or false if it should remain until the user explicitly closes
199 // it.
200 virtual bool LinkClicked(WindowOpenDisposition disposition) {
[email protected]f0a51fb52009-03-05 12:46:38201 return true;
[email protected]5855df72008-12-03 01:36:00202 }
203
204 // Overridden from InfoBarDelegate:
205 virtual InfoBar* CreateInfoBar();
206 virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate() {
207 return this;
208 }
209
210 protected:
211 explicit LinkInfoBarDelegate(TabContents* contents);
212
213 DISALLOW_COPY_AND_ASSIGN(LinkInfoBarDelegate);
214};
215
216// An interface derived from InfoBarDelegate implemented by objects wishing to
[email protected]616ed5a2008-11-21 22:27:24217// control a ConfirmInfoBar.
218class ConfirmInfoBarDelegate : public AlertInfoBarDelegate {
219 public:
220 enum InfoBarButton {
221 BUTTON_NONE = 0,
[email protected]362e00412009-05-02 00:40:02222 BUTTON_OK = 1,
223 BUTTON_CANCEL = 2,
224 // Specifies that the OK button should be rendered like a default button.
225 BUTTON_OK_DEFAULT = 4
[email protected]616ed5a2008-11-21 22:27:24226 };
227
228 // Return the buttons to be shown for this InfoBar.
229 virtual int GetButtons() const {
230 return BUTTON_NONE;
231 }
232
233 // Return the label for the specified button. The default implementation
234 // returns "OK" for the OK button and "Cancel" for the Cancel button.
235 virtual std::wstring GetButtonLabel(InfoBarButton button) const;
236
[email protected]28918392010-03-23 07:09:30237 // Return whether or not the specified button needs elevation.
238 virtual bool NeedElevation(InfoBarButton button) const { return false; }
239
[email protected]818b9252008-11-25 01:55:10240 // Called when the OK button is pressed. If the function returns true, the
241 // InfoBarDelegate should be removed from the associated TabContents.
242 virtual bool Accept() { return true; }
[email protected]616ed5a2008-11-21 22:27:24243
[email protected]818b9252008-11-25 01:55:10244 // Called when the Cancel button is pressed. If the function returns true,
245 // the InfoBarDelegate should be removed from the associated TabContents.
246 virtual bool Cancel() { return true; }
[email protected]616ed5a2008-11-21 22:27:24247
[email protected]ff667b932010-03-18 13:48:02248 // Returns the text of the link to be displayed, if any. Otherwise returns
249 // and empty string.
250 virtual std::wstring GetLinkText() {
251 return std::wstring();
252 }
253
254 // Called when the Link is clicked. The |disposition| specifies how the
255 // resulting document should be loaded (based on the event flags present when
256 // the link was clicked). This function returns true if the InfoBar should be
257 // closed now or false if it should remain until the user explicitly closes
258 // it.
259 // Will only be called if GetLinkText() returns non-empty string.
260 virtual bool LinkClicked(WindowOpenDisposition disposition) {
261 return true;
262 }
263
[email protected]616ed5a2008-11-21 22:27:24264 // Overridden from InfoBarDelegate:
265 virtual InfoBar* CreateInfoBar();
266 virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate() {
267 return this;
268 }
[email protected]f86a07022008-11-25 01:06:05269
270 protected:
271 explicit ConfirmInfoBarDelegate(TabContents* contents);
272
273 DISALLOW_COPY_AND_ASSIGN(ConfirmInfoBarDelegate);
[email protected]616ed5a2008-11-21 22:27:24274};
275
276// Simple implementations for common use cases ---------------------------------
277
278class SimpleAlertInfoBarDelegate : public AlertInfoBarDelegate {
279 public:
[email protected]938e1f92010-04-01 18:09:42280 // |icon| may be |NULL|.
[email protected]f86a07022008-11-25 01:06:05281 SimpleAlertInfoBarDelegate(TabContents* contents,
282 const std::wstring& message,
[email protected]938e1f92010-04-01 18:09:42283 SkBitmap* icon,
284 bool auto_expire);
[email protected]616ed5a2008-11-21 22:27:24285
286 // Overridden from AlertInfoBarDelegate:
[email protected]938e1f92010-04-01 18:09:42287 virtual bool ShouldExpire(
288 const NavigationController::LoadCommittedDetails& details) const;
[email protected]616ed5a2008-11-21 22:27:24289 virtual std::wstring GetMessageText() const;
290 virtual SkBitmap* GetIcon() const;
291 virtual void InfoBarClosed();
292
293 private:
294 std::wstring message_;
295 SkBitmap* icon_;
[email protected]938e1f92010-04-01 18:09:42296 bool auto_expire_; // Should it expire automatically on navigation?
[email protected]616ed5a2008-11-21 22:27:24297
298 DISALLOW_COPY_AND_ASSIGN(SimpleAlertInfoBarDelegate);
299};
300
[email protected]038d52e12009-10-14 16:53:41301#endif // CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_