| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 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 "components/data_reduction_proxy/browser/data_reduction_proxy_settings.
h" | 5 #include "components/data_reduction_proxy/browser/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/prefs/scoped_user_pref_update.h" | 13 #include "base/prefs/scoped_user_pref_update.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_requ
est_handler.h" | 18 #include "components/data_reduction_proxy/browser/data_reduction_proxy_auth_requ
est_handler.h" |
| 19 #include "components/data_reduction_proxy/browser/data_reduction_proxy_configura
tor.h" | 19 #include "components/data_reduction_proxy/browser/data_reduction_proxy_configura
tor.h" |
| 20 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" | 20 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" |
| 21 #include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_sta
ts.h" | 21 #include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_sta
ts.h" |
| 22 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names
.h" | 22 #include "components/data_reduction_proxy/common/data_reduction_proxy_pref_names
.h" |
| 23 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h
" | 23 #include "components/data_reduction_proxy/common/data_reduction_proxy_switches.h
" |
| 24 #include "net/base/host_port_pair.h" | 24 #include "net/base/host_port_pair.h" |
| 25 #include "net/base/load_flags.h" | 25 #include "net/base/load_flags.h" |
| 26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 27 #include "net/base/net_util.h" |
| 27 #include "net/http/http_network_session.h" | 28 #include "net/http/http_network_session.h" |
| 28 #include "net/http/http_response_headers.h" | 29 #include "net/http/http_response_headers.h" |
| 29 #include "net/url_request/url_fetcher.h" | 30 #include "net/url_request/url_fetcher.h" |
| 30 #include "net/url_request/url_fetcher_delegate.h" | 31 #include "net/url_request/url_fetcher_delegate.h" |
| 31 #include "net/url_request/url_request_context_getter.h" | 32 #include "net/url_request/url_request_context_getter.h" |
| 32 #include "net/url_request/url_request_status.h" | 33 #include "net/url_request/url_request_status.h" |
| 33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
| 34 | 35 |
| 35 | 36 |
| 36 using base::StringPrintf; | 37 using base::StringPrintf; |
| 37 | 38 |
| 38 namespace { | 39 namespace { |
| 40 // Values of the UMA DataReductionProxy.NetworkChangeEvents histograms. |
| 41 // This enum must remain synchronized with the enum of the same |
| 42 // name in metrics/histograms/histograms.xml. |
| 43 enum DataReductionProxyNetworkChangeEvent { |
| 44 IP_CHANGED = 0, // The client IP address changed. |
| 45 DISABLED_ON_VPN = 1, // The proxy is disabled because a VPN is running. |
| 46 CHANGE_EVENT_COUNT = 2 // This must always be last. |
| 47 }; |
| 39 | 48 |
| 40 // Key of the UMA DataReductionProxy.StartupState histogram. | 49 // Key of the UMA DataReductionProxy.StartupState histogram. |
| 41 const char kUMAProxyStartupStateHistogram[] = | 50 const char kUMAProxyStartupStateHistogram[] = |
| 42 "DataReductionProxy.StartupState"; | 51 "DataReductionProxy.StartupState"; |
| 43 | 52 |
| 44 // Key of the UMA DataReductionProxy.ProbeURL histogram. | 53 // Key of the UMA DataReductionProxy.ProbeURL histogram. |
| 45 const char kUMAProxyProbeURL[] = "DataReductionProxy.ProbeURL"; | 54 const char kUMAProxyProbeURL[] = "DataReductionProxy.ProbeURL"; |
| 46 | 55 |
| 56 // Record a network change event. |
| 57 void RecordNetworkChangeEvent(DataReductionProxyNetworkChangeEvent event) { |
| 58 UMA_HISTOGRAM_ENUMERATION("DataReductionProxy.NetworkChangeEvents", |
| 59 event, |
| 60 CHANGE_EVENT_COUNT); |
| 61 } |
| 62 |
| 47 int64 GetInt64PrefValue(const base::ListValue& list_value, size_t index) { | 63 int64 GetInt64PrefValue(const base::ListValue& list_value, size_t index) { |
| 48 int64 val = 0; | 64 int64 val = 0; |
| 49 std::string pref_value; | 65 std::string pref_value; |
| 50 bool rv = list_value.GetString(index, &pref_value); | 66 bool rv = list_value.GetString(index, &pref_value); |
| 51 DCHECK(rv); | 67 DCHECK(rv); |
| 52 if (rv) { | 68 if (rv) { |
| 53 rv = base::StringToInt64(pref_value, &val); | 69 rv = base::StringToInt64(pref_value, &val); |
| 54 DCHECK(rv); | 70 DCHECK(rv); |
| 55 } | 71 } |
| 56 return val; | 72 return val; |
| 57 } | 73 } |
| 58 | 74 |
| 59 bool IsEnabledOnCommandLine() { | 75 bool IsEnabledOnCommandLine() { |
| 60 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 76 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 61 return command_line.HasSwitch( | 77 return command_line.HasSwitch( |
| 62 data_reduction_proxy::switches::kEnableDataReductionProxy); | 78 data_reduction_proxy::switches::kEnableDataReductionProxy); |
| 63 } | 79 } |
| 64 | 80 |
| 65 } // namespace | 81 } // namespace |
| 66 | 82 |
| 67 namespace data_reduction_proxy { | 83 namespace data_reduction_proxy { |
| 68 | 84 |
| 69 DataReductionProxySettings::DataReductionProxySettings( | 85 DataReductionProxySettings::DataReductionProxySettings( |
| 70 DataReductionProxyParams* params) | 86 DataReductionProxyParams* params) |
| 71 : restricted_by_carrier_(false), | 87 : restricted_by_carrier_(false), |
| 72 enabled_by_user_(false), | 88 enabled_by_user_(false), |
| 89 disabled_on_vpn_(false), |
| 73 prefs_(NULL), | 90 prefs_(NULL), |
| 74 local_state_prefs_(NULL), | 91 local_state_prefs_(NULL), |
| 75 url_request_context_getter_(NULL), | 92 url_request_context_getter_(NULL) { |
| 76 usage_stats_(NULL) { | |
| 77 DCHECK(params); | 93 DCHECK(params); |
| 78 params_.reset(params); | 94 params_.reset(params); |
| 79 } | 95 } |
| 80 | 96 |
| 81 DataReductionProxySettings::~DataReductionProxySettings() { | 97 DataReductionProxySettings::~DataReductionProxySettings() { |
| 82 if (params_->allowed()) | 98 if (params_->allowed()) |
| 83 spdy_proxy_auth_enabled_.Destroy(); | 99 spdy_proxy_auth_enabled_.Destroy(); |
| 84 } | 100 } |
| 85 | 101 |
| 86 void DataReductionProxySettings::InitPrefMembers() { | 102 void DataReductionProxySettings::InitPrefMembers() { |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 kOn + std::string(" ") + (restricted ? kRestricted : kUnrestricted); | 321 kOn + std::string(" ") + (restricted ? kRestricted : kUnrestricted); |
| 306 | 322 |
| 307 LOG(WARNING) << "SPDY proxy " << (enabled ? annotated_on : kOff) | 323 LOG(WARNING) << "SPDY proxy " << (enabled ? annotated_on : kOff) |
| 308 << " " << (at_startup ? kAtStartup : kByUser); | 324 << " " << (at_startup ? kAtStartup : kByUser); |
| 309 } | 325 } |
| 310 | 326 |
| 311 void DataReductionProxySettings::OnIPAddressChanged() { | 327 void DataReductionProxySettings::OnIPAddressChanged() { |
| 312 DCHECK(thread_checker_.CalledOnValidThread()); | 328 DCHECK(thread_checker_.CalledOnValidThread()); |
| 313 if (enabled_by_user_) { | 329 if (enabled_by_user_) { |
| 314 DCHECK(params_->allowed()); | 330 DCHECK(params_->allowed()); |
| 331 RecordNetworkChangeEvent(IP_CHANGED); |
| 332 if (DisableIfVPN()) |
| 333 return; |
| 315 ProbeWhetherDataReductionProxyIsAvailable(); | 334 ProbeWhetherDataReductionProxyIsAvailable(); |
| 316 WarmProxyConnection(); | 335 WarmProxyConnection(); |
| 317 } | 336 } |
| 318 } | 337 } |
| 319 | 338 |
| 320 void DataReductionProxySettings::OnProxyEnabledPrefChange() { | 339 void DataReductionProxySettings::OnProxyEnabledPrefChange() { |
| 321 DCHECK(thread_checker_.CalledOnValidThread()); | 340 DCHECK(thread_checker_.CalledOnValidThread()); |
| 322 if (!params_->allowed()) | 341 if (!params_->allowed()) |
| 323 return; | 342 return; |
| 324 MaybeActivateDataReductionProxy(false); | 343 MaybeActivateDataReductionProxy(false); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 353 // TODO(marq): Consider moving this so stats are wiped the first time the | 372 // TODO(marq): Consider moving this so stats are wiped the first time the |
| 354 // proxy settings are actually (not maybe) turned on. | 373 // proxy settings are actually (not maybe) turned on. |
| 355 if (spdy_proxy_auth_enabled_.GetValue() && | 374 if (spdy_proxy_auth_enabled_.GetValue() && |
| 356 !prefs->GetBoolean(prefs::kDataReductionProxyWasEnabledBefore)) { | 375 !prefs->GetBoolean(prefs::kDataReductionProxyWasEnabledBefore)) { |
| 357 prefs->SetBoolean(prefs::kDataReductionProxyWasEnabledBefore, true); | 376 prefs->SetBoolean(prefs::kDataReductionProxyWasEnabledBefore, true); |
| 358 ResetDataReductionStatistics(); | 377 ResetDataReductionStatistics(); |
| 359 } | 378 } |
| 360 | 379 |
| 361 // Configure use of the data reduction proxy if it is enabled. | 380 // Configure use of the data reduction proxy if it is enabled. |
| 362 enabled_by_user_= IsDataReductionProxyEnabled(); | 381 enabled_by_user_= IsDataReductionProxyEnabled(); |
| 363 SetProxyConfigs(enabled_by_user_, | 382 SetProxyConfigs(enabled_by_user_ && !disabled_on_vpn_, |
| 364 IsDataReductionProxyAlternativeEnabled(), | 383 IsDataReductionProxyAlternativeEnabled(), |
| 365 restricted_by_carrier_, | 384 restricted_by_carrier_, |
| 366 at_startup); | 385 at_startup); |
| 367 | 386 |
| 368 // Check if the proxy has been restricted explicitly by the carrier. | 387 // Check if the proxy has been restricted explicitly by the carrier. |
| 369 if (enabled_by_user_) { | 388 if (enabled_by_user_ && !disabled_on_vpn_) { |
| 370 ProbeWhetherDataReductionProxyIsAvailable(); | 389 ProbeWhetherDataReductionProxyIsAvailable(); |
| 371 WarmProxyConnection(); | 390 WarmProxyConnection(); |
| 372 } | 391 } |
| 373 } | 392 } |
| 374 | 393 |
| 375 void DataReductionProxySettings::SetProxyConfigs(bool enabled, | 394 void DataReductionProxySettings::SetProxyConfigs(bool enabled, |
| 376 bool alternative_enabled, | 395 bool alternative_enabled, |
| 377 bool restricted, | 396 bool restricted, |
| 378 bool at_startup) { | 397 bool at_startup) { |
| 379 DCHECK(thread_checker_.CalledOnValidThread()); | 398 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 result, | 438 result, |
| 420 PROBE_URL_FETCH_RESULT_COUNT); | 439 PROBE_URL_FETCH_RESULT_COUNT); |
| 421 } | 440 } |
| 422 | 441 |
| 423 void DataReductionProxySettings::RecordStartupState(ProxyStartupState state) { | 442 void DataReductionProxySettings::RecordStartupState(ProxyStartupState state) { |
| 424 UMA_HISTOGRAM_ENUMERATION(kUMAProxyStartupStateHistogram, | 443 UMA_HISTOGRAM_ENUMERATION(kUMAProxyStartupStateHistogram, |
| 425 state, | 444 state, |
| 426 PROXY_STARTUP_STATE_COUNT); | 445 PROXY_STARTUP_STATE_COUNT); |
| 427 } | 446 } |
| 428 | 447 |
| 448 void DataReductionProxySettings::GetNetworkList( |
| 449 net::NetworkInterfaceList* interfaces, |
| 450 int policy) { |
| 451 net::GetNetworkList(interfaces, policy); |
| 452 } |
| 453 |
| 429 void DataReductionProxySettings::ResetParamsForTest( | 454 void DataReductionProxySettings::ResetParamsForTest( |
| 430 DataReductionProxyParams* params) { | 455 DataReductionProxyParams* params) { |
| 431 params_.reset(params); | 456 params_.reset(params); |
| 432 } | 457 } |
| 433 | 458 |
| 434 DataReductionProxySettings::ContentLengthList | 459 DataReductionProxySettings::ContentLengthList |
| 435 DataReductionProxySettings::GetDailyContentLengths(const char* pref_name) { | 460 DataReductionProxySettings::GetDailyContentLengths(const char* pref_name) { |
| 436 DCHECK(thread_checker_.CalledOnValidThread()); | 461 DCHECK(thread_checker_.CalledOnValidThread()); |
| 437 DataReductionProxySettings::ContentLengthList content_lengths; | 462 DataReductionProxySettings::ContentLengthList content_lengths; |
| 438 const base::ListValue* list_value = GetLocalStatePrefs()->GetList(pref_name); | 463 const base::ListValue* list_value = GetLocalStatePrefs()->GetList(pref_name); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 479 i < kNumDaysInHistory; ++i) { | 504 i < kNumDaysInHistory; ++i) { |
| 480 orig += GetInt64PrefValue(*original_list, i); | 505 orig += GetInt64PrefValue(*original_list, i); |
| 481 recv += GetInt64PrefValue(*received_list, i); | 506 recv += GetInt64PrefValue(*received_list, i); |
| 482 } | 507 } |
| 483 *original_content_length = orig; | 508 *original_content_length = orig; |
| 484 *received_content_length = recv; | 509 *received_content_length = recv; |
| 485 *last_update_time = | 510 *last_update_time = |
| 486 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); | 511 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); |
| 487 } | 512 } |
| 488 | 513 |
| 489 // static | |
| 490 base::string16 DataReductionProxySettings::AuthHashForSalt( | |
| 491 int64 salt, | |
| 492 const std::string& key) { | |
| 493 std::string salted_key = | |
| 494 base::StringPrintf("%lld%s%lld", | |
| 495 static_cast<long long>(salt), | |
| 496 key.c_str(), | |
| 497 static_cast<long long>(salt)); | |
| 498 return base::UTF8ToUTF16(base::MD5String(salted_key)); | |
| 499 } | |
| 500 | |
| 501 net::URLFetcher* DataReductionProxySettings::GetBaseURLFetcher( | 514 net::URLFetcher* DataReductionProxySettings::GetBaseURLFetcher( |
| 502 const GURL& gurl, | 515 const GURL& gurl, |
| 503 int load_flags) { | 516 int load_flags) { |
| 504 | 517 |
| 505 net::URLFetcher* fetcher = net::URLFetcher::Create(gurl, | 518 net::URLFetcher* fetcher = net::URLFetcher::Create(gurl, |
| 506 net::URLFetcher::GET, | 519 net::URLFetcher::GET, |
| 507 this); | 520 this); |
| 508 fetcher->SetLoadFlags(load_flags); | 521 fetcher->SetLoadFlags(load_flags); |
| 509 DCHECK(url_request_context_getter_); | 522 DCHECK(url_request_context_getter_); |
| 510 fetcher->SetRequestContext(url_request_context_getter_); | 523 fetcher->SetRequestContext(url_request_context_getter_); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 536 } | 549 } |
| 537 | 550 |
| 538 void DataReductionProxySettings::WarmProxyConnection() { | 551 void DataReductionProxySettings::WarmProxyConnection() { |
| 539 net::URLFetcher* fetcher = GetURLFetcherForWarmup(); | 552 net::URLFetcher* fetcher = GetURLFetcherForWarmup(); |
| 540 if (!fetcher) | 553 if (!fetcher) |
| 541 return; | 554 return; |
| 542 warmup_fetcher_.reset(fetcher); | 555 warmup_fetcher_.reset(fetcher); |
| 543 warmup_fetcher_->Start(); | 556 warmup_fetcher_->Start(); |
| 544 } | 557 } |
| 545 | 558 |
| 559 bool DataReductionProxySettings::DisableIfVPN() { |
| 560 net::NetworkInterfaceList network_interfaces; |
| 561 GetNetworkList(&network_interfaces, 0); |
| 562 // VPNs use a "tun" interface, so the presence of a "tun" interface indicates |
| 563 // a VPN is in use. |
| 564 // TODO(kundaji): Verify this works on Windows. |
| 565 const std::string vpn_interface_name_prefix = "tun"; |
| 566 for (size_t i = 0; i < network_interfaces.size(); ++i) { |
| 567 std::string interface_name = network_interfaces[i].name; |
| 568 if (LowerCaseEqualsASCII( |
| 569 interface_name.begin(), |
| 570 interface_name.begin() + vpn_interface_name_prefix.size(), |
| 571 vpn_interface_name_prefix.c_str())) { |
| 572 SetProxyConfigs(false, |
| 573 IsDataReductionProxyAlternativeEnabled(), |
| 574 false, |
| 575 false); |
| 576 disabled_on_vpn_ = true; |
| 577 RecordNetworkChangeEvent(DISABLED_ON_VPN); |
| 578 return true; |
| 579 } |
| 580 } |
| 581 if (disabled_on_vpn_) { |
| 582 SetProxyConfigs(enabled_by_user_, |
| 583 IsDataReductionProxyAlternativeEnabled(), |
| 584 restricted_by_carrier_, |
| 585 false); |
| 586 } |
| 587 disabled_on_vpn_ = false; |
| 588 return false; |
| 589 } |
| 590 |
| 546 } // namespace data_reduction_proxy | 591 } // namespace data_reduction_proxy |
| OLD | NEW |