1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_member.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prefs/proxy_prefs.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "crypto/random.h"
#include "net/base/auth.h"
#include "net/base/host_port_pair.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/http/http_auth.h"
#include "net/http/http_auth_cache.h"
#include "net/http/http_network_session.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_status.h"
#include "url/gurl.h"
using base::FieldTrialList;
using base::StringPrintf;
namespace {
// Key of the UMA DataReductionProxy.StartupState histogram.
const char kUMAProxyStartupStateHistogram[] =
"DataReductionProxy.StartupState";
// Values of the UMA DataReductionProxy.StartupState histogram.
enum ProxyStartupState {
PROXY_NOT_AVAILABLE = 0,
PROXY_DISABLED,
PROXY_ENABLED,
PROXY_STARTUP_STATE_COUNT,
};
// Key of the UMA DataReductionProxy.ProbeURL histogram.
const char kUMAProxyProbeURL[] = "DataReductionProxy.ProbeURL";
// Values of the UMA DataReductionProxy.ProbeURL histogram.
// This enum must remain synchronized with DataReductionProxyProbeURLFetchResult
// in metrics/histograms/histograms.xml.
enum ProbeURLFetchResult {
// The probe failed because the internet was disconnected.
INTERNET_DISCONNECTED = 0,
// The probe failed for any other reason, and as a result, the proxy was
// disabled.
FAILED_PROXY_DISABLED,
// The probe failed, but the proxy was already disabled.
FAILED_PROXY_ALREADY_DISABLED,
// THe probe succeeded, and as a result the proxy was enabled.
SUCCEEDED_PROXY_ENABLED,
// The probe succeeded, but the proxy was already enabled.
SUCCEEDED_PROXY_ALREADY_ENABLED,
// This must always be last.
FETCH_RESULT_COUNT
};
void RecordProbeURLFetchResult(ProbeURLFetchResult result) {
UMA_HISTOGRAM_ENUMERATION(kUMAProxyProbeURL, result, FETCH_RESULT_COUNT);
}
const char kEnabled[] = "Enabled";
// TODO(marq): Factor this string out into a constant here and in
// http_auth_handler_spdyproxy.
const char kAuthenticationRealmName[] = "SpdyProxy";
int64 GetInt64PrefValue(const base::ListValue& list_value, size_t index) {
int64 val = 0;
std::string pref_value;
bool rv = list_value.GetString(index, &pref_value);
DCHECK(rv);
if (rv) {
rv = base::StringToInt64(pref_value, &val);
DCHECK(rv);
}
return val;
}
bool IsProxyOriginSetOnCommandLine() {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
return command_line.HasSwitch(switches::kSpdyProxyAuthOrigin);
}
} // namespace
DataReductionProxySettings::DataReductionProxySettings()
: restricted_by_carrier_(false),
enabled_by_user_(false) {
}
DataReductionProxySettings::~DataReductionProxySettings() {
if (IsDataReductionProxyAllowed())
spdy_proxy_auth_enabled_.Destroy();
}
void DataReductionProxySettings::InitPrefMembers() {
spdy_proxy_auth_enabled_.Init(
prefs::kSpdyProxyAuthEnabled,
GetOriginalProfilePrefs(),
base::Bind(&DataReductionProxySettings::OnProxyEnabledPrefChange,
base::Unretained(this)));
}
void DataReductionProxySettings::InitDataReductionProxySettings() {
InitPrefMembers();
// Disable the proxy if it is not allowed to be used.
if (!IsDataReductionProxyAllowed())
return;
AddDefaultProxyBypassRules();
net::NetworkChangeNotifier::AddIPAddressObserver(this);
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
// Setting the kEnableSpdyProxyAuth switch has the same effect as enabling
// the feature via settings, in that once set, the preference will be sticky
// across instances of Chrome. Disabling the feature can only be done through
// the settings menu.
RecordDataReductionInit();
if (spdy_proxy_auth_enabled_.GetValue() ||
command_line.HasSwitch(switches::kEnableSpdyProxyAuth)) {
MaybeActivateDataReductionProxy(true);
} else {
// This is logged so we can use this information in user feedback.
LogProxyState(false /* enabled */,
false /* restricted */,
true /* at startup */);
}
}
// static
void DataReductionProxySettings::InitDataReductionProxySession(
net::HttpNetworkSession* session) {
// This is a no-op unless the authentication parameters are compiled in.
// (even though values for them may be specified on the command line).
// Authentication will still work if the command line parameters are used,
// however there will be a round-trip overhead for each challenge/response
// (typically once per session).
#if defined(SPDY_PROXY_AUTH_ORIGIN) && defined(SPDY_PROXY_AUTH_VALUE)
DCHECK(session);
net::HttpAuthCache* auth_cache = session->http_auth_cache();
DCHECK(auth_cache);
InitDataReductionAuthentication(auth_cache);
#endif // defined(SPDY_PROXY_AUTH_ORIGIN) && defined(SPDY_PROXY_AUTH_VALUE)
}
// static
void DataReductionProxySettings::InitDataReductionAuthentication(
net::HttpAuthCache* auth_cache) {
DCHECK(auth_cache);
int64 timestamp =
(base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000;
DataReductionProxyList proxies = GetDataReductionProxies();
for (DataReductionProxyList::iterator it = proxies.begin();
it != proxies.end(); ++it) {
GURL auth_origin = (*it).GetOrigin();
int32 rand[3];
crypto::RandBytes(rand, 3 * sizeof(rand[0]));
std::string realm =
base::StringPrintf("%s%lld", kAuthenticationRealmName, timestamp);
std::string challenge = base::StringPrintf(
"%s realm=\"%s\", ps=\"%lld-%u-%u-%u\"", kAuthenticationRealmName,
realm.data(), timestamp, rand[0], rand[1], rand[2]);
base::string16 password = AuthHashForSalt(timestamp);
DVLOG(1) << "origin: [" << auth_origin << "] realm: [" << realm
<< "] challenge: [" << challenge << "] password: [" << password << "]";
net::AuthCredentials credentials(base::string16(), password);
// |HttpAuthController| searches this cache by origin and path, the latter
// being '/' in the case of the data reduction proxy.
auth_cache->Add(auth_origin,
realm,
net::HttpAuth::AUTH_SCHEME_SPDYPROXY,
challenge,
credentials,
std::string("/"));
}
}
void DataReductionProxySettings::AddHostPatternToBypass(
const std::string& pattern) {
bypass_rules_.push_back(pattern);
}
void DataReductionProxySettings::AddURLPatternToBypass(
const std::string& pattern) {
size_t pos = pattern.find("/");
if (pattern.find("/", pos + 1) == pos + 1)
pos = pattern.find("/", pos + 2);
std::string host_pattern;
if (pos != std::string::npos)
host_pattern = pattern.substr(0, pos);
else
host_pattern = pattern;
AddHostPatternToBypass(host_pattern);
}
// static
bool DataReductionProxySettings::IsDataReductionProxyAllowed() {
return IsProxyOriginSetOnCommandLine() ||
(FieldTrialList::FindFullName("DataCompressionProxyRollout") == kEnabled);
}
// static
bool DataReductionProxySettings::IsDataReductionProxyPromoAllowed() {
return IsProxyOriginSetOnCommandLine() ||
(IsDataReductionProxyAllowed() &&
FieldTrialList::FindFullName("DataCompressionProxyPromoVisibility") ==
kEnabled);
}
// static
bool DataReductionProxySettings::IsPreconnectHintingAllowed() {
if (!IsDataReductionProxyAllowed())
return false;
return FieldTrialList::FindFullName("DataCompressionProxyPreconnectHints") ==
kEnabled;
}
// static
bool DataReductionProxySettings::WasFetchedViaProxy(
const net::HttpResponseHeaders* headers) {
const char kChromeProxyViaValue[] = "1.1 Chrome Compression Proxy";
void* iter = NULL;
std::string value;
while (headers->EnumerateHeader(&iter, "via", &value))
if (value == kChromeProxyViaValue) return true;
return false;
}
// static
std::string DataReductionProxySettings::GetDataReductionProxyOrigin() {
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin))
return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthOrigin);
#if defined(SPDY_PROXY_AUTH_ORIGIN)
return SPDY_PROXY_AUTH_ORIGIN;
#else
return std::string();
#endif
}
// static
std::string DataReductionProxySettings::GetDataReductionProxyFallback() {
// Regardless of what else is defined, only return a value if the main proxy
// origin is defined.
if (GetDataReductionProxyOrigin().empty())
return std::string();
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kSpdyProxyAuthFallback))
return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthFallback);
#if defined(DATA_REDUCTION_FALLBACK_HOST)
return DATA_REDUCTION_FALLBACK_HOST;
#else
return std::string();
#endif
}
bool DataReductionProxySettings::IsAcceptableAuthChallenge(
net::AuthChallengeInfo* auth_info) {
// Challenge realm must start with the authentication realm name.
std::string realm_prefix =
auth_info->realm.substr(0, strlen(kAuthenticationRealmName));
if (realm_prefix != kAuthenticationRealmName)
return false;
// The challenger must be one of the configured proxies.
DataReductionProxyList proxies = GetDataReductionProxies();
for (DataReductionProxyList::iterator it = proxies.begin();
it != proxies.end(); ++it) {
net::HostPortPair origin_host = net::HostPortPair::FromURL(*it);
if (origin_host.Equals(auth_info->challenger))
return true;
}
return false;
}
base::string16 DataReductionProxySettings::GetTokenForAuthChallenge(
net::AuthChallengeInfo* auth_info) {
if (auth_info->realm.length() > strlen(kAuthenticationRealmName)) {
int64 salt;
std::string realm_suffix =
auth_info->realm.substr(strlen(kAuthenticationRealmName));
if (base::StringToInt64(realm_suffix, &salt)) {
return AuthHashForSalt(salt);
} else {
DVLOG(1) << "Unable to parse realm name " << auth_info->realm
<< "into an int for salting.";
return base::string16();
}
} else {
return base::string16();
}
}
bool DataReductionProxySettings::IsDataReductionProxyEnabled() {
return spdy_proxy_auth_enabled_.GetValue();
}
bool DataReductionProxySettings::IsDataReductionProxyManaged() {
return spdy_proxy_auth_enabled_.IsManaged();
}
// static
DataReductionProxySettings::DataReductionProxyList
DataReductionProxySettings::GetDataReductionProxies() {
DataReductionProxyList proxies;
std::string proxy = GetDataReductionProxyOrigin();
std::string fallback = GetDataReductionProxyFallback();
if (!proxy.empty())
proxies.push_back(GURL(proxy));
if (!fallback.empty()) {
// Sanity check: fallback isn't the only proxy.
DCHECK(!proxies.empty());
proxies.push_back(GURL(fallback));
}
return proxies;
}
void DataReductionProxySettings::SetDataReductionProxyEnabled(bool enabled) {
// Prevent configuring the proxy when it is not allowed to be used.
if (!IsDataReductionProxyAllowed())
return;
spdy_proxy_auth_enabled_.SetValue(enabled);
OnProxyEnabledPrefChange();
}
int64 DataReductionProxySettings::GetDataReductionLastUpdateTime() {
PrefService* local_state = GetLocalStatePrefs();
int64 last_update_internal =
local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate);
base::Time last_update = base::Time::FromInternalValue(last_update_internal);
return static_cast<int64>(last_update.ToJsTime());
}
DataReductionProxySettings::ContentLengthList
DataReductionProxySettings::GetDailyOriginalContentLengths() {
return GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength);
}
DataReductionProxySettings::ContentLengthList
DataReductionProxySettings::GetDailyReceivedContentLengths() {
return GetDailyContentLengths(prefs::kDailyHttpReceivedContentLength);
}
void DataReductionProxySettings::OnURLFetchComplete(
const net::URLFetcher* source) {
net::URLRequestStatus status = source->GetStatus();
if (status.status() == net::URLRequestStatus::FAILED &&
status.error() == net::ERR_INTERNET_DISCONNECTED) {
RecordProbeURLFetchResult(INTERNET_DISCONNECTED);
return;
}
std::string response;
source->GetResponseAsString(&response);
if ("OK" == response.substr(0, 2)) {
DVLOG(1) << "The data reduction proxy is unrestricted.";
if (enabled_by_user_) {
if (restricted_by_carrier_) {
// The user enabled the proxy, but sometime previously in the session,
// the network operator had blocked the canary and restricted the user.
// The current network doesn't block the canary, so don't restrict the
// proxy configurations.
SetProxyConfigs(true /* enabled */,
false /* restricted */,
false /* at_startup */);
RecordProbeURLFetchResult(SUCCEEDED_PROXY_ENABLED);
} else {
RecordProbeURLFetchResult(SUCCEEDED_PROXY_ALREADY_ENABLED);
}
}
restricted_by_carrier_ = false;
return;
}
DVLOG(1) << "The data reduction proxy is restricted to the configured "
<< "fallback proxy.";
if (enabled_by_user_) {
if (!restricted_by_carrier_) {
// Restrict the proxy.
SetProxyConfigs(true /* enabled */,
true /* restricted */,
false /* at_startup */);
RecordProbeURLFetchResult(FAILED_PROXY_DISABLED);
} else {
RecordProbeURLFetchResult(FAILED_PROXY_ALREADY_DISABLED);
}
}
restricted_by_carrier_ = true;
}
void DataReductionProxySettings::OnIPAddressChanged() {
if (enabled_by_user_) {
DCHECK(IsDataReductionProxyAllowed());
ProbeWhetherDataReductionProxyIsAvailable();
}
}
void DataReductionProxySettings::OnProxyEnabledPrefChange() {
if (!DataReductionProxySettings::IsDataReductionProxyAllowed())
return;
MaybeActivateDataReductionProxy(false);
}
void DataReductionProxySettings::AddDefaultProxyBypassRules() {
// localhost
AddHostPatternToBypass("<local>");
// RFC1918 private addresses.
AddHostPatternToBypass("10.0.0.0/8");
AddHostPatternToBypass("172.16.0.0/12");
AddHostPatternToBypass("192.168.0.0/16");
// RFC4193 private addresses.
AddHostPatternToBypass("fc00::/7");
// IPV6 probe addresses.
AddHostPatternToBypass("*-ds.metric.gstatic.com");
AddHostPatternToBypass("*-v4.metric.gstatic.com");
}
void DataReductionProxySettings::LogProxyState(
bool enabled, bool restricted, bool at_startup) {
// This must stay a LOG(WARNING); the output is used in processing customer
// feedback.
const char kAtStartup[] = "at startup";
const char kByUser[] = "by user action";
const char kOn[] = "ON";
const char kOff[] = "OFF";
const char kRestricted[] = "(Restricted)";
const char kUnrestricted[] = "(Unrestricted)";
std::string annotated_on =
kOn + std::string(" ") + (restricted ? kRestricted : kUnrestricted);
LOG(WARNING) << "SPDY proxy " << (enabled ? annotated_on : kOff)
<< " " << (at_startup ? kAtStartup : kByUser);
}
PrefService* DataReductionProxySettings::GetOriginalProfilePrefs() {
return g_browser_process->profile_manager()->GetLastUsedProfile()->
GetOriginalProfile()->GetPrefs();
}
PrefService* DataReductionProxySettings::GetLocalStatePrefs() {
return g_browser_process->local_state();
}
void DataReductionProxySettings::ResetDataReductionStatistics() {
PrefService* prefs = GetLocalStatePrefs();
if (!prefs)
return;
ListPrefUpdate original_update(prefs, prefs::kDailyHttpOriginalContentLength);
ListPrefUpdate received_update(prefs, prefs::kDailyHttpReceivedContentLength);
original_update->Clear();
received_update->Clear();
for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) {
original_update->AppendString(base::Int64ToString(0));
received_update->AppendString(base::Int64ToString(0));
}
}
void DataReductionProxySettings::MaybeActivateDataReductionProxy(
bool at_startup) {
PrefService* prefs = GetOriginalProfilePrefs();
// TODO(marq): Consider moving this so stats are wiped the first time the
// proxy settings are actually (not maybe) turned on.
if (spdy_proxy_auth_enabled_.GetValue() &&
!prefs->GetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore)) {
prefs->SetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore, true);
ResetDataReductionStatistics();
}
std::string proxy = GetDataReductionProxyOrigin();
// Configure use of the data reduction proxy if it is enabled and the proxy
// origin is non-empty.
enabled_by_user_= spdy_proxy_auth_enabled_.GetValue() && !proxy.empty();
SetProxyConfigs(enabled_by_user_, restricted_by_carrier_, at_startup);
// Check if the proxy has been restricted explicitly by the carrier.
if (enabled_by_user_)
ProbeWhetherDataReductionProxyIsAvailable();
}
void DataReductionProxySettings::SetProxyConfigs(
bool enabled, bool restricted, bool at_startup) {
// If |restricted| is true and there is no defined fallback proxy.
// treat this as a disable.
std::string fallback = GetDataReductionProxyFallback();
if (fallback.empty() && enabled && restricted)
enabled = false;
LogProxyState(enabled, restricted, at_startup);
PrefService* prefs = GetOriginalProfilePrefs();
DCHECK(prefs);
DictionaryPrefUpdate update(prefs, prefs::kProxy);
base::DictionaryValue* dict = update.Get();
if (enabled) {
std::string proxy_list;
if (restricted) {
DCHECK(!fallback.empty());
proxy_list = fallback;
} else {
proxy_list = GetDataReductionProxyOrigin() +
(fallback.empty() ? "" : "," + fallback);
}
std::string proxy_server_config = "http=" + proxy_list + ",direct://;";
dict->SetString("server", proxy_server_config);
dict->SetString("mode",
ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS));
dict->SetString("bypass_list", JoinString(bypass_rules_, ", "));
} else {
dict->SetString("mode", ProxyModeToString(ProxyPrefs::MODE_SYSTEM));
dict->SetString("server", "");
dict->SetString("bypass_list", "");
}
}
// Metrics methods
void DataReductionProxySettings::RecordDataReductionInit() {
ProxyStartupState state = PROXY_NOT_AVAILABLE;
if (IsDataReductionProxyAllowed())
state = IsDataReductionProxyEnabled() ? PROXY_ENABLED : PROXY_DISABLED;
UMA_HISTOGRAM_ENUMERATION(kUMAProxyStartupStateHistogram,
state,
PROXY_STARTUP_STATE_COUNT);
}
DataReductionProxySettings::ContentLengthList
DataReductionProxySettings::GetDailyContentLengths(const char* pref_name) {
DataReductionProxySettings::ContentLengthList content_lengths;
const base::ListValue* list_value = GetLocalStatePrefs()->GetList(pref_name);
if (list_value->GetSize() == spdyproxy::kNumDaysInHistory) {
for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) {
content_lengths.push_back(GetInt64PrefValue(*list_value, i));
}
}
return content_lengths;
}
void DataReductionProxySettings::GetContentLengths(
unsigned int days,
int64* original_content_length,
int64* received_content_length,
int64* last_update_time) {
DCHECK_LE(days, spdyproxy::kNumDaysInHistory);
PrefService* local_state = GetLocalStatePrefs();
if (!local_state) {
*original_content_length = 0L;
*received_content_length = 0L;
*last_update_time = 0L;
return;
}
const base::ListValue* original_list =
local_state->GetList(prefs::kDailyHttpOriginalContentLength);
const base::ListValue* received_list =
local_state->GetList(prefs::kDailyHttpReceivedContentLength);
if (original_list->GetSize() != spdyproxy::kNumDaysInHistory ||
received_list->GetSize() != spdyproxy::kNumDaysInHistory) {
*original_content_length = 0L;
*received_content_length = 0L;
*last_update_time = 0L;
return;
}
int64 orig = 0L;
int64 recv = 0L;
// Include days from the end of the list going backwards.
for (size_t i = spdyproxy::kNumDaysInHistory - days;
i < spdyproxy::kNumDaysInHistory; ++i) {
orig += GetInt64PrefValue(*original_list, i);
recv += GetInt64PrefValue(*received_list, i);
}
*original_content_length = orig;
*received_content_length = recv;
*last_update_time =
local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate);
}
std::string DataReductionProxySettings::GetProxyCheckURL() {
if (!IsDataReductionProxyAllowed())
return std::string();
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kDataReductionProxyProbeURL)) {
return command_line.GetSwitchValueASCII(
switches::kDataReductionProxyProbeURL);
}
#if defined(DATA_REDUCTION_PROXY_PROBE_URL)
return DATA_REDUCTION_PROXY_PROBE_URL;
#else
return std::string();
#endif
}
// static
base::string16 DataReductionProxySettings::AuthHashForSalt(int64 salt) {
if (!IsDataReductionProxyAllowed())
return base::string16();
std::string key;
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) {
// If an origin is provided via a switch, then only consider the value
// that is provided by a switch. Do not use the preprocessor constant.
// Don't expose SPDY_PROXY_AUTH_VALUE to a proxy passed in via the command
// line.
if (!command_line.HasSwitch(switches::kSpdyProxyAuthValue))
return base::string16();
key = command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthValue);
} else {
#if defined(SPDY_PROXY_AUTH_VALUE)
key = SPDY_PROXY_AUTH_VALUE;
#else
return base::string16();
#endif
}
DCHECK(!key.empty());
std::string salted_key =
base::StringPrintf("%lld%s%lld", salt, key.c_str(), salt);
return UTF8ToUTF16(base::MD5String(salted_key));
}
net::URLFetcher* DataReductionProxySettings::GetURLFetcher() {
std::string url = GetProxyCheckURL();
if (url.empty())
return NULL;
net::URLFetcher* fetcher = net::URLFetcher::Create(GURL(url),
net::URLFetcher::GET,
this);
fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | net::LOAD_BYPASS_PROXY);
// If this code ever goes multi profile, the used profile needs to be taken
// from the browser context.
Profile* profile = ProfileManager::GetActiveUserProfile();
fetcher->SetRequestContext(profile->GetRequestContext());
// Configure max retries to be at most kMaxRetries times for 5xx errors.
static const int kMaxRetries = 5;
fetcher->SetMaxRetriesOn5xx(kMaxRetries);
return fetcher;
}
void
DataReductionProxySettings::ProbeWhetherDataReductionProxyIsAvailable() {
net::URLFetcher* fetcher = GetURLFetcher();
if (!fetcher)
return;
fetcher_.reset(fetcher);
fetcher_->Start();
}
|