xref: /aosp_15_r20/external/cronet/net/nqe/effective_connection_type.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 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/effective_connection_type.h"
6 
7 #include <string_view>
8 
9 #include "base/notreached.h"
10 
11 namespace {
12 
13 const char kDeprectedEffectiveConnectionTypeSlow2G[] = "Slow2G";
14 
15 }  // namespace
16 
17 namespace net {
18 
19 const char kEffectiveConnectionTypeUnknown[] = "Unknown";
20 const char kEffectiveConnectionTypeOffline[] = "Offline";
21 const char kEffectiveConnectionTypeSlow2G[] = "Slow-2G";
22 const char kEffectiveConnectionType2G[] = "2G";
23 const char kEffectiveConnectionType3G[] = "3G";
24 const char kEffectiveConnectionType4G[] = "4G";
25 
GetNameForEffectiveConnectionType(EffectiveConnectionType type)26 const char* GetNameForEffectiveConnectionType(EffectiveConnectionType type) {
27   switch (type) {
28     case EFFECTIVE_CONNECTION_TYPE_UNKNOWN:
29       return kEffectiveConnectionTypeUnknown;
30     case EFFECTIVE_CONNECTION_TYPE_OFFLINE:
31       return kEffectiveConnectionTypeOffline;
32     case EFFECTIVE_CONNECTION_TYPE_SLOW_2G:
33       return kEffectiveConnectionTypeSlow2G;
34     case EFFECTIVE_CONNECTION_TYPE_2G:
35       return kEffectiveConnectionType2G;
36     case EFFECTIVE_CONNECTION_TYPE_3G:
37       return kEffectiveConnectionType3G;
38     case EFFECTIVE_CONNECTION_TYPE_4G:
39       return kEffectiveConnectionType4G;
40     case EFFECTIVE_CONNECTION_TYPE_LAST:
41       NOTREACHED();
42       return "";
43   }
44   NOTREACHED();
45   return "";
46 }
47 
GetEffectiveConnectionTypeForName(std::string_view connection_type_name)48 std::optional<EffectiveConnectionType> GetEffectiveConnectionTypeForName(
49     std::string_view connection_type_name) {
50   if (connection_type_name == kEffectiveConnectionTypeUnknown)
51     return EFFECTIVE_CONNECTION_TYPE_UNKNOWN;
52   if (connection_type_name == kEffectiveConnectionTypeOffline)
53     return EFFECTIVE_CONNECTION_TYPE_OFFLINE;
54   if (connection_type_name == kEffectiveConnectionTypeSlow2G)
55     return EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
56   // Return EFFECTIVE_CONNECTION_TYPE_SLOW_2G if the deprecated string
57   // representation is in use.
58   if (connection_type_name == kDeprectedEffectiveConnectionTypeSlow2G)
59     return EFFECTIVE_CONNECTION_TYPE_SLOW_2G;
60   if (connection_type_name == kEffectiveConnectionType2G)
61     return EFFECTIVE_CONNECTION_TYPE_2G;
62   if (connection_type_name == kEffectiveConnectionType3G)
63     return EFFECTIVE_CONNECTION_TYPE_3G;
64   if (connection_type_name == kEffectiveConnectionType4G)
65     return EFFECTIVE_CONNECTION_TYPE_4G;
66   return std::nullopt;
67 }
68 
DeprecatedGetNameForEffectiveConnectionType(EffectiveConnectionType type)69 const char* DeprecatedGetNameForEffectiveConnectionType(
70     EffectiveConnectionType type) {
71   switch (type) {
72     case EFFECTIVE_CONNECTION_TYPE_SLOW_2G:
73       return kDeprectedEffectiveConnectionTypeSlow2G;
74     default:
75       return GetNameForEffectiveConnectionType(type);
76   }
77 }
78 
79 }  // namespace net
80