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/infobars/infobar.h" | 12 #include "chrome/browser/infobars/infobar.h" |
13 #include "chrome/browser/infobars/infobar_delegate.h" | 13 #include "chrome/browser/infobars/infobar_delegate.h" |
14 #include "chrome/browser/infobars/infobar_service.h" | |
15 #include "ui/gfx/animation/slide_animation.h" | 14 #include "ui/gfx/animation/slide_animation.h" |
16 | 15 |
17 InfoBarContainer::Delegate::~Delegate() { | 16 InfoBarContainer::Delegate::~Delegate() { |
18 } | 17 } |
19 | 18 |
20 InfoBarContainer::InfoBarContainer(Delegate* delegate) | 19 InfoBarContainer::InfoBarContainer(Delegate* delegate) |
21 : delegate_(delegate), | 20 : delegate_(delegate), |
22 infobar_service_(NULL), | 21 infobar_manager_(NULL), |
23 top_arrow_target_height_(InfoBar::kDefaultArrowTargetHeight) { | 22 top_arrow_target_height_(InfoBar::kDefaultArrowTargetHeight) { |
24 } | 23 } |
25 | 24 |
26 InfoBarContainer::~InfoBarContainer() { | 25 InfoBarContainer::~InfoBarContainer() { |
27 // RemoveAllInfoBarsForDestruction() should have already cleared our infobars. | 26 // RemoveAllInfoBarsForDestruction() should have already cleared our infobars. |
28 DCHECK(infobars_.empty()); | 27 DCHECK(infobars_.empty()); |
29 if (infobar_service_) | 28 if (infobar_manager_) |
30 infobar_service_->RemoveObserver(this); | 29 infobar_manager_->RemoveObserver(this); |
31 } | 30 } |
32 | 31 |
33 void InfoBarContainer::ChangeInfoBarService(InfoBarService* infobar_service) { | 32 void InfoBarContainer::ChangeInfoBarManager(InfoBarManager* infobar_manager) { |
34 if (infobar_service_) | 33 if (infobar_manager_) |
35 infobar_service_->RemoveObserver(this); | 34 infobar_manager_->RemoveObserver(this); |
36 | 35 |
37 // Hides all infobars in this container without animation. | 36 // Hides all infobars in this container without animation. |
38 while (!infobars_.empty()) { | 37 while (!infobars_.empty()) { |
39 InfoBar* infobar = infobars_.front(); | 38 InfoBar* infobar = infobars_.front(); |
40 // Inform the infobar that it's hidden. If it was already closing, this | 39 // Inform the infobar that it's hidden. If it was already closing, this |
41 // deletes it. Otherwise, this ensures the infobar will be deleted if it's | 40 // deletes it. Otherwise, this ensures the infobar will be deleted if it's |
42 // closed while it's not in an InfoBarContainer. | 41 // closed while it's not in an InfoBarContainer. |
43 infobar->Hide(false); | 42 infobar->Hide(false); |
44 } | 43 } |
45 | 44 |
46 infobar_service_ = infobar_service; | 45 infobar_manager_ = infobar_manager; |
47 if (infobar_service_) { | 46 if (infobar_manager_) { |
48 infobar_service_->AddObserver(this); | 47 infobar_manager_->AddObserver(this); |
49 | 48 |
50 for (size_t i = 0; i < infobar_service_->infobar_count(); ++i) { | 49 for (size_t i = 0; i < infobar_manager_->infobar_count(); ++i) { |
51 // As when we removed the infobars above, we prevent callbacks to | 50 // As when we removed the infobars above, we prevent callbacks to |
52 // OnInfoBarStateChanged() for each infobar. | 51 // OnInfoBarStateChanged() for each infobar. |
53 AddInfoBar(infobar_service_->infobar_at(i), i, false, NO_CALLBACK); | 52 AddInfoBar(infobar_manager_->infobar_at(i), i, false, NO_CALLBACK); |
54 } | 53 } |
55 } | 54 } |
56 | 55 |
57 // Now that everything is up to date, signal the delegate to re-layout. | 56 // Now that everything is up to date, signal the delegate to re-layout. |
58 OnInfoBarStateChanged(false); | 57 OnInfoBarStateChanged(false); |
59 } | 58 } |
60 | 59 |
61 int InfoBarContainer::GetVerticalOverlap(int* total_height) { | 60 int InfoBarContainer::GetVerticalOverlap(int* total_height) { |
62 // Our |total_height| is the sum of the preferred heights of the InfoBars | 61 // Our |total_height| is the sum of the preferred heights of the InfoBars |
63 // contained within us plus the |vertical_overlap|. | 62 // contained within us plus the |vertical_overlap|. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 infobars_.erase(i); | 99 infobars_.erase(i); |
101 } | 100 } |
102 | 101 |
103 void InfoBarContainer::RemoveAllInfoBarsForDestruction() { | 102 void InfoBarContainer::RemoveAllInfoBarsForDestruction() { |
104 // Before we remove any children, we reset |delegate_|, so that no removals | 103 // Before we remove any children, we reset |delegate_|, so that no removals |
105 // will result in us trying to call | 104 // will result in us trying to call |
106 // delegate_->InfoBarContainerStateChanged(). This is important because at | 105 // delegate_->InfoBarContainerStateChanged(). This is important because at |
107 // this point |delegate_| may be shutting down, and it's at best unimportant | 106 // this point |delegate_| may be shutting down, and it's at best unimportant |
108 // and at worst disastrous to call that. | 107 // and at worst disastrous to call that. |
109 delegate_ = NULL; | 108 delegate_ = NULL; |
110 ChangeInfoBarService(NULL); | 109 ChangeInfoBarManager(NULL); |
111 } | 110 } |
112 | 111 |
113 void InfoBarContainer::OnInfoBarAdded(InfoBar* infobar) { | 112 void InfoBarContainer::OnInfoBarAdded(InfoBar* infobar) { |
114 AddInfoBar(infobar, infobars_.size(), true, WANT_CALLBACK); | 113 AddInfoBar(infobar, infobars_.size(), true, WANT_CALLBACK); |
115 } | 114 } |
116 | 115 |
117 void InfoBarContainer::OnInfoBarRemoved(InfoBar* infobar, bool animate) { | 116 void InfoBarContainer::OnInfoBarRemoved(InfoBar* infobar, bool animate) { |
118 infobar->Hide(animate); | 117 infobar->Hide(animate); |
119 UpdateInfoBarArrowTargetHeights(); | 118 UpdateInfoBarArrowTargetHeights(); |
120 } | 119 } |
121 | 120 |
122 void InfoBarContainer::OnInfoBarReplaced(InfoBar* old_infobar, | 121 void InfoBarContainer::OnInfoBarReplaced(InfoBar* old_infobar, |
123 InfoBar* new_infobar) { | 122 InfoBar* new_infobar) { |
124 PlatformSpecificReplaceInfoBar(old_infobar, new_infobar); | 123 PlatformSpecificReplaceInfoBar(old_infobar, new_infobar); |
125 InfoBars::const_iterator i(std::find(infobars_.begin(), infobars_.end(), | 124 InfoBars::const_iterator i(std::find(infobars_.begin(), infobars_.end(), |
126 old_infobar)); | 125 old_infobar)); |
127 DCHECK(i != infobars_.end()); | 126 DCHECK(i != infobars_.end()); |
128 size_t position = i - infobars_.begin(); | 127 size_t position = i - infobars_.begin(); |
129 old_infobar->Hide(false); | 128 old_infobar->Hide(false); |
130 AddInfoBar(new_infobar, position, false, WANT_CALLBACK); | 129 AddInfoBar(new_infobar, position, false, WANT_CALLBACK); |
131 } | 130 } |
132 | 131 |
133 void InfoBarContainer::OnServiceShuttingDown(InfoBarService* service) { | 132 void InfoBarContainer::OnManagerShuttingDown(InfoBarManager* manager) { |
134 DCHECK_EQ(infobar_service_, service); | 133 DCHECK_EQ(infobar_manager_, manager); |
135 infobar_service_->RemoveObserver(this); | 134 infobar_manager_->RemoveObserver(this); |
136 infobar_service_ = NULL; | 135 infobar_manager_ = NULL; |
137 } | 136 } |
138 | 137 |
139 void InfoBarContainer::AddInfoBar(InfoBar* infobar, | 138 void InfoBarContainer::AddInfoBar(InfoBar* infobar, |
140 size_t position, | 139 size_t position, |
141 bool animate, | 140 bool animate, |
142 CallbackStatus callback_status) { | 141 CallbackStatus callback_status) { |
143 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == | 142 DCHECK(std::find(infobars_.begin(), infobars_.end(), infobar) == |
144 infobars_.end()); | 143 infobars_.end()); |
145 DCHECK_LE(position, infobars_.size()); | 144 DCHECK_LE(position, infobars_.size()); |
146 infobars_.insert(infobars_.begin() + position, infobar); | 145 infobars_.insert(infobars_.begin() + position, infobar); |
(...skipping 20 matching lines...) Expand all Loading... |
167 const_cast<const InfoBar*>(infobars_.front())->animation(); | 166 const_cast<const InfoBar*>(infobars_.front())->animation(); |
168 if ((infobar_index > 1) || first_infobar_animation.IsShowing()) | 167 if ((infobar_index > 1) || first_infobar_animation.IsShowing()) |
169 return InfoBar::kDefaultArrowTargetHeight; | 168 return InfoBar::kDefaultArrowTargetHeight; |
170 // When the first infobar is animating closed, we animate the second infobar's | 169 // When the first infobar is animating closed, we animate the second infobar's |
171 // arrow target height from the default to the top target height. Note that | 170 // arrow target height from the default to the top target height. Note that |
172 // the animation values here are going from 1.0 -> 0.0 as the top bar closes. | 171 // the animation values here are going from 1.0 -> 0.0 as the top bar closes. |
173 return top_arrow_target_height_ + static_cast<int>( | 172 return top_arrow_target_height_ + static_cast<int>( |
174 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * | 173 (InfoBar::kDefaultArrowTargetHeight - top_arrow_target_height_) * |
175 first_infobar_animation.GetCurrentValue()); | 174 first_infobar_animation.GetCurrentValue()); |
176 } | 175 } |
OLD | NEW |