e961246e8acffc0cc82cb4f9575d786fe1c488fe
[platform/framework/web/crosswalk.git] / src / components / data_reduction_proxy / browser / data_reduction_proxy_params.h
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.
4
5 #ifndef COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_PARAMS_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_PARAMS_H_
7
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 #include "base/memory/scoped_ptr.h"
13 #include "net/base/host_port_pair.h"
14 #include "net/proxy/proxy_retry_info.h"
15 #include "url/gurl.h"
16
17 namespace base {
18 class TimeDelta;
19 }
20
21 namespace net {
22 class ProxyConfig;
23 class URLRequest;
24 }
25
26 namespace data_reduction_proxy {
27
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;
36   bool is_fallback;
37   bool is_alternative;
38   bool is_ssl;
39 };
40
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 {
46  public:
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);
56
57   typedef std::vector<GURL> DataReductionProxyList;
58
59   // Returns true if this client is part of the data reduction proxy field
60   // trial.
61   static bool IsIncludedInFieldTrial();
62
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();
66
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();
70
71   // Returns true if this client is part of a field trial that uses preconnect
72   // hinting.
73   static bool IsIncludedInPreconnectHintingFieldTrial();
74
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();
78
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
83   // is in effect.
84   static bool IsIncludedInHoldbackFieldTrial();
85
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
93   // proxy.
94   //
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);
99
100   // Creates a copy of the configuration parameters.
101   scoped_ptr<DataReductionProxyParams> Clone();
102
103   virtual ~DataReductionProxyParams();
104
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;
116
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;
128
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
131   // request URL.
132   bool IsBypassedByDataReductionProxyLocalRules(
133       const net::URLRequest& request,
134       const net::ProxyConfig& data_reduction_proxy_config) const;
135
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;
144
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,
152                           bool is_https,
153                           base::TimeDelta* min_retry_delay) const;
154
155   // Returns the data reduction proxy primary origin.
156   const GURL& origin() const {
157     return origin_;
158   }
159
160   // Returns the data reduction proxy fallback origin.
161   const GURL& fallback_origin() const {
162     return fallback_origin_;
163   }
164
165   // Returns the data reduction proxy ssl origin that is used with the
166   // alternative proxy configuration.
167   const GURL& ssl_origin() const {
168     return ssl_origin_;
169   }
170
171   // Returns the alternative data reduction proxy primary origin.
172   const GURL& alt_origin() const {
173     return alt_origin_;
174   }
175
176   // Returns the alternative data reduction proxy fallback origin.
177   const GURL& alt_fallback_origin() const {
178     return alt_fallback_origin_;
179   }
180
181   // Returns the URL to probe to decide if the primary origin should be used.
182   const GURL& probe_url() const {
183     return probe_url_;
184   }
185
186   // Returns the URL to fetch to warm the data reduction proxy connection.
187   const GURL& warmup_url() const {
188     return warmup_url_;
189   }
190
191   // Returns true if the data reduction proxy configuration may be used.
192   bool allowed() const {
193     return allowed_;
194   }
195
196   // Returns true if the fallback proxy may be used.
197   bool fallback_allowed() const {
198     return fallback_allowed_;
199   }
200
201   // Returns true if the alternative data reduction proxy configuration may be
202   // used.
203   bool alternative_allowed() const {
204     return alt_allowed_;
205   }
206
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_;
212   }
213
214   // Returns true if the data reduction proxy should not actually use the
215   // proxy if enabled.
216   bool holdback() const {
217     return holdback_;
218   }
219
220   // Given |allowed_|, |fallback_allowed_|, and |alt_allowed_|, returns the
221   // list of data reduction proxies that may be used.
222   DataReductionProxyList GetAllowedProxies() const;
223
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_;
227   }
228
229  protected:
230   // Test constructor that optionally won't call Init();
231   DataReductionProxyParams(int flags,
232                            bool should_call_init);
233
234   DataReductionProxyParams(const DataReductionProxyParams& params);
235
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);
240
241   // Initialize the values of the proxies, and probe URL from command
242   // line flags and preprocessor constants.
243   void InitWithoutChecks();
244
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;
255
256  private:
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,
263                                      const GURL& primary,
264                                      const GURL& fallback,
265                                      base::TimeDelta* min_retry_delay) const;
266
267   DataReductionProxyParams& operator=(const DataReductionProxyParams& params);
268
269   GURL origin_;
270   GURL fallback_origin_;
271   GURL ssl_origin_;
272   GURL alt_origin_;
273   GURL alt_fallback_origin_;
274   GURL probe_url_;
275   GURL warmup_url_;
276
277   bool allowed_;
278   bool fallback_allowed_;
279   bool alt_allowed_;
280   bool promo_allowed_;
281   bool holdback_;
282
283   bool configured_on_command_line_;
284 };
285
286 }  // namespace data_reduction_proxy
287 #endif  // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_PARAMS_H_