1 // Copyright 2014 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.
5 #ifndef COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_PARAMS_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_PARAMS_H_
12 #include "base/memory/scoped_ptr.h"
13 #include "net/base/host_port_pair.h"
14 #include "net/proxy/proxy_retry_info.h"
25 namespace data_reduction_proxy
{
27 // Contains information about a given proxy server. |proxy_servers| contains
28 // the configured data reduction proxy servers. |is_fallback|, |is_alternative|
29 // and |is_ssl| note whether the given proxy is a fallback, an alternative,
30 // or a proxy for ssl; these are not mutually exclusive.
31 struct DataReductionProxyTypeInfo
{
32 DataReductionProxyTypeInfo();
33 ~DataReductionProxyTypeInfo();
34 std::pair
<GURL
, GURL
> proxy_servers
;
40 // Provides initialization parameters. Proxy origins, and the probe url are
41 // are taken from flags if available and from preprocessor constants otherwise.
42 // The DataReductionProxySettings class and others use this class to determine
43 // the necessary DNS names to configure use of the data reduction proxy.
44 class DataReductionProxyParams
{
46 // Flags used during construction that specify if the data reduction proxy
47 // is allowed to be used, if the fallback proxy is allowed to be used, if
48 // an alternative set of proxies is allowed to be used, if the promotion is
49 // allowed to be shown, and if this instance is part of a holdback experiment.
50 static const unsigned int kAllowed
= (1 << 0);
51 static const unsigned int kFallbackAllowed
= (1 << 1);
52 static const unsigned int kAlternativeAllowed
= (1 << 2);
53 static const unsigned int kPromoAllowed
= (1 << 3);
54 static const unsigned int kHoldback
= (1 << 4);
56 typedef std::vector
<GURL
> DataReductionProxyList
;
58 // Returns true if this client is part of the data reduction proxy field
60 static bool IsIncludedInFieldTrial();
62 // Returns true if this client is part of field trial to use an alternative
63 // configuration for the data reduction proxy.
64 static bool IsIncludedInAlternativeFieldTrial();
66 // Returns true if this client is part of the field trial that should display
67 // a promotion for the data reduction proxy.
68 static bool IsIncludedInPromoFieldTrial();
70 // Returns true if this client is part of a field trial that uses preconnect
72 static bool IsIncludedInPreconnectHintingFieldTrial();
74 // Returns true if this client is part of a field trial that bypasses the
75 // proxy if the request resource type is on the critical path (e.g. HTML).
76 static bool IsIncludedInCriticalPathBypassFieldTrial();
78 // Returns true if this client is part of a field trial that runs a holdback
79 // experiment. A holdback experiment is one in which a fraction of browser
80 // instances will not be configured to use the data reduction proxy even if
81 // users have enabled it to be used. The UI will not indicate that a holdback
83 static bool IsIncludedInHoldbackFieldTrial();
85 // Constructs configuration parameters. If |kAllowed|, then the standard
86 // data reduction proxy configuration is allowed to be used. If
87 // |kfallbackAllowed| a fallback proxy can be used if the primary proxy is
88 // bypassed or disabled. If |kAlternativeAllowed| then an alternative proxy
89 // configuration is allowed to be used. This alternative configuration would
90 // replace the primary and fallback proxy configurations if enabled. Finally
91 // if |kPromoAllowed|, the client may show a promotion for the data reduction
94 // A standard configuration has a primary proxy, and a fallback proxy for
95 // HTTP traffic. The alternative configuration has a different primary and
96 // fallback proxy for HTTP traffic, and an SSL proxy.
97 explicit DataReductionProxyParams(int flags
);
99 // Creates a copy of the configuration parameters.
100 scoped_ptr
<DataReductionProxyParams
> Clone();
102 virtual ~DataReductionProxyParams();
104 // Returns true if a data reduction proxy was used for the given |request|.
105 // If true, |proxy_info.proxy_servers.first| will contain the name of the
106 // proxy that was used. |proxy_info.proxy_servers.second| will contain the
107 // name of the data reduction proxy server that would be used if
108 // |proxy_info.proxy_server.first| is bypassed, if one exists. In addition,
109 // |proxy_info| will note if the proxy used was a fallback, an alternative,
110 // or a proxy for ssl; these are not mutually exclusive. |proxy_info| can be
111 // NULL if the caller isn't interested in its values.
112 virtual bool WasDataReductionProxyUsed(
113 const net::URLRequest
* request
,
114 DataReductionProxyTypeInfo
* proxy_info
) const;
116 // Returns true if the specified |host_port_pair| matches a data reduction
117 // proxy. If true, |proxy_info.proxy_servers.first| will contain the name of
118 // the proxy that matches. |proxy_info.proxy_servers.second| will contain the
119 // name of the data reduction proxy server that would be used if
120 // |proxy_info.proxy_server.first| is bypassed, if one exists. In addition,
121 // |proxy_info| will note if the proxy was a fallback, an alternative, or a
122 // proxy for ssl; these are not mutually exclusive. |proxy_info| can be NULL
123 // if the caller isn't interested in its values. Virtual for testing.
124 virtual bool IsDataReductionProxy(
125 const net::HostPortPair
& host_port_pair
,
126 DataReductionProxyTypeInfo
* proxy_info
) const;
128 // Returns true if this request will be sent through the data request proxy
129 // based on applying the param rules to the URL. We do not check bad proxy
131 virtual bool IsDataReductionProxyEligible(const net::URLRequest
* request
);
133 // Checks if all configured data reduction proxies are in the retry map.
134 // Returns true if the request is bypassed by all configured data reduction
135 // proxies and returns the bypass delay in delay_seconds (if not NULL). If
136 // there are no configured data reduction proxies, returns false. If
137 // the request is bypassed by more than one proxy, delay_seconds returns
138 // the shortest delay.
139 bool AreDataReductionProxiesBypassed(const net::URLRequest
& request
,
140 base::TimeDelta
* min_retry_delay
) const;
142 // Checks if all configured data reduction proxies are in the retry map.
143 // Returns true if the request is bypassed by all configured data reduction
144 // proxies and returns the bypass delay in delay_seconds (if not NULL). If
145 // there are no configured data reduction proxies, returns false. If
146 // the request is bypassed by more than one proxy, delay_seconds returns
147 // the shortest delay.
148 bool AreProxiesBypassed(const net::ProxyRetryInfoMap
& retry_map
,
150 base::TimeDelta
* min_retry_delay
) const;
152 // Returns the data reduction proxy primary origin.
153 const GURL
& origin() const {
157 // Returns the data reduction proxy fallback origin.
158 const GURL
& fallback_origin() const {
159 return fallback_origin_
;
162 // Returns the data reduction proxy ssl origin that is used with the
163 // alternative proxy configuration.
164 const GURL
& ssl_origin() const {
168 // Returns the alternative data reduction proxy primary origin.
169 const GURL
& alt_origin() const {
173 // Returns the alternative data reduction proxy fallback origin.
174 const GURL
& alt_fallback_origin() const {
175 return alt_fallback_origin_
;
178 // Returns the URL to probe to decide if the primary origin should be used.
179 const GURL
& probe_url() const {
183 // Returns the URL to fetch to warm the data reduction proxy connection.
184 const GURL
& warmup_url() const {
188 // Returns true if the data reduction proxy configuration may be used.
189 bool allowed() const {
193 // Returns true if the fallback proxy may be used.
194 bool fallback_allowed() const {
195 return fallback_allowed_
;
198 // Returns true if the alternative data reduction proxy configuration may be
200 bool alternative_allowed() const {
204 // Returns true if the data reduction proxy promo may be shown.
205 // This is idependent of whether the data reduction proxy is allowed.
206 // TODO(bengr): maybe tie to whether proxy is allowed.
207 bool promo_allowed() const {
208 return promo_allowed_
;
211 // Returns true if the data reduction proxy should not actually use the
213 bool holdback() const {
217 // Given |allowed_|, |fallback_allowed_|, and |alt_allowed_|, returns the
218 // list of data reduction proxies that may be used.
219 DataReductionProxyList
GetAllowedProxies() const;
221 // Returns true if any proxy origins are set on the command line.
222 bool is_configured_on_command_line() const {
223 return configured_on_command_line_
;
227 // Test constructor that optionally won't call Init();
228 DataReductionProxyParams(int flags
,
229 bool should_call_init
);
231 DataReductionProxyParams(const DataReductionProxyParams
& params
);
233 // Initialize the values of the proxies, and probe URL, from command
234 // line flags and preprocessor constants, and check that there are
235 // corresponding definitions for the allowed configurations.
236 bool Init(bool allowed
, bool fallback_allowed
, bool alt_allowed
);
238 // Initialize the values of the proxies, and probe URL from command
239 // line flags and preprocessor constants.
240 void InitWithoutChecks();
242 // Returns the corresponding string from preprocessor constants if defined,
243 // and an empty string otherwise.
244 virtual std::string
GetDefaultDevOrigin() const;
245 virtual std::string
GetDefaultOrigin() const;
246 virtual std::string
GetDefaultFallbackOrigin() const;
247 virtual std::string
GetDefaultSSLOrigin() const;
248 virtual std::string
GetDefaultAltOrigin() const;
249 virtual std::string
GetDefaultAltFallbackOrigin() const;
250 virtual std::string
GetDefaultProbeURL() const;
251 virtual std::string
GetDefaultWarmupURL() const;
254 // Checks if the primary and fallback data reduction proxies are in the retry
255 // map. Returns true if the request is bypassed by both data reduction
256 // proxies and returns the shortest bypass delay in delay_seconds (if not
257 // NULL). If the fallback proxy is not valid, returns true if primary proxy
258 // was bypassed and returns its bypass delay.
259 bool ArePrimaryAndFallbackBypassed(const net::ProxyRetryInfoMap
& retry_map
,
261 const GURL
& fallback
,
262 base::TimeDelta
* min_retry_delay
) const;
264 DataReductionProxyParams
& operator=(const DataReductionProxyParams
& params
);
267 GURL fallback_origin_
;
270 GURL alt_fallback_origin_
;
275 bool fallback_allowed_
;
280 bool configured_on_command_line_
;
283 } // namespace data_reduction_proxy
284 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_PARAMS_H_