blob: 1d43850e870bd5d154640a04a1cb53147b22395d [file] [log] [blame]
[email protected]cce15bb2014-06-17 13:43:511// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]a09159a2012-11-29 12:51:482// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]cce15bb2014-06-17 13:43:515#include "chrome/browser/supervised_user/supervised_user_interstitial.h"
[email protected]a09159a2012-11-29 12:51:486
treibe7ccb7e2014-09-09 19:21:457#include "base/memory/weak_ptr.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]95c47d02014-08-19 09:17:5010#include "base/strings/string_number_conversions.h"
[email protected]112158af2013-06-07 23:46:1811#include "base/strings/utf_string_conversions.h"
[email protected]59f21562013-05-23 16:19:3012#include "base/values.h"
[email protected]93c23212013-09-19 14:22:3813#include "chrome/browser/infobars/infobar_service.h"
[email protected]a09159a2012-11-29 12:51:4814#include "chrome/browser/profiles/profile.h"
[email protected]cce15bb2014-06-17 13:43:5115#include "chrome/browser/supervised_user/supervised_user_service.h"
16#include "chrome/browser/supervised_user/supervised_user_service_factory.h"
[email protected]a09159a2012-11-29 12:51:4817#include "chrome/common/pref_names.h"
[email protected]af39f002014-08-22 10:18:1818#include "chrome/grit/generated_resources.h"
[email protected]051655ad2014-04-18 15:09:4119#include "components/infobars/core/infobar.h"
20#include "components/infobars/core/infobar_delegate.h"
[email protected]a09159a2012-11-29 12:51:4821#include "content/public/browser/browser_thread.h"
22#include "content/public/browser/interstitial_page.h"
[email protected]93c23212013-09-19 14:22:3823#include "content/public/browser/navigation_controller.h"
24#include "content/public/browser/navigation_details.h"
25#include "content/public/browser/navigation_entry.h"
[email protected]a09159a2012-11-29 12:51:4826#include "content/public/browser/web_contents.h"
treibe7ccb7e2014-09-09 19:21:4527#include "content/public/browser/web_contents_user_data.h"
[email protected]59f21562013-05-23 16:19:3028#include "content/public/browser/web_ui.h"
[email protected]a09159a2012-11-29 12:51:4829#include "grit/browser_resources.h"
[email protected]a09159a2012-11-29 12:51:4830#include "ui/base/l10n/l10n_util.h"
31#include "ui/base/resource/resource_bundle.h"
[email protected]cd67ed52013-10-15 01:22:1332#include "ui/base/webui/jstemplate_builder.h"
33#include "ui/base/webui/web_ui_util.h"
[email protected]a09159a2012-11-29 12:51:4834
treib9dbb9302015-01-08 14:42:3935#if defined(OS_ANDROID)
36#include "chrome/browser/supervised_user/child_accounts/child_account_feedback_reporter_android.h"
37#else
treibe7ccb7e2014-09-09 19:21:4538#include "chrome/browser/ui/browser_finder.h"
treib5dd8daf32014-12-18 17:36:3739#include "chrome/browser/ui/chrome_pages.h"
treibe7ccb7e2014-09-09 19:21:4540#include "chrome/browser/ui/tabs/tab_strip_model.h"
41#endif
42
[email protected]a09159a2012-11-29 12:51:4843using content::BrowserThread;
treibe7ccb7e2014-09-09 19:21:4544using content::WebContents;
[email protected]a09159a2012-11-29 12:51:4845
[email protected]95c47d02014-08-19 09:17:5046namespace {
47
48static const int kAvatarSize1x = 45;
49static const int kAvatarSize2x = 90;
50
treib885fee12014-08-29 08:07:5851std::string BuildAvatarImageUrl(const std::string& url, int size) {
[email protected]95c47d02014-08-19 09:17:5052 std::string result = url;
53 size_t slash = result.rfind('/');
54 if (slash != std::string::npos)
55 result.insert(slash, "/s" + base::IntToString(size));
treib885fee12014-08-29 08:07:5856 return result;
[email protected]95c47d02014-08-19 09:17:5057}
58
treibe7ccb7e2014-09-09 19:21:4559class TabCloser : public content::WebContentsUserData<TabCloser> {
60 // To use, call TabCloser::CreateForWebContents.
61 private:
62 friend class content::WebContentsUserData<TabCloser>;
63
64 explicit TabCloser(WebContents* web_contents)
65 : web_contents_(web_contents), weak_ptr_factory_(this) {
66 BrowserThread::PostTask(
67 BrowserThread::UI,
68 FROM_HERE,
69 base::Bind(&TabCloser::CloseTabImpl, weak_ptr_factory_.GetWeakPtr()));
70 }
dchengc072fff2014-10-21 11:39:0571 ~TabCloser() override {}
treibe7ccb7e2014-09-09 19:21:4572
73 void CloseTabImpl() {
74 // On Android, FindBrowserWithWebContents and TabStripModel don't exist.
75#if !defined(OS_ANDROID)
76 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_);
77 DCHECK(browser);
78 TabStripModel* tab_strip = browser->tab_strip_model();
79 DCHECK_NE(TabStripModel::kNoTab,
80 tab_strip->GetIndexOfWebContents(web_contents_));
81 if (tab_strip->count() <= 1) {
82 // Don't close the last tab in the window.
83 web_contents_->RemoveUserData(UserDataKey());
84 return;
85 }
86#endif
87 web_contents_->Close();
88 }
89
90 WebContents* web_contents_;
91 base::WeakPtrFactory<TabCloser> weak_ptr_factory_;
92};
93
94} // namespace
95
96DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabCloser);
[email protected]95c47d02014-08-19 09:17:5097
[email protected]b8af2d12014-04-29 07:30:4298// static
[email protected]95c47d02014-08-19 09:17:5099void SupervisedUserInterstitial::Show(
treibe7ccb7e2014-09-09 19:21:45100 WebContents* web_contents,
[email protected]95c47d02014-08-19 09:17:50101 const GURL& url,
treib170c3f82014-12-10 19:13:25102 SupervisedUserURLFilter::FilteringBehaviorReason reason,
[email protected]95c47d02014-08-19 09:17:50103 const base::Callback<void(bool)>& callback) {
[email protected]cce15bb2014-06-17 13:43:51104 SupervisedUserInterstitial* interstitial =
treib170c3f82014-12-10 19:13:25105 new SupervisedUserInterstitial(web_contents, url, reason, callback);
[email protected]b8af2d12014-04-29 07:30:42106
107 // If Init() does not complete fully, immediately delete the interstitial.
108 if (!interstitial->Init())
109 delete interstitial;
110 // Otherwise |interstitial_page_| is responsible for deleting it.
111}
112
[email protected]cce15bb2014-06-17 13:43:51113SupervisedUserInterstitial::SupervisedUserInterstitial(
treibe7ccb7e2014-09-09 19:21:45114 WebContents* web_contents,
[email protected]a09159a2012-11-29 12:51:48115 const GURL& url,
treib170c3f82014-12-10 19:13:25116 SupervisedUserURLFilter::FilteringBehaviorReason reason,
[email protected]a09159a2012-11-29 12:51:48117 const base::Callback<void(bool)>& callback)
118 : web_contents_(web_contents),
treibab0a39e2014-09-24 14:48:28119 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())),
[email protected]61f5da22013-09-05 15:52:10120 interstitial_page_(NULL),
[email protected]a09159a2012-11-29 12:51:48121 url_(url),
treib170c3f82014-12-10 19:13:25122 reason_(reason),
bauerb646019b12014-10-16 16:23:09123 callback_(callback),
124 weak_ptr_factory_(this) {}
[email protected]b8af2d12014-04-29 07:30:42125
treibab0a39e2014-09-24 14:48:28126SupervisedUserInterstitial::~SupervisedUserInterstitial() {
127 DCHECK(!web_contents_);
128}
[email protected]b8af2d12014-04-29 07:30:42129
[email protected]cce15bb2014-06-17 13:43:51130bool SupervisedUserInterstitial::Init() {
[email protected]61f5da22013-09-05 15:52:10131 if (ShouldProceed()) {
132 // It can happen that the site was only allowed very recently and the URL
133 // filter on the IO thread had not been updated yet. Proceed with the
134 // request without showing the interstitial.
135 DispatchContinueRequest(true);
[email protected]b8af2d12014-04-29 07:30:42136 return false;
[email protected]61f5da22013-09-05 15:52:10137 }
138
[email protected]b8af2d12014-04-29 07:30:42139 InfoBarService* service = InfoBarService::FromWebContents(web_contents_);
[email protected]93c23212013-09-19 14:22:38140 if (service) {
[email protected]b8af2d12014-04-29 07:30:42141 // Remove all the infobars which are attached to |web_contents_| and for
[email protected]93c23212013-09-19 14:22:38142 // which ShouldExpire() returns true.
143 content::LoadCommittedDetails details;
144 // |details.is_in_page| is default false, and |details.is_main_frame| is
145 // default true. This results in is_navigation_to_different_page() returning
146 // true.
147 DCHECK(details.is_navigation_to_different_page());
148 const content::NavigationController& controller =
[email protected]b8af2d12014-04-29 07:30:42149 web_contents_->GetController();
[email protected]93c23212013-09-19 14:22:38150 details.entry = controller.GetActiveEntry();
151 if (controller.GetLastCommittedEntry()) {
152 details.previous_entry_index = controller.GetLastCommittedEntryIndex();
153 details.previous_url = controller.GetLastCommittedEntry()->GetURL();
154 }
155 details.type = content::NAVIGATION_TYPE_NEW_PAGE;
[email protected]b44f1d32014-04-10 13:53:26156 for (int i = service->infobar_count() - 1; i >= 0; --i) {
[email protected]051655ad2014-04-18 15:09:41157 infobars::InfoBar* infobar = service->infobar_at(i);
[email protected]5daf1d92014-04-04 15:52:13158 if (infobar->delegate()->ShouldExpire(
159 InfoBarService::NavigationDetailsFromLoadCommittedDetails(
160 details)))
[email protected]b44f1d32014-04-10 13:53:26161 service->RemoveInfoBar(infobar);
[email protected]93c23212013-09-19 14:22:38162 }
163 }
164
treibab0a39e2014-09-24 14:48:28165 SupervisedUserService* supervised_user_service =
166 SupervisedUserServiceFactory::GetForProfile(profile_);
167 supervised_user_service->AddObserver(this);
[email protected]a09159a2012-11-29 12:51:48168
[email protected]0369d6ab2013-08-09 01:52:59169 interstitial_page_ =
[email protected]b8af2d12014-04-29 07:30:42170 content::InterstitialPage::Create(web_contents_, true, url_, this);
[email protected]a09159a2012-11-29 12:51:48171 interstitial_page_->Show();
[email protected]a09159a2012-11-29 12:51:48172
[email protected]b8af2d12014-04-29 07:30:42173 return true;
174}
[email protected]a09159a2012-11-29 12:51:48175
[email protected]cce15bb2014-06-17 13:43:51176std::string SupervisedUserInterstitial::GetHTMLContents() {
[email protected]cb1078de2013-12-23 20:04:22177 base::DictionaryValue strings;
[email protected]a09159a2012-11-29 12:51:48178 strings.SetString("blockPageTitle",
179 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE));
[email protected]fae057a2013-06-21 22:46:08180
[email protected]cce15bb2014-06-17 13:43:51181 SupervisedUserService* supervised_user_service =
treibab0a39e2014-09-24 14:48:28182 SupervisedUserServiceFactory::GetForProfile(profile_);
[email protected]0369d6ab2013-08-09 01:52:59183
[email protected]cce15bb2014-06-17 13:43:51184 bool allow_access_requests = supervised_user_service->AccessRequestsEnabled();
[email protected]0369d6ab2013-08-09 01:52:59185 strings.SetBoolean("allowAccessRequests", allow_access_requests);
186
treibab0a39e2014-09-24 14:48:28187 std::string profile_image_url = profile_->GetPrefs()->GetString(
[email protected]95c47d02014-08-19 09:17:50188 prefs::kSupervisedUserCustodianProfileImageURL);
189 strings.SetString("avatarURL1x", BuildAvatarImageUrl(profile_image_url,
[email protected]95c47d02014-08-19 09:17:50190 kAvatarSize1x));
191 strings.SetString("avatarURL2x", BuildAvatarImageUrl(profile_image_url,
[email protected]95c47d02014-08-19 09:17:50192 kAvatarSize2x));
193
treibab0a39e2014-09-24 14:48:28194 std::string profile_image_url2 = profile_->GetPrefs()->GetString(
[email protected]95c47d02014-08-19 09:17:50195 prefs::kSupervisedUserSecondCustodianProfileImageURL);
196 strings.SetString("secondAvatarURL1x", BuildAvatarImageUrl(profile_image_url2,
[email protected]95c47d02014-08-19 09:17:50197 kAvatarSize1x));
198 strings.SetString("secondAvatarURL2x", BuildAvatarImageUrl(profile_image_url2,
[email protected]95c47d02014-08-19 09:17:50199 kAvatarSize2x));
200
treibad86c012014-12-09 11:12:55201 bool is_child_account = profile_->IsChild();
treib5fc1aed2014-12-08 12:46:03202
[email protected]0085863a2013-12-06 21:19:03203 base::string16 custodian =
[email protected]cce15bb2014-06-17 13:43:51204 base::UTF8ToUTF16(supervised_user_service->GetCustodianName());
treib5fc1aed2014-12-08 12:46:03205 base::string16 second_custodian =
206 base::UTF8ToUTF16(supervised_user_service->GetSecondCustodianName());
207
208 base::string16 block_message;
209 if (allow_access_requests) {
210 if (is_child_account) {
211 block_message = l10n_util::GetStringUTF16(
212 second_custodian.empty()
213 ? IDS_CHILD_BLOCK_INTERSTITIAL_MESSAGE_SINGLE_PARENT
214 : IDS_CHILD_BLOCK_INTERSTITIAL_MESSAGE_MULTI_PARENT);
215 } else {
216 block_message = l10n_util::GetStringFUTF16(
217 IDS_BLOCK_INTERSTITIAL_MESSAGE, custodian);
218 }
219 } else {
220 block_message = l10n_util::GetStringUTF16(
221 IDS_BLOCK_INTERSTITIAL_MESSAGE_ACCESS_REQUESTS_DISABLED);
222 }
223 strings.SetString("blockPageMessage", block_message);
treib170c3f82014-12-10 19:13:25224 strings.SetString("blockReasonMessage", is_child_account
225 ? l10n_util::GetStringUTF16(
226 SupervisedUserURLFilter::GetBlockMessageID(reason_))
227 : base::string16());
[email protected]fae057a2013-06-21 22:46:08228
treib5dd8daf32014-12-18 17:36:37229 bool show_feedback = false;
treib9dbb9302015-01-08 14:42:39230#if defined(GOOGLE_CHROME_BUILD)
treib5dd8daf32014-12-18 17:36:37231 show_feedback = is_child_account &&
232 SupervisedUserURLFilter::ReasonIsAutomatic(reason_);
233#endif
234 strings.SetBoolean("showFeedbackLink", show_feedback);
235 strings.SetString("feedbackLink",
236 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_SEND_FEEDBACK));
237
[email protected]a09159a2012-11-29 12:51:48238 strings.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON));
treib5fc1aed2014-12-08 12:46:03239 strings.SetString("requestAccessButton", l10n_util::GetStringUTF16(
240 is_child_account
241 ? IDS_CHILD_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON
242 : IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON));
[email protected]59f21562013-05-23 16:19:30243
treib5fc1aed2014-12-08 12:46:03244 base::string16 request_sent_message;
245 if (is_child_account) {
246 request_sent_message = l10n_util::GetStringUTF16(
247 second_custodian.empty()
248 ? IDS_CHILD_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE_SINGLE_PARENT
249 : IDS_CHILD_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE_MULTI_PARENT);
250 } else {
251 request_sent_message = l10n_util::GetStringFUTF16(
252 IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE, custodian);
253 }
254 strings.SetString("requestSentMessage", request_sent_message);
[email protected]59f21562013-05-23 16:19:30255
[email protected]5053d402013-01-23 05:19:26256 webui::SetFontAndTextDirection(&strings);
[email protected]a09159a2012-11-29 12:51:48257
[email protected]0369d6ab2013-08-09 01:52:59258 base::StringPiece html(ResourceBundle::GetSharedInstance().GetRawDataResource(
[email protected]a7340d612014-06-25 22:09:15259 IDR_SUPERVISED_USER_BLOCK_INTERSTITIAL_HTML));
[email protected]a09159a2012-11-29 12:51:48260
[email protected]2779e3a2013-01-22 18:40:21261 return webui::GetI18nTemplateHtml(html, &strings);
[email protected]a09159a2012-11-29 12:51:48262}
263
[email protected]cce15bb2014-06-17 13:43:51264void SupervisedUserInterstitial::CommandReceived(const std::string& command) {
[email protected]2c145422013-01-24 18:15:40265 // For use in histograms.
266 enum Commands {
267 PREVIEW,
268 BACK,
269 NTP,
[email protected]59f21562013-05-23 16:19:30270 ACCESS_REQUEST,
[email protected]2c145422013-01-24 18:15:40271 HISTOGRAM_BOUNDING_VALUE
272 };
273
[email protected]a09159a2012-11-29 12:51:48274 if (command == "\"back\"") {
[email protected]2c145422013-01-24 18:15:40275 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
276 BACK,
277 HISTOGRAM_BOUNDING_VALUE);
treibe7ccb7e2014-09-09 19:21:45278
279 // Close the tab if there is no history entry to go back to.
280 DCHECK(web_contents_->GetController().GetTransientEntry());
281 if (web_contents_->GetController().GetEntryCount() == 1)
282 TabCloser::CreateForWebContents(web_contents_);
283
[email protected]a09159a2012-11-29 12:51:48284 interstitial_page_->DontProceed();
285 return;
286 }
287
[email protected]59f21562013-05-23 16:19:30288 if (command == "\"request\"") {
[email protected]2c145422013-01-24 18:15:40289 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
[email protected]59f21562013-05-23 16:19:30290 ACCESS_REQUEST,
[email protected]2c145422013-01-24 18:15:40291 HISTOGRAM_BOUNDING_VALUE);
[email protected]ad7a89e2013-05-31 12:03:24292
[email protected]cce15bb2014-06-17 13:43:51293 SupervisedUserService* supervised_user_service =
treibab0a39e2014-09-24 14:48:28294 SupervisedUserServiceFactory::GetForProfile(profile_);
bauerb646019b12014-10-16 16:23:09295 supervised_user_service->AddAccessRequest(
296 url_, base::Bind(&SupervisedUserInterstitial::OnAccessRequestAdded,
297 weak_ptr_factory_.GetWeakPtr()));
[email protected]a09159a2012-11-29 12:51:48298 return;
299 }
300
treib5dd8daf32014-12-18 17:36:37301 if (command == "\"feedback\"") {
treib9dbb9302015-01-08 14:42:39302#if defined(OS_ANDROID)
303 ReportChildAccountFeedback(web_contents_, url_);
304#else
treib5dd8daf32014-12-18 17:36:37305 std::string bucket;
306#if defined(OS_CHROMEOS)
307 bucket = "UnicornCrOS";
308#else
309 bucket = "UnicornDesktop";
310#endif
311 chrome::ShowFeedbackPage(
312 chrome::FindBrowserWithWebContents(web_contents_),
313 l10n_util::GetStringUTF8(IDS_BLOCK_INTERSTITIAL_DEFAULT_FEEDBACK_TEXT),
314 bucket);
treib9dbb9302015-01-08 14:42:39315#endif
treib5dd8daf32014-12-18 17:36:37316 return;
317 }
treib5dd8daf32014-12-18 17:36:37318
[email protected]a09159a2012-11-29 12:51:48319 NOTREACHED();
320}
321
[email protected]cce15bb2014-06-17 13:43:51322void SupervisedUserInterstitial::OnProceed() {
[email protected]61f5da22013-09-05 15:52:10323 DispatchContinueRequest(true);
324}
[email protected]a09159a2012-11-29 12:51:48325
[email protected]cce15bb2014-06-17 13:43:51326void SupervisedUserInterstitial::OnDontProceed() {
[email protected]6c7af96d2013-03-28 22:55:14327 DispatchContinueRequest(false);
[email protected]a09159a2012-11-29 12:51:48328}
329
treibab0a39e2014-09-24 14:48:28330void SupervisedUserInterstitial::OnURLFilterChanged() {
331 if (ShouldProceed())
332 interstitial_page_->Proceed();
333}
334
bauerb646019b12014-10-16 16:23:09335void SupervisedUserInterstitial::OnAccessRequestAdded(bool success) {
336 // TODO(akuegel): Figure out how to show the result of issuing the permission
337 // request in the UI. Currently, we assume the permission request was created
338 // successfully.
bauerbfc4af2ac2014-11-07 14:27:05339 VLOG(1) << "Sent access request for " << url_.spec()
340 << (success ? " successfully" : " unsuccessfully");
bauerb646019b12014-10-16 16:23:09341}
342
[email protected]cce15bb2014-06-17 13:43:51343bool SupervisedUserInterstitial::ShouldProceed() {
[email protected]cce15bb2014-06-17 13:43:51344 SupervisedUserService* supervised_user_service =
treibab0a39e2014-09-24 14:48:28345 SupervisedUserServiceFactory::GetForProfile(profile_);
[email protected]cce15bb2014-06-17 13:43:51346 SupervisedUserURLFilter* url_filter =
347 supervised_user_service->GetURLFilterForUIThread();
treib9e4fab902014-10-29 14:25:26348 SupervisedUserURLFilter::FilteringBehavior behavior;
349 return (url_filter->GetManualFilteringBehaviorForURL(url_, &behavior) &&
350 behavior != SupervisedUserURLFilter::BLOCK);
[email protected]61f5da22013-09-05 15:52:10351}
352
[email protected]cce15bb2014-06-17 13:43:51353void SupervisedUserInterstitial::DispatchContinueRequest(
354 bool continue_request) {
treibab0a39e2014-09-24 14:48:28355 SupervisedUserService* supervised_user_service =
356 SupervisedUserServiceFactory::GetForProfile(profile_);
357 supervised_user_service->RemoveObserver(this);
358
[email protected]0369d6ab2013-08-09 01:52:59359 BrowserThread::PostTask(
360 BrowserThread::IO, FROM_HERE, base::Bind(callback_, continue_request));
treibab0a39e2014-09-24 14:48:28361
362 // After this, the WebContents may be destroyed. Make sure we don't try to use
363 // it again.
364 web_contents_ = NULL;
[email protected]6c7af96d2013-03-28 22:55:14365}