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/grit/components_resources.h"
21 #include "components/security_interstitials/core/metrics_helper.h"
22 #include "components/url_formatter/url_formatter.h"
23 #include "content/public/browser/interstitial_page.h"
24 #include "content/public/browser/page_navigator.h"
25 #include "content/public/browser/web_contents.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/base/webui/jstemplate_builder.h"
29 #include "ui/base/webui/web_ui_util.h"
31 namespace interstitials
{
32 const char kBoxChecked
[] = "boxchecked";
33 const char kDisplayCheckBox
[] = "displaycheckbox";
34 const char kOptInLink
[] = "optInLink";
35 const char kPrivacyLinkHtml
[] =
36 "<a id=\"privacy-link\" href=\"\" onclick=\"sendCommand(%d); "
37 "return false;\" onmousedown=\"return false;\">%s</a>";
40 using content::OpenURLParams
;
41 using content::Referrer
;
43 SecurityInterstitialPage::SecurityInterstitialPage(
44 content::WebContents
* web_contents
,
45 const GURL
& request_url
)
46 : web_contents_(web_contents
),
47 request_url_(request_url
),
48 interstitial_page_(NULL
),
50 // Creating interstitial_page_ without showing it leaks memory, so don't
54 SecurityInterstitialPage::~SecurityInterstitialPage() {
57 content::InterstitialPage
* SecurityInterstitialPage::interstitial_page() const {
58 return interstitial_page_
;
61 content::WebContents
* SecurityInterstitialPage::web_contents() const {
65 GURL
SecurityInterstitialPage::request_url() const {
69 void SecurityInterstitialPage::DontCreateViewForTesting() {
73 void SecurityInterstitialPage::Show() {
74 DCHECK(!interstitial_page_
);
75 interstitial_page_
= content::InterstitialPage::Create(
76 web_contents_
, ShouldCreateNewNavigation(), request_url_
, this);
78 interstitial_page_
->DontCreateViewForTesting();
79 interstitial_page_
->Show();
82 void SecurityInterstitialPage::SetReportingPreference(bool report
) {
84 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
85 PrefService
* pref
= profile
->GetPrefs();
86 pref
->SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled
, report
);
87 metrics_helper()->RecordUserInteraction(
88 report
? security_interstitials::MetricsHelper::
89 SET_EXTENDED_REPORTING_ENABLED
90 : security_interstitials::MetricsHelper::
91 SET_EXTENDED_REPORTING_DISABLED
);
94 bool SecurityInterstitialPage::IsPrefEnabled(const char* pref
) {
96 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
97 return profile
->GetPrefs()->GetBoolean(pref
);
100 void SecurityInterstitialPage::OpenExtendedReportingPrivacyPolicy() {
101 metrics_helper()->RecordUserInteraction(
102 security_interstitials::MetricsHelper::SHOW_PRIVACY_POLICY
);
104 l10n_util::GetStringUTF8(IDS_SAFE_BROWSING_PRIVACY_POLICY_URL
));
105 privacy_url
= google_util::AppendGoogleLocaleParam(
106 privacy_url
, g_browser_process
->GetApplicationLocale());
107 OpenURLParams
params(privacy_url
, Referrer(), CURRENT_TAB
,
108 ui::PAGE_TRANSITION_LINK
, false);
109 web_contents()->OpenURL(params
);
112 security_interstitials::MetricsHelper
*
113 SecurityInterstitialPage::metrics_helper() const {
114 return metrics_helper_
.get();
117 void SecurityInterstitialPage::set_metrics_helper(
118 security_interstitials::MetricsHelper
* metrics_helper
) {
119 metrics_helper_
.reset(metrics_helper
);
122 base::string16
SecurityInterstitialPage::GetFormattedHostName() const {
123 std::string languages
;
125 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
127 languages
= profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
);
128 base::string16 host
=
129 url_formatter::IDNToUnicode(request_url_
.host(), languages
);
130 if (base::i18n::IsRTL())
131 base::i18n::WrapStringWithLTRFormatting(&host
);
135 std::string
SecurityInterstitialPage::GetHTMLContents() {
136 base::DictionaryValue load_time_data
;
137 PopulateInterstitialStrings(&load_time_data
);
138 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
139 webui::SetLoadTimeDataDefaults(app_locale
, &load_time_data
);
140 std::string html
= ResourceBundle::GetSharedInstance()
141 .GetRawDataResource(IDR_SECURITY_INTERSTITIAL_HTML
)
143 webui::AppendWebUiCssTextDefaults(&html
);
144 return webui::GetI18nTemplateHtml(html
, &load_time_data
);