Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_interstitial.cc
blob8349992a0aebd37f969eb2c81babd8850c2fe6be
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.
5 #include "chrome/browser/supervised_user/supervised_user_interstitial.h"
7 #include "base/metrics/histogram.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_number_conversions.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 "chrome/grit/generated_resources.h"
18 #include "components/infobars/core/infobar.h"
19 #include "components/infobars/core/infobar_delegate.h"
20 #include "content/public/browser/browser_thread.h"
21 #include "content/public/browser/interstitial_page.h"
22 #include "content/public/browser/navigation_controller.h"
23 #include "content/public/browser/navigation_details.h"
24 #include "content/public/browser/navigation_entry.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_ui.h"
27 #include "grit/browser_resources.h"
28 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/base/webui/jstemplate_builder.h"
31 #include "ui/base/webui/web_ui_util.h"
33 using content::BrowserThread;
35 namespace {
37 static const int kAvatarSize1x = 45;
38 static const int kAvatarSize2x = 90;
40 std::string BuildAvatarImageUrl(const std::string& url,
41 const GURL& base_url,
42 int size) {
43 std::string result = url;
44 size_t slash = result.rfind('/');
45 if (slash != std::string::npos)
46 result.insert(slash, "/s" + base::IntToString(size));
47 return base_url.Resolve(result).spec();
50 } // namespace
52 // static
53 void SupervisedUserInterstitial::Show(
54 content::WebContents* web_contents,
55 const GURL& url,
56 const base::Callback<void(bool)>& callback) {
57 SupervisedUserInterstitial* interstitial =
58 new SupervisedUserInterstitial(web_contents, url, callback);
60 // If Init() does not complete fully, immediately delete the interstitial.
61 if (!interstitial->Init())
62 delete interstitial;
63 // Otherwise |interstitial_page_| is responsible for deleting it.
66 SupervisedUserInterstitial::SupervisedUserInterstitial(
67 content::WebContents* web_contents,
68 const GURL& url,
69 const base::Callback<void(bool)>& callback)
70 : web_contents_(web_contents),
71 interstitial_page_(NULL),
72 url_(url),
73 callback_(callback) {}
75 SupervisedUserInterstitial::~SupervisedUserInterstitial() {}
77 bool SupervisedUserInterstitial::Init() {
78 if (ShouldProceed()) {
79 // It can happen that the site was only allowed very recently and the URL
80 // filter on the IO thread had not been updated yet. Proceed with the
81 // request without showing the interstitial.
82 DispatchContinueRequest(true);
83 return false;
86 InfoBarService* service = InfoBarService::FromWebContents(web_contents_);
87 if (service) {
88 // Remove all the infobars which are attached to |web_contents_| and for
89 // which ShouldExpire() returns true.
90 content::LoadCommittedDetails details;
91 // |details.is_in_page| is default false, and |details.is_main_frame| is
92 // default true. This results in is_navigation_to_different_page() returning
93 // true.
94 DCHECK(details.is_navigation_to_different_page());
95 const content::NavigationController& controller =
96 web_contents_->GetController();
97 details.entry = controller.GetActiveEntry();
98 if (controller.GetLastCommittedEntry()) {
99 details.previous_entry_index = controller.GetLastCommittedEntryIndex();
100 details.previous_url = controller.GetLastCommittedEntry()->GetURL();
102 details.type = content::NAVIGATION_TYPE_NEW_PAGE;
103 for (int i = service->infobar_count() - 1; i >= 0; --i) {
104 infobars::InfoBar* infobar = service->infobar_at(i);
105 if (infobar->delegate()->ShouldExpire(
106 InfoBarService::NavigationDetailsFromLoadCommittedDetails(
107 details)))
108 service->RemoveInfoBar(infobar);
112 // TODO(bauerb): Extract an observer callback on SupervisedUserService for
113 // this.
114 Profile* profile =
115 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
116 PrefService* prefs = profile->GetPrefs();
117 pref_change_registrar_.Init(prefs);
118 pref_change_registrar_.Add(
119 prefs::kDefaultSupervisedUserFilteringBehavior,
120 base::Bind(&SupervisedUserInterstitial::OnFilteringPrefsChanged,
121 base::Unretained(this)));
122 pref_change_registrar_.Add(
123 prefs::kSupervisedUserManualHosts,
124 base::Bind(&SupervisedUserInterstitial::OnFilteringPrefsChanged,
125 base::Unretained(this)));
126 pref_change_registrar_.Add(
127 prefs::kSupervisedUserManualURLs,
128 base::Bind(&SupervisedUserInterstitial::OnFilteringPrefsChanged,
129 base::Unretained(this)));
131 interstitial_page_ =
132 content::InterstitialPage::Create(web_contents_, true, url_, this);
133 interstitial_page_->Show();
135 return true;
138 std::string SupervisedUserInterstitial::GetHTMLContents() {
139 base::DictionaryValue strings;
140 strings.SetString("blockPageTitle",
141 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE));
143 Profile* profile =
144 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
145 SupervisedUserService* supervised_user_service =
146 SupervisedUserServiceFactory::GetForProfile(profile);
148 bool allow_access_requests = supervised_user_service->AccessRequestsEnabled();
149 strings.SetBoolean("allowAccessRequests", allow_access_requests);
151 GURL base_url = GURL(profile->GetPrefs()->GetString(
152 prefs::kSupervisedUserCustodianProfileURL));
153 if (!base_url.is_valid())
154 base_url = GURL("https://dummy.url");
155 DCHECK(base_url.is_valid());
156 std::string profile_image_url = profile->GetPrefs()->GetString(
157 prefs::kSupervisedUserCustodianProfileImageURL);
158 strings.SetString("avatarURL1x", BuildAvatarImageUrl(profile_image_url,
159 base_url,
160 kAvatarSize1x));
161 strings.SetString("avatarURL2x", BuildAvatarImageUrl(profile_image_url,
162 base_url,
163 kAvatarSize2x));
165 GURL base_url2 = GURL(profile->GetPrefs()->GetString(
166 prefs::kSupervisedUserSecondCustodianProfileURL));
167 if (!base_url2.is_valid())
168 base_url2 = GURL("https://dummy.url");
169 DCHECK(base_url2.is_valid());
170 std::string profile_image_url2 = profile->GetPrefs()->GetString(
171 prefs::kSupervisedUserSecondCustodianProfileImageURL);
172 strings.SetString("secondAvatarURL1x", BuildAvatarImageUrl(profile_image_url2,
173 base_url2,
174 kAvatarSize1x));
175 strings.SetString("secondAvatarURL2x", BuildAvatarImageUrl(profile_image_url2,
176 base_url2,
177 kAvatarSize2x));
179 base::string16 custodian =
180 base::UTF8ToUTF16(supervised_user_service->GetCustodianName());
181 strings.SetString(
182 "blockPageMessage",
183 allow_access_requests
184 ? l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_MESSAGE,
185 custodian)
186 : l10n_util::GetStringUTF16(
187 IDS_BLOCK_INTERSTITIAL_MESSAGE_ACCESS_REQUESTS_DISABLED));
189 strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON));
190 strings.SetString(
191 "requestAccessButton",
192 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON));
194 strings.SetString(
195 "requestSentMessage",
196 l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE,
197 custodian));
199 webui::SetFontAndTextDirection(&strings);
201 base::StringPiece html(ResourceBundle::GetSharedInstance().GetRawDataResource(
202 IDR_SUPERVISED_USER_BLOCK_INTERSTITIAL_HTML));
204 webui::UseVersion2 version;
205 return webui::GetI18nTemplateHtml(html, &strings);
208 void SupervisedUserInterstitial::CommandReceived(const std::string& command) {
209 // For use in histograms.
210 enum Commands {
211 PREVIEW,
212 BACK,
213 NTP,
214 ACCESS_REQUEST,
215 HISTOGRAM_BOUNDING_VALUE
218 if (command == "\"back\"") {
219 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
220 BACK,
221 HISTOGRAM_BOUNDING_VALUE);
222 interstitial_page_->DontProceed();
223 return;
226 if (command == "\"request\"") {
227 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
228 ACCESS_REQUEST,
229 HISTOGRAM_BOUNDING_VALUE);
231 Profile* profile =
232 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
233 SupervisedUserService* supervised_user_service =
234 SupervisedUserServiceFactory::GetForProfile(profile);
235 supervised_user_service->AddAccessRequest(url_);
236 DVLOG(1) << "Sent access request for " << url_.spec();
238 return;
241 NOTREACHED();
244 void SupervisedUserInterstitial::OnProceed() {
245 // CHECK instead of DCHECK as defense in depth in case we'd accidentally
246 // proceed on a blocked page.
247 CHECK(ShouldProceed());
248 DispatchContinueRequest(true);
251 void SupervisedUserInterstitial::OnDontProceed() {
252 DispatchContinueRequest(false);
255 bool SupervisedUserInterstitial::ShouldProceed() {
256 Profile* profile =
257 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
258 SupervisedUserService* supervised_user_service =
259 SupervisedUserServiceFactory::GetForProfile(profile);
260 SupervisedUserURLFilter* url_filter =
261 supervised_user_service->GetURLFilterForUIThread();
262 return url_filter->GetFilteringBehaviorForURL(url_) !=
263 SupervisedUserURLFilter::BLOCK;
266 void SupervisedUserInterstitial::OnFilteringPrefsChanged() {
267 if (ShouldProceed())
268 interstitial_page_->Proceed();
271 void SupervisedUserInterstitial::DispatchContinueRequest(
272 bool continue_request) {
273 // If there is no history entry to go back to, close the tab instead.
274 int nav_entry_count = web_contents_->GetController().GetEntryCount();
275 if (!continue_request && nav_entry_count == 0)
276 web_contents_->Close();
278 BrowserThread::PostTask(
279 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request));