OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/interstitials/security_interstitial_page.h" | 5 #include "chrome/browser/interstitials/security_interstitial_page.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" |
7 #include "base/i18n/rtl.h" | 9 #include "base/i18n/rtl.h" |
| 10 #include "base/metrics/histogram.h" |
| 11 #include "base/prefs/pref_service.h" |
8 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
9 #include "base/values.h" | 13 #include "base/values.h" |
10 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/pref_names.h" |
11 #include "chrome/grit/browser_resources.h" | 17 #include "chrome/grit/browser_resources.h" |
12 #include "content/public/browser/interstitial_page.h" | 18 #include "content/public/browser/interstitial_page.h" |
13 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
14 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
15 #include "ui/base/webui/jstemplate_builder.h" | 21 #include "ui/base/webui/jstemplate_builder.h" |
16 #include "ui/base/webui/web_ui_util.h" | 22 #include "ui/base/webui/web_ui_util.h" |
17 | 23 |
| 24 namespace interstitials { |
| 25 const char kBoxChecked[] = "boxchecked"; |
| 26 const char kDisplayCheckBox[] = "displaycheckbox"; |
| 27 const char kOptInLink[] = "optInLink"; |
| 28 const char kPrivacyLinkHtml[] = |
| 29 "<a id=\"privacy-link\" href=\"\" onclick=\"sendCommand('showPrivacy'); " |
| 30 "return false;\" onmousedown=\"return false;\">%s</a>"; |
| 31 } |
| 32 |
18 SecurityInterstitialPage::SecurityInterstitialPage( | 33 SecurityInterstitialPage::SecurityInterstitialPage( |
19 content::WebContents* web_contents, | 34 content::WebContents* web_contents, |
20 const GURL& request_url) | 35 const GURL& request_url) |
21 : web_contents_(web_contents), | 36 : certificate_report_callback_for_testing_(base::Bind(&base::DoNothing)), |
| 37 web_contents_(web_contents), |
22 request_url_(request_url), | 38 request_url_(request_url), |
23 interstitial_page_(NULL), | 39 interstitial_page_(NULL), |
24 create_view_(true) { | 40 create_view_(true) { |
25 // Creating interstitial_page_ without showing it leaks memory, so don't | 41 // Creating interstitial_page_ without showing it leaks memory, so don't |
26 // create it here. | 42 // create it here. |
27 } | 43 } |
28 | 44 |
29 SecurityInterstitialPage::~SecurityInterstitialPage() { | 45 SecurityInterstitialPage::~SecurityInterstitialPage() { |
30 } | 46 } |
31 | 47 |
32 content::InterstitialPage* SecurityInterstitialPage::interstitial_page() const { | 48 content::InterstitialPage* SecurityInterstitialPage::interstitial_page() const { |
33 return interstitial_page_; | 49 return interstitial_page_; |
34 } | 50 } |
35 | 51 |
36 content::WebContents* SecurityInterstitialPage::web_contents() const { | 52 content::WebContents* SecurityInterstitialPage::web_contents() const { |
37 return web_contents_; | 53 return web_contents_; |
38 } | 54 } |
39 | 55 |
40 GURL SecurityInterstitialPage::request_url() const { | 56 GURL SecurityInterstitialPage::request_url() const { |
41 return request_url_; | 57 return request_url_; |
42 } | 58 } |
43 | 59 |
44 void SecurityInterstitialPage::DontCreateViewForTesting() { | 60 void SecurityInterstitialPage::DontCreateViewForTesting() { |
45 create_view_ = false; | 61 create_view_ = false; |
46 } | 62 } |
47 | 63 |
| 64 void SecurityInterstitialPage::SetCertificateReportCallbackForTesting( |
| 65 const base::Closure& callback) { |
| 66 certificate_report_callback_for_testing_ = callback; |
| 67 } |
| 68 |
48 void SecurityInterstitialPage::Show() { | 69 void SecurityInterstitialPage::Show() { |
49 DCHECK(!interstitial_page_); | 70 DCHECK(!interstitial_page_); |
50 interstitial_page_ = content::InterstitialPage::Create( | 71 interstitial_page_ = content::InterstitialPage::Create( |
51 web_contents_, ShouldCreateNewNavigation(), request_url_, this); | 72 web_contents_, ShouldCreateNewNavigation(), request_url_, this); |
52 if (!create_view_) | 73 if (!create_view_) |
53 interstitial_page_->DontCreateViewForTesting(); | 74 interstitial_page_->DontCreateViewForTesting(); |
54 interstitial_page_->Show(); | 75 interstitial_page_->Show(); |
55 } | 76 } |
56 | 77 |
57 base::string16 SecurityInterstitialPage::GetFormattedHostName() const { | 78 base::string16 SecurityInterstitialPage::GetFormattedHostName() const { |
58 base::string16 host(base::UTF8ToUTF16(request_url_.host())); | 79 base::string16 host(base::UTF8ToUTF16(request_url_.host())); |
59 if (base::i18n::IsRTL()) | 80 if (base::i18n::IsRTL()) |
60 base::i18n::WrapStringWithLTRFormatting(&host); | 81 base::i18n::WrapStringWithLTRFormatting(&host); |
61 return host; | 82 return host; |
62 } | 83 } |
63 | 84 |
64 std::string SecurityInterstitialPage::GetHTMLContents() { | 85 std::string SecurityInterstitialPage::GetHTMLContents() { |
65 base::DictionaryValue load_time_data; | 86 base::DictionaryValue load_time_data; |
66 PopulateInterstitialStrings(&load_time_data); | 87 PopulateInterstitialStrings(&load_time_data); |
67 const std::string& app_locale = g_browser_process->GetApplicationLocale(); | 88 const std::string& app_locale = g_browser_process->GetApplicationLocale(); |
68 webui::SetLoadTimeDataDefaults(app_locale, &load_time_data); | 89 webui::SetLoadTimeDataDefaults(app_locale, &load_time_data); |
69 std::string html = ResourceBundle::GetSharedInstance() | 90 std::string html = ResourceBundle::GetSharedInstance() |
70 .GetRawDataResource(IDR_SECURITY_INTERSTITIAL_HTML) | 91 .GetRawDataResource(IDR_SECURITY_INTERSTITIAL_HTML) |
71 .as_string(); | 92 .as_string(); |
72 webui::AppendWebUiCssTextDefaults(&html); | 93 webui::AppendWebUiCssTextDefaults(&html); |
73 return webui::GetI18nTemplateHtml(html, &load_time_data); | 94 return webui::GetI18nTemplateHtml(html, &load_time_data); |
74 } | 95 } |
| 96 |
| 97 void SecurityInterstitialPage::SetReportingPreference(bool report) { |
| 98 Profile* profile = |
| 99 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 100 PrefService* pref = profile->GetPrefs(); |
| 101 pref->SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled, report); |
| 102 UMA_HISTOGRAM_BOOLEAN("SB2.SetExtendedReportingEnabled", report); |
| 103 } |
| 104 |
| 105 bool SecurityInterstitialPage::IsPrefEnabled(const char* pref) { |
| 106 Profile* profile = |
| 107 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
| 108 return profile->GetPrefs()->GetBoolean(pref); |
| 109 } |
OLD | NEW |