OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include "chrome/browser/infobars/infobar_container.h" | 7 #include "chrome/browser/infobars/infobar_container.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
(...skipping 29 matching lines...) Expand all Loading... |
40 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | 40 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, |
41 source); | 41 source); |
42 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | 42 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
43 source); | 43 source); |
44 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, | 44 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, |
45 source); | 45 source); |
46 | 46 |
47 for (size_t i = 0; i < infobar_service_->infobar_count(); ++i) { | 47 for (size_t i = 0; i < infobar_service_->infobar_count(); ++i) { |
48 // As when we removed the infobars above, we prevent callbacks to | 48 // As when we removed the infobars above, we prevent callbacks to |
49 // OnInfoBarAnimated() for each infobar. | 49 // OnInfoBarAnimated() for each infobar. |
50 AddInfoBar( | 50 AddInfoBar(infobar_service_->infobar_at(i), i, false, NO_CALLBACK); |
51 infobar_service_->infobar_at(i)->CreateInfoBar(infobar_service_), | |
52 i, false, NO_CALLBACK); | |
53 } | 51 } |
54 } | 52 } |
55 | 53 |
56 // Now that everything is up to date, signal the delegate to re-layout. | 54 // Now that everything is up to date, signal the delegate to re-layout. |
57 OnInfoBarStateChanged(false); | 55 OnInfoBarStateChanged(false); |
58 } | 56 } |
59 | 57 |
60 int InfoBarContainer::GetVerticalOverlap(int* total_height) { | 58 int InfoBarContainer::GetVerticalOverlap(int* total_height) { |
61 // Our |total_height| is the sum of the preferred heights of the InfoBars | 59 // Our |total_height| is the sum of the preferred heights of the InfoBars |
62 // contained within us plus the |vertical_overlap|. | 60 // contained within us plus the |vertical_overlap|. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // and at worst disastrous to call that. | 105 // and at worst disastrous to call that. |
108 delegate_ = NULL; | 106 delegate_ = NULL; |
109 ChangeInfoBarService(NULL); | 107 ChangeInfoBarService(NULL); |
110 } | 108 } |
111 | 109 |
112 void InfoBarContainer::Observe(int type, | 110 void InfoBarContainer::Observe(int type, |
113 const content::NotificationSource& source, | 111 const content::NotificationSource& source, |
114 const content::NotificationDetails& details) { | 112 const content::NotificationDetails& details) { |
115 switch (type) { | 113 switch (type) { |
116 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED: | 114 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED: |
117 AddInfoBar( | 115 AddInfoBar(content::Details<InfoBar::AddedDetails>(details).ptr(), |
118 content::Details<InfoBar::AddedDetails>(details)->CreateInfoBar( | 116 infobars_.size(), true, WANT_CALLBACK); |
119 infobar_service_), | |
120 infobars_.size(), true, WANT_CALLBACK); | |
121 break; | 117 break; |
122 | 118 |
123 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED: { | 119 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED: { |
124 InfoBar::RemovedDetails* removed_details = | 120 InfoBar::RemovedDetails* removed_details = |
125 content::Details<InfoBar::RemovedDetails>(details).ptr(); | 121 content::Details<InfoBar::RemovedDetails>(details).ptr(); |
126 HideInfoBar(FindInfoBar(removed_details->first), removed_details->second); | 122 removed_details->first->Hide(removed_details->second); |
| 123 UpdateInfoBarArrowTargetHeights(); |
127 break; | 124 break; |
128 } | 125 } |
129 | 126 |
130 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED: { | 127 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED: { |
131 InfoBar::ReplacedDetails* replaced_details = | 128 InfoBar::ReplacedDetails* replaced_details = |
132 content::Details<InfoBar::ReplacedDetails>(details).ptr(); | 129 content::Details<InfoBar::ReplacedDetails>(details).ptr(); |
133 InfoBar* old_infobar = FindInfoBar(replaced_details->first); | 130 InfoBar* old_infobar = replaced_details->first; |
134 InfoBar* new_infobar = | 131 InfoBar* new_infobar = replaced_details->second; |
135 replaced_details->second->CreateInfoBar(infobar_service_); | |
136 PlatformSpecificReplaceInfoBar(old_infobar, new_infobar); | 132 PlatformSpecificReplaceInfoBar(old_infobar, new_infobar); |
137 AddInfoBar(new_infobar, HideInfoBar(old_infobar, false), false, | 133 InfoBars::const_iterator i(std::find(infobars_.begin(), infobars_.end(), |
138 WANT_CALLBACK); | 134 old_infobar)); |
| 135 DCHECK(i != infobars_.end()); |
| 136 size_t position = i - infobars_.begin(); |
| 137 old_infobar->Hide(false); |
| 138 AddInfoBar(new_infobar, position, false, WANT_CALLBACK); |
139 break; | 139 break; |
140 } | 140 } |
141 | 141 |
142 default: | 142 default: |
143 NOTREACHED(); | 143 NOTREACHED(); |
144 break; | 144 break; |
145 } | 145 } |
146 } | 146 } |
147 | 147 |
148 InfoBar* InfoBarContainer::FindInfoBar(InfoBarDelegate* delegate) { | |
149 // Search for the infobar associated with |delegate|. We cannot search for | |
150 // |delegate| in |tab_helper_|, because an InfoBar remains alive until its | |
151 // close animation completes, while the delegate is removed from the tab | |
152 // immediately. | |
153 for (InfoBars::iterator i(infobars_.begin()); i != infobars_.end(); ++i) { | |
154 InfoBar* infobar = *i; | |
155 if (infobar->delegate() == delegate) | |
156 return infobar; | |
157 } | |
158 NOTREACHED(); | |
159 return NULL; | |
160 } | |
161 | |
162 size_t InfoBarContainer::HideInfoBar(InfoBar* infobar, bool use_animation) { | |
163 InfoBars::iterator it = | |
164 std::find(infobars_.begin(), infobars_.end(), infobar); | |
165 DCHECK(it != infobars_.end()); | |
166 size_t position = it - infobars_.begin(); | |
167 // We merely need hide the infobar; it will call back to RemoveInfoBar() | |
168 // itself once it's hidden. | |
169 infobar->Hide(use_animation); | |
170 infobar->CloseSoon(); | |
171 UpdateInfoBarArrowTargetHeights(); | |
172 return position; | |
173 } | |
174 | |
175 void InfoBarContainer::HideAllInfoBars() { | 148 void InfoBarContainer::HideAllInfoBars() { |
176 registrar_.RemoveAll(); | 149 registrar_.RemoveAll(); |
177 | 150 |
178 while (!infobars_.empty()) { | 151 while (!infobars_.empty()) { |
179 InfoBar* infobar = infobars_.front(); | 152 InfoBar* infobar = infobars_.front(); |
180 // Inform the infobar that it's hidden. If it was already closing, this | 153 // Inform the infobar that it's hidden. If it was already closing, this |
181 // closes its delegate. | 154 // deletes it. Otherwise, this ensures the infobar will be deleted if it's |
| 155 // closed while it's not in an InfoBarContainer. |
182 infobar->Hide(false); | 156 infobar->Hide(false); |
183 } | 157 } |
184 } | 158 } |
185 | 159 |
186 void InfoBarContainer::AddInfoBar(InfoBar* infobar, | 160 void InfoBarContainer::AddInfoBar(InfoBar* infobar, |
187 size_t position, | 161 size_t position, |
188 bool animate, | 162 bool animate, |
189 CallbackStatus callback_status) { | 163 CallbackStatus callback_status) { |
190 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == | 164 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == |
191 infobars_.end()); | 165 infobars_.end()); |
(...skipping 22 matching lines...) Expand all Loading... |
214 const_cast<const InfoBar*>(infobars_.front())->animation(); | 188 const_cast<const InfoBar*>(infobars_.front())->animation(); |
215 if ((infobar_index > 1) || first_infobar_animation.IsShowing()) | 189 if ((infobar_index > 1) || first_infobar_animation.IsShowing()) |
216 return InfoBar::kDefaultArrowTargetHeight; | 190 return InfoBar::kDefaultArrowTargetHeight; |
217 // When the first infobar is animating closed, we animate the second infobar's | 191 // When the first infobar is animating closed, we animate the second infobar's |
218 // arrow target height from the default to the top target height. Note that | 192 // arrow target height from the default to the top target height. Note that |
219 // the animation values here are going from 1.0 -> 0.0 as the top bar closes. | 193 // the animation values here are going from 1.0 -> 0.0 as the top bar closes. |
220 return top_arrow_target_height_ + static_cast<int>( | 194 return top_arrow_target_height_ + static_cast<int>( |
221 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * | 195 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * |
222 first_infobar_animation.GetCurrentValue()); | 196 first_infobar_animation.GetCurrentValue()); |
223 } | 197 } |
OLD | NEW |