Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / components / metrics / net / network_metrics_provider.cc
blob954df23a4ba241ab45a8fe0c8ea6bb1bdfef4f4d
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 "components/metrics/net/network_metrics_provider.h"
7 #include <string>
8 #include <vector>
10 #include "base/compiler_specific.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/string_util.h"
14 #include "base/task_runner_util.h"
16 #if defined(OS_CHROMEOS)
17 #include "components/metrics/net/wifi_access_point_info_provider_chromeos.h"
18 #endif // OS_CHROMEOS
20 namespace metrics {
22 NetworkMetricsProvider::NetworkMetricsProvider(
23 base::TaskRunner* io_task_runner)
24 : io_task_runner_(io_task_runner),
25 connection_type_is_ambiguous_(false),
26 wifi_phy_layer_protocol_is_ambiguous_(false),
27 wifi_phy_layer_protocol_(net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN),
28 weak_ptr_factory_(this) {
29 net::NetworkChangeNotifier::AddConnectionTypeObserver(this);
30 connection_type_ = net::NetworkChangeNotifier::GetConnectionType();
31 ProbeWifiPHYLayerProtocol();
34 NetworkMetricsProvider::~NetworkMetricsProvider() {
35 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
38 void NetworkMetricsProvider::OnDidCreateMetricsLog() {
39 net::NetworkChangeNotifier::LogOperatorCodeHistogram(
40 net::NetworkChangeNotifier::GetConnectionType());
43 void NetworkMetricsProvider::ProvideSystemProfileMetrics(
44 SystemProfileProto* system_profile) {
45 SystemProfileProto::Network* network = system_profile->mutable_network();
46 network->set_connection_type_is_ambiguous(connection_type_is_ambiguous_);
47 network->set_connection_type(GetConnectionType());
48 network->set_wifi_phy_layer_protocol_is_ambiguous(
49 wifi_phy_layer_protocol_is_ambiguous_);
50 network->set_wifi_phy_layer_protocol(GetWifiPHYLayerProtocol());
52 // Update the connection type. Note that this is necessary to set the network
53 // type to "none" if there is no network connection for an entire UMA logging
54 // window, since OnConnectionTypeChanged() ignores transitions to the "none"
55 // state.
56 connection_type_ = net::NetworkChangeNotifier::GetConnectionType();
57 // Reset the "ambiguous" flags, since a new metrics log session has started.
58 connection_type_is_ambiguous_ = false;
59 wifi_phy_layer_protocol_is_ambiguous_ = false;
61 if (!wifi_access_point_info_provider_.get()) {
62 #if defined(OS_CHROMEOS)
63 wifi_access_point_info_provider_.reset(
64 new WifiAccessPointInfoProviderChromeos());
65 #else
66 wifi_access_point_info_provider_.reset(
67 new WifiAccessPointInfoProvider());
68 #endif // OS_CHROMEOS
71 // Connected wifi access point information.
72 WifiAccessPointInfoProvider::WifiAccessPointInfo info;
73 if (wifi_access_point_info_provider_->GetInfo(&info))
74 WriteWifiAccessPointProto(info, network);
77 void NetworkMetricsProvider::OnConnectionTypeChanged(
78 net::NetworkChangeNotifier::ConnectionType type) {
79 // To avoid reporting an ambiguous connection type for users on flaky
80 // connections, ignore transitions to the "none" state. Note that the
81 // connection type is refreshed in ProvideSystemProfileMetrics() each time a
82 // new UMA logging window begins, so users who genuinely transition to offline
83 // mode for an extended duration will still be at least partially represented
84 // in the metrics logs.
85 if (type == net::NetworkChangeNotifier::CONNECTION_NONE)
86 return;
88 if (type != connection_type_ &&
89 connection_type_ != net::NetworkChangeNotifier::CONNECTION_NONE) {
90 connection_type_is_ambiguous_ = true;
92 connection_type_ = type;
94 ProbeWifiPHYLayerProtocol();
97 SystemProfileProto::Network::ConnectionType
98 NetworkMetricsProvider::GetConnectionType() const {
99 switch (connection_type_) {
100 case net::NetworkChangeNotifier::CONNECTION_NONE:
101 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN:
102 return SystemProfileProto::Network::CONNECTION_UNKNOWN;
103 case net::NetworkChangeNotifier::CONNECTION_ETHERNET:
104 return SystemProfileProto::Network::CONNECTION_ETHERNET;
105 case net::NetworkChangeNotifier::CONNECTION_WIFI:
106 return SystemProfileProto::Network::CONNECTION_WIFI;
107 case net::NetworkChangeNotifier::CONNECTION_2G:
108 return SystemProfileProto::Network::CONNECTION_2G;
109 case net::NetworkChangeNotifier::CONNECTION_3G:
110 return SystemProfileProto::Network::CONNECTION_3G;
111 case net::NetworkChangeNotifier::CONNECTION_4G:
112 return SystemProfileProto::Network::CONNECTION_4G;
113 case net::NetworkChangeNotifier::CONNECTION_BLUETOOTH:
114 return SystemProfileProto::Network::CONNECTION_BLUETOOTH;
116 NOTREACHED();
117 return SystemProfileProto::Network::CONNECTION_UNKNOWN;
120 SystemProfileProto::Network::WifiPHYLayerProtocol
121 NetworkMetricsProvider::GetWifiPHYLayerProtocol() const {
122 switch (wifi_phy_layer_protocol_) {
123 case net::WIFI_PHY_LAYER_PROTOCOL_NONE:
124 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_NONE;
125 case net::WIFI_PHY_LAYER_PROTOCOL_ANCIENT:
126 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_ANCIENT;
127 case net::WIFI_PHY_LAYER_PROTOCOL_A:
128 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_A;
129 case net::WIFI_PHY_LAYER_PROTOCOL_B:
130 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_B;
131 case net::WIFI_PHY_LAYER_PROTOCOL_G:
132 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_G;
133 case net::WIFI_PHY_LAYER_PROTOCOL_N:
134 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_N;
135 case net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN:
136 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
138 NOTREACHED();
139 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN;
142 void NetworkMetricsProvider::ProbeWifiPHYLayerProtocol() {
143 PostTaskAndReplyWithResult(
144 io_task_runner_,
145 FROM_HERE,
146 base::Bind(&net::GetWifiPHYLayerProtocol),
147 base::Bind(&NetworkMetricsProvider::OnWifiPHYLayerProtocolResult,
148 weak_ptr_factory_.GetWeakPtr()));
151 void NetworkMetricsProvider::OnWifiPHYLayerProtocolResult(
152 net::WifiPHYLayerProtocol mode) {
153 if (wifi_phy_layer_protocol_ != net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN &&
154 mode != wifi_phy_layer_protocol_) {
155 wifi_phy_layer_protocol_is_ambiguous_ = true;
157 wifi_phy_layer_protocol_ = mode;
160 void NetworkMetricsProvider::WriteWifiAccessPointProto(
161 const WifiAccessPointInfoProvider::WifiAccessPointInfo& info,
162 SystemProfileProto::Network* network_proto) {
163 SystemProfileProto::Network::WifiAccessPoint* access_point_info =
164 network_proto->mutable_access_point_info();
165 SystemProfileProto::Network::WifiAccessPoint::SecurityMode security =
166 SystemProfileProto::Network::WifiAccessPoint::SECURITY_UNKNOWN;
167 switch (info.security) {
168 case WifiAccessPointInfoProvider::WIFI_SECURITY_NONE:
169 security = SystemProfileProto::Network::WifiAccessPoint::SECURITY_NONE;
170 break;
171 case WifiAccessPointInfoProvider::WIFI_SECURITY_WPA:
172 security = SystemProfileProto::Network::WifiAccessPoint::SECURITY_WPA;
173 break;
174 case WifiAccessPointInfoProvider::WIFI_SECURITY_WEP:
175 security = SystemProfileProto::Network::WifiAccessPoint::SECURITY_WEP;
176 break;
177 case WifiAccessPointInfoProvider::WIFI_SECURITY_RSN:
178 security = SystemProfileProto::Network::WifiAccessPoint::SECURITY_RSN;
179 break;
180 case WifiAccessPointInfoProvider::WIFI_SECURITY_802_1X:
181 security = SystemProfileProto::Network::WifiAccessPoint::SECURITY_802_1X;
182 break;
183 case WifiAccessPointInfoProvider::WIFI_SECURITY_PSK:
184 security = SystemProfileProto::Network::WifiAccessPoint::SECURITY_PSK;
185 break;
186 case WifiAccessPointInfoProvider::WIFI_SECURITY_UNKNOWN:
187 security = SystemProfileProto::Network::WifiAccessPoint::SECURITY_UNKNOWN;
188 break;
190 access_point_info->set_security_mode(security);
192 // |bssid| is xx:xx:xx:xx:xx:xx, extract the first three components and
193 // pack into a uint32.
194 std::string bssid = info.bssid;
195 if (bssid.size() == 17 && bssid[2] == ':' && bssid[5] == ':' &&
196 bssid[8] == ':' && bssid[11] == ':' && bssid[14] == ':') {
197 std::string vendor_prefix_str;
198 uint32 vendor_prefix;
200 base::RemoveChars(bssid.substr(0, 9), ":", &vendor_prefix_str);
201 DCHECK_EQ(6U, vendor_prefix_str.size());
202 if (base::HexStringToUInt(vendor_prefix_str, &vendor_prefix))
203 access_point_info->set_vendor_prefix(vendor_prefix);
204 else
205 NOTREACHED();
208 // Return if vendor information is not provided.
209 if (info.model_number.empty() && info.model_name.empty() &&
210 info.device_name.empty() && info.oui_list.empty())
211 return;
213 SystemProfileProto::Network::WifiAccessPoint::VendorInformation* vendor =
214 access_point_info->mutable_vendor_info();
215 if (!info.model_number.empty())
216 vendor->set_model_number(info.model_number);
217 if (!info.model_name.empty())
218 vendor->set_model_name(info.model_name);
219 if (!info.device_name.empty())
220 vendor->set_device_name(info.device_name);
222 // Return if OUI list is not provided.
223 if (info.oui_list.empty())
224 return;
226 // Parse OUI list.
227 std::vector<std::string> oui_list;
228 base::SplitString(info.oui_list, ' ', &oui_list);
229 for (std::vector<std::string>::const_iterator it = oui_list.begin();
230 it != oui_list.end();
231 ++it) {
232 uint32 oui;
233 if (base::HexStringToUInt(*it, &oui))
234 vendor->add_element_identifier(oui);
235 else
236 NOTREACHED();
240 } // namespace metrics