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"
26 namespace data_reduction_proxy {
28 // Contains information about a given proxy server. |proxy_servers| contains
29 // the configured data reduction proxy servers. |is_fallback|, |is_alternative|
30 // and |is_ssl| note whether the given proxy is a fallback, an alternative,
31 // or a proxy for ssl; these are not mutually exclusive.
32 struct DataReductionProxyTypeInfo {
33 DataReductionProxyTypeInfo();
34 ~DataReductionProxyTypeInfo();
35 std::pair<GURL, GURL> proxy_servers;
41 // Provides initialization parameters. Proxy origins, and the probe url are
42 // are taken from flags if available and from preprocessor constants otherwise.
43 // The DataReductionProxySettings class and others use this class to determine
44 // the necessary DNS names to configure use of the data reduction proxy.
45 class DataReductionProxyParams {
47 // Flags used during construction that specify if the data reduction proxy
48 // is allowed to be used, if the fallback proxy is allowed to be used, if
49 // an alternative set of proxies is allowed to be used, if the promotion is
50 // allowed to be shown, and if this instance is part of a holdback experiment.
51 static const unsigned int kAllowed = (1 << 0);
52 static const unsigned int kFallbackAllowed = (1 << 1);
53 static const unsigned int kAlternativeAllowed = (1 << 2);
54 static const unsigned int kPromoAllowed = (1 << 3);
55 static const unsigned int kHoldback = (1 << 4);
57 typedef std::vector<GURL> DataReductionProxyList;
59 // Returns true if this client is part of the data reduction proxy field
61 static bool IsIncludedInFieldTrial();
63 // Returns true if this client is part of field trial to use an alternative
64 // configuration for the data reduction proxy.
65 static bool IsIncludedInAlternativeFieldTrial();
67 // Returns true if this client is part of the field trial that should display
68 // a promotion for the data reduction proxy.
69 static bool IsIncludedInPromoFieldTrial();
71 // Returns true if this client is part of a field trial that uses preconnect
73 static bool IsIncludedInPreconnectHintingFieldTrial();
75 // Returns true if this client is part of a field trial that bypasses the
76 // proxy if the request resource type is on the critical path (e.g. HTML).
77 static bool IsIncludedInCriticalPathBypassFieldTrial();
79 // Returns true if this client is part of a field trial that runs a holdback
80 // experiment. A holdback experiment is one in which a fraction of browser
81 // instances will not be configured to use the data reduction proxy even if
82 // users have enabled it to be used. The UI will not indicate that a holdback
84 static bool IsIncludedInHoldbackFieldTrial();
86 // Constructs configuration parameters. If |kAllowed|, then the standard
87 // data reduction proxy configuration is allowed to be used. If
88 // |kfallbackAllowed| a fallback proxy can be used if the primary proxy is
89 // bypassed or disabled. If |kAlternativeAllowed| then an alternative proxy
90 // configuration is allowed to be used. This alternative configuration would
91 // replace the primary and fallback proxy configurations if enabled. Finally
92 // if |kPromoAllowed|, the client may show a promotion for the data reduction
95 // A standard configuration has a primary proxy, and a fallback proxy for
96 // HTTP traffic. The alternative configuration has a different primary and
97 // fallback proxy for HTTP traffic, and an SSL proxy.
98 explicit DataReductionProxyParams(int flags);
100 // Creates a copy of the configuration parameters.
101 scoped_ptr<DataReductionProxyParams> Clone();
103 virtual ~DataReductionProxyParams();
105 // Returns true if a data reduction proxy was used for the given |request|.
106 // If true, |proxy_info.proxy_servers.first| will contain the name of the
107 // proxy that was used. |proxy_info.proxy_servers.second| will contain the
108 // name of the data reduction proxy server that would be used if
109 // |proxy_info.proxy_server.first| is bypassed, if one exists. In addition,
110 // |proxy_info| will note if the proxy used was a fallback, an alternative,
111 // or a proxy for ssl; these are not mutually exclusive. |proxy_info| can be
112 // NULL if the caller isn't interested in its values.
113 virtual bool WasDataReductionProxyUsed(
114 const net::URLRequest* request,
115 DataReductionProxyTypeInfo* proxy_info) const;
117 // Returns true if the specified |host_port_pair| matches a data reduction
118 // proxy. If true, |proxy_info.proxy_servers.first| will contain the name of
119 // the proxy that matches. |proxy_info.proxy_servers.second| will contain the
120 // name of the data reduction proxy server that would be used if
121 // |proxy_info.proxy_server.first| is bypassed, if one exists. In addition,
122 // |proxy_info| will note if the proxy was a fallback, an alternative, or a
123 // proxy for ssl; these are not mutually exclusive. |proxy_info| can be NULL
124 // if the caller isn't interested in its values. Virtual for testing.
125 virtual bool IsDataReductionProxy(
126 const net::HostPortPair& host_port_pair,
127 DataReductionProxyTypeInfo* proxy_info) const;
129 // Returns true if this request would be bypassed by the data request proxy
130 // based on applying the |data_reduction_proxy_config| param rules to the
132 bool IsBypassedByDataReductionProxyLocalRules(
133 const net::URLRequest& request,
134 const net::ProxyConfig& data_reduction_proxy_config) const;
136 // Checks if all configured data reduction proxies are in the retry map.
137 // Returns true if the request is bypassed by all configured data reduction
138 // proxies and returns the bypass delay in delay_seconds (if not NULL). If
139 // there are no configured data reduction proxies, returns false. If
140 // the request is bypassed by more than one proxy, delay_seconds returns
141 // the shortest delay.
142 bool AreDataReductionProxiesBypassed(const net::URLRequest& request,
143 base::TimeDelta* min_retry_delay) const;
145 // Checks if all configured data reduction proxies are in the retry map.
146 // Returns true if the request is bypassed by all configured data reduction
147 // proxies and returns the bypass delay in delay_seconds (if not NULL). If
148 // there are no configured data reduction proxies, returns false. If
149 // the request is bypassed by more than one proxy, delay_seconds returns
150 // the shortest delay.
151 bool AreProxiesBypassed(const net::ProxyRetryInfoMap& retry_map,
153 base::TimeDelta* min_retry_delay) const;
155 // Returns the data reduction proxy primary origin.
156 const GURL& origin() const {
160 // Returns the data reduction proxy fallback origin.
161 const GURL& fallback_origin() const {
162 return fallback_origin_;
165 // Returns the data reduction proxy ssl origin that is used with the
166 // alternative proxy configuration.
167 const GURL& ssl_origin() const {
171 // Returns the alternative data reduction proxy primary origin.
172 const GURL& alt_origin() const {
176 // Returns the alternative data reduction proxy fallback origin.
177 const GURL& alt_fallback_origin() const {
178 return alt_fallback_origin_;
181 // Returns the URL to probe to decide if the primary origin should be used.
182 const GURL& probe_url() const {
186 // Returns the URL to fetch to warm the data reduction proxy connection.
187 const GURL& warmup_url() const {
191 // Returns true if the data reduction proxy configuration may be used.
192 bool allowed() const {
196 // Returns true if the fallback proxy may be used.
197 bool fallback_allowed() const {
198 return fallback_allowed_;
201 // Returns true if the alternative data reduction proxy configuration may be
203 bool alternative_allowed() const {
207 // Returns true if the data reduction proxy promo may be shown.
208 // This is idependent of whether the data reduction proxy is allowed.
209 // TODO(bengr): maybe tie to whether proxy is allowed.
210 bool promo_allowed() const {
211 return promo_allowed_;
214 // Returns true if the data reduction proxy should not actually use the
216 bool holdback() const {
220 // Given |allowed_|, |fallback_allowed_|, and |alt_allowed_|, returns the
221 // list of data reduction proxies that may be used.
222 DataReductionProxyList GetAllowedProxies() const;
224 // Returns true if any proxy origins are set on the command line.
225 bool is_configured_on_command_line() const {
226 return configured_on_command_line_;
230 // Test constructor that optionally won't call Init();
231 DataReductionProxyParams(int flags,
232 bool should_call_init);
234 DataReductionProxyParams(const DataReductionProxyParams& params);
236 // Initialize the values of the proxies, and probe URL, from command
237 // line flags and preprocessor constants, and check that there are
238 // corresponding definitions for the allowed configurations.
239 bool Init(bool allowed, bool fallback_allowed, bool alt_allowed);
241 // Initialize the values of the proxies, and probe URL from command
242 // line flags and preprocessor constants.
243 void InitWithoutChecks();
245 // Returns the corresponding string from preprocessor constants if defined,
246 // and an empty string otherwise.
247 virtual std::string GetDefaultDevOrigin() const;
248 virtual std::string GetDefaultOrigin() const;
249 virtual std::string GetDefaultFallbackOrigin() const;
250 virtual std::string GetDefaultSSLOrigin() const;
251 virtual std::string GetDefaultAltOrigin() const;
252 virtual std::string GetDefaultAltFallbackOrigin() const;
253 virtual std::string GetDefaultProbeURL() const;
254 virtual std::string GetDefaultWarmupURL() const;
257 // Checks if the primary and fallback data reduction proxies are in the retry
258 // map. Returns true if the request is bypassed by both data reduction
259 // proxies and returns the shortest bypass delay in delay_seconds (if not
260 // NULL). If the fallback proxy is not valid, returns true if primary proxy
261 // was bypassed and returns its bypass delay.
262 bool ArePrimaryAndFallbackBypassed(const net::ProxyRetryInfoMap& retry_map,
264 const GURL& fallback,
265 base::TimeDelta* min_retry_delay) const;
267 DataReductionProxyParams& operator=(const DataReductionProxyParams& params);
270 GURL fallback_origin_;
273 GURL alt_fallback_origin_;
278 bool fallback_allowed_;
283 bool configured_on_command_line_;
286 } // namespace data_reduction_proxy
287 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_PARAMS_H_