xref: /aosp_15_r20/external/cronet/net/base/transport_info.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 std::string_view 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,bool cert_is_issued_by_known_root,NextProto negotiated_protocol)37 TransportInfo::TransportInfo(TransportType type_arg,
38                              IPEndPoint endpoint_arg,
39                              std::string accept_ch_frame_arg,
40                              bool cert_is_issued_by_known_root,
41                              NextProto negotiated_protocol)
42     : type(type_arg),
43       endpoint(std::move(endpoint_arg)),
44       accept_ch_frame(std::move(accept_ch_frame_arg)),
45       cert_is_issued_by_known_root(cert_is_issued_by_known_root),
46       negotiated_protocol(negotiated_protocol) {
47   switch (type) {
48     case TransportType::kCached:
49     case TransportType::kCachedFromProxy:
50       DCHECK_EQ(accept_ch_frame, "");
51       break;
52     case TransportType::kDirect:
53     case TransportType::kProxied:
54       // `accept_ch_frame` can be empty or not. We use an exhaustive switch
55       // statement to force this check to account for changes in the definition
56       // of `TransportType`.
57       break;
58   }
59 }
60 
61 TransportInfo::TransportInfo(const TransportInfo&) = default;
62 
63 TransportInfo::~TransportInfo() = default;
64 
65 bool TransportInfo::operator==(const TransportInfo& other) const = default;
66 
ToString() const67 std::string TransportInfo::ToString() const {
68   return base::StrCat({
69       "TransportInfo{ type = ",
70       TransportTypeToString(type),
71       ", endpoint = ",
72       endpoint.ToString(),
73       ", accept_ch_frame = ",
74       accept_ch_frame,
75       ", cert_is_issued_by_known_root = ",
76       cert_is_issued_by_known_root ? "true" : "false",
77       ", negotiated_protocol = ",
78       NextProtoToString(negotiated_protocol),
79       " }",
80   });
81 }
82 
operator <<(std::ostream & out,TransportType type)83 std::ostream& operator<<(std::ostream& out, TransportType type) {
84   return out << TransportTypeToString(type);
85 }
86 
operator <<(std::ostream & out,const TransportInfo & info)87 std::ostream& operator<<(std::ostream& out, const TransportInfo& info) {
88   return out << info.ToString();
89 }
90 
91 }  // namespace net
92