• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/macros.h"
13 #include "net/base/host_port_pair.h"
14 #include "url/gurl.h"
15 
16 namespace net {
17 class URLRequest;
18 }
19 
20 namespace data_reduction_proxy {
21 // Provides initialization parameters. Proxy origins, the probe url, and the
22 // authentication key are taken from flags if available and from preprocessor
23 // constants otherwise. Only the key may be changed after construction. The
24 // DataReductionProxySettings class and others use this class to determine
25 // the necessary DNS names and keys to configure use of the data reduction
26 // proxy.
27 class DataReductionProxyParams {
28  public:
29   static const unsigned int kAllowed = (1 << 0);
30   static const unsigned int kFallbackAllowed = (1 << 1);
31   static const unsigned int kAlternativeAllowed = (1 << 2);
32   static const unsigned int kPromoAllowed = (1 << 3);
33 
34   typedef std::vector<GURL> DataReductionProxyList;
35 
36   // Returns true if this client is part of the data reduction proxy field
37   // trial.
38   static bool IsIncludedInFieldTrial();
39 
40   // Returns true if this client is part of field trial to use an alternative
41   // configuration for the data reduction proxy.
42   static bool IsIncludedInAlternativeFieldTrial();
43 
44   // Returns true if this client is part of the field trial that should display
45   // a promotion for the data reduction proxy.
46   static bool IsIncludedInPromoFieldTrial();
47 
48   // Returns true if this client is part of a field trial that uses preconnect
49   // hinting.
50   static bool IsIncludedInPreconnectHintingFieldTrial();
51 
52   // Returns true if the authentication key was set on the command line.
53   static bool IsKeySetOnCommandLine();
54 
55   // Constructs configuration parameters. If |kAllowed|, then the standard
56   // data reduction proxy configuration is allowed to be used. If
57   // |kfallbackAllowed| a fallback proxy can be used if the primary proxy is
58   // bypassed or disabled. If |kAlternativeAllowed| then an alternative proxy
59   // configuration is allowed to be used. This alternative configuration would
60   // replace the primary and fallback proxy configurations if enabled. Finally
61   // if |kPromoAllowed|, the client may show a promotion for the data reduction
62   // proxy.
63   //
64   // A standard configuration has a primary proxy, and a fallback proxy for
65   // HTTP traffic. The alternative configuration has a different primary and
66   // fallback proxy for HTTP traffic, and an SSL proxy.
67 
68   DataReductionProxyParams(int flags);
69 
70   virtual ~DataReductionProxyParams();
71 
72   // Returns true if a data reduction proxy was used for the given |request|.
73   // If true, |proxy_servers.first| will contain the name of the proxy that was
74   // used. |proxy_servers.second| will contain the name of the data reduction
75   // proxy server that would be used if |proxy_server.first| is bypassed, if one
76   // exists. |proxy_servers| can be NULL if the caller isn't interested in its
77   // values.
78   virtual bool WasDataReductionProxyUsed(
79       const net::URLRequest* request,
80       std::pair<GURL, GURL>* proxy_servers) const;
81 
82   // Returns true if the specified |host_port_pair| matches a data reduction
83   // proxy. If true, |proxy_servers.first| will contain the name of the proxy
84   // that matches. |proxy_servers.second| will contain the name of the
85   // data reduction proxy server that would be used if |proxy_server.first| is
86   // bypassed, if one exists. |proxy_servers| can be NULL if the caller isn't
87   // interested in its values.
88   bool IsDataReductionProxy(const net::HostPortPair& host_port_pair,
89                             std::pair<GURL, GURL>* proxy_servers) const;
90 
91   // Returns the data reduction proxy primary origin.
origin()92   const GURL& origin() const {
93     return origin_;
94   }
95 
96   // Returns the data reduction proxy fallback origin.
fallback_origin()97   const GURL& fallback_origin() const {
98     return fallback_origin_;
99   }
100 
101   // Returns the data reduction proxy ssl origin that is used with the
102   // alternative proxy configuration.
ssl_origin()103   const GURL& ssl_origin() const {
104     return ssl_origin_;
105   }
106 
107   // Returns the alternative data reduction proxy primary origin.
alt_origin()108   const GURL& alt_origin() const {
109     return alt_origin_;
110   }
111 
112   // Returns the alternative data reduction proxy fallback origin.
alt_fallback_origin()113   const GURL& alt_fallback_origin() const {
114     return alt_fallback_origin_;
115   }
116 
117   // Returns the URL to probe to decide if the primary origin should be used.
probe_url()118   const GURL& probe_url() const {
119     return probe_url_;
120   }
121 
122   // Returns the URL to fetch to warm the data reduction proxy connection.
warmup_url()123   const GURL& warmup_url() const {
124     return warmup_url_;
125   }
126 
127   // Set the proxy authentication key.
set_key(const std::string & key)128   void set_key(const std::string& key) {
129     key_ = key;
130   }
131 
132   // Returns the proxy authentication key.
key()133   const std::string& key() const {
134     return key_;
135   }
136 
137   // Returns true if the data reduction proxy configuration may be used.
allowed()138   bool allowed() const {
139     return allowed_;
140   }
141 
142   // Returns true if the fallback proxy may be used.
fallback_allowed()143   bool fallback_allowed() const {
144     return fallback_allowed_;
145   }
146 
147   // Returns true if the alternative data reduction proxy configuration may be
148   // used.
alternative_allowed()149   bool alternative_allowed() const {
150     return alt_allowed_;
151   }
152 
153   // Returns true if the data reduction proxy promo may be shown.
154   // This is idependent of whether the data reduction proxy is allowed.
155   // TODO(bengr): maybe tie to whether proxy is allowed.
promo_allowed()156   bool promo_allowed() const {
157     return promo_allowed_;
158   }
159 
160   // Given |allowed_|, |fallback_allowed_|, and |alt_allowed_|, returns the
161   // list of data reduction proxies that may be used.
162   DataReductionProxyList GetAllowedProxies() const;
163 
164  protected:
165   // Test constructor that optionally won't call Init();
166   DataReductionProxyParams(int flags,
167                            bool should_call_init);
168 
169   // Initialize the values of the proxies, probe URL, and key from command
170   // line flags and preprocessor constants, and check that there are
171   // corresponding definitions for the allowed configurations.
172   bool Init(bool allowed, bool fallback_allowed, bool alt_allowed);
173 
174   // Initialize the values of the proxies, probe URL, and key from command
175   // line flags and preprocessor constants.
176   void InitWithoutChecks();
177 
178   // Returns the corresponding string from preprocessor constants if defined,
179   // and an empty string otherwise.
180   virtual std::string GetDefaultKey() const;
181   virtual std::string GetDefaultDevOrigin() const;
182   virtual std::string GetDefaultOrigin() const;
183   virtual std::string GetDefaultFallbackOrigin() const;
184   virtual std::string GetDefaultSSLOrigin() const;
185   virtual std::string GetDefaultAltOrigin() const;
186   virtual std::string GetDefaultAltFallbackOrigin() const;
187   virtual std::string GetDefaultProbeURL() const;
188   virtual std::string GetDefaultWarmupURL() const;
189 
190  private:
191   GURL origin_;
192   GURL fallback_origin_;
193   GURL ssl_origin_;
194   GURL alt_origin_;
195   GURL alt_fallback_origin_;
196   GURL probe_url_;
197   GURL warmup_url_;
198 
199   std::string key_;
200 
201   bool allowed_;
202   const bool fallback_allowed_;
203   bool alt_allowed_;
204   const bool promo_allowed_;
205 
206   DISALLOW_COPY_AND_ASSIGN(DataReductionProxyParams);
207 };
208 
209 }  // namespace data_reduction_proxy
210 #endif  // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_PARAMS_H_
211