Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: chrome/browser/infobars/infobar.cc

Issue 22694006: Infobar system refactor. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 SkColor GetInfoBarTopColor(InfoBarDelegate::Type infobar_type) { 15 InfoBar::InfoBar(scoped_ptr<InfoBarDelegate> delegate)
16 : owner_(NULL),
17 delegate_(delegate.Pass()),
18 container_(NULL),
19 animation_(this),
20 arrow_height_(0),
21 arrow_target_height_(kDefaultArrowTargetHeight),
22 arrow_half_width_(0),
23 bar_height_(0),
24 bar_target_height_(kDefaultBarTargetHeight) {
25 DCHECK(delegate_ != NULL);
26 animation_.SetTweenType(gfx::Tween::LINEAR);
27 delegate_->set_infobar(this);
28 }
29
30 InfoBar::~InfoBar() {
31 DCHECK(!owner_);
32 }
33
34 // static
35 SkColor InfoBar::GetTopColor(InfoBarDelegate::Type infobar_type) {
16 static const SkColor kWarningBackgroundColorTop = 36 static const SkColor kWarningBackgroundColorTop =
17 SkColorSetRGB(255, 242, 183); // Yellow 37 SkColorSetRGB(255, 242, 183); // Yellow
18 static const SkColor kPageActionBackgroundColorTop = 38 static const SkColor kPageActionBackgroundColorTop =
19 SkColorSetRGB(237, 237, 237); // Gray 39 SkColorSetRGB(237, 237, 237); // Gray
20 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? 40 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ?
21 kWarningBackgroundColorTop : kPageActionBackgroundColorTop; 41 kWarningBackgroundColorTop : kPageActionBackgroundColorTop;
22 } 42 }
23 43
24 SkColor GetInfoBarBottomColor(InfoBarDelegate::Type infobar_type) { 44 // static
45 SkColor InfoBar::GetBottomColor(InfoBarDelegate::Type infobar_type) {
25 static const SkColor kWarningBackgroundColorBottom = 46 static const SkColor kWarningBackgroundColorBottom =
26 SkColorSetRGB(250, 230, 145); // Yellow 47 SkColorSetRGB(250, 230, 145); // Yellow
27 static const SkColor kPageActionBackgroundColorBottom = 48 static const SkColor kPageActionBackgroundColorBottom =
28 SkColorSetRGB(217, 217, 217); // Gray 49 SkColorSetRGB(217, 217, 217); // Gray
29 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ? 50 return (infobar_type == InfoBarDelegate::WARNING_TYPE) ?
30 kWarningBackgroundColorBottom : kPageActionBackgroundColorBottom; 51 kWarningBackgroundColorBottom : kPageActionBackgroundColorBottom;
31 } 52 }
32 53
33 InfoBar::InfoBar(InfoBarService* owner, InfoBarDelegate* delegate) 54 void InfoBar::SetOwner(InfoBarService* owner) {
34 : owner_(owner), 55 DCHECK(!owner_);
35 delegate_(delegate), 56 owner_ = owner;
36 container_(NULL), 57 delegate_->StoreActiveEntryUniqueID();
37 animation_(this), 58 PlatformSpecificSetOwner();
38 arrow_height_(0),
39 arrow_target_height_(kDefaultArrowTargetHeight),
40 arrow_half_width_(0),
41 bar_height_(0),
42 bar_target_height_(kDefaultBarTargetHeight) {
43 DCHECK(owner_ != NULL);
44 DCHECK(delegate_ != NULL);
45 animation_.SetTweenType(gfx::Tween::LINEAR);
46 }
47
48 InfoBar::~InfoBar() {
49 } 59 }
50 60
51 void InfoBar::Show(bool animate) { 61 void InfoBar::Show(bool animate) {
52 PlatformSpecificShow(animate); 62 PlatformSpecificShow(animate);
53 if (animate) { 63 if (animate) {
54 animation_.Show(); 64 animation_.Show();
55 } else { 65 } else {
56 animation_.Reset(1.0); 66 animation_.Reset(1.0);
57 RecalculateHeights(true); 67 RecalculateHeights(true);
58 } 68 }
(...skipping 22 matching lines...) Expand all
81 RecalculateHeights(false); 91 RecalculateHeights(false);
82 } 92 }
83 } 93 }
84 94
85 void InfoBar::CloseSoon() { 95 void InfoBar::CloseSoon() {
86 owner_ = NULL; 96 owner_ = NULL;
87 PlatformSpecificOnCloseSoon(); 97 PlatformSpecificOnCloseSoon();
88 MaybeDelete(); 98 MaybeDelete();
89 } 99 }
90 100
91 void InfoBar::AnimationProgressed(const gfx::Animation* animation) {
92 RecalculateHeights(false);
93 }
94
95 void InfoBar::RemoveSelf() { 101 void InfoBar::RemoveSelf() {
96 // |owner_| should never be NULL here. If it is, then someone violated what
97 // they were supposed to do -- e.g. a ConfirmInfoBarDelegate subclass returned
98 // true from Accept() or Cancel() even though the infobar was already closing.
99 // In the worst case, if we also switched tabs during that process, then
100 // |this| has already been destroyed. But if that's the case, then we're
101 // going to deref a garbage |this| pointer here whether we check |owner_| or
102 // not, and in other cases (where we're still closing and |this| is valid),
103 // checking |owner_| here will avoid a NULL deref.
104 if (owner_) 102 if (owner_)
105 owner_->RemoveInfoBar(delegate_); 103 owner_->RemoveInfoBar(this);
106 } 104 }
107 105
108 void InfoBar::SetBarTargetHeight(int height) { 106 void InfoBar::SetBarTargetHeight(int height) {
109 if (bar_target_height_ != height) { 107 if (bar_target_height_ != height) {
110 bar_target_height_ = height; 108 bar_target_height_ = height;
111 RecalculateHeights(false); 109 RecalculateHeights(false);
112 } 110 }
113 } 111 }
114 112
113 void InfoBar::AnimationProgressed(const gfx::Animation* animation) {
114 RecalculateHeights(false);
115 }
116
115 int InfoBar::OffsetY(const gfx::Size& prefsize) const { 117 int InfoBar::OffsetY(const gfx::Size& prefsize) const {
116 return arrow_height_ + 118 return arrow_height_ +
117 std::max((bar_target_height_ - prefsize.height()) / 2, 0) - 119 std::max((bar_target_height_ - prefsize.height()) / 2, 0) -
118 (bar_target_height_ - bar_height_); 120 (bar_target_height_ - bar_height_);
119 } 121 }
120 122
121 void InfoBar::AnimationEnded(const gfx::Animation* animation) { 123 void InfoBar::AnimationEnded(const gfx::Animation* animation) {
122 // 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
123 // 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"
124 // 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
164 bool heights_differ = 166 bool heights_differ =
165 (old_arrow_height != arrow_height_) || (old_bar_height != bar_height_); 167 (old_arrow_height != arrow_height_) || (old_bar_height != bar_height_);
166 if (heights_differ) 168 if (heights_differ)
167 PlatformSpecificOnHeightsRecalculated(); 169 PlatformSpecificOnHeightsRecalculated();
168 170
169 if (container_ && (heights_differ || force_notify)) 171 if (container_ && (heights_differ || force_notify))
170 container_->OnInfoBarStateChanged(animation_.is_animating()); 172 container_->OnInfoBarStateChanged(animation_.is_animating());
171 } 173 }
172 174
173 void InfoBar::MaybeDelete() { 175 void InfoBar::MaybeDelete() {
174 if (!owner_ && delegate_ && (animation_.GetCurrentValue() == 0.0)) { 176 if (!owner_ && (animation_.GetCurrentValue() == 0.0)) {
175 if (container_) 177 if (container_)
176 container_->RemoveInfoBar(this); 178 container_->RemoveInfoBar(this);
177 delete delegate_; 179 delete this;
178 delegate_ = NULL;
179 } 180 }
180 } 181 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698