[email protected] | f34e7963 | 2010-03-17 02:34:08 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 5632b20 | 2009-01-16 19:20:56 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ |
| 6 | #define CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 8 | |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 9 | #include "base/basictypes.h" |
[email protected] | 2449241 | 2010-08-15 19:00:19 | [diff] [blame] | 10 | #include "base/string16.h" |
[email protected] | f3ec774 | 2009-01-15 00:59:16 | [diff] [blame] | 11 | #include "chrome/browser/tab_contents/navigation_controller.h" |
[email protected] | e09ba55 | 2009-02-05 03:26:29 | [diff] [blame] | 12 | #include "webkit/glue/window_open_disposition.h" |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 13 | |
| 14 | class AlertInfoBarDelegate; |
| 15 | class ConfirmInfoBarDelegate; |
[email protected] | ad0c2e1b | 2010-01-30 00:00:10 | [diff] [blame] | 16 | class CrashedExtensionInfoBarDelegate; |
[email protected] | f34e7963 | 2010-03-17 02:34:08 | [diff] [blame] | 17 | class ExtensionInfoBarDelegate; |
[email protected] | fb53e5d | 2010-01-27 19:04:55 | [diff] [blame] | 18 | class TranslateInfoBarDelegate; |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 19 | class InfoBar; |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 20 | class LinkInfoBarDelegate; |
[email protected] | 1db6ff15 | 2009-10-12 15:32:07 | [diff] [blame] | 21 | class SkBitmap; |
[email protected] | 47e4c879 | 2009-10-09 20:52:48 | [diff] [blame] | 22 | class ThemeInstalledInfoBarDelegate; |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 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 | // AlertInfoBarDelegate or ConfirmInfoBarDelegate, or override with your own |
| 28 | // delegate for your own InfoBar variety. |
[email protected] | c3409919 | 2009-01-16 22:46:51 | [diff] [blame] | 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 |
[email protected] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 43 | // anymore. |
[email protected] | c3409919 | 2009-01-16 22:46:51 | [diff] [blame] | 44 | // As such, it is generally much safer to dedicate a delegate instance to |
| 45 | // AddInfoBar! |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 46 | class InfoBarDelegate { |
| 47 | public: |
[email protected] | 54bc0925 | 2010-01-31 21:45:17 | [diff] [blame] | 48 | // The type of the infobar. It controls its appearance, such as its background |
[email protected] | c886548 | 2009-07-23 20:40:10 | [diff] [blame] | 49 | // color. |
| 50 | enum Type { |
[email protected] | c886548 | 2009-07-23 20:40:10 | [diff] [blame] | 51 | WARNING_TYPE, |
[email protected] | 96d1ed0c | 2010-08-14 16:33:50 | [diff] [blame] | 52 | PAGE_ACTION_TYPE, |
[email protected] | c886548 | 2009-07-23 20:40:10 | [diff] [blame] | 53 | }; |
| 54 | |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 55 | // 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] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 66 | // 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 70 | |
[email protected] | c886548 | 2009-07-23 20:40:10 | [diff] [blame] | 71 | // Called when the user clicks on the close button to dismiss the infobar. |
| 72 | virtual void InfoBarDismissed() {} |
| 73 | |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 74 | // 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] | 358fbd5 | 2009-04-10 19:26:32 | [diff] [blame] | 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 { return NULL; } |
| 85 | |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 86 | // Returns a pointer to the AlertInfoBarDelegate interface, if implemented. |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 87 | virtual AlertInfoBarDelegate* AsAlertInfoBarDelegate() { |
| 88 | return NULL; |
| 89 | } |
| 90 | |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 91 | // Returns a pointer to the LinkInfoBarDelegate interface, if implemented. |
| 92 | virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate() { |
| 93 | return NULL; |
| 94 | } |
| 95 | |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 96 | // Returns a pointer to the ConfirmInfoBarDelegate interface, if implemented. |
| 97 | virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate() { |
| 98 | return NULL; |
| 99 | } |
[email protected] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 100 | |
[email protected] | 47e4c879 | 2009-10-09 20:52:48 | [diff] [blame] | 101 | // Returns a pointer to the ThemeInstalledInfoBarDelegate interface, if |
[email protected] | 7a691a96 | 2009-07-28 22:57:22 | [diff] [blame] | 102 | // implemented. |
[email protected] | 47e4c879 | 2009-10-09 20:52:48 | [diff] [blame] | 103 | virtual ThemeInstalledInfoBarDelegate* AsThemePreviewInfobarDelegate() { |
[email protected] | 7a691a96 | 2009-07-28 22:57:22 | [diff] [blame] | 104 | return NULL; |
| 105 | } |
| 106 | |
[email protected] | fb53e5d | 2010-01-27 19:04:55 | [diff] [blame] | 107 | // Returns a pointer to the TranslateInfoBarDelegate interface, if |
| 108 | // implemented. |
| 109 | virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate() { |
| 110 | return NULL; |
| 111 | } |
| 112 | |
[email protected] | f34e7963 | 2010-03-17 02:34:08 | [diff] [blame] | 113 | // Returns a pointer to the ExtensionInfoBarDelegate interface, if |
| 114 | // implemented. |
| 115 | virtual ExtensionInfoBarDelegate* AsExtensionInfoBarDelegate() { |
| 116 | return NULL; |
| 117 | } |
| 118 | |
[email protected] | ad0c2e1b | 2010-01-30 00:00:10 | [diff] [blame] | 119 | // Returns a pointer to the CrashedExtensionInfoBarDelegate interface, if |
| 120 | // implemented. |
| 121 | virtual CrashedExtensionInfoBarDelegate* AsCrashedExtensionInfoBarDelegate() { |
| 122 | return NULL; |
| 123 | } |
| 124 | |
[email protected] | c886548 | 2009-07-23 20:40:10 | [diff] [blame] | 125 | // 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] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 131 | protected: |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 132 | // 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] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 135 | explicit InfoBarDelegate(TabContents* contents); |
[email protected] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 136 | |
[email protected] | b618713 | 2009-01-21 23:20:48 | [diff] [blame] | 137 | virtual ~InfoBarDelegate() { } |
| 138 | |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 139 | // 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] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 145 | // 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | // An interface derived from InfoBarDelegate implemented by objects wishing to |
| 153 | // control an AlertInfoBar. |
| 154 | class AlertInfoBarDelegate : public InfoBarDelegate { |
| 155 | public: |
| 156 | // Returns the message string to be displayed for the InfoBar. |
[email protected] | e23d3a3 | 2010-08-13 19:39:58 | [diff] [blame] | 157 | virtual string16 GetMessageText() const = 0; |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 158 | |
[email protected] | 358fbd5 | 2009-04-10 19:26:32 | [diff] [blame] | 159 | // Overridden from InfoBarDelegate. |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 160 | 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] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 166 | |
| 167 | protected: |
| 168 | explicit AlertInfoBarDelegate(TabContents* contents); |
| 169 | |
| 170 | DISALLOW_COPY_AND_ASSIGN(AlertInfoBarDelegate); |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | // An interface derived from InfoBarDelegate implemented by objects wishing to |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 174 | // control a LinkInfoBar. |
| 175 | class 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] | e23d3a3 | 2010-08-13 19:39:58 | [diff] [blame] | 179 | // to string16::npos (it is by default), the link is right aligned within |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 180 | // the InfoBar rather than being embedded in the message text. |
[email protected] | e23d3a3 | 2010-08-13 19:39:58 | [diff] [blame] | 181 | virtual string16 GetMessageTextWithOffset(size_t* link_offset) const { |
| 182 | *link_offset = string16::npos; |
| 183 | return string16(); |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Returns the text of the link to be displayed. |
[email protected] | e23d3a3 | 2010-08-13 19:39:58 | [diff] [blame] | 187 | virtual string16 GetLinkText() const = 0; |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 188 | |
[email protected] | 358fbd5 | 2009-04-10 19:26:32 | [diff] [blame] | 189 | // Overridden from InfoBarDelegate. |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 190 | 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] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 198 | return true; |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 199 | } |
| 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 214 | // control a ConfirmInfoBar. |
| 215 | class ConfirmInfoBarDelegate : public AlertInfoBarDelegate { |
| 216 | public: |
| 217 | enum InfoBarButton { |
| 218 | BUTTON_NONE = 0, |
[email protected] | 362e0041 | 2009-05-02 00:40:02 | [diff] [blame] | 219 | 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 223 | }; |
| 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] | e23d3a3 | 2010-08-13 19:39:58 | [diff] [blame] | 232 | virtual string16 GetButtonLabel(InfoBarButton button) const; |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 233 | |
[email protected] | 2891839 | 2010-03-23 07:09:30 | [diff] [blame] | 234 | // Return whether or not the specified button needs elevation. |
| 235 | virtual bool NeedElevation(InfoBarButton button) const { return false; } |
| 236 | |
[email protected] | 818b925 | 2008-11-25 01:55:10 | [diff] [blame] | 237 | // 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 240 | |
[email protected] | 818b925 | 2008-11-25 01:55:10 | [diff] [blame] | 241 | // 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 244 | |
[email protected] | ff667b93 | 2010-03-18 13:48:02 | [diff] [blame] | 245 | // Returns the text of the link to be displayed, if any. Otherwise returns |
| 246 | // and empty string. |
[email protected] | e23d3a3 | 2010-08-13 19:39:58 | [diff] [blame] | 247 | virtual string16 GetLinkText() { |
| 248 | return string16(); |
[email protected] | ff667b93 | 2010-03-18 13:48:02 | [diff] [blame] | 249 | } |
| 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 261 | // Overridden from InfoBarDelegate: |
| 262 | virtual InfoBar* CreateInfoBar(); |
| 263 | virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate() { |
| 264 | return this; |
| 265 | } |
[email protected] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 266 | |
| 267 | protected: |
| 268 | explicit ConfirmInfoBarDelegate(TabContents* contents); |
| 269 | |
| 270 | DISALLOW_COPY_AND_ASSIGN(ConfirmInfoBarDelegate); |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | // Simple implementations for common use cases --------------------------------- |
| 274 | |
| 275 | class SimpleAlertInfoBarDelegate : public AlertInfoBarDelegate { |
| 276 | public: |
[email protected] | 938e1f9 | 2010-04-01 18:09:42 | [diff] [blame] | 277 | // |icon| may be |NULL|. |
[email protected] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 278 | SimpleAlertInfoBarDelegate(TabContents* contents, |
[email protected] | e23d3a3 | 2010-08-13 19:39:58 | [diff] [blame] | 279 | const string16& message, |
[email protected] | 938e1f9 | 2010-04-01 18:09:42 | [diff] [blame] | 280 | SkBitmap* icon, |
| 281 | bool auto_expire); |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 282 | |
| 283 | // Overridden from AlertInfoBarDelegate: |
[email protected] | 938e1f9 | 2010-04-01 18:09:42 | [diff] [blame] | 284 | virtual bool ShouldExpire( |
| 285 | const NavigationController::LoadCommittedDetails& details) const; |
[email protected] | e23d3a3 | 2010-08-13 19:39:58 | [diff] [blame] | 286 | virtual string16 GetMessageText() const; |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 287 | virtual SkBitmap* GetIcon() const; |
| 288 | virtual void InfoBarClosed(); |
| 289 | |
| 290 | private: |
[email protected] | e23d3a3 | 2010-08-13 19:39:58 | [diff] [blame] | 291 | string16 message_; |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 292 | SkBitmap* icon_; |
[email protected] | 938e1f9 | 2010-04-01 18:09:42 | [diff] [blame] | 293 | bool auto_expire_; // Should it expire automatically on navigation? |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 294 | |
| 295 | DISALLOW_COPY_AND_ASSIGN(SimpleAlertInfoBarDelegate); |
| 296 | }; |
| 297 | |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 298 | #endif // CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ |