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

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

Issue 7810002: Move infobar handling to a tab helper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: update Created 9 years, 3 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/infobars/infobar_tab_helper.h"
6
7 #include "chrome/browser/tab_contents/infobar.h"
8 #include "chrome/browser/tab_contents/infobar_delegate.h"
9 #include "chrome/browser/tab_contents/insecure_content_infobar_delegate.h"
10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
11 #include "chrome/common/chrome_notification_types.h"
12 #include "chrome/common/render_messages.h"
13 #include "content/common/notification_service.h"
14 #include "content/browser/tab_contents/tab_contents.h"
15
16 InfoBarTabHelper::InfoBarTabHelper(TabContentsWrapper* tab_contents)
17 : TabContentsObserver(tab_contents->tab_contents()),
18 infobars_enabled_(true),
19 tab_contents_wrapper_(tab_contents) {
20 DCHECK(tab_contents);
21 }
22
23 InfoBarTabHelper::~InfoBarTabHelper() {
24 // Destroy all remaining InfoBars. It's important to not animate here so that
25 // we guarantee that we'll delete all delegates before we do anything else.
26 //
27 // TODO(pkasting): If there is no InfoBarContainer, this leaks all the
28 // InfoBarDelegates. This will be fixed once we call CloseSoon() directly on
29 // Infobars.
30 RemoveAllInfoBars(false);
31 }
32
33 void InfoBarTabHelper::AddInfoBar(InfoBarDelegate* delegate) {
34 if (!infobars_enabled_) {
35 delegate->InfoBarClosed();
36 return;
37 }
38
39 for (size_t i = 0; i < infobars_.size(); ++i) {
40 if (GetInfoBarDelegateAt(i)->EqualsDelegate(delegate)) {
41 delegate->InfoBarClosed();
42 return;
43 }
44 }
45
46 infobars_.push_back(delegate);
47 NotificationService::current()->Notify(
48 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
49 Source<TabContentsWrapper>(tab_contents_wrapper_),
50 Details<InfoBarAddedDetails>(delegate));
51
52 // Add ourselves as an observer for navigations the first time a delegate is
53 // added. We use this notification to expire InfoBars that need to expire on
54 // page transitions.
55 if (infobars_.size() == 1) {
56 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
57 Source<NavigationController>(&tab_contents()->controller()));
58 }
59 }
60
61 void InfoBarTabHelper::RemoveInfoBar(InfoBarDelegate* delegate) {
62 RemoveInfoBarInternal(delegate, true);
63 }
64
65 void InfoBarTabHelper::ReplaceInfoBar(InfoBarDelegate* old_delegate,
66 InfoBarDelegate* new_delegate) {
67 if (!infobars_enabled_) {
68 AddInfoBar(new_delegate); // Deletes the delegate.
69 return;
70 }
71
72 size_t i;
73 for (i = 0; i < infobars_.size(); ++i) {
74 if (GetInfoBarDelegateAt(i) == old_delegate)
75 break;
76 }
77 DCHECK_LT(i, infobars_.size());
78
79 infobars_.insert(infobars_.begin() + i, new_delegate);
80
81 old_delegate->clear_owner();
82 InfoBarReplacedDetails replaced_details(old_delegate, new_delegate);
83 NotificationService::current()->Notify(
84 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REPLACED,
85 Source<TabContentsWrapper>(tab_contents_wrapper_),
86 Details<InfoBarReplacedDetails>(&replaced_details));
87
88 infobars_.erase(infobars_.begin() + i + 1);
89 }
90
91 InfoBarDelegate* InfoBarTabHelper::GetInfoBarDelegateAt(size_t index) {
92 return infobars_[index];
93 }
94
95 void InfoBarTabHelper::RemoveInfoBarInternal(InfoBarDelegate* delegate,
96 bool animate) {
97 if (!infobars_enabled_) {
98 DCHECK(infobars_.empty());
99 return;
100 }
101
102 size_t i;
103 for (i = 0; i < infobars_.size(); ++i) {
104 if (GetInfoBarDelegateAt(i) == delegate)
105 break;
106 }
107 DCHECK_LT(i, infobars_.size());
108 InfoBarDelegate* infobar = infobars_[i];
109
110 infobar->clear_owner();
111 InfoBarRemovedDetails removed_details(infobar, animate);
112 NotificationService::current()->Notify(
113 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
114 Source<TabContentsWrapper>(tab_contents_wrapper_),
115 Details<InfoBarRemovedDetails>(&removed_details));
116
117 infobars_.erase(infobars_.begin() + i);
118 // Remove ourselves as an observer if we are tracking no more InfoBars. Check
119 // to see if the TabContents is still there, as this may be called during the
120 // TabContentsWrapper destructor after the TabContents is gone.
121 if (infobars_.empty() && tab_contents()) {
122 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
123 Source<NavigationController>(&tab_contents()->controller()));
124 }
125 }
126
127 void InfoBarTabHelper::RemoveAllInfoBars(bool animate) {
128 while (!infobars_.empty())
129 RemoveInfoBarInternal(GetInfoBarDelegateAt(infobar_count() - 1), animate);
130 }
131
132 void InfoBarTabHelper::OnDidBlockDisplayingInsecureContent() {
133 // At most one infobar and do not supersede the stronger running content bar.
134 for (size_t i = 0; i < infobars_.size(); ++i) {
135 if (GetInfoBarDelegateAt(i)->AsInsecureContentInfoBarDelegate())
136 return;
137 }
138 AddInfoBar(new InsecureContentInfoBarDelegate(tab_contents_wrapper_,
139 InsecureContentInfoBarDelegate::DISPLAY));
140 }
141
142 void InfoBarTabHelper::OnDidBlockRunningInsecureContent() {
143 // At most one infobar superseding any weaker displaying content bar.
144 for (size_t i = 0; i < infobars_.size(); ++i) {
145 InsecureContentInfoBarDelegate* delegate =
146 GetInfoBarDelegateAt(i)->AsInsecureContentInfoBarDelegate();
147 if (delegate) {
148 if (delegate->type() != InsecureContentInfoBarDelegate::RUN) {
149 ReplaceInfoBar(delegate, new InsecureContentInfoBarDelegate(
150 tab_contents_wrapper_,
151 InsecureContentInfoBarDelegate::RUN));
152 }
153 return;
154 }
155 }
156 AddInfoBar(new InsecureContentInfoBarDelegate(tab_contents_wrapper_,
157 InsecureContentInfoBarDelegate::RUN));
158 }
159
160 void InfoBarTabHelper::RenderViewGone() {
161 RemoveAllInfoBars(true);
162 }
163
164 bool InfoBarTabHelper::OnMessageReceived(const IPC::Message& message) {
165 bool handled = true;
166 IPC_BEGIN_MESSAGE_MAP(InfoBarTabHelper, message)
167 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockDisplayingInsecureContent,
168 OnDidBlockDisplayingInsecureContent)
169 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_DidBlockRunningInsecureContent,
170 OnDidBlockRunningInsecureContent)
171 IPC_MESSAGE_UNHANDLED(handled = false)
172 IPC_END_MESSAGE_MAP()
173 return handled;
174 }
175
176 void InfoBarTabHelper::Observe(int type,
177 const NotificationSource& source,
178 const NotificationDetails& details) {
179 switch (type) {
180 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: {
181 DCHECK(&tab_contents()->controller() ==
182 Source<NavigationController>(source).ptr());
183
184 content::LoadCommittedDetails& committed_details =
185 *(Details<content::LoadCommittedDetails>(details).ptr());
186
187 // NOTE: It is not safe to change the following code to count upwards or
188 // use iterators, as the RemoveInfoBar() call synchronously modifies our
189 // delegate list.
190 for (size_t i = infobars_.size(); i > 0; --i) {
191 InfoBarDelegate* delegate = GetInfoBarDelegateAt(i - 1);
192 if (delegate->ShouldExpire(committed_details))
193 RemoveInfoBar(delegate);
194 }
195
196 break;
197 }
198 default:
199 NOTREACHED();
200 }
201 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698