data_reduction_proxy_settings.cc revision 1e9bf3e0803691d0a228da41fc608347b6db4340
18bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
28bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
38bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// found in the LICENSE file.
48bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
58bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings.h"
68bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/bind.h"
88bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/command_line.h"
98bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/metrics/field_trial.h"
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/metrics/histogram.h"
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/prefs/pref_member.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/prefs/pref_service.h"
131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/prefs/scoped_user_pref_update.h"
148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/strings/string_number_conversions.h"
158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/strings/string_util.h"
168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/strings/stringprintf.h"
178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/strings/utf_string_conversions.h"
188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/browser_process.h"
198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/prefs/proxy_prefs.h"
208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/common/chrome_switches.h"
238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "chrome/common/pref_names.h"
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "crypto/random.h"
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "net/base/auth.h"
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "net/base/host_port_pair.h"
278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "net/base/load_flags.h"
288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "net/base/net_errors.h"
291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "net/http/http_auth.h"
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "net/http/http_auth_cache.h"
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "net/http/http_network_session.h"
328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "net/url_request/url_fetcher.h"
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "net/url_request/url_fetcher_delegate.h"
348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "net/url_request/url_request_status.h"
358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "url/gurl.h"
368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)using base::FieldTrialList;
388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)using base::StringPrintf;
398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)namespace {
418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Key of the UMA DataReductionProxy.StartupState histogram.
438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)const char kUMAProxyStartupStateHistogram[] =
448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "DataReductionProxy.StartupState";
458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Values of the UMA DataReductionProxy.StartupState histogram.
468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)enum ProxyStartupState {
478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PROXY_NOT_AVAILABLE = 0,
488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PROXY_DISABLED,
498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PROXY_ENABLED,
508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PROXY_STARTUP_STATE_COUNT,
518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)};
528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)const char kEnabled[] = "Enabled";
548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// TODO(marq): Factor this string out into a constant here and in
561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)//             http_auth_handler_spdyproxy.
571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const char kAuthenticationRealmName[] = "SpdyProxy";
581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)int64 GetInt64PrefValue(const ListValue& list_value, size_t index) {
608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int64 val = 0;
618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  std::string pref_value;
628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool rv = list_value.GetString(index, &pref_value);
638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK(rv);
648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (rv) {
658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    rv = base::StringToInt64(pref_value, &val);
668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    DCHECK(rv);
678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return val;
698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool IsProxyOriginSetOnCommandLine() {
721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return command_line.HasSwitch(switches::kSpdyProxyAuthOrigin);
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}  // namespace
778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)DataReductionProxySettings::DataReductionProxySettings()
798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    : has_turned_on_(false),
808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      has_turned_off_(false),
818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      disabled_by_carrier_(false),
828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      enabled_by_user_(false) {
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)DataReductionProxySettings::~DataReductionProxySettings() {
868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (IsDataReductionProxyAllowed())
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    spdy_proxy_auth_enabled_.Destroy();
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::InitPrefMembers() {
918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  spdy_proxy_auth_enabled_.Init(
928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      prefs::kSpdyProxyAuthEnabled,
938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      GetOriginalProfilePrefs(),
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      base::Bind(&DataReductionProxySettings::OnProxyEnabledPrefChange,
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                 base::Unretained(this)));
968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::InitDataReductionProxySettings() {
991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  InitPrefMembers();
1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Disable the proxy if it is not allowed to be used.
1028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!IsDataReductionProxyAllowed())
1038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AddDefaultProxyBypassRules();
1068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  net::NetworkChangeNotifier::AddIPAddressObserver(this);
1078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
1098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Setting the kEnableSpdyProxyAuth switch has the same effect as enabling
1118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // the feature via settings, in that once set, the preference will be sticky
1128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // across instances of Chrome. Disabling the feature can only be done through
1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // the settings menu.
1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  RecordDataReductionInit();
1158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (spdy_proxy_auth_enabled_.GetValue() ||
1168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      command_line.HasSwitch(switches::kEnableSpdyProxyAuth)) {
1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    MaybeActivateDataReductionProxy(true);
1188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  } else {
1198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // This is logged so we can use this information in user feedback.
1208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    LogProxyState(false /* enabled */, true /* at startup */);
1218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
1228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void DataReductionProxySettings::InitDataReductionProxySession(
1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    net::HttpNetworkSession* session) {
1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// This is a no-op unless the authentication parameters are compiled in.
1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// (even though values for them may be specified on the command line).
1281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Authentication will still work if the command line parameters are used,
1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// however there will be a round-trip overhead for each challenge/response
1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// (typically once per session).
1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if defined(SPDY_PROXY_AUTH_ORIGIN) && defined(SPDY_PROXY_AUTH_VALUE)
1321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(session);
1331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  net::HttpAuthCache* auth_cache = session->http_auth_cache();
1341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(auth_cache);
1351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  InitDataReductionAuthentication(auth_cache);
1361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#endif  // defined(SPDY_PROXY_AUTH_ORIGIN) && defined(SPDY_PROXY_AUTH_VALUE)
1371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void DataReductionProxySettings::InitDataReductionAuthentication(
1401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    net::HttpAuthCache* auth_cache) {
1411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(auth_cache);
1421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  int64 timestamp =
1431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000;
1441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DataReductionProxyList proxies = GetDataReductionProxies();
1461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  for (DataReductionProxyList::iterator it = proxies.begin();
1471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      it != proxies.end(); ++it) {
1481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    GURL auth_origin = (*it).GetOrigin();
1491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    int32 rand[3];
1501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    crypto::RandBytes(rand, 3 * sizeof(rand[0]));
1511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    std::string realm =
1531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        base::StringPrintf("%s%lld", kAuthenticationRealmName, timestamp);
1541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    std::string challenge = base::StringPrintf(
1551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        "%s realm=\"%s\", ps=\"%lld-%u-%u-%u\"", kAuthenticationRealmName,
1561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        realm.data(), timestamp, rand[0], rand[1], rand[2]);
1571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    base::string16 password = AuthHashForSalt(timestamp);
1581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    DVLOG(1) << "origin: [" << auth_origin << "] realm: [" << realm
1601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        << "] challenge: [" << challenge << "] password: [" << password << "]";
1611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    net::AuthCredentials credentials(base::string16(), password);
1631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    auth_cache->Add(auth_origin,
1641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                    realm,
1651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                    net::HttpAuth::AUTH_SCHEME_SPDYPROXY,
1661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                    challenge,
1671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                    credentials,
1681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                    std::string()); // Proxy auth uses an empty path for lookup.
1691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::AddHostPatternToBypass(
1738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const std::string& pattern) {
1748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bypass_rules_.push_back(pattern);
1758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::AddURLPatternToBypass(
1788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const std::string& pattern) {
1798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  size_t pos = pattern.find("/");
1808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (pattern.find("/", pos + 1) == pos + 1)
1818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    pos = pattern.find("/", pos + 2);
1828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  std::string host_pattern;
1848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (pos != std::string::npos)
1858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    host_pattern = pattern.substr(0, pos);
1868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  else
1878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    host_pattern = pattern;
1888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AddHostPatternToBypass(host_pattern);
1908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)bool DataReductionProxySettings::IsDataReductionProxyAllowed() {
1938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return IsProxyOriginSetOnCommandLine() ||
1948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      (FieldTrialList::FindFullName("DataCompressionProxyRollout") == kEnabled);
1958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)bool DataReductionProxySettings::IsDataReductionProxyPromoAllowed() {
1988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return IsProxyOriginSetOnCommandLine() ||
1998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      (IsDataReductionProxyAllowed() &&
2008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        FieldTrialList::FindFullName("DataCompressionProxyPromoVisibility") ==
2018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)            kEnabled);
2028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
2038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)std::string DataReductionProxySettings::GetDataReductionProxyOrigin() {
2058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
2068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin))
2078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthOrigin);
2088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if defined(SPDY_PROXY_AUTH_ORIGIN)
2098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return SPDY_PROXY_AUTH_ORIGIN;
2108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#else
2118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return std::string();
2128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif
2138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
2148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)std::string DataReductionProxySettings::GetDataReductionProxyFallback() {
2161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Regardless of what else is defined, only return a value if the main proxy
2171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // origin is defined.
2181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (GetDataReductionProxyOrigin().empty())
2198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return std::string();
2208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
2211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (command_line.HasSwitch(switches::kSpdyProxyAuthFallback))
2221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthFallback);
2231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if defined(DATA_REDUCTION_FALLBACK_HOST)
2241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return DATA_REDUCTION_FALLBACK_HOST;
2258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#else
2268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return std::string();
2278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif
2288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
2298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool DataReductionProxySettings::IsAcceptableAuthChallenge(
2311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    net::AuthChallengeInfo* auth_info) {
2321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Challenge realm must start with the authentication realm name.
2331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  std::string realm_prefix =
2341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      auth_info->realm.substr(0, strlen(kAuthenticationRealmName));
2351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (realm_prefix != kAuthenticationRealmName)
2361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return false;
2371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // The challenger must be one of the configured proxies.
2391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DataReductionProxyList proxies = GetDataReductionProxies();
2401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  for (DataReductionProxyList::iterator it = proxies.begin();
2411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)       it != proxies.end(); ++it) {
2421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    net::HostPortPair origin_host = net::HostPortPair::FromURL(*it);
2431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if (origin_host.Equals(auth_info->challenger))
2441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return true;
2451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
2461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return false;
2471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)base::string16 DataReductionProxySettings::GetTokenForAuthChallenge(
2501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    net::AuthChallengeInfo* auth_info) {
2511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (auth_info->realm.length() > strlen(kAuthenticationRealmName)) {
2521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    int64 salt;
2531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    std::string realm_suffix =
2541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        auth_info->realm.substr(strlen(kAuthenticationRealmName));
2551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if (base::StringToInt64(realm_suffix, &salt)) {
2561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return AuthHashForSalt(salt);
2571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    } else {
2581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      DVLOG(1) << "Unable to parse realm name " << auth_info->realm
2591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)               << "into an int for salting.";
2601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return base::string16();
2611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
2621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  } else {
2631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return base::string16();
2641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
2651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)bool DataReductionProxySettings::IsDataReductionProxyEnabled() {
2688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return spdy_proxy_auth_enabled_.GetValue();
2698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
2708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)bool DataReductionProxySettings::IsDataReductionProxyManaged() {
2728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return spdy_proxy_auth_enabled_.IsManaged();
2738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
2748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)DataReductionProxySettings::DataReductionProxyList
2761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)DataReductionProxySettings::GetDataReductionProxies() {
2771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DataReductionProxyList proxies;
2781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  std::string proxy = GetDataReductionProxyOrigin();
2791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  std::string fallback = GetDataReductionProxyFallback();
2801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!proxy.empty())
2821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    proxies.push_back(GURL(proxy));
2831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!fallback.empty()) {
2851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Sanity check: fallback isn't the only proxy.
2861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    DCHECK(!proxies.empty());
2871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    proxies.push_back(GURL(fallback));
2881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
2891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return proxies;
2911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::SetDataReductionProxyEnabled(bool enabled) {
2948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Prevent configuring the proxy when it is not allowed to be used.
2958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!IsDataReductionProxyAllowed())
2968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
2978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  spdy_proxy_auth_enabled_.SetValue(enabled);
2998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  OnProxyEnabledPrefChange();
3008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)int64 DataReductionProxySettings::GetDataReductionLastUpdateTime() {
3038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PrefService* local_state = GetLocalStatePrefs();
3048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int64 last_update_internal =
3058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate);
3068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::Time last_update = base::Time::FromInternalValue(last_update_internal);
3078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return static_cast<int64>(last_update.ToJsTime());
3088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)DataReductionProxySettings::ContentLengthList
3118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)DataReductionProxySettings::GetDailyOriginalContentLengths() {
3128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength);
3138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)DataReductionProxySettings::ContentLengthList
3168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)DataReductionProxySettings::GetDailyReceivedContentLengths() {
3178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return GetDailyContentLengths(prefs::kDailyHttpReceivedContentLength);
3188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::OnURLFetchComplete(
3218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    const net::URLFetcher* source) {
3228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  net::URLRequestStatus status = source->GetStatus();
3238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (status.status() == net::URLRequestStatus::FAILED &&
3248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      status.error() == net::ERR_INTERNET_DISCONNECTED) {
3258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
3268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  std::string response;
3298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  source->GetResponseAsString(&response);
3308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if ("OK" == response.substr(0, 2)) {
3328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    DVLOG(1) << "The data reduction proxy is not blocked.";
3338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    if (enabled_by_user_ && disabled_by_carrier_) {
3358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      // The user enabled the proxy, but sometime previously in the session,
3368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      // the network operator had blocked the proxy. Now that the network
3378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      // operator is unblocking it, configure it to the user's desires.
3388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      SetProxyConfigs(true, false);
3398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
3408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    disabled_by_carrier_ = false;
3418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
3428bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DVLOG(1) << "The data reduction proxy is blocked.";
3448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3458bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (enabled_by_user_ && !disabled_by_carrier_) {
3468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    // Disable the proxy.
3478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    SetProxyConfigs(false, false);
3488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  disabled_by_carrier_ = true;
3508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::OnIPAddressChanged() {
3538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (enabled_by_user_) {
3548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    DCHECK(IsDataReductionProxyAllowed());
3558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ProbeWhetherDataReductionProxyIsAvailable();
3568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
3578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::OnProxyEnabledPrefChange() {
3601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!DataReductionProxySettings::IsDataReductionProxyAllowed())
3618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
3628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  MaybeActivateDataReductionProxy(false);
3638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::AddDefaultProxyBypassRules() {
3668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // localhost
3678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AddHostPatternToBypass("<local>");
3688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // RFC1918 private addresses.
3698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AddHostPatternToBypass("10.0.0.0/8");
3708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AddHostPatternToBypass("172.16.0.0/12");
3718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AddHostPatternToBypass("192.168.0.0/16");
3728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)   // RFC4193 private addresses.
3738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  AddHostPatternToBypass("fc00::/7");
3748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::LogProxyState(bool enabled, bool at_startup) {
3778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // This must stay a LOG(WARNING); the output is used in processing customer
3788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // feedback.
3798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const char kAtStartup[] = "at startup";
3808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const char kByUser[] = "by user action";
3818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const char kOn[] = "ON";
3828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const char kOff[] = "OFF";
3838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  LOG(WARNING) << "SPDY proxy " << (enabled ? kOn : kOff)
3858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)               << " " << (at_startup ? kAtStartup : kByUser);
3868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)PrefService* DataReductionProxySettings::GetOriginalProfilePrefs() {
3898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return g_browser_process->profile_manager()->GetLastUsedProfile()->
3908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      GetOriginalProfile()->GetPrefs();
3918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)PrefService* DataReductionProxySettings::GetLocalStatePrefs() {
3948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return g_browser_process->local_state();
3958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
3968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
3978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::ResetDataReductionStatistics() {
3988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PrefService* prefs = GetLocalStatePrefs();
3998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!prefs)
4008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
4018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ListPrefUpdate original_update(prefs, prefs::kDailyHttpOriginalContentLength);
4028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ListPrefUpdate received_update(prefs, prefs::kDailyHttpReceivedContentLength);
4038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  original_update->Clear();
4048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  received_update->Clear();
4058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) {
4068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    original_update->AppendString(base::Int64ToString(0));
4078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    received_update->AppendString(base::Int64ToString(0));
4088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
4098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
4108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::MaybeActivateDataReductionProxy(
4128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    bool at_startup) {
4138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PrefService* prefs = GetOriginalProfilePrefs();
4148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // TODO(marq): Consider moving this so stats are wiped the first time the
4168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // proxy settings are actually (not maybe) turned on.
4178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (spdy_proxy_auth_enabled_.GetValue() &&
4188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      !prefs->GetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore)) {
4198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    prefs->SetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore, true);
4208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ResetDataReductionStatistics();
4218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
4228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  std::string proxy = GetDataReductionProxyOrigin();
4248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Configure use of the data reduction proxy if it is enabled and the proxy
4258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // origin is non-empty.
4261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  enabled_by_user_= spdy_proxy_auth_enabled_.GetValue() && !proxy.empty();
4278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  SetProxyConfigs(enabled_by_user_ && !disabled_by_carrier_, at_startup);
4288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Check if the proxy has been disabled explicitly by the carrier.
4308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (enabled_by_user_)
4318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    ProbeWhetherDataReductionProxyIsAvailable();
4328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
4338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::SetProxyConfigs(bool enabled,
4358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                                 bool at_startup) {
4368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  LogProxyState(enabled, at_startup);
4378bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PrefService* prefs = GetOriginalProfilePrefs();
4388bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK(prefs);
4398bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DictionaryPrefUpdate update(prefs, prefs::kProxy);
4408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  base::DictionaryValue* dict = update.Get();
4418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (enabled) {
4421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    std::string fallback = GetDataReductionProxyFallback();
4438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    std::string proxy_server_config =
4441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        "http=" + GetDataReductionProxyOrigin() +
4451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        (fallback.empty() ? "" : "," + fallback) +
4461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        ",direct://;";
4478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    dict->SetString("server", proxy_server_config);
4488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    dict->SetString("mode",
4498bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                    ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS));
4508bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    dict->SetString("bypass_list", JoinString(bypass_rules_, ", "));
4518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  } else {
4528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    dict->SetString("mode", ProxyModeToString(ProxyPrefs::MODE_SYSTEM));
4538bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    dict->SetString("server", "");
4548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    dict->SetString("bypass_list", "");
4558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
4568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
4578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)// Metrics methods
4598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::RecordDataReductionInit() {
4608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ProxyStartupState state = PROXY_NOT_AVAILABLE;
4618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (IsDataReductionProxyAllowed())
4628bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    state = IsDataReductionProxyEnabled() ? PROXY_ENABLED : PROXY_DISABLED;
4638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  UMA_HISTOGRAM_ENUMERATION(kUMAProxyStartupStateHistogram,
4648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                            state,
4658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                            PROXY_STARTUP_STATE_COUNT);
4668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
4678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)DataReductionProxySettings::ContentLengthList
4698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)DataReductionProxySettings::GetDailyContentLengths(const char* pref_name) {
4708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DataReductionProxySettings::ContentLengthList content_lengths;
4718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const ListValue* list_value = GetLocalStatePrefs()->GetList(pref_name);
4728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (list_value->GetSize() == spdyproxy::kNumDaysInHistory) {
4738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) {
4748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      content_lengths.push_back(GetInt64PrefValue(*list_value, i));
4758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    }
4768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
4778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return content_lengths;
4788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles) }
4798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void DataReductionProxySettings::GetContentLengths(
4818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    unsigned int days,
4828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    int64* original_content_length,
4838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    int64* received_content_length,
4848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    int64* last_update_time) {
4858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DCHECK_LE(days, spdyproxy::kNumDaysInHistory);
4868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  PrefService* local_state = GetLocalStatePrefs();
4878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!local_state) {
4888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    *original_content_length = 0L;
4898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    *received_content_length = 0L;
4908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    *last_update_time = 0L;
4918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
4928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
4938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const ListValue* original_list =
4958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      local_state->GetList(prefs::kDailyHttpOriginalContentLength);
4968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const ListValue* received_list =
4978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      local_state->GetList(prefs::kDailyHttpReceivedContentLength);
4988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
4998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (original_list->GetSize() != spdyproxy::kNumDaysInHistory ||
5008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      received_list->GetSize() != spdyproxy::kNumDaysInHistory) {
5018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    *original_content_length = 0L;
5028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    *received_content_length = 0L;
5038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    *last_update_time = 0L;
5048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
5058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
5068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
5078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int64 orig = 0L;
5088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  int64 recv = 0L;
5098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Include days from the end of the list going backwards.
5108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  for (size_t i = spdyproxy::kNumDaysInHistory - days;
5118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)       i < spdyproxy::kNumDaysInHistory; ++i) {
5128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    orig += GetInt64PrefValue(*original_list, i);
5138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    recv += GetInt64PrefValue(*received_list, i);
5148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
5158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  *original_content_length = orig;
5168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  *received_content_length = recv;
5178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  *last_update_time =
5188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate);
5198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
5208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
5218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)std::string DataReductionProxySettings::GetProxyCheckURL() {
5228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!IsDataReductionProxyAllowed())
5238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return std::string();
5248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
5258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (command_line.HasSwitch(switches::kDataReductionProxyProbeURL)) {
5268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return command_line.GetSwitchValueASCII(
5278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)        switches::kDataReductionProxyProbeURL);
5288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
5298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if defined(DATA_REDUCTION_PROXY_PROBE_URL)
5308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return DATA_REDUCTION_PROXY_PROBE_URL;
5318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#else
5328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return std::string();
5338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif
5348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
5358bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
5361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)base::string16 DataReductionProxySettings::AuthHashForSalt(int64 salt) {
5371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!IsDataReductionProxyAllowed())
5381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return base::string16();
5391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  std::string key;
5411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
5431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) {
5441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // If an origin is provided via a switch, then only consider the value
5451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // that is provided by a switch. Do not use the preprocessor constant.
5461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // Don't expose SPDY_PROXY_AUTH_VALUE to a proxy passed in via the command
5471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // line.
5481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if (!command_line.HasSwitch(switches::kSpdyProxyAuthValue))
5491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return base::string16();
5501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    key = command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthValue);
5511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  } else {
5521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if defined(SPDY_PROXY_AUTH_VALUE)
5531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    key = SPDY_PROXY_AUTH_VALUE;
5541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#else
5551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return base::string16();
5561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#endif
5571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
5581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(!key.empty());
5601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  std::string salted_key =
5621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::StringPrintf("%lld%s%lld", salt, key.c_str(), salt);
5631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return UTF8ToUTF16(base::MD5String(salted_key));
5641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
5651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)net::URLFetcher* DataReductionProxySettings::GetURLFetcher() {
5678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  std::string url = GetProxyCheckURL();
5688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (url.empty())
5698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return NULL;
5708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  net::URLFetcher* fetcher = net::URLFetcher::Create(GURL(url),
5718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                                     net::URLFetcher::GET,
5728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                                     this);
5731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | net::LOAD_BYPASS_PROXY);
5748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Profile* profile = g_browser_process->profile_manager()->
5758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      GetDefaultProfile();
5768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  fetcher->SetRequestContext(profile->GetRequestContext());
5778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Configure max retries to be at most kMaxRetries times for 5xx errors.
5788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  static const int kMaxRetries = 5;
5798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  fetcher->SetMaxRetriesOn5xx(kMaxRetries);
5808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return fetcher;
5818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
5828bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
5838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)void
5848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)DataReductionProxySettings::ProbeWhetherDataReductionProxyIsAvailable() {
5858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  net::URLFetcher* fetcher = GetURLFetcher();
5868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!fetcher)
5878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return;
5888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  fetcher_.reset(fetcher);
5898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  fetcher_->Start();
5908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
591