| 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 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" | |
| 13 #include "chrome/browser/infobars/infobar.h" | 12 #include "chrome/browser/infobars/infobar.h" |
| 14 #include "chrome/browser/infobars/infobar_delegate.h" | 13 #include "chrome/browser/infobars/infobar_delegate.h" |
| 15 #include "chrome/browser/infobars/infobar_service.h" | 14 #include "chrome/browser/infobars/infobar_manager.h" |
| 16 #include "content/public/browser/notification_details.h" | |
| 17 #include "content/public/browser/notification_source.h" | |
| 18 #include "ui/gfx/animation/slide_animation.h" | 15 #include "ui/gfx/animation/slide_animation.h" |
| 19 | 16 |
| 20 InfoBarContainer::Delegate::~Delegate() { | 17 InfoBarContainer::Delegate::~Delegate() { |
| 21 } | 18 } |
| 22 | 19 |
| 23 InfoBarContainer::InfoBarContainer(Delegate* delegate) | 20 InfoBarContainer::InfoBarContainer(Delegate* delegate) |
| 24 : delegate_(delegate), | 21 : delegate_(delegate), |
| 25 infobar_service_(NULL), | 22 infobar_manager_(NULL), |
| 26 top_arrow_target_height_(InfoBar::kDefaultArrowTargetHeight) { | 23 top_arrow_target_height_(InfoBar::kDefaultArrowTargetHeight) {} |
| 27 } | |
| 28 | 24 |
| 29 InfoBarContainer::~InfoBarContainer() { | 25 InfoBarContainer::~InfoBarContainer() { |
| 26 if (infobar_manager_) |
| 27 infobar_manager_->RemoveObserver(this); |
| 30 // RemoveAllInfoBarsForDestruction() should have already cleared our infobars. | 28 // RemoveAllInfoBarsForDestruction() should have already cleared our infobars. |
| 31 DCHECK(infobars_.empty()); | 29 DCHECK(infobars_.empty()); |
| 32 } | 30 } |
| 33 | 31 |
| 34 void InfoBarContainer::ChangeInfoBarService(InfoBarService* infobar_service) { | 32 void InfoBarContainer::ChangeInfoBarManager(InfoBarManager* infobar_manager) { |
| 35 HideAllInfoBars(); | 33 HideAllInfoBars(); |
| 36 | 34 |
| 37 infobar_service_ = infobar_service; | 35 if (infobar_manager_) |
| 38 if (infobar_service_) { | 36 infobar_manager_->RemoveObserver(this); |
| 39 content::Source<InfoBarService> source(infobar_service_); | |
| 40 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
| 41 source); | |
| 42 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | |
| 43 source); | |
| 44 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, | |
| 45 source); | |
| 46 | 37 |
| 47 for (size_t i = 0; i < infobar_service_->infobar_count(); ++i) { | 38 infobar_manager_ = infobar_manager; |
| 39 if (infobar_manager_) { |
| 40 infobar_manager_->AddObserver(this); |
| 41 for (size_t i = 0; i < infobar_manager_->infobar_count(); ++i) { |
| 48 // As when we removed the infobars above, we prevent callbacks to | 42 // As when we removed the infobars above, we prevent callbacks to |
| 49 // OnInfoBarStateChanged() for each infobar. | 43 // OnInfoBarStateChanged() for each infobar. |
| 50 AddInfoBar(infobar_service_->infobar_at(i), i, false, NO_CALLBACK); | 44 AddInfoBar(infobar_manager_->infobar_at(i), i, false, NO_CALLBACK); |
| 51 } | 45 } |
| 52 } | 46 } |
| 53 | 47 |
| 54 // Now that everything is up to date, signal the delegate to re-layout. | 48 // Now that everything is up to date, signal the delegate to re-layout. |
| 55 OnInfoBarStateChanged(false); | 49 OnInfoBarStateChanged(false); |
| 56 } | 50 } |
| 57 | 51 |
| 58 int InfoBarContainer::GetVerticalOverlap(int* total_height) { | 52 int InfoBarContainer::GetVerticalOverlap(int* total_height) { |
| 59 // Our |total_height| is the sum of the preferred heights of the InfoBars | 53 // Our |total_height| is the sum of the preferred heights of the InfoBars |
| 60 // contained within us plus the |vertical_overlap|. | 54 // contained within us plus the |vertical_overlap|. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 infobars_.erase(i); | 91 infobars_.erase(i); |
| 98 } | 92 } |
| 99 | 93 |
| 100 void InfoBarContainer::RemoveAllInfoBarsForDestruction() { | 94 void InfoBarContainer::RemoveAllInfoBarsForDestruction() { |
| 101 // Before we remove any children, we reset |delegate_|, so that no removals | 95 // Before we remove any children, we reset |delegate_|, so that no removals |
| 102 // will result in us trying to call | 96 // will result in us trying to call |
| 103 // delegate_->InfoBarContainerStateChanged(). This is important because at | 97 // delegate_->InfoBarContainerStateChanged(). This is important because at |
| 104 // this point |delegate_| may be shutting down, and it's at best unimportant | 98 // this point |delegate_| may be shutting down, and it's at best unimportant |
| 105 // and at worst disastrous to call that. | 99 // and at worst disastrous to call that. |
| 106 delegate_ = NULL; | 100 delegate_ = NULL; |
| 107 ChangeInfoBarService(NULL); | 101 ChangeInfoBarManager(NULL); |
| 108 } | 102 } |
| 109 | 103 |
| 110 void InfoBarContainer::Observe(int type, | 104 void InfoBarContainer::OnInfoBarAdded(InfoBar* infobar) { |
| 111 const content::NotificationSource& source, | 105 AddInfoBar(infobar, infobars_.size(), true, WANT_CALLBACK); |
| 112 const content::NotificationDetails& details) { | 106 } |
| 113 switch (type) { | |
| 114 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED: | |
| 115 AddInfoBar(content::Details<InfoBar::AddedDetails>(details).ptr(), | |
| 116 infobars_.size(), true, WANT_CALLBACK); | |
| 117 break; | |
| 118 | 107 |
| 119 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED: { | 108 void InfoBarContainer::OnInfoBarReplaced(InfoBar* old_infobar, |
| 120 InfoBar::RemovedDetails* removed_details = | 109 InfoBar* new_infobar) { |
| 121 content::Details<InfoBar::RemovedDetails>(details).ptr(); | 110 PlatformSpecificReplaceInfoBar(old_infobar, new_infobar); |
| 122 removed_details->first->Hide(removed_details->second); | 111 InfoBars::const_iterator i( |
| 123 UpdateInfoBarArrowTargetHeights(); | 112 std::find(infobars_.begin(), infobars_.end(), old_infobar)); |
| 124 break; | 113 DCHECK(i != infobars_.end()); |
| 125 } | 114 size_t position = i - infobars_.begin(); |
| 115 old_infobar->Hide(false); |
| 116 AddInfoBar(new_infobar, position, false, WANT_CALLBACK); |
| 117 } |
| 126 | 118 |
| 127 case chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED: { | 119 void InfoBarContainer::OnInfoBarRemoved(InfoBar* infobar, bool animate) { |
| 128 InfoBar::ReplacedDetails* replaced_details = | 120 infobar->Hide(animate); |
| 129 content::Details<InfoBar::ReplacedDetails>(details).ptr(); | 121 UpdateInfoBarArrowTargetHeights(); |
| 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); | |
| 139 break; | |
| 140 } | |
| 141 | |
| 142 default: | |
| 143 NOTREACHED(); | |
| 144 break; | |
| 145 } | |
| 146 } | 122 } |
| 147 | 123 |
| 148 void InfoBarContainer::HideAllInfoBars() { | 124 void InfoBarContainer::HideAllInfoBars() { |
| 149 registrar_.RemoveAll(); | |
| 150 | |
| 151 while (!infobars_.empty()) { | 125 while (!infobars_.empty()) { |
| 152 InfoBar* infobar = infobars_.front(); | 126 InfoBar* infobar = infobars_.front(); |
| 153 // Inform the infobar that it's hidden. If it was already closing, this | 127 // 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 | 128 // deletes it. Otherwise, this ensures the infobar will be deleted if it's |
| 155 // closed while it's not in an InfoBarContainer. | 129 // closed while it's not in an InfoBarContainer. |
| 156 infobar->Hide(false); | 130 infobar->Hide(false); |
| 157 } | 131 } |
| 158 } | 132 } |
| 159 | 133 |
| 160 void InfoBarContainer::AddInfoBar(InfoBar* infobar, | 134 void InfoBarContainer::AddInfoBar(InfoBar* infobar, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 188 const_cast<const InfoBar*>(infobars_.front())->animation(); | 162 const_cast<const InfoBar*>(infobars_.front())->animation(); |
| 189 if ((infobar_index > 1) || first_infobar_animation.IsShowing()) | 163 if ((infobar_index > 1) || first_infobar_animation.IsShowing()) |
| 190 return InfoBar::kDefaultArrowTargetHeight; | 164 return InfoBar::kDefaultArrowTargetHeight; |
| 191 // When the first infobar is animating closed, we animate the second infobar's | 165 // 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 | 166 // 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. | 167 // 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>( | 168 return top_arrow_target_height_ + static_cast<int>( |
| 195 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * | 169 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * |
| 196 first_infobar_animation.GetCurrentValue()); | 170 first_infobar_animation.GetCurrentValue()); |
| 197 } | 171 } |
| OLD | NEW |