OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/supervised_user/supervised_user_interstitial.h" | 5 #include "chrome/browser/supervised_user/supervised_user_interstitial.h" |
6 | 6 |
7 #include "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 if (!interstitial->Init()) | 104 if (!interstitial->Init()) |
105 delete interstitial; | 105 delete interstitial; |
106 // Otherwise |interstitial_page_| is responsible for deleting it. | 106 // Otherwise |interstitial_page_| is responsible for deleting it. |
107 } | 107 } |
108 | 108 |
109 SupervisedUserInterstitial::SupervisedUserInterstitial( | 109 SupervisedUserInterstitial::SupervisedUserInterstitial( |
110 WebContents* web_contents, | 110 WebContents* web_contents, |
111 const GURL& url, | 111 const GURL& url, |
112 const base::Callback<void(bool)>& callback) | 112 const base::Callback<void(bool)>& callback) |
113 : web_contents_(web_contents), | 113 : web_contents_(web_contents), |
| 114 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())), |
114 interstitial_page_(NULL), | 115 interstitial_page_(NULL), |
115 url_(url), | 116 url_(url), |
116 callback_(callback) {} | 117 callback_(callback) {} |
117 | 118 |
118 SupervisedUserInterstitial::~SupervisedUserInterstitial() {} | 119 SupervisedUserInterstitial::~SupervisedUserInterstitial() { |
| 120 DCHECK(!web_contents_); |
| 121 } |
119 | 122 |
120 bool SupervisedUserInterstitial::Init() { | 123 bool SupervisedUserInterstitial::Init() { |
121 if (ShouldProceed()) { | 124 if (ShouldProceed()) { |
122 // It can happen that the site was only allowed very recently and the URL | 125 // It can happen that the site was only allowed very recently and the URL |
123 // filter on the IO thread had not been updated yet. Proceed with the | 126 // filter on the IO thread had not been updated yet. Proceed with the |
124 // request without showing the interstitial. | 127 // request without showing the interstitial. |
125 DispatchContinueRequest(true); | 128 DispatchContinueRequest(true); |
126 return false; | 129 return false; |
127 } | 130 } |
128 | 131 |
(...skipping 16 matching lines...) Expand all Loading... |
145 details.type = content::NAVIGATION_TYPE_NEW_PAGE; | 148 details.type = content::NAVIGATION_TYPE_NEW_PAGE; |
146 for (int i = service->infobar_count() - 1; i >= 0; --i) { | 149 for (int i = service->infobar_count() - 1; i >= 0; --i) { |
147 infobars::InfoBar* infobar = service->infobar_at(i); | 150 infobars::InfoBar* infobar = service->infobar_at(i); |
148 if (infobar->delegate()->ShouldExpire( | 151 if (infobar->delegate()->ShouldExpire( |
149 InfoBarService::NavigationDetailsFromLoadCommittedDetails( | 152 InfoBarService::NavigationDetailsFromLoadCommittedDetails( |
150 details))) | 153 details))) |
151 service->RemoveInfoBar(infobar); | 154 service->RemoveInfoBar(infobar); |
152 } | 155 } |
153 } | 156 } |
154 | 157 |
155 // TODO(bauerb): Extract an observer callback on SupervisedUserService for | 158 SupervisedUserService* supervised_user_service = |
156 // this. | 159 SupervisedUserServiceFactory::GetForProfile(profile_); |
157 Profile* profile = | 160 supervised_user_service->AddObserver(this); |
158 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | |
159 PrefService* prefs = profile->GetPrefs(); | |
160 pref_change_registrar_.Init(prefs); | |
161 pref_change_registrar_.Add( | |
162 prefs::kDefaultSupervisedUserFilteringBehavior, | |
163 base::Bind(&SupervisedUserInterstitial::OnFilteringPrefsChanged, | |
164 base::Unretained(this))); | |
165 pref_change_registrar_.Add( | |
166 prefs::kSupervisedUserManualHosts, | |
167 base::Bind(&SupervisedUserInterstitial::OnFilteringPrefsChanged, | |
168 base::Unretained(this))); | |
169 pref_change_registrar_.Add( | |
170 prefs::kSupervisedUserManualURLs, | |
171 base::Bind(&SupervisedUserInterstitial::OnFilteringPrefsChanged, | |
172 base::Unretained(this))); | |
173 | 161 |
174 interstitial_page_ = | 162 interstitial_page_ = |
175 content::InterstitialPage::Create(web_contents_, true, url_, this); | 163 content::InterstitialPage::Create(web_contents_, true, url_, this); |
176 interstitial_page_->Show(); | 164 interstitial_page_->Show(); |
177 | 165 |
178 return true; | 166 return true; |
179 } | 167 } |
180 | 168 |
181 std::string SupervisedUserInterstitial::GetHTMLContents() { | 169 std::string SupervisedUserInterstitial::GetHTMLContents() { |
182 base::DictionaryValue strings; | 170 base::DictionaryValue strings; |
183 strings.SetString("blockPageTitle", | 171 strings.SetString("blockPageTitle", |
184 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE)); | 172 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE)); |
185 | 173 |
186 Profile* profile = | |
187 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | |
188 SupervisedUserService* supervised_user_service = | 174 SupervisedUserService* supervised_user_service = |
189 SupervisedUserServiceFactory::GetForProfile(profile); | 175 SupervisedUserServiceFactory::GetForProfile(profile_); |
190 | 176 |
191 bool allow_access_requests = supervised_user_service->AccessRequestsEnabled(); | 177 bool allow_access_requests = supervised_user_service->AccessRequestsEnabled(); |
192 strings.SetBoolean("allowAccessRequests", allow_access_requests); | 178 strings.SetBoolean("allowAccessRequests", allow_access_requests); |
193 | 179 |
194 std::string profile_image_url = profile->GetPrefs()->GetString( | 180 std::string profile_image_url = profile_->GetPrefs()->GetString( |
195 prefs::kSupervisedUserCustodianProfileImageURL); | 181 prefs::kSupervisedUserCustodianProfileImageURL); |
196 strings.SetString("avatarURL1x", BuildAvatarImageUrl(profile_image_url, | 182 strings.SetString("avatarURL1x", BuildAvatarImageUrl(profile_image_url, |
197 kAvatarSize1x)); | 183 kAvatarSize1x)); |
198 strings.SetString("avatarURL2x", BuildAvatarImageUrl(profile_image_url, | 184 strings.SetString("avatarURL2x", BuildAvatarImageUrl(profile_image_url, |
199 kAvatarSize2x)); | 185 kAvatarSize2x)); |
200 | 186 |
201 std::string profile_image_url2 = profile->GetPrefs()->GetString( | 187 std::string profile_image_url2 = profile_->GetPrefs()->GetString( |
202 prefs::kSupervisedUserSecondCustodianProfileImageURL); | 188 prefs::kSupervisedUserSecondCustodianProfileImageURL); |
203 strings.SetString("secondAvatarURL1x", BuildAvatarImageUrl(profile_image_url2, | 189 strings.SetString("secondAvatarURL1x", BuildAvatarImageUrl(profile_image_url2, |
204 kAvatarSize1x)); | 190 kAvatarSize1x)); |
205 strings.SetString("secondAvatarURL2x", BuildAvatarImageUrl(profile_image_url2, | 191 strings.SetString("secondAvatarURL2x", BuildAvatarImageUrl(profile_image_url2, |
206 kAvatarSize2x)); | 192 kAvatarSize2x)); |
207 | 193 |
208 base::string16 custodian = | 194 base::string16 custodian = |
209 base::UTF8ToUTF16(supervised_user_service->GetCustodianName()); | 195 base::UTF8ToUTF16(supervised_user_service->GetCustodianName()); |
210 strings.SetString( | 196 strings.SetString( |
211 "blockPageMessage", | 197 "blockPageMessage", |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 | 242 |
257 interstitial_page_->DontProceed(); | 243 interstitial_page_->DontProceed(); |
258 return; | 244 return; |
259 } | 245 } |
260 | 246 |
261 if (command == "\"request\"") { | 247 if (command == "\"request\"") { |
262 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", | 248 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
263 ACCESS_REQUEST, | 249 ACCESS_REQUEST, |
264 HISTOGRAM_BOUNDING_VALUE); | 250 HISTOGRAM_BOUNDING_VALUE); |
265 | 251 |
266 Profile* profile = | |
267 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | |
268 SupervisedUserService* supervised_user_service = | 252 SupervisedUserService* supervised_user_service = |
269 SupervisedUserServiceFactory::GetForProfile(profile); | 253 SupervisedUserServiceFactory::GetForProfile(profile_); |
270 supervised_user_service->AddAccessRequest(url_); | 254 supervised_user_service->AddAccessRequest(url_); |
271 DVLOG(1) << "Sent access request for " << url_.spec(); | 255 DVLOG(1) << "Sent access request for " << url_.spec(); |
272 | 256 |
273 return; | 257 return; |
274 } | 258 } |
275 | 259 |
276 NOTREACHED(); | 260 NOTREACHED(); |
277 } | 261 } |
278 | 262 |
279 void SupervisedUserInterstitial::OnProceed() { | 263 void SupervisedUserInterstitial::OnProceed() { |
280 // CHECK instead of DCHECK as defense in depth in case we'd accidentally | 264 // CHECK instead of DCHECK as defense in depth in case we'd accidentally |
281 // proceed on a blocked page. | 265 // proceed on a blocked page. |
282 CHECK(ShouldProceed()); | 266 CHECK(ShouldProceed()); |
283 DispatchContinueRequest(true); | 267 DispatchContinueRequest(true); |
284 } | 268 } |
285 | 269 |
286 void SupervisedUserInterstitial::OnDontProceed() { | 270 void SupervisedUserInterstitial::OnDontProceed() { |
287 DispatchContinueRequest(false); | 271 DispatchContinueRequest(false); |
288 } | 272 } |
289 | 273 |
| 274 void SupervisedUserInterstitial::OnURLFilterChanged() { |
| 275 if (ShouldProceed()) |
| 276 interstitial_page_->Proceed(); |
| 277 } |
| 278 |
290 bool SupervisedUserInterstitial::ShouldProceed() { | 279 bool SupervisedUserInterstitial::ShouldProceed() { |
291 Profile* profile = | |
292 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); | |
293 SupervisedUserService* supervised_user_service = | 280 SupervisedUserService* supervised_user_service = |
294 SupervisedUserServiceFactory::GetForProfile(profile); | 281 SupervisedUserServiceFactory::GetForProfile(profile_); |
295 SupervisedUserURLFilter* url_filter = | 282 SupervisedUserURLFilter* url_filter = |
296 supervised_user_service->GetURLFilterForUIThread(); | 283 supervised_user_service->GetURLFilterForUIThread(); |
297 return url_filter->GetFilteringBehaviorForURL(url_) != | 284 return url_filter->GetFilteringBehaviorForURL(url_) != |
298 SupervisedUserURLFilter::BLOCK; | 285 SupervisedUserURLFilter::BLOCK; |
299 } | 286 } |
300 | 287 |
301 void SupervisedUserInterstitial::OnFilteringPrefsChanged() { | |
302 if (ShouldProceed()) | |
303 interstitial_page_->Proceed(); | |
304 } | |
305 | |
306 void SupervisedUserInterstitial::DispatchContinueRequest( | 288 void SupervisedUserInterstitial::DispatchContinueRequest( |
307 bool continue_request) { | 289 bool continue_request) { |
| 290 SupervisedUserService* supervised_user_service = |
| 291 SupervisedUserServiceFactory::GetForProfile(profile_); |
| 292 supervised_user_service->RemoveObserver(this); |
| 293 |
308 BrowserThread::PostTask( | 294 BrowserThread::PostTask( |
309 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); | 295 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); |
| 296 |
| 297 // After this, the WebContents may be destroyed. Make sure we don't try to use |
| 298 // it again. |
| 299 web_contents_ = NULL; |
310 } | 300 } |
OLD | NEW |