infobar_manager.h 4.5 KB

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