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/stringprintf.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/infobars/infobar_service.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/supervised_user/supervised_user_service.h"
18 #include "chrome/browser/supervised_user/supervised_user_service_factory.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/grit/generated_resources.h"
21 #include "components/infobars/core/infobar.h"
22 #include "components/infobars/core/infobar_delegate.h"
23 #include "content/public/browser/browser_thread.h"
24 #include "content/public/browser/interstitial_page.h"
25 #include "content/public/browser/navigation_controller.h"
26 #include "content/public/browser/navigation_details.h"
27 #include "content/public/browser/navigation_entry.h"
28 #include "content/public/browser/render_frame_host.h"
29 #include "content/public/browser/render_view_host.h"
30 #include "content/public/browser/web_contents.h"
31 #include "content/public/browser/web_contents_user_data.h"
32 #include "content/public/browser/web_ui.h"
33 #include "grit/browser_resources.h"
34 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/base/resource/resource_bundle.h"
36 #include "ui/base/webui/jstemplate_builder.h"
37 #include "ui/base/webui/web_ui_util.h"
39 #if defined(OS_ANDROID)
40 #include "chrome/browser/supervised_user/child_accounts/child_account_feedback_reporter_android.h"
42 #include "chrome/browser/ui/browser_finder.h"
43 #include "chrome/browser/ui/chrome_pages.h"
44 #include "chrome/browser/ui/tabs/tab_strip_model.h"
47 using content::BrowserThread
;
48 using content::WebContents
;
52 static const int kAvatarSize1x
= 45;
53 static const int kAvatarSize2x
= 90;
55 std::string
BuildAvatarImageUrl(const std::string
& url
, int size
) {
56 std::string result
= url
;
57 size_t slash
= result
.rfind('/');
58 if (slash
!= std::string::npos
)
59 result
.insert(slash
, "/s" + base::IntToString(size
));
63 class TabCloser
: public content::WebContentsUserData
<TabCloser
> {
64 // To use, call TabCloser::CreateForWebContents.
66 friend class content::WebContentsUserData
<TabCloser
>;
68 explicit TabCloser(WebContents
* web_contents
)
69 : web_contents_(web_contents
), weak_ptr_factory_(this) {
70 BrowserThread::PostTask(
73 base::Bind(&TabCloser::CloseTabImpl
, weak_ptr_factory_
.GetWeakPtr()));
75 ~TabCloser() override
{}
78 // On Android, FindBrowserWithWebContents and TabStripModel don't exist.
79 #if !defined(OS_ANDROID)
80 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents_
);
82 TabStripModel
* tab_strip
= browser
->tab_strip_model();
83 DCHECK_NE(TabStripModel::kNoTab
,
84 tab_strip
->GetIndexOfWebContents(web_contents_
));
85 if (tab_strip
->count() <= 1) {
86 // Don't close the last tab in the window.
87 web_contents_
->RemoveUserData(UserDataKey());
91 web_contents_
->Close();
94 WebContents
* web_contents_
;
95 base::WeakPtrFactory
<TabCloser
> weak_ptr_factory_
;
100 DEFINE_WEB_CONTENTS_USER_DATA_KEY(TabCloser
);
102 content::InterstitialPageDelegate::TypeID
103 SupervisedUserInterstitial::kTypeForTesting
=
104 &SupervisedUserInterstitial::kTypeForTesting
;
107 void SupervisedUserInterstitial::Show(
108 WebContents
* web_contents
,
110 SupervisedUserURLFilter::FilteringBehaviorReason reason
,
111 const base::Callback
<void(bool)>& callback
) {
112 SupervisedUserInterstitial
* interstitial
=
113 new SupervisedUserInterstitial(web_contents
, url
, reason
, callback
);
115 // If Init() does not complete fully, immediately delete the interstitial.
116 if (!interstitial
->Init())
118 // Otherwise |interstitial_page_| is responsible for deleting it.
121 SupervisedUserInterstitial::SupervisedUserInterstitial(
122 WebContents
* web_contents
,
124 SupervisedUserURLFilter::FilteringBehaviorReason reason
,
125 const base::Callback
<void(bool)>& callback
)
126 : web_contents_(web_contents
),
127 profile_(Profile::FromBrowserContext(web_contents
->GetBrowserContext())),
128 interstitial_page_(NULL
),
132 weak_ptr_factory_(this) {}
134 SupervisedUserInterstitial::~SupervisedUserInterstitial() {
135 DCHECK(!web_contents_
);
138 bool SupervisedUserInterstitial::Init() {
139 if (ShouldProceed()) {
140 // It can happen that the site was only allowed very recently and the URL
141 // filter on the IO thread had not been updated yet. Proceed with the
142 // request without showing the interstitial.
143 DispatchContinueRequest(true);
147 InfoBarService
* service
= InfoBarService::FromWebContents(web_contents_
);
149 // Remove all the infobars which are attached to |web_contents_| and for
150 // which ShouldExpire() returns true.
151 content::LoadCommittedDetails details
;
152 // |details.is_in_page| is default false, and |details.is_main_frame| is
153 // default true. This results in is_navigation_to_different_page() returning
155 DCHECK(details
.is_navigation_to_different_page());
156 const content::NavigationController
& controller
=
157 web_contents_
->GetController();
158 details
.entry
= controller
.GetActiveEntry();
159 if (controller
.GetLastCommittedEntry()) {
160 details
.previous_entry_index
= controller
.GetLastCommittedEntryIndex();
161 details
.previous_url
= controller
.GetLastCommittedEntry()->GetURL();
163 details
.type
= content::NAVIGATION_TYPE_NEW_PAGE
;
164 for (int i
= service
->infobar_count() - 1; i
>= 0; --i
) {
165 infobars::InfoBar
* infobar
= service
->infobar_at(i
);
166 if (infobar
->delegate()->ShouldExpire(
167 InfoBarService::NavigationDetailsFromLoadCommittedDetails(
169 service
->RemoveInfoBar(infobar
);
173 SupervisedUserService
* supervised_user_service
=
174 SupervisedUserServiceFactory::GetForProfile(profile_
);
175 supervised_user_service
->AddObserver(this);
178 content::InterstitialPage::Create(web_contents_
, true, url_
, this);
179 interstitial_page_
->Show();
184 std::string
SupervisedUserInterstitial::GetHTMLContents() {
185 base::DictionaryValue strings
;
186 strings
.SetString("blockPageTitle",
187 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE
));
189 SupervisedUserService
* supervised_user_service
=
190 SupervisedUserServiceFactory::GetForProfile(profile_
);
192 bool allow_access_requests
= supervised_user_service
->AccessRequestsEnabled();
193 strings
.SetBoolean("allowAccessRequests", allow_access_requests
);
195 std::string profile_image_url
= profile_
->GetPrefs()->GetString(
196 prefs::kSupervisedUserCustodianProfileImageURL
);
197 strings
.SetString("avatarURL1x", BuildAvatarImageUrl(profile_image_url
,
199 strings
.SetString("avatarURL2x", BuildAvatarImageUrl(profile_image_url
,
202 std::string profile_image_url2
= profile_
->GetPrefs()->GetString(
203 prefs::kSupervisedUserSecondCustodianProfileImageURL
);
204 strings
.SetString("secondAvatarURL1x", BuildAvatarImageUrl(profile_image_url2
,
206 strings
.SetString("secondAvatarURL2x", BuildAvatarImageUrl(profile_image_url2
,
209 bool is_child_account
= profile_
->IsChild();
211 base::string16 custodian
=
212 base::UTF8ToUTF16(supervised_user_service
->GetCustodianName());
213 base::string16 second_custodian
=
214 base::UTF8ToUTF16(supervised_user_service
->GetSecondCustodianName());
216 base::string16 block_message
;
217 if (allow_access_requests
) {
218 if (is_child_account
) {
219 block_message
= l10n_util::GetStringUTF16(
220 second_custodian
.empty()
221 ? IDS_CHILD_BLOCK_INTERSTITIAL_MESSAGE_SINGLE_PARENT
222 : IDS_CHILD_BLOCK_INTERSTITIAL_MESSAGE_MULTI_PARENT
);
224 block_message
= l10n_util::GetStringFUTF16(
225 IDS_BLOCK_INTERSTITIAL_MESSAGE
, custodian
);
228 block_message
= l10n_util::GetStringUTF16(
229 IDS_BLOCK_INTERSTITIAL_MESSAGE_ACCESS_REQUESTS_DISABLED
);
231 strings
.SetString("blockPageMessage", block_message
);
232 strings
.SetString("blockReasonMessage", is_child_account
233 ? l10n_util::GetStringUTF16(
234 SupervisedUserURLFilter::GetBlockMessageID(reason_
))
237 bool show_feedback
= false;
238 #if defined(GOOGLE_CHROME_BUILD)
239 show_feedback
= is_child_account
&&
240 SupervisedUserURLFilter::ReasonIsAutomatic(reason_
);
242 strings
.SetBoolean("showFeedbackLink", show_feedback
);
243 strings
.SetString("feedbackLink",
244 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_SEND_FEEDBACK
));
246 strings
.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON
));
247 strings
.SetString("requestAccessButton", l10n_util::GetStringUTF16(
249 ? IDS_CHILD_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON
250 : IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON
));
252 base::string16 request_sent_message
;
253 base::string16 request_failed_message
;
254 if (is_child_account
) {
255 if (second_custodian
.empty()) {
256 request_sent_message
= l10n_util::GetStringUTF16(
257 IDS_CHILD_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE_SINGLE_PARENT
);
258 request_failed_message
= l10n_util::GetStringUTF16(
259 IDS_CHILD_BLOCK_INTERSTITIAL_REQUEST_FAILED_MESSAGE_SINGLE_PARENT
);
261 request_sent_message
= l10n_util::GetStringUTF16(
262 IDS_CHILD_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE_MULTI_PARENT
);
263 request_failed_message
= l10n_util::GetStringUTF16(
264 IDS_CHILD_BLOCK_INTERSTITIAL_REQUEST_FAILED_MESSAGE_MULTI_PARENT
);
267 request_sent_message
= l10n_util::GetStringFUTF16(
268 IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE
, custodian
);
269 request_failed_message
= l10n_util::GetStringFUTF16(
270 IDS_BLOCK_INTERSTITIAL_REQUEST_FAILED_MESSAGE
, custodian
);
272 strings
.SetString("requestSentMessage", request_sent_message
);
273 strings
.SetString("requestFailedMessage", request_failed_message
);
275 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
276 webui::SetLoadTimeDataDefaults(app_locale
, &strings
);
279 ResourceBundle::GetSharedInstance()
280 .GetRawDataResource(IDR_SUPERVISED_USER_BLOCK_INTERSTITIAL_HTML
)
282 webui::AppendWebUiCssTextDefaults(&html
);
284 return webui::GetI18nTemplateHtml(html
, &strings
);
287 void SupervisedUserInterstitial::CommandReceived(const std::string
& command
) {
288 // For use in histograms.
294 HISTOGRAM_BOUNDING_VALUE
297 if (command
== "\"back\"") {
298 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
300 HISTOGRAM_BOUNDING_VALUE
);
302 // Close the tab if there is no history entry to go back to.
303 DCHECK(web_contents_
->GetController().GetTransientEntry());
304 if (web_contents_
->GetController().GetEntryCount() == 1)
305 TabCloser::CreateForWebContents(web_contents_
);
307 interstitial_page_
->DontProceed();
311 if (command
== "\"request\"") {
312 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
314 HISTOGRAM_BOUNDING_VALUE
);
316 SupervisedUserService
* supervised_user_service
=
317 SupervisedUserServiceFactory::GetForProfile(profile_
);
318 supervised_user_service
->AddURLAccessRequest(
319 url_
, base::Bind(&SupervisedUserInterstitial::OnAccessRequestAdded
,
320 weak_ptr_factory_
.GetWeakPtr()));
324 if (command
== "\"feedback\"") {
325 base::string16 reason
= l10n_util::GetStringUTF16(
326 SupervisedUserURLFilter::GetBlockMessageID(reason_
));
327 std::string message
= l10n_util::GetStringFUTF8(
328 IDS_BLOCK_INTERSTITIAL_DEFAULT_FEEDBACK_TEXT
, reason
);
329 #if defined(OS_ANDROID)
330 ReportChildAccountFeedback(web_contents_
, message
, url_
);
332 chrome::ShowFeedbackPage(chrome::FindBrowserWithWebContents(web_contents_
),
333 message
, std::string());
341 void SupervisedUserInterstitial::OnProceed() {
342 DispatchContinueRequest(true);
345 void SupervisedUserInterstitial::OnDontProceed() {
346 DispatchContinueRequest(false);
349 content::InterstitialPageDelegate::TypeID
350 SupervisedUserInterstitial::GetTypeForTesting() const {
351 return SupervisedUserInterstitial::kTypeForTesting
;
354 void SupervisedUserInterstitial::OnURLFilterChanged() {
356 interstitial_page_
->Proceed();
359 void SupervisedUserInterstitial::OnAccessRequestAdded(bool success
) {
360 VLOG(1) << "Sent access request for " << url_
.spec()
361 << (success
? " successfully" : " unsuccessfully");
363 base::StringPrintf("setRequestStatus(%s);", success
? "true" : "false");
364 interstitial_page_
->GetMainFrame()->ExecuteJavaScript(
365 base::ASCIIToUTF16(jsFunc
));
368 bool SupervisedUserInterstitial::ShouldProceed() {
369 SupervisedUserService
* supervised_user_service
=
370 SupervisedUserServiceFactory::GetForProfile(profile_
);
371 SupervisedUserURLFilter
* url_filter
=
372 supervised_user_service
->GetURLFilterForUIThread();
373 SupervisedUserURLFilter::FilteringBehavior behavior
;
374 if (url_filter
->HasAsyncURLChecker()) {
375 if (!url_filter
->GetManualFilteringBehaviorForURL(url_
, &behavior
))
378 behavior
= url_filter
->GetFilteringBehaviorForURL(url_
);
380 return behavior
!= SupervisedUserURLFilter::BLOCK
;
383 void SupervisedUserInterstitial::DispatchContinueRequest(
384 bool continue_request
) {
385 SupervisedUserService
* supervised_user_service
=
386 SupervisedUserServiceFactory::GetForProfile(profile_
);
387 supervised_user_service
->RemoveObserver(this);
389 BrowserThread::PostTask(
390 BrowserThread::IO
, FROM_HERE
, base::Bind(callback_
, continue_request
));
392 // After this, the WebContents may be destroyed. Make sure we don't try to use
394 web_contents_
= NULL
;