Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(854)

Side by Side Diff: chrome/browser/net/spdyproxy/data_reduction_proxy_settings.cc

Issue 30883003: Simple fallback implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@patched
Patch Set: Updated javadocs. Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings.h" 5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/metrics/field_trial.h" 9 #include "base/metrics/field_trial.h"
10 #include "base/metrics/histogram.h" 10 #include "base/metrics/histogram.h"
11 #include "base/prefs/pref_member.h" 11 #include "base/prefs/pref_member.h"
12 #include "base/prefs/pref_service.h" 12 #include "base/prefs/pref_service.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
17 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/prefs/proxy_prefs.h" 18 #include "chrome/browser/prefs/proxy_prefs.h"
19 #include "chrome/browser/prefs/scoped_user_pref_update.h" 19 #include "chrome/browser/prefs/scoped_user_pref_update.h"
20 #include "chrome/browser/profiles/profile.h" 20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_manager.h" 21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/common/chrome_switches.h" 22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
24 #include "crypto/random.h"
25 #include "net/base/auth.h"
24 #include "net/base/host_port_pair.h" 26 #include "net/base/host_port_pair.h"
25 #include "net/base/load_flags.h" 27 #include "net/base/load_flags.h"
26 #include "net/base/net_errors.h" 28 #include "net/base/net_errors.h"
29 #include "net/http/http_auth.h"
30 #include "net/http/http_auth_cache.h"
31 #include "net/http/http_network_session.h"
27 #include "net/url_request/url_fetcher.h" 32 #include "net/url_request/url_fetcher.h"
28 #include "net/url_request/url_fetcher_delegate.h" 33 #include "net/url_request/url_fetcher_delegate.h"
29 #include "net/url_request/url_request_status.h" 34 #include "net/url_request/url_request_status.h"
30 #include "url/gurl.h" 35 #include "url/gurl.h"
31 36
32 using base::FieldTrialList; 37 using base::FieldTrialList;
33 using base::StringPrintf; 38 using base::StringPrintf;
34 39
35 namespace { 40 namespace {
36 41
37 // Key of the UMA DataReductionProxy.StartupState histogram. 42 // Key of the UMA DataReductionProxy.StartupState histogram.
38 const char kUMAProxyStartupStateHistogram[] = 43 const char kUMAProxyStartupStateHistogram[] =
39 "DataReductionProxy.StartupState"; 44 "DataReductionProxy.StartupState";
40 // Values of the UMA DataReductionProxy.StartupState histogram. 45 // Values of the UMA DataReductionProxy.StartupState histogram.
41 enum ProxyStartupState { 46 enum ProxyStartupState {
42 PROXY_NOT_AVAILABLE = 0, 47 PROXY_NOT_AVAILABLE = 0,
43 PROXY_DISABLED, 48 PROXY_DISABLED,
44 PROXY_ENABLED, 49 PROXY_ENABLED,
45 PROXY_STARTUP_STATE_COUNT, 50 PROXY_STARTUP_STATE_COUNT,
46 }; 51 };
47 52
48 const char kEnabled[] = "Enabled"; 53 const char kEnabled[] = "Enabled";
49 54
55 // TODO(marq): Factor this string out into a constant here and in
56 // http_auth_handler_spdyproxy.
57 const char kAuthenticationRealmName[] = "SpdyProxy";
58
50 int64 GetInt64PrefValue(const ListValue& list_value, size_t index) { 59 int64 GetInt64PrefValue(const ListValue& list_value, size_t index) {
51 int64 val = 0; 60 int64 val = 0;
52 std::string pref_value; 61 std::string pref_value;
53 bool rv = list_value.GetString(index, &pref_value); 62 bool rv = list_value.GetString(index, &pref_value);
54 DCHECK(rv); 63 DCHECK(rv);
55 if (rv) { 64 if (rv) {
56 rv = base::StringToInt64(pref_value, &val); 65 rv = base::StringToInt64(pref_value, &val);
57 DCHECK(rv); 66 DCHECK(rv);
58 } 67 }
59 return val; 68 return val;
60 } 69 }
61 70
71 bool IsProxyOriginSetOnCommandLine() {
72 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
73 return command_line.HasSwitch(switches::kSpdyProxyAuthOrigin);
74 }
75
62 } // namespace 76 } // namespace
63 77
64 DataReductionProxySettings::DataReductionProxySettings() 78 DataReductionProxySettings::DataReductionProxySettings()
65 : has_turned_on_(false), 79 : has_turned_on_(false),
66 has_turned_off_(false), 80 has_turned_off_(false),
67 disabled_by_carrier_(false), 81 disabled_by_carrier_(false),
68 enabled_by_user_(false) { 82 enabled_by_user_(false) {
69 } 83 }
70 84
71 DataReductionProxySettings::~DataReductionProxySettings() { 85 DataReductionProxySettings::~DataReductionProxySettings() {
(...skipping 28 matching lines...) Expand all
100 RecordDataReductionInit(); 114 RecordDataReductionInit();
101 if (spdy_proxy_auth_enabled_.GetValue() || 115 if (spdy_proxy_auth_enabled_.GetValue() ||
102 command_line.HasSwitch(switches::kEnableSpdyProxyAuth)) { 116 command_line.HasSwitch(switches::kEnableSpdyProxyAuth)) {
103 MaybeActivateDataReductionProxy(true); 117 MaybeActivateDataReductionProxy(true);
104 } else { 118 } else {
105 // This is logged so we can use this information in user feedback. 119 // This is logged so we can use this information in user feedback.
106 LogProxyState(false /* enabled */, true /* at startup */); 120 LogProxyState(false /* enabled */, true /* at startup */);
107 } 121 }
108 } 122 }
109 123
124 void DataReductionProxySettings::InitDataReductionProxySession(
125 net::HttpNetworkSession* session) {
126 // This is a no-op unless the authentication parameters are compiled in.
127 // (even though values for them may be specified on the command line).
128 // Authentication will still work if the command line parameters are used,
129 // however there will be a round-trip overhead for each challenge/response
130 // (typically once per session).
131 #if defined(SPDY_PROXY_AUTH_ORIGIN) && defined(SPDY_PROXY_AUTH_VALUE)
132 DCHECK(session);
133 net::HttpAuthCache* auth_cache = session->http_auth_cache();
134 DCHECK(auth_cache);
135 InitDataReductionAuthentication(auth_cache);
136 #endif // defined(SPDY_PROXY_AUTH_ORIGIN) && defined(SPDY_PROXY_AUTH_VALUE)
137 }
138
139 void DataReductionProxySettings::InitDataReductionAuthentication(
140 net::HttpAuthCache* auth_cache) {
141 DCHECK(auth_cache);
142 int64 timestamp =
143 (base::Time::Now() - base::Time::UnixEpoch()).InMilliseconds() / 1000;
144
145 DataReductionProxyList proxies = GetDataReductionProxies();
146 for (DataReductionProxyList::iterator it = proxies.begin();
147 it != proxies.end(); ++it) {
148 GURL auth_origin = (*it).GetOrigin();
149 int32 rand[3];
150 crypto::RandBytes(rand, 3 * sizeof(rand[0]));
151
152 std::string realm =
153 base::StringPrintf("%s%lld", kAuthenticationRealmName, timestamp);
154 std::string challenge = base::StringPrintf(
155 "%s realm=\"%s\", ps=\"%lld-%u-%u-%u\"", kAuthenticationRealmName,
156 realm.data(), timestamp, rand[0], rand[1], rand[2]);
157 base::string16 password = AuthHashForSalt(timestamp);
158
159 DVLOG(1) << "origin: [" << auth_origin << "] realm: [" << realm
160 << "] challenge: [" << challenge << "] password: [" << password << "]";
161
162 net::AuthCredentials credentials(base::string16(), password);
163 auth_cache->Add(auth_origin,
164 realm,
165 net::HttpAuth::AUTH_SCHEME_SPDYPROXY,
166 challenge,
167 credentials,
168 std::string()); // Proxy auth uses an empty path for lookup.
169 }
170 }
171
110 void DataReductionProxySettings::AddHostPatternToBypass( 172 void DataReductionProxySettings::AddHostPatternToBypass(
111 const std::string& pattern) { 173 const std::string& pattern) {
112 bypass_rules_.push_back(pattern); 174 bypass_rules_.push_back(pattern);
113 } 175 }
114 176
115 void DataReductionProxySettings::AddURLPatternToBypass( 177 void DataReductionProxySettings::AddURLPatternToBypass(
116 const std::string& pattern) { 178 const std::string& pattern) {
117 size_t pos = pattern.find("/"); 179 size_t pos = pattern.find("/");
118 if (pattern.find("/", pos + 1) == pos + 1) 180 if (pattern.find("/", pos + 1) == pos + 1)
119 pos = pattern.find("/", pos + 2); 181 pos = pattern.find("/", pos + 2);
(...skipping 23 matching lines...) Expand all
143 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 205 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
144 if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) 206 if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin))
145 return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthOrigin); 207 return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthOrigin);
146 #if defined(SPDY_PROXY_AUTH_ORIGIN) 208 #if defined(SPDY_PROXY_AUTH_ORIGIN)
147 return SPDY_PROXY_AUTH_ORIGIN; 209 return SPDY_PROXY_AUTH_ORIGIN;
148 #else 210 #else
149 return std::string(); 211 return std::string();
150 #endif 212 #endif
151 } 213 }
152 214
153 std::string DataReductionProxySettings::GetDataReductionProxyAuth() { 215 std::string DataReductionProxySettings::GetDataReductionProxyFallback() {
154 if (!IsDataReductionProxyAllowed()) 216 // Regardless of what else is defined, only return a value if the main proxy
217 // origin is defined.
218 if (GetDataReductionProxyOrigin().empty())
155 return std::string(); 219 return std::string();
156 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 220 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
157 if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) { 221 if (command_line.HasSwitch(switches::kSpdyProxyAuthFallback))
158 // If an origin is provided via a switch, then only consider the value 222 return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthFallback);
159 // that is provided by a switch. Do not use the preprocessor constant. 223 #if defined(DATA_REDUCTION_FALLBACK_HOST)
160 // Don't expose SPDY_PROXY_AUTH_VALUE to a proxy passed in via the command 224 return DATA_REDUCTION_FALLBACK_HOST;
161 // line.
162 if (command_line.HasSwitch(switches::kSpdyProxyAuthValue))
163 return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthValue);
164 return std::string();
165 }
166 #if defined(SPDY_PROXY_AUTH_VALUE)
167 return SPDY_PROXY_AUTH_VALUE;
168 #else 225 #else
169 return std::string(); 226 return std::string();
170 #endif 227 #endif
171 } 228 }
172 229
230 bool DataReductionProxySettings::IsAcceptableAuthChallenge(
231 net::AuthChallengeInfo* auth_info) {
232 // Challenge realm must start with the authentication realm name.
233 std::string realm_prefix =
234 auth_info->realm.substr(0, strlen(kAuthenticationRealmName));
235 if (realm_prefix != kAuthenticationRealmName)
236 return false;
237
238 // The challenger must be one of the configured proxies.
239 DataReductionProxyList proxies = GetDataReductionProxies();
240 for (DataReductionProxyList::iterator it = proxies.begin();
241 it != proxies.end(); ++it) {
242 net::HostPortPair origin_host = net::HostPortPair::FromURL(*it);
243 if (origin_host.Equals(auth_info->challenger))
244 return true;
245 }
246 return false;
247 }
248
249 base::string16 DataReductionProxySettings::GetTokenForAuthChallenge(
250 net::AuthChallengeInfo* auth_info) {
251 if (auth_info->realm.length() > strlen(kAuthenticationRealmName)) {
252 int64 salt;
253 std::string realm_suffix =
254 auth_info->realm.substr(strlen(kAuthenticationRealmName));
255 if (base::StringToInt64(realm_suffix, &salt)) {
256 return AuthHashForSalt(salt);
257 } else {
258 DVLOG(1) << "Unable to parse realm name " << auth_info->realm
259 << "into an int for salting.";
260 return base::string16();
261 }
262 } else {
263 return base::string16();
264 }
265 }
266
173 bool DataReductionProxySettings::IsDataReductionProxyEnabled() { 267 bool DataReductionProxySettings::IsDataReductionProxyEnabled() {
174 return spdy_proxy_auth_enabled_.GetValue(); 268 return spdy_proxy_auth_enabled_.GetValue();
175 } 269 }
176 270
177 bool DataReductionProxySettings::IsDataReductionProxyManaged() { 271 bool DataReductionProxySettings::IsDataReductionProxyManaged() {
178 return spdy_proxy_auth_enabled_.IsManaged(); 272 return spdy_proxy_auth_enabled_.IsManaged();
179 } 273 }
180 274
275 DataReductionProxySettings::DataReductionProxyList
276 DataReductionProxySettings::GetDataReductionProxies() {
277 DataReductionProxyList proxies;
278 std::string proxy = GetDataReductionProxyOrigin();
279 std::string fallback = GetDataReductionProxyFallback();
280
281 if (!proxy.empty())
282 proxies.push_back(GURL(proxy));
283
284 if (!fallback.empty()) {
285 // Sanity check: fallback isn't the only proxy.
286 DCHECK(!proxies.empty());
287 proxies.push_back(GURL(fallback));
288 }
289
290 return proxies;
291 }
292
181 void DataReductionProxySettings::SetDataReductionProxyEnabled(bool enabled) { 293 void DataReductionProxySettings::SetDataReductionProxyEnabled(bool enabled) {
182 // Prevent configuring the proxy when it is not allowed to be used. 294 // Prevent configuring the proxy when it is not allowed to be used.
183 if (!IsDataReductionProxyAllowed()) 295 if (!IsDataReductionProxyAllowed())
184 return; 296 return;
185 297
186 spdy_proxy_auth_enabled_.SetValue(enabled); 298 spdy_proxy_auth_enabled_.SetValue(enabled);
187 OnProxyEnabledPrefChange(); 299 OnProxyEnabledPrefChange();
188 } 300 }
189 301
190 int64 DataReductionProxySettings::GetDataReductionLastUpdateTime() { 302 int64 DataReductionProxySettings::GetDataReductionLastUpdateTime() {
191 PrefService* local_state = GetLocalStatePrefs(); 303 PrefService* local_state = GetLocalStatePrefs();
192 int64 last_update_internal = 304 int64 last_update_internal =
193 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); 305 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate);
194 base::Time last_update = base::Time::FromInternalValue(last_update_internal); 306 base::Time last_update = base::Time::FromInternalValue(last_update_internal);
195 return static_cast<int64>(last_update.ToJsTime()); 307 return static_cast<int64>(last_update.ToJsTime());
196 } 308 }
197 309
198 std::vector<long long> 310 DataReductionProxySettings::ContentLengthList
199 DataReductionProxySettings::GetDailyOriginalContentLengths() { 311 DataReductionProxySettings::GetDailyOriginalContentLengths() {
200 return GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength); 312 return GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength);
201 } 313 }
202 314
203 std::vector<long long> 315 DataReductionProxySettings::ContentLengthList
204 DataReductionProxySettings::GetDailyReceivedContentLengths() { 316 DataReductionProxySettings::GetDailyReceivedContentLengths() {
205 return GetDailyContentLengths(prefs::kDailyHttpReceivedContentLength); 317 return GetDailyContentLengths(prefs::kDailyHttpReceivedContentLength);
206 } 318 }
207 319
208 void DataReductionProxySettings::OnURLFetchComplete( 320 void DataReductionProxySettings::OnURLFetchComplete(
209 const net::URLFetcher* source) { 321 const net::URLFetcher* source) {
210 net::URLRequestStatus status = source->GetStatus(); 322 net::URLRequestStatus status = source->GetStatus();
211 if (status.status() == net::URLRequestStatus::FAILED && 323 if (status.status() == net::URLRequestStatus::FAILED &&
212 status.error() == net::ERR_INTERNET_DISCONNECTED) { 324 status.error() == net::ERR_INTERNET_DISCONNECTED) {
213 return; 325 return;
(...skipping 16 matching lines...) Expand all
230 } 342 }
231 DVLOG(1) << "The data reduction proxy is blocked."; 343 DVLOG(1) << "The data reduction proxy is blocked.";
232 344
233 if (enabled_by_user_ && !disabled_by_carrier_) { 345 if (enabled_by_user_ && !disabled_by_carrier_) {
234 // Disable the proxy. 346 // Disable the proxy.
235 SetProxyConfigs(false, false); 347 SetProxyConfigs(false, false);
236 } 348 }
237 disabled_by_carrier_ = true; 349 disabled_by_carrier_ = true;
238 } 350 }
239 351
240 std::string DataReductionProxySettings::GetDataReductionProxyOriginHostPort() {
241 std::string spdy_proxy = GetDataReductionProxyOrigin();
242 if (spdy_proxy.empty()) {
243 DLOG(ERROR) << "A SPDY proxy has not been set.";
244 return spdy_proxy;
245 }
246 // Remove a trailing slash from the proxy string if one exists as well as
247 // leading HTTPS scheme.
248 return net::HostPortPair::FromURL(GURL(spdy_proxy)).ToString();
249 }
250
251 void DataReductionProxySettings::OnIPAddressChanged() { 352 void DataReductionProxySettings::OnIPAddressChanged() {
252 if (enabled_by_user_) { 353 if (enabled_by_user_) {
253 DCHECK(IsDataReductionProxyAllowed()); 354 DCHECK(IsDataReductionProxyAllowed());
254 ProbeWhetherDataReductionProxyIsAvailable(); 355 ProbeWhetherDataReductionProxyIsAvailable();
255 } 356 }
256 } 357 }
257 358
258 void DataReductionProxySettings::OnProxyEnabledPrefChange() { 359 void DataReductionProxySettings::OnProxyEnabledPrefChange() {
259 if (!IsDataReductionProxyAllowed()) 360 if (!DataReductionProxySettings::IsDataReductionProxyAllowed())
260 return; 361 return;
261 MaybeActivateDataReductionProxy(false); 362 MaybeActivateDataReductionProxy(false);
262 } 363 }
263 364
264 void DataReductionProxySettings::AddDefaultProxyBypassRules() { 365 void DataReductionProxySettings::AddDefaultProxyBypassRules() {
265 // localhost 366 // localhost
266 AddHostPatternToBypass("<local>"); 367 AddHostPatternToBypass("<local>");
267 // RFC1918 private addresses. 368 // RFC1918 private addresses.
268 AddHostPatternToBypass("10.0.0.0/8"); 369 AddHostPatternToBypass("10.0.0.0/8");
269 AddHostPatternToBypass("172.16.0.0/12"); 370 AddHostPatternToBypass("172.16.0.0/12");
(...skipping 16 matching lines...) Expand all
286 387
287 PrefService* DataReductionProxySettings::GetOriginalProfilePrefs() { 388 PrefService* DataReductionProxySettings::GetOriginalProfilePrefs() {
288 return g_browser_process->profile_manager()->GetLastUsedProfile()-> 389 return g_browser_process->profile_manager()->GetLastUsedProfile()->
289 GetOriginalProfile()->GetPrefs(); 390 GetOriginalProfile()->GetPrefs();
290 } 391 }
291 392
292 PrefService* DataReductionProxySettings::GetLocalStatePrefs() { 393 PrefService* DataReductionProxySettings::GetLocalStatePrefs() {
293 return g_browser_process->local_state(); 394 return g_browser_process->local_state();
294 } 395 }
295 396
296 bool DataReductionProxySettings::IsProxyOriginSetOnCommandLine() {
297 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
298 return command_line.HasSwitch(switches::kSpdyProxyAuthOrigin);
299 }
300
301 void DataReductionProxySettings::ResetDataReductionStatistics() { 397 void DataReductionProxySettings::ResetDataReductionStatistics() {
302 PrefService* prefs = GetLocalStatePrefs(); 398 PrefService* prefs = GetLocalStatePrefs();
303 if (!prefs) 399 if (!prefs)
304 return; 400 return;
305 ListPrefUpdate original_update(prefs, prefs::kDailyHttpOriginalContentLength); 401 ListPrefUpdate original_update(prefs, prefs::kDailyHttpOriginalContentLength);
306 ListPrefUpdate received_update(prefs, prefs::kDailyHttpReceivedContentLength); 402 ListPrefUpdate received_update(prefs, prefs::kDailyHttpReceivedContentLength);
307 original_update->Clear(); 403 original_update->Clear();
308 received_update->Clear(); 404 received_update->Clear();
309 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { 405 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) {
310 original_update->AppendString(base::Int64ToString(0)); 406 original_update->AppendString(base::Int64ToString(0));
311 received_update->AppendString(base::Int64ToString(0)); 407 received_update->AppendString(base::Int64ToString(0));
312 } 408 }
313 } 409 }
314 410
315 void DataReductionProxySettings::MaybeActivateDataReductionProxy( 411 void DataReductionProxySettings::MaybeActivateDataReductionProxy(
316 bool at_startup) { 412 bool at_startup) {
317 PrefService* prefs = GetOriginalProfilePrefs(); 413 PrefService* prefs = GetOriginalProfilePrefs();
318 414
319 // TODO(marq): Consider moving this so stats are wiped the first time the 415 // TODO(marq): Consider moving this so stats are wiped the first time the
320 // proxy settings are actually (not maybe) turned on. 416 // proxy settings are actually (not maybe) turned on.
321 if (spdy_proxy_auth_enabled_.GetValue() && 417 if (spdy_proxy_auth_enabled_.GetValue() &&
322 !prefs->GetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore)) { 418 !prefs->GetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore)) {
323 prefs->SetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore, true); 419 prefs->SetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore, true);
324 ResetDataReductionStatistics(); 420 ResetDataReductionStatistics();
325 } 421 }
326 422
327 std::string spdy_proxy_origin = GetDataReductionProxyOriginHostPort(); 423 std::string proxy = GetDataReductionProxyOrigin();
328
329 // Configure use of the data reduction proxy if it is enabled and the proxy 424 // Configure use of the data reduction proxy if it is enabled and the proxy
330 // origin is non-empty. 425 // origin is non-empty.
331 enabled_by_user_= 426 enabled_by_user_= spdy_proxy_auth_enabled_.GetValue() && !proxy.empty();
332 spdy_proxy_auth_enabled_.GetValue() && !spdy_proxy_origin.empty();
333 SetProxyConfigs(enabled_by_user_ && !disabled_by_carrier_, at_startup); 427 SetProxyConfigs(enabled_by_user_ && !disabled_by_carrier_, at_startup);
334 428
335 // Check if the proxy has been disabled explicitly by the carrier. 429 // Check if the proxy has been disabled explicitly by the carrier.
336 if (enabled_by_user_) 430 if (enabled_by_user_)
337 ProbeWhetherDataReductionProxyIsAvailable(); 431 ProbeWhetherDataReductionProxyIsAvailable();
338 } 432 }
339 433
340 void DataReductionProxySettings::SetProxyConfigs(bool enabled, 434 void DataReductionProxySettings::SetProxyConfigs(bool enabled,
341 bool at_startup) { 435 bool at_startup) {
342 LogProxyState(enabled, at_startup); 436 LogProxyState(enabled, at_startup);
343 PrefService* prefs = GetOriginalProfilePrefs(); 437 PrefService* prefs = GetOriginalProfilePrefs();
344 DCHECK(prefs); 438 DCHECK(prefs);
345 DictionaryPrefUpdate update(prefs, prefs::kProxy); 439 DictionaryPrefUpdate update(prefs, prefs::kProxy);
346 base::DictionaryValue* dict = update.Get(); 440 base::DictionaryValue* dict = update.Get();
347 if (enabled) { 441 if (enabled) {
442 std::string fallback = GetDataReductionProxyFallback();
348 std::string proxy_server_config = 443 std::string proxy_server_config =
349 "http=" + GetDataReductionProxyOrigin() + ",direct://;"; 444 "http=" + GetDataReductionProxyOrigin() +
445 (fallback.empty() ? "" : "," + fallback) +
446 ",direct://;";
350 dict->SetString("server", proxy_server_config); 447 dict->SetString("server", proxy_server_config);
351 dict->SetString("mode", 448 dict->SetString("mode",
352 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS)); 449 ProxyModeToString(ProxyPrefs::MODE_FIXED_SERVERS));
353 dict->SetString("bypass_list", JoinString(bypass_rules_, ", ")); 450 dict->SetString("bypass_list", JoinString(bypass_rules_, ", "));
354 } else { 451 } else {
355 dict->SetString("mode", ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); 452 dict->SetString("mode", ProxyModeToString(ProxyPrefs::MODE_SYSTEM));
356 dict->SetString("server", ""); 453 dict->SetString("server", "");
357 dict->SetString("bypass_list", ""); 454 dict->SetString("bypass_list", "");
358 } 455 }
359 } 456 }
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 return command_line.GetSwitchValueASCII( 526 return command_line.GetSwitchValueASCII(
430 switches::kDataReductionProxyProbeURL); 527 switches::kDataReductionProxyProbeURL);
431 } 528 }
432 #if defined(DATA_REDUCTION_PROXY_PROBE_URL) 529 #if defined(DATA_REDUCTION_PROXY_PROBE_URL)
433 return DATA_REDUCTION_PROXY_PROBE_URL; 530 return DATA_REDUCTION_PROXY_PROBE_URL;
434 #else 531 #else
435 return std::string(); 532 return std::string();
436 #endif 533 #endif
437 } 534 }
438 535
536 base::string16 DataReductionProxySettings::AuthHashForSalt(int64 salt) {
537 if (!IsDataReductionProxyAllowed())
538 return base::string16();
539
540 std::string key;
541
542 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
543 if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) {
544 // If an origin is provided via a switch, then only consider the value
545 // that is provided by a switch. Do not use the preprocessor constant.
546 // Don't expose SPDY_PROXY_AUTH_VALUE to a proxy passed in via the command
547 // line.
548 if (!command_line.HasSwitch(switches::kSpdyProxyAuthValue))
549 return base::string16();
550 key = command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthValue);
551 } else {
552 #if defined(SPDY_PROXY_AUTH_VALUE)
553 key = SPDY_PROXY_AUTH_VALUE;
554 #else
555 return base::string16();
556 #endif
557 }
558
559 DCHECK(!key.empty());
560
561 std::string salted_key =
562 base::StringPrintf("%lld%s%lld", salt, key.c_str(), salt);
563 return UTF8ToUTF16(base::MD5String(salted_key));
564 }
565
439 net::URLFetcher* DataReductionProxySettings::GetURLFetcher() { 566 net::URLFetcher* DataReductionProxySettings::GetURLFetcher() {
440 std::string url = GetProxyCheckURL(); 567 std::string url = GetProxyCheckURL();
441 if (url.empty()) 568 if (url.empty())
442 return NULL; 569 return NULL;
443 net::URLFetcher* fetcher = net::URLFetcher::Create(GURL(url), 570 net::URLFetcher* fetcher = net::URLFetcher::Create(GURL(url),
444 net::URLFetcher::GET, 571 net::URLFetcher::GET,
445 this); 572 this);
446 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); 573 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE | net::LOAD_BYPASS_PROXY);
447 fetcher->SetLoadFlags(net::LOAD_BYPASS_PROXY);
448 Profile* profile = g_browser_process->profile_manager()-> 574 Profile* profile = g_browser_process->profile_manager()->
449 GetDefaultProfile(); 575 GetDefaultProfile();
450 fetcher->SetRequestContext(profile->GetRequestContext()); 576 fetcher->SetRequestContext(profile->GetRequestContext());
451 // Configure max retries to be at most kMaxRetries times for 5xx errors. 577 // Configure max retries to be at most kMaxRetries times for 5xx errors.
452 static const int kMaxRetries = 5; 578 static const int kMaxRetries = 5;
453 fetcher->SetMaxRetriesOn5xx(kMaxRetries); 579 fetcher->SetMaxRetriesOn5xx(kMaxRetries);
454 return fetcher; 580 return fetcher;
455 } 581 }
456 582
457 void 583 void
458 DataReductionProxySettings::ProbeWhetherDataReductionProxyIsAvailable() { 584 DataReductionProxySettings::ProbeWhetherDataReductionProxyIsAvailable() {
459 net::URLFetcher* fetcher = GetURLFetcher(); 585 net::URLFetcher* fetcher = GetURLFetcher();
460 if (!fetcher) 586 if (!fetcher)
461 return; 587 return;
462 fetcher_.reset(fetcher); 588 fetcher_.reset(fetcher);
463 fetcher_->Start(); 589 fetcher_->Start();
464 } 590 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698