| 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<InfoBarAddedDetails>(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 InfoBarRemovedDetails* removed_details = | 120 InfoBar::RemovedDetails* removed_details = |
| 125 content::Details<InfoBarRemovedDetails>(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 InfoBarReplacedDetails* replaced_details = | 128 InfoBar::ReplacedDetails* replaced_details = |
| 132 content::Details<InfoBarReplacedDetails>(details).ptr(); | 129 content::Details<InfoBar::ReplacedDetails>(details).ptr(); |
| 133 ReplaceInfoBar(replaced_details->first, replaced_details->second); | 130 InfoBar* old_infobar = replaced_details->first; |
| 131 InfoBar* new_infobar = replaced_details->second; |
| 132 PlatformSpecificReplaceInfoBar(old_infobar, new_infobar); |
| 133 InfoBars::const_iterator i(std::find(infobars_.begin(), infobars_.end(), |
| 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); |
| 134 break; | 139 break; |
| 135 } | 140 } |
| 136 | 141 |
| 137 default: | 142 default: |
| 138 NOTREACHED(); | 143 NOTREACHED(); |
| 139 break; | 144 break; |
| 140 } | 145 } |
| 141 } | 146 } |
| 142 | 147 |
| 143 void InfoBarContainer::ReplaceInfoBar(InfoBarDelegate* old_delegate, | |
| 144 InfoBarDelegate* new_delegate) { | |
| 145 InfoBar* new_infobar = new_delegate->CreateInfoBar(infobar_service_); | |
| 146 InfoBar* old_infobar = FindInfoBar(old_delegate); | |
| 147 #if defined(OS_ANDROID) | |
| 148 PlatformSpecificReplaceInfoBar(old_infobar, new_infobar); | |
| 149 #endif | |
| 150 AddInfoBar( | |
| 151 new_infobar, HideInfoBar(old_infobar, false), false, WANT_CALLBACK); | |
| 152 } | |
| 153 | |
| 154 InfoBar* InfoBarContainer::FindInfoBar(InfoBarDelegate* delegate) { | |
| 155 // Search for the infobar associated with |delegate|. We cannot search for | |
| 156 // |delegate| in |tab_helper_|, because an InfoBar remains alive until its | |
| 157 // close animation completes, while the delegate is removed from the tab | |
| 158 // immediately. | |
| 159 for (InfoBars::iterator i(infobars_.begin()); i != infobars_.end(); ++i) { | |
| 160 InfoBar* infobar = *i; | |
| 161 if (infobar->delegate() == delegate) | |
| 162 return infobar; | |
| 163 } | |
| 164 NOTREACHED(); | |
| 165 return NULL; | |
| 166 } | |
| 167 | |
| 168 size_t InfoBarContainer::HideInfoBar(InfoBar* infobar, bool use_animation) { | |
| 169 InfoBars::iterator it = | |
| 170 std::find(infobars_.begin(), infobars_.end(), infobar); | |
| 171 DCHECK(it != infobars_.end()); | |
| 172 size_t position = it - infobars_.begin(); | |
| 173 // We merely need hide the infobar; it will call back to RemoveInfoBar() | |
| 174 // itself once it's hidden. | |
| 175 infobar->Hide(use_animation); | |
| 176 infobar->CloseSoon(); | |
| 177 UpdateInfoBarArrowTargetHeights(); | |
| 178 return position; | |
| 179 } | |
| 180 | |
| 181 void InfoBarContainer::HideAllInfoBars() { | 148 void InfoBarContainer::HideAllInfoBars() { |
| 182 registrar_.RemoveAll(); | 149 registrar_.RemoveAll(); |
| 183 | 150 |
| 184 while (!infobars_.empty()) { | 151 while (!infobars_.empty()) { |
| 185 InfoBar* infobar = infobars_.front(); | 152 InfoBar* infobar = infobars_.front(); |
| 186 // 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 |
| 187 // 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. |
| 188 infobar->Hide(false); | 156 infobar->Hide(false); |
| 189 } | 157 } |
| 190 } | 158 } |
| 191 | 159 |
| 192 void InfoBarContainer::AddInfoBar(InfoBar* infobar, | 160 void InfoBarContainer::AddInfoBar(InfoBar* infobar, |
| 193 size_t position, | 161 size_t position, |
| 194 bool animate, | 162 bool animate, |
| 195 CallbackStatus callback_status) { | 163 CallbackStatus callback_status) { |
| 196 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == | 164 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == |
| 197 infobars_.end()); | 165 infobars_.end()); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 220 const_cast<const InfoBar*>(infobars_.front())->animation(); | 188 const_cast<const InfoBar*>(infobars_.front())->animation(); |
| 221 if ((infobar_index > 1) || first_infobar_animation.IsShowing()) | 189 if ((infobar_index > 1) || first_infobar_animation.IsShowing()) |
| 222 return InfoBar::kDefaultArrowTargetHeight; | 190 return InfoBar::kDefaultArrowTargetHeight; |
| 223 // 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 |
| 224 // 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 |
| 225 // 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. |
| 226 return top_arrow_target_height_ + static_cast<int>( | 194 return top_arrow_target_height_ + static_cast<int>( |
| 227 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * | 195 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * |
| 228 first_infobar_animation.GetCurrentValue()); | 196 first_infobar_animation.GetCurrentValue()); |
| 229 } | 197 } |
| OLD | NEW |