effective_connection_type.cc 2.6 KB

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