summaryrefslogtreecommitdiff
path: root/chromium/components/data_reduction_proxy/core/browser/data_reduction_proxy_network_delegate.h
blob: 33c717f128e5f64405316622c641b0ba938d3e2d (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// 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_NETWORK_DELEGATE_H_
#define COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_NETWORK_DELEGATE_H_

#include <stdint.h>

#include <memory>
#include <string>

#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/threading/thread_checker.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"
#include "net/base/completion_callback.h"
#include "net/base/layered_network_delegate.h"
#include "net/proxy/proxy_retry_info.h"

class GURL;

namespace net {
class HttpRequestHeaders;
class NetworkDelegate;
class ProxyConfig;
class ProxyInfo;
class URLRequest;
}

namespace data_reduction_proxy {
class DataReductionProxyBypassStats;
class DataReductionProxyConfig;
class DataReductionProxyConfigurator;
class DataReductionProxyIOData;
class DataReductionProxyRequestOptions;
class DataUseGroupProvider;
class DataUseGroup;

// Values of the UMA DataReductionProxy.LoFi.TransformationType histogram.
// This enum must remain synchronized with
// DataReductionProxyLoFiTransformationType in
// metrics/histograms/histograms.xml.
enum LitePageTransformationType {
  LITE_PAGE = 0,
  NO_TRANSFORMATION_LITE_PAGE_REQUESTED,
  LITE_PAGE_TRANSFORMATION_TYPES_INDEX_BOUNDARY,
};

// DataReductionProxyNetworkDelegate is a LayeredNetworkDelegate that wraps a
// NetworkDelegate and adds Data Reduction Proxy specific logic.
class DataReductionProxyNetworkDelegate : public net::LayeredNetworkDelegate {
 public:
  // Provides an additional proxy configuration that can be consulted after
  // proxy resolution. Used to get the Data Reduction Proxy config and give it
  // to the OnResolveProxyHandler and RecordBytesHistograms.
  typedef base::Callback<const net::ProxyConfig&()> ProxyConfigGetter;

  // Constructs a DataReductionProxyNetworkDelegate object with the given
  // |network_delegate|, |config|, |handler|, |configurator|, and
  // |experiments_stats|. Takes ownership of
  // and wraps the |network_delegate|, calling an internal implementation for
  // each delegate method. For example, the implementation of
  // OnHeadersReceived() calls OnHeadersReceivedInternal().
  DataReductionProxyNetworkDelegate(
      std::unique_ptr<net::NetworkDelegate> network_delegate,
      DataReductionProxyConfig* config,
      DataReductionProxyRequestOptions* handler,
      const DataReductionProxyConfigurator* configurator);
  ~DataReductionProxyNetworkDelegate() override;

  // Initializes member variables to record data reduction proxy prefs and
  // report UMA.
  void InitIODataAndUMA(
      DataReductionProxyIOData* io_data,
      DataReductionProxyBypassStats* bypass_stats);

  void SetDataUseGroupProvider(
      std::unique_ptr<DataUseGroupProvider> data_use_group_provider);

 private:
  friend class DataReductionProxyTestContext;

  // Resets if Lo-Fi has been used for the last main frame load to false.
  void OnBeforeURLRequestInternal(net::URLRequest* request,
                                  const net::CompletionCallback& callback,
                                  GURL* new_url) override;

  // Called before an HTTP transaction is started. Allows the delegate to
  // modify the Chrome-Proxy-Accept-Transform header to convey acceptable
  // content transformations.
  void OnBeforeStartTransactionInternal(
      net::URLRequest* request,
      const net::CompletionCallback& callback,
      net::HttpRequestHeaders* headers) override;

  // Called after connection. Allows the delegate to read/write
  // |headers| before they get sent out. |headers| is valid only until
  // OnCompleted or OnURLRequestDestroyed is called for this request.
  void OnBeforeSendHeadersInternal(
      net::URLRequest* request,
      const net::ProxyInfo& proxy_info,
      const net::ProxyRetryInfoMap& proxy_retry_info,
      net::HttpRequestHeaders* headers) override;

  // Called after a redirect response. Clears out persistent
  // DataReductionProxyData from the URLRequest.
  void OnBeforeRedirectInternal(net::URLRequest* request,
                                const GURL& new_location) override;

  // Indicates that the URL request has been completed or failed.
  // |started| indicates whether the request has been started. If false,
  // some information like the socket address is not available.
  void OnCompletedInternal(net::URLRequest* request,
                           bool started) override;

  // Checks if a LoFi or Lite Pages response was received and sets the state on
  // DataReductionProxyData for |request|.
  void OnHeadersReceivedInternal(
      net::URLRequest* request,
      const net::CompletionCallback& callback,
      const net::HttpResponseHeaders* original_response_headers,
      scoped_refptr<net::HttpResponseHeaders>* override_response_headers,
      GURL* allowed_unsafe_redirect_url) override;

  // Calculates actual data usage that went over the network at the HTTP layer
  // (e.g. not including network layer overhead) and estimates original data
  // usage for |request|.
  void CalculateAndRecordDataUsage(const net::URLRequest& request,
                                   DataReductionProxyRequestType request_type);

  // Posts to the UI thread to UpdateContentLengthPrefs in the data reduction
  // proxy metrics and updates |received_content_length_| and
  // |original_content_length_|.
  void AccumulateDataUsage(int64_t data_used,
                           int64_t original_size,
                           DataReductionProxyRequestType request_type,
                           const scoped_refptr<DataUseGroup>& data_use_group,
                           const std::string& mime_type);

  // Record information such as histograms related to the Content-Length of
  // |request|. |original_content_length| is the length of the resource if
  // fetched over a direct connection without the Data Reduction Proxy, or -1 if
  // no original content length is available.
  void RecordContentLength(const net::URLRequest& request,
                           DataReductionProxyRequestType request_type,
                           int64_t original_content_length);

  // Records UMA that counts how many pages were transformed by various lite
  // page transformations.
  void RecordLitePageTransformationType(LitePageTransformationType type);

  // Returns whether |request| would have used the data reduction proxy server
  // if the holdback fieldtrial weren't enabled. |proxy_info| is the list of
  // proxies being used, and |proxy_retry_info| contains a list of bad proxies.
  bool WasEligibleWithoutHoldback(
      const net::URLRequest& request,
      const net::ProxyInfo& proxy_info,
      const net::ProxyRetryInfoMap& proxy_retry_info) const;

  // May add Brotli to Accept Encoding request header if |proxy_info| contains
  // a proxy server that is expected to support Brotli encoding.
  void MaybeAddBrotliToAcceptEncodingHeader(
      const net::ProxyInfo& proxy_info,
      net::HttpRequestHeaders* request_headers,
      const net::URLRequest& request) const;

  // All raw Data Reduction Proxy pointers must outlive |this|.
  DataReductionProxyConfig* data_reduction_proxy_config_;

  DataReductionProxyBypassStats* data_reduction_proxy_bypass_stats_;

  DataReductionProxyRequestOptions* data_reduction_proxy_request_options_;

  DataReductionProxyIOData* data_reduction_proxy_io_data_;

  const DataReductionProxyConfigurator* configurator_;

  std::unique_ptr<DataUseGroupProvider> data_use_group_provider_;

  base::ThreadChecker thread_checker_;

  DISALLOW_COPY_AND_ASSIGN(DataReductionProxyNetworkDelegate);
};
}  // namespace data_reduction_proxy

#endif  // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_NETWORK_DELEGATE_H_