xref: /aosp_15_r20/external/cronet/net/base/transport_info.h (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 #ifndef NET_BASE_TRANSPORT_INFO_H_
6 #define NET_BASE_TRANSPORT_INFO_H_
7 
8 #include <iosfwd>
9 #include <string>
10 #include <string_view>
11 
12 #include "net/base/ip_endpoint.h"
13 #include "net/base/net_export.h"
14 #include "net/socket/next_proto.h"
15 
16 namespace net {
17 
18 // Specifies the type of a network transport over which a resource is loaded.
19 enum class TransportType {
20   // The transport was established directly to a peer.
21   kDirect,
22   // The transport was established to a proxy of some kind.
23   kProxied,
24   // The transport was "established" to a cache entry.
25   kCached,
26   // Same as `kCached`, but the resource was initially loaded through a proxy.
27   kCachedFromProxy,
28 };
29 
30 // Returns a string representation of the given transport type.
31 // The returned StringPiece is static, has no lifetime restrictions.
32 NET_EXPORT std::string_view TransportTypeToString(TransportType type);
33 
34 // Describes a network transport.
35 struct NET_EXPORT TransportInfo {
36   TransportInfo();
37   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   TransportInfo(const TransportInfo&);
43   ~TransportInfo();
44 
45   // Instances of this type are comparable for equality.
46   bool operator==(const TransportInfo& other) const;
47 
48   // Returns a string representation of this struct, suitable for debugging.
49   std::string ToString() const;
50 
51   // The type of the transport.
52   TransportType type = TransportType::kDirect;
53 
54   // If `type` is `kDirect`, then this identifies the peer endpoint.
55   // If `type` is `kProxied`, then this identifies the proxy endpoint.
56   // If `type` is `kCached`, then this identifies the peer endpoint from which
57   // the resource was originally loaded.
58   // If `type` is `kCachedFromProxy`, then this identifies the proxy endpoint
59   // from which the resource was originally loaded.
60   IPEndPoint endpoint;
61 
62   // The value of the ACCEPT_CH HTTP2/3 frame, as pulled in through ALPS.
63   //
64   // Invariant: if `type` is `kCached` or `kCachedFromProxy`, then this is
65   // empty.
66   std::string accept_ch_frame;
67 
68   // True if the transport layer was secure and the certificate was rooted at a
69   // standard CA root. (As opposed to a user-installed root.)
70   //
71   // Invariant: if `type` is `kCached` or `kCachedFromProxy`, then this is
72   // always false.
73   bool cert_is_issued_by_known_root = false;
74 
75   // The negotiated protocol info for the transport layer.
76   //
77   // Invariant: if `type` is `kCached` or `kCachedFromProxy`, then this is
78   // always kProtoUnknown.
79   NextProto negotiated_protocol = kProtoUnknown;
80 };
81 
82 // Instances of these types are streamable for easier debugging.
83 NET_EXPORT std::ostream& operator<<(std::ostream& out, TransportType type);
84 NET_EXPORT std::ostream& operator<<(std::ostream& out,
85                                     const TransportInfo& info);
86 
87 }  // namespace net
88 
89 #endif  // NET_BASE_TRANSPORT_INFO_H_
90