Commit b6e03254 authored by Alexandr Ilin's avatar Alexandr Ilin Committed by Commit Bot

predictors: Skip dns preresolution if a proxy is configured

The PreconnectManager issues preconnect requests only after it
preresolves a host name. If a fixed proxy configuration is in place,
there is no need to make a DNS request.

Bug: 808036
Change-Id: I15f6baf6405386fd6bd8154b2691c5b858c6e04b
Reviewed-on: https://chromium-review.googlesource.com/958913Reviewed-by: default avatarBenoit L <lizeb@chromium.org>
Commit-Queue: Alexandr Ilin <alexilin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543402}
parent 7b23f00a
...@@ -6,11 +6,14 @@ ...@@ -6,11 +6,14 @@
#include <utility> #include <utility>
#include "base/trace_event/trace_event.h"
#include "chrome/browser/predictors/resource_prefetch_predictor.h" #include "chrome/browser/predictors/resource_prefetch_predictor.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/resource_hints.h" #include "content/public/browser/resource_hints.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/http/transport_security_state.h" #include "net/http/transport_security_state.h"
#include "net/proxy_resolution/proxy_info.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
#include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context.h"
namespace predictors { namespace predictors {
...@@ -160,9 +163,16 @@ void PreconnectManager::TryToLaunchPreresolveJobs() { ...@@ -160,9 +163,16 @@ void PreconnectManager::TryToLaunchPreresolveJobs() {
PreresolveInfo* info = job.info; PreresolveInfo* info = job.info;
if (!info || !info->was_canceled) { if (!info || !info->was_canceled) {
int status = PreresolveUrl( int status;
job.url, base::Bind(&PreconnectManager::OnPreresolveFinished, if (WouldLikelyProxyURL(job.url)) {
weak_factory_.GetWeakPtr(), job)); // Skip preresolve and go straight to preconnect if a proxy is enabled.
status = net::OK;
} else {
status = PreresolveUrl(
job.url, base::Bind(&PreconnectManager::OnPreresolveFinished,
weak_factory_.GetWeakPtr(), job));
}
if (status == net::ERR_IO_PENDING) { if (status == net::ERR_IO_PENDING) {
// Will complete asynchronously. // Will complete asynchronously.
if (info) if (info)
...@@ -243,4 +253,17 @@ GURL PreconnectManager::GetHSTSRedirect(const GURL& url) const { ...@@ -243,4 +253,17 @@ GURL PreconnectManager::GetHSTSRedirect(const GURL& url) const {
return url.ReplaceComponents(replacements); return url.ReplaceComponents(replacements);
} }
bool PreconnectManager::WouldLikelyProxyURL(const GURL& url) const {
auto* proxy_resolution_service =
context_getter_->GetURLRequestContext()->proxy_resolution_service();
if (!proxy_resolution_service)
return false;
net::ProxyInfo info;
bool synchronous_success =
proxy_resolution_service->TryResolveProxySynchronously(
url, std::string(), &info, nullptr, net::NetLogWithSource());
return synchronous_success && !info.is_direct();
}
} // namespace predictors } // namespace predictors
...@@ -141,6 +141,7 @@ class PreconnectManager { ...@@ -141,6 +141,7 @@ class PreconnectManager {
void FinishPreresolve(const PreresolveJob& job, bool found, bool cached); void FinishPreresolve(const PreresolveJob& job, bool found, bool cached);
void AllPreresolvesForUrlFinished(PreresolveInfo* info); void AllPreresolvesForUrlFinished(PreresolveInfo* info);
GURL GetHSTSRedirect(const GURL& url) const; GURL GetHSTSRedirect(const GURL& url) const;
bool WouldLikelyProxyURL(const GURL& url) const;
base::WeakPtr<Delegate> delegate_; base::WeakPtr<Delegate> delegate_;
scoped_refptr<net::URLRequestContextGetter> context_getter_; scoped_refptr<net::URLRequestContextGetter> context_getter_;
......
...@@ -14,6 +14,9 @@ ...@@ -14,6 +14,9 @@
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "chrome/browser/predictors/resource_prefetch_predictor.h" #include "chrome/browser/predictors/resource_prefetch_predictor.h"
#include "content/public/test/test_browser_thread_bundle.h" #include "content/public/test/test_browser_thread_bundle.h"
#include "net/proxy_resolution/mock_proxy_resolver.h"
#include "net/proxy_resolution/proxy_config.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_test_util.h" #include "net/url_request/url_request_test_util.h"
#include "testing/gmock/include/gmock/gmock.h" #include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -332,4 +335,46 @@ TEST_F(PreconnectManagerTest, TestHSTSRedirectRespectedForPreconnect) { ...@@ -332,4 +335,46 @@ TEST_F(PreconnectManagerTest, TestHSTSRedirectRespectedForPreconnect) {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
class MockProxyConfigService : public net::ProxyConfigService {
public:
explicit MockProxyConfigService(const net::ProxyConfig& config)
: config_(net::ProxyConfigWithAnnotation(config,
TRAFFIC_ANNOTATION_FOR_TESTS)) {}
void AddObserver(Observer* observer) override {}
void RemoveObserver(Observer* observer) override {}
ConfigAvailability GetLatestProxyConfig(
net::ProxyConfigWithAnnotation* results) override {
*results = config_;
return CONFIG_VALID;
}
private:
net::ProxyConfigWithAnnotation config_;
};
// Tests that the predictor doesn't preresolve in the presence of the proxy
// server.
TEST_F(PreconnectManagerTest, TestPreresolveSkippedIfProxyEnabled) {
net::ProxyConfig proxy_config;
proxy_config.proxy_rules().ParseFromString("foopy:8080");
proxy_config.set_auto_detect(false);
net::ProxyResolutionService proxy_service(
std::make_unique<MockProxyConfigService>(proxy_config),
std::make_unique<net::MockAsyncProxyResolverFactory>(false), nullptr);
context_getter_->GetURLRequestContext()->set_proxy_resolution_service(
&proxy_service);
GURL main_frame_url("http://google.com");
GURL url_to_preconnect("http://cdn.google.com");
GURL url_to_preresolve("http://images.google.com");
EXPECT_CALL(*preconnect_manager_,
PreconnectUrl(url_to_preconnect, main_frame_url, 1, true));
EXPECT_CALL(*mock_delegate_, PreconnectFinishedProxy(main_frame_url));
preconnect_manager_->Start(main_frame_url,
{PreconnectRequest(url_to_preconnect, 1),
PreconnectRequest(url_to_preresolve, 0)});
base::RunLoop().RunUntilIdle();
}
} // namespace predictors } // namespace predictors
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