base::Time multiplicative operator overloading
[chromium-blink-merge.git] / chrome / browser / ssl / captive_portal_blocking_page.cc
blob8c7138fcc70351ab652e8e5a189042bc6a1cc053
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_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/pref_names.h"
16 #include "components/captive_portal/captive_portal_detector.h"
17 #include "components/wifi/wifi_service.h"
18 #include "content/public/browser/web_contents.h"
19 #include "grit/generated_resources.h"
20 #include "net/base/net_util.h"
21 #include "net/base/network_change_notifier.h"
22 #include "ui/base/l10n/l10n_util.h"
24 #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
25 #error This file must be built with ENABLE_CAPTIVE_PORTAL_DETECTION flag.
26 #endif
28 namespace {
30 // Events for UMA.
31 enum CaptivePortalBlockingPageEvent {
32 SHOW_ALL,
33 OPEN_LOGIN_PAGE,
34 CAPTIVE_PORTAL_BLOCKING_PAGE_EVENT_COUNT
37 const char kOpenLoginPageCommand[] = "openLoginPage";
39 void RecordUMA(CaptivePortalBlockingPageEvent event) {
40 UMA_HISTOGRAM_ENUMERATION("interstitial.captive_portal",
41 event,
42 CAPTIVE_PORTAL_BLOCKING_PAGE_EVENT_COUNT);
45 class ConnectionInfoDelegate : public CaptivePortalBlockingPage::Delegate {
46 public:
47 ConnectionInfoDelegate() {}
48 ~ConnectionInfoDelegate() override {}
50 bool IsWifiConnection() const override {
51 // |net::NetworkChangeNotifier::GetConnectionType| isn't accurate on Linux
52 // and Windows. See https://crbug.com/160537 for details.
53 // TODO(meacer): Add heuristics to get a more accurate connection type on
54 // these platforms.
55 return net::NetworkChangeNotifier::GetConnectionType() ==
56 net::NetworkChangeNotifier::CONNECTION_WIFI;
59 std::string GetWiFiSSID() const override {
60 // On Windows and Mac, |WiFiService| provides an easy to use API to get the
61 // currently associated WiFi access point. |WiFiService| isn't available on
62 // Linux so |net::GetWifiSSID| is used instead.
63 std::string ssid;
64 #if defined(OS_WIN) || defined(OS_MACOSX)
65 scoped_ptr<wifi::WiFiService> wifi_service(wifi::WiFiService::Create());
66 wifi_service->Initialize(NULL);
67 std::string error;
68 wifi_service->GetConnectedNetworkSSID(&ssid, &error);
69 if (!error.empty())
70 return "";
71 #elif defined(OS_LINUX)
72 ssid = net::GetWifiSSID();
73 #endif
74 // TODO(meacer): Handle non UTF8 SSIDs.
75 if (!base::IsStringUTF8(ssid))
76 return "";
77 return ssid;
81 } // namespace
83 // static
84 const void* CaptivePortalBlockingPage::kTypeForTesting =
85 &CaptivePortalBlockingPage::kTypeForTesting;
87 CaptivePortalBlockingPage::CaptivePortalBlockingPage(
88 content::WebContents* web_contents,
89 const GURL& request_url,
90 const GURL& login_url,
91 const base::Callback<void(bool)>& callback)
92 : SecurityInterstitialPage(web_contents, request_url),
93 login_url_(login_url),
94 delegate_(new ConnectionInfoDelegate),
95 callback_(callback) {
96 DCHECK(login_url_.is_valid());
97 RecordUMA(SHOW_ALL);
100 CaptivePortalBlockingPage::~CaptivePortalBlockingPage() {
101 // Need to explicity deny the certificate via the callback, otherwise memory
102 // is leaked.
103 if (!callback_.is_null()) {
104 callback_.Run(false);
105 callback_.Reset();
109 const void* CaptivePortalBlockingPage::GetTypeForTesting() const {
110 return CaptivePortalBlockingPage::kTypeForTesting;
113 bool CaptivePortalBlockingPage::ShouldCreateNewNavigation() const {
114 return true;
117 void CaptivePortalBlockingPage::PopulateInterstitialStrings(
118 base::DictionaryValue* load_time_data) {
119 load_time_data->SetString("iconClass", "icon-offline");
120 load_time_data->SetString("type", "CAPTIVE_PORTAL");
121 load_time_data->SetBoolean("overridable", false);
123 // |IsWifiConnection| isn't accurate on some platforms, so always try to get
124 // the Wi-Fi SSID even if |IsWifiConnection| is false.
125 std::string wifi_ssid = delegate_.get()->GetWiFiSSID();
126 bool is_wifi_connection = !wifi_ssid.empty() ||
127 delegate_.get()->IsWifiConnection();
129 load_time_data->SetString(
130 "primaryButtonText",
131 l10n_util::GetStringUTF16(IDS_CAPTIVE_PORTAL_BUTTON_OPEN_LOGIN_PAGE));
132 load_time_data->SetString(
133 "tabTitle", l10n_util::GetStringUTF16(
134 is_wifi_connection ? IDS_CAPTIVE_PORTAL_HEADING_WIFI
135 : IDS_CAPTIVE_PORTAL_HEADING_WIRED));
136 load_time_data->SetString(
137 "heading", l10n_util::GetStringUTF16(
138 is_wifi_connection ? IDS_CAPTIVE_PORTAL_HEADING_WIFI
139 : IDS_CAPTIVE_PORTAL_HEADING_WIRED));
141 if (login_url_.spec() == captive_portal::CaptivePortalDetector::kDefaultURL) {
142 // Captive portal may intercept requests without HTTP redirects, in which
143 // case the login url would be the same as the captive portal detection url.
144 // Don't show the login url in that case.
145 if (wifi_ssid.empty()) {
146 load_time_data->SetString(
147 "primaryParagraph",
148 l10n_util::GetStringUTF16(
149 is_wifi_connection
150 ? IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIFI
151 : IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIRED));
152 } else {
153 load_time_data->SetString(
154 "primaryParagraph",
155 l10n_util::GetStringFUTF16(
156 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIFI_SSID,
157 net::EscapeForHTML(base::UTF8ToUTF16(wifi_ssid))));
159 } else {
160 // Portal redirection was done with HTTP redirects, so show the login URL.
161 // If |languages| is empty, punycode in |login_host| will always be decoded.
162 std::string languages;
163 Profile* profile = Profile::FromBrowserContext(
164 web_contents()->GetBrowserContext());
165 if (profile)
166 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
167 base::string16 login_host = net::IDNToUnicode(login_url_.host(), languages);
168 if (base::i18n::IsRTL())
169 base::i18n::WrapStringWithLTRFormatting(&login_host);
171 if (wifi_ssid.empty()) {
172 load_time_data->SetString(
173 "primaryParagraph",
174 l10n_util::GetStringFUTF16(
175 is_wifi_connection ? IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIFI
176 : IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIRED,
177 login_host));
178 } else {
179 load_time_data->SetString(
180 "primaryParagraph",
181 l10n_util::GetStringFUTF16(
182 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIFI_SSID,
183 net::EscapeForHTML(base::UTF8ToUTF16(wifi_ssid)), login_host));
187 // Fill the empty strings to avoid getting debug warnings.
188 load_time_data->SetString("openDetails", base::string16());
189 load_time_data->SetString("closeDetails", base::string16());
190 load_time_data->SetString("explanationParagraph", base::string16());
191 load_time_data->SetString("finalParagraph", base::string16());
194 void CaptivePortalBlockingPage::CommandReceived(const std::string& command) {
195 // The response has quotes around it.
196 if (command == std::string("\"") + kOpenLoginPageCommand + "\"") {
197 RecordUMA(OPEN_LOGIN_PAGE);
198 CaptivePortalTabHelper::OpenLoginTabForWebContents(web_contents(), true);