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(scoped_ptr<InfoBarDelegate> delegate) | 15 InfoBar::InfoBar(InfoBarService* owner, InfoBarDelegate* delegate) |
16 : owner_(NULL), | 16 : owner_(owner), |
17 delegate_(delegate.Pass()), | 17 delegate_(delegate), |
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); |
25 DCHECK(delegate_ != NULL); | 26 DCHECK(delegate_ != NULL); |
26 animation_.SetTweenType(gfx::Tween::LINEAR); | 27 animation_.SetTweenType(gfx::Tween::LINEAR); |
27 delegate_->set_infobar(this); | |
28 } | 28 } |
29 | 29 |
30 InfoBar::~InfoBar() { | 30 InfoBar::~InfoBar() { |
31 DCHECK(!owner_); | |
32 } | 31 } |
33 | 32 |
34 // static | 33 // static |
35 SkColor InfoBar::GetTopColor(InfoBarDelegate::Type infobar_type) { | 34 SkColor InfoBar::GetTopColor(InfoBarDelegate::Type infobar_type) { |
36 static const SkColor kWarningBackgroundColorTop = | 35 static const SkColor kWarningBackgroundColorTop = |
37 SkColorSetRGB(255, 242, 183); // Yellow | 36 SkColorSetRGB(255, 242, 183); // Yellow |
38 static const SkColor kPageActionBackgroundColorTop = | 37 static const SkColor kPageActionBackgroundColorTop = |
39 SkColorSetRGB(237, 237, 237); // Gray | 38 SkColorSetRGB(237, 237, 237); // Gray |
40 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? | 39 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? |
41 kWarningBackgroundColorTop : kPageActionBackgroundColorTop; | 40 kWarningBackgroundColorTop : kPageActionBackgroundColorTop; |
42 } | 41 } |
43 | 42 |
44 // static | 43 // static |
45 SkColor InfoBar::GetBottomColor(InfoBarDelegate::Type infobar_type) { | 44 SkColor InfoBar::GetBottomColor(InfoBarDelegate::Type infobar_type) { |
46 static const SkColor kWarningBackgroundColorBottom = | 45 static const SkColor kWarningBackgroundColorBottom = |
47 SkColorSetRGB(250, 230, 145); // Yellow | 46 SkColorSetRGB(250, 230, 145); // Yellow |
48 static const SkColor kPageActionBackgroundColorBottom = | 47 static const SkColor kPageActionBackgroundColorBottom = |
49 SkColorSetRGB(217, 217, 217); // Gray | 48 SkColorSetRGB(217, 217, 217); // Gray |
50 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? | 49 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? |
51 kWarningBackgroundColorBottom : kPageActionBackgroundColorBottom; | 50 kWarningBackgroundColorBottom : kPageActionBackgroundColorBottom; |
52 } | 51 } |
53 | 52 |
54 void InfoBar::SetOwner(InfoBarService* owner) { | |
55 DCHECK(!owner_); | |
56 owner_ = owner; | |
57 delegate_->StoreActiveEntryUniqueID(); | |
58 PlatformSpecificSetOwner(); | |
59 } | |
60 | |
61 void InfoBar::Show(bool animate) { | 53 void InfoBar::Show(bool animate) { |
62 PlatformSpecificShow(animate); | 54 PlatformSpecificShow(animate); |
63 if (animate) { | 55 if (animate) { |
64 animation_.Show(); | 56 animation_.Show(); |
65 } else { | 57 } else { |
66 animation_.Reset(1.0); | 58 animation_.Reset(1.0); |
67 RecalculateHeights(true); | 59 RecalculateHeights(true); |
68 } | 60 } |
69 } | 61 } |
70 | 62 |
(...skipping 20 matching lines...) Expand all Loading... |
91 RecalculateHeights(false); | 83 RecalculateHeights(false); |
92 } | 84 } |
93 } | 85 } |
94 | 86 |
95 void InfoBar::CloseSoon() { | 87 void InfoBar::CloseSoon() { |
96 owner_ = NULL; | 88 owner_ = NULL; |
97 PlatformSpecificOnCloseSoon(); | 89 PlatformSpecificOnCloseSoon(); |
98 MaybeDelete(); | 90 MaybeDelete(); |
99 } | 91 } |
100 | 92 |
101 void InfoBar::RemoveSelf() { | |
102 if (owner_) | |
103 owner_->RemoveInfoBar(this); | |
104 } | |
105 | |
106 void InfoBar::SetBarTargetHeight(int height) { | 93 void InfoBar::SetBarTargetHeight(int height) { |
107 if (bar_target_height_ != height) { | 94 if (bar_target_height_ != height) { |
108 bar_target_height_ = height; | 95 bar_target_height_ = height; |
109 RecalculateHeights(false); | 96 RecalculateHeights(false); |
110 } | 97 } |
111 } | 98 } |
112 | 99 |
113 void InfoBar::AnimationProgressed(const gfx::Animation* animation) { | 100 void InfoBar::AnimationProgressed(const gfx::Animation* animation) { |
114 RecalculateHeights(false); | 101 RecalculateHeights(false); |
115 } | 102 } |
116 | 103 |
| 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_ && (animation_.GetCurrentValue() == 0.0)) { | 176 if (!owner_ && delegate_ && (animation_.GetCurrentValue() == 0.0)) { |
177 if (container_) | 177 if (container_) |
178 container_->RemoveInfoBar(this); | 178 container_->RemoveInfoBar(this); |
179 delete this; | 179 delete delegate_; |
| 180 delegate_ = NULL; |
180 } | 181 } |
181 } | 182 } |
OLD | NEW |