OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_service.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/infobars/insecure_content_infobar_delegate.h" | 9 #include "chrome/browser/infobars/insecure_content_infobar_delegate.h" |
10 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" |
11 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
12 #include "content/public/browser/navigation_controller.h" | 12 #include "content/public/browser/navigation_controller.h" |
13 #include "content/public/browser/notification_service.h" | 13 #include "content/public/browser/notification_service.h" |
14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
15 | 15 |
16 using content::NavigationController; | |
17 using content::WebContents; | |
18 | 16 |
19 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InfoBarTabHelper); | 17 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InfoBarService); |
20 | 18 |
21 void InfoBarService::CreateForWebContents(content::WebContents* web_contents) { | 19 InfoBarDelegate* InfoBarService::AddInfoBar( |
22 return content::WebContentsUserData<InfoBarTabHelper>::CreateForWebContents( | |
23 web_contents); | |
24 } | |
25 | |
26 InfoBarService* InfoBarService::FromWebContents(WebContents* web_contents) { | |
27 return content::WebContentsUserData<InfoBarTabHelper>::FromWebContents( | |
28 web_contents); | |
29 } | |
30 | |
31 const InfoBarService* InfoBarService::FromWebContents( | |
32 const WebContents* web_contents) { | |
33 return content::WebContentsUserData<InfoBarTabHelper>::FromWebContents( | |
34 web_contents); | |
35 } | |
36 | |
37 InfoBarService::~InfoBarService() { | |
38 } | |
39 | |
40 InfoBarTabHelper::InfoBarTabHelper(WebContents* web_contents) | |
41 : content::WebContentsObserver(web_contents), | |
42 infobars_enabled_(true) { | |
43 DCHECK(web_contents); | |
44 registrar_.Add(this, | |
45 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
46 content::Source<WebContents>(web_contents)); | |
47 } | |
48 | |
49 InfoBarTabHelper::~InfoBarTabHelper() { | |
50 // Destroy all remaining InfoBars. It's important to not animate here so that | |
51 // we guarantee that we'll delete all delegates before we do anything else. | |
52 // | |
53 // TODO(pkasting): If there is no InfoBarContainer, this leaks all the | |
54 // InfoBarDelegates. This will be fixed once we call CloseSoon() directly on | |
55 // Infobars. | |
56 RemoveAllInfoBars(false); | |
57 } | |
58 | |
59 void InfoBarTabHelper::SetInfoBarsEnabled(bool enabled) { | |
60 infobars_enabled_ = enabled; | |
61 } | |
62 | |
63 InfoBarDelegate* InfoBarTabHelper::AddInfoBar( | |
64 scoped_ptr<InfoBarDelegate> delegate) { | 20 scoped_ptr<InfoBarDelegate> delegate) { |
65 if (!infobars_enabled_) | 21 if (!infobars_enabled_) |
66 return NULL; | 22 return NULL; |
67 | 23 |
68 for (InfoBars::const_iterator i(infobars_.begin()); i != infobars_.end(); | 24 for (InfoBars::const_iterator i(infobars_.begin()); i != infobars_.end(); |
69 ++i) { | 25 ++i) { |
70 if ((*i)->EqualsDelegate(delegate.get())) { | 26 if ((*i)->EqualsDelegate(delegate.get())) { |
71 DCHECK_NE(*i, delegate.get()); | 27 DCHECK_NE(*i, delegate.get()); |
72 return NULL; | 28 return NULL; |
73 } | 29 } |
74 } | 30 } |
75 | 31 |
76 // TODO(pkasting): Consider removing InfoBarTabHelper arg from delegate | 32 // TODO(pkasting): Consider removing InfoBarService arg from delegate |
77 // constructors and instead using a setter from here. | 33 // constructors and instead using a setter from here. |
78 InfoBarDelegate* delegate_ptr = delegate.release(); | 34 InfoBarDelegate* delegate_ptr = delegate.release(); |
79 infobars_.push_back(delegate_ptr); | 35 infobars_.push_back(delegate_ptr); |
80 // Add ourselves as an observer for navigations the first time a delegate is | 36 // Add ourselves as an observer for navigations the first time a delegate is |
81 // added. We use this notification to expire InfoBars that need to expire on | 37 // added. We use this notification to expire InfoBars that need to expire on |
82 // page transitions. | 38 // page transitions. |
83 if (infobars_.size() == 1) { | 39 if (infobars_.size() == 1) { |
84 registrar_.Add( | 40 registrar_.Add( |
85 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 41 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
86 content::Source<NavigationController>( | 42 content::Source<content::NavigationController>( |
87 &web_contents()->GetController())); | 43 &web_contents()->GetController())); |
88 } | 44 } |
89 | 45 |
90 content::NotificationService::current()->Notify( | 46 content::NotificationService::current()->Notify( |
91 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | 47 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, |
92 content::Source<InfoBarService>(this), | 48 content::Source<InfoBarService>(this), |
93 content::Details<InfoBarAddedDetails>(delegate_ptr)); | 49 content::Details<InfoBarAddedDetails>(delegate_ptr)); |
94 return delegate_ptr; | 50 return delegate_ptr; |
95 } | 51 } |
96 | 52 |
97 void InfoBarTabHelper::RemoveInfoBar(InfoBarDelegate* delegate) { | 53 void InfoBarService::RemoveInfoBar(InfoBarDelegate* delegate) { |
98 RemoveInfoBarInternal(delegate, true); | 54 RemoveInfoBarInternal(delegate, true); |
99 } | 55 } |
100 | 56 |
101 InfoBarDelegate* InfoBarTabHelper::ReplaceInfoBar( | 57 InfoBarDelegate* InfoBarService::ReplaceInfoBar( |
102 InfoBarDelegate* old_delegate, | 58 InfoBarDelegate* old_delegate, |
103 scoped_ptr<InfoBarDelegate> new_delegate) { | 59 scoped_ptr<InfoBarDelegate> new_delegate) { |
104 if (!infobars_enabled_) | 60 if (!infobars_enabled_) |
105 return AddInfoBar(new_delegate.Pass()); // Deletes the delegate. | 61 return AddInfoBar(new_delegate.Pass()); // Deletes the delegate. |
106 | 62 |
107 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), | 63 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), |
108 old_delegate)); | 64 old_delegate)); |
109 DCHECK(i != infobars_.end()); | 65 DCHECK(i != infobars_.end()); |
110 | 66 |
111 InfoBarDelegate* new_delegate_ptr = new_delegate.release(); | 67 InfoBarDelegate* new_delegate_ptr = new_delegate.release(); |
112 i = infobars_.insert(i, new_delegate_ptr); | 68 i = infobars_.insert(i, new_delegate_ptr); |
113 InfoBarReplacedDetails replaced_details(old_delegate, new_delegate_ptr); | 69 InfoBarReplacedDetails replaced_details(old_delegate, new_delegate_ptr); |
114 // Remove the old delegate before notifying, so that if any observers call | 70 // Remove the old delegate before notifying, so that if any observers call |
115 // back to AddInfoBar() or similar, we don't dupe-check against this delegate. | 71 // back to AddInfoBar() or similar, we don't dupe-check against this delegate. |
116 infobars_.erase(++i); | 72 infobars_.erase(++i); |
117 | 73 |
118 old_delegate->clear_owner(); | 74 old_delegate->clear_owner(); |
119 content::NotificationService::current()->Notify( | 75 content::NotificationService::current()->Notify( |
120 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, | 76 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED, |
121 content::Source<InfoBarService>(this), | 77 content::Source<InfoBarService>(this), |
122 content::Details<InfoBarReplacedDetails>(&replaced_details)); | 78 content::Details<InfoBarReplacedDetails>(&replaced_details)); |
123 return new_delegate_ptr; | 79 return new_delegate_ptr; |
124 } | 80 } |
125 | 81 |
126 size_t InfoBarTabHelper::GetInfoBarCount() const { | 82 InfoBarService::InfoBarService(content::WebContents* web_contents) |
127 return infobars_.size(); | 83 : content::WebContentsObserver(web_contents), |
| 84 infobars_enabled_(true) { |
| 85 DCHECK(web_contents); |
| 86 registrar_.Add(this, |
| 87 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 88 content::Source<content::WebContents>(web_contents)); |
128 } | 89 } |
129 | 90 |
130 InfoBarDelegate* InfoBarTabHelper::GetInfoBarDelegateAt(size_t index) { | 91 InfoBarService::~InfoBarService() { |
131 return infobars_[index]; | 92 // Destroy all remaining InfoBars. It's important to not animate here so that |
| 93 // we guarantee that we'll delete all delegates before we do anything else. |
| 94 // |
| 95 // TODO(pkasting): If there is no InfoBarContainer, this leaks all the |
| 96 // InfoBarDelegates. This will be fixed once we call CloseSoon() directly on |
| 97 // Infobars. |
| 98 RemoveAllInfoBars(false); |
132 } | 99 } |
133 | 100 |
134 content::WebContents* InfoBarTabHelper::GetWebContents() { | 101 void InfoBarService::RenderViewGone(base::TerminationStatus status) { |
135 return content::WebContentsObserver::web_contents(); | |
136 } | |
137 | |
138 void InfoBarTabHelper::RenderViewGone(base::TerminationStatus status) { | |
139 RemoveAllInfoBars(true); | 102 RemoveAllInfoBars(true); |
140 } | 103 } |
141 | 104 |
142 bool InfoBarTabHelper::OnMessageReceived(const IPC::Message& message) { | 105 bool InfoBarService::OnMessageReceived(const IPC::Message& message) { |
143 bool handled = true; | 106 bool handled = true; |
144 IPC_BEGIN_MESSAGE_MAP(InfoBarTabHelper, message) | 107 IPC_BEGIN_MESSAGE_MAP(InfoBarService, message) |
145 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, | 108 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent, |
146 OnDidBlockDisplayingInsecureContent) | 109 OnDidBlockDisplayingInsecureContent) |
147 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent, | 110 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent, |
148 OnDidBlockRunningInsecureContent) | 111 OnDidBlockRunningInsecureContent) |
149 IPC_MESSAGE_UNHANDLED(handled = false) | 112 IPC_MESSAGE_UNHANDLED(handled = false) |
150 IPC_END_MESSAGE_MAP() | 113 IPC_END_MESSAGE_MAP() |
151 return handled; | 114 return handled; |
152 } | 115 } |
153 | 116 |
154 void InfoBarTabHelper::Observe(int type, | 117 void InfoBarService::Observe(int type, |
155 const content::NotificationSource& source, | 118 const content::NotificationSource& source, |
156 const content::NotificationDetails& details) { | 119 const content::NotificationDetails& details) { |
157 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { | 120 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { |
158 DCHECK(&web_contents()->GetController() == | 121 DCHECK(&web_contents()->GetController() == |
159 content::Source<NavigationController>(source).ptr()); | 122 content::Source<content::NavigationController>(source).ptr()); |
160 | 123 |
161 content::LoadCommittedDetails& committed_details = | 124 content::LoadCommittedDetails& committed_details = |
162 *(content::Details<content::LoadCommittedDetails>(details).ptr()); | 125 *(content::Details<content::LoadCommittedDetails>(details).ptr()); |
163 | 126 |
164 // NOTE: It is not safe to change the following code to count upwards or | 127 // NOTE: It is not safe to change the following code to count upwards or |
165 // use iterators, as the RemoveInfoBar() call synchronously modifies our | 128 // use iterators, as the RemoveInfoBar() call synchronously modifies our |
166 // delegate list. | 129 // delegate list. |
167 for (size_t i = infobars_.size(); i > 0; --i) { | 130 for (size_t i = infobars_.size(); i > 0; --i) { |
168 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i - 1); | 131 InfoBarDelegate* delegate = infobars_[i - 1]; |
169 if (delegate->ShouldExpire(committed_details)) | 132 if (delegate->ShouldExpire(committed_details)) |
170 RemoveInfoBar(delegate); | 133 RemoveInfoBar(delegate); |
171 } | 134 } |
172 | 135 |
173 return; | 136 return; |
174 } | 137 } |
175 | 138 |
176 DCHECK_EQ(type, content::NOTIFICATION_WEB_CONTENTS_DESTROYED); | 139 DCHECK_EQ(type, content::NOTIFICATION_WEB_CONTENTS_DESTROYED); |
177 // The WebContents is going away; be aggressively paranoid and delete | 140 // The WebContents is going away; be aggressively paranoid and delete |
178 // ourselves lest other parts of the system attempt to add infobars or use | 141 // ourselves lest other parts of the system attempt to add infobars or use |
179 // us otherwise during the destruction. | 142 // us otherwise during the destruction. |
180 DCHECK_EQ(web_contents(), content::Source<WebContents>(source).ptr()); | 143 DCHECK_EQ(web_contents(), |
| 144 content::Source<content::WebContents>(source).ptr()); |
181 web_contents()->RemoveUserData(UserDataKey()); | 145 web_contents()->RemoveUserData(UserDataKey()); |
182 // That was the equivalent of "delete this". This object is now destroyed; | 146 // That was the equivalent of "delete this". This object is now destroyed; |
183 // returning from this function is the only safe thing to do. | 147 // returning from this function is the only safe thing to do. |
184 return; | 148 return; |
185 } | 149 } |
186 void InfoBarTabHelper::RemoveInfoBarInternal(InfoBarDelegate* delegate, | 150 |
187 bool animate) { | 151 void InfoBarService::RemoveInfoBarInternal(InfoBarDelegate* delegate, |
| 152 bool animate) { |
188 if (!infobars_enabled_) { | 153 if (!infobars_enabled_) { |
189 DCHECK(infobars_.empty()); | 154 DCHECK(infobars_.empty()); |
190 return; | 155 return; |
191 } | 156 } |
192 | 157 |
193 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), delegate)); | 158 InfoBars::iterator i(std::find(infobars_.begin(), infobars_.end(), delegate)); |
194 DCHECK(i != infobars_.end()); | 159 DCHECK(i != infobars_.end()); |
195 | 160 |
196 delegate->clear_owner(); | 161 delegate->clear_owner(); |
197 // Remove the delegate before notifying, so that if any observers call back to | 162 // Remove the delegate before notifying, so that if any observers call back to |
198 // AddInfoBar() or similar, we don't dupe-check against this delegate. | 163 // AddInfoBar() or similar, we don't dupe-check against this delegate. |
199 infobars_.erase(i); | 164 infobars_.erase(i); |
200 // Remove ourselves as an observer if we are tracking no more InfoBars. | 165 // Remove ourselves as an observer if we are tracking no more InfoBars. |
201 if (infobars_.empty()) { | 166 if (infobars_.empty()) { |
202 registrar_.Remove( | 167 registrar_.Remove( |
203 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 168 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
204 content::Source<NavigationController>( | 169 content::Source<content::NavigationController>( |
205 &web_contents()->GetController())); | 170 &web_contents()->GetController())); |
206 } | 171 } |
207 | 172 |
208 InfoBarRemovedDetails removed_details(delegate, animate); | 173 InfoBarRemovedDetails removed_details(delegate, animate); |
209 content::NotificationService::current()->Notify( | 174 content::NotificationService::current()->Notify( |
210 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, | 175 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
211 content::Source<InfoBarService>(this), | 176 content::Source<InfoBarService>(this), |
212 content::Details<InfoBarRemovedDetails>(&removed_details)); | 177 content::Details<InfoBarRemovedDetails>(&removed_details)); |
213 } | 178 } |
214 | 179 |
215 void InfoBarTabHelper::RemoveAllInfoBars(bool animate) { | 180 void InfoBarService::RemoveAllInfoBars(bool animate) { |
216 while (!infobars_.empty()) | 181 while (!infobars_.empty()) |
217 RemoveInfoBarInternal(GetInfoBarDelegateAt(GetInfoBarCount() - 1), animate); | 182 RemoveInfoBarInternal(infobars_.back(), animate); |
218 } | 183 } |
219 | 184 |
220 void InfoBarTabHelper::OnDidBlockDisplayingInsecureContent() { | 185 void InfoBarService::OnDidBlockDisplayingInsecureContent() { |
221 InsecureContentInfoBarDelegate::Create( | 186 InsecureContentInfoBarDelegate::Create( |
222 this, InsecureContentInfoBarDelegate::DISPLAY); | 187 this, InsecureContentInfoBarDelegate::DISPLAY); |
223 } | 188 } |
224 | 189 |
225 void InfoBarTabHelper::OnDidBlockRunningInsecureContent() { | 190 void InfoBarService::OnDidBlockRunningInsecureContent() { |
226 InsecureContentInfoBarDelegate::Create(this, | 191 InsecureContentInfoBarDelegate::Create(this, |
227 InsecureContentInfoBarDelegate::RUN); | 192 InsecureContentInfoBarDelegate::RUN); |
228 } | 193 } |
OLD | NEW |