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

Side by Side Diff: chrome/browser/supervised_user/supervised_user_interstitial.cc

Issue 597573002: Add a SupervisedUserServiceObserver. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix use-after-destroy of webcontents Created 6 years, 2 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
OLDNEW
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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 interstitial_page_(NULL), 114 interstitial_page_(NULL),
115 url_(url), 115 url_(url),
116 callback_(callback) {} 116 callback_(callback) {}
117 117
118 SupervisedUserInterstitial::~SupervisedUserInterstitial() {} 118 SupervisedUserInterstitial::~SupervisedUserInterstitial() {
119 DCHECK(!web_contents_);
120 }
119 121
120 bool SupervisedUserInterstitial::Init() { 122 bool SupervisedUserInterstitial::Init() {
121 if (ShouldProceed()) { 123 if (ShouldProceed()) {
122 // It can happen that the site was only allowed very recently and the URL 124 // 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 125 // filter on the IO thread had not been updated yet. Proceed with the
124 // request without showing the interstitial. 126 // request without showing the interstitial.
125 DispatchContinueRequest(true); 127 DispatchContinueRequest(true);
126 return false; 128 return false;
127 } 129 }
128 130
(...skipping 16 matching lines...) Expand all
145 details.type = content::NAVIGATION_TYPE_NEW_PAGE; 147 details.type = content::NAVIGATION_TYPE_NEW_PAGE;
146 for (int i = service->infobar_count() - 1; i >= 0; --i) { 148 for (int i = service->infobar_count() - 1; i >= 0; --i) {
147 infobars::InfoBar* infobar = service->infobar_at(i); 149 infobars::InfoBar* infobar = service->infobar_at(i);
148 if (infobar->delegate()->ShouldExpire( 150 if (infobar->delegate()->ShouldExpire(
149 InfoBarService::NavigationDetailsFromLoadCommittedDetails( 151 InfoBarService::NavigationDetailsFromLoadCommittedDetails(
150 details))) 152 details)))
151 service->RemoveInfoBar(infobar); 153 service->RemoveInfoBar(infobar);
152 } 154 }
153 } 155 }
154 156
155 // TODO(bauerb): Extract an observer callback on SupervisedUserService for
156 // this.
157 Profile* profile = 157 Profile* profile =
158 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); 158 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
159 PrefService* prefs = profile->GetPrefs(); 159 SupervisedUserService* supervised_user_service =
160 pref_change_registrar_.Init(prefs); 160 SupervisedUserServiceFactory::GetForProfile(profile);
161 pref_change_registrar_.Add( 161 supervised_user_service->AddObserver(this);
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 162
174 interstitial_page_ = 163 interstitial_page_ =
175 content::InterstitialPage::Create(web_contents_, true, url_, this); 164 content::InterstitialPage::Create(web_contents_, true, url_, this);
176 interstitial_page_->Show(); 165 interstitial_page_->Show();
177 166
178 return true; 167 return true;
179 } 168 }
180 169
181 std::string SupervisedUserInterstitial::GetHTMLContents() { 170 std::string SupervisedUserInterstitial::GetHTMLContents() {
182 base::DictionaryValue strings; 171 base::DictionaryValue strings;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 // CHECK instead of DCHECK as defense in depth in case we'd accidentally 269 // CHECK instead of DCHECK as defense in depth in case we'd accidentally
281 // proceed on a blocked page. 270 // proceed on a blocked page.
282 CHECK(ShouldProceed()); 271 CHECK(ShouldProceed());
283 DispatchContinueRequest(true); 272 DispatchContinueRequest(true);
284 } 273 }
285 274
286 void SupervisedUserInterstitial::OnDontProceed() { 275 void SupervisedUserInterstitial::OnDontProceed() {
287 DispatchContinueRequest(false); 276 DispatchContinueRequest(false);
288 } 277 }
289 278
279 void SupervisedUserInterstitial::OnURLFilterChanged() {
280 if (web_contents_ && ShouldProceed())
Bernhard Bauer 2014/09/24 11:51:59 Why do we need the |web_contents_| check now? Just
Marc Treib 2014/09/24 12:02:27 Because ShouldProceed uses the WebContents to get
281 interstitial_page_->Proceed();
282 }
283
290 bool SupervisedUserInterstitial::ShouldProceed() { 284 bool SupervisedUserInterstitial::ShouldProceed() {
285 DCHECK(web_contents_);
291 Profile* profile = 286 Profile* profile =
292 Profile::FromBrowserContext(web_contents_->GetBrowserContext()); 287 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
293 SupervisedUserService* supervised_user_service = 288 SupervisedUserService* supervised_user_service =
294 SupervisedUserServiceFactory::GetForProfile(profile); 289 SupervisedUserServiceFactory::GetForProfile(profile);
295 SupervisedUserURLFilter* url_filter = 290 SupervisedUserURLFilter* url_filter =
296 supervised_user_service->GetURLFilterForUIThread(); 291 supervised_user_service->GetURLFilterForUIThread();
297 return url_filter->GetFilteringBehaviorForURL(url_) != 292 return url_filter->GetFilteringBehaviorForURL(url_) !=
298 SupervisedUserURLFilter::BLOCK; 293 SupervisedUserURLFilter::BLOCK;
299 } 294 }
300 295
301 void SupervisedUserInterstitial::OnFilteringPrefsChanged() {
302 if (ShouldProceed())
303 interstitial_page_->Proceed();
304 }
305
306 void SupervisedUserInterstitial::DispatchContinueRequest( 296 void SupervisedUserInterstitial::DispatchContinueRequest(
307 bool continue_request) { 297 bool continue_request) {
298 DCHECK(web_contents_);
299 Profile* profile =
300 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
301 SupervisedUserService* supervised_user_service =
302 SupervisedUserServiceFactory::GetForProfile(profile);
303 supervised_user_service->RemoveObserver(this);
304
308 BrowserThread::PostTask( 305 BrowserThread::PostTask(
309 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); 306 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request));
307
308 // After this, the WebContents may be destroyed. Make sure we don't try to use
309 // it again.
310 web_contents_ = NULL;
Bernhard Bauer 2014/09/24 11:51:59 Is it guaranteed that this will be called, even if
Marc Treib 2014/09/24 12:02:27 Yes, InterstitialPageImpl calls OnDontProceed when
Bernhard Bauer 2014/09/24 12:58:43 Hm, accessing |web_contents_| above already worrie
Marc Treib 2014/09/24 13:25:25 That's a good point.
310 } 311 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698