summaryrefslogtreecommitdiff
path: root/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_delegate.h
blob: d412dc3b1cc6d1e4b4d86709e27288ed92272d76 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_DELEGATE_H_
#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_DELEGATE_H_

#include <string>

#include "base/macros.h"
#include "base/threading/thread_checker.h"
#include "net/base/network_change_notifier.h"
#include "net/base/proxy_delegate.h"
#include "net/proxy/proxy_retry_info.h"
#include "net/proxy/proxy_server.h"
#include "url/gurl.h"

namespace base {
class TickClock;
}

namespace net {
class HostPortPair;
class HttpRequestHeaders;
class HttpResponseHeaders;
class NetLog;
class ProxyConfig;
class ProxyInfo;
class ProxyService;
}

namespace data_reduction_proxy {

class DataReductionProxyBypassStats;
class DataReductionProxyConfig;
class DataReductionProxyConfigurator;
class DataReductionProxyEventCreator;
class DataReductionProxyIOData;

class DataReductionProxyDelegate
    : public net::ProxyDelegate,
      public net::NetworkChangeNotifier::IPAddressObserver {
 public:
  // ProxyDelegate instance is owned by io_thread. |auth_handler| and |config|
  // outlives this class instance.
  DataReductionProxyDelegate(DataReductionProxyConfig* config,
                             const DataReductionProxyConfigurator* configurator,
                             DataReductionProxyEventCreator* event_creator,
                             DataReductionProxyBypassStats* bypass_stats,
                             net::NetLog* net_log);

  ~DataReductionProxyDelegate() override;

  // Performs initialization on the IO thread.
  void InitializeOnIOThread(DataReductionProxyIOData* io_data);

  // net::ProxyDelegate implementation:
  void OnResolveProxy(const GURL& url,
                      const std::string& method,
                      const net::ProxyService& proxy_service,
                      net::ProxyInfo* result) override;
  void OnFallback(const net::ProxyServer& bad_proxy, int net_error) override;
  void OnBeforeTunnelRequest(const net::HostPortPair& proxy_server,
                             net::HttpRequestHeaders* extra_headers) override;
  void OnTunnelConnectCompleted(const net::HostPortPair& endpoint,
                                const net::HostPortPair& proxy_server,
                                int net_error) override;
  bool IsTrustedSpdyProxy(const net::ProxyServer& proxy_server) override;
  void OnTunnelHeadersReceived(
      const net::HostPortPair& origin,
      const net::HostPortPair& proxy_server,
      const net::HttpResponseHeaders& response_headers) override;

  void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock);

 protected:
  // Protected so that these methods are accessible for testing.
  // net::ProxyDelegate implementation:
  void GetAlternativeProxy(
      const GURL& url,
      const net::ProxyServer& resolved_proxy_server,
      net::ProxyServer* alternative_proxy_server) const override;
  void OnAlternativeProxyBroken(
      const net::ProxyServer& alternative_proxy_server) override;
  net::ProxyServer GetDefaultAlternativeProxy() const override;

  // Protected so that it can be overridden during testing.
  // Returns true if |proxy_server| supports QUIC.
  virtual bool SupportsQUIC(const net::ProxyServer& proxy_server) const;

  // Availability status of data reduction QUIC proxy.
  // Protected so that the enum values are accessible for testing.
  enum QuicProxyStatus {
    QUIC_PROXY_STATUS_AVAILABLE,
    QUIC_PROXY_NOT_SUPPORTED,
    QUIC_PROXY_STATUS_MARKED_AS_BROKEN,
    QUIC_PROXY_STATUS_BOUNDARY
  };

  // Availability status of data reduction proxy that supports 0-RTT QUIC.
  // Protected so that the enum values are accessible for testing.
  enum DefaultAlternativeProxyStatus {
    DEFAULT_ALTERNATIVE_PROXY_STATUS_AVAILABLE,
    DEFAULT_ALTERNATIVE_PROXY_STATUS_BROKEN,
    DEFAULT_ALTERNATIVE_PROXY_STATUS_UNAVAILABLE,
    DEFAULT_ALTERNATIVE_PROXY_STATUS_BOUNDARY,
  };

 private:
  // Records the availability status of data reduction proxy.
  void RecordQuicProxyStatus(QuicProxyStatus status) const;

  // Records the availability status of data reduction proxy that supports 0-RTT
  // QUIC.
  void RecordGetDefaultAlternativeProxy(
      DefaultAlternativeProxyStatus status) const;

  // NetworkChangeNotifier::IPAddressObserver:
  void OnIPAddressChanged() override;

  const DataReductionProxyConfig* config_;
  const DataReductionProxyConfigurator* configurator_;
  DataReductionProxyEventCreator* event_creator_;
  DataReductionProxyBypassStats* bypass_stats_;

  // True if the use of alternate proxies is disabled.
  bool alternative_proxies_broken_;

  // Tick clock used for obtaining the current time.
  std::unique_ptr<base::TickClock> tick_clock_;

  // True if the metrics related to the first request whose resolved proxy was a
  // data saver proxy has been recorded. |first_data_saver_request_recorded_| is
  // reset to false on IP address change events.
  bool first_data_saver_request_recorded_;

  // Set to the time when last IP address change event was received, or the time
  // of initialization of |this|, whichever is later.
  base::TimeTicks last_network_change_time_;

  DataReductionProxyIOData* io_data_;

  net::NetLog* net_log_;

  base::ThreadChecker thread_checker_;

  DISALLOW_COPY_AND_ASSIGN(DataReductionProxyDelegate);
};

// Adds data reduction proxies to |result|, where applicable, if result
// otherwise uses a direct connection for |url|, and the data reduction proxy is
// not bypassed. Also, configures |result| to proceed directly to the origin if
// |result|'s current proxy is the data reduction proxy
// This is visible for test purposes.
void OnResolveProxyHandler(
    const GURL& url,
    const std::string& method,
    const net::ProxyConfig& proxy_config,
    const net::ProxyRetryInfoMap& proxy_retry_info,
    const DataReductionProxyConfig& data_reduction_proxy_config,
    DataReductionProxyIOData* io_data,
    net::ProxyInfo* result);
}  // namespace data_reduction_proxy

#endif  // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_DELEGATE_H_