transport_info.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2020 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/base/transport_info.h"
  5. #include <ostream>
  6. #include <utility>
  7. #include "base/check.h"
  8. #include "base/notreached.h"
  9. #include "base/strings/strcat.h"
  10. namespace net {
  11. base::StringPiece TransportTypeToString(TransportType type) {
  12. switch (type) {
  13. case TransportType::kDirect:
  14. return "TransportType::kDirect";
  15. case TransportType::kProxied:
  16. return "TransportType::kProxied";
  17. case TransportType::kCached:
  18. return "TransportType::kCached";
  19. case TransportType::kCachedFromProxy:
  20. return "TransportType::kCachedFromProxy";
  21. }
  22. // We define this here instead of as a `default` clause above so as to force
  23. // a compiler error if a new value is added to the enum and this method is
  24. // not updated to reflect it.
  25. NOTREACHED();
  26. return "<invalid transport type>";
  27. }
  28. TransportInfo::TransportInfo() = default;
  29. TransportInfo::TransportInfo(TransportType type_arg,
  30. IPEndPoint endpoint_arg,
  31. std::string accept_ch_frame_arg)
  32. : type(type_arg),
  33. endpoint(std::move(endpoint_arg)),
  34. accept_ch_frame(std::move(accept_ch_frame_arg)) {
  35. switch (type) {
  36. case TransportType::kCached:
  37. case TransportType::kCachedFromProxy:
  38. DCHECK_EQ(accept_ch_frame, "");
  39. break;
  40. case TransportType::kDirect:
  41. case TransportType::kProxied:
  42. // `accept_ch_frame` can be empty or not. We use an exhaustive switch
  43. // statement to force this check to account for changes in the definition
  44. // of `TransportType`.
  45. break;
  46. }
  47. }
  48. TransportInfo::TransportInfo(const TransportInfo&) = default;
  49. TransportInfo::~TransportInfo() = default;
  50. bool TransportInfo::operator==(const TransportInfo& other) const {
  51. return type == other.type && endpoint == other.endpoint &&
  52. accept_ch_frame == other.accept_ch_frame;
  53. }
  54. bool TransportInfo::operator!=(const TransportInfo& other) const {
  55. return !(*this == other);
  56. }
  57. std::string TransportInfo::ToString() const {
  58. return base::StrCat({
  59. "TransportInfo{ type = ",
  60. TransportTypeToString(type),
  61. ", endpoint = ",
  62. endpoint.ToString(),
  63. ", accept_ch_frame = ",
  64. accept_ch_frame,
  65. " }",
  66. });
  67. }
  68. std::ostream& operator<<(std::ostream& out, TransportType type) {
  69. return out << TransportTypeToString(type);
  70. }
  71. std::ostream& operator<<(std::ostream& out, const TransportInfo& info) {
  72. return out << info.ToString();
  73. }
  74. } // namespace net