1 // Copyright (c) 2011 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 CHROME_BROWSER_INFOBARS_INFOBAR_CONTAINER_H_
6 #define CHROME_BROWSER_INFOBARS_INFOBAR_CONTAINER_H_
10 #include "base/compiler_specific.h"
11 #include "base/time.h"
12 #include "chrome/browser/ui/search/search_model_observer.h"
13 #include "chrome/browser/ui/search/search_types.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "third_party/skia/include/core/SkColor.h"
19 class InfoBarDelegate
;
20 class InfoBarTabHelper
;
28 // InfoBarContainer is a cross-platform base class to handle the visibility-
29 // related aspects of InfoBars. While InfoBars own themselves, the
30 // InfoBarContainer is responsible for telling particular InfoBars that they
31 // should be hidden or visible.
33 // Platforms need to subclass this to implement a few platform-specific
34 // functions, which are pure virtual here.
36 // This class also observes changes to the SearchModel mode. If the user changes
37 // into suggestions mode, it hides all the infobars temporarily. When the user
38 // changes back out of suggestions mode, it reshows any infobars, and starts a
39 // 50 ms window during which any attempts to re-hide any infobars are handled
40 // without animation. This prevents glitchy-looking behavior when the user
41 // navigates following a mode change, which otherwise would re-show the infobars
42 // only to instantly animate them closed. The window is canceled if a tab
44 class InfoBarContainer
: public content::NotificationObserver
,
45 public chrome::search::SearchModelObserver
{
49 // The separator color may vary depending on where the container is hosted.
50 virtual SkColor
GetInfoBarSeparatorColor() const = 0;
52 // The delegate is notified each time the infobar container changes height,
53 // as well as when it stops animating.
54 virtual void InfoBarContainerStateChanged(bool is_animating
) = 0;
56 // The delegate needs to tell us whether "unspoofable" arrows should be
57 // drawn, and if so, at what |x| coordinate. |x| may be NULL.
58 virtual bool DrawInfoBarArrows(int* x
) const = 0;
64 // |search_model| may be NULL if this class is used in a window that does not
65 // support Instant Extended.
66 InfoBarContainer(Delegate
* delegate
,
67 chrome::search::SearchModel
* search_model
);
68 virtual ~InfoBarContainer();
70 // Changes the InfoBarTabHelper for which this container is showing
71 // infobars. This will remove all current infobars from the container, add
72 // the infobars from |contents|, and show them all. |contents| may be NULL.
73 void ChangeTabContents(InfoBarTabHelper
* tab_helper
);
75 // Returns the amount by which to overlap the toolbar above, and, when
76 // |total_height| is non-NULL, set it to the height of the InfoBarContainer
77 // (including overlap).
78 int GetVerticalOverlap(int* total_height
);
80 // Called by the delegate when the distance between what the top infobar's
81 // "unspoofable" arrow would point to and the top infobar itself changes.
82 // This enables the top infobar to show a longer arrow (e.g. because of a
83 // visible bookmark bar) or shorter (e.g. due to being in a popup window) if
86 // IMPORTANT: This MUST NOT result in a call back to
87 // Delegate::InfoBarContainerStateChanged() unless it causes an actual
88 // change, lest we infinitely recurse.
89 void SetMaxTopArrowHeight(int height
);
91 // Called when a contained infobar has animated or by some other means changed
92 // its height, or when it stops animating. The container is expected to do
93 // anything necessary to respond, e.g. re-layout.
94 void OnInfoBarStateChanged(bool is_animating
);
96 // Called by |infobar| to request that it be removed from the container, as it
97 // is about to delete itself. At this point, |infobar| should already be
99 void RemoveInfoBar(InfoBar
* infobar
);
101 const Delegate
* delegate() const { return delegate_
; }
104 // Subclasses must call this during destruction, so that we can remove
105 // infobars (which will call the pure virtual functions below) while the
106 // subclass portion of |this| has not yet been destroyed.
107 void RemoveAllInfoBarsForDestruction();
109 // These must be implemented on each platform to e.g. adjust the visible
111 virtual void PlatformSpecificAddInfoBar(InfoBar
* infobar
,
112 size_t position
) = 0;
113 virtual void PlatformSpecificRemoveInfoBar(InfoBar
* infobar
) = 0;
114 virtual void PlatformSpecificInfoBarStateChanged(bool is_animating
) {}
117 typedef std::vector
<InfoBar
*> InfoBars
;
119 // content::NotificationObserver:
120 virtual void Observe(int type
,
121 const content::NotificationSource
& source
,
122 const content::NotificationDetails
& details
) OVERRIDE
;
124 // chrome::search::SearchModelObserver:
125 virtual void ModeChanged(const chrome::search::Mode
& old_mode
,
126 const chrome::search::Mode
& new_mode
) OVERRIDE
;
128 // Hides an InfoBar for the specified delegate, in response to a notification
129 // from the selected InfoBarTabHelper. The InfoBar's disappearance will be
130 // animated if |use_animation| is true and it has been more than 50ms since
131 // infobars were reshown due to an Instant Extended mode change. The InfoBar
132 // will call back to RemoveInfoBar() to remove itself once it's hidden (which
133 // may mean synchronously). Returns the position within |infobars_| the
134 // infobar was previously at.
135 size_t HideInfoBar(InfoBarDelegate
* delegate
, bool use_animation
);
137 // Hides all infobars in this container without animation.
138 void HideAllInfoBars();
140 // Adds |infobar| to this container before the existing infobar at position
141 // |position| and calls Show() on it. |animate| is passed along to
142 // infobar->Show(). Depending on the value of |callback_status|, this calls
143 // infobar->set_container(this) either before or after the call to Show() so
144 // that OnInfoBarStateChanged() either will or won't be called as a result.
145 enum CallbackStatus
{ NO_CALLBACK
, WANT_CALLBACK
};
146 void AddInfoBar(InfoBar
* infobar
,
149 CallbackStatus callback_status
);
151 void UpdateInfoBarArrowTargetHeights();
152 int ArrowTargetHeightForInfoBar(size_t infobar_index
) const;
154 content::NotificationRegistrar registrar_
;
156 InfoBarTabHelper
* tab_helper_
;
159 // Tracks the most recent time infobars were re-shown after being hidden due
160 // to Instant Extended's ModeChanged.
161 base::TimeTicks infobars_shown_time_
;
163 // Tracks which search mode is active, as well as mode changes, for Instant
165 chrome::search::SearchModel
* search_model_
;
167 // Calculated in SetMaxTopArrowHeight().
168 int top_arrow_target_height_
;
170 DISALLOW_COPY_AND_ASSIGN(InfoBarContainer
);
173 #endif // CHROME_BROWSER_INFOBARS_INFOBAR_CONTAINER_H_