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

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

Issue 550973002: Supervised user interstitial: Don't destroy WebContents that might still be in use (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix Android build Created 6 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
« no previous file with comments | « chrome/browser/supervised_user/supervised_user_browsertest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/metrics/histogram.h" 8 #include "base/metrics/histogram.h"
8 #include "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h" 12 #include "base/values.h"
12 #include "chrome/browser/infobars/infobar_service.h" 13 #include "chrome/browser/infobars/infobar_service.h"
13 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/supervised_user/supervised_user_service.h" 15 #include "chrome/browser/supervised_user/supervised_user_service.h"
15 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" 16 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
16 #include "chrome/common/pref_names.h" 17 #include "chrome/common/pref_names.h"
17 #include "chrome/grit/generated_resources.h" 18 #include "chrome/grit/generated_resources.h"
18 #include "components/infobars/core/infobar.h" 19 #include "components/infobars/core/infobar.h"
19 #include "components/infobars/core/infobar_delegate.h" 20 #include "components/infobars/core/infobar_delegate.h"
20 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/interstitial_page.h" 22 #include "content/public/browser/interstitial_page.h"
22 #include "content/public/browser/navigation_controller.h" 23 #include "content/public/browser/navigation_controller.h"
23 #include "content/public/browser/navigation_details.h" 24 #include "content/public/browser/navigation_details.h"
24 #include "content/public/browser/navigation_entry.h" 25 #include "content/public/browser/navigation_entry.h"
25 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
27 #include "content/public/browser/web_contents_user_data.h"
26 #include "content/public/browser/web_ui.h" 28 #include "content/public/browser/web_ui.h"
27 #include "grit/browser_resources.h" 29 #include "grit/browser_resources.h"
28 #include "ui/base/l10n/l10n_util.h" 30 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/base/resource/resource_bundle.h" 31 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/base/webui/jstemplate_builder.h" 32 #include "ui/base/webui/jstemplate_builder.h"
31 #include "ui/base/webui/web_ui_util.h" 33 #include "ui/base/webui/web_ui_util.h"
32 34
35 #if !defined(OS_ANDROID)
36 #include "chrome/browser/ui/browser_finder.h"
37 #include "chrome/browser/ui/tabs/tab_strip_model.h"
38 #endif
39
33 using content::BrowserThread; 40 using content::BrowserThread;
41 using content::WebContents;
34 42
35 namespace { 43 namespace {
36 44
37 static const int kAvatarSize1x = 45; 45 static const int kAvatarSize1x = 45;
38 static const int kAvatarSize2x = 90; 46 static const int kAvatarSize2x = 90;
39 47
40 std::string BuildAvatarImageUrl(const std::string& url, int size) { 48 std::string BuildAvatarImageUrl(const std::string& url, int size) {
41 std::string result = url; 49 std::string result = url;
42 size_t slash = result.rfind('/'); 50 size_t slash = result.rfind('/');
43 if (slash != std::string::npos) 51 if (slash != std::string::npos)
44 result.insert(slash, "/s" + base::IntToString(size)); 52 result.insert(slash, "/s" + base::IntToString(size));
45 return result; 53 return result;
46 } 54 }
47 55
48 } // namespace 56 class TabCloser : public content::WebContentsUserData<TabCloser> {
57 // To use, call TabCloser::CreateForWebContents.
58 private:
59 friend class content::WebContentsUserData<TabCloser>;
60
61 explicit TabCloser(WebContents* web_contents)
62 : web_contents_(web_contents), weak_ptr_factory_(this) {
63 BrowserThread::PostTask(
64 BrowserThread::UI,
65 FROM_HERE,
66 base::Bind(&TabCloser::CloseTabImpl, weak_ptr_factory_.GetWeakPtr()));
67 }
68 virtual ~TabCloser() {}
69
70 void CloseTabImpl() {
71 #if defined(OS_ANDROID)
72 // On Android, FindBrowserWithWebContents and TabStripModel don't exist.
73 // For now, just close the WebContents directly.
74 web_contents_->Close();
75 #else
76 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
77 DCHECK(browser);
78 TabStripModel* tab_strip = browser->tab_strip_model();
79 if (tab_strip->count() > 1) {
80 int index = tab_strip->GetIndexOfWebContents(web_contents_);
81 DCHECK_NE(TabStripModel::kNoTab, index);
82 tab_strip->CloseWebContentsAt(
Bernhard Bauer 2014/09/09 14:12:20 Do we actually need to do it this way, or could we
Marc Treib 2014/09/09 14:28:10 Turns out that web_contents_->Close() ends up call
Bernhard Bauer 2014/09/09 14:31:51 Yeah, exactly.
83 index, TabStripModel::CLOSE_CREATE_HISTORICAL_TAB);
84 } else {
85 // Don't close the last tab in the window.
86 web_contents_->RemoveUserData(UserDataKey());
87 }
88 #endif
89 }
90
91 WebContents* web_contents_;
92 base::WeakPtrFactory<TabCloser> weak_ptr_factory_;
93 };
94
95 } // namespace
96
97 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabCloser);
49 98
50 // static 99 // static
51 void SupervisedUserInterstitial::Show( 100 void SupervisedUserInterstitial::Show(
52 content::WebContents* web_contents, 101 WebContents* web_contents,
53 const GURL& url, 102 const GURL& url,
54 const base::Callback<void(bool)>& callback) { 103 const base::Callback<void(bool)>& callback) {
55 SupervisedUserInterstitial* interstitial = 104 SupervisedUserInterstitial* interstitial =
56 new SupervisedUserInterstitial(web_contents, url, callback); 105 new SupervisedUserInterstitial(web_contents, url, callback);
57 106
58 // If Init() does not complete fully, immediately delete the interstitial. 107 // If Init() does not complete fully, immediately delete the interstitial.
59 if (!interstitial->Init()) 108 if (!interstitial->Init())
60 delete interstitial; 109 delete interstitial;
61 // Otherwise |interstitial_page_| is responsible for deleting it. 110 // Otherwise |interstitial_page_| is responsible for deleting it.
62 } 111 }
63 112
64 SupervisedUserInterstitial::SupervisedUserInterstitial( 113 SupervisedUserInterstitial::SupervisedUserInterstitial(
65 content::WebContents* web_contents, 114 WebContents* web_contents,
66 const GURL& url, 115 const GURL& url,
67 const base::Callback<void(bool)>& callback) 116 const base::Callback<void(bool)>& callback)
68 : web_contents_(web_contents), 117 : web_contents_(web_contents),
69 interstitial_page_(NULL), 118 interstitial_page_(NULL),
70 url_(url), 119 url_(url),
71 callback_(callback) {} 120 callback_(callback) {}
72 121
73 SupervisedUserInterstitial::~SupervisedUserInterstitial() {} 122 SupervisedUserInterstitial::~SupervisedUserInterstitial() {}
74 123
75 bool SupervisedUserInterstitial::Init() { 124 bool SupervisedUserInterstitial::Init() {
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 BACK, 245 BACK,
197 NTP, 246 NTP,
198 ACCESS_REQUEST, 247 ACCESS_REQUEST,
199 HISTOGRAM_BOUNDING_VALUE 248 HISTOGRAM_BOUNDING_VALUE
200 }; 249 };
201 250
202 if (command == "\"back\"") { 251 if (command == "\"back\"") {
203 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", 252 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
204 BACK, 253 BACK,
205 HISTOGRAM_BOUNDING_VALUE); 254 HISTOGRAM_BOUNDING_VALUE);
255
256 // Close the tab if there is no history entry to go back to.
257 DCHECK(web_contents_->GetController().GetTransientEntry());
258 if (web_contents_->GetController().GetEntryCount() == 1)
259 TabCloser::CreateForWebContents(web_contents_);
260
206 interstitial_page_->DontProceed(); 261 interstitial_page_->DontProceed();
207 return; 262 return;
208 } 263 }
209 264
210 if (command == "\"request\"") { 265 if (command == "\"request\"") {
211 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", 266 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
212 ACCESS_REQUEST, 267 ACCESS_REQUEST,
213 HISTOGRAM_BOUNDING_VALUE); 268 HISTOGRAM_BOUNDING_VALUE);
214 269
215 Profile* profile = 270 Profile* profile =
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 SupervisedUserURLFilter::BLOCK; 302 SupervisedUserURLFilter::BLOCK;
248 } 303 }
249 304
250 void SupervisedUserInterstitial::OnFilteringPrefsChanged() { 305 void SupervisedUserInterstitial::OnFilteringPrefsChanged() {
251 if (ShouldProceed()) 306 if (ShouldProceed())
252 interstitial_page_->Proceed(); 307 interstitial_page_->Proceed();
253 } 308 }
254 309
255 void SupervisedUserInterstitial::DispatchContinueRequest( 310 void SupervisedUserInterstitial::DispatchContinueRequest(
256 bool continue_request) { 311 bool continue_request) {
257 // If there is no history entry to go back to, close the tab instead.
258 int nav_entry_count = web_contents_->GetController().GetEntryCount();
259 if (!continue_request && nav_entry_count == 0)
260 web_contents_->Close();
261
262 BrowserThread::PostTask( 312 BrowserThread::PostTask(
263 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); 313 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request));
264 } 314 }
OLDNEW
« no previous file with comments | « chrome/browser/supervised_user/supervised_user_browsertest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698