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/managed_mode/managed_mode_navigation_observer.h" | 5 #include "chrome/browser/managed_mode/managed_mode_navigation_observer.h" |
6 | 6 |
7 #include "base/bind.h" | |
7 #include "base/i18n/rtl.h" | 8 #include "base/i18n/rtl.h" |
9 #include "base/string_number_conversions.h" | |
8 #include "chrome/browser/api/infobars/simple_alert_infobar_delegate.h" | 10 #include "chrome/browser/api/infobars/simple_alert_infobar_delegate.h" |
9 #include "chrome/browser/infobars/infobar_tab_helper.h" | 11 #include "chrome/browser/infobars/infobar_tab_helper.h" |
10 #include "chrome/browser/managed_mode/managed_mode.h" | 12 #include "chrome/browser/managed_mode/managed_mode.h" |
11 #include "chrome/browser/managed_mode/managed_mode_interstitial.h" | 13 #include "chrome/browser/managed_mode/managed_mode_interstitial.h" |
12 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" | 14 #include "chrome/browser/managed_mode/managed_mode_url_filter.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 17 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
16 #include "chrome/common/jstemplate_builder.h" | 18 #include "chrome/common/jstemplate_builder.h" |
17 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
18 #include "chrome/common/url_constants.h" | 20 #include "chrome/common/url_constants.h" |
19 #include "content/public/browser/web_contents_delegate.h" | 21 #include "content/public/browser/web_contents_delegate.h" |
20 #include "content/public/common/frame_navigate_params.h" | 22 #include "content/public/common/frame_navigate_params.h" |
21 #include "grit/generated_resources.h" | 23 #include "grit/generated_resources.h" |
22 #include "grit/locale_settings.h" | 24 #include "grit/locale_settings.h" |
23 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
24 | 26 |
25 namespace { | 27 namespace { |
26 | 28 |
29 bool IsInList(const ListValue *list, const std::string& url_to_add) { | |
30 return list->Find(*Value::CreateStringValue(url_to_add)) != list->end(); | |
Bernhard Bauer
2012/11/16 15:04:46
This leaks a StringValue. You could just iterate o
Sergiu
2012/11/26 14:48:08
Moved to managed_mode.cc and declared a StringValu
| |
31 } | |
32 | |
27 class ManagedModeWarningInfobarDelegate : public ConfirmInfoBarDelegate { | 33 class ManagedModeWarningInfobarDelegate : public ConfirmInfoBarDelegate { |
28 public: | 34 public: |
29 explicit ManagedModeWarningInfobarDelegate( | 35 explicit ManagedModeWarningInfobarDelegate( |
30 InfoBarTabHelper* infobar_tab_helper); | 36 InfoBarTabHelper* infobar_tab_helper); |
31 | 37 |
32 private: | 38 private: |
33 virtual ~ManagedModeWarningInfobarDelegate(); | 39 virtual ~ManagedModeWarningInfobarDelegate(); |
34 | 40 |
35 // ConfirmInfoBarDelegate overrides: | 41 // ConfirmInfoBarDelegate overrides: |
36 virtual string16 GetMessageText() const OVERRIDE; | 42 virtual string16 GetMessageText() const OVERRIDE; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 | 95 |
90 bool ManagedModeWarningInfobarDelegate::ShouldExpire( | 96 bool ManagedModeWarningInfobarDelegate::ShouldExpire( |
91 const content::LoadCommittedDetails& details) const { | 97 const content::LoadCommittedDetails& details) const { |
92 // ManagedModeNavigationObserver removes us below. | 98 // ManagedModeNavigationObserver removes us below. |
93 return false; | 99 return false; |
94 } | 100 } |
95 | 101 |
96 void ManagedModeWarningInfobarDelegate::InfoBarDismissed() { | 102 void ManagedModeWarningInfobarDelegate::InfoBarDismissed() { |
97 ManagedModeNavigationObserver* observer = | 103 ManagedModeNavigationObserver* observer = |
98 ManagedModeNavigationObserver::FromWebContents(owner()->GetWebContents()); | 104 ManagedModeNavigationObserver::FromWebContents(owner()->GetWebContents()); |
99 observer->InfobarDismissed(); | 105 observer->WarnInfobarDismissed(); |
106 } | |
107 | |
108 class ManagedModePreviewInfobarDelegate : public ConfirmInfoBarDelegate { | |
109 public: | |
110 explicit ManagedModePreviewInfobarDelegate( | |
111 InfoBarTabHelper* infobar_tab_helper); | |
112 | |
113 private: | |
114 virtual ~ManagedModePreviewInfobarDelegate(); | |
115 | |
116 // ConfirmInfoBarDelegate overrides: | |
117 virtual string16 GetMessageText() const OVERRIDE; | |
118 virtual int GetButtons() const OVERRIDE; | |
119 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; | |
120 virtual bool Accept() OVERRIDE; | |
121 virtual bool Cancel() OVERRIDE; | |
122 | |
123 // InfoBarDelegate override: | |
124 virtual bool ShouldExpire( | |
125 const content::LoadCommittedDetails& details) const OVERRIDE; | |
126 virtual void InfoBarDismissed() OVERRIDE; | |
127 | |
128 DISALLOW_COPY_AND_ASSIGN(ManagedModePreviewInfobarDelegate); | |
129 }; | |
130 | |
131 ManagedModePreviewInfobarDelegate::ManagedModePreviewInfobarDelegate( | |
132 InfoBarTabHelper* infobar_tab_helper) | |
133 : ConfirmInfoBarDelegate(infobar_tab_helper) {} | |
134 | |
135 ManagedModePreviewInfobarDelegate::~ManagedModePreviewInfobarDelegate() {} | |
136 | |
137 string16 ManagedModePreviewInfobarDelegate::GetMessageText() const { | |
138 return l10n_util::GetStringUTF16(IDS_MANAGED_MODE_PREVIEW_MESSAGE); | |
139 } | |
140 | |
141 int ManagedModePreviewInfobarDelegate::GetButtons() const { | |
142 return BUTTON_OK | BUTTON_CANCEL; | |
143 } | |
144 | |
145 string16 ManagedModePreviewInfobarDelegate::GetButtonLabel( | |
146 InfoBarButton button) const { | |
147 return l10n_util::GetStringUTF16( | |
148 (button == BUTTON_OK) ? IDS_MANAGED_MODE_PREVIEW_ACCEPT | |
149 : IDS_MANAGED_MODE_PREVIEW_CANCEL); | |
150 } | |
151 | |
152 bool ManagedModePreviewInfobarDelegate::Accept() { | |
153 ManagedModeNavigationObserver* observer = | |
154 ManagedModeNavigationObserver::FromWebContents( | |
155 owner()->GetWebContents()); | |
156 observer->AddURLList(); | |
157 // Clear the pointer as the infobar was closed. | |
Bernhard Bauer
2012/11/16 15:04:46
The ManagedModePreviewInfobarDelegate doesn't *rea
| |
158 observer->PreviewInfobarDismissed(); | |
159 | |
160 return true; | |
161 } | |
162 | |
163 bool ManagedModePreviewInfobarDelegate::Cancel() { | |
164 // TODO(bauerb): Go back to the last page. | |
165 return false; | |
166 } | |
167 | |
168 bool ManagedModePreviewInfobarDelegate::ShouldExpire( | |
169 const content::LoadCommittedDetails& details) const { | |
170 // ManagedModeNavigationObserver removes us below. | |
171 return false; | |
172 } | |
173 | |
174 void ManagedModePreviewInfobarDelegate::InfoBarDismissed() { | |
175 ManagedModeNavigationObserver* observer = | |
176 ManagedModeNavigationObserver::FromWebContents( | |
177 owner()->GetWebContents()); | |
178 observer->PreviewInfobarDismissed(); | |
100 } | 179 } |
101 | 180 |
102 } // namespace | 181 } // namespace |
103 | 182 |
104 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ManagedModeNavigationObserver) | 183 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ManagedModeNavigationObserver) |
105 | 184 |
106 ManagedModeNavigationObserver::~ManagedModeNavigationObserver() {} | 185 ManagedModeNavigationObserver::~ManagedModeNavigationObserver() {} |
107 | 186 |
108 ManagedModeNavigationObserver::ManagedModeNavigationObserver( | 187 ManagedModeNavigationObserver::ManagedModeNavigationObserver( |
109 content::WebContents* web_contents) | 188 content::WebContents* web_contents) |
110 : WebContentsObserver(web_contents), | 189 : WebContentsObserver(web_contents), |
111 url_filter_(ManagedMode::GetURLFilterForUIThread()), | 190 url_filter_(ManagedMode::GetURLFilterForUIThread()), |
112 infobar_delegate_(NULL) {} | 191 warn_infobar_delegate_(NULL), |
192 preview_infobar_delegate_(NULL), | |
193 after_interstitial_(false) {} | |
113 | 194 |
114 void ManagedModeNavigationObserver::InfobarDismissed() { | 195 void ManagedModeNavigationObserver::WarnInfobarDismissed() { |
115 DCHECK(infobar_delegate_); | 196 DCHECK(warn_infobar_delegate_); |
116 infobar_delegate_ = NULL; | 197 warn_infobar_delegate_ = NULL; |
198 } | |
199 | |
200 void ManagedModeNavigationObserver::PreviewInfobarDismissed() { | |
201 DCHECK(preview_infobar_delegate_); | |
202 preview_infobar_delegate_ = NULL; | |
203 } | |
204 | |
205 void ManagedModeNavigationObserver::AddNavigatedURL(const GURL& url) { | |
206 if (std::find(navigated_urls_.begin(), navigated_urls_.end(), url) != | |
207 navigated_urls_.end()) { | |
208 navigated_urls_.push_back(url); | |
209 } | |
210 } | |
211 | |
212 void ManagedModeNavigationObserver::AddURLList() { | |
213 // Get a copy of the whitelist since it can't be edited in place. | |
214 // |whitelist| is the preference list while AddStringToManualWhitelist adds | |
215 // the navigated urls to the URL filter. | |
Bernhard Bauer
2012/11/16 15:04:46
Why don't we do that stuff with one method? Where
Sergiu
2012/11/26 14:48:08
Moved in ManagedMode.
| |
216 scoped_ptr<base::ListValue> whitelist( | |
217 ManagedMode::GetWhitelist()->DeepCopy()); | |
218 std::string url_to_add; | |
Bernhard Bauer
2012/11/16 15:04:46
Move this down where it's initialized? Also, it's
| |
219 int added_url_count = 0; | |
220 | |
221 for (std::vector<GURL>::const_iterator it = navigated_urls_.begin(); | |
222 it+1 != navigated_urls_.end(); ++it) { | |
223 url_to_add = it->spec(); | |
224 if (!IsInList(ManagedMode::GetWhitelist().get(), url_to_add)) { | |
225 DLOG(ERROR) << "Adding (exact):" << url_to_add; | |
226 ManagedMode::AddStringToManualWhitelist(url_to_add); | |
227 whitelist->Append(Value::CreateStringValue(url_to_add)); | |
228 ++added_url_count; | |
229 } | |
230 } | |
231 | |
232 // If the URL uses https add the protocol as well instead of just the | |
233 // hostname. | |
234 if (navigated_urls_.back().SchemeIs("https")) { | |
235 url_to_add = navigated_urls_.back().GetOrigin().spec(); | |
236 } else { | |
237 url_to_add = navigated_urls_.back().host(); | |
238 } | |
239 | |
240 // Use the local whitelist to see if this last URL is already there. | |
241 if (!IsInList(ManagedMode::GetWhitelist().get(), url_to_add)) { | |
Bernhard Bauer
2012/11/16 15:04:46
Argh! Please don't ever do this. You create a scop
Sergiu
2012/11/26 14:48:08
Got it, refactored and as we discussed a bit, I ch
| |
242 DLOG(ERROR) << "Adding (hostname): " << url_to_add; | |
243 ManagedMode::AddStringToManualWhitelist(url_to_add); | |
244 whitelist->Append(Value::CreateStringValue(url_to_add)); | |
245 ++added_url_count; | |
246 } else { | |
247 // Tell the user that the site was already present in the whitelist. | |
248 InfoBarTabHelper* infobar_tab_helper = | |
249 InfoBarTabHelper::FromWebContents(web_contents()); | |
250 infobar_tab_helper->AddInfoBar(new SimpleAlertInfoBarDelegate( | |
251 infobar_tab_helper, | |
252 NULL, | |
253 l10n_util::GetStringFUTF16(IDS_MANAGED_MODE_ALREADY_ADDED_MESSAGE, | |
254 base::IntToString16(added_url_count)), | |
255 true)); | |
256 } | |
257 | |
258 ManagedMode::SetWhitelist(whitelist.get()); | |
117 } | 259 } |
118 | 260 |
119 void ManagedModeNavigationObserver::NavigateToPendingEntry( | 261 void ManagedModeNavigationObserver::NavigateToPendingEntry( |
120 const GURL& url, | 262 const GURL& url, |
121 content::NavigationController::ReloadType reload_type) { | 263 content::NavigationController::ReloadType reload_type) { |
122 DLOG(ERROR) << "NavigateToPendingEntry: " << url; | 264 DLOG(ERROR) << "NavigateToPendingEntry: " << url; |
265 // This means that a new navigation was instantiated and the data related to | |
Bernhard Bauer
2012/11/16 15:04:46
What do you mean by "a new navigation was instanti
Sergiu
2012/11/26 14:48:08
Gave an example in the .h.
| |
266 // the list of URLs needs to be cleared. | |
267 navigated_urls_.clear(); | |
268 after_interstitial_ = false; | |
123 } | 269 } |
124 | 270 |
125 void ManagedModeNavigationObserver::DidNavigateMainFrame( | 271 void ManagedModeNavigationObserver::DidNavigateMainFrame( |
126 const content::LoadCommittedDetails& details, | 272 const content::LoadCommittedDetails& details, |
127 const content::FrameNavigateParams& params) { | 273 const content::FrameNavigateParams& params) { |
128 DLOG(ERROR) << "DidNavigateMainFrame: " << params.url; | 274 DLOG(ERROR) << "DidNavigateMainFrame: " << params.url; |
275 | |
276 ManagedModeURLFilter::FilteringBehavior behavior = | |
277 url_filter_->GetFilteringBehaviorForURL(params.url); | |
278 | |
279 if (behavior != ManagedModeURLFilter::ALLOW) | |
280 AddNavigatedURL(params.url); | |
281 | |
282 if (behavior == ManagedModeURLFilter::ALLOW && after_interstitial_) { | |
283 // The initial page that triggered the interstitial was blocked but the | |
284 // final page is already in the whitelist so add the series of URLs | |
285 // which lead to the final page to the whitelist as well. | |
286 AddURLList(); | |
287 } | |
129 } | 288 } |
130 | 289 |
131 void ManagedModeNavigationObserver::DidStartProvisionalLoadForFrame( | 290 void ManagedModeNavigationObserver::DidStartProvisionalLoadForFrame( |
132 int64 frame_id, | 291 int64 frame_id, |
133 int64 parent_frame_id, | 292 int64 parent_frame_id, |
134 bool is_main_frame, | 293 bool is_main_frame, |
135 const GURL& url, | 294 const GURL& url, |
136 bool is_error_page, | 295 bool is_error_page, |
137 content::RenderViewHost* render_view_host) { | 296 content::RenderViewHost* render_view_host) { |
138 if (!is_main_frame) | 297 if (!is_main_frame) |
139 return; | 298 return; |
140 | 299 |
141 DLOG(ERROR) << "DidStartProvisionalLoadForFrame: " << url; | 300 DLOG(ERROR) << "DidStartProvisionalLoadForFrame: " << url; |
142 } | 301 } |
143 | 302 |
144 void ManagedModeNavigationObserver::ProvisionalChangeToMainFrameUrl( | 303 void ManagedModeNavigationObserver::ProvisionalChangeToMainFrameUrl( |
145 const GURL& url, | 304 const GURL& url, |
146 const GURL& opener_url, | 305 const GURL& opener_url, |
147 content::RenderViewHost* render_view_host) { | 306 content::RenderViewHost* render_view_host) { |
148 DLOG(ERROR) << "ProvisionalChangeToMainFrameUrl: " << url; | 307 DLOG(ERROR) << "ProvisionalChangeToMainFrameUrl: " << url; |
308 // Mark the fact that an interstitial will be triggered here if the URL | |
309 // must be blocked. | |
Bernhard Bauer
2012/11/16 15:04:46
I'm not sure if I understand the interplay between
Sergiu
2012/11/26 14:48:08
Rephrased here and provided an example in the .h
| |
310 ManagedModeURLFilter::FilteringBehavior behavior = | |
311 url_filter_->GetFilteringBehaviorForURL(url); | |
312 if (behavior == ManagedModeURLFilter::BLOCK) | |
313 after_interstitial_ = true; | |
314 if (behavior != ManagedModeURLFilter::ALLOW) | |
315 AddNavigatedURL(url); | |
149 } | 316 } |
150 | 317 |
151 void ManagedModeNavigationObserver::DidCommitProvisionalLoadForFrame( | 318 void ManagedModeNavigationObserver::DidCommitProvisionalLoadForFrame( |
152 int64 frame_id, | 319 int64 frame_id, |
153 bool is_main_frame, | 320 bool is_main_frame, |
154 const GURL& url, | 321 const GURL& url, |
155 content::PageTransition transition_type, | 322 content::PageTransition transition_type, |
156 content::RenderViewHost* render_view_host) { | 323 content::RenderViewHost* render_view_host) { |
157 if (!is_main_frame) | 324 if (!is_main_frame) |
158 return; | 325 return; |
159 | 326 |
160 DLOG(ERROR) << "DidCommitProvisionalLoadForFrame: " << url; | 327 DLOG(ERROR) << "DidCommitProvisionalLoadForFrame: " << url; |
161 | |
162 ManagedModeURLFilter::FilteringBehavior behavior = | 328 ManagedModeURLFilter::FilteringBehavior behavior = |
163 url_filter_->GetFilteringBehaviorForURL(url); | 329 url_filter_->GetFilteringBehaviorForURL(url); |
330 | |
331 DLOG(ERROR) << "Current behavior: " << behavior; | |
164 if (behavior == ManagedModeURLFilter::WARN) { | 332 if (behavior == ManagedModeURLFilter::WARN) { |
165 if (!infobar_delegate_) { | 333 if (!warn_infobar_delegate_) { |
166 InfoBarTabHelper* infobar_tab_helper = | 334 InfoBarTabHelper* infobar_tab_helper = |
167 InfoBarTabHelper::FromWebContents(web_contents()); | 335 InfoBarTabHelper::FromWebContents(web_contents()); |
168 infobar_delegate_ = | 336 warn_infobar_delegate_ = |
169 new ManagedModeWarningInfobarDelegate(infobar_tab_helper); | 337 new ManagedModeWarningInfobarDelegate(infobar_tab_helper); |
170 infobar_tab_helper->AddInfoBar(infobar_delegate_); | 338 infobar_tab_helper->AddInfoBar(warn_infobar_delegate_); |
171 } | 339 } |
172 } else { | 340 } else { |
173 if (infobar_delegate_) { | 341 if (warn_infobar_delegate_) { |
174 InfoBarTabHelper* infobar_tab_helper = | 342 InfoBarTabHelper* infobar_tab_helper = |
175 InfoBarTabHelper::FromWebContents(web_contents()); | 343 InfoBarTabHelper::FromWebContents(web_contents()); |
176 infobar_tab_helper->RemoveInfoBar(infobar_delegate_); | 344 infobar_tab_helper->RemoveInfoBar(warn_infobar_delegate_); |
177 infobar_delegate_= NULL; | 345 warn_infobar_delegate_= NULL; |
178 } | 346 } |
179 } | 347 } |
180 | 348 |
181 // if (behavior == ManagedModeURLFilter::BLOCK) | 349 if (behavior == ManagedModeURLFilter::BLOCK) { |
182 // new ManagedModeInterstitial(web_contents(), url); | 350 if (!preview_infobar_delegate_) { |
351 InfoBarTabHelper* infobar_tab_helper = | |
352 InfoBarTabHelper::FromWebContents(web_contents()); | |
353 preview_infobar_delegate_ = | |
354 new ManagedModePreviewInfobarDelegate(infobar_tab_helper); | |
355 infobar_tab_helper->AddInfoBar(preview_infobar_delegate_); | |
356 } | |
357 } else { | |
358 if (preview_infobar_delegate_) { | |
359 InfoBarTabHelper* infobar_tab_helper = | |
360 InfoBarTabHelper::FromWebContents(web_contents()); | |
361 infobar_tab_helper->RemoveInfoBar(preview_infobar_delegate_); | |
362 preview_infobar_delegate_= NULL; | |
363 } | |
364 } | |
183 } | 365 } |
OLD | NEW |