Home | History | Annotate | Download | only in core
      1 // Copyright 2014 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 COMPONENTS_INFOBARS_CORE_INFOBAR_DELEGATE_H_
      6 #define COMPONENTS_INFOBARS_CORE_INFOBAR_DELEGATE_H_
      7 
      8 #include "base/basictypes.h"
      9 #include "base/strings/string16.h"
     10 #include "ui/base/window_open_disposition.h"
     11 
     12 class AutoLoginInfoBarDelegate;
     13 class ConfirmInfoBarDelegate;
     14 class ExtensionInfoBarDelegate;
     15 class InsecureContentInfoBarDelegate;
     16 class MediaStreamInfoBarDelegate;
     17 class PopupBlockedInfoBarDelegate;
     18 class RegisterProtocolHandlerInfoBarDelegate;
     19 class ScreenCaptureInfoBarDelegate;
     20 class ThemeInstalledInfoBarDelegate;
     21 class ThreeDAPIInfoBarDelegate;
     22 class TranslateInfoBarDelegate;
     23 
     24 namespace gfx {
     25 class Image;
     26 }
     27 
     28 namespace infobars {
     29 
     30 class InfoBar;
     31 
     32 // An interface implemented by objects wishing to control an InfoBar.
     33 // Implementing this interface is not sufficient to use an InfoBar, since it
     34 // does not map to a specific InfoBar type. Instead, you must implement
     35 // ConfirmInfoBarDelegate, or override with your own delegate for your own
     36 // InfoBar variety.
     37 class InfoBarDelegate {
     38  public:
     39   // The type of the infobar. It controls its appearance, such as its background
     40   // color.
     41   enum Type {
     42     WARNING_TYPE,
     43     PAGE_ACTION_TYPE,
     44   };
     45 
     46   enum InfoBarAutomationType {
     47     CONFIRM_INFOBAR,
     48     PASSWORD_INFOBAR,
     49     RPH_INFOBAR,
     50     UNKNOWN_INFOBAR,
     51   };
     52 
     53   // Describes navigation events, used to decide whether infobars should be
     54   // dismissed.
     55   struct NavigationDetails {
     56     // Unique identifier for the entry.
     57     int entry_id;
     58     // True if it is a navigation to a different page (as opposed to in-page).
     59     bool is_navigation_to_different_page;
     60     // True if the entry replaced the existing one.
     61     bool did_replace_entry;
     62     // True for the main frame, false for a sub-frame.
     63     bool is_main_frame;
     64     bool is_reload;
     65     bool is_redirect;
     66   };
     67 
     68   // Value to use when the InfoBar has no icon to show.
     69   static const int kNoIconID;
     70 
     71   // Called when the InfoBar that owns this delegate is being destroyed.  At
     72   // this point nothing is visible onscreen.
     73   virtual ~InfoBarDelegate();
     74 
     75   virtual InfoBarAutomationType GetInfoBarAutomationType() const;
     76 
     77   // Returns true if the supplied |delegate| is equal to this one. Equality is
     78   // left to the implementation to define. This function is called by the
     79   // InfoBarManager when determining whether or not a delegate should be
     80   // added because a matching one already exists. If this function returns true,
     81   // the InfoBarManager will not add the new delegate because it considers
     82   // one to already be present.
     83   virtual bool EqualsDelegate(InfoBarDelegate* delegate) const;
     84 
     85   // Returns true if the InfoBar should be closed automatically after the page
     86   // is navigated. By default this returns true if the navigation is to a new
     87   // page (not including reloads).  Subclasses wishing to change this behavior
     88   // can override either this function or ShouldExpireInternal(), depending on
     89   // what level of control they need.
     90   virtual bool ShouldExpire(const NavigationDetails& details) const;
     91 
     92   // Called when the user clicks on the close button to dismiss the infobar.
     93   virtual void InfoBarDismissed();
     94 
     95   // Return the resource ID of the icon to be shown for this InfoBar.  If the
     96   // value is equal to |kNoIconID|, no icon is shown.
     97   virtual int GetIconID() const;
     98 
     99   // Returns the type of the infobar.  The type determines the appearance (such
    100   // as background color) of the infobar.
    101   virtual Type GetInfoBarType() const;
    102 
    103   // Type-checking downcast routines:
    104   virtual AutoLoginInfoBarDelegate* AsAutoLoginInfoBarDelegate();
    105   virtual ConfirmInfoBarDelegate* AsConfirmInfoBarDelegate();
    106   virtual ExtensionInfoBarDelegate* AsExtensionInfoBarDelegate();
    107   virtual InsecureContentInfoBarDelegate* AsInsecureContentInfoBarDelegate();
    108   virtual MediaStreamInfoBarDelegate* AsMediaStreamInfoBarDelegate();
    109   virtual PopupBlockedInfoBarDelegate* AsPopupBlockedInfoBarDelegate();
    110   virtual RegisterProtocolHandlerInfoBarDelegate*
    111       AsRegisterProtocolHandlerInfoBarDelegate();
    112   virtual ScreenCaptureInfoBarDelegate* AsScreenCaptureInfoBarDelegate();
    113   virtual ThemeInstalledInfoBarDelegate* AsThemePreviewInfobarDelegate();
    114   virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate();
    115 
    116   void set_infobar(InfoBar* infobar) { infobar_ = infobar; }
    117 
    118   // Store the unique id for the active entry, to be used later upon navigation
    119   // to determine if this InfoBarDelegate should be expired.
    120   void StoreActiveEntryUniqueID();
    121 
    122   // Return the icon to be shown for this InfoBar. If the returned Image is
    123   // empty, no icon is shown.
    124   virtual gfx::Image GetIcon() const;
    125 
    126  protected:
    127   InfoBarDelegate();
    128 
    129   // Returns true if the navigation is to a new URL or a reload occured.
    130   virtual bool ShouldExpireInternal(const NavigationDetails& details) const;
    131 
    132   int contents_unique_id() const { return contents_unique_id_; }
    133   InfoBar* infobar() { return infobar_; }
    134 
    135  private:
    136   // The unique id of the active NavigationEntry of the WebContents that we were
    137   // opened for. Used to help expire on navigations.
    138   int contents_unique_id_;
    139 
    140   // The InfoBar associated with us.
    141   InfoBar* infobar_;
    142 
    143   DISALLOW_COPY_AND_ASSIGN(InfoBarDelegate);
    144 };
    145 
    146 }  // namespace infobars
    147 
    148 #endif  // COMPONENTS_INFOBARS_CORE_INFOBAR_DELEGATE_H_
    149