summaryrefslogtreecommitdiffstats
path: root/components/data_reduction_proxy/browser/data_reduction_proxy_params.h
blob: ae024b8063e2dd1515622e97a6cfda3808136eaf (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// 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_BROWSER_DATA_REDUCTION_PROXY_PARAMS_H_
#define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_PARAMS_H_

#include <string>
#include <utility>
#include <vector>

#include "base/macros.h"
#include "net/base/host_port_pair.h"
#include "net/proxy/proxy_retry_info.h"
#include "url/gurl.h"

namespace base {
class TimeDelta;
}

namespace net {
class URLRequest;
}

namespace data_reduction_proxy {
// Provides initialization parameters. Proxy origins, and the probe url are
// are taken from flags if available and from preprocessor constants otherwise.
// The DataReductionProxySettings class and others use this class to determine
// the necessary DNS names to configure use of the data reduction proxy.
class DataReductionProxyParams {
 public:
  // Flags used during construction that specify if the data reduction proxy
  // is allowed to be used, if the fallback proxy is allowed to be used, if
  // an alternative set of proxies is allowed to be used, if the promotion is
  // allowed to be shown, and if this instance is part of a holdback experiment.
  static const unsigned int kAllowed = (1 << 0);
  static const unsigned int kFallbackAllowed = (1 << 1);
  static const unsigned int kAlternativeAllowed = (1 << 2);
  static const unsigned int kPromoAllowed = (1 << 3);
  static const unsigned int kHoldback = (1 << 4);

  typedef std::vector<GURL> DataReductionProxyList;

  // Returns true if this client is part of the data reduction proxy field
  // trial.
  static bool IsIncludedInFieldTrial();

  // Returns true if this client is part of field trial to use an alternative
  // configuration for the data reduction proxy.
  static bool IsIncludedInAlternativeFieldTrial();

  // Returns true if this client is part of the field trial that should display
  // a promotion for the data reduction proxy.
  static bool IsIncludedInPromoFieldTrial();

  // Returns true if this client is part of a field trial that uses preconnect
  // hinting.
  static bool IsIncludedInPreconnectHintingFieldTrial();

  // Returns true if this client is part of a field trial that bypasses the
  // proxy if the request resource type is on the critical path (e.g. HTML).
  static bool IsIncludedInCriticalPathBypassFieldTrial();

  // Returns true if this client is part of a field trial that runs a holdback
  // experiment. A holdback experiment is one in which a fraction of browser
  // instances will not be configured to use the data reduction proxy even if
  // users have enabled it to be used. The UI will not indicate that a holdback
  // is in effect.
  static bool IsIncludedInHoldbackFieldTrial();

  // Constructs configuration parameters. If |kAllowed|, then the standard
  // data reduction proxy configuration is allowed to be used. If
  // |kfallbackAllowed| a fallback proxy can be used if the primary proxy is
  // bypassed or disabled. If |kAlternativeAllowed| then an alternative proxy
  // configuration is allowed to be used. This alternative configuration would
  // replace the primary and fallback proxy configurations if enabled. Finally
  // if |kPromoAllowed|, the client may show a promotion for the data reduction
  // proxy.
  //
  // A standard configuration has a primary proxy, and a fallback proxy for
  // HTTP traffic. The alternative configuration has a different primary and
  // fallback proxy for HTTP traffic, and an SSL proxy.

  DataReductionProxyParams(int flags);

  virtual ~DataReductionProxyParams();

  // Returns true if a data reduction proxy was used for the given |request|.
  // If true, |proxy_servers.first| will contain the name of the proxy that was
  // used. |proxy_servers.second| will contain the name of the data reduction
  // proxy server that would be used if |proxy_server.first| is bypassed, if one
  // exists. |proxy_servers| can be NULL if the caller isn't interested in its
  // values.
  virtual bool WasDataReductionProxyUsed(
      const net::URLRequest* request,
      std::pair<GURL, GURL>* proxy_servers) const;

  // Returns true if the specified |host_port_pair| matches a data reduction
  // proxy. If true, |proxy_servers.first| will contain the name of the proxy
  // that matches. |proxy_servers.second| will contain the name of the
  // data reduction proxy server that would be used if |proxy_server.first| is
  // bypassed, if one exists. |proxy_servers| can be NULL if the caller isn't
  // interested in its values. Virtual for testing.
  virtual bool IsDataReductionProxy(const net::HostPortPair& host_port_pair,
                                    std::pair<GURL, GURL>* proxy_servers) const;

  // Returns true if this request will be sent through the data request proxy
  // based on applying the param rules to the URL. We do not check bad proxy
  // list.
  virtual bool IsDataReductionProxyEligible(const net::URLRequest* request);

  // Checks if all configured data reduction proxies are in the retry map.
  // Returns true if the request is bypassed by all configured data reduction
  // proxies and returns the bypass delay in delay_seconds (if not NULL). If
  // there are no configured data reduction proxies, returns false. If
  // the request is bypassed by more than one proxy, delay_seconds returns
  // the shortest delay.
  bool AreDataReductionProxiesBypassed(const net::URLRequest& request,
                                       base::TimeDelta* min_retry_delay) const;

  // Checks if all configured data reduction proxies are in the retry map.
  // Returns true if the request is bypassed by all configured data reduction
  // proxies and returns the bypass delay in delay_seconds (if not NULL). If
  // there are no configured data reduction proxies, returns false. If
  // the request is bypassed by more than one proxy, delay_seconds returns
  // the shortest delay.
  bool AreProxiesBypassed(const net::ProxyRetryInfoMap& retry_map,
                          bool is_https,
                          base::TimeDelta* min_retry_delay) const;

  // Returns the data reduction proxy primary origin.
  const GURL& origin() const {
    return origin_;
  }

  // Returns the data reduction proxy fallback origin.
  const GURL& fallback_origin() const {
    return fallback_origin_;
  }

  // Returns the data reduction proxy ssl origin that is used with the
  // alternative proxy configuration.
  const GURL& ssl_origin() const {
    return ssl_origin_;
  }

  // Returns the alternative data reduction proxy primary origin.
  const GURL& alt_origin() const {
    return alt_origin_;
  }

  // Returns the alternative data reduction proxy fallback origin.
  const GURL& alt_fallback_origin() const {
    return alt_fallback_origin_;
  }

  // Returns the URL to probe to decide if the primary origin should be used.
  const GURL& probe_url() const {
    return probe_url_;
  }

  // Returns the URL to fetch to warm the data reduction proxy connection.
  const GURL& warmup_url() const {
    return warmup_url_;
  }

  // Returns true if the data reduction proxy configuration may be used.
  bool allowed() const {
    return allowed_;
  }

  // Returns true if the fallback proxy may be used.
  bool fallback_allowed() const {
    return fallback_allowed_;
  }

  // Returns true if the alternative data reduction proxy configuration may be
  // used.
  bool alternative_allowed() const {
    return alt_allowed_;
  }

  // Returns true if the data reduction proxy promo may be shown.
  // This is idependent of whether the data reduction proxy is allowed.
  // TODO(bengr): maybe tie to whether proxy is allowed.
  bool promo_allowed() const {
    return promo_allowed_;
  }

  // Returns true if the data reduction proxy should not actually use the
  // proxy if enabled.
  bool holdback() const {
    return holdback_;
  }

  // Given |allowed_|, |fallback_allowed_|, and |alt_allowed_|, returns the
  // list of data reduction proxies that may be used.
  DataReductionProxyList GetAllowedProxies() const;

  // Returns true if any proxy origins are set on the command line.
  bool is_configured_on_command_line() const {
    return configured_on_command_line_;
  }

 protected:
  // Test constructor that optionally won't call Init();
  DataReductionProxyParams(int flags,
                           bool should_call_init);

  // Initialize the values of the proxies, and probe URL, from command
  // line flags and preprocessor constants, and check that there are
  // corresponding definitions for the allowed configurations.
  bool Init(bool allowed, bool fallback_allowed, bool alt_allowed);

  // Initialize the values of the proxies, and probe URL from command
  // line flags and preprocessor constants.
  void InitWithoutChecks();

  // Returns the corresponding string from preprocessor constants if defined,
  // and an empty string otherwise.
  virtual std::string GetDefaultDevOrigin() const;
  virtual std::string GetDefaultOrigin() const;
  virtual std::string GetDefaultFallbackOrigin() const;
  virtual std::string GetDefaultSSLOrigin() const;
  virtual std::string GetDefaultAltOrigin() const;
  virtual std::string GetDefaultAltFallbackOrigin() const;
  virtual std::string GetDefaultProbeURL() const;
  virtual std::string GetDefaultWarmupURL() const;

 private:
  // Checks if the primary and fallback data reduction proxies are in the retry
  // map. Returns true if the request is bypassed by both data reduction
  // proxies and returns the shortest bypass delay in delay_seconds (if not
  // NULL). If the fallback proxy is not valid, returns true if primary proxy
  // was bypassed and returns its bypass delay.
  bool ArePrimaryAndFallbackBypassed(const net::ProxyRetryInfoMap& retry_map,
                                     const GURL& primary,
                                     const GURL& fallback,
                                     base::TimeDelta* min_retry_delay) const;
  GURL origin_;
  GURL fallback_origin_;
  GURL ssl_origin_;
  GURL alt_origin_;
  GURL alt_fallback_origin_;
  GURL probe_url_;
  GURL warmup_url_;

  bool allowed_;
  const bool fallback_allowed_;
  bool alt_allowed_;
  const bool promo_allowed_;
  bool holdback_;

  bool configured_on_command_line_;

  DISALLOW_COPY_AND_ASSIGN(DataReductionProxyParams);
};

}  // namespace data_reduction_proxy
#endif  // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_PARAMS_H_