| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef NET_NQE_NETWORK_ID_H_ | 5 #ifndef NET_NQE_NETWORK_ID_H_ |
| 6 #define NET_NQE_NETWORK_ID_H_ | 6 #define NET_NQE_NETWORK_ID_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <tuple> | 9 #include <tuple> |
| 10 | 10 |
| 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "net/base/net_export.h" | 12 #include "net/base/net_export.h" |
| 12 #include "net/base/network_change_notifier.h" | 13 #include "net/base/network_change_notifier.h" |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 16 |
| 15 const char kValueSeparator[] = ","; | 17 const char kValueSeparator[] = ","; |
| 18 |
| 19 // Parses |connection_type_string| as a NetworkChangeNotifier::ConnectionType. |
| 20 // |connection_type_string| must contain the |
| 21 // NetworkChangeNotifier::ConnectionType enum as an interger. |
| 22 net::NetworkChangeNotifier::ConnectionType ConvertStringToConnectionType( |
| 23 const std::string& connection_type_string) { |
| 24 int connection_type_int = |
| 25 static_cast<int>(net::NetworkChangeNotifier::CONNECTION_UNKNOWN); |
| 26 bool connection_type_available = |
| 27 base::StringToInt(connection_type_string, &connection_type_int); |
| 28 |
| 29 if (!connection_type_available || connection_type_int < 0 || |
| 30 connection_type_int >= |
| 31 static_cast<int>(net::NetworkChangeNotifier::CONNECTION_LAST)) { |
| 32 DCHECK(false); |
| 33 return net::NetworkChangeNotifier::CONNECTION_UNKNOWN; |
| 34 } |
| 35 return static_cast<net::NetworkChangeNotifier::ConnectionType>( |
| 36 connection_type_int); |
| 16 } | 37 } |
| 17 | 38 |
| 39 } // namespace |
| 40 |
| 18 namespace net { | 41 namespace net { |
| 19 namespace nqe { | 42 namespace nqe { |
| 20 namespace internal { | 43 namespace internal { |
| 21 | 44 |
| 22 // NetworkID is used to uniquely identify a network. | 45 // NetworkID is used to uniquely identify a network. |
| 23 // For the purpose of network quality estimation and caching, a network is | 46 // For the purpose of network quality estimation and caching, a network is |
| 24 // uniquely identified by a combination of |type| and | 47 // uniquely identified by a combination of |type| and |
| 25 // |id|. This approach is unable to distinguish networks with | 48 // |id|. This approach is unable to distinguish networks with |
| 26 // same name (e.g., different Wi-Fi networks with same SSID). | 49 // same name (e.g., different Wi-Fi networks with same SSID). |
| 27 // This is a protected member to expose it to tests. | 50 // This is a protected member to expose it to tests. |
| 28 struct NET_EXPORT_PRIVATE NetworkID { | 51 struct NET_EXPORT_PRIVATE NetworkID { |
| 52 static NetworkID FromString(const std::string& network_id) { |
| 53 size_t separator_index = network_id.find(kValueSeparator); |
| 54 DCHECK_NE(std::string::npos, separator_index); |
| 55 if (separator_index == std::string::npos) { |
| 56 return NetworkID(NetworkChangeNotifier::CONNECTION_UNKNOWN, |
| 57 std::string()); |
| 58 } |
| 59 |
| 60 return NetworkID( |
| 61 ConvertStringToConnectionType(network_id.substr(separator_index + 1)), |
| 62 network_id.substr(0, separator_index)); |
| 63 } |
| 29 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | 64 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) |
| 30 : type(type), id(id) {} | 65 : type(type), id(id) {} |
| 31 NetworkID(const NetworkID& other) : type(other.type), id(other.id) {} | 66 NetworkID(const NetworkID& other) : type(other.type), id(other.id) {} |
| 32 ~NetworkID() {} | 67 ~NetworkID() {} |
| 33 | 68 |
| 69 bool operator==(const NetworkID& other) const { |
| 70 return type == other.type && id == other.id; |
| 71 } |
| 72 |
| 73 bool operator!=(const NetworkID& other) const { return !operator==(other); } |
| 74 |
| 34 NetworkID& operator=(const NetworkID& other) { | 75 NetworkID& operator=(const NetworkID& other) { |
| 35 type = other.type; | 76 type = other.type; |
| 36 id = other.id; | 77 id = other.id; |
| 37 return *this; | 78 return *this; |
| 38 } | 79 } |
| 39 | 80 |
| 40 // Overloaded to support ordered collections. | 81 // Overloaded to support ordered collections. |
| 41 bool operator<(const NetworkID& other) const { | 82 bool operator<(const NetworkID& other) const { |
| 42 return std::tie(type, id) < std::tie(other.type, other.id); | 83 return std::tie(type, id) < std::tie(other.type, other.id); |
| 43 } | 84 } |
| 44 | 85 |
| 45 std::string ToString() const { | 86 std::string ToString() const { |
| 46 return id + kValueSeparator + | 87 return id + kValueSeparator + base::IntToString(static_cast<int>(type)); |
| 47 NetworkChangeNotifier::ConnectionTypeToString(type); | |
| 48 } | 88 } |
| 49 | 89 |
| 50 // Connection type of the network. | 90 // Connection type of the network. |
| 51 NetworkChangeNotifier::ConnectionType type; | 91 NetworkChangeNotifier::ConnectionType type; |
| 52 | 92 |
| 53 // Name of this network. This is set to: | 93 // Name of this network. This is set to: |
| 54 // - Wi-Fi SSID if the device is connected to a Wi-Fi access point and the | 94 // - Wi-Fi SSID if the device is connected to a Wi-Fi access point and the |
| 55 // SSID name is available, or | 95 // SSID name is available, or |
| 56 // - MCC/MNC code of the cellular carrier if the device is connected to a | 96 // - MCC/MNC code of the cellular carrier if the device is connected to a |
| 57 // cellular network, or | 97 // cellular network, or |
| 58 // - "Ethernet" in case the device is connected to ethernet. | 98 // - "Ethernet" in case the device is connected to ethernet. |
| 59 // - An empty string in all other cases or if the network name is not | 99 // - An empty string in all other cases or if the network name is not |
| 60 // exposed by platform APIs. | 100 // exposed by platform APIs. |
| 61 std::string id; | 101 std::string id; |
| 62 }; | 102 }; |
| 63 | 103 |
| 64 } // namespace internal | 104 } // namespace internal |
| 65 } // namespace nqe | 105 } // namespace nqe |
| 66 } // namespace net | 106 } // namespace net |
| 67 | 107 |
| 68 #endif // NET_NQE_NETWORK_QUALITY_ESTIMATOR_H_ | 108 #endif // NET_NQE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |