• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }
33 
34 TransportInfo::TransportInfo() = default;
35 
TransportInfo(TransportType type_arg,IPEndPoint endpoint_arg,std::string accept_ch_frame_arg,bool cert_is_issued_by_known_root,NextProto negotiated_protocol)36 TransportInfo::TransportInfo(TransportType type_arg,
37                              IPEndPoint endpoint_arg,
38                              std::string accept_ch_frame_arg,
39                              bool cert_is_issued_by_known_root,
40                              NextProto negotiated_protocol)
41     : type(type_arg),
42       endpoint(std::move(endpoint_arg)),
43       accept_ch_frame(std::move(accept_ch_frame_arg)),
44       cert_is_issued_by_known_root(cert_is_issued_by_known_root),
45       negotiated_protocol(negotiated_protocol) {
46   switch (type) {
47     case TransportType::kCached:
48     case TransportType::kCachedFromProxy:
49       DCHECK_EQ(accept_ch_frame, "");
50       break;
51     case TransportType::kDirect:
52     case TransportType::kProxied:
53       // `accept_ch_frame` can be empty or not. We use an exhaustive switch
54       // statement to force this check to account for changes in the definition
55       // of `TransportType`.
56       break;
57   }
58 }
59 
60 TransportInfo::TransportInfo(const TransportInfo&) = default;
61 
62 TransportInfo::~TransportInfo() = default;
63 
64 bool TransportInfo::operator==(const TransportInfo& other) const = default;
65 
ToString() const66 std::string TransportInfo::ToString() const {
67   return base::StrCat({
68       "TransportInfo{ type = ",
69       TransportTypeToString(type),
70       ", endpoint = ",
71       endpoint.ToString(),
72       ", accept_ch_frame = ",
73       accept_ch_frame,
74       ", cert_is_issued_by_known_root = ",
75       cert_is_issued_by_known_root ? "true" : "false",
76       ", negotiated_protocol = ",
77       NextProtoToString(negotiated_protocol),
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