Commit b5b864d7 authored by Helen Li's avatar Helen Li Committed by Commit Bot

Make GCM's ConnectionFactoryImpl uses ProxyResolvingClientSocketFactory

ProxyResolvingClientSocketFactory takes care of creating a new
HttpNetworkSession that is based on a given net::URLRequestContext.

This CL switches GCM's ConnectionFactoryImpl to using
ProxyResolvingClientSocketFactory, so there is less code duplication. This
will makes it easier to standardize usage of ProxyResolvingClientSocket to
prepare for servicifying it.

Bug: 844187
Cq-Include-Trybots: luci.chromium.try:linux_mojo
Change-Id: I06b1f6b7c6fc7e8c458b24f2ee3dcc5664bd4c21
Reviewed-on: https://chromium-review.googlesource.com/1101058
Commit-Queue: Helen Li <xunjieli@chromium.org>
Reviewed-by: default avatarMaks Orlovich <morlovich@chromium.org>
Reviewed-by: default avatarNicolas Zea <zea@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567674}
parent ae094e39
...@@ -41,8 +41,6 @@ ...@@ -41,8 +41,6 @@
#include "google_apis/gcm/monitoring/gcm_stats_recorder.h" #include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
#include "google_apis/gcm/protocol/checkin.pb.h" #include "google_apis/gcm/protocol/checkin.pb.h"
#include "google_apis/gcm/protocol/mcs.pb.h" #include "google_apis/gcm/protocol/mcs.pb.h"
#include "net/http/http_network_session.h"
#include "net/http/http_transaction_factory.h"
#include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -280,12 +278,10 @@ std::unique_ptr<MCSClient> GCMInternalsBuilder::BuildMCSClient( ...@@ -280,12 +278,10 @@ std::unique_ptr<MCSClient> GCMInternalsBuilder::BuildMCSClient(
std::unique_ptr<ConnectionFactory> GCMInternalsBuilder::BuildConnectionFactory( std::unique_ptr<ConnectionFactory> GCMInternalsBuilder::BuildConnectionFactory(
const std::vector<GURL>& endpoints, const std::vector<GURL>& endpoints,
const net::BackoffEntry::Policy& backoff_policy, const net::BackoffEntry::Policy& backoff_policy,
net::HttpNetworkSession* gcm_network_session, net::URLRequestContext* url_request_context,
net::HttpNetworkSession* http_network_session,
GCMStatsRecorder* recorder) { GCMStatsRecorder* recorder) {
return base::WrapUnique<ConnectionFactory>( return base::WrapUnique<ConnectionFactory>(new ConnectionFactoryImpl(
new ConnectionFactoryImpl(endpoints, backoff_policy, gcm_network_session, endpoints, backoff_policy, url_request_context, recorder));
http_network_session, nullptr, recorder));
} }
GCMClientImpl::CheckinInfo::CheckinInfo() GCMClientImpl::CheckinInfo::CheckinInfo()
...@@ -342,16 +338,6 @@ void GCMClientImpl::Initialize( ...@@ -342,16 +338,6 @@ void GCMClientImpl::Initialize(
DCHECK(delegate); DCHECK(delegate);
url_request_context_getter_ = url_request_context_getter; url_request_context_getter_ = url_request_context_getter;
const net::HttpNetworkSession::Params* network_session_params =
url_request_context_getter_->GetURLRequestContext()->
GetNetworkSessionParams();
DCHECK(network_session_params);
const net::HttpNetworkSession::Context* network_session_context =
url_request_context_getter_->GetURLRequestContext()
->GetNetworkSessionContext();
network_session_.reset(new net::HttpNetworkSession(*network_session_params,
*network_session_context));
chrome_build_info_ = chrome_build_info; chrome_build_info_ = chrome_build_info;
gcm_store_.reset( gcm_store_.reset(
...@@ -508,13 +494,8 @@ void GCMClientImpl::InitializeMCSClient() { ...@@ -508,13 +494,8 @@ void GCMClientImpl::InitializeMCSClient() {
if (fallback_endpoint.is_valid()) if (fallback_endpoint.is_valid())
endpoints.push_back(fallback_endpoint); endpoints.push_back(fallback_endpoint);
connection_factory_ = internals_builder_->BuildConnectionFactory( connection_factory_ = internals_builder_->BuildConnectionFactory(
endpoints, endpoints, GetGCMBackoffPolicy(),
GetGCMBackoffPolicy(), url_request_context_getter_->GetURLRequestContext(), &recorder_);
network_session_.get(),
url_request_context_getter_->GetURLRequestContext()
->http_transaction_factory()
->GetSession(),
&recorder_);
connection_factory_->SetConnectionListener(this); connection_factory_->SetConnectionListener(this);
mcs_client_ = internals_builder_->BuildMCSClient( mcs_client_ = internals_builder_->BuildMCSClient(
chrome_build_info_.version, clock_, connection_factory_.get(), chrome_build_info_.version, clock_, connection_factory_.get(),
......
...@@ -43,7 +43,7 @@ class DataMessageStanza; ...@@ -43,7 +43,7 @@ class DataMessageStanza;
} // namespace mcs_proto } // namespace mcs_proto
namespace net { namespace net {
class HttpNetworkSession; class URLRequestContext;
} // namespace net } // namespace net
namespace gcm { namespace gcm {
...@@ -69,8 +69,7 @@ class GCMInternalsBuilder { ...@@ -69,8 +69,7 @@ class GCMInternalsBuilder {
virtual std::unique_ptr<ConnectionFactory> BuildConnectionFactory( virtual std::unique_ptr<ConnectionFactory> BuildConnectionFactory(
const std::vector<GURL>& endpoints, const std::vector<GURL>& endpoints,
const net::BackoffEntry::Policy& backoff_policy, const net::BackoffEntry::Policy& backoff_policy,
net::HttpNetworkSession* gcm_network_session, net::URLRequestContext* url_request_context,
net::HttpNetworkSession* http_network_session,
GCMStatsRecorder* recorder); GCMStatsRecorder* recorder);
}; };
...@@ -363,7 +362,6 @@ class GCMClientImpl ...@@ -363,7 +362,6 @@ class GCMClientImpl
// resetting and loading from the store again and again. // resetting and loading from the store again and again.
bool gcm_store_reset_; bool gcm_store_reset_;
std::unique_ptr<net::HttpNetworkSession> network_session_;
std::unique_ptr<ConnectionFactory> connection_factory_; std::unique_ptr<ConnectionFactory> connection_factory_;
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
......
...@@ -226,8 +226,7 @@ class FakeGCMInternalsBuilder : public GCMInternalsBuilder { ...@@ -226,8 +226,7 @@ class FakeGCMInternalsBuilder : public GCMInternalsBuilder {
std::unique_ptr<ConnectionFactory> BuildConnectionFactory( std::unique_ptr<ConnectionFactory> BuildConnectionFactory(
const std::vector<GURL>& endpoints, const std::vector<GURL>& endpoints,
const net::BackoffEntry::Policy& backoff_policy, const net::BackoffEntry::Policy& backoff_policy,
net::HttpNetworkSession* gcm_network_session, net::URLRequestContext* url_request_context,
net::HttpNetworkSession* http_network_session,
GCMStatsRecorder* recorder) override; GCMStatsRecorder* recorder) override;
private: private:
...@@ -257,8 +256,7 @@ std::unique_ptr<ConnectionFactory> ...@@ -257,8 +256,7 @@ std::unique_ptr<ConnectionFactory>
FakeGCMInternalsBuilder::BuildConnectionFactory( FakeGCMInternalsBuilder::BuildConnectionFactory(
const std::vector<GURL>& endpoints, const std::vector<GURL>& endpoints,
const net::BackoffEntry::Policy& backoff_policy, const net::BackoffEntry::Policy& backoff_policy,
net::HttpNetworkSession* gcm_network_session, net::URLRequestContext* url_request_context,
net::HttpNetworkSession* http_network_session,
GCMStatsRecorder* recorder) { GCMStatsRecorder* recorder) {
return base::WrapUnique<ConnectionFactory>(new FakeConnectionFactory()); return base::WrapUnique<ConnectionFactory>(new FakeConnectionFactory());
} }
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include "google_apis/gcm/monitoring/gcm_stats_recorder.h" #include "google_apis/gcm/monitoring/gcm_stats_recorder.h"
#include "google_apis/gcm/protocol/mcs.pb.h" #include "google_apis/gcm/protocol/mcs.pb.h"
#include "net/base/net_errors.h" #include "net/base/net_errors.h"
#include "net/http/http_network_session.h"
#include "net/http/http_request_headers.h" #include "net/http/http_request_headers.h"
#include "net/http/proxy_fallback.h" #include "net/http/proxy_fallback.h"
#include "net/log/net_log_source_type.h" #include "net/log/net_log_source_type.h"
...@@ -25,6 +24,7 @@ ...@@ -25,6 +24,7 @@
#include "net/socket/client_socket_pool_manager.h" #include "net/socket/client_socket_pool_manager.h"
#include "net/ssl/ssl_config_service.h" #include "net/ssl/ssl_config_service.h"
#include "services/network/proxy_resolving_client_socket.h" #include "services/network/proxy_resolving_client_socket.h"
#include "services/network/proxy_resolving_client_socket_factory.h"
namespace gcm { namespace gcm {
...@@ -52,16 +52,15 @@ bool ShouldRestorePreviousBackoff(const base::TimeTicks& login_time, ...@@ -52,16 +52,15 @@ bool ShouldRestorePreviousBackoff(const base::TimeTicks& login_time,
ConnectionFactoryImpl::ConnectionFactoryImpl( ConnectionFactoryImpl::ConnectionFactoryImpl(
const std::vector<GURL>& mcs_endpoints, const std::vector<GURL>& mcs_endpoints,
const net::BackoffEntry::Policy& backoff_policy, const net::BackoffEntry::Policy& backoff_policy,
net::HttpNetworkSession* gcm_network_session, net::URLRequestContext* url_request_context,
net::HttpNetworkSession* http_network_session,
net::NetLog* net_log,
GCMStatsRecorder* recorder) GCMStatsRecorder* recorder)
: mcs_endpoints_(mcs_endpoints), : mcs_endpoints_(mcs_endpoints),
next_endpoint_(0), next_endpoint_(0),
last_successful_endpoint_(0), last_successful_endpoint_(0),
backoff_policy_(backoff_policy), backoff_policy_(backoff_policy),
gcm_network_session_(gcm_network_session), socket_factory_(
http_network_session_(http_network_session), std::make_unique<network::ProxyResolvingClientSocketFactory>(
url_request_context)),
connecting_(false), connecting_(false),
waiting_for_backoff_(false), waiting_for_backoff_(false),
waiting_for_network_online_(false), waiting_for_network_online_(false),
...@@ -70,8 +69,6 @@ ConnectionFactoryImpl::ConnectionFactoryImpl( ...@@ -70,8 +69,6 @@ ConnectionFactoryImpl::ConnectionFactoryImpl(
listener_(NULL), listener_(NULL),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
DCHECK_GE(mcs_endpoints_.size(), 1U); DCHECK_GE(mcs_endpoints_.size(), 1U);
DCHECK(!http_network_session_ ||
(gcm_network_session_ != http_network_session_));
} }
ConnectionFactoryImpl::~ConnectionFactoryImpl() { ConnectionFactoryImpl::~ConnectionFactoryImpl() {
...@@ -319,11 +316,7 @@ void ConnectionFactoryImpl::StartConnection() { ...@@ -319,11 +316,7 @@ void ConnectionFactoryImpl::StartConnection() {
connecting_ = true; connecting_ = true;
GURL current_endpoint = GetCurrentEndpoint(); GURL current_endpoint = GetCurrentEndpoint();
recorder_->RecordConnectionInitiated(current_endpoint.host()); recorder_->RecordConnectionInitiated(current_endpoint.host());
UpdateFromHttpNetworkSession(); socket_ = socket_factory_->CreateSocket(current_endpoint, true /*use_tls*/);
net::SSLConfig ssl_config;
gcm_network_session_->ssl_config_service()->GetSSLConfig(&ssl_config);
socket_ = std::make_unique<network::ProxyResolvingClientSocket>(
gcm_network_session_, ssl_config, current_endpoint, true /*use_tls*/);
int status = socket_->Connect(base::BindRepeating( int status = socket_->Connect(base::BindRepeating(
&ConnectionFactoryImpl::OnConnectDone, weak_ptr_factory_.GetWeakPtr())); &ConnectionFactoryImpl::OnConnectDone, weak_ptr_factory_.GetWeakPtr()));
if (status != net::ERR_IO_PENDING) if (status != net::ERR_IO_PENDING)
...@@ -472,15 +465,4 @@ void ConnectionFactoryImpl::CloseSocket() { ...@@ -472,15 +465,4 @@ void ConnectionFactoryImpl::CloseSocket() {
socket_ = nullptr; socket_ = nullptr;
} }
void ConnectionFactoryImpl::UpdateFromHttpNetworkSession() {
if (!http_network_session_ || !http_network_session_->http_auth_cache())
return;
gcm_network_session_->http_auth_cache()->UpdateAllFrom(
*http_network_session_->http_auth_cache());
if (!http_network_session_->IsQuicEnabled())
gcm_network_session_->DisableQuic();
}
} // namespace gcm } // namespace gcm
...@@ -22,11 +22,11 @@ ...@@ -22,11 +22,11 @@
namespace network { namespace network {
class ProxyResolvingClientSocket; class ProxyResolvingClientSocket;
class ProxyResolvingClientSocketFactory;
} }
namespace net { namespace net {
class HttpNetworkSession; class URLRequestContext;
class NetLog;
} }
namespace gcm { namespace gcm {
...@@ -37,20 +37,12 @@ class GCM_EXPORT ConnectionFactoryImpl : ...@@ -37,20 +37,12 @@ class GCM_EXPORT ConnectionFactoryImpl :
public ConnectionFactory, public ConnectionFactory,
public net::NetworkChangeNotifier::NetworkChangeObserver { public net::NetworkChangeNotifier::NetworkChangeObserver {
public: public:
// |http_network_session| is an optional network session to use as a source
// for proxy auth credentials (via its HttpAuthCache). |gcm_network_session|
// is the network session through which GCM connections should be made, and
// must not be the same as |http_network_session|.
//
// The caller is responsible for making sure the ConnectionFactoryImpl is // The caller is responsible for making sure the ConnectionFactoryImpl is
// destroyed before the |gcm_network_session| and |http_network_session|. // destroyed before the |url_request_context|.
ConnectionFactoryImpl( ConnectionFactoryImpl(const std::vector<GURL>& mcs_endpoints,
const std::vector<GURL>& mcs_endpoints, const net::BackoffEntry::Policy& backoff_policy,
const net::BackoffEntry::Policy& backoff_policy, net::URLRequestContext* url_request_context,
net::HttpNetworkSession* gcm_network_session, GCMStatsRecorder* recorder);
net::HttpNetworkSession* http_network_session,
net::NetLog* net_log,
GCMStatsRecorder* recorder);
~ConnectionFactoryImpl() override; ~ConnectionFactoryImpl() override;
// ConnectionFactory implementation. // ConnectionFactory implementation.
...@@ -147,11 +139,8 @@ class GCM_EXPORT ConnectionFactoryImpl : ...@@ -147,11 +139,8 @@ class GCM_EXPORT ConnectionFactoryImpl :
const net::BackoffEntry::Policy backoff_policy_; const net::BackoffEntry::Policy backoff_policy_;
// ---- net:: components for establishing connections. ---- // ---- net:: components for establishing connections. ----
// Network session for creating new GCM connections. // Socket factory for creating new GCM connections.
net::HttpNetworkSession* gcm_network_session_; std::unique_ptr<network::ProxyResolvingClientSocketFactory> socket_factory_;
// HTTP Network session. If set, is used for extracting proxy auth
// credentials. If nullptr, is ignored.
net::HttpNetworkSession* http_network_session_;
// The handle to the socket for the current connection, if one exists. // The handle to the socket for the current connection, if one exists.
std::unique_ptr<network::ProxyResolvingClientSocket> socket_; std::unique_ptr<network::ProxyResolvingClientSocket> socket_;
// Current backoff entry. // Current backoff entry.
......
...@@ -16,8 +16,8 @@ ...@@ -16,8 +16,8 @@
#include "google_apis/gcm/engine/fake_connection_handler.h" #include "google_apis/gcm/engine/fake_connection_handler.h"
#include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" #include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h"
#include "net/base/backoff_entry.h" #include "net/base/backoff_entry.h"
#include "net/http/http_network_session.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h" #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
class Policy; class Policy;
...@@ -86,7 +86,8 @@ void WriteContinuation() { ...@@ -86,7 +86,8 @@ void WriteContinuation() {
// backoff policy. // backoff policy.
class TestConnectionFactoryImpl : public ConnectionFactoryImpl { class TestConnectionFactoryImpl : public ConnectionFactoryImpl {
public: public:
TestConnectionFactoryImpl(const base::Closure& finished_callback); TestConnectionFactoryImpl(net::URLRequestContext* request_context,
const base::Closure& finished_callback);
~TestConnectionFactoryImpl() override; ~TestConnectionFactoryImpl() override;
void InitializeFactory(); void InitializeFactory();
...@@ -141,12 +142,11 @@ class TestConnectionFactoryImpl : public ConnectionFactoryImpl { ...@@ -141,12 +142,11 @@ class TestConnectionFactoryImpl : public ConnectionFactoryImpl {
}; };
TestConnectionFactoryImpl::TestConnectionFactoryImpl( TestConnectionFactoryImpl::TestConnectionFactoryImpl(
net::URLRequestContext* request_context,
const base::Closure& finished_callback) const base::Closure& finished_callback)
: ConnectionFactoryImpl(BuildEndpoints(), : ConnectionFactoryImpl(BuildEndpoints(),
net::BackoffEntry::Policy(), net::BackoffEntry::Policy(),
NULL, request_context,
NULL,
NULL,
&dummy_recorder_), &dummy_recorder_),
connect_result_(net::ERR_UNEXPECTED), connect_result_(net::ERR_UNEXPECTED),
num_expected_attempts_(0), num_expected_attempts_(0),
...@@ -274,16 +274,23 @@ class ConnectionFactoryImplTest ...@@ -274,16 +274,23 @@ class ConnectionFactoryImplTest
private: private:
void ConnectionsComplete(); void ConnectionsComplete();
TestConnectionFactoryImpl factory_;
base::MessageLoop message_loop_; base::MessageLoop message_loop_;
// Dummy request context that is not used to make network requests, and is
// added to make ProxyResolvingClientSocketFactory to not DCHECK on a null
// context.
net::TestURLRequestContext request_context_;
TestConnectionFactoryImpl factory_;
std::unique_ptr<base::RunLoop> run_loop_; std::unique_ptr<base::RunLoop> run_loop_;
GURL connected_server_; GURL connected_server_;
}; };
ConnectionFactoryImplTest::ConnectionFactoryImplTest() ConnectionFactoryImplTest::ConnectionFactoryImplTest()
: factory_(base::Bind(&ConnectionFactoryImplTest::ConnectionsComplete, : factory_(&request_context_,
base::Unretained(this))), base::Bind(&ConnectionFactoryImplTest::ConnectionsComplete,
base::Unretained(this))),
run_loop_(new base::RunLoop()) { run_loop_(new base::RunLoop()) {
factory()->SetConnectionListener(this); factory()->SetConnectionListener(this);
factory()->Initialize( factory()->Initialize(
......
...@@ -216,7 +216,6 @@ class MCSProbe { ...@@ -216,7 +216,6 @@ class MCSProbe {
private: private:
void CheckIn(); void CheckIn();
void InitializeNetworkState(); void InitializeNetworkState();
void BuildNetworkSession();
void LoadCallback(std::unique_ptr<GCMStore::LoadResult> load_result); void LoadCallback(std::unique_ptr<GCMStore::LoadResult> load_result);
void UpdateCallback(bool success); void UpdateCallback(bool success);
...@@ -305,14 +304,13 @@ MCSProbe::~MCSProbe() { ...@@ -305,14 +304,13 @@ MCSProbe::~MCSProbe() {
void MCSProbe::Start() { void MCSProbe::Start() {
file_thread_.Start(); file_thread_.Start();
InitializeNetworkState(); InitializeNetworkState();
BuildNetworkSession();
std::vector<GURL> endpoints( std::vector<GURL> endpoints(
1, GURL("https://" + 1, GURL("https://" +
net::HostPortPair(server_host_, server_port_).ToString())); net::HostPortPair(server_host_, server_port_).ToString()));
connection_factory_ = std::make_unique<ConnectionFactoryImpl>( connection_factory_ = std::make_unique<ConnectionFactoryImpl>(
endpoints, kDefaultBackoffPolicy, network_session_.get(), nullptr, endpoints, kDefaultBackoffPolicy,
&net_log_, &recorder_); url_request_context_getter_->GetURLRequestContext(), &recorder_);
gcm_store_ = std::make_unique<GCMStoreImpl>( gcm_store_ = std::make_unique<GCMStoreImpl>(
gcm_store_path_, file_thread_.task_runner(), gcm_store_path_, file_thread_.task_runner(),
std::make_unique<FakeEncryptor>()); std::make_unique<FakeEncryptor>());
...@@ -389,30 +387,6 @@ void MCSProbe::InitializeNetworkState() { ...@@ -389,30 +387,6 @@ void MCSProbe::InitializeNetworkState() {
net::ProxyResolutionService::CreateDirectWithNetLog(&net_log_); net::ProxyResolutionService::CreateDirectWithNetLog(&net_log_);
} }
void MCSProbe::BuildNetworkSession() {
net::HttpNetworkSession::Params session_params;
session_params.ignore_certificate_errors = true;
session_params.testing_fixed_http_port = 0;
session_params.testing_fixed_https_port = 0;
net::HttpNetworkSession::Context session_context;
session_context.host_resolver = host_resolver_.get();
session_context.cert_verifier = cert_verifier_.get();
session_context.channel_id_service = system_channel_id_service_.get();
session_context.transport_security_state = transport_security_state_.get();
session_context.cert_transparency_verifier =
cert_transparency_verifier_.get();
session_context.ct_policy_enforcer = ct_policy_enforcer_.get();
session_context.ssl_config_service = new net::SSLConfigServiceDefaults();
session_context.http_auth_handler_factory = http_auth_handler_factory_.get();
session_context.http_server_properties = http_server_properties_.get();
session_context.net_log = &net_log_;
session_context.proxy_resolution_service = proxy_resolution_service_.get();
network_session_ = std::make_unique<net::HttpNetworkSession>(session_params,
session_context);
}
void MCSProbe::ErrorCallback() { void MCSProbe::ErrorCallback() {
LOG(INFO) << "MCS error happened"; LOG(INFO) << "MCS error happened";
} }
......
...@@ -59,6 +59,11 @@ ProxyResolvingClientSocketFactory::ProxyResolvingClientSocketFactory( ...@@ -59,6 +59,11 @@ ProxyResolvingClientSocketFactory::ProxyResolvingClientSocketFactory(
session_params.enable_http2 = reference_params->enable_http2; session_params.enable_http2 = reference_params->enable_http2;
session_params.enable_http2_alternative_service = session_params.enable_http2_alternative_service =
reference_params->enable_http2_alternative_service; reference_params->enable_http2_alternative_service;
// Note that ProxyResolvingClientSocket uses either
// net::InitSocketHandleForTlsConnect() or
// net::InitSocketHandleForRawConnect() to establish connections through
// socket pools. QUIC's connection establishment is in another path, so
// enabling QUIC won't do anything here.
} }
network_session_ = std::make_unique<net::HttpNetworkSession>(session_params, network_session_ = std::make_unique<net::HttpNetworkSession>(session_params,
......
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