1 // Copyright (c) 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/interstitials/security_interstitial_page.h"
7 #include "base/i18n/rtl.h"
9 #include "base/metrics/histogram.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/net/referrer.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/pref_names.h"
17 #include "chrome/grit/browser_resources.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "components/google/core/browser/google_util.h"
20 #include "components/security_interstitials/core/metrics_helper.h"
21 #include "components/url_formatter/url_formatter.h"
22 #include "content/public/browser/interstitial_page.h"
23 #include "content/public/browser/page_navigator.h"
24 #include "content/public/browser/web_contents.h"
25 #include "ui/base/l10n/l10n_util.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/base/webui/jstemplate_builder.h"
28 #include "ui/base/webui/web_ui_util.h"
30 namespace interstitials
{
31 const char kBoxChecked
[] = "boxchecked";
32 const char kDisplayCheckBox
[] = "displaycheckbox";
33 const char kOptInLink
[] = "optInLink";
34 const char kPrivacyLinkHtml
[] =
35 "<a id=\"privacy-link\" href=\"\" onclick=\"sendCommand(%d); "
36 "return false;\" onmousedown=\"return false;\">%s</a>";
39 using content::OpenURLParams
;
40 using content::Referrer
;
42 SecurityInterstitialPage::SecurityInterstitialPage(
43 content::WebContents
* web_contents
,
44 const GURL
& request_url
)
45 : web_contents_(web_contents
),
46 request_url_(request_url
),
47 interstitial_page_(NULL
),
49 // Creating interstitial_page_ without showing it leaks memory, so don't
53 SecurityInterstitialPage::~SecurityInterstitialPage() {
56 content::InterstitialPage
* SecurityInterstitialPage::interstitial_page() const {
57 return interstitial_page_
;
60 content::WebContents
* SecurityInterstitialPage::web_contents() const {
64 GURL
SecurityInterstitialPage::request_url() const {
68 void SecurityInterstitialPage::DontCreateViewForTesting() {
72 void SecurityInterstitialPage::Show() {
73 DCHECK(!interstitial_page_
);
74 interstitial_page_
= content::InterstitialPage::Create(
75 web_contents_
, ShouldCreateNewNavigation(), request_url_
, this);
77 interstitial_page_
->DontCreateViewForTesting();
78 interstitial_page_
->Show();
81 void SecurityInterstitialPage::SetReportingPreference(bool report
) {
83 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
84 PrefService
* pref
= profile
->GetPrefs();
85 pref
->SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled
, report
);
86 metrics_helper()->RecordUserInteraction(
87 report
? security_interstitials::MetricsHelper::
88 SET_EXTENDED_REPORTING_ENABLED
89 : security_interstitials::MetricsHelper::
90 SET_EXTENDED_REPORTING_DISABLED
);
93 bool SecurityInterstitialPage::IsPrefEnabled(const char* pref
) {
95 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
96 return profile
->GetPrefs()->GetBoolean(pref
);
99 void SecurityInterstitialPage::OpenExtendedReportingPrivacyPolicy() {
100 metrics_helper()->RecordUserInteraction(
101 security_interstitials::MetricsHelper::SHOW_PRIVACY_POLICY
);
103 l10n_util::GetStringUTF8(IDS_SAFE_BROWSING_PRIVACY_POLICY_URL
));
104 privacy_url
= google_util::AppendGoogleLocaleParam(
105 privacy_url
, g_browser_process
->GetApplicationLocale());
106 OpenURLParams
params(privacy_url
, Referrer(), CURRENT_TAB
,
107 ui::PAGE_TRANSITION_LINK
, false);
108 web_contents()->OpenURL(params
);
111 security_interstitials::MetricsHelper
*
112 SecurityInterstitialPage::metrics_helper() const {
113 return metrics_helper_
.get();
116 void SecurityInterstitialPage::set_metrics_helper(
117 security_interstitials::MetricsHelper
* metrics_helper
) {
118 metrics_helper_
.reset(metrics_helper
);
121 base::string16
SecurityInterstitialPage::GetFormattedHostName() const {
122 std::string languages
;
124 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
126 languages
= profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
);
127 base::string16 host
=
128 url_formatter::IDNToUnicode(request_url_
.host(), languages
);
129 if (base::i18n::IsRTL())
130 base::i18n::WrapStringWithLTRFormatting(&host
);
134 std::string
SecurityInterstitialPage::GetHTMLContents() {
135 base::DictionaryValue load_time_data
;
136 PopulateInterstitialStrings(&load_time_data
);
137 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
138 webui::SetLoadTimeDataDefaults(app_locale
, &load_time_data
);
139 std::string html
= ResourceBundle::GetSharedInstance()
140 .GetRawDataResource(IDR_SECURITY_INTERSTITIAL_HTML
)
142 webui::AppendWebUiCssTextDefaults(&html
);
143 return webui::GetI18nTemplateHtml(html
, &load_time_data
);