File: cronet_proxy_delegate.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (151 lines) | stat: -rw-r--r-- 6,320 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/cronet/cronet_proxy_delegate.h"

#include "base/logging.h"
#include "base/notreached.h"
#include "base/trace_event/trace_event.h"
#include "net/base/net_errors.h"
#include "net/proxy_resolution/proxy_info.h"

namespace cronet {

CronetProxyDelegate::CronetProxyDelegate(
    cronet::proto::ProxyOptions proxy_options,
    CronetContext::NetworkTasks* network_tasks)
    : proxy_options_(std::move(proxy_options)), network_tasks_(network_tasks) {
  CHECK(network_tasks);
}

CronetProxyDelegate::~CronetProxyDelegate() = default;

void CronetProxyDelegate::OnResolveProxy(
    const GURL& url,
    const net::NetworkAnonymizationKey& network_anonymization_key,
    const std::string& method,
    const net::ProxyRetryInfoMap& proxy_retry_info,
    net::ProxyInfo* result) {
  TRACE_EVENT_BEGIN("cronet", "CronetProxyDelegate::OnResolveProxy", "url", url,
                    "method", method, "initial_proxy_info",
                    result->ToDebugString());
  // Using a self-calling lambda to make sure we always call TRACE_EVENT_END
  // regardless of early returns.
  [&] {
    net::ProxyList proxy_list;
    for (int i = 0; i < proxy_options_.proxies_size(); ++i) {
      const auto& proxy_server = proxy_options_.proxies(i);
      if (proxy_server.scheme() == cronet::proto::ProxyScheme::DIRECT) {
        proxy_list.AddProxyChain(
            net::ProxyChain::WithOpaqueData(std::vector<net::ProxyServer>(),
                                            /*opaque_data=*/i));
      } else {
        net::ProxyServer::Scheme scheme =
            net::ProxyServer::Scheme::SCHEME_INVALID;
        switch (proxy_server.scheme()) {
          case cronet::proto::ProxyScheme::HTTP:
            scheme = net::ProxyServer::Scheme::SCHEME_HTTP;
            break;
          case cronet::proto::ProxyScheme::HTTPS:
            scheme = net::ProxyServer::Scheme::SCHEME_HTTPS;
            break;
          default:
            NOTREACHED();
        }
        proxy_list.AddProxyChain(net::ProxyChain::WithOpaqueData(
            std::vector<net::ProxyServer>{net::ProxyServer(
                scheme,
                net::HostPortPair(proxy_server.host(), proxy_server.port()))},
            /*opaque_data=*/i));
      }
    }
    result->UseProxyList(proxy_list);
  }();
  TRACE_EVENT_END("cronet", "resulting_proxy_info", result->ToDebugString());
}

void CronetProxyDelegate::OnSuccessfulRequestAfterFailures(
    const net::ProxyRetryInfoMap& proxy_retry_info) {
  TRACE_EVENT_INSTANT("cronet",
                      "CronetProxyDelegate::OnSuccessfulRequestAfterFailures");
}

void CronetProxyDelegate::OnFallback(const net::ProxyChain& bad_chain,
                                     int net_error) {
  TRACE_EVENT_INSTANT("cronet", "CronetProxyDelegate::OnFallback", "bad_chain",
                      bad_chain.ToDebugString(), "net_error", net_error);
}

net::Error CronetProxyDelegate::OnBeforeTunnelRequest(
    // Don't be confused, this is the index of the proxy within the chain, not
    // the index of the chain itself.
    const net::ProxyChain& proxy_chain,
    size_t chain_index,
    net::HttpRequestHeaders* extra_headers) {
  TRACE_EVENT_BEGIN("cronet", "CronetProxyDelegate::OnBeforeTunnelRequest",
                    "proxy_chain", proxy_chain.ToDebugString(), "chain_index",
                    chain_index);
  CHECK(proxy_chain.opaque_data().has_value());
  auto result = [&] {
    if (network_tasks_->OnBeforeTunnelRequest(*proxy_chain.opaque_data(),
                                              extra_headers)) {
      return net::OK;
    }
    // TODO(https://crbug.com/422428959): Decide whether we want to propagate
    // org.chromium.net.Proxy.Callback canceling a tunnel establishment request
    // as net::ERR_TUNNEL_CONNECTION_FAILED. This is currently not possible, as
    // net::ProxyFallback::CanFalloverToNextProxy does not try the next proxy in
    // the list for net::ERR_TUNNEL_CONNECTION_FAILED, unless the chain is for
    // IP Protection. For the time being, we return another error for which the
    // next proxy is in the list is always attempted.
    return net::ERR_CONNECTION_CLOSED;
  }();
  TRACE_EVENT_END("cronet", "result", result);
  return result;
}

net::Error CronetProxyDelegate::OnTunnelHeadersReceived(
    // Don't be confused, this is the index of the proxy within the chain, not
    // the index of the chain itself.
    const net::ProxyChain& proxy_chain,
    size_t chain_index,
    const net::HttpResponseHeaders& response_headers) {
  TRACE_EVENT_BEGIN("cronet", "CronetProxyDelegate::OnTunnelHeadersReceived",
                    "proxy_chain", proxy_chain.ToDebugString(), "chain_index",
                    chain_index);
  CHECK(proxy_chain.opaque_data().has_value());
  auto result = [&] {
    if (network_tasks_->OnTunnelHeadersReceived(*proxy_chain.opaque_data(),
                                                response_headers)) {
      return net::OK;
    }
    // TODO(https://crbug.com/422428959): Decide whether we want to propagate
    // org.chromium.net.Proxy.Callback canceling a tunnel establishment request
    // as net::ERR_TUNNEL_CONNECTION_FAILED. This is currently not possible, as
    // net::ProxyFallback::CanFalloverToNextProxy does not try the next proxy in
    // the list for net::ERR_TUNNEL_CONNECTION_FAILED, unless the chain is for
    // IP Protection. For the time being, we return another error for which the
    // next proxy is in the list is always attempted.
    return net::ERR_CONNECTION_CLOSED;
  }();
  TRACE_EVENT_END("cronet", "result", result);
  return result;
}

void CronetProxyDelegate::SetProxyResolutionService(
    net::ProxyResolutionService* proxy_resolution_service) {
  TRACE_EVENT_INSTANT("cronet",
                      "CronetProxyDelegate::SetProxyResolutionService");
}

bool CronetProxyDelegate::AliasRequiresProxyOverride(
    const std::string scheme,
    const std::vector<std::string>& dns_aliases,
    const net::NetworkAnonymizationKey& network_anonymization_key) {
  TRACE_EVENT_INSTANT("cronet",
                      "CronetProxyDelegate::AliasRequiresProxyOverride");
  return false;
}

}  // namespace cronet