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.
4
5 #include "chrome/browser/ui/views/infobars/infobar_container.h"
6
7 #include "chrome/browser/tab_contents/infobar_delegate.h"
8 #include "chrome/browser/ui/views/infobars/infobar.h"
9 #include "content/browser/tab_contents/tab_contents.h"
10 #include "content/common/notification_details.h"
11 #include "content/common/notification_source.h"
12 #include "ui/base/animation/slide_animation.h"
13
~Delegate()14 InfoBarContainer::Delegate::~Delegate() {
15 }
16
InfoBarContainer(Delegate * delegate)17 InfoBarContainer::InfoBarContainer(Delegate* delegate)
18 : delegate_(delegate),
19 tab_contents_(NULL),
20 top_arrow_target_height_(InfoBar::kDefaultArrowTargetHeight) {
21 }
22
~InfoBarContainer()23 InfoBarContainer::~InfoBarContainer() {
24 // RemoveAllInfoBarsForDestruction() should have already cleared our infobars.
25 DCHECK(infobars_.empty());
26 }
27
ChangeTabContents(TabContents * contents)28 void InfoBarContainer::ChangeTabContents(TabContents* contents) {
29 registrar_.RemoveAll();
30
31 while (!infobars_.empty()) {
32 InfoBar* infobar = infobars_.front();
33 // NULL the container pointer first so that if the infobar is currently
34 // animating, OnInfoBarAnimated() won't get called; we'll manually trigger
35 // this once for the whole set of changes below. This also prevents
36 // InfoBar::MaybeDelete() from calling RemoveInfoBar() a second time if the
37 // infobar happens to be at zero height (dunno if this can actually happen).
38 infobar->set_container(NULL);
39 RemoveInfoBar(infobar);
40 }
41
42 tab_contents_ = contents;
43 if (tab_contents_) {
44 Source<TabContents> tc_source(tab_contents_);
45 registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_ADDED,
46 tc_source);
47 registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_REMOVED,
48 tc_source);
49 registrar_.Add(this, NotificationType::TAB_CONTENTS_INFOBAR_REPLACED,
50 tc_source);
51
52 for (size_t i = 0; i < tab_contents_->infobar_count(); ++i) {
53 // As when we removed the infobars above, we prevent callbacks to
54 // OnInfoBarAnimated() for each infobar.
55 AddInfoBar(tab_contents_->GetInfoBarDelegateAt(i)->CreateInfoBar(), false,
56 NO_CALLBACK);
57 }
58 }
59
60 // Now that everything is up to date, signal the delegate to re-layout.
61 OnInfoBarStateChanged(false);
62 }
63
GetVerticalOverlap(int * total_height)64 int InfoBarContainer::GetVerticalOverlap(int* total_height) {
65 // Our |total_height| is the sum of the preferred heights of the InfoBars
66 // contained within us plus the |vertical_overlap|.
67 int vertical_overlap = 0;
68 int next_infobar_y = 0;
69
70 for (InfoBars::iterator i(infobars_.begin()); i != infobars_.end(); ++i) {
71 InfoBar* infobar = *i;
72 next_infobar_y -= infobar->arrow_height();
73 vertical_overlap = std::max(vertical_overlap, -next_infobar_y);
74 next_infobar_y += infobar->total_height();
75 }
76
77 if (total_height)
78 *total_height = next_infobar_y + vertical_overlap;
79 return vertical_overlap;
80 }
81
SetMaxTopArrowHeight(int height)82 void InfoBarContainer::SetMaxTopArrowHeight(int height) {
83 // Decrease the height by the arrow stroke thickness, which is the separator
84 // line height, because the infobar arrow target heights are without-stroke.
85 top_arrow_target_height_ = std::min(
86 std::max(height - InfoBar::kSeparatorLineHeight, 0),
87 InfoBar::kMaximumArrowTargetHeight);
88 UpdateInfoBarArrowTargetHeights();
89 }
90
OnInfoBarStateChanged(bool is_animating)91 void InfoBarContainer::OnInfoBarStateChanged(bool is_animating) {
92 if (delegate_)
93 delegate_->InfoBarContainerStateChanged(is_animating);
94 }
95
RemoveDelegate(InfoBarDelegate * delegate)96 void InfoBarContainer::RemoveDelegate(InfoBarDelegate* delegate) {
97 tab_contents_->RemoveInfoBar(delegate);
98 }
99
RemoveInfoBar(InfoBar * infobar)100 void InfoBarContainer::RemoveInfoBar(InfoBar* infobar) {
101 InfoBars::iterator infobar_iterator(std::find(infobars_.begin(),
102 infobars_.end(), infobar));
103 DCHECK(infobar_iterator != infobars_.end());
104 PlatformSpecificRemoveInfoBar(infobar);
105 infobars_.erase(infobar_iterator);
106 }
107
RemoveAllInfoBarsForDestruction()108 void InfoBarContainer::RemoveAllInfoBarsForDestruction() {
109 // Before we remove any children, we reset |delegate_|, so that no removals
110 // will result in us trying to call
111 // delegate_->InfoBarContainerStateChanged(). This is important because at
112 // this point |delegate_| may be shutting down, and it's at best unimportant
113 // and at worst disastrous to call that.
114 delegate_ = NULL;
115 ChangeTabContents(NULL);
116 }
117
Observe(NotificationType type,const NotificationSource & source,const NotificationDetails & details)118 void InfoBarContainer::Observe(NotificationType type,
119 const NotificationSource& source,
120 const NotificationDetails& details) {
121 switch (type.value) {
122 case NotificationType::TAB_CONTENTS_INFOBAR_ADDED:
123 AddInfoBar(Details<InfoBarDelegate>(details)->CreateInfoBar(), true,
124 WANT_CALLBACK);
125 break;
126
127 case NotificationType::TAB_CONTENTS_INFOBAR_REMOVED:
128 RemoveInfoBar(Details<InfoBarDelegate>(details).ptr(), true);
129 break;
130
131 case NotificationType::TAB_CONTENTS_INFOBAR_REPLACED: {
132 typedef std::pair<InfoBarDelegate*, InfoBarDelegate*> InfoBarPair;
133 InfoBarPair* infobar_pair = Details<InfoBarPair>(details).ptr();
134 RemoveInfoBar(infobar_pair->first, false);
135 AddInfoBar(infobar_pair->second->CreateInfoBar(), false, WANT_CALLBACK);
136 break;
137 }
138
139 default:
140 NOTREACHED();
141 break;
142 }
143 }
144
RemoveInfoBar(InfoBarDelegate * delegate,bool use_animation)145 void InfoBarContainer::RemoveInfoBar(InfoBarDelegate* delegate,
146 bool use_animation) {
147 // Search for the infobar associated with |delegate|. We cannot search for
148 // |delegate| in |tab_contents_|, because an InfoBar remains alive until its
149 // close animation completes, while the delegate is removed from the tab
150 // immediately.
151 for (InfoBars::iterator i(infobars_.begin()); i != infobars_.end(); ++i) {
152 InfoBar* infobar = *i;
153 if (infobar->delegate() == delegate) {
154 // We merely need hide the infobar; it will call back to RemoveInfoBar()
155 // itself once it's hidden.
156 infobar->Hide(use_animation);
157 UpdateInfoBarArrowTargetHeights();
158 break;
159 }
160 }
161 }
162
AddInfoBar(InfoBar * infobar,bool animate,CallbackStatus callback_status)163 void InfoBarContainer::AddInfoBar(InfoBar* infobar,
164 bool animate,
165 CallbackStatus callback_status) {
166 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) ==
167 infobars_.end());
168 infobars_.push_back(infobar);
169 UpdateInfoBarArrowTargetHeights();
170 PlatformSpecificAddInfoBar(infobar);
171 if (callback_status == WANT_CALLBACK)
172 infobar->set_container(this);
173 infobar->Show(animate);
174 if (callback_status == NO_CALLBACK)
175 infobar->set_container(this);
176 }
177
UpdateInfoBarArrowTargetHeights()178 void InfoBarContainer::UpdateInfoBarArrowTargetHeights() {
179 for (size_t i = 0; i < infobars_.size(); ++i)
180 infobars_[i]->SetArrowTargetHeight(ArrowTargetHeightForInfoBar(i));
181 }
182
ArrowTargetHeightForInfoBar(size_t infobar_index) const183 int InfoBarContainer::ArrowTargetHeightForInfoBar(size_t infobar_index) const {
184 if (!delegate_->DrawInfoBarArrows(NULL))
185 return 0;
186 if (infobar_index == 0)
187 return top_arrow_target_height_;
188 const ui::SlideAnimation* first_infobar_animation =
189 const_cast<const InfoBar*>(infobars_.front())->animation();
190 if ((infobar_index > 1) || first_infobar_animation->IsShowing())
191 return InfoBar::kDefaultArrowTargetHeight;
192 // When the first infobar is animating closed, we animate the second infobar's
193 // arrow target height from the default to the top target height. Note that
194 // the animation values here are going from 1.0 -> 0.0 as the top bar closes.
195 return top_arrow_target_height_ + static_cast<int>(
196 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) *
197 first_infobar_animation->GetCurrentValue());
198 }
199