[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] | d882cf9 | 2008-11-21 22:35:11 | [diff] [blame] | 9 | #include <string> |
| 10 | |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 11 | #include "base/basictypes.h" |
[email protected] | f3ec774 | 2009-01-15 00:59:16 | [diff] [blame] | 12 | #include "chrome/browser/tab_contents/navigation_controller.h" |
[email protected] | e09ba55 | 2009-02-05 03:26:29 | [diff] [blame] | 13 | #include "webkit/glue/window_open_disposition.h" |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 14 | |
| 15 | class AlertInfoBarDelegate; |
| 16 | class ConfirmInfoBarDelegate; |
[email protected] | ad0c2e1b | 2010-01-30 00:00:10 | [diff] [blame] | 17 | class CrashedExtensionInfoBarDelegate; |
[email protected] | f34e7963 | 2010-03-17 02:34:08 | [diff] [blame] | 18 | class ExtensionInfoBarDelegate; |
[email protected] | fb53e5d | 2010-01-27 19:04:55 | [diff] [blame] | 19 | class TranslateInfoBarDelegate; |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 20 | class InfoBar; |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 21 | class LinkInfoBarDelegate; |
[email protected] | 1db6ff15 | 2009-10-12 15:32:07 | [diff] [blame] | 22 | class SkBitmap; |
[email protected] | 47e4c879 | 2009-10-09 20:52:48 | [diff] [blame] | 23 | class ThemeInstalledInfoBarDelegate; |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 24 | |
| 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] | c3409919 | 2009-01-16 22:46:51 | [diff] [blame] | 30 | // |
| 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] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 44 | // anymore. |
[email protected] | c3409919 | 2009-01-16 22:46:51 | [diff] [blame] | 45 | // As such, it is generally much safer to dedicate a delegate instance to |
| 46 | // AddInfoBar! |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 47 | class InfoBarDelegate { |
| 48 | public: |
[email protected] | 54bc0925 | 2010-01-31 21:45:17 | [diff] [blame] | 49 | // The type of the infobar. It controls its appearance, such as its background |
[email protected] | c886548 | 2009-07-23 20:40:10 | [diff] [blame] | 50 | // color. |
| 51 | enum Type { |
[email protected] | d2ce4e3 | 2010-07-26 14:40:22 | [diff] [blame] | 52 | INFO_TYPE, |
[email protected] | c886548 | 2009-07-23 20:40:10 | [diff] [blame] | 53 | WARNING_TYPE, |
[email protected] | fb53e5d | 2010-01-27 19:04:55 | [diff] [blame] | 54 | ERROR_TYPE, |
| 55 | PAGE_ACTION_TYPE |
[email protected] | c886548 | 2009-07-23 20:40:10 | [diff] [blame] | 56 | }; |
| 57 | |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 58 | // 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] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 69 | // 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 73 | |
[email protected] | c886548 | 2009-07-23 20:40:10 | [diff] [blame] | 74 | // Called when the user clicks on the close button to dismiss the infobar. |
| 75 | virtual void InfoBarDismissed() {} |
| 76 | |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 77 | // 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] | 358fbd5 | 2009-04-10 19:26:32 | [diff] [blame] | 85 | // 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] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 89 | // Returns a pointer to the AlertInfoBarDelegate interface, if implemented. |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 90 | virtual AlertInfoBarDelegate* AsAlertInfoBarDelegate() { |
| 91 | return NULL; |
| 92 | } |
| 93 | |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 94 | // Returns a pointer to the LinkInfoBarDelegate interface, if implemented. |
| 95 | virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate() { |
| 96 | return NULL; |
| 97 | } |
| 98 | |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 99 | // Returns a pointer to the ConfirmInfoBarDelegate interface, if implemented. |
| 100 | virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate() { |
| 101 | return NULL; |
| 102 | } |
[email protected] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 103 | |
[email protected] | 47e4c879 | 2009-10-09 20:52:48 | [diff] [blame] | 104 | // Returns a pointer to the ThemeInstalledInfoBarDelegate interface, if |
[email protected] | 7a691a96 | 2009-07-28 22:57:22 | [diff] [blame] | 105 | // implemented. |
[email protected] | 47e4c879 | 2009-10-09 20:52:48 | [diff] [blame] | 106 | virtual ThemeInstalledInfoBarDelegate* AsThemePreviewInfobarDelegate() { |
[email protected] | 7a691a96 | 2009-07-28 22:57:22 | [diff] [blame] | 107 | return NULL; |
| 108 | } |
| 109 | |
[email protected] | fb53e5d | 2010-01-27 19:04:55 | [diff] [blame] | 110 | // Returns a pointer to the TranslateInfoBarDelegate interface, if |
| 111 | // implemented. |
| 112 | virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate() { |
| 113 | return NULL; |
| 114 | } |
[email protected] | fb53e5d | 2010-01-27 19:04:55 | [diff] [blame] | 115 | |
[email protected] | f34e7963 | 2010-03-17 02:34:08 | [diff] [blame] | 116 | // Returns a pointer to the ExtensionInfoBarDelegate interface, if |
| 117 | // implemented. |
| 118 | virtual ExtensionInfoBarDelegate* AsExtensionInfoBarDelegate() { |
| 119 | return NULL; |
| 120 | } |
| 121 | |
[email protected] | ad0c2e1b | 2010-01-30 00:00:10 | [diff] [blame] | 122 | // Returns a pointer to the CrashedExtensionInfoBarDelegate interface, if |
| 123 | // implemented. |
| 124 | virtual CrashedExtensionInfoBarDelegate* AsCrashedExtensionInfoBarDelegate() { |
| 125 | return NULL; |
| 126 | } |
| 127 | |
[email protected] | c886548 | 2009-07-23 20:40:10 | [diff] [blame] | 128 | // 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] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 134 | protected: |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 135 | // 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] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 138 | explicit InfoBarDelegate(TabContents* contents); |
[email protected] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 139 | |
[email protected] | b618713 | 2009-01-21 23:20:48 | [diff] [blame] | 140 | virtual ~InfoBarDelegate() { } |
| 141 | |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 142 | // 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] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 148 | // 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | // An interface derived from InfoBarDelegate implemented by objects wishing to |
| 156 | // control an AlertInfoBar. |
| 157 | class 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] | 358fbd5 | 2009-04-10 19:26:32 | [diff] [blame] | 162 | // Overridden from InfoBarDelegate. |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 163 | 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] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 169 | |
| 170 | protected: |
| 171 | explicit AlertInfoBarDelegate(TabContents* contents); |
| 172 | |
| 173 | DISALLOW_COPY_AND_ASSIGN(AlertInfoBarDelegate); |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | // An interface derived from InfoBarDelegate implemented by objects wishing to |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 177 | // control a LinkInfoBar. |
| 178 | class 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] | 358fbd5 | 2009-04-10 19:26:32 | [diff] [blame] | 192 | // Overridden from InfoBarDelegate. |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 193 | 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] | f0a51fb5 | 2009-03-05 12:46:38 | [diff] [blame] | 201 | return true; |
[email protected] | 5855df7 | 2008-12-03 01:36:00 | [diff] [blame] | 202 | } |
| 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 217 | // control a ConfirmInfoBar. |
| 218 | class ConfirmInfoBarDelegate : public AlertInfoBarDelegate { |
| 219 | public: |
| 220 | enum InfoBarButton { |
| 221 | BUTTON_NONE = 0, |
[email protected] | 362e0041 | 2009-05-02 00:40:02 | [diff] [blame] | 222 | 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 226 | }; |
| 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] | 2891839 | 2010-03-23 07:09:30 | [diff] [blame] | 237 | // Return whether or not the specified button needs elevation. |
| 238 | virtual bool NeedElevation(InfoBarButton button) const { return false; } |
| 239 | |
[email protected] | 818b925 | 2008-11-25 01:55:10 | [diff] [blame] | 240 | // 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 243 | |
[email protected] | 818b925 | 2008-11-25 01:55:10 | [diff] [blame] | 244 | // 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 247 | |
[email protected] | ff667b93 | 2010-03-18 13:48:02 | [diff] [blame] | 248 | // 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] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 264 | // Overridden from InfoBarDelegate: |
| 265 | virtual InfoBar* CreateInfoBar(); |
| 266 | virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate() { |
| 267 | return this; |
| 268 | } |
[email protected] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 269 | |
| 270 | protected: |
| 271 | explicit ConfirmInfoBarDelegate(TabContents* contents); |
| 272 | |
| 273 | DISALLOW_COPY_AND_ASSIGN(ConfirmInfoBarDelegate); |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | // Simple implementations for common use cases --------------------------------- |
| 277 | |
| 278 | class SimpleAlertInfoBarDelegate : public AlertInfoBarDelegate { |
| 279 | public: |
[email protected] | 938e1f9 | 2010-04-01 18:09:42 | [diff] [blame] | 280 | // |icon| may be |NULL|. |
[email protected] | f86a0702 | 2008-11-25 01:06:05 | [diff] [blame] | 281 | SimpleAlertInfoBarDelegate(TabContents* contents, |
| 282 | const std::wstring& message, |
[email protected] | 938e1f9 | 2010-04-01 18:09:42 | [diff] [blame] | 283 | SkBitmap* icon, |
| 284 | bool auto_expire); |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 285 | |
| 286 | // Overridden from AlertInfoBarDelegate: |
[email protected] | 938e1f9 | 2010-04-01 18:09:42 | [diff] [blame] | 287 | virtual bool ShouldExpire( |
| 288 | const NavigationController::LoadCommittedDetails& details) const; |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 289 | 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] | 938e1f9 | 2010-04-01 18:09:42 | [diff] [blame] | 296 | bool auto_expire_; // Should it expire automatically on navigation? |
[email protected] | 616ed5a | 2008-11-21 22:27:24 | [diff] [blame] | 297 | |
| 298 | DISALLOW_COPY_AND_ASSIGN(SimpleAlertInfoBarDelegate); |
| 299 | }; |
| 300 | |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 301 | #endif // CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ |