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 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
6
7 #include "base/command_line.h"
8 #include "base/metrics/field_trial.h"
9 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h"
10 #include "net/url_request/url_request.h"
11
12 using base::FieldTrialList;
13
14 namespace {
15 const char kEnabled[] = "Enabled";
16 }
17
18 namespace data_reduction_proxy {
19
20 // static
IsIncludedInFieldTrial()21 bool DataReductionProxyParams::IsIncludedInFieldTrial() {
22 return base::FieldTrialList::FindFullName(
23 "DataCompressionProxyRollout") == kEnabled;
24 }
25
26 // static
IsIncludedInAlternativeFieldTrial()27 bool DataReductionProxyParams::IsIncludedInAlternativeFieldTrial() {
28 return base::FieldTrialList::FindFullName(
29 "DataCompressionProxyAlternativeConfiguration") == kEnabled;
30 }
31
32 // static
IsIncludedInPromoFieldTrial()33 bool DataReductionProxyParams::IsIncludedInPromoFieldTrial() {
34 return FieldTrialList::FindFullName(
35 "DataCompressionProxyPromoVisibility") == kEnabled;
36 }
37
38 // static
IsIncludedInPreconnectHintingFieldTrial()39 bool DataReductionProxyParams::IsIncludedInPreconnectHintingFieldTrial() {
40 return IsIncludedInFieldTrial() &&
41 FieldTrialList::FindFullName(
42 "DataCompressionProxyPreconnectHints") == kEnabled;
43 }
44
45 // static
IsKeySetOnCommandLine()46 bool DataReductionProxyParams::IsKeySetOnCommandLine() {
47 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
48 return command_line.HasSwitch(
49 data_reduction_proxy::switches::kEnableDataReductionProxy);
50 }
51
DataReductionProxyParams(int flags)52 DataReductionProxyParams::DataReductionProxyParams(int flags)
53 : allowed_((flags & kAllowed) == kAllowed),
54 fallback_allowed_((flags & kFallbackAllowed) == kFallbackAllowed),
55 alt_allowed_((flags & kAlternativeAllowed) == kAlternativeAllowed),
56 promo_allowed_((flags & kPromoAllowed) == kPromoAllowed) {
57 bool result = Init(allowed_, fallback_allowed_, alt_allowed_);
58 DCHECK(result);
59 }
60
~DataReductionProxyParams()61 DataReductionProxyParams::~DataReductionProxyParams() {
62 }
63
64 DataReductionProxyParams::DataReductionProxyList
GetAllowedProxies() const65 DataReductionProxyParams::GetAllowedProxies() const {
66 DataReductionProxyList list;
67 if (allowed_)
68 list.push_back(origin_);
69 if (allowed_ && fallback_allowed_)
70 list.push_back(fallback_origin_);
71 if (alt_allowed_) {
72 list.push_back(alt_origin_);
73 list.push_back(ssl_origin_);
74 }
75 if (alt_allowed_ && fallback_allowed_)
76 list.push_back(alt_fallback_origin_);
77 return list;
78 }
79
DataReductionProxyParams(int flags,bool should_call_init)80 DataReductionProxyParams::DataReductionProxyParams(int flags,
81 bool should_call_init)
82 : allowed_((flags & kAllowed) == kAllowed),
83 fallback_allowed_((flags & kFallbackAllowed) == kFallbackAllowed),
84 alt_allowed_((flags & kAlternativeAllowed) == kAlternativeAllowed),
85 promo_allowed_((flags & kPromoAllowed) == kPromoAllowed) {
86 if (should_call_init) {
87 bool result = Init(allowed_, fallback_allowed_, alt_allowed_);
88 DCHECK(result);
89 }
90 }
91
Init(bool allowed,bool fallback_allowed,bool alt_allowed)92 bool DataReductionProxyParams::Init(
93 bool allowed, bool fallback_allowed, bool alt_allowed) {
94 InitWithoutChecks();
95 // Verify that all necessary params are set.
96 if (allowed) {
97 if (!origin_.is_valid()) {
98 DVLOG(1) << "Invalid data reduction proxy origin: " << origin_.spec();
99 return false;
100 }
101 }
102
103 if (allowed && fallback_allowed) {
104 if (!fallback_origin_.is_valid()) {
105 DVLOG(1) << "Invalid data reduction proxy fallback origin: "
106 << fallback_origin_.spec();
107 return false;
108 }
109 }
110
111 if (alt_allowed) {
112 if (!allowed) {
113 DVLOG(1) << "Alternative data reduction proxy configuration cannot "
114 << "be allowed if the regular configuration is not allowed";
115 return false;
116 }
117 if (!alt_origin_.is_valid()) {
118 DVLOG(1) << "Invalid alternative origin:" << alt_origin_.spec();
119 return false;
120 }
121 if (!ssl_origin_.is_valid()) {
122 DVLOG(1) << "Invalid ssl origin: " << ssl_origin_.spec();
123 return false;
124 }
125 }
126
127 if (alt_allowed && fallback_allowed) {
128 if (!alt_fallback_origin_.is_valid()) {
129 DVLOG(1) << "Invalid alternative fallback origin:"
130 << alt_fallback_origin_.spec();
131 return false;
132 }
133 }
134
135 if (allowed && !probe_url_.is_valid()) {
136 DVLOG(1) << "Invalid probe url: <null>";
137 return false;
138 }
139
140 if (fallback_allowed_ && !allowed_) {
141 DVLOG(1) << "The data reduction proxy fallback cannot be allowed if "
142 << "the data reduction proxy is not allowed";
143 return false;
144 }
145 if (promo_allowed_ && !allowed_) {
146 DVLOG(1) << "The data reduction proxy promo cannot be allowed if the "
147 << "data reduction proxy is not allowed";
148 return false;
149 }
150 return true;
151
152 }
153
154
InitWithoutChecks()155 void DataReductionProxyParams::InitWithoutChecks() {
156 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
157 std::string origin;
158 if (!command_line.HasSwitch(switches::kDisableDataReductionProxyDev)) {
159 origin = command_line.GetSwitchValueASCII(
160 switches::kDataReductionProxyDev);
161 }
162 if (origin.empty())
163 origin = command_line.GetSwitchValueASCII(switches::kDataReductionProxy);
164 std::string fallback_origin =
165 command_line.GetSwitchValueASCII(switches::kDataReductionProxyFallback);
166 std::string ssl_origin =
167 command_line.GetSwitchValueASCII(switches::kDataReductionSSLProxy);
168 std::string alt_origin =
169 command_line.GetSwitchValueASCII(switches::kDataReductionProxyAlt);
170 std::string alt_fallback_origin = command_line.GetSwitchValueASCII(
171 switches::kDataReductionProxyAltFallback);
172 key_ = command_line.GetSwitchValueASCII(switches::kDataReductionProxyKey);
173
174 bool configured_on_command_line =
175 !(origin.empty() && fallback_origin.empty() && ssl_origin.empty() &&
176 alt_origin.empty() && alt_fallback_origin.empty());
177
178
179 // Configuring the proxy on the command line overrides the values of
180 // |allowed_| and |alt_allowed_|.
181 if (configured_on_command_line)
182 allowed_ = true;
183 if (!(ssl_origin.empty() &&
184 alt_origin.empty() &&
185 alt_fallback_origin.empty()))
186 alt_allowed_ = true;
187
188 // Only use default key if non of the proxies are configured on the command
189 // line.
190 if (key_.empty() && !configured_on_command_line)
191 key_ = GetDefaultKey();
192
193 std::string probe_url = command_line.GetSwitchValueASCII(
194 switches::kDataReductionProxyProbeURL);
195 std::string warmup_url = command_line.GetSwitchValueASCII(
196 switches::kDataReductionProxyWarmupURL);
197
198 // Set from preprocessor constants those params that are not specified on the
199 // command line.
200 if (origin.empty())
201 origin = GetDefaultDevOrigin();
202 if (origin.empty())
203 origin = GetDefaultOrigin();
204 if (fallback_origin.empty())
205 fallback_origin = GetDefaultFallbackOrigin();
206 if (ssl_origin.empty())
207 ssl_origin = GetDefaultSSLOrigin();
208 if (alt_origin.empty())
209 alt_origin = GetDefaultAltOrigin();
210 if (alt_fallback_origin.empty())
211 alt_fallback_origin = GetDefaultAltFallbackOrigin();
212 if (probe_url.empty())
213 probe_url = GetDefaultProbeURL();
214 if (warmup_url.empty())
215 warmup_url = GetDefaultWarmupURL();
216
217 origin_ = GURL(origin);
218 fallback_origin_ = GURL(fallback_origin);
219 ssl_origin_ = GURL(ssl_origin);
220 alt_origin_ = GURL(alt_origin);
221 alt_fallback_origin_ = GURL(alt_fallback_origin);
222 probe_url_ = GURL(probe_url);
223 warmup_url_ = GURL(warmup_url);
224
225 }
226
WasDataReductionProxyUsed(const net::URLRequest * request,std::pair<GURL,GURL> * proxy_servers) const227 bool DataReductionProxyParams::WasDataReductionProxyUsed(
228 const net::URLRequest* request,
229 std::pair<GURL, GURL>* proxy_servers) const {
230 DCHECK(request);
231 return IsDataReductionProxy(request->proxy_server(), proxy_servers);
232 }
233
IsDataReductionProxy(const net::HostPortPair & host_port_pair,std::pair<GURL,GURL> * proxy_servers) const234 bool DataReductionProxyParams::IsDataReductionProxy(
235 const net::HostPortPair& host_port_pair,
236 std::pair<GURL, GURL>* proxy_servers) const {
237 if (net::HostPortPair::FromURL(origin()).Equals(host_port_pair)) {
238 if (proxy_servers) {
239 (*proxy_servers).first = origin();
240 if (fallback_allowed())
241 (*proxy_servers).second = fallback_origin();
242 }
243 return true;
244 }
245 if (fallback_allowed() &&
246 net::HostPortPair::FromURL(fallback_origin()).Equals(host_port_pair)) {
247 if (proxy_servers) {
248 (*proxy_servers).first = fallback_origin();
249 (*proxy_servers).second = GURL();
250 }
251 return true;
252 }
253 if (net::HostPortPair::FromURL(alt_origin()).Equals(host_port_pair)) {
254 if (proxy_servers) {
255 (*proxy_servers).first = alt_origin();
256 if (fallback_allowed())
257 (*proxy_servers).second = alt_fallback_origin();
258 }
259 return true;
260 }
261 if (fallback_allowed() &&
262 net::HostPortPair::FromURL(alt_fallback_origin()).Equals(
263 host_port_pair)) {
264 if (proxy_servers) {
265 (*proxy_servers).first = alt_fallback_origin();
266 (*proxy_servers).second = GURL();
267 }
268 return true;
269 }
270 if (net::HostPortPair::FromURL(ssl_origin()).Equals(host_port_pair)) {
271 if (proxy_servers) {
272 (*proxy_servers).first = ssl_origin();
273 (*proxy_servers).second = GURL();
274 }
275 return true;
276 }
277 return false;
278 }
279
GetDefaultKey() const280 std::string DataReductionProxyParams::GetDefaultKey() const {
281 #if defined(SPDY_PROXY_AUTH_VALUE)
282 return SPDY_PROXY_AUTH_VALUE;
283 #endif
284 return std::string();
285 }
286
GetDefaultDevOrigin() const287 std::string DataReductionProxyParams::GetDefaultDevOrigin() const {
288 #if defined(DATA_REDUCTION_DEV_HOST)
289 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
290 if (command_line.HasSwitch(switches::kDisableDataReductionProxyDev))
291 return std::string();
292 if (command_line.HasSwitch(switches::kEnableDataReductionProxyDev) ||
293 (FieldTrialList::FindFullName("DataCompressionProxyDevRollout") ==
294 kEnabled)) {
295 return DATA_REDUCTION_DEV_HOST;
296 }
297 #endif
298 return std::string();
299 }
300
GetDefaultOrigin() const301 std::string DataReductionProxyParams::GetDefaultOrigin() const {
302 #if defined(SPDY_PROXY_AUTH_ORIGIN)
303 return SPDY_PROXY_AUTH_ORIGIN;
304 #endif
305 return std::string();
306 }
307
GetDefaultFallbackOrigin() const308 std::string DataReductionProxyParams::GetDefaultFallbackOrigin() const {
309 #if defined(DATA_REDUCTION_FALLBACK_HOST)
310 return DATA_REDUCTION_FALLBACK_HOST;
311 #endif
312 return std::string();
313 }
314
GetDefaultSSLOrigin() const315 std::string DataReductionProxyParams::GetDefaultSSLOrigin() const {
316 #if defined(DATA_REDUCTION_PROXY_SSL_ORIGIN)
317 return DATA_REDUCTION_PROXY_SSL_ORIGIN;
318 #endif
319 return std::string();
320 }
321
GetDefaultAltOrigin() const322 std::string DataReductionProxyParams::GetDefaultAltOrigin() const {
323 #if defined(DATA_REDUCTION_PROXY_ALT_ORIGIN)
324 return DATA_REDUCTION_PROXY_ALT_ORIGIN;
325 #endif
326 return std::string();
327 }
328
GetDefaultAltFallbackOrigin() const329 std::string DataReductionProxyParams::GetDefaultAltFallbackOrigin() const {
330 #if defined(DATA_REDUCTION_PROXY_ALT_FALLBACK_ORIGIN)
331 return DATA_REDUCTION_PROXY_ALT_FALLBACK_ORIGIN;
332 #endif
333 return std::string();
334 }
335
GetDefaultProbeURL() const336 std::string DataReductionProxyParams::GetDefaultProbeURL() const {
337 #if defined(DATA_REDUCTION_PROXY_PROBE_URL)
338 return DATA_REDUCTION_PROXY_PROBE_URL;
339 #endif
340 return std::string();
341 }
342
GetDefaultWarmupURL() const343 std::string DataReductionProxyParams::GetDefaultWarmupURL() const {
344 #if defined(DATA_REDUCTION_PROXY_WARMUP_URL)
345 return DATA_REDUCTION_PROXY_WARMUP_URL;
346 #endif
347 return std::string();
348 }
349
350 } // namespace data_reduction_proxy
351