Index: chrome/browser/supervised_user/supervised_user_interstitial.cc |
diff --git a/chrome/browser/supervised_user/supervised_user_interstitial.cc b/chrome/browser/supervised_user/supervised_user_interstitial.cc |
index a87ed8cd127aed77d99c486199fbe8bc3f400ed8..73f0e7709e05300ed6c1c7e23d4cd6cd2ab7158f 100644 |
--- a/chrome/browser/supervised_user/supervised_user_interstitial.cc |
+++ b/chrome/browser/supervised_user/supervised_user_interstitial.cc |
@@ -4,6 +4,7 @@ |
#include "chrome/browser/supervised_user/supervised_user_interstitial.h" |
+#include "base/memory/weak_ptr.h" |
#include "base/metrics/histogram.h" |
#include "base/prefs/pref_service.h" |
#include "base/strings/string_number_conversions.h" |
@@ -13,6 +14,8 @@ |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/browser/supervised_user/supervised_user_service.h" |
#include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
+#include "chrome/browser/ui/browser_finder.h" |
+#include "chrome/browser/ui/tabs/tab_strip_model.h" |
#include "chrome/common/pref_names.h" |
#include "chrome/grit/generated_resources.h" |
#include "components/infobars/core/infobar.h" |
@@ -23,6 +26,7 @@ |
#include "content/public/browser/navigation_details.h" |
#include "content/public/browser/navigation_entry.h" |
#include "content/public/browser/web_contents.h" |
+#include "content/public/browser/web_contents_observer.h" |
#include "content/public/browser/web_ui.h" |
#include "grit/browser_resources.h" |
#include "ui/base/l10n/l10n_util.h" |
@@ -31,6 +35,8 @@ |
#include "ui/base/webui/web_ui_util.h" |
using content::BrowserThread; |
+using content::WebContents; |
+using content::WebContentsObserver; |
namespace { |
@@ -45,11 +51,53 @@ std::string BuildAvatarImageUrl(const std::string& url, int size) { |
return result; |
} |
-} // namespace |
+class TabCloser : WebContentsObserver { |
+ public: |
+ static void CloseTab(WebContents* web_contents) { |
+ // TabCloser will delete itself. |
+ new TabCloser(web_contents); |
+ } |
+ |
+ private: |
+ explicit TabCloser(WebContents* web_contents) |
+ : WebContentsObserver(web_contents), weak_ptr_factory_(this) { |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, |
+ FROM_HERE, |
+ base::Bind(&TabCloser::CloseTabImpl, weak_ptr_factory_.GetWeakPtr())); |
+ } |
+ virtual ~TabCloser() {} |
+ |
+ virtual void WebContentsDestroyed() OVERRIDE { |
+ 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
|
+ } |
+ |
+ void CloseTabImpl() { |
+ WebContents* contents = web_contents(); |
+ DCHECK(contents); |
+ Browser* browser = chrome::FindBrowserWithWebContents(contents); |
+ DCHECK(browser); |
+ TabStripModel* tab_strip = browser->tab_strip_model(); |
+ // Don't close the last tab in the window. |
+ if (tab_strip->count() > 1) { |
+ int index = tab_strip->GetIndexOfWebContents(contents); |
+ 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.
|
+ tab_strip->CloseWebContentsAt( |
+ index, TabStripModel::CLOSE_CREATE_HISTORICAL_TAB); |
+ // This causes WebContentsDestroyed to be called, which will delete us. |
+ } else { |
+ delete this; |
+ } |
+ } |
+ |
+ base::WeakPtrFactory<TabCloser> weak_ptr_factory_; |
+}; |
+ |
+} // namespace |
// static |
void SupervisedUserInterstitial::Show( |
- content::WebContents* web_contents, |
+ WebContents* web_contents, |
const GURL& url, |
const base::Callback<void(bool)>& callback) { |
SupervisedUserInterstitial* interstitial = |
@@ -62,7 +110,7 @@ void SupervisedUserInterstitial::Show( |
} |
SupervisedUserInterstitial::SupervisedUserInterstitial( |
- content::WebContents* web_contents, |
+ WebContents* web_contents, |
const GURL& url, |
const base::Callback<void(bool)>& callback) |
: web_contents_(web_contents), |
@@ -257,7 +305,7 @@ void SupervisedUserInterstitial::DispatchContinueRequest( |
// If there is no history entry to go back to, close the tab instead. |
int nav_entry_count = web_contents_->GetController().GetEntryCount(); |
if (!continue_request && nav_entry_count == 0) |
- web_contents_->Close(); |
+ 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
|
BrowserThread::PostTask( |
BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); |