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_MANAGER_H_
      6 #define COMPONENTS_INFOBARS_CORE_INFOBAR_MANAGER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/observer_list.h"
     12 #include "components/infobars/core/infobar_delegate.h"
     13 
     14 namespace content {
     15 class WebContents;
     16 }
     17 
     18 namespace infobars {
     19 
     20 class InfoBar;
     21 
     22 // Provides access to creating, removing and enumerating info bars
     23 // attached to a tab.
     24 class InfoBarManager {
     25  public:
     26   // Observer class for infobar events.
     27   class Observer {
     28    public:
     29     virtual void OnInfoBarAdded(InfoBar* infobar);
     30     virtual void OnInfoBarRemoved(InfoBar* infobar, bool animate);
     31     virtual void OnInfoBarReplaced(InfoBar* old_infobar,
     32                                    InfoBar* new_infobar);
     33     virtual void OnManagerShuttingDown(InfoBarManager* manager);
     34   };
     35 
     36   InfoBarManager();
     37   virtual ~InfoBarManager();
     38 
     39   // Must be called before destruction.
     40   // TODO(droger): Merge this method with the destructor once the virtual calls
     41   // for notifications are removed (see http://crbug.com/354380).
     42   void ShutDown();
     43 
     44   // Adds the specified |infobar|, which already owns a delegate.
     45   //
     46   // If infobars are disabled for this tab or the tab already has an infobar
     47   // whose delegate returns true for
     48   // InfoBarDelegate::EqualsDelegate(infobar->delegate()), |infobar| is deleted
     49   // immediately without being added.
     50   //
     51   // Returns the infobar if it was successfully added.
     52   InfoBar* AddInfoBar(scoped_ptr<InfoBar> infobar);
     53 
     54   // Removes the specified |infobar|.  This in turn may close immediately or
     55   // animate closed; at the end the infobar will delete itself.
     56   //
     57   // If infobars are disabled for this tab, this will do nothing, on the
     58   // assumption that the matching AddInfoBar() call will have already deleted
     59   // the infobar (see above).
     60   void RemoveInfoBar(InfoBar* infobar);
     61 
     62   // Removes all the infobars.
     63   void RemoveAllInfoBars(bool animate);
     64 
     65   // Replaces one infobar with another, without any animation in between.  This
     66   // will result in |old_infobar| being synchronously deleted.
     67   //
     68   // If infobars are disabled for this tab, |new_infobar| is deleted immediately
     69   // without being added, and nothing else happens.
     70   //
     71   // Returns the new infobar if it was successfully added.
     72   //
     73   // NOTE: This does not perform any EqualsDelegate() checks like AddInfoBar().
     74   InfoBar* ReplaceInfoBar(InfoBar* old_infobar,
     75                           scoped_ptr<InfoBar> new_infobar);
     76 
     77   // Returns the number of infobars for this tab.
     78   size_t infobar_count() const { return infobars_.size(); }
     79 
     80   // Returns the infobar at the given |index|.  The InfoBarManager retains
     81   // ownership.
     82   //
     83   // Warning: Does not sanity check |index|.
     84   InfoBar* infobar_at(size_t index) { return infobars_[index]; }
     85 
     86   // Must be called when a navigation happens.
     87   void OnNavigation(const InfoBarDelegate::NavigationDetails& details);
     88 
     89   void AddObserver(Observer* obs);
     90   void RemoveObserver(Observer* obs);
     91 
     92   // Returns the active entry ID.
     93   virtual int GetActiveEntryID() = 0;
     94 
     95  protected:
     96   // Notifies the observer in |observer_list_|.
     97   // TODO(droger): Absorb these methods back into their callers once virtual
     98   // overrides are removed (see http://crbug.com/354380).
     99   virtual void NotifyInfoBarAdded(InfoBar* infobar);
    100   virtual void NotifyInfoBarRemoved(InfoBar* infobar, bool animate);
    101 
    102  private:
    103   // InfoBars associated with this InfoBarManager.  We own these pointers.
    104   // However, this is not a ScopedVector, because we don't delete the infobars
    105   // directly once they've been added to this; instead, when we're done with an
    106   // infobar, we instruct it to delete itself and then orphan it.  See
    107   // RemoveInfoBarInternal().
    108   typedef std::vector<InfoBar*> InfoBars;
    109 
    110   void RemoveInfoBarInternal(InfoBar* infobar, bool animate);
    111 
    112   InfoBars infobars_;
    113   bool infobars_enabled_;
    114 
    115   ObserverList<Observer, true> observer_list_;
    116 
    117   DISALLOW_COPY_AND_ASSIGN(InfoBarManager);
    118 };
    119 
    120 }  // namespace infobars
    121 
    122 #endif  // COMPONENTS_INFOBARS_CORE_INFOBAR_MANAGER_H_
    123