| 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" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 | 86 |
| 87 namespace data_reduction_proxy { | 87 namespace data_reduction_proxy { |
| 88 | 88 |
| 89 DataReductionProxySettings::DataReductionProxySettings( | 89 DataReductionProxySettings::DataReductionProxySettings( |
| 90 DataReductionProxyParams* params) | 90 DataReductionProxyParams* params) |
| 91 : restricted_by_carrier_(false), | 91 : restricted_by_carrier_(false), |
| 92 enabled_by_user_(false), | 92 enabled_by_user_(false), |
| 93 disabled_on_vpn_(false), | 93 disabled_on_vpn_(false), |
| 94 unreachable_(false), | 94 unreachable_(false), |
| 95 prefs_(NULL), | 95 prefs_(NULL), |
| 96 local_state_prefs_(NULL), | |
| 97 url_request_context_getter_(NULL) { | 96 url_request_context_getter_(NULL) { |
| 98 DCHECK(params); | 97 DCHECK(params); |
| 99 params_.reset(params); | 98 params_.reset(params); |
| 100 } | 99 } |
| 101 | 100 |
| 102 DataReductionProxySettings::~DataReductionProxySettings() { | 101 DataReductionProxySettings::~DataReductionProxySettings() { |
| 103 if (params_->allowed()) | 102 if (params_->allowed()) |
| 104 spdy_proxy_auth_enabled_.Destroy(); | 103 spdy_proxy_auth_enabled_.Destroy(); |
| 105 } | 104 } |
| 106 | 105 |
| 107 void DataReductionProxySettings::InitPrefMembers() { | 106 void DataReductionProxySettings::InitPrefMembers() { |
| 108 DCHECK(thread_checker_.CalledOnValidThread()); | 107 DCHECK(thread_checker_.CalledOnValidThread()); |
| 109 spdy_proxy_auth_enabled_.Init( | 108 spdy_proxy_auth_enabled_.Init( |
| 110 prefs::kDataReductionProxyEnabled, | 109 prefs::kDataReductionProxyEnabled, |
| 111 GetOriginalProfilePrefs(), | 110 GetOriginalProfilePrefs(), |
| 112 base::Bind(&DataReductionProxySettings::OnProxyEnabledPrefChange, | 111 base::Bind(&DataReductionProxySettings::OnProxyEnabledPrefChange, |
| 113 base::Unretained(this))); | 112 base::Unretained(this))); |
| 114 data_reduction_proxy_alternative_enabled_.Init( | 113 data_reduction_proxy_alternative_enabled_.Init( |
| 115 prefs::kDataReductionProxyAltEnabled, | 114 prefs::kDataReductionProxyAltEnabled, |
| 116 GetOriginalProfilePrefs(), | 115 GetOriginalProfilePrefs(), |
| 117 base::Bind( | 116 base::Bind( |
| 118 &DataReductionProxySettings::OnProxyAlternativeEnabledPrefChange, | 117 &DataReductionProxySettings::OnProxyAlternativeEnabledPrefChange, |
| 119 base::Unretained(this))); | 118 base::Unretained(this))); |
| 120 } | 119 } |
| 121 | 120 |
| 122 void DataReductionProxySettings::InitDataReductionProxySettings( | 121 void DataReductionProxySettings::InitDataReductionProxySettings( |
| 123 PrefService* prefs, | 122 PrefService* prefs, |
| 124 PrefService* local_state_prefs, | |
| 125 net::URLRequestContextGetter* url_request_context_getter) { | 123 net::URLRequestContextGetter* url_request_context_getter) { |
| 126 DCHECK(thread_checker_.CalledOnValidThread()); | 124 DCHECK(thread_checker_.CalledOnValidThread()); |
| 127 DCHECK(prefs); | 125 DCHECK(prefs); |
| 128 DCHECK(local_state_prefs); | |
| 129 DCHECK(url_request_context_getter); | 126 DCHECK(url_request_context_getter); |
| 130 prefs_ = prefs; | 127 prefs_ = prefs; |
| 131 local_state_prefs_ = local_state_prefs; | |
| 132 url_request_context_getter_ = url_request_context_getter; | 128 url_request_context_getter_ = url_request_context_getter; |
| 133 InitPrefMembers(); | 129 InitPrefMembers(); |
| 134 RecordDataReductionInit(); | 130 RecordDataReductionInit(); |
| 135 | 131 |
| 136 // Disable the proxy if it is not allowed to be used. | 132 // Disable the proxy if it is not allowed to be used. |
| 137 if (!params_->allowed()) | 133 if (!params_->allowed()) |
| 138 return; | 134 return; |
| 139 | 135 |
| 140 AddDefaultProxyBypassRules(); | 136 AddDefaultProxyBypassRules(); |
| 141 net::NetworkChangeNotifier::AddIPAddressObserver(this); | 137 net::NetworkChangeNotifier::AddIPAddressObserver(this); |
| 142 | 138 |
| 143 // We set or reset the proxy pref at startup. | 139 // We set or reset the proxy pref at startup. |
| 144 MaybeActivateDataReductionProxy(true); | 140 MaybeActivateDataReductionProxy(true); |
| 145 } | 141 } |
| 146 | 142 |
| 147 void DataReductionProxySettings::InitDataReductionProxySettings( | 143 void DataReductionProxySettings::InitDataReductionProxySettings( |
| 148 PrefService* prefs, | 144 PrefService* prefs, |
| 149 PrefService* local_state_prefs, | |
| 150 net::URLRequestContextGetter* url_request_context_getter, | 145 net::URLRequestContextGetter* url_request_context_getter, |
| 151 scoped_ptr<DataReductionProxyConfigurator> configurator) { | 146 scoped_ptr<DataReductionProxyConfigurator> configurator) { |
| 152 InitDataReductionProxySettings(prefs, | 147 InitDataReductionProxySettings(prefs, |
| 153 local_state_prefs, | |
| 154 url_request_context_getter); | 148 url_request_context_getter); |
| 155 SetProxyConfigurator(configurator.Pass()); | 149 SetProxyConfigurator(configurator.Pass()); |
| 156 } | 150 } |
| 157 | 151 |
| 152 |
| 153 void DataReductionProxySettings::SetDataReductionProxyDelayedPrefService( |
| 154 base::WeakPtr<DataReductionProxyDelayedPrefService> delayed_prefs) { |
| 155 delayed_prefs_ = delayed_prefs; |
| 156 } |
| 157 |
| 158 void DataReductionProxySettings::SetOnDataReductionEnabledCallback( | 158 void DataReductionProxySettings::SetOnDataReductionEnabledCallback( |
| 159 const base::Callback<void(bool)>& on_data_reduction_proxy_enabled) { | 159 const base::Callback<void(bool)>& on_data_reduction_proxy_enabled) { |
| 160 on_data_reduction_proxy_enabled_ = on_data_reduction_proxy_enabled; | 160 on_data_reduction_proxy_enabled_ = on_data_reduction_proxy_enabled; |
| 161 on_data_reduction_proxy_enabled_.Run(IsDataReductionProxyEnabled()); | 161 on_data_reduction_proxy_enabled_.Run(IsDataReductionProxyEnabled()); |
| 162 } | 162 } |
| 163 | 163 |
| 164 void DataReductionProxySettings::SetProxyConfigurator( | 164 void DataReductionProxySettings::SetProxyConfigurator( |
| 165 scoped_ptr<DataReductionProxyConfigurator> configurator) { | 165 scoped_ptr<DataReductionProxyConfigurator> configurator) { |
| 166 DCHECK(configurator); | 166 DCHECK(configurator); |
| 167 configurator_ = configurator.Pass(); | 167 configurator_ = configurator.Pass(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 if (!params_->alternative_allowed()) | 199 if (!params_->alternative_allowed()) |
| 200 return; | 200 return; |
| 201 if (data_reduction_proxy_alternative_enabled_.GetValue() != enabled) { | 201 if (data_reduction_proxy_alternative_enabled_.GetValue() != enabled) { |
| 202 data_reduction_proxy_alternative_enabled_.SetValue(enabled); | 202 data_reduction_proxy_alternative_enabled_.SetValue(enabled); |
| 203 OnProxyAlternativeEnabledPrefChange(); | 203 OnProxyAlternativeEnabledPrefChange(); |
| 204 } | 204 } |
| 205 } | 205 } |
| 206 | 206 |
| 207 int64 DataReductionProxySettings::GetDataReductionLastUpdateTime() { | 207 int64 DataReductionProxySettings::GetDataReductionLastUpdateTime() { |
| 208 DCHECK(thread_checker_.CalledOnValidThread()); | 208 DCHECK(thread_checker_.CalledOnValidThread()); |
| 209 PrefService* local_state = GetLocalStatePrefs(); | 209 if (!delayed_prefs_) { |
| 210 return 0; |
| 211 } |
| 210 int64 last_update_internal = | 212 int64 last_update_internal = |
| 211 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); | 213 delayed_prefs_->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); |
| 212 base::Time last_update = base::Time::FromInternalValue(last_update_internal); | 214 base::Time last_update = base::Time::FromInternalValue(last_update_internal); |
| 213 return static_cast<int64>(last_update.ToJsTime()); | 215 return static_cast<int64>(last_update.ToJsTime()); |
| 214 } | 216 } |
| 215 | 217 |
| 216 DataReductionProxySettings::ContentLengthList | 218 DataReductionProxySettings::ContentLengthList |
| 217 DataReductionProxySettings::GetDailyOriginalContentLengths() { | 219 DataReductionProxySettings::GetDailyOriginalContentLengths() { |
| 218 DCHECK(thread_checker_.CalledOnValidThread()); | 220 DCHECK(thread_checker_.CalledOnValidThread()); |
| 219 return GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength); | 221 return GetDailyContentLengths(prefs::kDailyHttpOriginalContentLength); |
| 220 } | 222 } |
| 221 | 223 |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 } | 298 } |
| 297 } | 299 } |
| 298 restricted_by_carrier_ = true; | 300 restricted_by_carrier_ = true; |
| 299 } | 301 } |
| 300 | 302 |
| 301 PrefService* DataReductionProxySettings::GetOriginalProfilePrefs() { | 303 PrefService* DataReductionProxySettings::GetOriginalProfilePrefs() { |
| 302 DCHECK(thread_checker_.CalledOnValidThread()); | 304 DCHECK(thread_checker_.CalledOnValidThread()); |
| 303 return prefs_; | 305 return prefs_; |
| 304 } | 306 } |
| 305 | 307 |
| 306 PrefService* DataReductionProxySettings::GetLocalStatePrefs() { | |
| 307 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 308 return local_state_prefs_; | |
| 309 } | |
| 310 | |
| 311 void DataReductionProxySettings::AddDefaultProxyBypassRules() { | 308 void DataReductionProxySettings::AddDefaultProxyBypassRules() { |
| 312 // localhost | 309 // localhost |
| 313 DCHECK(configurator_); | 310 DCHECK(configurator_); |
| 314 configurator_->AddHostPatternToBypass("<local>"); | 311 configurator_->AddHostPatternToBypass("<local>"); |
| 315 // RFC1918 private addresses. | 312 // RFC1918 private addresses. |
| 316 configurator_->AddHostPatternToBypass("10.0.0.0/8"); | 313 configurator_->AddHostPatternToBypass("10.0.0.0/8"); |
| 317 configurator_->AddHostPatternToBypass("172.16.0.0/12"); | 314 configurator_->AddHostPatternToBypass("172.16.0.0/12"); |
| 318 configurator_->AddHostPatternToBypass("192.168.0.0/16"); | 315 configurator_->AddHostPatternToBypass("192.168.0.0/16"); |
| 319 // RFC4193 private addresses. | 316 // RFC4193 private addresses. |
| 320 configurator_->AddHostPatternToBypass("fc00::/7"); | 317 configurator_->AddHostPatternToBypass("fc00::/7"); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 | 361 |
| 365 void DataReductionProxySettings::OnProxyAlternativeEnabledPrefChange() { | 362 void DataReductionProxySettings::OnProxyAlternativeEnabledPrefChange() { |
| 366 DCHECK(thread_checker_.CalledOnValidThread()); | 363 DCHECK(thread_checker_.CalledOnValidThread()); |
| 367 if (!params_->alternative_allowed()) | 364 if (!params_->alternative_allowed()) |
| 368 return; | 365 return; |
| 369 MaybeActivateDataReductionProxy(false); | 366 MaybeActivateDataReductionProxy(false); |
| 370 } | 367 } |
| 371 | 368 |
| 372 void DataReductionProxySettings::ResetDataReductionStatistics() { | 369 void DataReductionProxySettings::ResetDataReductionStatistics() { |
| 373 DCHECK(thread_checker_.CalledOnValidThread()); | 370 DCHECK(thread_checker_.CalledOnValidThread()); |
| 374 PrefService* prefs = GetLocalStatePrefs(); | 371 if (!delayed_prefs_) |
| 375 if (!prefs) | |
| 376 return; | 372 return; |
| 377 ListPrefUpdate original_update(prefs, prefs::kDailyHttpOriginalContentLength); | 373 base::ListValue* original_update = |
| 378 ListPrefUpdate received_update(prefs, prefs::kDailyHttpReceivedContentLength); | 374 delayed_prefs_->GetList(prefs::kDailyHttpOriginalContentLength); |
| 375 base::ListValue* received_update = |
| 376 delayed_prefs_->GetList(prefs::kDailyHttpReceivedContentLength); |
| 379 original_update->Clear(); | 377 original_update->Clear(); |
| 380 received_update->Clear(); | 378 received_update->Clear(); |
| 381 for (size_t i = 0; i < kNumDaysInHistory; ++i) { | 379 for (size_t i = 0; i < kNumDaysInHistory; ++i) { |
| 382 original_update->AppendString(base::Int64ToString(0)); | 380 original_update->AppendString(base::Int64ToString(0)); |
| 383 received_update->AppendString(base::Int64ToString(0)); | 381 received_update->AppendString(base::Int64ToString(0)); |
| 384 } | 382 } |
| 385 } | 383 } |
| 386 | 384 |
| 387 void DataReductionProxySettings::MaybeActivateDataReductionProxy( | 385 void DataReductionProxySettings::MaybeActivateDataReductionProxy( |
| 388 bool at_startup) { | 386 bool at_startup) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 | 471 |
| 474 void DataReductionProxySettings::ResetParamsForTest( | 472 void DataReductionProxySettings::ResetParamsForTest( |
| 475 DataReductionProxyParams* params) { | 473 DataReductionProxyParams* params) { |
| 476 params_.reset(params); | 474 params_.reset(params); |
| 477 } | 475 } |
| 478 | 476 |
| 479 DataReductionProxySettings::ContentLengthList | 477 DataReductionProxySettings::ContentLengthList |
| 480 DataReductionProxySettings::GetDailyContentLengths(const char* pref_name) { | 478 DataReductionProxySettings::GetDailyContentLengths(const char* pref_name) { |
| 481 DCHECK(thread_checker_.CalledOnValidThread()); | 479 DCHECK(thread_checker_.CalledOnValidThread()); |
| 482 DataReductionProxySettings::ContentLengthList content_lengths; | 480 DataReductionProxySettings::ContentLengthList content_lengths; |
| 483 const base::ListValue* list_value = GetLocalStatePrefs()->GetList(pref_name); | 481 if (!delayed_prefs_) { |
| 482 return content_lengths; |
| 483 } |
| 484 const base::ListValue* list_value = delayed_prefs_->GetList(pref_name); |
| 484 if (list_value->GetSize() == kNumDaysInHistory) { | 485 if (list_value->GetSize() == kNumDaysInHistory) { |
| 485 for (size_t i = 0; i < kNumDaysInHistory; ++i) { | 486 for (size_t i = 0; i < kNumDaysInHistory; ++i) { |
| 486 content_lengths.push_back(GetInt64PrefValue(*list_value, i)); | 487 content_lengths.push_back(GetInt64PrefValue(*list_value, i)); |
| 487 } | 488 } |
| 488 } | 489 } |
| 489 return content_lengths; | 490 return content_lengths; |
| 490 } | 491 } |
| 491 | 492 |
| 492 void DataReductionProxySettings::GetContentLengths( | 493 void DataReductionProxySettings::GetContentLengths( |
| 493 unsigned int days, | 494 unsigned int days, |
| 494 int64* original_content_length, | 495 int64* original_content_length, |
| 495 int64* received_content_length, | 496 int64* received_content_length, |
| 496 int64* last_update_time) { | 497 int64* last_update_time) { |
| 497 DCHECK(thread_checker_.CalledOnValidThread()); | 498 DCHECK(thread_checker_.CalledOnValidThread()); |
| 498 DCHECK_LE(days, kNumDaysInHistory); | 499 DCHECK_LE(days, kNumDaysInHistory); |
| 499 PrefService* local_state = GetLocalStatePrefs(); | 500 if (!delayed_prefs_) { |
| 500 if (!local_state) { | |
| 501 *original_content_length = 0L; | 501 *original_content_length = 0L; |
| 502 *received_content_length = 0L; | 502 *received_content_length = 0L; |
| 503 *last_update_time = 0L; | 503 *last_update_time = 0L; |
| 504 return; | 504 return; |
| 505 } | 505 } |
| 506 | 506 |
| 507 const base::ListValue* original_list = | 507 const base::ListValue* original_list = |
| 508 local_state->GetList(prefs::kDailyHttpOriginalContentLength); | 508 delayed_prefs_->GetList(prefs::kDailyHttpOriginalContentLength); |
| 509 const base::ListValue* received_list = | 509 const base::ListValue* received_list = |
| 510 local_state->GetList(prefs::kDailyHttpReceivedContentLength); | 510 delayed_prefs_->GetList(prefs::kDailyHttpReceivedContentLength); |
| 511 | 511 |
| 512 if (original_list->GetSize() != kNumDaysInHistory || | 512 if (original_list->GetSize() != kNumDaysInHistory || |
| 513 received_list->GetSize() != kNumDaysInHistory) { | 513 received_list->GetSize() != kNumDaysInHistory) { |
| 514 *original_content_length = 0L; | 514 *original_content_length = 0L; |
| 515 *received_content_length = 0L; | 515 *received_content_length = 0L; |
| 516 *last_update_time = 0L; | 516 *last_update_time = 0L; |
| 517 return; | 517 return; |
| 518 } | 518 } |
| 519 | 519 |
| 520 int64 orig = 0L; | 520 int64 orig = 0L; |
| 521 int64 recv = 0L; | 521 int64 recv = 0L; |
| 522 // Include days from the end of the list going backwards. | 522 // Include days from the end of the list going backwards. |
| 523 for (size_t i = kNumDaysInHistory - days; | 523 for (size_t i = kNumDaysInHistory - days; |
| 524 i < kNumDaysInHistory; ++i) { | 524 i < kNumDaysInHistory; ++i) { |
| 525 orig += GetInt64PrefValue(*original_list, i); | 525 orig += GetInt64PrefValue(*original_list, i); |
| 526 recv += GetInt64PrefValue(*received_list, i); | 526 recv += GetInt64PrefValue(*received_list, i); |
| 527 } | 527 } |
| 528 *original_content_length = orig; | 528 *original_content_length = orig; |
| 529 *received_content_length = recv; | 529 *received_content_length = recv; |
| 530 *last_update_time = | 530 *last_update_time = |
| 531 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); | 531 delayed_prefs_->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); |
| 532 } | 532 } |
| 533 | 533 |
| 534 net::URLFetcher* DataReductionProxySettings::GetBaseURLFetcher( | 534 net::URLFetcher* DataReductionProxySettings::GetBaseURLFetcher( |
| 535 const GURL& gurl, | 535 const GURL& gurl, |
| 536 int load_flags) { | 536 int load_flags) { |
| 537 | 537 |
| 538 net::URLFetcher* fetcher = net::URLFetcher::Create(gurl, | 538 net::URLFetcher* fetcher = net::URLFetcher::Create(gurl, |
| 539 net::URLFetcher::GET, | 539 net::URLFetcher::GET, |
| 540 this); | 540 this); |
| 541 fetcher->SetLoadFlags(load_flags); | 541 fetcher->SetLoadFlags(load_flags); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 602 SetProxyConfigs(enabled_by_user_, | 602 SetProxyConfigs(enabled_by_user_, |
| 603 IsDataReductionProxyAlternativeEnabled(), | 603 IsDataReductionProxyAlternativeEnabled(), |
| 604 restricted_by_carrier_, | 604 restricted_by_carrier_, |
| 605 false); | 605 false); |
| 606 } | 606 } |
| 607 disabled_on_vpn_ = false; | 607 disabled_on_vpn_ = false; |
| 608 return false; | 608 return false; |
| 609 } | 609 } |
| 610 | 610 |
| 611 } // namespace data_reduction_proxy | 611 } // namespace data_reduction_proxy |
| OLD | NEW |