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