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

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 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_observer.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;
39 using content::WebContentsObserver;
34 40
35 namespace { 41 namespace {
36 42
37 static const int kAvatarSize1x = 45; 43 static const int kAvatarSize1x = 45;
38 static const int kAvatarSize2x = 90; 44 static const int kAvatarSize2x = 90;
39 45
40 std::string BuildAvatarImageUrl(const std::string& url, int size) { 46 std::string BuildAvatarImageUrl(const std::string& url, int size) {
41 std::string result = url; 47 std::string result = url;
42 size_t slash = result.rfind('/'); 48 size_t slash = result.rfind('/');
43 if (slash != std::string::npos) 49 if (slash != std::string::npos)
44 result.insert(slash, "/s" + base::IntToString(size)); 50 result.insert(slash, "/s" + base::IntToString(size));
45 return result; 51 return result;
46 } 52 }
47 53
48 } // namespace 54 class TabCloser : WebContentsObserver {
55 public:
56 static void CloseTab(WebContents* web_contents) {
57 // TabCloser will delete itself.
58 new TabCloser(web_contents);
59 }
60
61 private:
62 explicit TabCloser(WebContents* web_contents)
63 : WebContentsObserver(web_contents), weak_ptr_factory_(this) {
64 BrowserThread::PostTask(
65 BrowserThread::UI,
66 FROM_HERE,
67 base::Bind(&TabCloser::CloseTabImpl, weak_ptr_factory_.GetWeakPtr()));
68 }
69 virtual ~TabCloser() {}
70
71 virtual void WebContentsDestroyed() OVERRIDE {
72 delete this;
Bernhard Bauer 2014/09/08 16:29:46 If you want to tie the lifetime of this object to
Marc Treib 2014/09/09 09:17:09 That does seem less cumbersome, thanks! Looking...
Marc Treib 2014/09/09 09:30:47 Hm, on closer inspection: Only a single instance o
Bernhard Bauer 2014/09/09 09:41:55 Um... if we were to attach two instances of this o
Marc Treib 2014/09/09 09:44:05 No, the second one would get WebContentsDestroyed
Bernhard Bauer 2014/09/09 10:50:27 That does not seem less fragile than the WebConten
Marc Treib 2014/09/09 11:14:37 You're right, silently ignoring a second request t
73 }
74
75 void CloseTabImpl() {
76 WebContents* contents = web_contents();
77 DCHECK(contents);
78 Browser* browser = chrome::FindBrowserWithWebContents(contents);
79 DCHECK(browser);
80 TabStripModel* tab_strip = browser->tab_strip_model();
81 // Don't close the last tab in the window.
82 if (tab_strip->count() > 1) {
83 int index = tab_strip->GetIndexOfWebContents(contents);
84 DCHECK_NE(index, TabStripModel::kNoTab);
Bernhard Bauer 2014/09/08 16:29:46 Switch arguments please.
Marc Treib 2014/09/09 09:17:09 Done.
85 tab_strip->CloseWebContentsAt(
86 index, TabStripModel::CLOSE_CREATE_HISTORICAL_TAB);
87 // This causes WebContentsDestroyed to be called, which will delete us.
88 } else {
89 delete this;
90 }
91 }
92
93 base::WeakPtrFactory<TabCloser> weak_ptr_factory_;
94 };
95
96 } // namespace
49 97
50 // static 98 // static
51 void SupervisedUserInterstitial::Show( 99 void SupervisedUserInterstitial::Show(
52 content::WebContents* web_contents, 100 WebContents* web_contents,
53 const GURL& url, 101 const GURL& url,
54 const base::Callback<void(bool)>& callback) { 102 const base::Callback<void(bool)>& callback) {
55 SupervisedUserInterstitial* interstitial = 103 SupervisedUserInterstitial* interstitial =
56 new SupervisedUserInterstitial(web_contents, url, callback); 104 new SupervisedUserInterstitial(web_contents, url, callback);
57 105
58 // If Init() does not complete fully, immediately delete the interstitial. 106 // If Init() does not complete fully, immediately delete the interstitial.
59 if (!interstitial->Init()) 107 if (!interstitial->Init())
60 delete interstitial; 108 delete interstitial;
61 // Otherwise |interstitial_page_| is responsible for deleting it. 109 // Otherwise |interstitial_page_| is responsible for deleting it.
62 } 110 }
63 111
64 SupervisedUserInterstitial::SupervisedUserInterstitial( 112 SupervisedUserInterstitial::SupervisedUserInterstitial(
65 content::WebContents* web_contents, 113 WebContents* web_contents,
66 const GURL& url, 114 const GURL& url,
67 const base::Callback<void(bool)>& callback) 115 const base::Callback<void(bool)>& callback)
68 : web_contents_(web_contents), 116 : web_contents_(web_contents),
69 interstitial_page_(NULL), 117 interstitial_page_(NULL),
70 url_(url), 118 url_(url),
71 callback_(callback) {} 119 callback_(callback) {}
72 120
73 SupervisedUserInterstitial::~SupervisedUserInterstitial() {} 121 SupervisedUserInterstitial::~SupervisedUserInterstitial() {}
74 122
75 bool SupervisedUserInterstitial::Init() { 123 bool SupervisedUserInterstitial::Init() {
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 void SupervisedUserInterstitial::OnFilteringPrefsChanged() { 298 void SupervisedUserInterstitial::OnFilteringPrefsChanged() {
251 if (ShouldProceed()) 299 if (ShouldProceed())
252 interstitial_page_->Proceed(); 300 interstitial_page_->Proceed();
253 } 301 }
254 302
255 void SupervisedUserInterstitial::DispatchContinueRequest( 303 void SupervisedUserInterstitial::DispatchContinueRequest(
256 bool continue_request) { 304 bool continue_request) {
257 // If there is no history entry to go back to, close the tab instead. 305 // If there is no history entry to go back to, close the tab instead.
258 int nav_entry_count = web_contents_->GetController().GetEntryCount(); 306 int nav_entry_count = web_contents_->GetController().GetEntryCount();
259 if (!continue_request && nav_entry_count == 0) 307 if (!continue_request && nav_entry_count == 0)
260 web_contents_->Close(); 308 TabCloser::CloseTab(web_contents_);
Bernhard Bauer 2014/09/08 16:29:46 This is a bit weird. The WebContents might get clo
Marc Treib 2014/09/09 09:17:09 "we" = this interstitial? Then I don't see why we
261 309
262 BrowserThread::PostTask( 310 BrowserThread::PostTask(
263 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); 311 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request));
264 } 312 }
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