xref: /aosp_15_r20/external/cronet/net/nqe/network_id.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors
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 "net/nqe/network_id.h"
6 
7 #include <tuple>
8 
9 #include "base/base64.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "net/nqe/proto/network_id_proto.pb.h"
12 
13 namespace net::nqe::internal {
14 
15 // static
FromString(const std::string & network_id)16 NetworkID NetworkID::FromString(const std::string& network_id) {
17   std::string base64_decoded;
18   if (!base::Base64Decode(network_id, &base64_decoded)) {
19     return NetworkID(NetworkChangeNotifier::CONNECTION_UNKNOWN, std::string(),
20                      INT32_MIN);
21   }
22 
23   NetworkIDProto network_id_proto;
24   if (!network_id_proto.ParseFromString(base64_decoded)) {
25     return NetworkID(NetworkChangeNotifier::CONNECTION_UNKNOWN, std::string(),
26                      INT32_MIN);
27   }
28 
29   return NetworkID(static_cast<NetworkChangeNotifier::ConnectionType>(
30                        network_id_proto.connection_type()),
31                    network_id_proto.id(), network_id_proto.signal_strength());
32 }
33 
NetworkID(NetworkChangeNotifier::ConnectionType type,const std::string & id,int32_t signal_strength)34 NetworkID::NetworkID(NetworkChangeNotifier::ConnectionType type,
35                      const std::string& id,
36                      int32_t signal_strength)
37     : type(type), id(id), signal_strength(signal_strength) {
38   // A valid value of |signal_strength| must be between 0 and 4 (both
39   // inclusive).
40   DCHECK((0 <= signal_strength && 4 >= signal_strength) ||
41          (INT32_MIN == signal_strength));
42 }
43 
44 NetworkID::NetworkID(const NetworkID& other) = default;
45 
46 NetworkID::~NetworkID() = default;
47 
operator ==(const NetworkID & other) const48 bool NetworkID::operator==(const NetworkID& other) const {
49   return type == other.type && id == other.id &&
50          signal_strength == other.signal_strength;
51 }
52 
operator !=(const NetworkID & other) const53 bool NetworkID::operator!=(const NetworkID& other) const {
54   return !operator==(other);
55 }
56 
57 NetworkID& NetworkID::operator=(const NetworkID& other) = default;
58 
59 // Overloaded to support ordered collections.
operator <(const NetworkID & other) const60 bool NetworkID::operator<(const NetworkID& other) const {
61   return std::tie(type, id, signal_strength) <
62          std::tie(other.type, other.id, other.signal_strength);
63 }
64 
ToString() const65 std::string NetworkID::ToString() const {
66   NetworkIDProto network_id_proto;
67   network_id_proto.set_connection_type(static_cast<int>(type));
68   network_id_proto.set_id(id);
69   network_id_proto.set_signal_strength(signal_strength);
70 
71   std::string serialized_network_id;
72   if (!network_id_proto.SerializeToString(&serialized_network_id))
73     return "";
74 
75   return base::Base64Encode(serialized_network_id);
76 }
77 
78 }  // namespace net::nqe::internal
79