infobar_delegate.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ 6#define CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ 7#pragma once 8 9#include "base/basictypes.h" 10#include "base/string16.h" 11#include "chrome/browser/tab_contents/navigation_controller.h" 12#include "webkit/glue/window_open_disposition.h" 13 14class AlertInfoBarDelegate; 15class ConfirmInfoBarDelegate; 16class CrashedExtensionInfoBarDelegate; 17class ExtensionInfoBarDelegate; 18class TranslateInfoBarDelegate; 19class InfoBar; 20class LinkInfoBarDelegate; 21class SkBitmap; 22class ThemeInstalledInfoBarDelegate; 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. 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 43// anymore. 44// As such, it is generally much safer to dedicate a delegate instance to 45// AddInfoBar! 46class InfoBarDelegate { 47 public: 48 // The type of the infobar. It controls its appearance, such as its background 49 // color. 50 enum Type { 51 WARNING_TYPE, 52 PAGE_ACTION_TYPE, 53 }; 54 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 63 // Returns true if the InfoBar should be closed automatically after the page 64 // is navigated. The default behavior is to return true if the page is 65 // navigated somewhere else or reloaded. 66 virtual bool ShouldExpire( 67 const NavigationController::LoadCommittedDetails& details) const; 68 69 // Called when the user clicks on the close button to dismiss the infobar. 70 virtual void InfoBarDismissed() {} 71 72 // Called after the InfoBar is closed. The delegate is free to delete itself 73 // at this point. 74 virtual void InfoBarClosed() {} 75 76 // Called to create the InfoBar. Implementation of this method is 77 // platform-specific. 78 virtual InfoBar* CreateInfoBar() = 0; 79 80 // Return the icon to be shown for this InfoBar. If the returned bitmap is 81 // NULL, no icon is shown. 82 virtual SkBitmap* GetIcon() const; 83 84 // Returns a pointer to the AlertInfoBarDelegate interface, if implemented. 85 virtual AlertInfoBarDelegate* AsAlertInfoBarDelegate(); 86 87 // Returns a pointer to the LinkInfoBarDelegate interface, if implemented. 88 virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate(); 89 90 // Returns a pointer to the ConfirmInfoBarDelegate interface, if implemented. 91 virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate(); 92 93 // Returns a pointer to the ThemeInstalledInfoBarDelegate interface, if 94 // implemented. 95 virtual ThemeInstalledInfoBarDelegate* AsThemePreviewInfobarDelegate(); 96 97 // Returns a pointer to the TranslateInfoBarDelegate interface, if 98 // implemented. 99 virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate(); 100 101 // Returns a pointer to the ExtensionInfoBarDelegate interface, if 102 // implemented. 103 virtual ExtensionInfoBarDelegate* AsExtensionInfoBarDelegate(); 104 105 // Returns a pointer to the CrashedExtensionInfoBarDelegate interface, if 106 // implemented. 107 virtual CrashedExtensionInfoBarDelegate* AsCrashedExtensionInfoBarDelegate(); 108 109 // Returns the type of the infobar. The type determines the appearance (such 110 // as background color) of the infobar. 111 virtual Type GetInfoBarType(); 112 113 protected: 114 // Provided to subclasses as a convenience to initialize the state of this 115 // object. If |contents| is non-NULL, its active entry's unique ID will be 116 // stored using StoreActiveEntryUniqueID automatically. 117 explicit InfoBarDelegate(TabContents* contents); 118 119 virtual ~InfoBarDelegate() { } 120 121 // Store the unique id for the active entry in the specified TabContents, to 122 // be used later upon navigation to determine if this InfoBarDelegate should 123 // be expired from |contents_|. 124 void StoreActiveEntryUniqueID(TabContents* contents); 125 126 private: 127 // The unique id of the active NavigationEntry of the TabContents taht we were 128 // opened for. Used to help expire on navigations. 129 int contents_unique_id_; 130 131 DISALLOW_COPY_AND_ASSIGN(InfoBarDelegate); 132}; 133 134// An interface derived from InfoBarDelegate implemented by objects wishing to 135// control an AlertInfoBar. 136class AlertInfoBarDelegate : public InfoBarDelegate { 137 public: 138 // Returns the message string to be displayed for the InfoBar. 139 virtual string16 GetMessageText() const = 0; 140 141 // Overridden from InfoBarDelegate. 142 virtual SkBitmap* GetIcon() const; 143 144 // Overridden from InfoBarDelegate: 145 virtual bool EqualsDelegate(InfoBarDelegate* delegate) const; 146 virtual InfoBar* CreateInfoBar(); 147 virtual AlertInfoBarDelegate* AsAlertInfoBarDelegate(); 148 149 protected: 150 explicit AlertInfoBarDelegate(TabContents* contents); 151 152 DISALLOW_COPY_AND_ASSIGN(AlertInfoBarDelegate); 153}; 154 155// An interface derived from InfoBarDelegate implemented by objects wishing to 156// control a LinkInfoBar. 157class LinkInfoBarDelegate : public InfoBarDelegate { 158 public: 159 // Returns the message string to be displayed in the InfoBar. |link_offset| 160 // is the position where the link should be inserted. If |link_offset| is set 161 // to string16::npos (it is by default), the link is right aligned within 162 // the InfoBar rather than being embedded in the message text. 163 virtual string16 GetMessageTextWithOffset(size_t* link_offset) const; 164 165 // Returns the text of the link to be displayed. 166 virtual string16 GetLinkText() const = 0; 167 168 // Overridden from InfoBarDelegate. 169 virtual SkBitmap* GetIcon() const; 170 171 // Called when the Link is clicked. The |disposition| specifies how the 172 // resulting document should be loaded (based on the event flags present when 173 // the link was clicked). This function returns true if the InfoBar should be 174 // closed now or false if it should remain until the user explicitly closes 175 // it. 176 virtual bool LinkClicked(WindowOpenDisposition disposition); 177 178 // Overridden from InfoBarDelegate: 179 virtual InfoBar* CreateInfoBar(); 180 virtual LinkInfoBarDelegate* AsLinkInfoBarDelegate(); 181 182 protected: 183 explicit LinkInfoBarDelegate(TabContents* contents); 184 185 DISALLOW_COPY_AND_ASSIGN(LinkInfoBarDelegate); 186}; 187 188// An interface derived from InfoBarDelegate implemented by objects wishing to 189// control a ConfirmInfoBar. 190class ConfirmInfoBarDelegate : public AlertInfoBarDelegate { 191 public: 192 enum InfoBarButton { 193 BUTTON_NONE = 0, 194 BUTTON_OK = 1, 195 BUTTON_CANCEL = 2, 196 // Specifies that the OK button should be rendered like a default button. 197 BUTTON_OK_DEFAULT = 4 198 }; 199 200 // Return the buttons to be shown for this InfoBar. 201 virtual int GetButtons() const; 202 203 // Return the label for the specified button. The default implementation 204 // returns "OK" for the OK button and "Cancel" for the Cancel button. 205 virtual string16 GetButtonLabel(InfoBarButton button) const; 206 207 // Return whether or not the specified button needs elevation. 208 virtual bool NeedElevation(InfoBarButton button) const; 209 210 // Called when the OK button is pressed. If the function returns true, the 211 // InfoBarDelegate should be removed from the associated TabContents. 212 virtual bool Accept(); 213 214 // Called when the Cancel button is pressed. If the function returns true, 215 // the InfoBarDelegate should be removed from the associated TabContents. 216 virtual bool Cancel(); 217 218 // Returns the text of the link to be displayed, if any. Otherwise returns 219 // and empty string. 220 virtual string16 GetLinkText(); 221 222 // Called when the Link is clicked. The |disposition| specifies how the 223 // resulting document should be loaded (based on the event flags present when 224 // the link was clicked). This function returns true if the InfoBar should be 225 // closed now or false if it should remain until the user explicitly closes 226 // it. 227 // Will only be called if GetLinkText() returns non-empty string. 228 virtual bool LinkClicked(WindowOpenDisposition disposition); 229 230 // Overridden from InfoBarDelegate: 231 virtual InfoBar* CreateInfoBar(); 232 virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate(); 233 234 protected: 235 explicit ConfirmInfoBarDelegate(TabContents* contents); 236 237 DISALLOW_COPY_AND_ASSIGN(ConfirmInfoBarDelegate); 238}; 239 240// Simple implementations for common use cases --------------------------------- 241 242class SimpleAlertInfoBarDelegate : public AlertInfoBarDelegate { 243 public: 244 // |icon| may be |NULL|. 245 SimpleAlertInfoBarDelegate(TabContents* contents, 246 const string16& message, 247 SkBitmap* icon, 248 bool auto_expire); 249 250 // Overridden from AlertInfoBarDelegate: 251 virtual bool ShouldExpire( 252 const NavigationController::LoadCommittedDetails& details) const; 253 virtual string16 GetMessageText() const; 254 virtual SkBitmap* GetIcon() const; 255 virtual void InfoBarClosed(); 256 257 private: 258 string16 message_; 259 SkBitmap* icon_; 260 bool auto_expire_; // Should it expire automatically on navigation? 261 262 DISALLOW_COPY_AND_ASSIGN(SimpleAlertInfoBarDelegate); 263}; 264 265#endif // CHROME_BROWSER_TAB_CONTENTS_INFOBAR_DELEGATE_H_ 266