| 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(infobar_service_->infobar_at(i), i, false, NO_CALLBACK); | 50 AddInfoBar( |
| 51 infobar_service_->infobar_at(i)->CreateInfoBar(infobar_service_), |
| 52 i, false, NO_CALLBACK); |
| 51 } | 53 } |
| 52 } | 54 } |
| 53 | 55 |
| 54 // Now that everything is up to date, signal the delegate to re-layout. | 56 // Now that everything is up to date, signal the delegate to re-layout. |
| 55 OnInfoBarStateChanged(false); | 57 OnInfoBarStateChanged(false); |
| 56 } | 58 } |
| 57 | 59 |
| 58 int InfoBarContainer::GetVerticalOverlap(int* total_height) { | 60 int InfoBarContainer::GetVerticalOverlap(int* total_height) { |
| 59 // Our |total_height| is the sum of the preferred heights of the InfoBars | 61 // Our |total_height| is the sum of the preferred heights of the InfoBars |
| 60 // contained within us plus the |vertical_overlap|. | 62 // contained within us plus the |vertical_overlap|. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 // and at worst disastrous to call that. | 107 // and at worst disastrous to call that. |
| 106 delegate_ = NULL; | 108 delegate_ = NULL; |
| 107 ChangeInfoBarService(NULL); | 109 ChangeInfoBarService(NULL); |
| 108 } | 110 } |
| 109 | 111 |
| 110 void InfoBarContainer::Observe(int type, | 112 void InfoBarContainer::Observe(int type, |
| 111 const content::NotificationSource& source, | 113 const content::NotificationSource& source, |
| 112 const content::NotificationDetails& details) { | 114 const content::NotificationDetails& details) { |
| 113 switch (type) { | 115 switch (type) { |
| 114 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED: | 116 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED: |
| 115 AddInfoBar(content::Details<InfoBar::AddedDetails>(details).ptr(), | 117 AddInfoBar( |
| 116 infobars_.size(), true, WANT_CALLBACK); | 118 content::Details<InfoBar::AddedDetails>(details)->CreateInfoBar( |
| 119 infobar_service_), |
| 120 infobars_.size(), true, WANT_CALLBACK); |
| 117 break; | 121 break; |
| 118 | 122 |
| 119 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED: { | 123 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED: { |
| 120 InfoBar::RemovedDetails* removed_details = | 124 InfoBar::RemovedDetails* removed_details = |
| 121 content::Details<InfoBar::RemovedDetails>(details).ptr(); | 125 content::Details<InfoBar::RemovedDetails>(details).ptr(); |
| 122 removed_details->first->Hide(removed_details->second); | 126 HideInfoBar(FindInfoBar(removed_details->first), removed_details->second); |
| 123 UpdateInfoBarArrowTargetHeights(); | |
| 124 break; | 127 break; |
| 125 } | 128 } |
| 126 | 129 |
| 127 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED: { | 130 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED: { |
| 128 InfoBar::ReplacedDetails* replaced_details = | 131 InfoBar::ReplacedDetails* replaced_details = |
| 129 content::Details<InfoBar::ReplacedDetails>(details).ptr(); | 132 content::Details<InfoBar::ReplacedDetails>(details).ptr(); |
| 130 InfoBar* old_infobar = replaced_details->first; | 133 InfoBar* old_infobar = FindInfoBar(replaced_details->first); |
| 131 InfoBar* new_infobar = replaced_details->second; | 134 InfoBar* new_infobar = |
| 135 replaced_details->second->CreateInfoBar(infobar_service_); |
| 132 PlatformSpecificReplaceInfoBar(old_infobar, new_infobar); | 136 PlatformSpecificReplaceInfoBar(old_infobar, new_infobar); |
| 133 InfoBars::const_iterator i(std::find(infobars_.begin(), infobars_.end(), | 137 AddInfoBar(new_infobar, HideInfoBar(old_infobar, false), false, |
| 134 old_infobar)); | 138 WANT_CALLBACK); |
| 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 |
| 148 void InfoBarContainer::HideAllInfoBars() { | 175 void InfoBarContainer::HideAllInfoBars() { |
| 149 registrar_.RemoveAll(); | 176 registrar_.RemoveAll(); |
| 150 | 177 |
| 151 while (!infobars_.empty()) { | 178 while (!infobars_.empty()) { |
| 152 InfoBar* infobar = infobars_.front(); | 179 InfoBar* infobar = infobars_.front(); |
| 153 // Inform the infobar that it's hidden. If it was already closing, this | 180 // Inform the infobar that it's hidden. If it was already closing, this |
| 154 // deletes it. Otherwise, this ensures the infobar will be deleted if it's | 181 // closes its delegate. |
| 155 // closed while it's not in an InfoBarContainer. | |
| 156 infobar->Hide(false); | 182 infobar->Hide(false); |
| 157 } | 183 } |
| 158 } | 184 } |
| 159 | 185 |
| 160 void InfoBarContainer::AddInfoBar(InfoBar* infobar, | 186 void InfoBarContainer::AddInfoBar(InfoBar* infobar, |
| 161 size_t position, | 187 size_t position, |
| 162 bool animate, | 188 bool animate, |
| 163 CallbackStatus callback_status) { | 189 CallbackStatus callback_status) { |
| 164 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == | 190 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == |
| 165 infobars_.end()); | 191 infobars_.end()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 188 const_cast<const InfoBar*>(infobars_.front())->animation(); | 214 const_cast<const InfoBar*>(infobars_.front())->animation(); |
| 189 if ((infobar_index > 1) || first_infobar_animation.IsShowing()) | 215 if ((infobar_index > 1) || first_infobar_animation.IsShowing()) |
| 190 return InfoBar::kDefaultArrowTargetHeight; | 216 return InfoBar::kDefaultArrowTargetHeight; |
| 191 // When the first infobar is animating closed, we animate the second infobar's | 217 // When the first infobar is animating closed, we animate the second infobar's |
| 192 // arrow target height from the default to the top target height. Note that | 218 // arrow target height from the default to the top target height. Note that |
| 193 // the animation values here are going from 1.0 -> 0.0 as the top bar closes. | 219 // the animation values here are going from 1.0 -> 0.0 as the top bar closes. |
| 194 return top_arrow_target_height_ + static_cast<int>( | 220 return top_arrow_target_height_ + static_cast<int>( |
| 195 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * | 221 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * |
| 196 first_infobar_animation.GetCurrentValue()); | 222 first_infobar_animation.GetCurrentValue()); |
| 197 } | 223 } |
| OLD | NEW |