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