OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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. |
| 4 |
| 5 #include "chrome/browser/metrics/metrics_network_observer.h" |
| 6 |
| 7 #include "base/compiler_specific.h" |
| 8 #include "base/threading/sequenced_worker_pool.h" |
| 9 #include "content/public/browser/browser_thread.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 void ReadWifiPHYLayerProtocol(net::WifiPHYLayerProtocol* phy_layer_protocol) { |
| 14 *phy_layer_protocol = net::GetWifiPHYLayerProtocol(); |
| 15 } |
| 16 |
| 17 } // namespace |
| 18 |
| 19 MetricsNetworkObserver::MetricsNetworkObserver() |
| 20 : weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 21 connection_type_is_ambiguous_(false), |
| 22 wifi_phy_layer_protocol_is_ambiguous_(false), |
| 23 wifi_phy_layer_protocol_(net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN) { |
| 24 net::NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 25 connection_type_ = net::NetworkChangeNotifier::GetConnectionType(); |
| 26 ProbeWifiPHYLayerProtocol(); |
| 27 } |
| 28 |
| 29 MetricsNetworkObserver::~MetricsNetworkObserver() { |
| 30 net::NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 31 } |
| 32 |
| 33 void MetricsNetworkObserver::Reset() { |
| 34 connection_type_is_ambiguous_ = false; |
| 35 connection_type_ = net::NetworkChangeNotifier::GetConnectionType(); |
| 36 wifi_phy_layer_protocol_is_ambiguous_ = false; |
| 37 } |
| 38 |
| 39 void MetricsNetworkObserver::OnConnectionTypeChanged( |
| 40 net::NetworkChangeNotifier::ConnectionType type) { |
| 41 if (type == net::NetworkChangeNotifier::CONNECTION_NONE) |
| 42 return; |
| 43 if (type != connection_type_ && |
| 44 connection_type_ != net::NetworkChangeNotifier::CONNECTION_NONE) { |
| 45 connection_type_is_ambiguous_ = true; |
| 46 } |
| 47 connection_type_ = type; |
| 48 |
| 49 ProbeWifiPHYLayerProtocol(); |
| 50 } |
| 51 |
| 52 SystemProfileProto::Network::ConnectionType |
| 53 MetricsNetworkObserver::connection_type() const { |
| 54 switch (connection_type_) { |
| 55 case net::NetworkChangeNotifier::CONNECTION_NONE: |
| 56 case net::NetworkChangeNotifier::CONNECTION_UNKNOWN: |
| 57 return SystemProfileProto::Network::CONNECTION_UNKNOWN; |
| 58 case net::NetworkChangeNotifier::CONNECTION_ETHERNET: |
| 59 return SystemProfileProto::Network::CONNECTION_ETHERNET; |
| 60 case net::NetworkChangeNotifier::CONNECTION_WIFI: |
| 61 return SystemProfileProto::Network::CONNECTION_WIFI; |
| 62 case net::NetworkChangeNotifier::CONNECTION_2G: |
| 63 return SystemProfileProto::Network::CONNECTION_2G; |
| 64 case net::NetworkChangeNotifier::CONNECTION_3G: |
| 65 return SystemProfileProto::Network::CONNECTION_3G; |
| 66 case net::NetworkChangeNotifier::CONNECTION_4G: |
| 67 return SystemProfileProto::Network::CONNECTION_4G; |
| 68 } |
| 69 NOTREACHED(); |
| 70 return SystemProfileProto::Network::CONNECTION_UNKNOWN; |
| 71 } |
| 72 |
| 73 SystemProfileProto::Network::WifiPHYLayerProtocol |
| 74 MetricsNetworkObserver::wifi_phy_layer_protocol() const { |
| 75 switch (wifi_phy_layer_protocol_) { |
| 76 case net::WIFI_PHY_LAYER_PROTOCOL_NONE: |
| 77 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_NONE; |
| 78 case net::WIFI_PHY_LAYER_PROTOCOL_ANCIENT: |
| 79 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_ANCIENT; |
| 80 case net::WIFI_PHY_LAYER_PROTOCOL_A: |
| 81 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_A; |
| 82 case net::WIFI_PHY_LAYER_PROTOCOL_B: |
| 83 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_B; |
| 84 case net::WIFI_PHY_LAYER_PROTOCOL_G: |
| 85 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_G; |
| 86 case net::WIFI_PHY_LAYER_PROTOCOL_N: |
| 87 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_N; |
| 88 case net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN: |
| 89 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; |
| 90 } |
| 91 NOTREACHED(); |
| 92 return SystemProfileProto::Network::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN; |
| 93 } |
| 94 |
| 95 void MetricsNetworkObserver::ProbeWifiPHYLayerProtocol() { |
| 96 net::WifiPHYLayerProtocol* phy_layer_protocol = new net::WifiPHYLayerProtocol; |
| 97 content::BrowserThread::GetBlockingPool()->PostTaskAndReply( |
| 98 FROM_HERE, |
| 99 base::Bind(&ReadWifiPHYLayerProtocol, |
| 100 base::Unretained(phy_layer_protocol)), |
| 101 base::Bind(&MetricsNetworkObserver::OnWifiPHYLayerProtocolResult, |
| 102 weak_ptr_factory_.GetWeakPtr(), |
| 103 base::Owned(phy_layer_protocol))); |
| 104 } |
| 105 |
| 106 void MetricsNetworkObserver::OnWifiPHYLayerProtocolResult( |
| 107 net::WifiPHYLayerProtocol* mode) { |
| 108 if (wifi_phy_layer_protocol_ != net::WIFI_PHY_LAYER_PROTOCOL_UNKNOWN && |
| 109 *mode != wifi_phy_layer_protocol_) { |
| 110 wifi_phy_layer_protocol_is_ambiguous_ = true; |
| 111 } |
| 112 wifi_phy_layer_protocol_ = *mode; |
| 113 } |
| 114 |
OLD | NEW |