| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/infobars/infobar.h" | 5 #include "chrome/browser/infobars/infobar.h" |
| 6 | 6 |
| 7 #include <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/infobars/infobar_container.h" | 11 #include "chrome/browser/infobars/infobar_container.h" |
| 12 #include "chrome/browser/infobars/infobar_service.h" | 12 #include "chrome/browser/infobars/infobar_service.h" |
| 13 #include "ui/gfx/animation/slide_animation.h" | 13 #include "ui/gfx/animation/slide_animation.h" |
| 14 | 14 |
| 15 InfoBar::InfoBar(InfoBarService* owner, InfoBarDelegate* delegate) | 15 InfoBar::InfoBar(scoped_ptr<InfoBarDelegate> delegate) |
| 16 : owner_(owner), | 16 : owner_(NULL), |
| 17 delegate_(delegate), | 17 delegate_(delegate.Pass()), |
| 18 container_(NULL), | 18 container_(NULL), |
| 19 animation_(this), | 19 animation_(this), |
| 20 arrow_height_(0), | 20 arrow_height_(0), |
| 21 arrow_target_height_(kDefaultArrowTargetHeight), | 21 arrow_target_height_(kDefaultArrowTargetHeight), |
| 22 arrow_half_width_(0), | 22 arrow_half_width_(0), |
| 23 bar_height_(0), | 23 bar_height_(0), |
| 24 bar_target_height_(kDefaultBarTargetHeight) { | 24 bar_target_height_(kDefaultBarTargetHeight) { |
| 25 DCHECK(owner_ != NULL); | |
| 26 DCHECK(delegate_ != NULL); | 25 DCHECK(delegate_ != NULL); |
| 27 animation_.SetTweenType(gfx::Tween::LINEAR); | 26 animation_.SetTweenType(gfx::Tween::LINEAR); |
| 27 delegate_->set_infobar(this); |
| 28 } | 28 } |
| 29 | 29 |
| 30 InfoBar::~InfoBar() { | 30 InfoBar::~InfoBar() { |
| 31 DCHECK(!owner_); |
| 31 } | 32 } |
| 32 | 33 |
| 33 // static | 34 // static |
| 34 SkColor InfoBar::GetTopColor(InfoBarDelegate::Type infobar_type) { | 35 SkColor InfoBar::GetTopColor(InfoBarDelegate::Type infobar_type) { |
| 35 static const SkColor kWarningBackgroundColorTop = | 36 static const SkColor kWarningBackgroundColorTop = |
| 36 SkColorSetRGB(255, 242, 183); // Yellow | 37 SkColorSetRGB(255, 242, 183); // Yellow |
| 37 static const SkColor kPageActionBackgroundColorTop = | 38 static const SkColor kPageActionBackgroundColorTop = |
| 38 SkColorSetRGB(237, 237, 237); // Gray | 39 SkColorSetRGB(237, 237, 237); // Gray |
| 39 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? | 40 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? |
| 40 kWarningBackgroundColorTop : kPageActionBackgroundColorTop; | 41 kWarningBackgroundColorTop : kPageActionBackgroundColorTop; |
| 41 } | 42 } |
| 42 | 43 |
| 43 // static | 44 // static |
| 44 SkColor InfoBar::GetBottomColor(InfoBarDelegate::Type infobar_type) { | 45 SkColor InfoBar::GetBottomColor(InfoBarDelegate::Type infobar_type) { |
| 45 static const SkColor kWarningBackgroundColorBottom = | 46 static const SkColor kWarningBackgroundColorBottom = |
| 46 SkColorSetRGB(250, 230, 145); // Yellow | 47 SkColorSetRGB(250, 230, 145); // Yellow |
| 47 static const SkColor kPageActionBackgroundColorBottom = | 48 static const SkColor kPageActionBackgroundColorBottom = |
| 48 SkColorSetRGB(217, 217, 217); // Gray | 49 SkColorSetRGB(217, 217, 217); // Gray |
| 49 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? | 50 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? |
| 50 kWarningBackgroundColorBottom : kPageActionBackgroundColorBottom; | 51 kWarningBackgroundColorBottom : kPageActionBackgroundColorBottom; |
| 51 } | 52 } |
| 52 | 53 |
| 54 void InfoBar::SetOwner(InfoBarService* owner) { |
| 55 DCHECK(!owner_); |
| 56 owner_ = owner; |
| 57 delegate_->StoreActiveEntryUniqueID(); |
| 58 PlatformSpecificSetOwner(); |
| 59 } |
| 60 |
| 53 void InfoBar::Show(bool animate) { | 61 void InfoBar::Show(bool animate) { |
| 54 PlatformSpecificShow(animate); | 62 PlatformSpecificShow(animate); |
| 55 if (animate) { | 63 if (animate) { |
| 56 animation_.Show(); | 64 animation_.Show(); |
| 57 } else { | 65 } else { |
| 58 animation_.Reset(1.0); | 66 animation_.Reset(1.0); |
| 59 RecalculateHeights(true); | 67 RecalculateHeights(true); |
| 60 } | 68 } |
| 61 } | 69 } |
| 62 | 70 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 83 RecalculateHeights(false); | 91 RecalculateHeights(false); |
| 84 } | 92 } |
| 85 } | 93 } |
| 86 | 94 |
| 87 void InfoBar::CloseSoon() { | 95 void InfoBar::CloseSoon() { |
| 88 owner_ = NULL; | 96 owner_ = NULL; |
| 89 PlatformSpecificOnCloseSoon(); | 97 PlatformSpecificOnCloseSoon(); |
| 90 MaybeDelete(); | 98 MaybeDelete(); |
| 91 } | 99 } |
| 92 | 100 |
| 101 void InfoBar::RemoveSelf() { |
| 102 if (owner_) |
| 103 owner_->RemoveInfoBar(this); |
| 104 } |
| 105 |
| 93 void InfoBar::SetBarTargetHeight(int height) { | 106 void InfoBar::SetBarTargetHeight(int height) { |
| 94 if (bar_target_height_ != height) { | 107 if (bar_target_height_ != height) { |
| 95 bar_target_height_ = height; | 108 bar_target_height_ = height; |
| 96 RecalculateHeights(false); | 109 RecalculateHeights(false); |
| 97 } | 110 } |
| 98 } | 111 } |
| 99 | 112 |
| 100 void InfoBar::AnimationProgressed(const gfx::Animation* animation) { | 113 void InfoBar::AnimationProgressed(const gfx::Animation* animation) { |
| 101 RecalculateHeights(false); | 114 RecalculateHeights(false); |
| 102 } | 115 } |
| 103 | 116 |
| 104 void InfoBar::RemoveSelf() { | |
| 105 // |owner_| should never be NULL here. If it is, then someone violated what | |
| 106 // they were supposed to do -- e.g. a ConfirmInfoBarDelegate subclass returned | |
| 107 // true from Accept() or Cancel() even though the infobar was already closing. | |
| 108 // In the worst case, if we also switched tabs during that process, then | |
| 109 // |this| has already been destroyed. But if that's the case, then we're | |
| 110 // going to deref a garbage |this| pointer here whether we check |owner_| or | |
| 111 // not, and in other cases (where we're still closing and |this| is valid), | |
| 112 // checking |owner_| here will avoid a NULL deref. | |
| 113 if (owner_) | |
| 114 owner_->RemoveInfoBar(delegate_); | |
| 115 } | |
| 116 | |
| 117 int InfoBar::OffsetY(const gfx::Size& prefsize) const { | 117 int InfoBar::OffsetY(const gfx::Size& prefsize) const { |
| 118 return arrow_height_ + | 118 return arrow_height_ + |
| 119 std::max((bar_target_height_ - prefsize.height()) / 2, 0) - | 119 std::max((bar_target_height_ - prefsize.height()) / 2, 0) - |
| 120 (bar_target_height_ - bar_height_); | 120 (bar_target_height_ - bar_height_); |
| 121 } | 121 } |
| 122 | 122 |
| 123 void InfoBar::AnimationEnded(const gfx::Animation* animation) { | 123 void InfoBar::AnimationEnded(const gfx::Animation* animation) { |
| 124 // When the animation ends, we must ensure the container is notified even if | 124 // When the animation ends, we must ensure the container is notified even if |
| 125 // the heights haven't changed, lest it never get an "animation finished" | 125 // the heights haven't changed, lest it never get an "animation finished" |
| 126 // notification. (If the browser doesn't get this notification, it will not | 126 // notification. (If the browser doesn't get this notification, it will not |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 bool heights_differ = | 166 bool heights_differ = |
| 167 (old_arrow_height != arrow_height_) || (old_bar_height != bar_height_); | 167 (old_arrow_height != arrow_height_) || (old_bar_height != bar_height_); |
| 168 if (heights_differ) | 168 if (heights_differ) |
| 169 PlatformSpecificOnHeightsRecalculated(); | 169 PlatformSpecificOnHeightsRecalculated(); |
| 170 | 170 |
| 171 if (container_ && (heights_differ || force_notify)) | 171 if (container_ && (heights_differ || force_notify)) |
| 172 container_->OnInfoBarStateChanged(animation_.is_animating()); | 172 container_->OnInfoBarStateChanged(animation_.is_animating()); |
| 173 } | 173 } |
| 174 | 174 |
| 175 void InfoBar::MaybeDelete() { | 175 void InfoBar::MaybeDelete() { |
| 176 if (!owner_ && delegate_ && (animation_.GetCurrentValue() == 0.0)) { | 176 if (!owner_ && (animation_.GetCurrentValue() == 0.0)) { |
| 177 if (container_) | 177 if (container_) |
| 178 container_->RemoveInfoBar(this); | 178 container_->RemoveInfoBar(this); |
| 179 delete delegate_; | 179 delete this; |
| 180 delegate_ = NULL; | |
| 181 } | 180 } |
| 182 } | 181 } |
| OLD | NEW |