Commit 5bb90a92 authored by kundaji's avatar kundaji Committed by Commit bot

Reset data reduction proxy state when data savings is switched off.

Reset unreachable data reduction proxy message when data savings is
switched off. Clear counts of successful requests through proxy and
net errors.

Reset bad proxy list so that the data reduction proxies are retried when Data
Saver is turned back on.

BUG=436326,438694

Review URL: https://codereview.chromium.org/1006583002

Cr-Commit-Position: refs/heads/master@{#330157}
parent defd2af0
......@@ -223,6 +223,21 @@ void DataReductionProxyBypassStats::OnConnectComplete(
}
}
void DataReductionProxyBypassStats::ClearRequestCounts() {
successful_requests_through_proxy_count_ = 0;
proxy_net_errors_count_ = 0;
}
void DataReductionProxyBypassStats::NotifyUnavailabilityIfChanged() {
bool prev_unavailable = unavailable_;
unavailable_ =
(proxy_net_errors_count_ >= kMinFailedRequestsWhenUnavailable &&
successful_requests_through_proxy_count_ <=
kMaxSuccessfulRequestsWhenUnavailable);
if (prev_unavailable != unavailable_)
unreachable_callback_.Run(unavailable_);
}
void DataReductionProxyBypassStats::RecordBypassedBytesHistograms(
const net::URLRequest& request,
bool data_reduction_proxy_enabled,
......@@ -365,21 +380,6 @@ void DataReductionProxyBypassStats::OnNetworkChanged(
ClearRequestCounts();
}
void DataReductionProxyBypassStats::ClearRequestCounts() {
successful_requests_through_proxy_count_ = 0;
proxy_net_errors_count_ = 0;
}
void DataReductionProxyBypassStats::NotifyUnavailabilityIfChanged() {
bool prev_unavailable = unavailable_;
unavailable_ =
(proxy_net_errors_count_ >= kMinFailedRequestsWhenUnavailable &&
successful_requests_through_proxy_count_ <=
kMaxSuccessfulRequestsWhenUnavailable);
if (prev_unavailable != unavailable_)
unreachable_callback_.Run(unavailable_);
}
void DataReductionProxyBypassStats::RecordBypassedBytes(
DataReductionProxyBypassType bypass_type,
DataReductionProxyBypassStats::BypassedBytesType bypassed_bytes_type,
......
......@@ -80,6 +80,14 @@ class DataReductionProxyBypassStats
void OnConnectComplete(const net::HostPortPair& proxy_server,
int net_error);
// Unconditionally clears counts of successful requests and net errors when
// using the Data Reduction Proxy.
void ClearRequestCounts();
// Checks if the availability status of the Data Reduction Proxy has changed,
// and calls |unreachable_callback_| if so.
void NotifyUnavailabilityIfChanged();
private:
friend class DataReductionProxyBypassStatsTest;
FRIEND_TEST_ALL_PREFIXES(DataReductionProxyBypassStatsTest,
......@@ -115,17 +123,6 @@ class DataReductionProxyBypassStats
void OnNetworkChanged(
net::NetworkChangeNotifier::ConnectionType type) override;
// Clears request counts unconditionally.
void ClearRequestCounts();
// Checks if the availability status of the data reduction proxy has changed,
// and notifies the UIThread via NotifyUnavailabilityOnUIThread if so. The
// data reduction proxy is considered unavailable if and only if no requests
// went through the proxy but some eligible requests were service by other
// routes.
void NotifyUnavailabilityIfChanged();
void NotifyUnavailabilityOnUIThread(bool unavailable);
void RecordBypassedBytes(
DataReductionProxyBypassType bypass_type,
BypassedBytesType bypassed_bytes_type,
......
......@@ -223,12 +223,24 @@ DataReductionProxyIOData::CreateNetworkDelegate(
return network_delegate.Pass();
}
// TODO(kundaji): Rename this method to something more descriptive.
// Bug http://crbug/488190.
void DataReductionProxyIOData::SetProxyPrefs(bool enabled,
bool alternative_enabled,
bool at_startup) {
DCHECK(io_task_runner_->BelongsToCurrentThread());
DCHECK(url_request_context_getter_->GetURLRequestContext()->proxy_service());
enabled_ = enabled;
config_->SetProxyConfig(enabled, alternative_enabled, at_startup);
// If Data Saver is disabled, reset data reduction proxy state.
if (!enabled) {
net::ProxyService* proxy_service =
url_request_context_getter_->GetURLRequestContext()->proxy_service();
proxy_service->ClearBadProxiesCache();
bypass_stats_->ClearRequestCounts();
bypass_stats_->NotifyUnavailabilityIfChanged();
}
}
void DataReductionProxyIOData::UpdateContentLengths(
......
......@@ -83,9 +83,7 @@ class DataReductionProxyIOData : public DataReductionProxyEventStorageDelegate {
// configuration should be set. Use the alternative configuration only if
// |enabled| and |alternative_enabled| are true. |at_startup| is true only
// when DataReductionProxySettings is initialized.
void SetProxyPrefs(bool enabled,
bool alternative_enabled,
bool at_startup);
void SetProxyPrefs(bool enabled, bool alternative_enabled, bool at_startup);
// Bridge methods to safely call to the UI thread objects.
void UpdateContentLengths(int64 received_content_length,
......@@ -159,6 +157,8 @@ class DataReductionProxyIOData : public DataReductionProxyEventStorageDelegate {
private:
friend class TestDataReductionProxyIOData;
FRIEND_TEST_ALL_PREFIXES(DataReductionProxyIODataTest, TestConstruction);
FRIEND_TEST_ALL_PREFIXES(DataReductionProxyIODataTest,
TestResetBadProxyListOnDisableDataSaver);
// Used for testing.
DataReductionProxyIOData();
......
......@@ -8,14 +8,21 @@
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/testing_pref_service.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_prefs.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_request_options.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_test_utils.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
#include "net/http/http_network_session.h"
#include "net/log/net_log.h"
#include "net/proxy/proxy_info.h"
#include "net/proxy/proxy_service.h"
#include "net/socket/next_proto.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
......@@ -144,4 +151,48 @@ TEST_F(DataReductionProxyIODataTest, TestConstruction) {
io_data->ShutdownOnUIThread();
}
TEST_F(DataReductionProxyIODataTest, TestResetBadProxyListOnDisableDataSaver) {
net::TestURLRequestContext context(false);
scoped_ptr<DataReductionProxyTestContext> drp_test_context =
DataReductionProxyTestContext::Builder()
.WithParamsFlags(DataReductionProxyParams::kAllowed |
DataReductionProxyParams::kFallbackAllowed |
DataReductionProxyParams::kPromoAllowed)
.WithURLRequestContext(&context)
.WithTestConfigurator()
.SkipSettingsInitialization()
.Build();
drp_test_context->pref_service()->SetBoolean(
prefs::kDataReductionProxyEnabled, true);
drp_test_context->InitSettings();
DataReductionProxyIOData* io_data = drp_test_context->io_data();
std::vector<net::ProxyServer> proxies;
proxies.push_back(net::ProxyServer::FromURI("http://foo1.com",
net::ProxyServer::SCHEME_HTTP));
net::ProxyService* proxy_service =
io_data->url_request_context_getter_->GetURLRequestContext()
->proxy_service();
net::ProxyInfo proxy_info;
proxy_info.UseNamedProxy("http://foo2.com");
net::BoundNetLog bound_net_log;
const net::ProxyRetryInfoMap& bad_proxy_list =
proxy_service->proxy_retry_info();
// Simulate network error to add proxies to the bad proxy list.
proxy_service->MarkProxiesAsBadUntil(proxy_info, base::TimeDelta::FromDays(1),
proxies, bound_net_log);
base::RunLoop().RunUntilIdle();
// Verify that there are 2 proxies in the bad proxies list.
EXPECT_EQ(2UL, bad_proxy_list.size());
// Turn Data Saver off.
drp_test_context->settings()->SetDataReductionProxyEnabled(false);
base::RunLoop().RunUntilIdle();
// Verify that bad proxy list is empty.
EXPECT_EQ(0UL, bad_proxy_list.size());
}
} // namespace data_reduction_proxy
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment