Commit 5e9e06bd authored by Tarun Bansal's avatar Tarun Bansal Committed by Commit Bot

NQE servicification of data saver proxy

Use network quality tracker instead of network quality estimator (NQE)
to obtain network quality in data reduction proxy component.

Network quality tracker can provide network service when network
service is enabled as well as when network service is disabled.

data_reduction_proxy_service.h receives network quality
estimates on UI thread from network quality tracker.
On receiving estimates, it posts them to d_r_p_io_data
which lives on IO thread, and consumes the estimates.

Bug: 819244
Cq-Include-Trybots: luci.chromium.try:linux_mojo
Change-Id: I8a89fae921c15d5e006c9d51cb19cb6116688fb1
Reviewed-on: https://chromium-review.googlesource.com/1171592Reviewed-by: default avatarHelen Li <xunjieli@chromium.org>
Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarRyan Sturm <ryansturm@chromium.org>
Commit-Queue: Tarun Bansal <tbansal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#582711}
parent e92d3e56
......@@ -4,6 +4,9 @@
#include <string>
#include "chrome/browser/browser_process.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
......@@ -11,6 +14,7 @@
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/data_reduction_proxy/core/browser/data_reduction_proxy_service.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/browser_test_base.h"
#include "content/public/test/browser_test_utils.h"
......@@ -67,6 +71,20 @@ class DataSaverWithServerBrowserTest : public InProcessBrowserTest {
content::RunAllPendingInMessageLoop();
}
net::EffectiveConnectionType GetEffectiveConnectionType() const {
return DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
browser()->profile())
->data_reduction_proxy_service()
->GetEffectiveConnectionType();
}
base::Optional<base::TimeDelta> GetHttpRttEstimate() const {
return DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
browser()->profile())
->data_reduction_proxy_service()
->GetHttpRttEstimate();
}
std::unique_ptr<net::test_server::HttpResponse> VerifySaveDataHeader(
const net::test_server::HttpRequest& request) {
auto save_data_header_it = request.headers.find("save-data");
......@@ -116,3 +134,50 @@ IN_PROC_BROWSER_TEST_F(DataSaverWithServerBrowserTest, ReloadPage) {
content::WaitForLoadStop(
browser()->tab_strip_model()->GetActiveWebContents());
}
// Test that the data saver receives changes in effective connection type.
IN_PROC_BROWSER_TEST_F(DataSaverWithServerBrowserTest,
EffectiveConnectionType) {
Init();
ASSERT_TRUE(test_server_->Start());
EnableDataSaver(true);
EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_4G, GetEffectiveConnectionType());
g_browser_process->network_quality_tracker()
->ReportEffectiveConnectionTypeForTesting(
net::EFFECTIVE_CONNECTION_TYPE_2G);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_2G, GetEffectiveConnectionType());
g_browser_process->network_quality_tracker()
->ReportEffectiveConnectionTypeForTesting(
net::EFFECTIVE_CONNECTION_TYPE_3G);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_3G, GetEffectiveConnectionType());
}
// Test that the data saver receives changes in HTTP RTT estimate.
IN_PROC_BROWSER_TEST_F(DataSaverWithServerBrowserTest, HttpRttEstimate) {
Init();
ASSERT_TRUE(test_server_->Start());
EnableDataSaver(true);
EXPECT_TRUE(GetHttpRttEstimate().has_value());
g_browser_process->network_quality_tracker()
->ReportRTTsAndThroughputForTesting(
base::TimeDelta::FromMilliseconds(100), 0);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(base::TimeDelta::FromMilliseconds(100), GetHttpRttEstimate());
g_browser_process->network_quality_tracker()
->ReportRTTsAndThroughputForTesting(
base::TimeDelta::FromMilliseconds(500), 0);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(base::TimeDelta::FromMilliseconds(500), GetHttpRttEstimate());
}
......@@ -168,8 +168,11 @@ IN_PROC_BROWSER_TEST_F(NetworkQualityTrackerBrowserTest,
EXPECT_NE(nullptr, tracker);
base::RunLoop run_loop;
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_4G);
TestNetworkQualityObserver network_quality_observer(tracker);
EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN,
network_quality_observer.WaitForNotification(
net::EFFECTIVE_CONNECTION_TYPE_4G);
EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_4G,
network_quality_observer.effective_connection_type());
SimulateNetworkQualityChange(net::EFFECTIVE_CONNECTION_TYPE_3G);
......@@ -177,8 +180,17 @@ IN_PROC_BROWSER_TEST_F(NetworkQualityTrackerBrowserTest,
net::EFFECTIVE_CONNECTION_TYPE_3G);
EXPECT_EQ(net::EFFECTIVE_CONNECTION_TYPE_3G,
network_quality_observer.effective_connection_type());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, network_quality_observer.num_notifications());
// Verify that not too many effective connection type observations are
// received. Note that setting the effective connection type to UNKNOWN above,
// and adding the observer is racy. In some cases, the observer may receiver
// the notification about effective connection type being UNKNOWN, followed
// by other notifications.
EXPECT_LE(1u, network_quality_observer.num_notifications());
EXPECT_GE(5u, network_quality_observer.num_notifications());
// Typical RTT and downlink values when effective connection type is 3G. Taken
// from net::NetworkQualityEstimatorParams.
EXPECT_EQ(base::TimeDelta::FromMilliseconds(450),
......
......@@ -32,11 +32,13 @@
#include "components/prefs/scoped_user_pref_update.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "components/proxy_config/proxy_prefs.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/host_port_pair.h"
#include "net/base/proxy_server.h"
#include "net/proxy_resolution/proxy_config.h"
#include "net/proxy_resolution/proxy_list.h"
#include "net/url_request/url_request_context_getter.h"
#include "services/network/public/cpp/network_quality_tracker.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace {
......@@ -194,6 +196,7 @@ void DataReductionProxyChromeSettings::InitDataReductionProxySettings(
std::unique_ptr<data_reduction_proxy::DataStore> store,
const scoped_refptr<base::SingleThreadTaskRunner>& ui_task_runner,
const scoped_refptr<base::SequencedTaskRunner>& db_task_runner) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
#if defined(OS_ANDROID)
// On mobile we write Data Reduction Proxy prefs directly to the pref service.
// On desktop we store Data Reduction Proxy prefs in memory, writing to disk
......@@ -211,8 +214,8 @@ void DataReductionProxyChromeSettings::InitDataReductionProxySettings(
std::make_unique<
data_reduction_proxy::DataReductionProxyPingbackClientImpl>(
url_loader_factory, ui_task_runner),
ui_task_runner, io_data->io_task_runner(), db_task_runner,
commit_delay);
g_browser_process->network_quality_tracker(), ui_task_runner,
io_data->io_task_runner(), db_task_runner, commit_delay);
data_reduction_proxy::DataReductionProxySettings::
InitDataReductionProxySettings(data_reduction_proxy_enabled_pref_name_,
profile_prefs, io_data,
......
......@@ -32,6 +32,7 @@
#include "media/media_buildflags.h"
#include "net/url_request/url_request_context_getter.h"
#include "printing/buildflags/buildflags.h"
#include "services/network/public/cpp/network_quality_tracker.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"
......@@ -169,7 +170,11 @@ TestingBrowserProcess::shared_url_loader_factory() {
network::NetworkQualityTracker*
TestingBrowserProcess::network_quality_tracker() {
return nullptr;
if (!network_quality_tracker_) {
network_quality_tracker_ = std::make_unique<network::NetworkQualityTracker>(
base::BindRepeating(&content::GetNetworkService));
}
return network_quality_tracker_.get();
}
WatchDogThread* TestingBrowserProcess::watchdog_thread() {
......
......@@ -44,6 +44,7 @@ class GCMDriver;
}
namespace network {
class NetworkQualityTracker;
class TestNetworkConnectionTracker;
}
......@@ -172,6 +173,7 @@ class TestingBrowserProcess : public BrowserProcess {
std::unique_ptr<policy::ChromeBrowserPolicyConnector>
browser_policy_connector_;
bool created_browser_policy_connector_ = false;
std::unique_ptr<network::NetworkQualityTracker> network_quality_tracker_;
std::unique_ptr<ProfileManager> profile_manager_;
std::unique_ptr<NotificationUIManager> notification_ui_manager_;
std::unique_ptr<NotificationPlatformBridge> notification_platform_bridge_;
......
......@@ -79,6 +79,7 @@ if (is_android) {
"//crypto",
"//google_apis",
"//net:net",
"//services/network/public/cpp",
"//url:url",
]
}
......@@ -113,6 +114,8 @@ static_library("browser") {
"//crypto",
"//google_apis",
"//net",
"//services/network:network_service",
"//services/network/public/cpp",
"//third_party/leveldatabase",
"//url",
]
......@@ -148,6 +151,7 @@ static_library("test_support") {
"//components/previews/core:test_support",
"//net",
"//net:test_support",
"//services/network:test_support",
"//testing/gmock",
"//testing/gtest",
]
......@@ -213,6 +217,8 @@ source_set("unit_tests") {
"//components/previews/core:test_support",
"//components/variations",
"//net:test_support",
"//services/network:network_service",
"//services/network:test_support",
"//testing/gmock",
"//testing/gtest",
]
......
include_rules = [
"+components/data_use_measurement/core",
"+services/network/public",
"+services/network/test",
"+third_party/leveldatabase",
]
......@@ -226,7 +226,9 @@ void DataReductionProxyConfig::InitializeOnIOThread(
url_request_context_getter,
base::BindRepeating(
&DataReductionProxyConfig::HandleWarmupFetcherResponse,
base::Unretained(this))));
base::Unretained(this)),
base::BindRepeating(&DataReductionProxyConfig::GetHttpRttEstimate,
base::Unretained(this))));
if (ShouldAddDefaultProxyBypassRules())
AddDefaultProxyBypassRules();
......@@ -752,6 +754,18 @@ size_t DataReductionProxyConfig::GetWarmupURLFetchAttemptCounts() const {
warmup_url_fetch_in_flight_core_proxy_);
}
void DataReductionProxyConfig::OnRTTOrThroughputEstimatesComputed(
base::TimeDelta http_rtt) {
DCHECK(thread_checker_.CalledOnValidThread());
http_rtt_ = http_rtt;
}
base::Optional<base::TimeDelta> DataReductionProxyConfig::GetHttpRttEstimate()
const {
DCHECK(thread_checker_.CalledOnValidThread());
return http_rtt_;
}
bool DataReductionProxyConfig::enabled_by_user_and_reachable() const {
DCHECK(thread_checker_.CalledOnValidThread());
return enabled_by_user_ && !unreachable_;
......
......@@ -205,6 +205,12 @@ class DataReductionProxyConfig
// When triggering previews, prevent long term black list rules.
void SetIgnoreLongTermBlackListRules(bool ignore_long_term_black_list_rules);
// Called when there is a change in the HTTP RTT estimate.
void OnRTTOrThroughputEstimatesComputed(base::TimeDelta http_rtt);
// Returns the current HTTP RTT estimate.
base::Optional<base::TimeDelta> GetHttpRttEstimate() const;
// Returns the value set in SetIgnoreLongTermBlackListRules.
bool IgnoreBlackListLongTermRulesForTesting() const;
......@@ -258,7 +264,7 @@ class DataReductionProxyConfig
ShouldAcceptServerPreview);
// Values of the estimated network quality at the beginning of the most
// recent query of the Network Quality Estimator.
// recent query of the Network quality estimate provider.
enum NetworkQualityAtLastQuery {
NETWORK_QUALITY_AT_LAST_QUERY_UNKNOWN,
NETWORK_QUALITY_AT_LAST_QUERY_SLOW,
......@@ -376,6 +382,9 @@ class DataReductionProxyConfig
// the lifetime of |this| if accessed on the IO thread.
NetworkPropertiesManager* network_properties_manager_;
// Current HTTP RTT estimate.
base::Optional<base::TimeDelta> http_rtt_;
base::WeakPtrFactory<DataReductionProxyConfig> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(DataReductionProxyConfig);
......
......@@ -19,10 +19,6 @@
using testing::_;
namespace net {
class NetworkQualityEstimator;
}
namespace data_reduction_proxy {
TestDataReductionProxyConfig::TestDataReductionProxyConfig(
......
......@@ -53,7 +53,6 @@
#include "net/http/http_status_code.h"
#include "net/log/test_net_log.h"
#include "net/nqe/effective_connection_type.h"
#include "net/nqe/network_quality_estimator_test_util.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/test_url_fetcher_factory.h"
......
......@@ -112,6 +112,7 @@ DataReductionProxyIOData::DataReductionProxyIOData(
basic_url_request_context_getter_(
new BasicHTTPURLRequestContextGetter(user_agent, io_task_runner)),
channel_(channel),
effective_connection_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
weak_factory_(this) {
DCHECK(net_log);
DCHECK(io_task_runner_);
......@@ -180,6 +181,7 @@ DataReductionProxyIOData::DataReductionProxyIOData(
io_task_runner_(io_task_runner),
ui_task_runner_(ui_task_runner),
url_request_context_getter_(nullptr),
effective_connection_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
weak_factory_(this) {
DCHECK(ui_task_runner_);
DCHECK(io_task_runner_);
......@@ -458,4 +460,22 @@ void DataReductionProxyIOData::UpdateProxyRequestHeaders(
service_, std::move(headers)));
}
void DataReductionProxyIOData::OnEffectiveConnectionTypeChanged(
net::EffectiveConnectionType type) {
DCHECK(io_task_runner_->BelongsToCurrentThread());
effective_connection_type_ = type;
}
void DataReductionProxyIOData::OnRTTOrThroughputEstimatesComputed(
base::TimeDelta http_rtt) {
DCHECK(io_task_runner_->BelongsToCurrentThread());
config_->OnRTTOrThroughputEstimatesComputed(http_rtt);
}
net::EffectiveConnectionType
DataReductionProxyIOData::GetEffectiveConnectionType() const {
DCHECK(io_task_runner_->BelongsToCurrentThread());
return effective_connection_type_;
}
} // namespace data_reduction_proxy
......@@ -153,6 +153,16 @@ class DataReductionProxyIOData : public DataReductionProxyEventStorageDelegate {
// Forwards proxy authentication headers to the UI thread.
void UpdateProxyRequestHeaders(net::HttpRequestHeaders headers);
// Notifies |this| that there there is a change in the effective connection
// type.
void OnEffectiveConnectionTypeChanged(net::EffectiveConnectionType type);
// Notifies |this| that there there is a change in the HTTP RTT estimate.
void OnRTTOrThroughputEstimatesComputed(base::TimeDelta http_rtt);
// Returns the current estimate of the effective connection type.
net::EffectiveConnectionType GetEffectiveConnectionType() const;
// Various accessor methods.
DataReductionProxyConfigurator* configurator() const {
return configurator_.get();
......@@ -324,6 +334,9 @@ class DataReductionProxyIOData : public DataReductionProxyEventStorageDelegate {
// is unavailable, then the destruction will happen on the UI thread.
std::unique_ptr<NetworkPropertiesManager> network_properties_manager_;
// Current estimate of the effective connection type.
net::EffectiveConnectionType effective_connection_type_;
base::WeakPtrFactory<DataReductionProxyIOData> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(DataReductionProxyIOData);
......
......@@ -31,7 +31,6 @@
#include "net/http/http_request_headers.h"
#include "net/http/http_response_headers.h"
#include "net/nqe/effective_connection_type.h"
#include "net/nqe/network_quality_estimator.h"
#include "net/proxy_resolution/proxy_info.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
#include "net/url_request/url_request.h"
......@@ -374,10 +373,9 @@ void DataReductionProxyNetworkDelegate::OnBeforeSendHeadersInternal(
data->set_session_key(
data_reduction_proxy_request_options_->GetSecureSession());
data->set_request_url(request->url());
if (request->context()->network_quality_estimator()) {
data->set_effective_connection_type(request->context()
->network_quality_estimator()
->GetEffectiveConnectionType());
if (data_reduction_proxy_io_data_) {
data->set_effective_connection_type(
data_reduction_proxy_io_data_->GetEffectiveConnectionType());
}
data->set_connection_type(
net::NetworkChangeNotifier::GetConnectionType());
......@@ -729,10 +727,9 @@ void DataReductionProxyNetworkDelegate::MaybeAddChromeProxyECTHeader(
if (request_headers->HasHeader(chrome_proxy_ect_header()))
request_headers->RemoveHeader(chrome_proxy_ect_header());
if (request.context()->network_quality_estimator()) {
net::EffectiveConnectionType type = request.context()
->network_quality_estimator()
->GetEffectiveConnectionType();
if (data_reduction_proxy_io_data_) {
net::EffectiveConnectionType type =
data_reduction_proxy_io_data_->GetEffectiveConnectionType();
if (type > net::EFFECTIVE_CONNECTION_TYPE_OFFLINE) {
DCHECK_NE(net::EFFECTIVE_CONNECTION_TYPE_LAST, type);
request_headers->SetHeader(chrome_proxy_ect_header(),
......
......@@ -58,7 +58,6 @@
#include "net/http/http_response_headers.h"
#include "net/http/http_util.h"
#include "net/nqe/effective_connection_type.h"
#include "net/nqe/network_quality_estimator_test_util.h"
#include "net/proxy_resolution/proxy_config.h"
#include "net/proxy_resolution/proxy_info.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
......@@ -72,6 +71,7 @@
#include "net/url_request/url_request_job_factory_impl.h"
#include "net/url_request/url_request_status.h"
#include "net/url_request/url_request_test_util.h"
#include "services/network/test/test_network_quality_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
......@@ -344,7 +344,6 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test {
net::ProxyResolutionService::CreateFixedFromPacResult(
proxy_server.ToPacString(), TRAFFIC_ANNOTATION_FOR_TESTS);
context_->set_proxy_resolution_service(proxy_resolution_service_.get());
context_->set_network_quality_estimator(&test_network_quality_estimator_);
mock_socket_factory_.reset(new net::MockClientSocketFactory());
......@@ -538,8 +537,9 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test {
const std::string& response_headers,
bool expect_cached,
bool expect_brotli) {
test_network_quality_estimator()->set_effective_connection_type(
test_network_quality_tracker()->ReportEffectiveConnectionTypeForTesting(
net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN);
base::RunLoop().RunUntilIdle();
GURL url(kTestURL);
int response_body_size = 140;
......@@ -708,7 +708,7 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test {
"www.google.com\r\nProxy-Connection: "
"keep-alive\r\nUser-Agent:\r\nAccept-Encoding: gzip, "
"deflate\r\nAccept-Language: en-us,fr\r\n"
"chrome-proxy-ect: 4G\r\n"
"chrome-proxy-ect: Unknown\r\n"
"Chrome-Proxy: " +
io_data()->test_request_options()->GetHeaderValueForTesting() +
(page_id_value.empty() ? "" : (", " + page_id_value)) + "\r\n\r\n";
......@@ -748,8 +748,9 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test {
net::EffectiveConnectionType effective_connection_type,
bool expect_ect_header,
bool expect_cached) {
test_network_quality_estimator()->set_effective_connection_type(
test_network_quality_tracker()->ReportEffectiveConnectionTypeForTesting(
effective_connection_type);
base::RunLoop().RunUntilIdle();
net::TestDelegate delegate;
std::unique_ptr<net::URLRequest> request = context_->CreateRequest(
......@@ -848,8 +849,8 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test {
TestLoFiDecider* lofi_decider() const { return lofi_decider_; }
net::TestNetworkQualityEstimator* test_network_quality_estimator() {
return &test_network_quality_estimator_;
network::TestNetworkQualityTracker* test_network_quality_tracker() {
return test_context_->test_network_quality_tracker();
}
net::SSLSocketDataProvider* ssl_socket_data_provider() {
......@@ -866,7 +867,6 @@ class DataReductionProxyNetworkDelegateTest : public testing::Test {
TestLoFiDecider* lofi_decider_;
TestLoFiUIService* lofi_ui_service_;
std::unique_ptr<DataReductionProxyTestContext> test_context_;
net::TestNetworkQualityEstimator test_network_quality_estimator_;
net::SSLSocketDataProvider ssl_socket_data_provider_;
......@@ -1074,8 +1074,9 @@ TEST_F(DataReductionProxyNetworkDelegateTest, RequestDataConfigurations) {
net::HttpRequestHeaders headers;
net::ProxyRetryInfoMap proxy_retry_info;
test_network_quality_estimator()->set_effective_connection_type(
test_network_quality_tracker()->ReportEffectiveConnectionTypeForTesting(
net::EFFECTIVE_CONNECTION_TYPE_OFFLINE);
base::RunLoop().RunUntilIdle();
std::unique_ptr<net::URLRequest> request =
context()->CreateRequest(GURL(kTestURL), net::RequestPriority::IDLE,
......@@ -1131,8 +1132,9 @@ TEST_F(DataReductionProxyNetworkDelegateTest,
true, true,
},
};
test_network_quality_estimator()->set_effective_connection_type(
test_network_quality_tracker()->ReportEffectiveConnectionTypeForTesting(
net::EFFECTIVE_CONNECTION_TYPE_4G);
base::RunLoop().RunUntilIdle();
base::FieldTrialList field_trial_list(nullptr);
ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
"DataCompressionProxyHoldback", "Enabled"));
......@@ -1179,8 +1181,9 @@ TEST_F(DataReductionProxyNetworkDelegateTest, RedirectRequestDataCleared) {
net::HttpRequestHeaders headers_original;
net::ProxyRetryInfoMap proxy_retry_info;
test_network_quality_estimator()->set_effective_connection_type(
test_network_quality_tracker()->ReportEffectiveConnectionTypeForTesting(
net::EFFECTIVE_CONNECTION_TYPE_OFFLINE);
base::RunLoop().RunUntilIdle();
std::unique_ptr<net::URLRequest> request =
context()->CreateRequest(GURL(kTestURL), net::RequestPriority::IDLE,
......
......@@ -36,6 +36,7 @@ DataReductionProxyService::DataReductionProxyService(
net::URLRequestContextGetter* request_context_getter,
std::unique_ptr<DataStore> store,
std::unique_ptr<DataReductionProxyPingbackClient> pingback_client,
network::NetworkQualityTracker* network_quality_tracker,
const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner,
const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
......@@ -48,8 +49,11 @@ DataReductionProxyService::DataReductionProxyService(
io_task_runner_(io_task_runner),
db_task_runner_(db_task_runner),
initialized_(false),
network_quality_tracker_(network_quality_tracker),
effective_connection_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
weak_factory_(this) {
DCHECK(settings);
DCHECK(network_quality_tracker_);
db_task_runner_->PostTask(FROM_HERE,
base::BindOnce(&DBDataOwner::InitializeOnDBThread,
db_data_owner_->GetWeakPtr()));
......@@ -58,10 +62,14 @@ DataReductionProxyService::DataReductionProxyService(
new DataReductionProxyCompressionStats(this, prefs_, commit_delay));
}
event_store_.reset(new DataReductionProxyEventStore());
network_quality_tracker_->AddEffectiveConnectionTypeObserver(this);
network_quality_tracker_->AddRTTAndThroughputEstimatesObserver(this);
}
DataReductionProxyService::~DataReductionProxyService() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
network_quality_tracker_->RemoveEffectiveConnectionTypeObserver(this);
network_quality_tracker_->RemoveRTTAndThroughputEstimatesObserver(this);
compression_stats_.reset();
db_task_runner_->DeleteSoon(FROM_HERE, db_data_owner_.release());
}
......@@ -71,6 +79,14 @@ void DataReductionProxyService::SetIOData(
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
io_data_ = io_data;
initialized_ = true;
// Notify IO data of the current network quality estimates.
OnEffectiveConnectionTypeChanged(effective_connection_type_);
if (http_rtt_) {
OnRTTOrThroughputEstimatesComputed(http_rtt_.value(), base::TimeDelta(),
INT32_MAX);
}
for (DataReductionProxyServiceObserver& observer : observer_list_)
observer.OnServiceInitialized();
......@@ -113,6 +129,34 @@ void DataReductionProxyService::ReadPersistedClientConfig() {
io_data_, config_value));
}
void DataReductionProxyService::OnEffectiveConnectionTypeChanged(
net::EffectiveConnectionType type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
effective_connection_type_ = type;
io_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(
&DataReductionProxyIOData::OnEffectiveConnectionTypeChanged, io_data_,
type));
}
void DataReductionProxyService::OnRTTOrThroughputEstimatesComputed(
base::TimeDelta http_rtt,
base::TimeDelta transport_rtt,
int32_t downstream_throughput_kbps) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
http_rtt_ = http_rtt;
io_task_runner_->PostTask(
FROM_HERE,
base::BindOnce(
&DataReductionProxyIOData::OnRTTOrThroughputEstimatesComputed,
io_data_, http_rtt));
}
void DataReductionProxyService::Shutdown() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
weak_factory_.InvalidateWeakPtrs();
......@@ -213,6 +257,18 @@ void DataReductionProxyService::OnCacheCleared(const base::Time start,
io_data_, start, end));
}
net::EffectiveConnectionType
DataReductionProxyService::GetEffectiveConnectionType() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return effective_connection_type_;
}
base::Optional<base::TimeDelta> DataReductionProxyService::GetHttpRttEstimate()
const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return http_rtt_;
}
void DataReductionProxyService::LoadHistoricalDataUsage(
const HistoricalDataUsageCallback& load_data_usage_callback) {
std::unique_ptr<std::vector<DataUsageBucket>> data_usage(
......
......@@ -22,6 +22,8 @@
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_storage_delegate.h"
#include "components/data_use_measurement/core/data_use_user_data.h"
#include "net/http/http_request_headers.h"
#include "net/nqe/effective_connection_type.h"
#include "services/network/public/cpp/network_quality_tracker.h"
class PrefService;
......@@ -48,7 +50,9 @@ class DataReductionProxySettings;
// Contains and initializes all Data Reduction Proxy objects that have a
// lifetime based on the UI thread.
class DataReductionProxyService
: public DataReductionProxyEventStorageDelegate {
: public DataReductionProxyEventStorageDelegate,
public network::NetworkQualityTracker::EffectiveConnectionTypeObserver,
public network::NetworkQualityTracker::RTTAndThroughputEstimatesObserver {
public:
// The caller must ensure that |settings|, |prefs|, |request_context|, and
// |io_task_runner| remain alive for the lifetime of the
......@@ -62,12 +66,13 @@ class DataReductionProxyService
net::URLRequestContextGetter* request_context_getter,
std::unique_ptr<DataStore> store,
std::unique_ptr<DataReductionProxyPingbackClient> pingback_client,
network::NetworkQualityTracker* network_quality_tracker,
const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner,
const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner,
const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
const base::TimeDelta& commit_delay);
virtual ~DataReductionProxyService();
~DataReductionProxyService() override;
// Sets the DataReductionProxyIOData weak pointer.
void SetIOData(base::WeakPtr<DataReductionProxyIOData> io_data);
......@@ -138,6 +143,10 @@ class DataReductionProxyService
// cleared.
void OnCacheCleared(const base::Time start, const base::Time end);
// Returns the current network quality estimates.
net::EffectiveConnectionType GetEffectiveConnectionType() const;
base::Optional<base::TimeDelta> GetHttpRttEstimate() const;
// Sets |proxy_request_headers_| with a forwarded value from the IO thread.
void SetProxyRequestHeaders(net::HttpRequestHeaders headers) {
proxy_request_headers_ = headers;
......@@ -172,6 +181,14 @@ class DataReductionProxyService
FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
TestLoFiSessionStateHistograms);
void OnEffectiveConnectionTypeChanged(
net::EffectiveConnectionType type) override;
void OnRTTOrThroughputEstimatesComputed(
base::TimeDelta http_rtt,
base::TimeDelta transport_rtt,
int32_t downstream_throughput_kbps) override;
// Loads the Data Reduction Proxy configuration from |prefs_| and applies it.
void ReadPersistedClientConfig();
......@@ -209,6 +226,14 @@ class DataReductionProxyService
bool initialized_;
// Must be accessed on UI thread. Guaranteed to be non-null during the
// lifetime of |this|.
network::NetworkQualityTracker* network_quality_tracker_;
// Current network quality estimates.
net::EffectiveConnectionType effective_connection_type_;
base::Optional<base::TimeDelta> http_rtt_;
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<DataReductionProxyService> weak_factory_;
......
......@@ -35,7 +35,6 @@
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/testing_pref_service.h"
#include "net/base/network_delegate_impl.h"
#include "net/nqe/network_quality_estimator_test_util.h"
#include "net/proxy_resolution/proxy_config.h"
#include "net/proxy_resolution/proxy_info.h"
#include "net/proxy_resolution/proxy_list.h"
......@@ -44,6 +43,7 @@
#include "net/url_request/url_request_intercepting_job_factory.h"
#include "net/url_request/url_request_job_factory_impl.h"
#include "net/url_request/url_request_test_util.h"
#include "services/network/test/test_network_quality_tracker.h"
#include "url/gurl.h"
namespace {
......@@ -235,6 +235,7 @@ bool TestDataReductionProxyConfigServiceClient::RemoteConfigApplied() const {
MockDataReductionProxyService::MockDataReductionProxyService(
DataReductionProxySettings* settings,
network::TestNetworkQualityTracker* test_network_quality_tracker,
PrefService* prefs,
net::URLRequestContextGetter* request_context,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner)
......@@ -243,6 +244,7 @@ MockDataReductionProxyService::MockDataReductionProxyService(
request_context,
std::make_unique<TestDataStore>(),
nullptr,
test_network_quality_tracker,
task_runner,
task_runner,
task_runner,
......@@ -420,24 +422,16 @@ DataReductionProxyTestContext::Builder::Build() {
std::unique_ptr<net::TestNetLog> net_log(new net::TestNetLog());
std::unique_ptr<TestConfigStorer> config_storer(
new TestConfigStorer(pref_service.get()));
std::unique_ptr<net::TestNetworkQualityEstimator> estimator;
if (request_context_) {
request_context_getter = new net::TrivialURLRequestContextGetter(
request_context_, task_runner);
if (!request_context_->network_quality_estimator()) {
estimator.reset(new net::TestNetworkQualityEstimator());
request_context_->set_network_quality_estimator(estimator.get());
}
} else {
std::unique_ptr<net::TestURLRequestContext> test_request_context(
new net::TestURLRequestContext(true));
if (mock_socket_factory_)
test_request_context->set_client_socket_factory(mock_socket_factory_);
if (!test_request_context->network_quality_estimator()) {
estimator.reset(new net::TestNetworkQualityEstimator());
test_request_context->set_network_quality_estimator(estimator.get());
}
test_request_context->Init();
request_context_getter = new net::TestURLRequestContextGetter(
task_runner, std::move(test_request_context));
......@@ -541,8 +535,7 @@ DataReductionProxyTestContext::Builder::Build() {
task_runner, std::move(pref_service), std::move(net_log),
request_context_getter, mock_socket_factory_, std::move(io_data),
std::move(settings), std::move(storage_delegate),
std::move(config_storer), std::move(estimator), raw_params,
test_context_flags));
std::move(config_storer), raw_params, test_context_flags));
if (!skip_settings_initialization_)
test_context->InitSettingsWithoutCheck();
......@@ -561,7 +554,6 @@ DataReductionProxyTestContext::DataReductionProxyTestContext(
std::unique_ptr<TestDataReductionProxyEventStorageDelegate>
storage_delegate,
std::unique_ptr<TestConfigStorer> config_storer,
std::unique_ptr<net::TestNetworkQualityEstimator> estimator,
TestDataReductionProxyParams* params,
unsigned int test_context_flags)
: test_context_flags_(test_context_flags),
......@@ -569,12 +561,13 @@ DataReductionProxyTestContext::DataReductionProxyTestContext(
simple_pref_service_(std::move(simple_pref_service)),
net_log_(std::move(net_log)),
request_context_getter_(request_context_getter),
estimator_(std::move(estimator)),
mock_socket_factory_(mock_socket_factory),
io_data_(std::move(io_data)),
settings_(std::move(settings)),
storage_delegate_(std::move(storage_delegate)),
config_storer_(std::move(config_storer)),
test_network_quality_tracker_(
std::make_unique<network::TestNetworkQualityTracker>()),
params_(params) {}
DataReductionProxyTestContext::~DataReductionProxyTestContext() {
......@@ -644,13 +637,15 @@ DataReductionProxyTestContext::CreateDataReductionProxyServiceInternal(
DataReductionProxySettings* settings) {
if (test_context_flags_ & USE_MOCK_SERVICE) {
return std::make_unique<MockDataReductionProxyService>(
settings, simple_pref_service_.get(), request_context_getter_.get(),
settings, test_network_quality_tracker_.get(),
simple_pref_service_.get(), request_context_getter_.get(),
task_runner_);
}
return std::make_unique<DataReductionProxyService>(
settings, simple_pref_service_.get(), request_context_getter_.get(),
base::WrapUnique(new TestDataStore()), nullptr, task_runner_,
task_runner_, task_runner_, base::TimeDelta());
base::WrapUnique(new TestDataStore()), nullptr,
test_network_quality_tracker_.get(), task_runner_, task_runner_,
task_runner_, base::TimeDelta());
}
void DataReductionProxyTestContext::AttachToURLRequestContext(
......
......@@ -39,11 +39,14 @@ class TestingPrefServiceSimple;
namespace net {
class MockClientSocketFactory;
class NetLog;
class TestNetworkQualityEstimator;
class URLRequestContext;
class URLRequestContextStorage;
}
namespace network {
class TestNetworkQualityTracker;
}
namespace data_reduction_proxy {
class ClientConfig;
......@@ -182,6 +185,7 @@ class MockDataReductionProxyService : public DataReductionProxyService {
public:
MockDataReductionProxyService(
DataReductionProxySettings* settings,
network::TestNetworkQualityTracker* test_network_quality_tracker,
PrefService* prefs,
net::URLRequestContextGetter* request_context,
const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
......@@ -483,6 +487,10 @@ class DataReductionProxyTestContext {
return params_;
}
network::TestNetworkQualityTracker* test_network_quality_tracker() const {
return test_network_quality_tracker_.get();
}
void InitSettingsWithoutCheck();
// Returns the proxies that are currently configured for "http://" requests,
......@@ -514,7 +522,6 @@ class DataReductionProxyTestContext {
std::unique_ptr<TestDataReductionProxyEventStorageDelegate>
storage_delegate,
std::unique_ptr<TestConfigStorer> config_storer,
std::unique_ptr<net::TestNetworkQualityEstimator> estimator,
TestDataReductionProxyParams* params,
unsigned int test_context_flags);
......@@ -527,7 +534,6 @@ class DataReductionProxyTestContext {
std::unique_ptr<TestingPrefServiceSimple> simple_pref_service_;
std::unique_ptr<net::TestNetLog> net_log_;
scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
std::unique_ptr<net::TestNetworkQualityEstimator> estimator_;
// Non-owned pointer. Will be NULL if |this| was built without specifying a
// |net::MockClientSocketFactory|.
net::MockClientSocketFactory* mock_socket_factory_;
......@@ -536,6 +542,8 @@ class DataReductionProxyTestContext {
std::unique_ptr<DataReductionProxySettings> settings_;
std::unique_ptr<TestDataReductionProxyEventStorageDelegate> storage_delegate_;
std::unique_ptr<TestConfigStorer> config_storer_;
std::unique_ptr<network::TestNetworkQualityTracker>
test_network_quality_tracker_;
TestDataReductionProxyParams* params_;
......
......@@ -16,7 +16,6 @@
#include "components/data_use_measurement/core/data_use_user_data.h"
#include "net/base/load_flags.h"
#include "net/http/http_status_code.h"
#include "net/nqe/network_quality_estimator.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_context.h"
......@@ -28,14 +27,14 @@ namespace data_reduction_proxy {
WarmupURLFetcher::WarmupURLFetcher(
const scoped_refptr<net::URLRequestContextGetter>&
url_request_context_getter,
WarmupURLFetcherCallback callback)
WarmupURLFetcherCallback callback,
GetHttpRttCallback get_http_rtt_callback)
: is_fetch_in_flight_(false),
previous_attempt_counts_(0),
url_request_context_getter_(url_request_context_getter),
callback_(callback) {
callback_(callback),
get_http_rtt_callback_(get_http_rtt_callback) {
DCHECK(url_request_context_getter_);
DCHECK(url_request_context_getter_->GetURLRequestContext()
->network_quality_estimator());
}
WarmupURLFetcher::~WarmupURLFetcher() {}
......@@ -236,12 +235,8 @@ base::TimeDelta WarmupURLFetcher::GetFetchTimeout() const {
DCHECK_LT(0u, http_rtt_multiplier);
DCHECK_GE(1000u, http_rtt_multiplier);
const net::NetworkQualityEstimator* network_quality_estimator =
url_request_context_getter_->GetURLRequestContext()
->network_quality_estimator();
base::Optional<base::TimeDelta> http_rtt_estimate =
network_quality_estimator->GetHttpRTT();
get_http_rtt_callback_.Run();
if (!http_rtt_estimate)
return max_timeout;
......
......@@ -10,6 +10,7 @@
#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/optional.h"
#include "base/sequence_checker.h"
#include "base/timer/timer.h"
#include "net/url_request/url_fetcher_delegate.h"
......@@ -36,9 +37,14 @@ class WarmupURLFetcher : public net::URLFetcherDelegate {
typedef base::RepeatingCallback<void(const net::ProxyServer&, FetchResult)>
WarmupURLFetcherCallback;
// Callback to obtain the current HTTP RTT estimate.
typedef base::RepeatingCallback<base::Optional<base::TimeDelta>()>
GetHttpRttCallback;
WarmupURLFetcher(const scoped_refptr<net::URLRequestContextGetter>&
url_request_context_getter,
WarmupURLFetcherCallback callback);
WarmupURLFetcherCallback callback,
GetHttpRttCallback get_http_rtt_callback);
~WarmupURLFetcher() override;
......@@ -100,6 +106,9 @@ class WarmupURLFetcher : public net::URLFetcherDelegate {
// completed.
WarmupURLFetcherCallback callback_;
// Callback to obtain the current HTTP RTT estimate.
GetHttpRttCallback get_http_rtt_callback_;
SEQUENCE_CHECKER(sequence_checker_);
DISALLOW_COPY_AND_ASSIGN(WarmupURLFetcher);
......@@ -107,4 +116,4 @@ class WarmupURLFetcher : public net::URLFetcherDelegate {
} // namespace data_reduction_proxy
#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_WARMUP_URL_FETCHER_H_
\ No newline at end of file
#endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_WARMUP_URL_FETCHER_H_
......@@ -21,7 +21,6 @@
#include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
#include "net/base/proxy_server.h"
#include "net/http/http_status_code.h"
#include "net/nqe/network_quality_estimator_test_util.h"
#include "net/socket/socket_test_util.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_context_getter.h"
......@@ -37,10 +36,13 @@ class WarmupURLFetcherTest : public WarmupURLFetcher {
public:
WarmupURLFetcherTest(const scoped_refptr<net::URLRequestContextGetter>&
url_request_context_getter)
: WarmupURLFetcher(url_request_context_getter,
base::BindRepeating(
&WarmupURLFetcherTest::HandleWarmupFetcherResponse,
base::Unretained(this))) {}
: WarmupURLFetcher(
url_request_context_getter,
base::BindRepeating(
&WarmupURLFetcherTest::HandleWarmupFetcherResponse,
base::Unretained(this)),
base::BindRepeating(&WarmupURLFetcherTest::GetHttpRttEstimate,
base::Unretained(this))) {}
~WarmupURLFetcherTest() override {}
......@@ -104,7 +106,16 @@ class WarmupURLFetcherTest : public WarmupURLFetcher {
net::URLFetcher* fetcher() const { return fetcher_.get(); }
void SetHttpRttOverride(base::TimeDelta http_rtt) {
http_rtt_override_ = http_rtt;
}
private:
base::Optional<base::TimeDelta> GetHttpRttEstimate() const {
if (http_rtt_override_)
return http_rtt_override_.value();
return base::TimeDelta::FromMilliseconds(5);
}
void HandleWarmupFetcherResponse(const net::ProxyServer& proxy_server,
FetchResult success_response) {
callback_received_count_++;
......@@ -117,6 +128,7 @@ class WarmupURLFetcherTest : public WarmupURLFetcher {
net::ProxyServer proxy_server_last_;
FetchResult success_response_last_ = FetchResult::kFailed;
base::Optional<base::TimeDelta> fetch_timeout_;
base::Optional<base::TimeDelta> http_rtt_override_;
DISALLOW_COPY_AND_ASSIGN(WarmupURLFetcherTest);
};
......@@ -125,9 +137,6 @@ TEST(WarmupURLFetcherTest, TestGetWarmupURLWithQueryParam) {
base::MessageLoopForIO message_loop;
scoped_refptr<net::URLRequestContextGetter> request_context_getter =
new net::TestURLRequestContextGetter(message_loop.task_runner());
net::TestNetworkQualityEstimator estimator;
request_context_getter->GetURLRequestContext()->set_network_quality_estimator(
&estimator);
WarmupURLFetcherTest warmup_url_fetcher(request_context_getter);
......@@ -184,9 +193,6 @@ TEST(WarmupURLFetcherTest, TestSuccessfulFetchWarmupURLNoViaHeader) {
scoped_refptr<net::URLRequestContextGetter> request_context_getter =
new net::TestURLRequestContextGetter(message_loop.task_runner(),
std::move(test_request_context));
net::TestNetworkQualityEstimator estimator;
request_context_getter->GetURLRequestContext()->set_network_quality_estimator(
&estimator);
WarmupURLFetcherTest warmup_url_fetcher(request_context_getter);
EXPECT_FALSE(warmup_url_fetcher.IsFetchInFlight());
......@@ -248,9 +254,6 @@ TEST(WarmupURLFetcherTest, TestSuccessfulFetchWarmupURLWithViaHeader) {
scoped_refptr<net::URLRequestContextGetter> request_context_getter =
new net::TestURLRequestContextGetter(message_loop.task_runner(),
std::move(test_request_context));
net::TestNetworkQualityEstimator estimator;
request_context_getter->GetURLRequestContext()->set_network_quality_estimator(
&estimator);
WarmupURLFetcherTest warmup_url_fetcher(request_context_getter);
EXPECT_FALSE(warmup_url_fetcher.IsFetchInFlight());
......@@ -312,9 +315,6 @@ TEST(WarmupURLFetcherTest,
scoped_refptr<net::URLRequestContextGetter> request_context_getter =
new net::TestURLRequestContextGetter(message_loop.task_runner(),
std::move(test_request_context));
net::TestNetworkQualityEstimator estimator;
request_context_getter->GetURLRequestContext()->set_network_quality_estimator(
&estimator);
WarmupURLFetcherTest warmup_url_fetcher(request_context_getter);
warmup_url_fetcher.FetchWarmupURL(0);
......@@ -365,9 +365,6 @@ TEST(WarmupURLFetcherTest, TestConnectionResetFetchWarmupURL) {
scoped_refptr<net::URLRequestContextGetter> request_context_getter =
new net::TestURLRequestContextGetter(message_loop.task_runner(),
std::move(test_request_context));
net::TestNetworkQualityEstimator estimator;
request_context_getter->GetURLRequestContext()->set_network_quality_estimator(
&estimator);
WarmupURLFetcherTest warmup_url_fetcher(request_context_getter);
EXPECT_FALSE(warmup_url_fetcher.IsFetchInFlight());
......@@ -425,9 +422,6 @@ TEST(WarmupURLFetcherTest, TestFetchTimesout) {
scoped_refptr<net::URLRequestContextGetter> request_context_getter =
new net::TestURLRequestContextGetter(message_loop.task_runner(),
std::move(test_request_context));
net::TestNetworkQualityEstimator estimator;
request_context_getter->GetURLRequestContext()->set_network_quality_estimator(
&estimator);
WarmupURLFetcherTest warmup_url_fetcher(request_context_getter);
// Set the timeout to a very low value. This should cause warmup URL fetcher
......@@ -485,9 +479,6 @@ TEST(WarmupURLFetcherTest, TestSuccessfulFetchWarmupURLWithDelay) {
scoped_refptr<net::URLRequestContextGetter> request_context_getter =
new net::TestURLRequestContextGetter(message_loop.task_runner(),
std::move(test_request_context));
net::TestNetworkQualityEstimator estimator;
request_context_getter->GetURLRequestContext()->set_network_quality_estimator(
&estimator);
WarmupURLFetcherTest warmup_url_fetcher(request_context_getter);
EXPECT_FALSE(warmup_url_fetcher.IsFetchInFlight());
......@@ -538,9 +529,6 @@ TEST(WarmupURLFetcherTest, TestFetchTimeoutIncreasing) {
scoped_refptr<net::URLRequestContextGetter> request_context_getter =
new net::TestURLRequestContextGetter(message_loop.task_runner(),
std::move(test_request_context));
net::TestNetworkQualityEstimator estimator;
request_context_getter->GetURLRequestContext()->set_network_quality_estimator(
&estimator);
WarmupURLFetcherTest warmup_url_fetcher(request_context_getter);
EXPECT_FALSE(warmup_url_fetcher.IsFetchInFlight());
......@@ -548,7 +536,7 @@ TEST(WarmupURLFetcherTest, TestFetchTimeoutIncreasing) {
EXPECT_EQ(kMinTimeout, warmup_url_fetcher.GetFetchTimeout());
base::TimeDelta http_rtt = base::TimeDelta::FromSeconds(2);
estimator.SetStartTimeNullHttpRtt(http_rtt);
warmup_url_fetcher.SetHttpRttOverride(http_rtt);
EXPECT_EQ(http_rtt * 12, warmup_url_fetcher.GetFetchTimeout());
warmup_url_fetcher.FetchWarmupURL(1);
......@@ -558,7 +546,7 @@ TEST(WarmupURLFetcherTest, TestFetchTimeoutIncreasing) {
EXPECT_EQ(kMaxTimeout, warmup_url_fetcher.GetFetchTimeout());
http_rtt = base::TimeDelta::FromSeconds(5);
estimator.SetStartTimeNullHttpRtt(http_rtt);
warmup_url_fetcher.SetHttpRttOverride(http_rtt);
EXPECT_EQ(kMaxTimeout, warmup_url_fetcher.GetFetchTimeout());
warmup_url_fetcher.FetchWarmupURL(0);
......@@ -583,9 +571,6 @@ TEST(WarmupURLFetcherTest, TestFetchTimeoutIncreasingWithFieldTrial) {
scoped_refptr<net::URLRequestContextGetter> request_context_getter =
new net::TestURLRequestContextGetter(message_loop.task_runner(),
std::move(test_request_context));
net::TestNetworkQualityEstimator estimator;
request_context_getter->GetURLRequestContext()->set_network_quality_estimator(
&estimator);
WarmupURLFetcherTest warmup_url_fetcher(request_context_getter);
EXPECT_FALSE(warmup_url_fetcher.IsFetchInFlight());
......@@ -593,7 +578,7 @@ TEST(WarmupURLFetcherTest, TestFetchTimeoutIncreasingWithFieldTrial) {
EXPECT_EQ(kMinTimeout, warmup_url_fetcher.GetFetchTimeout());
base::TimeDelta http_rtt = base::TimeDelta::FromSeconds(1);
estimator.SetStartTimeNullHttpRtt(http_rtt);
warmup_url_fetcher.SetHttpRttOverride(http_rtt);
EXPECT_EQ(http_rtt * 12, warmup_url_fetcher.GetFetchTimeout());
warmup_url_fetcher.FetchWarmupURL(1);
......@@ -603,7 +588,7 @@ TEST(WarmupURLFetcherTest, TestFetchTimeoutIncreasingWithFieldTrial) {
EXPECT_EQ(http_rtt * 48, warmup_url_fetcher.GetFetchTimeout());
http_rtt = base::TimeDelta::FromSeconds(5);
estimator.SetStartTimeNullHttpRtt(http_rtt);
warmup_url_fetcher.SetHttpRttOverride(http_rtt);
EXPECT_EQ(kMaxTimeout, warmup_url_fetcher.GetFetchTimeout());
warmup_url_fetcher.FetchWarmupURL(0);
......
......@@ -314,6 +314,8 @@ source_set("test_support") {
"test/test_network_connection_tracker.cc",
"test/test_network_connection_tracker.h",
"test/test_network_context.h",
"test/test_network_quality_tracker.cc",
"test/test_network_quality_tracker.h",
"test/test_network_service_client.cc",
"test/test_network_service_client.h",
"test/test_shared_url_loader_factory.cc",
......
......@@ -72,6 +72,35 @@ void NetworkQualityTracker::RemoveRTTAndThroughputEstimatesObserver(
rtt_and_throughput_observer_list_.RemoveObserver(observer);
}
void NetworkQualityTracker::ReportEffectiveConnectionTypeForTesting(
net::EffectiveConnectionType effective_connection_type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
effective_connection_type_ = effective_connection_type;
for (auto& observer : effective_connection_type_observer_list_)
observer.OnEffectiveConnectionTypeChanged(effective_connection_type);
}
void NetworkQualityTracker::ReportRTTsAndThroughputForTesting(
base::TimeDelta http_rtt,
int32_t downstream_throughput_kbps) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
http_rtt_ = http_rtt;
downlink_bandwidth_kbps_ = downstream_throughput_kbps;
for (auto& observer : rtt_and_throughput_observer_list_) {
observer.OnRTTOrThroughputEstimatesComputed(http_rtt_, http_rtt_,
downlink_bandwidth_kbps_);
}
}
// For testing only.
NetworkQualityTracker::NetworkQualityTracker()
: effective_connection_type_(net::EFFECTIVE_CONNECTION_TYPE_UNKNOWN),
downlink_bandwidth_kbps_(std::numeric_limits<int32_t>::max()),
binding_(this) {}
void NetworkQualityTracker::OnNetworkQualityChanged(
net::EffectiveConnectionType effective_connection_type,
base::TimeDelta http_rtt,
......
......@@ -29,7 +29,9 @@ class COMPONENT_EXPORT(NETWORK_CPP) NetworkQualityTracker
public:
class COMPONENT_EXPORT(NETWORK_CPP) EffectiveConnectionTypeObserver {
public:
// Called when there is a change in the effective connection type.
// Called when there is a change in the effective connection type. The
// |observer| is notified of the current effective connection type on the
// same thread on which it was added.
virtual void OnEffectiveConnectionTypeChanged(
net::EffectiveConnectionType type) = 0;
......@@ -49,6 +51,8 @@ class COMPONENT_EXPORT(NETWORK_CPP) NetworkQualityTracker
// RTT or downstream estimate. If either of the RTT estimates are
// unavailable, then the value of that estimate is set to base::TimeDelta().
// If downstream estimate is unavailable, its value is set to INT32_MAX.
// The |observer| is notified of the current effective connection type on
// the same thread on which it was added.
virtual void OnRTTOrThroughputEstimatesComputed(
base::TimeDelta http_rtt,
base::TimeDelta transport_rtt,
......@@ -122,7 +126,21 @@ class COMPONENT_EXPORT(NETWORK_CPP) NetworkQualityTracker
void RemoveRTTAndThroughputEstimatesObserver(
RTTAndThroughputEstimatesObserver* observer);
// Changes effective connection type estimate to the provided value, and
// reports |effective_connection_type| to all
// EffectiveConnectionTypeObservers.
void ReportEffectiveConnectionTypeForTesting(
net::EffectiveConnectionType effective_connection_type);
// Changes RTT and throughput estimate to the provided estimates, and
// reports it to all RTTAndThroughputEstimatesObservers.
void ReportRTTsAndThroughputForTesting(base::TimeDelta http_rtt,
int32_t downstream_throughput_kbps);
protected:
// Constructor for testing purposes only without the network service instance.
NetworkQualityTracker();
// NetworkQualityEstimatorManagerClient implementation. Protected for testing.
void OnNetworkQualityChanged(
net::EffectiveConnectionType effective_connection_type,
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "services/network/test/test_network_quality_tracker.h"
namespace network {
TestNetworkQualityTracker::TestNetworkQualityTracker() = default;
TestNetworkQualityTracker::~TestNetworkQualityTracker() = default;
} // namespace network
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SERVICES_NETWORK_TEST_TEST_NETWORK_QUALITY_TRACKER_H_
#define SERVICES_NETWORK_TEST_TEST_NETWORK_QUALITY_TRACKER_H_
#include "services/network/public/cpp/network_quality_tracker.h"
namespace network {
// Test version of NetworkQualityTracker without the network service instance.
class TestNetworkQualityTracker : public NetworkQualityTracker {
public:
TestNetworkQualityTracker();
~TestNetworkQualityTracker() override;
};
} // namespace network
#endif // SERVICES_NETWORK_TEST_TEST_NETWORK_QUALITY_TRACKER_H_
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