1 // Copyright (c) 2012 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/managed_mode/managed_mode_interstitial.h"
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.h"
13 #include "chrome/browser/infobars/infobar_delegate.h"
14 #include "chrome/browser/infobars/infobar_service.h"
15 #include "chrome/browser/managed_mode/managed_user_service.h"
16 #include "chrome/browser/managed_mode/managed_user_service_factory.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/common/pref_names.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"
34 using content::BrowserThread
;
36 ManagedModeInterstitial::ManagedModeInterstitial(
37 content::WebContents
* web_contents
,
39 const base::Callback
<void(bool)>& callback
)
40 : web_contents_(web_contents
),
41 interstitial_page_(NULL
),
44 if (ShouldProceed()) {
45 // It can happen that the site was only allowed very recently and the URL
46 // filter on the IO thread had not been updated yet. Proceed with the
47 // request without showing the interstitial.
48 DispatchContinueRequest(true);
53 InfoBarService
* service
= InfoBarService::FromWebContents(web_contents
);
55 // Remove all the infobars which are attached to |web_contents| and for
56 // which ShouldExpire() returns true.
57 content::LoadCommittedDetails details
;
58 // |details.is_in_page| is default false, and |details.is_main_frame| is
59 // default true. This results in is_navigation_to_different_page() returning
61 DCHECK(details
.is_navigation_to_different_page());
62 const content::NavigationController
& controller
=
63 web_contents
->GetController();
64 details
.entry
= controller
.GetActiveEntry();
65 if (controller
.GetLastCommittedEntry()) {
66 details
.previous_entry_index
= controller
.GetLastCommittedEntryIndex();
67 details
.previous_url
= controller
.GetLastCommittedEntry()->GetURL();
69 details
.type
= content::NAVIGATION_TYPE_NEW_PAGE
;
70 for (int i
= service
->infobar_count() - 1; i
>= 0; --i
) {
71 if (service
->infobar_at(i
)->delegate()->ShouldExpire(details
))
72 service
->RemoveInfoBar(service
->infobar_at(i
));
76 // TODO(bauerb): Extract an observer callback on ManagedUserService for this.
78 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
79 PrefService
* prefs
= profile
->GetPrefs();
80 pref_change_registrar_
.Init(prefs
);
81 pref_change_registrar_
.Add(
82 prefs::kDefaultManagedModeFilteringBehavior
,
83 base::Bind(&ManagedModeInterstitial::OnFilteringPrefsChanged
,
84 base::Unretained(this)));
85 pref_change_registrar_
.Add(
86 prefs::kManagedModeManualHosts
,
87 base::Bind(&ManagedModeInterstitial::OnFilteringPrefsChanged
,
88 base::Unretained(this)));
89 pref_change_registrar_
.Add(
90 prefs::kManagedModeManualURLs
,
91 base::Bind(&ManagedModeInterstitial::OnFilteringPrefsChanged
,
92 base::Unretained(this)));
94 languages_
= prefs
->GetString(prefs::kAcceptLanguages
);
96 content::InterstitialPage::Create(web_contents
, true, url_
, this);
97 interstitial_page_
->Show();
100 ManagedModeInterstitial::~ManagedModeInterstitial() {}
102 std::string
ManagedModeInterstitial::GetHTMLContents() {
103 base::DictionaryValue strings
;
104 strings
.SetString("blockPageTitle",
105 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_TITLE
));
108 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
109 ManagedUserService
* managed_user_service
=
110 ManagedUserServiceFactory::GetForProfile(profile
);
112 bool allow_access_requests
= managed_user_service
->AccessRequestsEnabled();
113 strings
.SetBoolean("allowAccessRequests", allow_access_requests
);
115 base::string16 custodian
=
116 base::UTF8ToUTF16(managed_user_service
->GetCustodianName());
119 allow_access_requests
120 ? l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_MESSAGE
,
122 : l10n_util::GetStringUTF16(
123 IDS_BLOCK_INTERSTITIAL_MESSAGE_ACCESS_REQUESTS_DISABLED
));
125 strings
.SetString("backButton", l10n_util::GetStringUTF16(IDS_BACK_BUTTON
));
127 "requestAccessButton",
128 l10n_util::GetStringUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_ACCESS_BUTTON
));
131 "requestSentMessage",
132 l10n_util::GetStringFUTF16(IDS_BLOCK_INTERSTITIAL_REQUEST_SENT_MESSAGE
,
135 webui::SetFontAndTextDirection(&strings
);
137 base::StringPiece
html(ResourceBundle::GetSharedInstance().GetRawDataResource(
138 IDR_MANAGED_MODE_BLOCK_INTERSTITIAL_HTML
));
140 webui::UseVersion2 version
;
141 return webui::GetI18nTemplateHtml(html
, &strings
);
144 void ManagedModeInterstitial::CommandReceived(const std::string
& command
) {
145 // For use in histograms.
151 HISTOGRAM_BOUNDING_VALUE
154 if (command
== "\"back\"") {
155 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
157 HISTOGRAM_BOUNDING_VALUE
);
158 interstitial_page_
->DontProceed();
162 if (command
== "\"request\"") {
163 UMA_HISTOGRAM_ENUMERATION("ManagedMode.BlockingInterstitialCommand",
165 HISTOGRAM_BOUNDING_VALUE
);
168 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
169 ManagedUserService
* managed_user_service
=
170 ManagedUserServiceFactory::GetForProfile(profile
);
171 managed_user_service
->AddAccessRequest(url_
);
172 DVLOG(1) << "Sent access request for " << url_
.spec();
180 void ManagedModeInterstitial::OnProceed() {
181 // CHECK instead of DCHECK as defense in depth in case we'd accidentally
182 // proceed on a blocked page.
183 CHECK(ShouldProceed());
184 DispatchContinueRequest(true);
187 void ManagedModeInterstitial::OnDontProceed() {
188 DispatchContinueRequest(false);
191 bool ManagedModeInterstitial::ShouldProceed() {
193 Profile::FromBrowserContext(web_contents_
->GetBrowserContext());
194 ManagedUserService
* managed_user_service
=
195 ManagedUserServiceFactory::GetForProfile(profile
);
196 ManagedModeURLFilter
* url_filter
=
197 managed_user_service
->GetURLFilterForUIThread();
198 return url_filter
->GetFilteringBehaviorForURL(url_
) !=
199 ManagedModeURLFilter::BLOCK
;
202 void ManagedModeInterstitial::OnFilteringPrefsChanged() {
204 interstitial_page_
->Proceed();
207 void ManagedModeInterstitial::DispatchContinueRequest(bool continue_request
) {
208 BrowserThread::PostTask(
209 BrowserThread::IO
, FROM_HERE
, base::Bind(callback_
, continue_request
));