Chromium Code Reviews| 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..58ced7cda067b89e73737930e14bccdd3f1f7595 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_user_data.h" |
| #include "content/public/browser/web_ui.h" |
| #include "grit/browser_resources.h" |
| #include "ui/base/l10n/l10n_util.h" |
| @@ -31,6 +35,7 @@ |
| #include "ui/base/webui/web_ui_util.h" |
| using content::BrowserThread; |
| +using content::WebContents; |
| namespace { |
| @@ -45,11 +50,49 @@ std::string BuildAvatarImageUrl(const std::string& url, int size) { |
| return result; |
| } |
| -} // namespace |
| +class TabCloser : public content::WebContentsUserData<TabCloser> { |
| + public: |
| + static void CloseTab(WebContents* web_contents) { |
| + CreateForWebContents(web_contents); |
| + } |
| + |
| + private: |
| + friend class content::WebContentsUserData<TabCloser>; |
| + explicit TabCloser(WebContents* web_contents) |
| + : web_contents_(web_contents), weak_ptr_factory_(this) { |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, |
| + FROM_HERE, |
| + base::Bind(&TabCloser::CloseTabImpl, weak_ptr_factory_.GetWeakPtr())); |
| + } |
| + virtual ~TabCloser() {} |
| + |
| + void CloseTabImpl() { |
| + Browser* browser = chrome::FindBrowserWithWebContents(web_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(web_contents_); |
| + DCHECK_NE(TabStripModel::kNoTab, index); |
| + tab_strip->CloseWebContentsAt( |
| + index, TabStripModel::CLOSE_CREATE_HISTORICAL_TAB); |
| + } else { |
| + web_contents_->RemoveUserData(UserDataKey()); |
| + } |
| + } |
| + |
| + WebContents* web_contents_; |
| + base::WeakPtrFactory<TabCloser> weak_ptr_factory_; |
| +}; |
| + |
| +} // namespace |
| + |
| +DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabCloser); |
| // static |
| void SupervisedUserInterstitial::Show( |
| - content::WebContents* web_contents, |
| + WebContents* web_contents, |
| const GURL& url, |
| const base::Callback<void(bool)>& callback) { |
| SupervisedUserInterstitial* interstitial = |
| @@ -62,13 +105,14 @@ 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), |
| interstitial_page_(NULL), |
| url_(url), |
| - callback_(callback) {} |
| + callback_(callback), |
| + back_button_pressed_(false) {} |
| SupervisedUserInterstitial::~SupervisedUserInterstitial() {} |
| @@ -203,6 +247,7 @@ void SupervisedUserInterstitial::CommandReceived(const std::string& command) { |
| UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
| BACK, |
| HISTOGRAM_BOUNDING_VALUE); |
| + back_button_pressed_ = true; |
| interstitial_page_->DontProceed(); |
| return; |
| } |
| @@ -254,10 +299,10 @@ void SupervisedUserInterstitial::OnFilteringPrefsChanged() { |
| void SupervisedUserInterstitial::DispatchContinueRequest( |
| bool continue_request) { |
| - // If there is no history entry to go back to, close the tab instead. |
| + // On "go back", close the tab if there is no history entry to go back to. |
| int nav_entry_count = web_contents_->GetController().GetEntryCount(); |
| - if (!continue_request && nav_entry_count == 0) |
| - web_contents_->Close(); |
| + if (back_button_pressed_ && !continue_request && nav_entry_count == 0) |
|
Bernhard Bauer
2014/09/09 11:32:31
Just call this directly from CommandReceived(). If
Marc Treib
2014/09/09 11:51:25
Right, that makes more sense now.
Small wrinkle: A
|
| + TabCloser::CloseTab(web_contents_); |
| BrowserThread::PostTask( |
| BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); |