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/interstitials/security_interstitial_metrics_helper.h"
15 #include "chrome/browser/net/referrer.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/grit/browser_resources.h"
19 #include "chrome/grit/generated_resources.h"
20 #include "components/google/core/browser/google_util.h"
21 #include "content/public/browser/interstitial_page.h"
22 #include "content/public/browser/page_navigator.h"
23 #include "content/public/browser/web_contents.h"
24 #include "net/base/net_util.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(
88 ? SecurityInterstitialMetricsHelper::SET_EXTENDED_REPORTING_ENABLED
89 : SecurityInterstitialMetricsHelper::SET_EXTENDED_REPORTING_DISABLED
);
92 bool SecurityInterstitialPage::IsPrefEnabled(const char* pref
) {
94 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
95 return profile
->GetPrefs()->GetBoolean(pref
);
98 void SecurityInterstitialPage::OpenExtendedReportingPrivacyPolicy() {
99 metrics_helper()->RecordUserInteraction(
100 SecurityInterstitialMetricsHelper::SHOW_PRIVACY_POLICY
);
102 l10n_util::GetStringUTF8(IDS_SAFE_BROWSING_PRIVACY_POLICY_URL
));
103 privacy_url
= google_util::AppendGoogleLocaleParam(
104 privacy_url
, g_browser_process
->GetApplicationLocale());
105 OpenURLParams
params(privacy_url
, Referrer(), CURRENT_TAB
,
106 ui::PAGE_TRANSITION_LINK
, false);
107 web_contents()->OpenURL(params
);
110 SecurityInterstitialMetricsHelper
* SecurityInterstitialPage::metrics_helper() {
111 return metrics_helper_
.get();
114 void SecurityInterstitialPage::set_metrics_helper(
115 SecurityInterstitialMetricsHelper
* metrics_helper
) {
116 metrics_helper_
.reset(metrics_helper
);
119 base::string16
SecurityInterstitialPage::GetFormattedHostName() const {
120 std::string languages
;
122 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
124 languages
= profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
);
125 base::string16 host
= net::IDNToUnicode(request_url_
.host(), languages
);
126 if (base::i18n::IsRTL())
127 base::i18n::WrapStringWithLTRFormatting(&host
);
131 std::string
SecurityInterstitialPage::GetHTMLContents() {
132 base::DictionaryValue load_time_data
;
133 PopulateInterstitialStrings(&load_time_data
);
134 const std::string
& app_locale
= g_browser_process
->GetApplicationLocale();
135 webui::SetLoadTimeDataDefaults(app_locale
, &load_time_data
);
136 std::string html
= ResourceBundle::GetSharedInstance()
137 .GetRawDataResource(IDR_SECURITY_INTERSTITIAL_HTML
)
139 webui::AppendWebUiCssTextDefaults(&html
);
140 return webui::GetI18nTemplateHtml(html
, &load_time_data
);