1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_INFOBARS_CORE_INFOBAR_MANAGER_H_
#define COMPONENTS_INFOBARS_CORE_INFOBAR_MANAGER_H_
#include <stddef.h>
#include <memory>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/observer_list.h"
#include "components/infobars/core/infobar_delegate.h"
class GURL;
class TestInfoBar;
namespace infobars {
class InfoBar;
// Provides access to creating, removing and enumerating info bars
// attached to a tab.
class InfoBarManager {
public:
// Observer class for infobar events.
class Observer {
public:
virtual ~Observer();
virtual void OnInfoBarAdded(InfoBar* infobar);
virtual void OnInfoBarRemoved(InfoBar* infobar, bool animate);
virtual void OnInfoBarReplaced(InfoBar* old_infobar, InfoBar* new_infobar);
virtual void OnManagerShuttingDown(InfoBarManager* manager);
};
InfoBarManager();
InfoBarManager(const InfoBarManager&) = delete;
InfoBarManager& operator=(const InfoBarManager&) = delete;
virtual ~InfoBarManager();
// Must be called before destruction.
// TODO(droger): Merge this method with the destructor once the virtual calls
// for notifications are removed (see http://crbug.com/354380).
void ShutDown();
// Adds the specified |infobar|, which already owns a delegate.
//
// If infobars are disabled for this tab, |infobar| is deleted immediately.
// If the tab already has an infobar whose delegate returns true for
// InfoBarDelegate::EqualsDelegate(infobar->delegate()), depending on the
// value of |replace_existing|, |infobar| is either deleted immediately
// without being added, or is added as replacement for the matching infobar.
//
// Returns the infobar if it was successfully added.
template <typename T>
requires std::derived_from<T, InfoBar>
T* AddInfoBar(std::unique_ptr<T> infobar, bool replace_existing = false) {
return static_cast<T*>(
AddInfoBarInternal(std::move(infobar), replace_existing));
}
// Removes the specified |infobar|. This in turn may close immediately or
// animate closed; at the end the infobar will delete itself.
//
// If infobars are disabled for this tab, this will do nothing, on the
// assumption that the matching AddInfoBar() call will have already deleted
// the infobar (see above).
void RemoveInfoBar(InfoBar* infobar);
// Removes all the infobars.
void RemoveAllInfoBars(bool animate);
// Replaces one infobar with another, without any animation in between. This
// will result in |old_infobar| being synchronously deleted.
//
// If infobars are disabled for this tab, |new_infobar| is deleted immediately
// without being added, and nothing else happens.
//
// Returns the new infobar if it was successfully added.
//
// NOTE: This does not perform any EqualsDelegate() checks like AddInfoBar().
template <typename T>
requires std::derived_from<T, InfoBar>
T* ReplaceInfoBar(InfoBar* old_infobar, std::unique_ptr<T> new_infobar) {
return static_cast<T*>(
ReplaceInfoBarInternal(old_infobar, std::move(new_infobar)));
}
// Returns managed infobars.
const std::vector<raw_ptr<InfoBar, VectorExperimental>>& infobars() const {
return infobars_;
}
// Must be called when a navigation happens.
void OnNavigation(const InfoBarDelegate::NavigationDetails& details);
void AddObserver(Observer* obs);
void RemoveObserver(Observer* obs);
bool animations_enabled() const { return animations_enabled_; }
// Returns the active entry ID.
virtual int GetActiveEntryID() = 0;
// Opens a URL according to the specified |disposition|.
virtual void OpenURL(const GURL& url, WindowOpenDisposition disposition) = 0;
bool ShouldHideInFullscreen() const;
protected:
void set_animations_enabled(bool animations_enabled) {
animations_enabled_ = animations_enabled;
}
bool ShouldShowInfoBar(const InfoBar* infobar) const;
private:
friend class ::TestInfoBar;
InfoBar* AddInfoBarInternal(std::unique_ptr<InfoBar> infobar,
bool replace_existing = false);
InfoBar* ReplaceInfoBarInternal(InfoBar* old_infobar,
std::unique_ptr<InfoBar> new_infobar);
// InfoBars associated with this InfoBarManager. We own these pointers.
// However, this is not a vector of unique_ptr, because we don't delete the
// infobars directly once they've been added to this; instead, when we're
// done with an infobar, we instruct it to delete itself and then orphan it.
// See RemoveInfoBarInternal().
using InfoBars = std::vector<raw_ptr<InfoBar, VectorExperimental>>;
void RemoveInfoBarInternal(InfoBar* infobar, bool animate);
InfoBars infobars_;
bool animations_enabled_ = true;
const bool infobars_enabled_ = true;
base::ObserverList<Observer, true>::Unchecked observer_list_;
};
} // namespace infobars
#endif // COMPONENTS_INFOBARS_CORE_INFOBAR_MANAGER_H_
|