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"
14 class ConfirmInfoBarDelegate
;
24 // Provides access to creating, removing and enumerating info bars
26 class InfoBarManager
{
28 // Observer class for infobar events.
33 virtual void OnInfoBarAdded(InfoBar
* infobar
);
34 virtual void OnInfoBarRemoved(InfoBar
* infobar
, bool animate
);
35 virtual void OnInfoBarReplaced(InfoBar
* old_infobar
,
36 InfoBar
* new_infobar
);
37 virtual void OnManagerShuttingDown(InfoBarManager
* manager
);
41 virtual ~InfoBarManager();
43 // Must be called before destruction.
44 // TODO(droger): Merge this method with the destructor once the virtual calls
45 // for notifications are removed (see http://crbug.com/354380).
48 // Adds the specified |infobar|, which already owns a delegate.
50 // If infobars are disabled for this tab or the tab already has an infobar
51 // whose delegate returns true for
52 // InfoBarDelegate::EqualsDelegate(infobar->delegate()), |infobar| is deleted
53 // immediately without being added.
55 // Returns the infobar if it was successfully added.
56 InfoBar
* AddInfoBar(scoped_ptr
<InfoBar
> infobar
);
58 // Removes the specified |infobar|. This in turn may close immediately or
59 // animate closed; at the end the infobar will delete itself.
61 // If infobars are disabled for this tab, this will do nothing, on the
62 // assumption that the matching AddInfoBar() call will have already deleted
63 // the infobar (see above).
64 void RemoveInfoBar(InfoBar
* infobar
);
66 // Removes all the infobars.
67 void RemoveAllInfoBars(bool animate
);
69 // Replaces one infobar with another, without any animation in between. This
70 // will result in |old_infobar| being synchronously deleted.
72 // If infobars are disabled for this tab, |new_infobar| is deleted immediately
73 // without being added, and nothing else happens.
75 // Returns the new infobar if it was successfully added.
77 // NOTE: This does not perform any EqualsDelegate() checks like AddInfoBar().
78 InfoBar
* ReplaceInfoBar(InfoBar
* old_infobar
,
79 scoped_ptr
<InfoBar
> new_infobar
);
81 // Returns the number of infobars for this tab.
82 size_t infobar_count() const { return infobars_
.size(); }
84 // Returns the infobar at the given |index|. The InfoBarManager retains
87 // Warning: Does not sanity check |index|.
88 InfoBar
* infobar_at(size_t index
) { return infobars_
[index
]; }
90 // Must be called when a navigation happens.
91 void OnNavigation(const InfoBarDelegate::NavigationDetails
& details
);
93 void AddObserver(Observer
* obs
);
94 void RemoveObserver(Observer
* obs
);
96 // Returns the active entry ID.
97 virtual int GetActiveEntryID() = 0;
99 // Returns a confirm infobar that owns |delegate|.
100 virtual scoped_ptr
<infobars::InfoBar
> CreateConfirmInfoBar(
101 scoped_ptr
<ConfirmInfoBarDelegate
> delegate
) = 0;
104 // Notifies the observer in |observer_list_|.
105 // TODO(droger): Absorb these methods back into their callers once virtual
106 // overrides are removed (see http://crbug.com/354380).
107 virtual void NotifyInfoBarAdded(InfoBar
* infobar
);
108 virtual void NotifyInfoBarRemoved(InfoBar
* infobar
, bool animate
);
111 // InfoBars associated with this InfoBarManager. We own these pointers.
112 // However, this is not a ScopedVector, because we don't delete the infobars
113 // directly once they've been added to this; instead, when we're done with an
114 // infobar, we instruct it to delete itself and then orphan it. See
115 // RemoveInfoBarInternal().
116 typedef std::vector
<InfoBar
*> InfoBars
;
118 void RemoveInfoBarInternal(InfoBar
* infobar
, bool animate
);
121 bool infobars_enabled_
;
123 base::ObserverList
<Observer
, true> observer_list_
;
125 DISALLOW_COPY_AND_ASSIGN(InfoBarManager
);
128 } // namespace infobars
130 #endif // COMPONENTS_INFOBARS_CORE_INFOBAR_MANAGER_H_