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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/supervised_user/supervised_user_interstitial.h"
6
7 #include "base/i18n/rtl.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "chrome/browser/infobars/infobar_service.h"
13 #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_factory.h"
16 #include "chrome/common/pref_names.h"
17 #include "components/infobars/core/infobar.h"
18 #include "components/infobars/core/infobar_delegate.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/interstitial_page.h"
21 #include "content/public/browser/navigation_controller.h"
22 #include "content/public/browser/navigation_details.h"
23 #include "content/public/browser/navigation_entry.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/browser/web_ui.h"
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"
31 #include "ui/base/webui/jstemplate_builder.h"
32 #include "ui/base/webui/web_ui_util.h"
33
34 using content::BrowserThread;
35
36 // static
Show(content::WebContents * web_contents,const GURL & url,const base::Callback<void (bool)> & callback)37 void SupervisedUserInterstitial::Show(content::WebContents* web_contents,
38 const GURL& url,
39 const base::Callback<void(bool)>& callback) {
40 SupervisedUserInterstitial* interstitial =
41 new SupervisedUserInterstitial(web_contents, url, callback);
42
43 // If Init() does not complete fully, immediately delete the interstitial.
44 if (!interstitial->Init())
45 delete interstitial;
46 // Otherwise |interstitial_page_| is responsible for deleting it.
47 }
48
SupervisedUserInterstitial(content::WebContents * web_contents,const GURL & url,const base::Callback<void (bool)> & callback)49 SupervisedUserInterstitial::SupervisedUserInterstitial(
50 content::WebContents* web_contents,
51 const GURL& url,
52 const base::Callback<void(bool)>& callback)
53 : web_contents_(web_contents),
54 interstitial_page_(NULL),
55 url_(url),
56 callback_(callback) {}
57
~SupervisedUserInterstitial()58 SupervisedUserInterstitial::~SupervisedUserInterstitial() {}
59
Init()60 bool SupervisedUserInterstitial::Init() {
61 if (ShouldProceed()) {
62 // It can happen that the site was only allowed very recently and the URL
63 // filter on the IO thread had not been updated yet. Proceed with the
64 // request without showing the interstitial.
65 DispatchContinueRequest(true);
66 return false;
67 }
68
69 InfoBarService* service = InfoBarService::FromWebContents(web_contents_);
70 if (service) {
71 // Remove all the infobars which are attached to |web_contents_| and for
72 // which ShouldExpire() returns true.
73 content::LoadCommittedDetails details;
74 // |details.is_in_page| is default false, and |details.is_main_frame| is
75 // default true. This results in is_navigation_to_different_page() returning
76 // true.
77 DCHECK(details.is_navigation_to_different_page());
78 const content::NavigationController& controller =
79 web_contents_->GetController();
80 details.entry = controller.GetActiveEntry();
81 if (controller.GetLastCommittedEntry()) {
82 details.previous_entry_index = controller.GetLastCommittedEntryIndex();
83 details.previous_url = controller.GetLastCommittedEntry()->GetURL();
84 }
85 details.type = content::NAVIGATION_TYPE_NEW_PAGE;
86 for (int i = service->infobar_count() - 1; i >= 0; --i) {
87 infobars::InfoBar* infobar = service->infobar_at(i);
88 if (infobar->delegate()->ShouldExpire(
89 InfoBarService::NavigationDetailsFromLoadCommittedDetails(
90 details)))
91 service->RemoveInfoBar(infobar);
92 }
93 }
94
95 // TODO(bauerb): Extract an observer callback on SupervisedUserService for
96 // this.
97 Profile* profile =
98 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
99 PrefService* prefs = profile->GetPrefs();
100 pref_change_registrar_.Init(prefs);
101 pref_change_registrar_.Add(
102 prefs::kDefaultSupervisedUserFilteringBehavior,
103 base::Bind(&SupervisedUserInterstitial::OnFilteringPrefsChanged,
104 base::Unretained(this)));
105 pref_change_registrar_.Add(
106 prefs::kSupervisedUserManualHosts,
107 base::Bind(&SupervisedUserInterstitial::OnFilteringPrefsChanged,
108 base::Unretained(this)));
109 pref_change_registrar_.Add(
110 prefs::kSupervisedUserManualURLs,
111 base::Bind(&SupervisedUserInterstitial::OnFilteringPrefsChanged,
112 base::Unretained(this)));
113
114 languages_ = prefs->GetString(prefs::kAcceptLanguages);
115 interstitial_page_ =
116 content::InterstitialPage::Create(web_contents_, true, url_, this);
117 interstitial_page_->Show();
118
119 return true;
120 }
121
GetHTMLContents()122 std::string SupervisedUserInterstitial::GetHTMLContents() {
123 base::DictionaryValue strings;
124 strings.SetString("blockPageTitle",
125 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE));
126
127 Profile* profile =
128 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
129 SupervisedUserService* supervised_user_service =
130 SupervisedUserServiceFactory::GetForProfile(profile);
131
132 bool allow_access_requests = supervised_user_service->AccessRequestsEnabled();
133 strings.SetBoolean("allowAccessRequests", allow_access_requests);
134
135 base::string16 custodian =
136 base::UTF8ToUTF16(supervised_user_service->GetCustodianName());
137 strings.SetString(
138 "blockPageMessage",
139 allow_access_requests
140 ? l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_MESSAGE,
141 custodian)
142 : l10n_util::GetStringUTF16(
143 IDS_BLOCK_INTERSTITIAL_MESSAGE_ACCESS_REQUESTS_DISABLED));
144
145 strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON));
146 strings.SetString(
147 "requestAccessButton",
148 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON));
149
150 strings.SetString(
151 "requestSentMessage",
152 l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE,
153 custodian));
154
155 webui::SetFontAndTextDirection(&strings);
156
157 base::StringPiece html(ResourceBundle::GetSharedInstance().GetRawDataResource(
158 IDR_MANAGED_MODE_BLOCK_INTERSTITIAL_HTML));
159
160 webui::UseVersion2 version;
161 return webui::GetI18nTemplateHtml(html, &strings);
162 }
163
CommandReceived(const std::string & command)164 void SupervisedUserInterstitial::CommandReceived(const std::string& command) {
165 // For use in histograms.
166 enum Commands {
167 PREVIEW,
168 BACK,
169 NTP,
170 ACCESS_REQUEST,
171 HISTOGRAM_BOUNDING_VALUE
172 };
173
174 if (command == "\"back\"") {
175 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
176 BACK,
177 HISTOGRAM_BOUNDING_VALUE);
178 interstitial_page_->DontProceed();
179 return;
180 }
181
182 if (command == "\"request\"") {
183 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
184 ACCESS_REQUEST,
185 HISTOGRAM_BOUNDING_VALUE);
186
187 Profile* profile =
188 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
189 SupervisedUserService* supervised_user_service =
190 SupervisedUserServiceFactory::GetForProfile(profile);
191 supervised_user_service->AddAccessRequest(url_);
192 DVLOG(1) << "Sent access request for " << url_.spec();
193
194 return;
195 }
196
197 NOTREACHED();
198 }
199
OnProceed()200 void SupervisedUserInterstitial::OnProceed() {
201 // CHECK instead of DCHECK as defense in depth in case we'd accidentally
202 // proceed on a blocked page.
203 CHECK(ShouldProceed());
204 DispatchContinueRequest(true);
205 }
206
OnDontProceed()207 void SupervisedUserInterstitial::OnDontProceed() {
208 DispatchContinueRequest(false);
209 }
210
ShouldProceed()211 bool SupervisedUserInterstitial::ShouldProceed() {
212 Profile* profile =
213 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
214 SupervisedUserService* supervised_user_service =
215 SupervisedUserServiceFactory::GetForProfile(profile);
216 SupervisedUserURLFilter* url_filter =
217 supervised_user_service->GetURLFilterForUIThread();
218 return url_filter->GetFilteringBehaviorForURL(url_) !=
219 SupervisedUserURLFilter::BLOCK;
220 }
221
OnFilteringPrefsChanged()222 void SupervisedUserInterstitial::OnFilteringPrefsChanged() {
223 if (ShouldProceed())
224 interstitial_page_->Proceed();
225 }
226
DispatchContinueRequest(bool continue_request)227 void SupervisedUserInterstitial::DispatchContinueRequest(
228 bool continue_request) {
229 // If there is no history entry to go back to, close the tab instead.
230 int nav_entry_count = web_contents_->GetController().GetEntryCount();
231 if (!continue_request && nav_entry_count == 0)
232 web_contents_->Close();
233
234 BrowserThread::PostTask(
235 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request));
236 }
237