Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / supervised_user / supervised_user_interstitial.cc
blob468820a2ba053052760bf0d6924cd84ef1888562
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/memory/weak_ptr.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/infobars/infobar_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/supervised_user/supervised_user_service.h"
16 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/infobars/core/infobar.h"
20 #include "components/infobars/core/infobar_delegate.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/interstitial_page.h"
23 #include "content/public/browser/navigation_controller.h"
24 #include "content/public/browser/navigation_details.h"
25 #include "content/public/browser/navigation_entry.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/browser/web_contents_user_data.h"
28 #include "content/public/browser/web_ui.h"
29 #include "grit/browser_resources.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/base/webui/jstemplate_builder.h"
33 #include "ui/base/webui/web_ui_util.h"
35 #if !defined(OS_ANDROID)
36 #include "chrome/browser/ui/browser_finder.h"
37 #include "chrome/browser/ui/tabs/tab_strip_model.h"
38 #endif
40 using content::BrowserThread;
41 using content::WebContents;
43 namespace {
45 static const int kAvatarSize1x = 45;
46 static const int kAvatarSize2x = 90;
48 std::string BuildAvatarImageUrl(const std::string& url, int size) {
49 std::string result = url;
50 size_t slash = result.rfind('/');
51 if (slash != std::string::npos)
52 result.insert(slash, "/s" + base::IntToString(size));
53 return result;
56 class TabCloser : public content::WebContentsUserData<TabCloser> {
57 // To use, call TabCloser::CreateForWebContents.
58 private:
59 friend class content::WebContentsUserData<TabCloser>;
61 explicit TabCloser(WebContents* web_contents)
62 : web_contents_(web_contents), weak_ptr_factory_(this) {
63 BrowserThread::PostTask(
64 BrowserThread::UI,
65 FROM_HERE,
66 base::Bind(&TabCloser::CloseTabImpl, weak_ptr_factory_.GetWeakPtr()));
68 virtual ~TabCloser() {}
70 void CloseTabImpl() {
71 // On Android, FindBrowserWithWebContents and TabStripModel don't exist.
72 #if !defined(OS_ANDROID)
73 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
74 DCHECK(browser);
75 TabStripModel* tab_strip = browser->tab_strip_model();
76 DCHECK_NE(TabStripModel::kNoTab,
77 tab_strip->GetIndexOfWebContents(web_contents_));
78 if (tab_strip->count() <= 1) {
79 // Don't close the last tab in the window.
80 web_contents_->RemoveUserData(UserDataKey());
81 return;
83 #endif
84 web_contents_->Close();
87 WebContents* web_contents_;
88 base::WeakPtrFactory<TabCloser> weak_ptr_factory_;
91 } // namespace
93 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabCloser);
95 // static
96 void SupervisedUserInterstitial::Show(
97 WebContents* web_contents,
98 const GURL& url,
99 const base::Callback<void(bool)>& callback) {
100 SupervisedUserInterstitial* interstitial =
101 new SupervisedUserInterstitial(web_contents, url, callback);
103 // If Init() does not complete fully, immediately delete the interstitial.
104 if (!interstitial->Init())
105 delete interstitial;
106 // Otherwise |interstitial_page_| is responsible for deleting it.
109 SupervisedUserInterstitial::SupervisedUserInterstitial(
110 WebContents* web_contents,
111 const GURL& url,
112 const base::Callback<void(bool)>& callback)
113 : web_contents_(web_contents),
114 interstitial_page_(NULL),
115 url_(url),
116 callback_(callback) {}
118 SupervisedUserInterstitial::~SupervisedUserInterstitial() {}
120 bool SupervisedUserInterstitial::Init() {
121 if (ShouldProceed()) {
122 // It can happen that the site was only allowed very recently and the URL
123 // filter on the IO thread had not been updated yet. Proceed with the
124 // request without showing the interstitial.
125 DispatchContinueRequest(true);
126 return false;
129 InfoBarService* service = InfoBarService::FromWebContents(web_contents_);
130 if (service) {
131 // Remove all the infobars which are attached to |web_contents_| and for
132 // which ShouldExpire() returns true.
133 content::LoadCommittedDetails details;
134 // |details.is_in_page| is default false, and |details.is_main_frame| is
135 // default true. This results in is_navigation_to_different_page() returning
136 // true.
137 DCHECK(details.is_navigation_to_different_page());
138 const content::NavigationController& controller =
139 web_contents_->GetController();
140 details.entry = controller.GetActiveEntry();
141 if (controller.GetLastCommittedEntry()) {
142 details.previous_entry_index = controller.GetLastCommittedEntryIndex();
143 details.previous_url = controller.GetLastCommittedEntry()->GetURL();
145 details.type = content::NAVIGATION_TYPE_NEW_PAGE;
146 for (int i = service->infobar_count() - 1; i >= 0; --i) {
147 infobars::InfoBar* infobar = service->infobar_at(i);
148 if (infobar->delegate()->ShouldExpire(
149 InfoBarService::NavigationDetailsFromLoadCommittedDetails(
150 details)))
151 service->RemoveInfoBar(infobar);
155 // TODO(bauerb): Extract an observer callback on SupervisedUserService for
156 // this.
157 Profile* profile =
158 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
159 PrefService* prefs = profile->GetPrefs();
160 pref_change_registrar_.Init(prefs);
161 pref_change_registrar_.Add(
162 prefs::kDefaultSupervisedUserFilteringBehavior,
163 base::Bind(&SupervisedUserInterstitial::OnFilteringPrefsChanged,
164 base::Unretained(this)));
165 pref_change_registrar_.Add(
166 prefs::kSupervisedUserManualHosts,
167 base::Bind(&SupervisedUserInterstitial::OnFilteringPrefsChanged,
168 base::Unretained(this)));
169 pref_change_registrar_.Add(
170 prefs::kSupervisedUserManualURLs,
171 base::Bind(&SupervisedUserInterstitial::OnFilteringPrefsChanged,
172 base::Unretained(this)));
174 interstitial_page_ =
175 content::InterstitialPage::Create(web_contents_, true, url_, this);
176 interstitial_page_->Show();
178 return true;
181 std::string SupervisedUserInterstitial::GetHTMLContents() {
182 base::DictionaryValue strings;
183 strings.SetString("blockPageTitle",
184 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE));
186 Profile* profile =
187 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
188 SupervisedUserService* supervised_user_service =
189 SupervisedUserServiceFactory::GetForProfile(profile);
191 bool allow_access_requests = supervised_user_service->AccessRequestsEnabled();
192 strings.SetBoolean("allowAccessRequests", allow_access_requests);
194 std::string profile_image_url = profile->GetPrefs()->GetString(
195 prefs::kSupervisedUserCustodianProfileImageURL);
196 strings.SetString("avatarURL1x", BuildAvatarImageUrl(profile_image_url,
197 kAvatarSize1x));
198 strings.SetString("avatarURL2x", BuildAvatarImageUrl(profile_image_url,
199 kAvatarSize2x));
201 std::string profile_image_url2 = profile->GetPrefs()->GetString(
202 prefs::kSupervisedUserSecondCustodianProfileImageURL);
203 strings.SetString("secondAvatarURL1x", BuildAvatarImageUrl(profile_image_url2,
204 kAvatarSize1x));
205 strings.SetString("secondAvatarURL2x", BuildAvatarImageUrl(profile_image_url2,
206 kAvatarSize2x));
208 base::string16 custodian =
209 base::UTF8ToUTF16(supervised_user_service->GetCustodianName());
210 strings.SetString(
211 "blockPageMessage",
212 allow_access_requests
213 ? l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_MESSAGE,
214 custodian)
215 : l10n_util::GetStringUTF16(
216 IDS_BLOCK_INTERSTITIAL_MESSAGE_ACCESS_REQUESTS_DISABLED));
218 strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON));
219 strings.SetString(
220 "requestAccessButton",
221 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON));
223 strings.SetString(
224 "requestSentMessage",
225 l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE,
226 custodian));
228 webui::SetFontAndTextDirection(&strings);
230 base::StringPiece html(ResourceBundle::GetSharedInstance().GetRawDataResource(
231 IDR_SUPERVISED_USER_BLOCK_INTERSTITIAL_HTML));
233 webui::UseVersion2 version;
234 return webui::GetI18nTemplateHtml(html, &strings);
237 void SupervisedUserInterstitial::CommandReceived(const std::string& command) {
238 // For use in histograms.
239 enum Commands {
240 PREVIEW,
241 BACK,
242 NTP,
243 ACCESS_REQUEST,
244 HISTOGRAM_BOUNDING_VALUE
247 if (command == "\"back\"") {
248 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
249 BACK,
250 HISTOGRAM_BOUNDING_VALUE);
252 // Close the tab if there is no history entry to go back to.
253 DCHECK(web_contents_->GetController().GetTransientEntry());
254 if (web_contents_->GetController().GetEntryCount() == 1)
255 TabCloser::CreateForWebContents(web_contents_);
257 interstitial_page_->DontProceed();
258 return;
261 if (command == "\"request\"") {
262 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
263 ACCESS_REQUEST,
264 HISTOGRAM_BOUNDING_VALUE);
266 Profile* profile =
267 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
268 SupervisedUserService* supervised_user_service =
269 SupervisedUserServiceFactory::GetForProfile(profile);
270 supervised_user_service->AddAccessRequest(url_);
271 DVLOG(1) << "Sent access request for " << url_.spec();
273 return;
276 NOTREACHED();
279 void SupervisedUserInterstitial::OnProceed() {
280 // CHECK instead of DCHECK as defense in depth in case we'd accidentally
281 // proceed on a blocked page.
282 CHECK(ShouldProceed());
283 DispatchContinueRequest(true);
286 void SupervisedUserInterstitial::OnDontProceed() {
287 DispatchContinueRequest(false);
290 bool SupervisedUserInterstitial::ShouldProceed() {
291 Profile* profile =
292 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
293 SupervisedUserService* supervised_user_service =
294 SupervisedUserServiceFactory::GetForProfile(profile);
295 SupervisedUserURLFilter* url_filter =
296 supervised_user_service->GetURLFilterForUIThread();
297 return url_filter->GetFilteringBehaviorForURL(url_) !=
298 SupervisedUserURLFilter::BLOCK;
301 void SupervisedUserInterstitial::OnFilteringPrefsChanged() {
302 if (ShouldProceed())
303 interstitial_page_->Proceed();
306 void SupervisedUserInterstitial::DispatchContinueRequest(
307 bool continue_request) {
308 BrowserThread::PostTask(
309 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request));