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 "chrome/browser/infobars/infobar_tab_helper.h" | 5 #include "chrome/browser/infobars/infobar_tab_helper.h" |
6 | 6 |
7 #include "chrome/browser/infobars/infobar.h" | 7 #include "chrome/browser/infobars/infobar.h" |
8 #include "chrome/browser/infobars/infobar_delegate.h" | 8 #include "chrome/browser/infobars/infobar_delegate.h" |
9 #include "chrome/browser/tab_contents/insecure_content_infobar_delegate.h" | 9 #include "chrome/browser/tab_contents/insecure_content_infobar_delegate.h" |
10 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 // Infobars. | 31 // Infobars. |
32 RemoveAllInfoBars(false); | 32 RemoveAllInfoBars(false); |
33 } | 33 } |
34 | 34 |
35 void InfoBarTabHelper::AddInfoBar(InfoBarDelegate* delegate) { | 35 void InfoBarTabHelper::AddInfoBar(InfoBarDelegate* delegate) { |
36 if (!infobars_enabled_) { | 36 if (!infobars_enabled_) { |
37 delegate->InfoBarClosed(); | 37 delegate->InfoBarClosed(); |
38 return; | 38 return; |
39 } | 39 } |
40 | 40 |
41 for (size_t i = 0; i < infobars_.size(); ++i) { | 41 for (InfoBars::const_iterator i(infobars_.begin()); i != infobars_.end(); |
42 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) { | 42 ++i) { |
| 43 if ((*i)->EqualsDelegate(delegate)) { |
| 44 DCHECK_NE(*i, delegate); |
43 delegate->InfoBarClosed(); | 45 delegate->InfoBarClosed(); |
44 return; | 46 return; |
45 } | 47 } |
46 } | 48 } |
47 | 49 |
48 // TODO(pkasting): Consider removing InfoBarTabHelper arg from delegate | 50 // TODO(pkasting): Consider removing InfoBarTabHelper arg from delegate |
49 // constructors and instead using a setter from here. | 51 // constructors and instead using a setter from here. |
50 infobars_.push_back(delegate); | 52 infobars_.push_back(delegate); |
51 // Add ourselves as an observer for navigations the first time a delegate is | 53 // Add ourselves as an observer for navigations the first time a delegate is |
52 // added. We use this notification to expire InfoBars that need to expire on | 54 // added. We use this notification to expire InfoBars that need to expire on |
(...skipping 15 matching lines...) Expand all Loading... |
68 RemoveInfoBarInternal(delegate, true); | 70 RemoveInfoBarInternal(delegate, true); |
69 } | 71 } |
70 | 72 |
71 void InfoBarTabHelper::ReplaceInfoBar(InfoBarDelegate* old_delegate, | 73 void InfoBarTabHelper::ReplaceInfoBar(InfoBarDelegate* old_delegate, |
72 InfoBarDelegate* new_delegate) { | 74 InfoBarDelegate* new_delegate) { |
73 if (!infobars_enabled_) { | 75 if (!infobars_enabled_) { |
74 AddInfoBar(new_delegate); // Deletes the delegate. | 76 AddInfoBar(new_delegate); // Deletes the delegate. |
75 return; | 77 return; |
76 } | 78 } |
77 | 79 |
78 size_t i; | 80 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), |
79 for (i = 0; i < infobars_.size(); ++i) { | 81 old_delegate)); |
80 if (GetInfoBarDelegateAt(i) == old_delegate) | 82 DCHECK(i != infobars_.end()); |
81 break; | |
82 } | |
83 DCHECK_LT(i, infobars_.size()); | |
84 | 83 |
85 infobars_.insert(infobars_.begin() + i, new_delegate); | 84 i = infobars_.insert(i, new_delegate) + 1; |
86 // Remove the old delegate before notifying, so that if any observers call | 85 // Remove the old delegate before notifying, so that if any observers call |
87 // back to AddInfoBar() or similar, we don't dupe-check against this delegate. | 86 // back to AddInfoBar() or similar, we don't dupe-check against this delegate. |
88 infobars_.erase(infobars_.begin() + i + 1); | 87 infobars_.erase(i); |
89 | 88 |
90 old_delegate->clear_owner(); | 89 old_delegate->clear_owner(); |
91 InfoBarReplacedDetails replaced_details(old_delegate, new_delegate); | 90 InfoBarReplacedDetails replaced_details(old_delegate, new_delegate); |
92 content::NotificationService::current()->Notify( | 91 content::NotificationService::current()->Notify( |
93 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, | 92 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, |
94 content::Source<InfoBarTabHelper>(this), | 93 content::Source<InfoBarTabHelper>(this), |
95 content::Details<InfoBarReplacedDetails>(&replaced_details)); | 94 content::Details<InfoBarReplacedDetails>(&replaced_details)); |
96 } | 95 } |
97 | 96 |
98 InfoBarDelegate* InfoBarTabHelper::GetInfoBarDelegateAt(size_t index) { | 97 InfoBarDelegate* InfoBarTabHelper::GetInfoBarDelegateAt(size_t index) { |
99 return infobars_[index]; | 98 return infobars_[index]; |
100 } | 99 } |
101 | 100 |
102 void InfoBarTabHelper::RemoveInfoBarInternal(InfoBarDelegate* delegate, | 101 void InfoBarTabHelper::RemoveInfoBarInternal(InfoBarDelegate* delegate, |
103 bool animate) { | 102 bool animate) { |
104 if (!infobars_enabled_) { | 103 if (!infobars_enabled_) { |
105 DCHECK(infobars_.empty()); | 104 DCHECK(infobars_.empty()); |
106 return; | 105 return; |
107 } | 106 } |
108 | 107 |
109 size_t i; | 108 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), delegate)); |
110 for (i = 0; i < infobars_.size(); ++i) { | 109 DCHECK(i != infobars_.end()); |
111 if (GetInfoBarDelegateAt(i) == delegate) | |
112 break; | |
113 } | |
114 DCHECK_LT(i, infobars_.size()); | |
115 InfoBarDelegate* infobar = infobars_[i]; | |
116 | 110 |
117 infobar->clear_owner(); | 111 delegate->clear_owner(); |
118 // Remove the delegate before notifying, so that if any observers call back to | 112 // Remove the delegate before notifying, so that if any observers call back to |
119 // AddInfoBar() or similar, we don't dupe-check against this delegate. | 113 // AddInfoBar() or similar, we don't dupe-check against this delegate. |
120 infobars_.erase(infobars_.begin() + i); | 114 infobars_.erase(i); |
121 // Remove ourselves as an observer if we are tracking no more InfoBars. | 115 // Remove ourselves as an observer if we are tracking no more InfoBars. |
122 if (infobars_.empty()) { | 116 if (infobars_.empty()) { |
123 registrar_.Remove( | 117 registrar_.Remove( |
124 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 118 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
125 content::Source<NavigationController>( | 119 content::Source<NavigationController>( |
126 &web_contents()->GetController())); | 120 &web_contents()->GetController())); |
127 } | 121 } |
128 | 122 |
129 InfoBarRemovedDetails removed_details(infobar, animate); | 123 InfoBarRemovedDetails removed_details(delegate, animate); |
130 content::NotificationService::current()->Notify( | 124 content::NotificationService::current()->Notify( |
131 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | 125 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
132 content::Source<InfoBarTabHelper>(this), | 126 content::Source<InfoBarTabHelper>(this), |
133 content::Details<InfoBarRemovedDetails>(&removed_details)); | 127 content::Details<InfoBarRemovedDetails>(&removed_details)); |
134 } | 128 } |
135 | 129 |
136 void InfoBarTabHelper::RemoveAllInfoBars(bool animate) { | 130 void InfoBarTabHelper::RemoveAllInfoBars(bool animate) { |
137 while (!infobars_.empty()) | 131 while (!infobars_.empty()) |
138 RemoveInfoBarInternal(GetInfoBarDelegateAt(infobar_count() - 1), animate); | 132 RemoveInfoBarInternal(GetInfoBarDelegateAt(infobar_count() - 1), animate); |
139 } | 133 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 if (delegate->ShouldExpire(committed_details)) | 194 if (delegate->ShouldExpire(committed_details)) |
201 RemoveInfoBar(delegate); | 195 RemoveInfoBar(delegate); |
202 } | 196 } |
203 | 197 |
204 break; | 198 break; |
205 } | 199 } |
206 default: | 200 default: |
207 NOTREACHED(); | 201 NOTREACHED(); |
208 } | 202 } |
209 } | 203 } |
OLD | NEW |