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