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/ssl/ssl_info.h"
27 #include "ui/base/l10n/l10n_util.h"
29 #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
30 #error This file must be built with ENABLE_CAPTIVE_PORTAL_DETECTION flag.
36 enum CaptivePortalBlockingPageEvent
{
39 CAPTIVE_PORTAL_BLOCKING_PAGE_EVENT_COUNT
42 void RecordUMA(CaptivePortalBlockingPageEvent event
) {
43 UMA_HISTOGRAM_ENUMERATION("interstitial.captive_portal",
45 CAPTIVE_PORTAL_BLOCKING_PAGE_EVENT_COUNT
);
48 class ConnectionInfoDelegate
: public CaptivePortalBlockingPage::Delegate
{
50 ConnectionInfoDelegate() {}
51 ~ConnectionInfoDelegate() override
{}
53 bool IsWifiConnection() const override
{
54 // |net::NetworkChangeNotifier::GetConnectionType| isn't accurate on Linux
55 // and Windows. See https://crbug.com/160537 for details.
56 // TODO(meacer): Add heuristics to get a more accurate connection type on
58 return net::NetworkChangeNotifier::GetConnectionType() ==
59 net::NetworkChangeNotifier::CONNECTION_WIFI
;
62 std::string
GetWiFiSSID() const override
{
63 // On Windows and Mac, |WiFiService| provides an easy to use API to get the
64 // currently associated WiFi access point. |WiFiService| isn't available on
65 // Linux so |net::GetWifiSSID| is used instead.
67 #if defined(OS_WIN) || defined(OS_MACOSX)
68 scoped_ptr
<wifi::WiFiService
> wifi_service(wifi::WiFiService::Create());
69 wifi_service
->Initialize(NULL
);
71 wifi_service
->GetConnectedNetworkSSID(&ssid
, &error
);
74 #elif defined(OS_LINUX)
75 ssid
= net::GetWifiSSID();
77 // TODO(meacer): Handle non UTF8 SSIDs.
78 if (!base::IsStringUTF8(ssid
))
87 const void* CaptivePortalBlockingPage::kTypeForTesting
=
88 &CaptivePortalBlockingPage::kTypeForTesting
;
90 CaptivePortalBlockingPage::CaptivePortalBlockingPage(
91 content::WebContents
* web_contents
,
92 const GURL
& request_url
,
93 const GURL
& login_url
,
94 scoped_ptr
<SSLCertReporter
> ssl_cert_reporter
,
95 const net::SSLInfo
& ssl_info
,
96 const base::Callback
<void(bool)>& callback
)
97 : SecurityInterstitialPage(web_contents
, request_url
),
98 login_url_(login_url
),
99 delegate_(new ConnectionInfoDelegate
),
100 callback_(callback
) {
101 DCHECK(login_url_
.is_valid());
103 if (ssl_cert_reporter
) {
104 cert_report_helper_
.reset(new CertReportHelper(
105 ssl_cert_reporter
.Pass(), web_contents
, request_url
, ssl_info
,
106 CertificateErrorReport::INTERSTITIAL_CAPTIVE_PORTAL
, false, nullptr));
112 CaptivePortalBlockingPage::~CaptivePortalBlockingPage() {
115 const void* CaptivePortalBlockingPage::GetTypeForTesting() const {
116 return CaptivePortalBlockingPage::kTypeForTesting
;
119 bool CaptivePortalBlockingPage::ShouldCreateNewNavigation() const {
123 void CaptivePortalBlockingPage::PopulateInterstitialStrings(
124 base::DictionaryValue
* load_time_data
) {
125 load_time_data
->SetString("iconClass", "icon-offline");
126 load_time_data
->SetString("type", "CAPTIVE_PORTAL");
127 load_time_data
->SetBoolean("overridable", false);
129 // |IsWifiConnection| isn't accurate on some platforms, so always try to get
130 // the Wi-Fi SSID even if |IsWifiConnection| is false.
131 std::string wifi_ssid
= delegate_
.get()->GetWiFiSSID();
132 bool is_wifi_connection
= !wifi_ssid
.empty() ||
133 delegate_
.get()->IsWifiConnection();
135 load_time_data
->SetString(
137 l10n_util::GetStringUTF16(IDS_CAPTIVE_PORTAL_BUTTON_OPEN_LOGIN_PAGE
));
138 load_time_data
->SetString(
139 "tabTitle", l10n_util::GetStringUTF16(
140 is_wifi_connection
? IDS_CAPTIVE_PORTAL_HEADING_WIFI
141 : IDS_CAPTIVE_PORTAL_HEADING_WIRED
));
142 load_time_data
->SetString(
143 "heading", l10n_util::GetStringUTF16(
144 is_wifi_connection
? IDS_CAPTIVE_PORTAL_HEADING_WIFI
145 : IDS_CAPTIVE_PORTAL_HEADING_WIRED
));
147 if (login_url_
.spec() == captive_portal::CaptivePortalDetector::kDefaultURL
) {
148 // Captive portal may intercept requests without HTTP redirects, in which
149 // case the login url would be the same as the captive portal detection url.
150 // Don't show the login url in that case.
151 if (wifi_ssid
.empty()) {
152 load_time_data
->SetString(
154 l10n_util::GetStringUTF16(
156 ? IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIFI
157 : IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIRED
));
159 load_time_data
->SetString(
161 l10n_util::GetStringFUTF16(
162 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIFI_SSID
,
163 net::EscapeForHTML(base::UTF8ToUTF16(wifi_ssid
))));
166 // Portal redirection was done with HTTP redirects, so show the login URL.
167 // If |languages| is empty, punycode in |login_host| will always be decoded.
168 std::string languages
;
169 Profile
* profile
= Profile::FromBrowserContext(
170 web_contents()->GetBrowserContext());
172 languages
= profile
->GetPrefs()->GetString(prefs::kAcceptLanguages
);
173 base::string16 login_host
=
174 url_formatter::IDNToUnicode(login_url_
.host(), languages
);
175 if (base::i18n::IsRTL())
176 base::i18n::WrapStringWithLTRFormatting(&login_host
);
178 if (wifi_ssid
.empty()) {
179 load_time_data
->SetString(
181 l10n_util::GetStringFUTF16(
182 is_wifi_connection
? IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIFI
183 : IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIRED
,
186 load_time_data
->SetString(
188 l10n_util::GetStringFUTF16(
189 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIFI_SSID
,
190 net::EscapeForHTML(base::UTF8ToUTF16(wifi_ssid
)), login_host
));
194 // Fill the empty strings to avoid getting debug warnings.
195 load_time_data
->SetString("openDetails", base::string16());
196 load_time_data
->SetString("closeDetails", base::string16());
197 load_time_data
->SetString("explanationParagraph", base::string16());
198 load_time_data
->SetString("finalParagraph", base::string16());
200 if (cert_report_helper_
)
201 cert_report_helper_
->PopulateExtendedReportingOption(load_time_data
);
204 void CaptivePortalBlockingPage::CommandReceived(const std::string
& command
) {
205 if (command
== "\"pageLoadComplete\"") {
206 // content::WaitForRenderFrameReady sends this message when the page
207 // load completes. Ignore it.
212 bool retval
= base::StringToInt(command
, &cmd
);
213 DCHECK(retval
) << command
;
215 if (cmd
== CMD_OPEN_LOGIN
) {
216 RecordUMA(OPEN_LOGIN_PAGE
);
217 CaptivePortalTabHelper::OpenLoginTabForWebContents(web_contents(), true);
221 void CaptivePortalBlockingPage::OnProceed() {
222 if (cert_report_helper_
) {
223 // Finish collecting information about invalid certificates, if the
225 cert_report_helper_
->FinishCertCollection(
226 CertificateErrorReport::USER_PROCEEDED
);
230 void CaptivePortalBlockingPage::OnDontProceed() {
231 if (cert_report_helper_
) {
232 // Finish collecting information about invalid certificates, if the
234 cert_report_helper_
->FinishCertCollection(
235 CertificateErrorReport::USER_DID_NOT_PROCEED
);
238 // Need to explicity deny the certificate via the callback, otherwise memory
240 if (!callback_
.is_null()) {
241 callback_
.Run(false);