Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / ssl / captive_portal_blocking_page.cc
blob955fea0ee65b0ed4aeb7677b042a93e767cb6d16
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/wifi/wifi_service.h"
21 #include "content/public/browser/web_contents.h"
22 #include "grit/generated_resources.h"
23 #include "net/base/net_util.h"
24 #include "net/base/network_change_notifier.h"
25 #include "net/ssl/ssl_info.h"
26 #include "ui/base/l10n/l10n_util.h"
28 #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
29 #error This file must be built with ENABLE_CAPTIVE_PORTAL_DETECTION flag.
30 #endif
32 namespace {
34 // Events for UMA.
35 enum CaptivePortalBlockingPageEvent {
36 SHOW_ALL,
37 OPEN_LOGIN_PAGE,
38 CAPTIVE_PORTAL_BLOCKING_PAGE_EVENT_COUNT
41 void RecordUMA(CaptivePortalBlockingPageEvent event) {
42 UMA_HISTOGRAM_ENUMERATION("interstitial.captive_portal",
43 event,
44 CAPTIVE_PORTAL_BLOCKING_PAGE_EVENT_COUNT);
47 class ConnectionInfoDelegate : public CaptivePortalBlockingPage::Delegate {
48 public:
49 ConnectionInfoDelegate() {}
50 ~ConnectionInfoDelegate() override {}
52 bool IsWifiConnection() const override {
53 // |net::NetworkChangeNotifier::GetConnectionType| isn't accurate on Linux
54 // and Windows. See https://crbug.com/160537 for details.
55 // TODO(meacer): Add heuristics to get a more accurate connection type on
56 // these platforms.
57 return net::NetworkChangeNotifier::GetConnectionType() ==
58 net::NetworkChangeNotifier::CONNECTION_WIFI;
61 std::string GetWiFiSSID() const override {
62 // On Windows and Mac, |WiFiService| provides an easy to use API to get the
63 // currently associated WiFi access point. |WiFiService| isn't available on
64 // Linux so |net::GetWifiSSID| is used instead.
65 std::string ssid;
66 #if defined(OS_WIN) || defined(OS_MACOSX)
67 scoped_ptr<wifi::WiFiService> wifi_service(wifi::WiFiService::Create());
68 wifi_service->Initialize(NULL);
69 std::string error;
70 wifi_service->GetConnectedNetworkSSID(&ssid, &error);
71 if (!error.empty())
72 return "";
73 #elif defined(OS_LINUX)
74 ssid = net::GetWifiSSID();
75 #endif
76 // TODO(meacer): Handle non UTF8 SSIDs.
77 if (!base::IsStringUTF8(ssid))
78 return "";
79 return ssid;
83 } // namespace
85 // static
86 const void* CaptivePortalBlockingPage::kTypeForTesting =
87 &CaptivePortalBlockingPage::kTypeForTesting;
89 CaptivePortalBlockingPage::CaptivePortalBlockingPage(
90 content::WebContents* web_contents,
91 const GURL& request_url,
92 const GURL& login_url,
93 scoped_ptr<SSLCertReporter> ssl_cert_reporter,
94 const net::SSLInfo& ssl_info,
95 const base::Callback<void(bool)>& callback)
96 : SecurityInterstitialPage(web_contents, request_url),
97 login_url_(login_url),
98 delegate_(new ConnectionInfoDelegate),
99 callback_(callback) {
100 DCHECK(login_url_.is_valid());
102 if (ssl_cert_reporter) {
103 cert_report_helper_.reset(new CertReportHelper(
104 ssl_cert_reporter.Pass(), web_contents, request_url, ssl_info,
105 CertificateErrorReport::INTERSTITIAL_CAPTIVE_PORTAL, false, nullptr));
108 RecordUMA(SHOW_ALL);
111 CaptivePortalBlockingPage::~CaptivePortalBlockingPage() {
114 const void* CaptivePortalBlockingPage::GetTypeForTesting() const {
115 return CaptivePortalBlockingPage::kTypeForTesting;
118 bool CaptivePortalBlockingPage::ShouldCreateNewNavigation() const {
119 return true;
122 void CaptivePortalBlockingPage::PopulateInterstitialStrings(
123 base::DictionaryValue* load_time_data) {
124 load_time_data->SetString("iconClass", "icon-offline");
125 load_time_data->SetString("type", "CAPTIVE_PORTAL");
126 load_time_data->SetBoolean("overridable", false);
128 // |IsWifiConnection| isn't accurate on some platforms, so always try to get
129 // the Wi-Fi SSID even if |IsWifiConnection| is false.
130 std::string wifi_ssid = delegate_.get()->GetWiFiSSID();
131 bool is_wifi_connection = !wifi_ssid.empty() ||
132 delegate_.get()->IsWifiConnection();
134 load_time_data->SetString(
135 "primaryButtonText",
136 l10n_util::GetStringUTF16(IDS_CAPTIVE_PORTAL_BUTTON_OPEN_LOGIN_PAGE));
137 load_time_data->SetString(
138 "tabTitle", l10n_util::GetStringUTF16(
139 is_wifi_connection ? IDS_CAPTIVE_PORTAL_HEADING_WIFI
140 : IDS_CAPTIVE_PORTAL_HEADING_WIRED));
141 load_time_data->SetString(
142 "heading", l10n_util::GetStringUTF16(
143 is_wifi_connection ? IDS_CAPTIVE_PORTAL_HEADING_WIFI
144 : IDS_CAPTIVE_PORTAL_HEADING_WIRED));
146 if (login_url_.spec() == captive_portal::CaptivePortalDetector::kDefaultURL) {
147 // Captive portal may intercept requests without HTTP redirects, in which
148 // case the login url would be the same as the captive portal detection url.
149 // Don't show the login url in that case.
150 if (wifi_ssid.empty()) {
151 load_time_data->SetString(
152 "primaryParagraph",
153 l10n_util::GetStringUTF16(
154 is_wifi_connection
155 ? IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIFI
156 : IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIRED));
157 } else {
158 load_time_data->SetString(
159 "primaryParagraph",
160 l10n_util::GetStringFUTF16(
161 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIFI_SSID,
162 net::EscapeForHTML(base::UTF8ToUTF16(wifi_ssid))));
164 } else {
165 // Portal redirection was done with HTTP redirects, so show the login URL.
166 // If |languages| is empty, punycode in |login_host| will always be decoded.
167 std::string languages;
168 Profile* profile = Profile::FromBrowserContext(
169 web_contents()->GetBrowserContext());
170 if (profile)
171 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
172 base::string16 login_host = net::IDNToUnicode(login_url_.host(), languages);
173 if (base::i18n::IsRTL())
174 base::i18n::WrapStringWithLTRFormatting(&login_host);
176 if (wifi_ssid.empty()) {
177 load_time_data->SetString(
178 "primaryParagraph",
179 l10n_util::GetStringFUTF16(
180 is_wifi_connection ? IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIFI
181 : IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIRED,
182 login_host));
183 } else {
184 load_time_data->SetString(
185 "primaryParagraph",
186 l10n_util::GetStringFUTF16(
187 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIFI_SSID,
188 net::EscapeForHTML(base::UTF8ToUTF16(wifi_ssid)), login_host));
192 // Fill the empty strings to avoid getting debug warnings.
193 load_time_data->SetString("openDetails", base::string16());
194 load_time_data->SetString("closeDetails", base::string16());
195 load_time_data->SetString("explanationParagraph", base::string16());
196 load_time_data->SetString("finalParagraph", base::string16());
198 if (cert_report_helper_)
199 cert_report_helper_->PopulateExtendedReportingOption(load_time_data);
202 void CaptivePortalBlockingPage::CommandReceived(const std::string& command) {
203 if (command == "\"pageLoadComplete\"") {
204 // content::WaitForRenderFrameReady sends this message when the page
205 // load completes. Ignore it.
206 return;
209 int cmd = 0;
210 bool retval = base::StringToInt(command, &cmd);
211 DCHECK(retval) << command;
213 if (cmd == CMD_OPEN_LOGIN) {
214 RecordUMA(OPEN_LOGIN_PAGE);
215 CaptivePortalTabHelper::OpenLoginTabForWebContents(web_contents(), true);
219 void CaptivePortalBlockingPage::OnProceed() {
220 if (cert_report_helper_) {
221 // Finish collecting information about invalid certificates, if the
222 // user opted in to.
223 cert_report_helper_->FinishCertCollection(
224 CertificateErrorReport::USER_PROCEEDED);
228 void CaptivePortalBlockingPage::OnDontProceed() {
229 if (cert_report_helper_) {
230 // Finish collecting information about invalid certificates, if the
231 // user opted in to.
232 cert_report_helper_->FinishCertCollection(
233 CertificateErrorReport::USER_DID_NOT_PROCEED);
236 // Need to explicity deny the certificate via the callback, otherwise memory
237 // is leaked.
238 if (!callback_.is_null()) {
239 callback_.Run(false);
240 callback_.Reset();