infobar_container.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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_CONTAINER_H_
  5. #define COMPONENTS_INFOBARS_CORE_INFOBAR_CONTAINER_H_
  6. #include <stddef.h>
  7. #include <vector>
  8. #include "base/compiler_specific.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "components/infobars/core/infobar_manager.h"
  11. #include "third_party/skia/include/core/SkColor.h"
  12. namespace infobars {
  13. class InfoBar;
  14. // InfoBarContainer is a cross-platform base class to handle the visibility-
  15. // related aspects of InfoBars. While InfoBarManager owns the InfoBars, the
  16. // InfoBarContainer is responsible for telling particular InfoBars that they
  17. // should be hidden or visible.
  18. //
  19. // Platforms need to subclass this to implement a few platform-specific
  20. // functions, which are pure virtual here.
  21. class InfoBarContainer : public InfoBarManager::Observer {
  22. public:
  23. class Delegate {
  24. public:
  25. // The delegate is notified each time the infobar container changes height,
  26. // as well as when it stops animating.
  27. virtual void InfoBarContainerStateChanged(bool is_animating) = 0;
  28. protected:
  29. virtual ~Delegate();
  30. };
  31. explicit InfoBarContainer(Delegate* delegate);
  32. InfoBarContainer(const InfoBarContainer&) = delete;
  33. InfoBarContainer& operator=(const InfoBarContainer&) = delete;
  34. ~InfoBarContainer() override;
  35. // Changes the InfoBarManager for which this container is showing infobars.
  36. // This will hide all current infobars, remove them from the container, add
  37. // the infobars from |infobar_manager|, and show them all. |infobar_manager|
  38. // may be NULL.
  39. void ChangeInfoBarManager(InfoBarManager* infobar_manager);
  40. // Called when a contained infobar has animated or by some other means changed
  41. // its height, or when it stops animating. The container is expected to do
  42. // anything necessary to respond, e.g. re-layout.
  43. void OnInfoBarStateChanged(bool is_animating);
  44. // Called by |infobar| to request that it be removed from the container. At
  45. // this point, |infobar| should already be hidden.
  46. void RemoveInfoBar(InfoBar* infobar);
  47. const Delegate* delegate() const { return delegate_; }
  48. protected:
  49. // Subclasses must call this during destruction, so that we can remove
  50. // infobars (which will call the pure virtual functions below) while the
  51. // subclass portion of |this| has not yet been destroyed.
  52. void RemoveAllInfoBarsForDestruction();
  53. // These must be implemented on each platform to e.g. adjust the visible
  54. // object hierarchy. The first two functions should each be called exactly
  55. // once during an infobar's life (see comments on RemoveInfoBar() and
  56. // AddInfoBar()).
  57. virtual void PlatformSpecificAddInfoBar(InfoBar* infobar,
  58. size_t position) = 0;
  59. // TODO(miguelg): Remove this; it is only necessary for Android, and only
  60. // until the translate infobar is implemented as three different infobars like
  61. // GTK does.
  62. virtual void PlatformSpecificReplaceInfoBar(InfoBar* old_infobar,
  63. InfoBar* new_infobar) {}
  64. virtual void PlatformSpecificRemoveInfoBar(InfoBar* infobar) = 0;
  65. virtual void PlatformSpecificInfoBarStateChanged(bool is_animating) {}
  66. private:
  67. typedef std::vector<InfoBar*> InfoBars;
  68. // InfoBarManager::Observer:
  69. void OnInfoBarAdded(InfoBar* infobar) override;
  70. void OnInfoBarRemoved(InfoBar* infobar, bool animate) override;
  71. void OnInfoBarReplaced(InfoBar* old_infobar, InfoBar* new_infobar) override;
  72. void OnManagerShuttingDown(InfoBarManager* manager) override;
  73. // Adds |infobar| to this container before the existing infobar at position
  74. // |position| and calls Show() on it. |animate| is passed along to
  75. // infobar->Show().
  76. void AddInfoBar(InfoBar* infobar, size_t position, bool animate);
  77. raw_ptr<Delegate> delegate_;
  78. raw_ptr<InfoBarManager> infobar_manager_;
  79. InfoBars infobars_;
  80. // Normally false. When true, OnInfoBarStateChanged() becomes a no-op. We
  81. // use this to ensure that ChangeInfoBarManager() only executes the
  82. // functionality in OnInfoBarStateChanged() once, to minimize unnecessary
  83. // layout and painting.
  84. bool ignore_infobar_state_changed_;
  85. };
  86. } // namespace infobars
  87. #endif // COMPONENTS_INFOBARS_CORE_INFOBAR_CONTAINER_H_