1 // Copyright 2018 The Chromium Authors. All rights reserved.
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 SERVICES_NETWORK_NETWORK_SERVICE_PROXY_DELEGATE_H_
6 #define SERVICES_NETWORK_NETWORK_SERVICE_PROXY_DELEGATE_H_
7 
8 #include <deque>
9 
10 #include "base/component_export.h"
11 #include "base/macros.h"
12 #include "mojo/public/cpp/bindings/receiver.h"
13 #include "net/base/proxy_delegate.h"
14 #include "services/network/public/mojom/network_context.mojom.h"
15 
16 namespace net {
17 class HttpRequestHeaders;
18 class ProxyResolutionService;
19 }  // namespace net
20 
21 namespace network {
22 
23 // NetworkServiceProxyDelegate is used to support the custom proxy
24 // configuration, which can be set in
25 // NetworkContextParams.custom_proxy_config_client_receiver.
COMPONENT_EXPORT(NETWORK_SERVICE)26 class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkServiceProxyDelegate
27     : public net::ProxyDelegate,
28       public mojom::CustomProxyConfigClient {
29  public:
30   explicit NetworkServiceProxyDelegate(
31       mojom::CustomProxyConfigPtr initial_config,
32       mojo::PendingReceiver<mojom::CustomProxyConfigClient>
33           config_client_receiver);
34   ~NetworkServiceProxyDelegate() override;
35 
36   void SetProxyResolutionService(
37       net::ProxyResolutionService* proxy_resolution_service) {
38     proxy_resolution_service_ = proxy_resolution_service;
39   }
40 
41   // net::ProxyDelegate implementation:
42   void OnResolveProxy(const GURL& url,
43                       const std::string& method,
44                       const net::ProxyRetryInfoMap& proxy_retry_info,
45                       net::ProxyInfo* result) override;
46   void OnFallback(const net::ProxyServer& bad_proxy, int net_error) override;
47   void OnBeforeTunnelRequest(const net::ProxyServer& proxy_server,
48                              net::HttpRequestHeaders* extra_headers) override;
49   net::Error OnTunnelHeadersReceived(
50       const net::ProxyServer& proxy_server,
51       const net::HttpResponseHeaders& response_headers) override;
52 
53  private:
54   // Checks whether |proxy_server| is present in the current proxy config.
55   bool IsInProxyConfig(const net::ProxyServer& proxy_server) const;
56 
57   // Whether the current config may proxy |url|.
58   bool MayProxyURL(const GURL& url) const;
59 
60   // Whether the HTTP |method| with current |proxy_info| is eligible to be
61   // proxied.
62   bool EligibleForProxy(const net::ProxyInfo& proxy_info,
63                         const std::string& method) const;
64 
65   // mojom::CustomProxyConfigClient implementation:
66   void OnCustomProxyConfigUpdated(
67       mojom::CustomProxyConfigPtr proxy_config) override;
68   void MarkProxiesAsBad(base::TimeDelta bypass_duration,
69                         const net::ProxyList& bad_proxies,
70                         MarkProxiesAsBadCallback callback) override;
71   void ClearBadProxiesCache() override;
72 
73   mojom::CustomProxyConfigPtr proxy_config_;
74   mojo::Receiver<mojom::CustomProxyConfigClient> receiver_;
75 
76   net::ProxyResolutionService* proxy_resolution_service_ = nullptr;
77 
78   DISALLOW_COPY_AND_ASSIGN(NetworkServiceProxyDelegate);
79 };
80 
81 }  // namespace network
82 
83 #endif  // SERVICES_NETWORK_NETWORK_SERVICE_PROXY_DELEGATE_H_
84