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.
5 #ifndef COMPONENTS_INFOBARS_CORE_INFOBAR_MANAGER_H_
6 #define COMPONENTS_INFOBARS_CORE_INFOBAR_MANAGER_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/observer_list.h"
12 #include "components/infobars/core/infobar_delegate.h"
22 // Provides access to creating, removing and enumerating info bars
24 class InfoBarManager {
26 // Observer class for infobar events.
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);
37 virtual ~InfoBarManager();
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).
44 // Adds the specified |infobar|, which already owns a delegate.
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.
51 // Returns the infobar if it was successfully added.
52 InfoBar* AddInfoBar(scoped_ptr<InfoBar> infobar);
54 // Removes the specified |infobar|. This in turn may close immediately or
55 // animate closed; at the end the infobar will delete itself.
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);
62 // Removes all the infobars.
63 void RemoveAllInfoBars(bool animate);
65 // Replaces one infobar with another, without any animation in between. This
66 // will result in |old_infobar| being synchronously deleted.
68 // If infobars are disabled for this tab, |new_infobar| is deleted immediately
69 // without being added, and nothing else happens.
71 // Returns the new infobar if it was successfully added.
73 // NOTE: This does not perform any EqualsDelegate() checks like AddInfoBar().
74 InfoBar* ReplaceInfoBar(InfoBar* old_infobar,
75 scoped_ptr<InfoBar> new_infobar);
77 // Returns the number of infobars for this tab.
78 size_t infobar_count() const { return infobars_.size(); }
80 // Returns the infobar at the given |index|. The InfoBarManager retains
83 // Warning: Does not sanity check |index|.
84 InfoBar* infobar_at(size_t index) { return infobars_[index]; }
86 // Must be called when a navigation happens.
87 void OnNavigation(const InfoBarDelegate::NavigationDetails& details);
89 void AddObserver(Observer* obs);
90 void RemoveObserver(Observer* obs);
92 // Returns the active entry ID.
93 virtual int GetActiveEntryID() = 0;
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 virtual void NotifyInfoBarReplaced(InfoBar* old_infobar,
102 InfoBar* new_infobar);
105 // InfoBars associated with this InfoBarManager. We own these pointers.
106 // However, this is not a ScopedVector, because we don't delete the infobars
107 // directly once they've been added to this; instead, when we're done with an
108 // infobar, we instruct it to delete itself and then orphan it. See
109 // RemoveInfoBarInternal().
110 typedef std::vector<InfoBar*> InfoBars;
112 void RemoveInfoBarInternal(InfoBar* infobar, bool animate);
115 bool infobars_enabled_;
117 ObserverList<Observer, true> observer_list_;
119 DISALLOW_COPY_AND_ASSIGN(InfoBarManager);
122 } // namespace infobars
124 #endif // COMPONENTS_INFOBARS_CORE_INFOBAR_MANAGER_H_