blob: cfa05e28bb4143029e583376656bd3128320dbdc [file] [log] [blame]
[email protected]a09159a2012-11-29 12:51:481// 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]2c145422013-01-24 18:15:408#include "base/metrics/histogram.h"
[email protected]3853a4c2013-02-11 17:15:579#include "base/prefs/pref_service.h"
[email protected]112158af2013-06-07 23:46:1810#include "base/strings/utf_string_conversions.h"
[email protected]59f21562013-05-23 16:19:3011#include "base/values.h"
[email protected]39308cb2013-12-06 03:01:4812#include "chrome/browser/infobars/infobar.h"
[email protected]93c23212013-09-19 14:22:3813#include "chrome/browser/infobars/infobar_delegate.h"
14#include "chrome/browser/infobars/infobar_service.h"
[email protected]ad7a89e2013-05-31 12:03:2415#include "chrome/browser/managed_mode/managed_user_service.h"
16#include "chrome/browser/managed_mode/managed_user_service_factory.h"
[email protected]a09159a2012-11-29 12:51:4817#include "chrome/browser/profiles/profile.h"
[email protected]a09159a2012-11-29 12:51:4818#include "chrome/common/pref_names.h"
[email protected]a09159a2012-11-29 12:51:4819#include "content/public/browser/browser_thread.h"
20#include "content/public/browser/interstitial_page.h"
[email protected]93c23212013-09-19 14:22:3821#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]a09159a2012-11-29 12:51:4824#include "content/public/browser/web_contents.h"
[email protected]59f21562013-05-23 16:19:3025#include "content/public/browser/web_ui.h"
[email protected]a09159a2012-11-29 12:51:4826#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]cd67ed52013-10-15 01:22:1331#include "ui/base/webui/jstemplate_builder.h"
32#include "ui/base/webui/web_ui_util.h"
[email protected]a09159a2012-11-29 12:51:4833
34using content::BrowserThread;
35
[email protected]a09159a2012-11-29 12:51:4836ManagedModeInterstitial::ManagedModeInterstitial(
37 content::WebContents* web_contents,
38 const GURL& url,
39 const base::Callback<void(bool)>& callback)
40 : web_contents_(web_contents),
[email protected]61f5da22013-09-05 15:52:1041 interstitial_page_(NULL),
[email protected]a09159a2012-11-29 12:51:4842 url_(url),
43 callback_(callback) {
[email protected]61f5da22013-09-05 15:52:1044 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]93c23212013-09-19 14:22:3853 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]39308cb2013-12-06 03:01:4871 if (service->infobar_at(i)->delegate()->ShouldExpire(details))
[email protected]93c23212013-09-19 14:22:3872 service->RemoveInfoBar(service->infobar_at(i));
73 }
74 }
75
[email protected]61f5da22013-09-05 15:52:1076 // TODO(bauerb): Extract an observer callback on ManagedUserService for this.
[email protected]a09159a2012-11-29 12:51:4877 Profile* profile =
78 Profile::FromBrowserContext(web_contents->GetBrowserContext());
[email protected]61f5da22013-09-05 15:52:1079 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]a09159a2012-11-29 12:51:4893
[email protected]61f5da22013-09-05 15:52:1094 languages_ = prefs->GetString(prefs::kAcceptLanguages);
[email protected]0369d6ab2013-08-09 01:52:5995 interstitial_page_ =
96 content::InterstitialPage::Create(web_contents, true, url_, this);
[email protected]a09159a2012-11-29 12:51:4897 interstitial_page_->Show();
98}
99
100ManagedModeInterstitial::~ManagedModeInterstitial() {}
101
[email protected]a09159a2012-11-29 12:51:48102std::string ManagedModeInterstitial::GetHTMLContents() {
[email protected]cb1078de2013-12-23 20:04:22103 base::DictionaryValue strings;
[email protected]a09159a2012-11-29 12:51:48104 strings.SetString("blockPageTitle",
105 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE));
[email protected]fae057a2013-06-21 22:46:08106
107 Profile* profile =
108 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
109 ManagedUserService* managed_user_service =
110 ManagedUserServiceFactory::GetForProfile(profile);
[email protected]0369d6ab2013-08-09 01:52:59111
112 bool allow_access_requests = managed_user_service->AccessRequestsEnabled();
113 strings.SetBoolean("allowAccessRequests", allow_access_requests);
114
[email protected]0085863a2013-12-06 21:19:03115 base::string16 custodian =
116 UTF8ToUTF16(managed_user_service->GetCustodianName());
[email protected]fae057a2013-06-21 22:46:08117 strings.SetString(
118 "blockPageMessage",
[email protected]0369d6ab2013-08-09 01:52:59119 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]fae057a2013-06-21 22:46:08124
[email protected]a09159a2012-11-29 12:51:48125 strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON));
126 strings.SetString(
[email protected]59f21562013-05-23 16:19:30127 "requestAccessButton",
128 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON));
129
[email protected]59f21562013-05-23 16:19:30130 strings.SetString(
131 "requestSentMessage",
132 l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE,
[email protected]fae057a2013-06-21 22:46:08133 custodian));
[email protected]59f21562013-05-23 16:19:30134
[email protected]5053d402013-01-23 05:19:26135 webui::SetFontAndTextDirection(&strings);
[email protected]a09159a2012-11-29 12:51:48136
[email protected]0369d6ab2013-08-09 01:52:59137 base::StringPiece html(ResourceBundle::GetSharedInstance().GetRawDataResource(
138 IDR_MANAGED_MODE_BLOCK_INTERSTITIAL_HTML));
[email protected]a09159a2012-11-29 12:51:48139
[email protected]2779e3a2013-01-22 18:40:21140 webui::UseVersion2 version;
141 return webui::GetI18nTemplateHtml(html, &strings);
[email protected]a09159a2012-11-29 12:51:48142}
143
144void ManagedModeInterstitial::CommandReceived(const std::string& command) {
[email protected]2c145422013-01-24 18:15:40145 // For use in histograms.
146 enum Commands {
147 PREVIEW,
148 BACK,
149 NTP,
[email protected]59f21562013-05-23 16:19:30150 ACCESS_REQUEST,
[email protected]2c145422013-01-24 18:15:40151 HISTOGRAM_BOUNDING_VALUE
152 };
153
[email protected]a09159a2012-11-29 12:51:48154 if (command == "\"back\"") {
[email protected]2c145422013-01-24 18:15:40155 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
156 BACK,
157 HISTOGRAM_BOUNDING_VALUE);
[email protected]a09159a2012-11-29 12:51:48158 interstitial_page_->DontProceed();
159 return;
160 }
161
[email protected]59f21562013-05-23 16:19:30162 if (command == "\"request\"") {
[email protected]2c145422013-01-24 18:15:40163 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
[email protected]59f21562013-05-23 16:19:30164 ACCESS_REQUEST,
[email protected]2c145422013-01-24 18:15:40165 HISTOGRAM_BOUNDING_VALUE);
[email protected]ad7a89e2013-05-31 12:03:24166
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]59f21562013-05-23 16:19:30172 DVLOG(1) << "Sent access request for " << url_.spec();
173
[email protected]a09159a2012-11-29 12:51:48174 return;
175 }
176
177 NOTREACHED();
178}
179
[email protected]61f5da22013-09-05 15:52:10180void 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]a09159a2012-11-29 12:51:48186
187void ManagedModeInterstitial::OnDontProceed() {
[email protected]6c7af96d2013-03-28 22:55:14188 DispatchContinueRequest(false);
[email protected]a09159a2012-11-29 12:51:48189}
190
[email protected]61f5da22013-09-05 15:52:10191bool 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
202void ManagedModeInterstitial::OnFilteringPrefsChanged() {
203 if (ShouldProceed())
204 interstitial_page_->Proceed();
205}
206
[email protected]6c7af96d2013-03-28 22:55:14207void ManagedModeInterstitial::DispatchContinueRequest(bool continue_request) {
[email protected]0369d6ab2013-08-09 01:52:59208 BrowserThread::PostTask(
209 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request));
[email protected]6c7af96d2013-03-28 22:55:14210}