[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/managed_mode/managed_mode_interstitial.h" |
| 6 | |
| 7 | #include "base/i18n/rtl.h" |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 8 | #include "base/metrics/histogram.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 9 | #include "base/prefs/pref_service.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 11 | #include "base/values.h" |
[email protected] | 39308cb | 2013-12-06 03:01:48 | [diff] [blame] | 12 | #include "chrome/browser/infobars/infobar.h" |
[email protected] | 93c2321 | 2013-09-19 14:22:38 | [diff] [blame] | 13 | #include "chrome/browser/infobars/infobar_delegate.h" |
| 14 | #include "chrome/browser/infobars/infobar_service.h" |
[email protected] | ad7a89e | 2013-05-31 12:03:24 | [diff] [blame] | 15 | #include "chrome/browser/managed_mode/managed_user_service.h" |
| 16 | #include "chrome/browser/managed_mode/managed_user_service_factory.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 17 | #include "chrome/browser/profiles/profile.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 18 | #include "chrome/common/pref_names.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 19 | #include "content/public/browser/browser_thread.h" |
| 20 | #include "content/public/browser/interstitial_page.h" |
[email protected] | 93c2321 | 2013-09-19 14:22:38 | [diff] [blame] | 21 | #include "content/public/browser/navigation_controller.h" |
| 22 | #include "content/public/browser/navigation_details.h" |
| 23 | #include "content/public/browser/navigation_entry.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 24 | #include "content/public/browser/web_contents.h" |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 25 | #include "content/public/browser/web_ui.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 26 | #include "grit/browser_resources.h" |
| 27 | #include "grit/generated_resources.h" |
| 28 | #include "net/base/net_util.h" |
| 29 | #include "ui/base/l10n/l10n_util.h" |
| 30 | #include "ui/base/resource/resource_bundle.h" |
[email protected] | cd67ed5 | 2013-10-15 01:22:13 | [diff] [blame] | 31 | #include "ui/base/webui/jstemplate_builder.h" |
| 32 | #include "ui/base/webui/web_ui_util.h" |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 33 | |
| 34 | using content::BrowserThread; |
| 35 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 36 | ManagedModeInterstitial::ManagedModeInterstitial( |
| 37 | content::WebContents* web_contents, |
| 38 | const GURL& url, |
| 39 | const base::Callback<void(bool)>& callback) |
| 40 | : web_contents_(web_contents), |
[email protected] | 61f5da2 | 2013-09-05 15:52:10 | [diff] [blame] | 41 | interstitial_page_(NULL), |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 42 | url_(url), |
| 43 | callback_(callback) { |
[email protected] | 61f5da2 | 2013-09-05 15:52:10 | [diff] [blame] | 44 | if (ShouldProceed()) { |
| 45 | // It can happen that the site was only allowed very recently and the URL |
| 46 | // filter on the IO thread had not been updated yet. Proceed with the |
| 47 | // request without showing the interstitial. |
| 48 | DispatchContinueRequest(true); |
| 49 | delete this; |
| 50 | return; |
| 51 | } |
| 52 | |
[email protected] | 93c2321 | 2013-09-19 14:22:38 | [diff] [blame] | 53 | InfoBarService* service = InfoBarService::FromWebContents(web_contents); |
| 54 | if (service) { |
| 55 | // Remove all the infobars which are attached to |web_contents| and for |
| 56 | // which ShouldExpire() returns true. |
| 57 | content::LoadCommittedDetails details; |
| 58 | // |details.is_in_page| is default false, and |details.is_main_frame| is |
| 59 | // default true. This results in is_navigation_to_different_page() returning |
| 60 | // true. |
| 61 | DCHECK(details.is_navigation_to_different_page()); |
| 62 | const content::NavigationController& controller = |
| 63 | web_contents->GetController(); |
| 64 | details.entry = controller.GetActiveEntry(); |
| 65 | if (controller.GetLastCommittedEntry()) { |
| 66 | details.previous_entry_index = controller.GetLastCommittedEntryIndex(); |
| 67 | details.previous_url = controller.GetLastCommittedEntry()->GetURL(); |
| 68 | } |
| 69 | details.type = content::NAVIGATION_TYPE_NEW_PAGE; |
| 70 | for (int i = service->infobar_count() - 1; i >= 0; --i) { |
[email protected] | 39308cb | 2013-12-06 03:01:48 | [diff] [blame] | 71 | if (service->infobar_at(i)->delegate()->ShouldExpire(details)) |
[email protected] | 93c2321 | 2013-09-19 14:22:38 | [diff] [blame] | 72 | service->RemoveInfoBar(service->infobar_at(i)); |
| 73 | } |
| 74 | } |
| 75 | |
[email protected] | 61f5da2 | 2013-09-05 15:52:10 | [diff] [blame] | 76 | // TODO(bauerb): Extract an observer callback on ManagedUserService for this. |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 77 | Profile* profile = |
| 78 | Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
[email protected] | 61f5da2 | 2013-09-05 15:52:10 | [diff] [blame] | 79 | PrefService* prefs = profile->GetPrefs(); |
| 80 | pref_change_registrar_.Init(prefs); |
| 81 | pref_change_registrar_.Add( |
| 82 | prefs::kDefaultManagedModeFilteringBehavior, |
| 83 | base::Bind(&ManagedModeInterstitial::OnFilteringPrefsChanged, |
| 84 | base::Unretained(this))); |
| 85 | pref_change_registrar_.Add( |
| 86 | prefs::kManagedModeManualHosts, |
| 87 | base::Bind(&ManagedModeInterstitial::OnFilteringPrefsChanged, |
| 88 | base::Unretained(this))); |
| 89 | pref_change_registrar_.Add( |
| 90 | prefs::kManagedModeManualURLs, |
| 91 | base::Bind(&ManagedModeInterstitial::OnFilteringPrefsChanged, |
| 92 | base::Unretained(this))); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 93 | |
[email protected] | 61f5da2 | 2013-09-05 15:52:10 | [diff] [blame] | 94 | languages_ = prefs->GetString(prefs::kAcceptLanguages); |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame] | 95 | interstitial_page_ = |
| 96 | content::InterstitialPage::Create(web_contents, true, url_, this); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 97 | interstitial_page_->Show(); |
| 98 | } |
| 99 | |
| 100 | ManagedModeInterstitial::~ManagedModeInterstitial() {} |
| 101 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 102 | std::string ManagedModeInterstitial::GetHTMLContents() { |
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame^] | 103 | base::DictionaryValue strings; |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 104 | strings.SetString("blockPageTitle", |
| 105 | l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE)); |
[email protected] | fae057a | 2013-06-21 22:46:08 | [diff] [blame] | 106 | |
| 107 | Profile* profile = |
| 108 | Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 109 | ManagedUserService* managed_user_service = |
| 110 | ManagedUserServiceFactory::GetForProfile(profile); |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame] | 111 | |
| 112 | bool allow_access_requests = managed_user_service->AccessRequestsEnabled(); |
| 113 | strings.SetBoolean("allowAccessRequests", allow_access_requests); |
| 114 | |
[email protected] | 0085863a | 2013-12-06 21:19:03 | [diff] [blame] | 115 | base::string16 custodian = |
| 116 | UTF8ToUTF16(managed_user_service->GetCustodianName()); |
[email protected] | fae057a | 2013-06-21 22:46:08 | [diff] [blame] | 117 | strings.SetString( |
| 118 | "blockPageMessage", |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame] | 119 | allow_access_requests |
| 120 | ? l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_MESSAGE, |
| 121 | custodian) |
| 122 | : l10n_util::GetStringUTF16( |
| 123 | IDS_BLOCK_INTERSTITIAL_MESSAGE_ACCESS_REQUESTS_DISABLED)); |
[email protected] | fae057a | 2013-06-21 22:46:08 | [diff] [blame] | 124 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 125 | strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON)); |
| 126 | strings.SetString( |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 127 | "requestAccessButton", |
| 128 | l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON)); |
| 129 | |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 130 | strings.SetString( |
| 131 | "requestSentMessage", |
| 132 | l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE, |
[email protected] | fae057a | 2013-06-21 22:46:08 | [diff] [blame] | 133 | custodian)); |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 134 | |
[email protected] | 5053d40 | 2013-01-23 05:19:26 | [diff] [blame] | 135 | webui::SetFontAndTextDirection(&strings); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 136 | |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame] | 137 | base::StringPiece html(ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 138 | IDR_MANAGED_MODE_BLOCK_INTERSTITIAL_HTML)); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 139 | |
[email protected] | 2779e3a | 2013-01-22 18:40:21 | [diff] [blame] | 140 | webui::UseVersion2 version; |
| 141 | return webui::GetI18nTemplateHtml(html, &strings); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | void ManagedModeInterstitial::CommandReceived(const std::string& command) { |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 145 | // For use in histograms. |
| 146 | enum Commands { |
| 147 | PREVIEW, |
| 148 | BACK, |
| 149 | NTP, |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 150 | ACCESS_REQUEST, |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 151 | HISTOGRAM_BOUNDING_VALUE |
| 152 | }; |
| 153 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 154 | if (command == "\"back\"") { |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 155 | UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
| 156 | BACK, |
| 157 | HISTOGRAM_BOUNDING_VALUE); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 158 | interstitial_page_->DontProceed(); |
| 159 | return; |
| 160 | } |
| 161 | |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 162 | if (command == "\"request\"") { |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 163 | UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand", |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 164 | ACCESS_REQUEST, |
[email protected] | 2c14542 | 2013-01-24 18:15:40 | [diff] [blame] | 165 | HISTOGRAM_BOUNDING_VALUE); |
[email protected] | ad7a89e | 2013-05-31 12:03:24 | [diff] [blame] | 166 | |
| 167 | Profile* profile = |
| 168 | Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 169 | ManagedUserService* managed_user_service = |
| 170 | ManagedUserServiceFactory::GetForProfile(profile); |
| 171 | managed_user_service->AddAccessRequest(url_); |
[email protected] | 59f2156 | 2013-05-23 16:19:30 | [diff] [blame] | 172 | DVLOG(1) << "Sent access request for " << url_.spec(); |
| 173 | |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 174 | return; |
| 175 | } |
| 176 | |
| 177 | NOTREACHED(); |
| 178 | } |
| 179 | |
[email protected] | 61f5da2 | 2013-09-05 15:52:10 | [diff] [blame] | 180 | void ManagedModeInterstitial::OnProceed() { |
| 181 | // CHECK instead of DCHECK as defense in depth in case we'd accidentally |
| 182 | // proceed on a blocked page. |
| 183 | CHECK(ShouldProceed()); |
| 184 | DispatchContinueRequest(true); |
| 185 | } |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 186 | |
| 187 | void ManagedModeInterstitial::OnDontProceed() { |
[email protected] | 6c7af96d | 2013-03-28 22:55:14 | [diff] [blame] | 188 | DispatchContinueRequest(false); |
[email protected] | a09159a | 2012-11-29 12:51:48 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | 61f5da2 | 2013-09-05 15:52:10 | [diff] [blame] | 191 | bool ManagedModeInterstitial::ShouldProceed() { |
| 192 | Profile* profile = |
| 193 | Profile::FromBrowserContext(web_contents_->GetBrowserContext()); |
| 194 | ManagedUserService* managed_user_service = |
| 195 | ManagedUserServiceFactory::GetForProfile(profile); |
| 196 | ManagedModeURLFilter* url_filter = |
| 197 | managed_user_service->GetURLFilterForUIThread(); |
| 198 | return url_filter->GetFilteringBehaviorForURL(url_) != |
| 199 | ManagedModeURLFilter::BLOCK; |
| 200 | } |
| 201 | |
| 202 | void ManagedModeInterstitial::OnFilteringPrefsChanged() { |
| 203 | if (ShouldProceed()) |
| 204 | interstitial_page_->Proceed(); |
| 205 | } |
| 206 | |
[email protected] | 6c7af96d | 2013-03-28 22:55:14 | [diff] [blame] | 207 | void ManagedModeInterstitial::DispatchContinueRequest(bool continue_request) { |
[email protected] | 0369d6ab | 2013-08-09 01:52:59 | [diff] [blame] | 208 | BrowserThread::PostTask( |
| 209 | BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request)); |
[email protected] | 6c7af96d | 2013-03-28 22:55:14 | [diff] [blame] | 210 | } |