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

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: WebContentsUserData 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 | « no previous file | 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"
17 #include "chrome/browser/ui/browser_finder.h"
18 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/common/pref_names.h" 19 #include "chrome/common/pref_names.h"
17 #include "chrome/grit/generated_resources.h" 20 #include "chrome/grit/generated_resources.h"
18 #include "components/infobars/core/infobar.h" 21 #include "components/infobars/core/infobar.h"
19 #include "components/infobars/core/infobar_delegate.h" 22 #include "components/infobars/core/infobar_delegate.h"
20 #include "content/public/browser/browser_thread.h" 23 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/interstitial_page.h" 24 #include "content/public/browser/interstitial_page.h"
22 #include "content/public/browser/navigation_controller.h" 25 #include "content/public/browser/navigation_controller.h"
23 #include "content/public/browser/navigation_details.h" 26 #include "content/public/browser/navigation_details.h"
24 #include "content/public/browser/navigation_entry.h" 27 #include "content/public/browser/navigation_entry.h"
25 #include "content/public/browser/web_contents.h" 28 #include "content/public/browser/web_contents.h"
29 #include "content/public/browser/web_contents_user_data.h"
26 #include "content/public/browser/web_ui.h" 30 #include "content/public/browser/web_ui.h"
27 #include "grit/browser_resources.h" 31 #include "grit/browser_resources.h"
28 #include "ui/base/l10n/l10n_util.h" 32 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/base/resource/resource_bundle.h" 33 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/base/webui/jstemplate_builder.h" 34 #include "ui/base/webui/jstemplate_builder.h"
31 #include "ui/base/webui/web_ui_util.h" 35 #include "ui/base/webui/web_ui_util.h"
32 36
33 using content::BrowserThread; 37 using content::BrowserThread;
38 using content::WebContents;
34 39
35 namespace { 40 namespace {
36 41
37 static const int kAvatarSize1x = 45; 42 static const int kAvatarSize1x = 45;
38 static const int kAvatarSize2x = 90; 43 static const int kAvatarSize2x = 90;
39 44
40 std::string BuildAvatarImageUrl(const std::string& url, int size) { 45 std::string BuildAvatarImageUrl(const std::string& url, int size) {
41 std::string result = url; 46 std::string result = url;
42 size_t slash = result.rfind('/'); 47 size_t slash = result.rfind('/');
43 if (slash != std::string::npos) 48 if (slash != std::string::npos)
44 result.insert(slash, "/s" + base::IntToString(size)); 49 result.insert(slash, "/s" + base::IntToString(size));
45 return result; 50 return result;
46 } 51 }
47 52
48 } // namespace 53 class TabCloser : public content::WebContentsUserData<TabCloser> {
54 public:
55 static void CloseTab(WebContents* web_contents) {
56 CreateForWebContents(web_contents);
57 }
58
59 private:
60 friend class content::WebContentsUserData<TabCloser>;
Bernhard Bauer 2014/09/09 11:26:06 Add a blank line afterwards?
Marc Treib 2014/09/09 11:51:25 Done.
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 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
72 DCHECK(browser);
73 TabStripModel* tab_strip = browser->tab_strip_model();
74 // Don't close the last tab in the window.
Bernhard Bauer 2014/09/09 11:26:06 Nit: Can you move the comment into the else branch
Marc Treib 2014/09/09 11:51:25 Done.
75 if (tab_strip->count() > 1) {
76 int index = tab_strip->GetIndexOfWebContents(web_contents_);
77 DCHECK_NE(TabStripModel::kNoTab, index);
78 tab_strip->CloseWebContentsAt(
79 index, TabStripModel::CLOSE_CREATE_HISTORICAL_TAB);
80 } else {
81 web_contents_->RemoveUserData(UserDataKey());
82 }
83 }
84
85 WebContents* web_contents_;
86 base::WeakPtrFactory<TabCloser> weak_ptr_factory_;
87 };
88
89 } // namespace
90
91 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabCloser);
Bernhard Bauer 2014/09/09 11:26:06 Can we put this into the anonymous namespace?
Marc Treib 2014/09/09 11:51:25 No. In Clang's words: ../../chrome/browser/supervi
49 92
50 // static 93 // static
51 void SupervisedUserInterstitial::Show( 94 void SupervisedUserInterstitial::Show(
52 content::WebContents* web_contents, 95 WebContents* web_contents,
53 const GURL& url, 96 const GURL& url,
54 const base::Callback<void(bool)>& callback) { 97 const base::Callback<void(bool)>& callback) {
55 SupervisedUserInterstitial* interstitial = 98 SupervisedUserInterstitial* interstitial =
56 new SupervisedUserInterstitial(web_contents, url, callback); 99 new SupervisedUserInterstitial(web_contents, url, callback);
57 100
58 // If Init() does not complete fully, immediately delete the interstitial. 101 // If Init() does not complete fully, immediately delete the interstitial.
59 if (!interstitial->Init()) 102 if (!interstitial->Init())
60 delete interstitial; 103 delete interstitial;
61 // Otherwise |interstitial_page_| is responsible for deleting it. 104 // Otherwise |interstitial_page_| is responsible for deleting it.
62 } 105 }
63 106
64 SupervisedUserInterstitial::SupervisedUserInterstitial( 107 SupervisedUserInterstitial::SupervisedUserInterstitial(
65 content::WebContents* web_contents, 108 WebContents* web_contents,
66 const GURL& url, 109 const GURL& url,
67 const base::Callback<void(bool)>& callback) 110 const base::Callback<void(bool)>& callback)
68 : web_contents_(web_contents), 111 : web_contents_(web_contents),
69 interstitial_page_(NULL), 112 interstitial_page_(NULL),
70 url_(url), 113 url_(url),
71 callback_(callback) {} 114 callback_(callback) {}
72 115
73 SupervisedUserInterstitial::~SupervisedUserInterstitial() {} 116 SupervisedUserInterstitial::~SupervisedUserInterstitial() {}
74 117
75 bool SupervisedUserInterstitial::Init() { 118 bool SupervisedUserInterstitial::Init() {
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 void SupervisedUserInterstitial::OnFilteringPrefsChanged() { 293 void SupervisedUserInterstitial::OnFilteringPrefsChanged() {
251 if (ShouldProceed()) 294 if (ShouldProceed())
252 interstitial_page_->Proceed(); 295 interstitial_page_->Proceed();
253 } 296 }
254 297
255 void SupervisedUserInterstitial::DispatchContinueRequest( 298 void SupervisedUserInterstitial::DispatchContinueRequest(
256 bool continue_request) { 299 bool continue_request) {
257 // If there is no history entry to go back to, close the tab instead. 300 // If there is no history entry to go back to, close the tab instead.
258 int nav_entry_count = web_contents_->GetController().GetEntryCount(); 301 int nav_entry_count = web_contents_->GetController().GetEntryCount();
259 if (!continue_request && nav_entry_count == 0) 302 if (!continue_request && nav_entry_count == 0)
260 web_contents_->Close(); 303 TabCloser::CloseTab(web_contents_);
Bernhard Bauer 2014/09/09 11:26:06 Directly call TabCloser::CreateForContents()? Alt
Marc Treib 2014/09/09 11:51:25 As you said, it doesn't really matter if there alr
261 304
262 BrowserThread::PostTask( 305 BrowserThread::PostTask(
263 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); 306 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request));
264 } 307 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698