| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/ssl/captive_portal_blocking_page.h" | 5 #include "chrome/browser/ssl/captive_portal_blocking_page.h" |
| 6 | 6 |
| 7 #include "base/i18n/rtl.h" | 7 #include "base/i18n/rtl.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h" | 13 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 15 #include "components/captive_portal/captive_portal_detector.h" | 16 #include "components/captive_portal/captive_portal_detector.h" |
| 17 #include "components/wifi/wifi_service.h" |
| 16 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 17 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
| 18 #include "net/base/net_util.h" | 20 #include "net/base/net_util.h" |
| 19 #include "net/base/network_change_notifier.h" | 21 #include "net/base/network_change_notifier.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 21 | 23 |
| 22 #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION) | 24 #if !defined(ENABLE_CAPTIVE_PORTAL_DETECTION) |
| 23 #error This file must be built with ENABLE_CAPTIVE_PORTAL_DETECTION flag. | 25 #error This file must be built with ENABLE_CAPTIVE_PORTAL_DETECTION flag. |
| 24 #endif | 26 #endif |
| 25 | 27 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 40 | 42 |
| 41 bool IsWifiConnection() { | 43 bool IsWifiConnection() { |
| 42 // |net::NetworkChangeNotifier::GetConnectionType| isn't accurate on Linux and | 44 // |net::NetworkChangeNotifier::GetConnectionType| isn't accurate on Linux and |
| 43 // Windows. See https://crbug.com/160537 for details. | 45 // Windows. See https://crbug.com/160537 for details. |
| 44 // TODO(meacer): Add heuristics to get a more accurate connection type on | 46 // TODO(meacer): Add heuristics to get a more accurate connection type on |
| 45 // these platforms. | 47 // these platforms. |
| 46 return net::NetworkChangeNotifier::GetConnectionType() == | 48 return net::NetworkChangeNotifier::GetConnectionType() == |
| 47 net::NetworkChangeNotifier::CONNECTION_WIFI; | 49 net::NetworkChangeNotifier::CONNECTION_WIFI; |
| 48 } | 50 } |
| 49 | 51 |
| 52 std::string GetWiFiName() { |
| 53 std::string ssid; |
| 54 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 55 scoped_ptr<wifi::WiFiService> wifi_service(wifi::WiFiService::Create()); |
| 56 wifi_service->Initialize(NULL); |
| 57 std::string error; |
| 58 wifi_service->GetConnectedNetworkSSID(&ssid, &error); |
| 59 if (!error.empty()) |
| 60 return ""; |
| 61 #endif |
| 62 // TODO(meacer): Handle non UTF8 SSIDs. |
| 63 if (!base::IsStringUTF8(ssid)) |
| 64 return ""; |
| 65 return ssid; |
| 66 } |
| 67 |
| 50 const char kOpenLoginPageCommand[] = "openLoginPage"; | 68 const char kOpenLoginPageCommand[] = "openLoginPage"; |
| 51 | 69 |
| 52 } // namespace | 70 } // namespace |
| 53 | 71 |
| 54 // static | 72 // static |
| 55 const void* CaptivePortalBlockingPage::kTypeForTesting = | 73 const void* CaptivePortalBlockingPage::kTypeForTesting = |
| 56 &CaptivePortalBlockingPage::kTypeForTesting; | 74 &CaptivePortalBlockingPage::kTypeForTesting; |
| 57 | 75 |
| 58 CaptivePortalBlockingPage::CaptivePortalBlockingPage( | 76 CaptivePortalBlockingPage::CaptivePortalBlockingPage( |
| 59 content::WebContents* web_contents, | 77 content::WebContents* web_contents, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 load_time_data->SetString("heading", | 120 load_time_data->SetString("heading", |
| 103 l10n_util::GetStringUTF16( | 121 l10n_util::GetStringUTF16( |
| 104 is_wifi_connection_ ? | 122 is_wifi_connection_ ? |
| 105 IDS_CAPTIVE_PORTAL_HEADING_WIFI : | 123 IDS_CAPTIVE_PORTAL_HEADING_WIFI : |
| 106 IDS_CAPTIVE_PORTAL_HEADING_WIRED)); | 124 IDS_CAPTIVE_PORTAL_HEADING_WIRED)); |
| 107 | 125 |
| 108 if (login_url_.spec() == captive_portal::CaptivePortalDetector::kDefaultURL) { | 126 if (login_url_.spec() == captive_portal::CaptivePortalDetector::kDefaultURL) { |
| 109 // Captive portal may intercept requests without HTTP redirects, in which | 127 // Captive portal may intercept requests without HTTP redirects, in which |
| 110 // case the login url would be the same as the captive portal detection url. | 128 // case the login url would be the same as the captive portal detection url. |
| 111 // Don't show the login url in that case. | 129 // Don't show the login url in that case. |
| 112 load_time_data->SetString( | 130 if (is_wifi_connection_) { |
| 113 "primaryParagraph", | 131 if (wifi_ssid_.empty()) |
| 114 l10n_util::GetStringUTF16( | 132 wifi_ssid_ = GetWiFiName(); |
| 115 is_wifi_connection_ ? | 133 if (wifi_ssid_.empty()) { |
| 116 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIFI : | 134 load_time_data->SetString( |
| 117 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIRED)); | 135 "primaryParagraph", |
| 136 l10n_util::GetStringUTF16( |
| 137 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIFI)); |
| 138 } else { |
| 139 load_time_data->SetString( |
| 140 "primaryParagraph", |
| 141 l10n_util::GetStringFUTF16( |
| 142 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIFI_SSID, |
| 143 net::EscapeForHTML(base::UTF8ToUTF16(wifi_ssid_)))); |
| 144 } |
| 145 } else { |
| 146 // Non-WiFi connection: |
| 147 load_time_data->SetString( |
| 148 "primaryParagraph", |
| 149 l10n_util::GetStringUTF16( |
| 150 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_NO_LOGIN_URL_WIRED)); |
| 151 } |
| 118 } else { | 152 } else { |
| 153 // Portal redirection was done with HTTP redirects, show the login URL. |
| 119 std::string languages; | 154 std::string languages; |
| 120 Profile* profile = Profile::FromBrowserContext( | 155 Profile* profile = Profile::FromBrowserContext( |
| 121 web_contents()->GetBrowserContext()); | 156 web_contents()->GetBrowserContext()); |
| 122 if (profile) | 157 if (profile) |
| 123 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); | 158 languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 124 | 159 |
| 125 base::string16 login_host = net::IDNToUnicode(login_url_.host(), languages); | 160 base::string16 login_host = net::IDNToUnicode(login_url_.host(), languages); |
| 126 if (base::i18n::IsRTL()) | 161 if (base::i18n::IsRTL()) |
| 127 base::i18n::WrapStringWithLTRFormatting(&login_host); | 162 base::i18n::WrapStringWithLTRFormatting(&login_host); |
| 128 load_time_data->SetString( | 163 |
| 129 "primaryParagraph", | 164 if (is_wifi_connection_) { |
| 130 l10n_util::GetStringFUTF16( | 165 if (wifi_ssid_.empty()) |
| 131 is_wifi_connection_ ? | 166 wifi_ssid_ = GetWiFiName(); |
| 132 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIFI : | 167 if (wifi_ssid_.empty()) { |
| 133 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIRED, | 168 load_time_data->SetString( |
| 134 login_host)); | 169 "primaryParagraph", |
| 170 l10n_util::GetStringFUTF16( |
| 171 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIFI, |
| 172 login_host)); |
| 173 } else { |
| 174 load_time_data->SetString( |
| 175 "primaryParagraph", |
| 176 l10n_util::GetStringFUTF16( |
| 177 IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIFI_SSID, |
| 178 net::EscapeForHTML(base::UTF8ToUTF16(wifi_ssid_)), |
| 179 login_host)); |
| 180 } |
| 181 } else { |
| 182 // Non-WiFi connection: |
| 183 load_time_data->SetString( |
| 184 "primaryParagraph", |
| 185 l10n_util::GetStringFUTF16(IDS_CAPTIVE_PORTAL_PRIMARY_PARAGRAPH_WIRED, |
| 186 login_host)); |
| 187 } |
| 135 } | 188 } |
| 136 | 189 |
| 137 // Fill the empty strings to avoid getting debug warnings. | 190 // Fill the empty strings to avoid getting debug warnings. |
| 138 load_time_data->SetString("openDetails", base::string16()); | 191 load_time_data->SetString("openDetails", base::string16()); |
| 139 load_time_data->SetString("closeDetails", base::string16()); | 192 load_time_data->SetString("closeDetails", base::string16()); |
| 140 load_time_data->SetString("explanationParagraph", base::string16()); | 193 load_time_data->SetString("explanationParagraph", base::string16()); |
| 141 load_time_data->SetString("finalParagraph", base::string16()); | 194 load_time_data->SetString("finalParagraph", base::string16()); |
| 142 } | 195 } |
| 143 | 196 |
| 144 void CaptivePortalBlockingPage::CommandReceived(const std::string& command) { | 197 void CaptivePortalBlockingPage::CommandReceived(const std::string& command) { |
| 145 // The response has quotes around it. | 198 // The response has quotes around it. |
| 146 if (command == std::string("\"") + kOpenLoginPageCommand + "\"") { | 199 if (command == std::string("\"") + kOpenLoginPageCommand + "\"") { |
| 147 RecordUMA(OPEN_LOGIN_PAGE); | 200 RecordUMA(OPEN_LOGIN_PAGE); |
| 148 CaptivePortalTabHelper::OpenLoginTabForWebContents(web_contents(), true); | 201 CaptivePortalTabHelper::OpenLoginTabForWebContents(web_contents(), true); |
| 149 } | 202 } |
| 150 } | 203 } |
| OLD | NEW |