Home | History | Annotate | Download | only in infobars
      1 // Copyright (c) 2011 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_UI_VIEWS_INFOBARS_INFOBAR_CONTAINER_H_
      6 #define CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_CONTAINER_H_
      7 #pragma once
      8 
      9 #include <vector>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "content/common/notification_observer.h"
     13 #include "content/common/notification_registrar.h"
     14 #include "third_party/skia/include/core/SkColor.h"
     15 
     16 class InfoBar;
     17 class InfoBarDelegate;
     18 class TabContents;
     19 
     20 // InfoBarContainer is a cross-platform base class to handle the visibility-
     21 // related aspects of InfoBars.  While InfoBars own themselves, the
     22 // InfoBarContainer is responsible for telling particular InfoBars that they
     23 // should be hidden or visible.
     24 //
     25 // Platforms need to subclass this to implement a few platform-specific
     26 // functions, which are pure virtual here.
     27 class InfoBarContainer : public NotificationObserver {
     28  public:
     29   class Delegate {
     30    public:
     31     // The separator color may vary depending on where the container is hosted.
     32     virtual SkColor GetInfoBarSeparatorColor() const = 0;
     33 
     34     // The delegate is notified each time the infobar container changes height,
     35     // as well as when it stops animating.
     36     virtual void InfoBarContainerStateChanged(bool is_animating) = 0;
     37 
     38     // The delegate needs to tell us whether "unspoofable" arrows should be
     39     // drawn, and if so, at what |x| coordinate.  |x| may be NULL.
     40     virtual bool DrawInfoBarArrows(int* x) const = 0;
     41 
     42    protected:
     43     virtual ~Delegate();
     44   };
     45 
     46   explicit InfoBarContainer(Delegate* delegate);
     47   virtual ~InfoBarContainer();
     48 
     49   // Changes the TabContents for which this container is showing infobars.  This
     50   // will remove all current infobars from the container, add the infobars from
     51   // |contents|, and show them all.  |contents| may be NULL.
     52   void ChangeTabContents(TabContents* contents);
     53 
     54   // Returns the amount by which to overlap the toolbar above, and, when
     55   // |total_height| is non-NULL, set it to the height of the InfoBarContainer
     56   // (including overlap).
     57   int GetVerticalOverlap(int* total_height);
     58 
     59   // Called by the delegate when the distance between what the top infobar's
     60   // "unspoofable" arrow would point to and the top infobar itself changes.
     61   // This enables the top infobar to show a longer arrow (e.g. because of a
     62   // visible bookmark bar) or shorter (e.g. due to being in a popup window) if
     63   // desired.
     64   //
     65   // IMPORTANT: This MUST NOT result in a call back to
     66   // Delegate::InfoBarContainerStateChanged() unless it causes an actual
     67   // change, lest we infinitely recurse.
     68   void SetMaxTopArrowHeight(int height);
     69 
     70   // Called when a contained infobar has animated or by some other means changed
     71   // its height, or when it stops animating.  The container is expected to do
     72   // anything necessary to respond, e.g. re-layout.
     73   void OnInfoBarStateChanged(bool is_animating);
     74 
     75   // Removes the specified InfoBarDelegate from the selected TabContents.  This
     76   // will notify us back and cause us to close the InfoBar.  This is called from
     77   // the InfoBar's close button handler.
     78   void RemoveDelegate(InfoBarDelegate* delegate);
     79 
     80   // Called by |infobar| to request that it be removed from the container, as it
     81   // is about to delete itself.  At this point, |infobar| should already be
     82   // hidden.
     83   void RemoveInfoBar(InfoBar* infobar);
     84 
     85   const Delegate* delegate() const { return delegate_; }
     86 
     87  protected:
     88   // Subclasses must call this during destruction, so that we can remove
     89   // infobars (which will call the pure virtual functions below) while the
     90   // subclass portion of |this| has not yet been destroyed.
     91   void RemoveAllInfoBarsForDestruction();
     92 
     93   // These must be implemented on each platform to e.g. adjust the visible
     94   // object hierarchy.
     95   virtual void PlatformSpecificAddInfoBar(InfoBar* infobar) = 0;
     96   virtual void PlatformSpecificRemoveInfoBar(InfoBar* infobar) = 0;
     97 
     98  private:
     99   typedef std::vector<InfoBar*> InfoBars;
    100 
    101   // NotificationObserver:
    102   virtual void Observe(NotificationType type,
    103                        const NotificationSource& source,
    104                        const NotificationDetails& details) OVERRIDE;
    105 
    106   // Removes an InfoBar for the specified delegate, in response to a
    107   // notification from the selected TabContents. The InfoBar's disappearance
    108   // will be animated if |use_animation| is true.
    109   void RemoveInfoBar(InfoBarDelegate* delegate, bool use_animation);
    110 
    111   // Adds |infobar| to this container and calls Show() on it.  |animate| is
    112   // passed along to infobar->Show().  Depending on the value of
    113   // |callback_status|, this calls infobar->set_container(this) either before or
    114   // after the call to Show() so that OnInfoBarAnimated() either will or won't
    115   // be called as a result.
    116   enum CallbackStatus { NO_CALLBACK, WANT_CALLBACK };
    117   void AddInfoBar(InfoBar* infobar,
    118                   bool animate,
    119                   CallbackStatus callback_status);
    120 
    121   void UpdateInfoBarArrowTargetHeights();
    122   int ArrowTargetHeightForInfoBar(size_t infobar_index) const;
    123 
    124   NotificationRegistrar registrar_;
    125   Delegate* delegate_;
    126   TabContents* tab_contents_;
    127   InfoBars infobars_;
    128 
    129   // Calculated in SetMaxTopArrowHeight().
    130   int top_arrow_target_height_;
    131 
    132   DISALLOW_COPY_AND_ASSIGN(InfoBarContainer);
    133 };
    134 
    135 #endif  // CHROME_BROWSER_UI_VIEWS_INFOBARS_INFOBAR_CONTAINER_H_
    136