Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / ssl / captive_portal_blocking_page.cc
blob6c460e123ba1ed963a3f3af6520e4fbc888a5811
1 // Copyright 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/ssl/captive_portal_blocking_page.h"
7 #include "base/i18n/rtl.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/ssl/cert_report_helper.h"
17 #include "chrome/browser/ssl/ssl_cert_reporter.h"
18 #include "chrome/common/pref_names.h"
19 #include "components/captive_portal/captive_portal_detector.h"
20 #include "components/url_formatter/url_formatter.h"
21 #include "components/wifi/wifi_service.h"
22 #include "content/public/browser/web_contents.h"
23 #include "grit/generated_resources.h"
24 #include "net/base/net_util.h"
25 #include "net/base/network_change_notifier.h"
26 #include "net/base/network_interfaces.h"
27 #include "net/ssl/ssl_info.h"
28 #include "ui/base/l10n/l10n_util.h"
30 namespace {
32 // Events for UMA.
33 enum CaptivePortalBlockingPageEvent {
34 SHOW_ALL,
35 OPEN_LOGIN_PAGE,
36 CAPTIVE_PORTAL_BLOCKING_PAGE_EVENT_COUNT
39 void RecordUMA(CaptivePortalBlockingPageEvent event) {
40 UMA_HISTOGRAM_ENUMERATION("interstitial.captive_portal", event,
41 CAPTIVE_PORTAL_BLOCKING_PAGE_EVENT_COUNT);
44 } // namespace
46 // static
47 const void* const CaptivePortalBlockingPage::kTypeForTesting =
48 &CaptivePortalBlockingPage::kTypeForTesting;
50 CaptivePortalBlockingPage::CaptivePortalBlockingPage(
51 content::WebContents* web_contents,
52 const GURL& request_url,
53 const GURL& login_url,
54 scoped_ptr<SSLCertReporter> ssl_cert_reporter,
55 const net::SSLInfo& ssl_info,
56 const base::Callback<void(bool)>& callback)
57 : SecurityInterstitialPage(web_contents, request_url),
58 login_url_(login_url),
59 callback_(callback) {
60 DCHECK(login_url_.is_valid());
62 if (ssl_cert_reporter) {
63 cert_report_helper_.reset(new CertReportHelper(
64 ssl_cert_reporter.Pass(), web_contents, request_url, ssl_info,
65 CertificateErrorReport::INTERSTITIAL_CAPTIVE_PORTAL, false, nullptr));
68 RecordUMA(SHOW_ALL);
71 CaptivePortalBlockingPage::~CaptivePortalBlockingPage() {
74 const void* CaptivePortalBlockingPage::GetTypeForTesting() const {
75 return CaptivePortalBlockingPage::kTypeForTesting;
78 bool CaptivePortalBlockingPage::IsWifiConnection() const {
79 // |net::NetworkChangeNotifier::GetConnectionType| isn't accurate on Linux
80 // and Windows. See https://crbug.com/160537 for details.
81 // TODO(meacer): Add heuristics to get a more accurate connection type on
82 // these platforms.
83 return net::NetworkChangeNotifier::GetConnectionType() ==
84 net::NetworkChangeNotifier::CONNECTION_WIFI;
87 std::string CaptivePortalBlockingPage::GetWiFiSSID() const {
88 // On Windows and Mac, |WiFiService| provides an easy to use API to get the
89 // currently associated WiFi access point. |WiFiService| isn't available on
90 // Linux so |net::GetWifiSSID| is used instead.
91 std::string ssid;
92 #if defined(OS_WIN) || defined(OS_MACOSX)
93 scoped_ptr<wifi::WiFiService> wifi_service(wifi::WiFiService::Create());
94 wifi_service->Initialize(nullptr);
95 std::string error;
96 wifi_service->GetConnectedNetworkSSID(&ssid, &error);
97 if (!error.empty())
98 return std::string();
99 #elif defined(OS_LINUX)
100 ssid = net::GetWifiSSID();
101 #endif
102 // TODO(meacer): Handle non UTF8 SSIDs.
103 if (!base::IsStringUTF8(ssid))
104 return std::string();
105 return ssid;
108 bool CaptivePortalBlockingPage::ShouldCreateNewNavigation() const {
109 // Captive portal interstitials always create new navigation entries, as
110 // opposed to SafeBrowsing subresource interstitials which just block access
111 // to the current page and don't create a new entry.
112 return true;
115 void CaptivePortalBlockingPage::PopulateInterstitialStrings(
116 base::DictionaryValue* load_time_data) {
117 load_time_data->SetString("iconClass", "icon-offline");
118 load_time_data->SetString("type", "CAPTIVE_PORTAL");
119 load_time_data->SetBoolean("overridable", false);
121 // |IsWifiConnection| isn't accurate on some platforms, so always try to get
122 // the Wi-Fi SSID even if |IsWifiConnection| is false.
123 std::string wifi_ssid = GetWiFiSSID();
124 bool is_wifi = !wifi_ssid.empty() || IsWifiConnection();
126 load_time_data->SetString(
127 "primaryButtonText",
128 l10n_util::GetStringUTF16(IDS_CAPTIVE_PORTAL_BUTTON_OPEN_LOGIN_PAGE));
130 base::string16 tab_title =
131 l10n_util::GetStringUTF16(is_wifi ? IDS_CAPTIVE_PORTAL_HEADING_WIFI
132 : IDS_CAPTIVE_PORTAL_HEADING_WIRED);
133 load_time_data->SetString("tabTitle", tab_title);
134 load_time_data->SetString("heading", tab_title);
136 base::string16 paragraph;
137 if (login_url_.spec() == captive_portal::CaptivePortalDetector::kDefaultURL) {
138 // Captive portal may intercept requests without HTTP redirects, in which
139 // case the login url would be the same as the captive portal detection url.
140 // Don't show the login url in that case.
141 if (wifi_ssid.empty()) {
142 paragraph = l10n_util::GetStringUTF16(
143 is_wifi ? IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIFI
144 : IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIRED);
145 } else {
146 paragraph = l10n_util::GetStringFUTF16(
147 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIFI_SSID,
148 net::EscapeForHTML(base::UTF8ToUTF16(wifi_ssid)));
150 } else {
151 // Portal redirection was done with HTTP redirects, so show the login URL.
152 // If |languages| is empty, punycode in |login_host| will always be decoded.
153 std::string languages;
154 Profile* profile =
155 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
156 if (profile)
157 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
158 base::string16 login_host =
159 url_formatter::IDNToUnicode(login_url_.host(), languages);
160 if (base::i18n::IsRTL())
161 base::i18n::WrapStringWithLTRFormatting(&login_host);
163 if (wifi_ssid.empty()) {
164 paragraph = l10n_util::GetStringFUTF16(
165 is_wifi ? IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIFI
166 : IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIRED,
167 login_host);
168 } else {
169 paragraph = l10n_util::GetStringFUTF16(
170 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIFI_SSID,
171 net::EscapeForHTML(base::UTF8ToUTF16(wifi_ssid)), login_host);
174 load_time_data->SetString("primaryParagraph", paragraph);
175 // Explicitly specify other expected fields to empty.
176 load_time_data->SetString("openDetails", base::string16());
177 load_time_data->SetString("closeDetails", base::string16());
178 load_time_data->SetString("explanationParagraph", base::string16());
179 load_time_data->SetString("finalParagraph", base::string16());
181 if (cert_report_helper_)
182 cert_report_helper_->PopulateExtendedReportingOption(load_time_data);
185 void CaptivePortalBlockingPage::CommandReceived(const std::string& command) {
186 if (command == "\"pageLoadComplete\"") {
187 // content::WaitForRenderFrameReady sends this message when the page
188 // load completes. Ignore it.
189 return;
191 int command_num = 0;
192 bool command_is_num = base::StringToInt(command, &command_num);
193 DCHECK(command_is_num) << command;
194 // Any command other than "open the login page" is ignored.
195 if (command_num == CMD_OPEN_LOGIN) {
196 RecordUMA(OPEN_LOGIN_PAGE);
197 CaptivePortalTabHelper::OpenLoginTabForWebContents(web_contents(), true);
201 void CaptivePortalBlockingPage::OnProceed() {
202 if (cert_report_helper_) {
203 // Finish collecting information about invalid certificates, if the
204 // user opted in to.
205 cert_report_helper_->FinishCertCollection(
206 CertificateErrorReport::USER_PROCEEDED);
210 void CaptivePortalBlockingPage::OnDontProceed() {
211 if (cert_report_helper_) {
212 // Finish collecting information about invalid certificates, if the
213 // user opted in to.
214 cert_report_helper_->FinishCertCollection(
215 CertificateErrorReport::USER_DID_NOT_PROCEED);
218 // Need to explicity deny the certificate via the callback, otherwise memory
219 // is leaked.
220 if (!callback_.is_null()) {
221 callback_.Run(false);
222 callback_.Reset();