From 338cd36a1deb48bbd66d8f920b5aa9da1a6132d2 Mon Sep 17 00:00:00 2001 From: rtenneti Date: Mon, 5 Jan 2015 16:20:07 -0800 Subject: QUIC - Persist MRU 200 server's srtt (smoothed_rtt) of ServerNetworkStats to disk. + Converted ServerNetworkStatsMap to MRUCache. + Renamed "NetworkStats" to "ServerNetworkStats" and made it a top level struct. + Didn't persist QuicBandwidth (bandwidth_estimate) because it is not being used + Deleted unnecessary net:: prefixes. + Ran "git cl format net". R=rch@chromium.org Review URL: https://codereview.chromium.org/826543002 Cr-Commit-Position: refs/heads/master@{#310005} --- net/http/http_server_properties.h | 23 +- net/http/http_server_properties_impl.cc | 36 ++- net/http/http_server_properties_impl.h | 19 +- net/http/http_server_properties_impl_unittest.cc | 60 ++++- net/http/http_server_properties_manager.cc | 290 +++++++++++++-------- net/http/http_server_properties_manager.h | 10 +- .../http_server_properties_manager_unittest.cc | 214 +++++++++------ net/quic/quic_stream_factory.cc | 4 +- 8 files changed, 421 insertions(+), 235 deletions(-) (limited to 'net') diff --git a/net/http/http_server_properties.h b/net/http/http_server_properties.h index b860cc7..ce939e1 100644 --- a/net/http/http_server_properties.h +++ b/net/http/http_server_properties.h @@ -129,10 +129,18 @@ struct NET_EXPORT SupportsQuic { std::string address; }; +struct NET_EXPORT ServerNetworkStats { + ServerNetworkStats() : bandwidth_estimate(QuicBandwidth::Zero()) {} + + base::TimeDelta srtt; + QuicBandwidth bandwidth_estimate; +}; + typedef base::MRUCache< HostPortPair, AlternateProtocolInfo> AlternateProtocolMap; typedef base::MRUCache SpdySettingsMap; typedef std::map SupportsQuicMap; +typedef base::MRUCache ServerNetworkStatsMap; extern const char kAlternateProtocolHeader[]; @@ -143,13 +151,6 @@ extern const char kAlternateProtocolHeader[]; // * Spdy Settings (like CWND ID field) class NET_EXPORT HttpServerProperties { public: - struct NetworkStats { - NetworkStats() : bandwidth_estimate(QuicBandwidth::Zero()) {} - - base::TimeDelta srtt; - QuicBandwidth bandwidth_estimate; - }; - HttpServerProperties() {} virtual ~HttpServerProperties() {} @@ -237,10 +238,12 @@ class NET_EXPORT HttpServerProperties { virtual const SupportsQuicMap& supports_quic_map() const = 0; virtual void SetServerNetworkStats(const HostPortPair& host_port_pair, - NetworkStats stats) = 0; + ServerNetworkStats stats) = 0; - virtual const NetworkStats* GetServerNetworkStats( - const HostPortPair& host_port_pair) const = 0; + virtual const ServerNetworkStats* GetServerNetworkStats( + const HostPortPair& host_port_pair) = 0; + + virtual const ServerNetworkStatsMap& server_network_stats_map() const = 0; private: DISALLOW_COPY_AND_ASSIGN(HttpServerProperties); diff --git a/net/http/http_server_properties_impl.cc b/net/http/http_server_properties_impl.cc index 8a8e49a3..f571c11 100644 --- a/net/http/http_server_properties_impl.cc +++ b/net/http/http_server_properties_impl.cc @@ -24,6 +24,7 @@ HttpServerPropertiesImpl::HttpServerPropertiesImpl() : spdy_servers_map_(SpdyServerHostPortMap::NO_AUTO_EVICT), alternate_protocol_map_(AlternateProtocolMap::NO_AUTO_EVICT), spdy_settings_map_(SpdySettingsMap::NO_AUTO_EVICT), + server_network_stats_map_(ServerNetworkStatsMap::NO_AUTO_EVICT), alternate_protocol_probability_threshold_(1), weak_ptr_factory_(this) { canonical_suffixes_.push_back(".c.youtube.com"); @@ -109,6 +110,15 @@ void HttpServerPropertiesImpl::InitializeSupportsQuic( } } +void HttpServerPropertiesImpl::InitializeServerNetworkStats( + ServerNetworkStatsMap* server_network_stats_map) { + for (ServerNetworkStatsMap::reverse_iterator it = + server_network_stats_map->rbegin(); + it != server_network_stats_map->rend(); ++it) { + server_network_stats_map_.Put(it->first, it->second); + } +} + void HttpServerPropertiesImpl::GetSpdyServerList( base::ListValue* spdy_server_list, size_t max_size) const { @@ -155,10 +165,11 @@ void HttpServerPropertiesImpl::Clear() { canonical_host_to_origin_map_.clear(); spdy_settings_map_.Clear(); supports_quic_map_.clear(); + server_network_stats_map_.Clear(); } bool HttpServerPropertiesImpl::SupportsSpdy( - const net::HostPortPair& host_port_pair) { + const HostPortPair& host_port_pair) { DCHECK(CalledOnValidThread()); if (host_port_pair.host().empty()) return false; @@ -171,7 +182,7 @@ bool HttpServerPropertiesImpl::SupportsSpdy( } void HttpServerPropertiesImpl::SetSupportsSpdy( - const net::HostPortPair& host_port_pair, + const HostPortPair& host_port_pair, bool support_spdy) { DCHECK(CalledOnValidThread()); if (host_port_pair.host().empty()) @@ -412,28 +423,31 @@ void HttpServerPropertiesImpl::SetSupportsQuic( supports_quic_map_.insert(std::make_pair(host_port_pair, supports_quic)); } -const SupportsQuicMap& -HttpServerPropertiesImpl::supports_quic_map() const { +const SupportsQuicMap& HttpServerPropertiesImpl::supports_quic_map() const { return supports_quic_map_; } void HttpServerPropertiesImpl::SetServerNetworkStats( const HostPortPair& host_port_pair, - NetworkStats stats) { - server_network_stats_map_[host_port_pair] = stats; + ServerNetworkStats stats) { + server_network_stats_map_.Put(host_port_pair, stats); } -const HttpServerProperties::NetworkStats* -HttpServerPropertiesImpl::GetServerNetworkStats( - const HostPortPair& host_port_pair) const { - ServerNetworkStatsMap::const_iterator it = - server_network_stats_map_.find(host_port_pair); +const ServerNetworkStats* HttpServerPropertiesImpl::GetServerNetworkStats( + const HostPortPair& host_port_pair) { + ServerNetworkStatsMap::iterator it = + server_network_stats_map_.Get(host_port_pair); if (it == server_network_stats_map_.end()) { return NULL; } return &it->second; } +const ServerNetworkStatsMap& +HttpServerPropertiesImpl::server_network_stats_map() const { + return server_network_stats_map_; +} + void HttpServerPropertiesImpl::SetAlternateProtocolProbabilityThreshold( double threshold) { alternate_protocol_probability_threshold_ = threshold; diff --git a/net/http/http_server_properties_impl.h b/net/http/http_server_properties_impl.h index 46df379..fc98689 100644 --- a/net/http/http_server_properties_impl.h +++ b/net/http/http_server_properties_impl.h @@ -44,6 +44,9 @@ class NET_EXPORT HttpServerPropertiesImpl void InitializeSupportsQuic(SupportsQuicMap* supports_quic_map); + void InitializeServerNetworkStats( + ServerNetworkStatsMap* server_network_stats_map); + // Get the list of servers (host/port) that support SPDY. The max_size is the // number of MRU servers that support SPDY that are to be returned. void GetSpdyServerList(base::ListValue* spdy_server_list, @@ -51,8 +54,7 @@ class NET_EXPORT HttpServerPropertiesImpl // Returns flattened string representation of the |host_port_pair|. Used by // unittests. - static std::string GetFlattenedSpdyServer( - const net::HostPortPair& host_port_pair); + static std::string GetFlattenedSpdyServer(const HostPortPair& host_port_pair); // Debugging to simulate presence of an AlternateProtocol. // If we don't have an alternate protocol in the map for any given host/port @@ -62,7 +64,7 @@ class NET_EXPORT HttpServerPropertiesImpl // Returns the canonical host suffix for |server|, or std::string() if none // exists. - std::string GetCanonicalSuffix(const net::HostPortPair& server); + std::string GetCanonicalSuffix(const HostPortPair& server); // ----------------------------- // HttpServerProperties methods: @@ -142,18 +144,19 @@ class NET_EXPORT HttpServerPropertiesImpl const SupportsQuicMap& supports_quic_map() const override; - // Methods for NetworkStats. + // Methods for ServerNetworkStats. void SetServerNetworkStats(const HostPortPair& host_port_pair, - NetworkStats stats) override; + ServerNetworkStats stats) override; - const NetworkStats* GetServerNetworkStats( - const HostPortPair& host_port_pair) const override; + const ServerNetworkStats* GetServerNetworkStats( + const HostPortPair& host_port_pair) override; + + const ServerNetworkStatsMap& server_network_stats_map() const override; private: // |spdy_servers_map_| has flattened representation of servers (host, port) // that either support or not support SPDY protocol. typedef base::MRUCache SpdyServerHostPortMap; - typedef std::map ServerNetworkStatsMap; typedef std::map CanonicalHostMap; typedef std::vector CanonicalSufficList; // List of broken host:ports and the times when they can be expired. diff --git a/net/http/http_server_properties_impl_unittest.cc b/net/http/http_server_properties_impl_unittest.cc index 4dda0db..1675833 100644 --- a/net/http/http_server_properties_impl_unittest.cc +++ b/net/http/http_server_properties_impl_unittest.cc @@ -295,8 +295,8 @@ TEST_F(AlternateProtocolServerPropertiesTest, Initialize) { impl_.InitializeAlternateProtocolServers(&alternate_protocol_map); // Verify test_host_port_pair3 is the MRU server. - const net::AlternateProtocolMap& map = impl_.alternate_protocol_map(); - net::AlternateProtocolMap::const_iterator it = map.begin(); + const AlternateProtocolMap& map = impl_.alternate_protocol_map(); + AlternateProtocolMap::const_iterator it = map.begin(); it = map.begin(); EXPECT_TRUE(it->first.Equals(test_host_port_pair3)); EXPECT_EQ(1234, it->second.port); @@ -317,8 +317,8 @@ TEST_F(AlternateProtocolServerPropertiesTest, MRUOfHasAlternateProtocol) { HostPortPair test_host_port_pair2("foo2", 80); impl_.SetAlternateProtocol(test_host_port_pair2, 1234, NPN_SPDY_3, 1); - const net::AlternateProtocolMap& map = impl_.alternate_protocol_map(); - net::AlternateProtocolMap::const_iterator it = map.begin(); + const AlternateProtocolMap& map = impl_.alternate_protocol_map(); + AlternateProtocolMap::const_iterator it = map.begin(); EXPECT_TRUE(it->first.Equals(test_host_port_pair2)); EXPECT_EQ(1234, it->second.port); EXPECT_EQ(NPN_SPDY_3, it->second.protocol); @@ -337,8 +337,8 @@ TEST_F(AlternateProtocolServerPropertiesTest, MRUOfGetAlternateProtocol) { HostPortPair test_host_port_pair2("foo2", 80); impl_.SetAlternateProtocol(test_host_port_pair2, 1234, NPN_SPDY_3, 1); - const net::AlternateProtocolMap& map = impl_.alternate_protocol_map(); - net::AlternateProtocolMap::const_iterator it = map.begin(); + const AlternateProtocolMap& map = impl_.alternate_protocol_map(); + AlternateProtocolMap::const_iterator it = map.begin(); EXPECT_TRUE(it->first.Equals(test_host_port_pair2)); EXPECT_EQ(1234, it->second.port); EXPECT_EQ(NPN_SPDY_3, it->second.protocol); @@ -685,8 +685,8 @@ TEST_F(SpdySettingsServerPropertiesTest, MRUOfGetSpdySettings) { EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id2, flags2, value2)); // Verify the first element is docs.google.com:443. - const net::SpdySettingsMap& map = impl_.spdy_settings_map(); - net::SpdySettingsMap::const_iterator it = map.begin(); + const SpdySettingsMap& map = impl_.spdy_settings_map(); + SpdySettingsMap::const_iterator it = map.begin(); EXPECT_TRUE(it->first.Equals(spdy_server_docs)); const SettingsMap& settings_map2_ret = it->second; ASSERT_EQ(1U, settings_map2_ret.size()); @@ -755,6 +755,50 @@ TEST_F(SupportsQuicServerPropertiesTest, SetSupportsQuic) { EXPECT_FALSE(supports_quic2.used_quic); EXPECT_EQ("", supports_quic2.address); } + +typedef HttpServerPropertiesImplTest ServerNetworkStatsServerPropertiesTest; + +TEST_F(ServerNetworkStatsServerPropertiesTest, Initialize) { + HostPortPair google_server("www.google.com", 443); + + // Check by initializing empty ServerNetworkStats. + ServerNetworkStatsMap server_network_stats_map( + ServerNetworkStatsMap::NO_AUTO_EVICT); + impl_.InitializeServerNetworkStats(&server_network_stats_map); + const ServerNetworkStats* stats = impl_.GetServerNetworkStats(google_server); + EXPECT_EQ(NULL, stats); + + // Check by initializing with www.google.com:443. + ServerNetworkStats stats1; + stats1.srtt = base::TimeDelta::FromMicroseconds(10); + stats1.bandwidth_estimate = QuicBandwidth::FromBitsPerSecond(100); + server_network_stats_map.Put(google_server, stats1); + impl_.InitializeServerNetworkStats(&server_network_stats_map); + + const ServerNetworkStats* stats2 = impl_.GetServerNetworkStats(google_server); + EXPECT_EQ(10, stats2->srtt.ToInternalValue()); + EXPECT_EQ(100, stats2->bandwidth_estimate.ToBitsPerSecond()); +} + +TEST_F(ServerNetworkStatsServerPropertiesTest, SetServerNetworkStats) { + HostPortPair foo_server("foo", 80); + const ServerNetworkStats* stats = impl_.GetServerNetworkStats(foo_server); + EXPECT_EQ(NULL, stats); + + ServerNetworkStats stats1; + stats1.srtt = base::TimeDelta::FromMicroseconds(10); + stats1.bandwidth_estimate = QuicBandwidth::FromBitsPerSecond(100); + impl_.SetServerNetworkStats(foo_server, stats1); + + const ServerNetworkStats* stats2 = impl_.GetServerNetworkStats(foo_server); + EXPECT_EQ(10, stats2->srtt.ToInternalValue()); + EXPECT_EQ(100, stats2->bandwidth_estimate.ToBitsPerSecond()); + + impl_.Clear(); + const ServerNetworkStats* stats3 = impl_.GetServerNetworkStats(foo_server); + EXPECT_EQ(NULL, stats3); +} + } // namespace } // namespace net diff --git a/net/http/http_server_properties_manager.cc b/net/http/http_server_properties_manager.cc index 1cc1c7c..af72884 100644 --- a/net/http/http_server_properties_manager.cc +++ b/net/http/http_server_properties_manager.cc @@ -47,6 +47,9 @@ const int kMaxSpdySettingsHostsToPersist = 200; // Persist 300 MRU SupportsSpdyServerHostPortPairs. const int kMaxSupportsSpdyServerHostsToPersist = 300; +// Persist 200 ServerNetworkStats. +const int kMaxServerNetworkStatsHostsToPersist = 200; + } // namespace //////////////////////////////////////////////////////////////////////////////// @@ -83,7 +86,7 @@ void HttpServerPropertiesManager::InitializeOnNetworkThread() { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); network_weak_ptr_factory_.reset( new base::WeakPtrFactory(this)); - http_server_properties_impl_.reset(new net::HttpServerPropertiesImpl()); + http_server_properties_impl_.reset(new HttpServerPropertiesImpl()); network_prefs_update_timer_.reset( new base::OneShotTimer); @@ -114,8 +117,7 @@ void HttpServerPropertiesManager::SetVersion( } // This is required for conformance with the HttpServerProperties interface. -base::WeakPtr -HttpServerPropertiesManager::GetWeakPtr() { +base::WeakPtr HttpServerPropertiesManager::GetWeakPtr() { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); return network_weak_ptr_factory_->GetWeakPtr(); } @@ -131,15 +133,13 @@ void HttpServerPropertiesManager::Clear(const base::Closure& completion) { UpdatePrefsFromCacheOnNetworkThread(completion); } -bool HttpServerPropertiesManager::SupportsSpdy( - const net::HostPortPair& server) { +bool HttpServerPropertiesManager::SupportsSpdy(const HostPortPair& server) { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); return http_server_properties_impl_->SupportsSpdy(server); } -void HttpServerPropertiesManager::SetSupportsSpdy( - const net::HostPortPair& server, - bool support_spdy) { +void HttpServerPropertiesManager::SetSupportsSpdy(const HostPortPair& server, + bool support_spdy) { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); http_server_properties_impl_->SetSupportsSpdy(server, support_spdy); @@ -147,20 +147,19 @@ void HttpServerPropertiesManager::SetSupportsSpdy( } bool HttpServerPropertiesManager::HasAlternateProtocol( - const net::HostPortPair& server) { + const HostPortPair& server) { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); return http_server_properties_impl_->HasAlternateProtocol(server); } -net::AlternateProtocolInfo -HttpServerPropertiesManager::GetAlternateProtocol( - const net::HostPortPair& server) { +AlternateProtocolInfo HttpServerPropertiesManager::GetAlternateProtocol( + const HostPortPair& server) { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); return http_server_properties_impl_->GetAlternateProtocol(server); } void HttpServerPropertiesManager::SetAlternateProtocol( - const net::HostPortPair& server, + const HostPortPair& server, uint16 alternate_port, AlternateProtocol alternate_protocol, double alternate_probability) { @@ -171,34 +170,34 @@ void HttpServerPropertiesManager::SetAlternateProtocol( } void HttpServerPropertiesManager::SetBrokenAlternateProtocol( - const net::HostPortPair& server) { + const HostPortPair& server) { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); http_server_properties_impl_->SetBrokenAlternateProtocol(server); ScheduleUpdatePrefsOnNetworkThread(); } bool HttpServerPropertiesManager::WasAlternateProtocolRecentlyBroken( - const net::HostPortPair& server) { + const HostPortPair& server) { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); return http_server_properties_impl_->WasAlternateProtocolRecentlyBroken( server); } void HttpServerPropertiesManager::ConfirmAlternateProtocol( - const net::HostPortPair& server) { + const HostPortPair& server) { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); http_server_properties_impl_->ConfirmAlternateProtocol(server); ScheduleUpdatePrefsOnNetworkThread(); } void HttpServerPropertiesManager::ClearAlternateProtocol( - const net::HostPortPair& server) { + const HostPortPair& server) { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); http_server_properties_impl_->ClearAlternateProtocol(server); ScheduleUpdatePrefsOnNetworkThread(); } -const net::AlternateProtocolMap& +const AlternateProtocolMap& HttpServerPropertiesManager::alternate_protocol_map() const { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); return http_server_properties_impl_->alternate_protocol_map(); @@ -249,15 +248,14 @@ const SpdySettingsMap& HttpServerPropertiesManager::spdy_settings_map() return http_server_properties_impl_->spdy_settings_map(); } -net::SupportsQuic -HttpServerPropertiesManager::GetSupportsQuic( - const net::HostPortPair& host_port_pair) const { +SupportsQuic HttpServerPropertiesManager::GetSupportsQuic( + const HostPortPair& host_port_pair) const { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); return http_server_properties_impl_->GetSupportsQuic(host_port_pair); } void HttpServerPropertiesManager::SetSupportsQuic( - const net::HostPortPair& host_port_pair, + const HostPortPair& host_port_pair, bool used_quic, const std::string& address) { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); @@ -273,19 +271,25 @@ const SupportsQuicMap& HttpServerPropertiesManager::supports_quic_map() } void HttpServerPropertiesManager::SetServerNetworkStats( - const net::HostPortPair& host_port_pair, - NetworkStats stats) { + const HostPortPair& host_port_pair, + ServerNetworkStats stats) { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); http_server_properties_impl_->SetServerNetworkStats(host_port_pair, stats); + ScheduleUpdatePrefsOnNetworkThread(); } -const HttpServerPropertiesManager::NetworkStats* -HttpServerPropertiesManager::GetServerNetworkStats( - const net::HostPortPair& host_port_pair) const { +const ServerNetworkStats* HttpServerPropertiesManager::GetServerNetworkStats( + const HostPortPair& host_port_pair) { DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); return http_server_properties_impl_->GetServerNetworkStats(host_port_pair); } +const ServerNetworkStatsMap& +HttpServerPropertiesManager::server_network_stats_map() const { + DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); + return http_server_properties_impl_->server_network_stats_map(); +} + // // Update the HttpServerPropertiesImpl's cache with data from preferences. // @@ -336,18 +340,19 @@ void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { // String is host/port pair of spdy server. scoped_ptr spdy_servers(new StringVector); - scoped_ptr spdy_settings_map( - new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist)); - scoped_ptr alternate_protocol_map( - new net::AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist)); - scoped_ptr supports_quic_map( - new net::SupportsQuicMap()); + scoped_ptr spdy_settings_map( + new SpdySettingsMap(kMaxSpdySettingsHostsToPersist)); + scoped_ptr alternate_protocol_map( + new AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist)); + scoped_ptr supports_quic_map(new SupportsQuicMap()); + scoped_ptr server_network_stats_map( + new ServerNetworkStatsMap(kMaxServerNetworkStatsHostsToPersist)); for (base::DictionaryValue::Iterator it(*servers_dict); !it.IsAtEnd(); it.Advance()) { // Get server's host/pair. const std::string& server_str = it.key(); - net::HostPortPair server = net::HostPortPair::FromString(server_str); + HostPortPair server = HostPortPair::FromString(server_str); if (server.host().empty()) { DVLOG(1) << "Malformed http_server_properties for server: " << server_str; detected_corrupted_prefs = true; @@ -373,7 +378,7 @@ void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { const base::DictionaryValue* spdy_settings_dict = NULL; if (server_pref_dict->GetDictionaryWithoutPathExpansion( "settings", &spdy_settings_dict)) { - net::SettingsMap settings_map; + SettingsMap settings_map; for (base::DictionaryValue::Iterator dict_it(*spdy_settings_dict); !dict_it.IsAtEnd(); dict_it.Advance()) { @@ -391,9 +396,8 @@ void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { NOTREACHED(); continue; } - net::SettingsFlagsAndValue flags_and_value(net::SETTINGS_FLAG_PERSISTED, - value); - settings_map[static_cast(id)] = flags_and_value; + SettingsFlagsAndValue flags_and_value(SETTINGS_FLAG_PERSISTED, value); + settings_map[static_cast(id)] = flags_and_value; } spdy_settings_map->Put(server, settings_map); } @@ -419,9 +423,8 @@ void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { detected_corrupted_prefs = true; continue; } - net::AlternateProtocol protocol = - net::AlternateProtocolFromString(protocol_str); - if (!net::IsAlternateProtocolValid(protocol)) { + AlternateProtocol protocol = AlternateProtocolFromString(protocol_str); + if (!IsAlternateProtocolValid(protocol)) { DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; detected_corrupted_prefs = true; continue; @@ -436,8 +439,8 @@ void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { continue; } - net::AlternateProtocolInfo port_alternate_protocol( - static_cast(port), protocol, probability); + AlternateProtocolInfo port_alternate_protocol(static_cast(port), + protocol, probability); alternate_protocol_map->Put(server, port_alternate_protocol); } @@ -460,28 +463,49 @@ void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { detected_corrupted_prefs = true; continue; } - net::SupportsQuic supports_quic(used_quic, address); + SupportsQuic supports_quic(used_quic, address); supports_quic_map->insert(std::make_pair(server, supports_quic)); } + + // Get ServerNetworkStats. + DCHECK(server_network_stats_map->Peek(server) == + server_network_stats_map->end()); + const base::DictionaryValue* server_network_stats_dict = NULL; + if (server_pref_dict->GetDictionaryWithoutPathExpansion( + "network_stats", &server_network_stats_dict)) { + int srtt; + if (!server_network_stats_dict->GetIntegerWithoutPathExpansion("srtt", + &srtt)) { + DVLOG(1) << "Malformed ServerNetworkStats for server: " << server_str; + detected_corrupted_prefs = true; + continue; + } + ServerNetworkStats server_network_stats; + server_network_stats.srtt = base::TimeDelta::FromInternalValue(srtt); + // TODO(rtenneti): When QUIC starts using bandwidth_estimate, then persist + // bandwidth_estimate. + server_network_stats_map->Put(server, server_network_stats); + } } network_task_runner_->PostTask( FROM_HERE, base::Bind( &HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread, - base::Unretained(this), - base::Owned(spdy_servers.release()), + base::Unretained(this), base::Owned(spdy_servers.release()), base::Owned(spdy_settings_map.release()), base::Owned(alternate_protocol_map.release()), base::Owned(supports_quic_map.release()), + base::Owned(server_network_stats_map.release()), detected_corrupted_prefs)); } void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread( StringVector* spdy_servers, - net::SpdySettingsMap* spdy_settings_map, - net::AlternateProtocolMap* alternate_protocol_map, - net::SupportsQuicMap* supports_quic_map, + SpdySettingsMap* spdy_settings_map, + AlternateProtocolMap* alternate_protocol_map, + SupportsQuicMap* supports_quic_map, + ServerNetworkStatsMap* server_network_stats_map, bool detected_corrupted_prefs) { // Preferences have the master data because admins might have pushed new // preferences. Update the cached data with new data from preferences. @@ -504,6 +528,9 @@ void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread( http_server_properties_impl_->InitializeSupportsQuic(supports_quic_map); + http_server_properties_impl_->InitializeServerNetworkStats( + server_network_stats_map); + // Update the prefs with what we have read (delete all corrupted prefs). if (detected_corrupted_prefs) ScheduleUpdatePrefsOnNetworkThread(); @@ -544,28 +571,27 @@ void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread( http_server_properties_impl_->GetSpdyServerList( spdy_server_list, kMaxSupportsSpdyServerHostsToPersist); - net::SpdySettingsMap* spdy_settings_map = - new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist); - const net::SpdySettingsMap& main_map = + SpdySettingsMap* spdy_settings_map = + new SpdySettingsMap(kMaxSpdySettingsHostsToPersist); + const SpdySettingsMap& main_map = http_server_properties_impl_->spdy_settings_map(); int count = 0; - for (net::SpdySettingsMap::const_iterator it = main_map.begin(); + for (SpdySettingsMap::const_iterator it = main_map.begin(); it != main_map.end() && count < kMaxSpdySettingsHostsToPersist; ++it, ++count) { spdy_settings_map->Put(it->first, it->second); } - net::AlternateProtocolMap* alternate_protocol_map = - new net::AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist); - const net::AlternateProtocolMap& map = + AlternateProtocolMap* alternate_protocol_map = + new AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist); + const AlternateProtocolMap& map = http_server_properties_impl_->alternate_protocol_map(); count = 0; typedef std::map CanonicalHostPersistedMap; CanonicalHostPersistedMap persisted_map; - for (net::AlternateProtocolMap::const_iterator it = map.begin(); - it != map.end() && count < kMaxAlternateProtocolHostsToPersist; - ++it) { - const net::HostPortPair& server = it->first; + for (AlternateProtocolMap::const_iterator it = map.begin(); + it != map.end() && count < kMaxAlternateProtocolHostsToPersist; ++it) { + const HostPortPair& server = it->first; std::string canonical_suffix = http_server_properties_impl_->GetCanonicalSuffix(server); if (!canonical_suffix.empty()) { @@ -577,55 +603,69 @@ void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread( ++count; } - net::SupportsQuicMap* supports_quic_map = new net::SupportsQuicMap(); - const net::SupportsQuicMap& main_supports_quic_map = + SupportsQuicMap* supports_quic_map = new SupportsQuicMap(); + const SupportsQuicMap& main_supports_quic_map = http_server_properties_impl_->supports_quic_map(); - for (net::SupportsQuicMap::const_iterator it = main_supports_quic_map.begin(); + for (SupportsQuicMap::const_iterator it = main_supports_quic_map.begin(); it != main_supports_quic_map.end(); ++it) { supports_quic_map->insert(std::make_pair(it->first, it->second)); } + ServerNetworkStatsMap* server_network_stats_map = + new ServerNetworkStatsMap(kMaxServerNetworkStatsHostsToPersist); + const ServerNetworkStatsMap& main_server_network_stats_map = + http_server_properties_impl_->server_network_stats_map(); + for (ServerNetworkStatsMap::const_iterator it = + main_server_network_stats_map.begin(); + it != main_server_network_stats_map.end(); ++it) { + server_network_stats_map->Put(it->first, it->second); + } + // Update the preferences on the pref thread. pref_task_runner_->PostTask( FROM_HERE, - base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnPrefThread, - pref_weak_ptr_, - base::Owned(spdy_server_list), - base::Owned(spdy_settings_map), - base::Owned(alternate_protocol_map), - base::Owned(supports_quic_map), - completion)); + base::Bind( + &HttpServerPropertiesManager::UpdatePrefsOnPrefThread, pref_weak_ptr_, + base::Owned(spdy_server_list), base::Owned(spdy_settings_map), + base::Owned(alternate_protocol_map), base::Owned(supports_quic_map), + base::Owned(server_network_stats_map), completion)); } // A local or temporary data structure to hold |supports_spdy|, SpdySettings, // AlternateProtocolInfo and SupportsQuic preferences for a server. This is used // only in UpdatePrefsOnPrefThread. struct ServerPref { - ServerPref() : supports_spdy(false), - settings_map(NULL), - alternate_protocol(NULL), - supports_quic(NULL) {} + ServerPref() + : supports_spdy(false), + settings_map(NULL), + alternate_protocol(NULL), + supports_quic(NULL), + server_network_stats(NULL) {} ServerPref(bool supports_spdy, - const net::SettingsMap* settings_map, - const net::AlternateProtocolInfo* alternate_protocol, - const net::SupportsQuic* supports_quic) + const SettingsMap* settings_map, + const AlternateProtocolInfo* alternate_protocol, + const SupportsQuic* supports_quic, + const ServerNetworkStats* server_network_stats) : supports_spdy(supports_spdy), settings_map(settings_map), alternate_protocol(alternate_protocol), - supports_quic(supports_quic) {} + supports_quic(supports_quic), + server_network_stats(server_network_stats) {} bool supports_spdy; - const net::SettingsMap* settings_map; - const net::AlternateProtocolInfo* alternate_protocol; - const net::SupportsQuic* supports_quic; + const SettingsMap* settings_map; + const AlternateProtocolInfo* alternate_protocol; + const SupportsQuic* supports_quic; + const ServerNetworkStats* server_network_stats; }; void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( base::ListValue* spdy_server_list, - net::SpdySettingsMap* spdy_settings_map, - net::AlternateProtocolMap* alternate_protocol_map, - net::SupportsQuicMap* supports_quic_map, + SpdySettingsMap* spdy_settings_map, + AlternateProtocolMap* alternate_protocol_map, + SupportsQuicMap* supports_quic_map, + ServerNetworkStatsMap* server_network_stats_map, const base::Closure& completion) { - typedef std::map ServerPrefMap; + typedef std::map ServerPrefMap; ServerPrefMap server_pref_map; DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); @@ -636,11 +676,11 @@ void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( list_it != spdy_server_list->end(); ++list_it) { if ((*list_it)->GetAsString(&s)) { - net::HostPortPair server = net::HostPortPair::FromString(s); + HostPortPair server = HostPortPair::FromString(s); ServerPrefMap::iterator it = server_pref_map.find(server); if (it == server_pref_map.end()) { - ServerPref server_pref(true, NULL, NULL, NULL); + ServerPref server_pref(true, NULL, NULL, NULL, NULL); server_pref_map[server] = server_pref; } else { it->second.supports_spdy = true; @@ -649,14 +689,13 @@ void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( } // Add servers that have SpdySettings to server_pref_map. - for (net::SpdySettingsMap::iterator map_it = spdy_settings_map->begin(); - map_it != spdy_settings_map->end(); - ++map_it) { - const net::HostPortPair& server = map_it->first; + for (SpdySettingsMap::iterator map_it = spdy_settings_map->begin(); + map_it != spdy_settings_map->end(); ++map_it) { + const HostPortPair& server = map_it->first; ServerPrefMap::iterator it = server_pref_map.find(server); if (it == server_pref_map.end()) { - ServerPref server_pref(false, &map_it->second, NULL, NULL); + ServerPref server_pref(false, &map_it->second, NULL, NULL, NULL); server_pref_map[server] = server_pref; } else { it->second.settings_map = &map_it->second; @@ -664,20 +703,18 @@ void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( } // Add AlternateProtocol servers to server_pref_map. - for (net::AlternateProtocolMap::const_iterator map_it = + for (AlternateProtocolMap::const_iterator map_it = alternate_protocol_map->begin(); - map_it != alternate_protocol_map->end(); - ++map_it) { - const net::HostPortPair& server = map_it->first; - const net::AlternateProtocolInfo& port_alternate_protocol = - map_it->second; - if (!net::IsAlternateProtocolValid(port_alternate_protocol.protocol)) { + map_it != alternate_protocol_map->end(); ++map_it) { + const HostPortPair& server = map_it->first; + const AlternateProtocolInfo& port_alternate_protocol = map_it->second; + if (!IsAlternateProtocolValid(port_alternate_protocol.protocol)) { continue; } ServerPrefMap::iterator it = server_pref_map.find(server); if (it == server_pref_map.end()) { - ServerPref server_pref(false, NULL, &map_it->second, NULL); + ServerPref server_pref(false, NULL, &map_it->second, NULL, NULL); server_pref_map[server] = server_pref; } else { it->second.alternate_protocol = &map_it->second; @@ -685,26 +722,41 @@ void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( } // Add SupportsQuic servers to server_pref_map. - for (net::SupportsQuicMap::const_iterator map_it = supports_quic_map->begin(); + for (SupportsQuicMap::const_iterator map_it = supports_quic_map->begin(); map_it != supports_quic_map->end(); ++map_it) { - const net::HostPortPair& server = map_it->first; + const HostPortPair& server = map_it->first; ServerPrefMap::iterator it = server_pref_map.find(server); if (it == server_pref_map.end()) { - ServerPref server_pref(false, NULL, NULL, &map_it->second); + ServerPref server_pref(false, NULL, NULL, &map_it->second, NULL); server_pref_map[server] = server_pref; } else { it->second.supports_quic = &map_it->second; } } + // Add ServerNetworkStats servers to server_pref_map. + for (ServerNetworkStatsMap::const_iterator map_it = + server_network_stats_map->begin(); + map_it != server_network_stats_map->end(); ++map_it) { + const HostPortPair& server = map_it->first; + + ServerPrefMap::iterator it = server_pref_map.find(server); + if (it == server_pref_map.end()) { + ServerPref server_pref(false, NULL, NULL, NULL, &map_it->second); + server_pref_map[server] = server_pref; + } else { + it->second.server_network_stats = &map_it->second; + } + } + // Persist properties to the |path_|. base::DictionaryValue http_server_properties_dict; base::DictionaryValue* servers_dict = new base::DictionaryValue; for (ServerPrefMap::const_iterator map_it = server_pref_map.begin(); map_it != server_pref_map.end(); ++map_it) { - const net::HostPortPair& server = map_it->first; + const HostPortPair& server = map_it->first; const ServerPref& server_pref = map_it->second; base::DictionaryValue* server_pref_dict = new base::DictionaryValue; @@ -716,11 +768,9 @@ void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( // Save SPDY settings. if (server_pref.settings_map) { base::DictionaryValue* spdy_settings_dict = new base::DictionaryValue; - for (net::SettingsMap::const_iterator it = - server_pref.settings_map->begin(); - it != server_pref.settings_map->end(); - ++it) { - net::SpdySettingsIds id = it->first; + for (SettingsMap::const_iterator it = server_pref.settings_map->begin(); + it != server_pref.settings_map->end(); ++it) { + SpdySettingsIds id = it->first; uint32 value = it->second.second; std::string key = base::StringPrintf("%u", id); spdy_settings_dict->SetInteger(key, value); @@ -729,7 +779,7 @@ void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( } // Save alternate_protocol. - const net::AlternateProtocolInfo* port_alternate_protocol = + const AlternateProtocolInfo* port_alternate_protocol = server_pref.alternate_protocol; if (port_alternate_protocol && !port_alternate_protocol->is_broken) { base::DictionaryValue* port_alternate_protocol_dict = @@ -737,7 +787,7 @@ void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( port_alternate_protocol_dict->SetInteger("port", port_alternate_protocol->port); const char* protocol_str = - net::AlternateProtocolToString(port_alternate_protocol->protocol); + AlternateProtocolToString(port_alternate_protocol->protocol); port_alternate_protocol_dict->SetString("protocol_str", protocol_str); port_alternate_protocol_dict->SetDouble( "probability", port_alternate_protocol->probability); @@ -748,13 +798,29 @@ void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( // Save supports_quic. if (server_pref.supports_quic) { base::DictionaryValue* supports_quic_dict = new base::DictionaryValue; - const net::SupportsQuic* supports_quic = server_pref.supports_quic; + const SupportsQuic* supports_quic = server_pref.supports_quic; supports_quic_dict->SetBoolean("used_quic", supports_quic->used_quic); supports_quic_dict->SetString("address", supports_quic->address); server_pref_dict->SetWithoutPathExpansion( "supports_quic", supports_quic_dict); } + // Save ServerNetworkStats. + if (server_pref.server_network_stats) { + base::DictionaryValue* server_network_stats_dict = + new base::DictionaryValue; + const ServerNetworkStats* server_network_stats = + server_pref.server_network_stats; + // Becasue JSON doesn't support int64, persist int64 as a string. + server_network_stats_dict->SetInteger( + "srtt", + static_cast(server_network_stats->srtt.ToInternalValue())); + // TODO(rtenneti): When QUIC starts using bandwidth_estimate, then persist + // bandwidth_estimate. + server_pref_dict->SetWithoutPathExpansion("network_stats", + server_network_stats_dict); + } + servers_dict->SetWithoutPathExpansion(server.ToString(), server_pref_dict); } diff --git a/net/http/http_server_properties_manager.h b/net/http/http_server_properties_manager.h index 22e2499..4a8c71b 100644 --- a/net/http/http_server_properties_manager.h +++ b/net/http/http_server_properties_manager.h @@ -155,10 +155,12 @@ class NET_EXPORT HttpServerPropertiesManager : public HttpServerProperties { const SupportsQuicMap& supports_quic_map() const override; void SetServerNetworkStats(const HostPortPair& host_port_pair, - NetworkStats stats) override; + ServerNetworkStats stats) override; - const NetworkStats* GetServerNetworkStats( - const HostPortPair& host_port_pair) const override; + const ServerNetworkStats* GetServerNetworkStats( + const HostPortPair& host_port_pair) override; + + const ServerNetworkStatsMap& server_network_stats_map() const override; protected: // -------------------- @@ -186,6 +188,7 @@ class NET_EXPORT HttpServerPropertiesManager : public HttpServerProperties { SpdySettingsMap* spdy_settings_map, AlternateProtocolMap* alternate_protocol_map, SupportsQuicMap* supports_quic_map, + ServerNetworkStatsMap* server_network_stats_map, bool detected_corrupted_prefs); // These are used to delay updating the preferences when cached data in @@ -214,6 +217,7 @@ class NET_EXPORT HttpServerPropertiesManager : public HttpServerProperties { SpdySettingsMap* spdy_settings_map, AlternateProtocolMap* alternate_protocol_map, SupportsQuicMap* supports_quic_map, + ServerNetworkStatsMap* server_network_stats_map, const base::Closure& completion); private: diff --git a/net/http/http_server_properties_manager_unittest.cc b/net/http/http_server_properties_manager_unittest.cc index 1d62296..99647b3f 100644 --- a/net/http/http_server_properties_manager_unittest.cc +++ b/net/http/http_server_properties_manager_unittest.cc @@ -9,6 +9,7 @@ #include "base/prefs/pref_registry_simple.h" #include "base/prefs/testing_pref_service.h" #include "base/run_loop.h" +#include "base/strings/string_number_conversions.h" #include "base/strings/stringprintf.h" #include "base/test/test_simple_task_runner.h" #include "base/values.h" @@ -69,17 +70,19 @@ class TestingHttpServerPropertiesManager : public HttpServerPropertiesManager { MOCK_METHOD0(UpdateCacheFromPrefsOnPrefThread, void()); MOCK_METHOD1(UpdatePrefsFromCacheOnNetworkThread, void(const base::Closure&)); - MOCK_METHOD5(UpdateCacheFromPrefsOnNetworkThread, + MOCK_METHOD6(UpdateCacheFromPrefsOnNetworkThread, void(std::vector* spdy_servers, - net::SpdySettingsMap* spdy_settings_map, - net::AlternateProtocolMap* alternate_protocol_map, - net::SupportsQuicMap* supports_quic_map, + SpdySettingsMap* spdy_settings_map, + AlternateProtocolMap* alternate_protocol_map, + SupportsQuicMap* supports_quic_map, + ServerNetworkStatsMap* server_network_stats_map, bool detected_corrupted_prefs)); - MOCK_METHOD4(UpdatePrefsOnPref, + MOCK_METHOD5(UpdatePrefsOnPref, void(base::ListValue* spdy_server_list, - net::SpdySettingsMap* spdy_settings_map, - net::AlternateProtocolMap* alternate_protocol_map, - net::SupportsQuicMap* supports_quic_map)); + SpdySettingsMap* spdy_settings_map, + AlternateProtocolMap* alternate_protocol_map, + SupportsQuicMap* supports_quic_map, + ServerNetworkStatsMap* server_network_stats_map)); private: DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager); @@ -147,6 +150,8 @@ TEST_F(HttpServerPropertiesManagerTest, // it twice. Only expect a single cache update. base::DictionaryValue* server_pref_dict = new base::DictionaryValue; + HostPortPair google_server("www.google.com", 80); + HostPortPair mail_server("mail.google.com", 80); // Set supports_spdy for www.google.com:80. server_pref_dict->SetBoolean("supports_spdy", true); @@ -164,6 +169,11 @@ TEST_F(HttpServerPropertiesManagerTest, supports_quic->SetString("address", "foo"); server_pref_dict->SetWithoutPathExpansion("supports_quic", supports_quic); + // Set up ServerNetworkStats for www.google.com:80. + base::DictionaryValue* stats = new base::DictionaryValue; + stats->SetInteger("srtt", 10); + server_pref_dict->SetWithoutPathExpansion("network_stats", stats); + // Set the server preference for www.google.com:80. base::DictionaryValue* servers_dict = new base::DictionaryValue; servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict); @@ -188,6 +198,10 @@ TEST_F(HttpServerPropertiesManagerTest, supports_quic1->SetString("address", "bar"); server_pref_dict1->SetWithoutPathExpansion("supports_quic", supports_quic1); + // Set up ServerNetworkStats for mail.google.com:80. + base::DictionaryValue* stats1 = new base::DictionaryValue; + stats1->SetInteger("srtt", 20); + server_pref_dict1->SetWithoutPathExpansion("network_stats", stats1); // Set the server preference for mail.google.com:80. servers_dict->SetWithoutPathExpansion("mail.google.com:80", server_pref_dict1); @@ -209,38 +223,39 @@ TEST_F(HttpServerPropertiesManagerTest, Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); // Verify SupportsSpdy. - EXPECT_TRUE(http_server_props_manager_->SupportsSpdy( - net::HostPortPair::FromString("www.google.com:80"))); - EXPECT_TRUE(http_server_props_manager_->SupportsSpdy( - net::HostPortPair::FromString("mail.google.com:80"))); + EXPECT_TRUE(http_server_props_manager_->SupportsSpdy(google_server)); + EXPECT_TRUE(http_server_props_manager_->SupportsSpdy(mail_server)); EXPECT_FALSE(http_server_props_manager_->SupportsSpdy( - net::HostPortPair::FromString("foo.google.com:1337"))); + HostPortPair::FromString("foo.google.com:1337"))); // Verify AlternateProtocol. - ASSERT_TRUE(http_server_props_manager_->HasAlternateProtocol( - net::HostPortPair::FromString("www.google.com:80"))); - ASSERT_TRUE(http_server_props_manager_->HasAlternateProtocol( - net::HostPortPair::FromString("mail.google.com:80"))); - net::AlternateProtocolInfo port_alternate_protocol = - http_server_props_manager_->GetAlternateProtocol( - net::HostPortPair::FromString("www.google.com:80")); + ASSERT_TRUE(http_server_props_manager_->HasAlternateProtocol(google_server)); + ASSERT_TRUE(http_server_props_manager_->HasAlternateProtocol(mail_server)); + AlternateProtocolInfo port_alternate_protocol = + http_server_props_manager_->GetAlternateProtocol(google_server); EXPECT_EQ(443, port_alternate_protocol.port); - EXPECT_EQ(net::NPN_SPDY_3, port_alternate_protocol.protocol); - port_alternate_protocol = http_server_props_manager_->GetAlternateProtocol( - net::HostPortPair::FromString("mail.google.com:80")); + EXPECT_EQ(NPN_SPDY_3, port_alternate_protocol.protocol); + port_alternate_protocol = + http_server_props_manager_->GetAlternateProtocol(mail_server); EXPECT_EQ(444, port_alternate_protocol.port); - EXPECT_EQ(net::NPN_SPDY_3_1, port_alternate_protocol.protocol); + EXPECT_EQ(NPN_SPDY_3_1, port_alternate_protocol.protocol); // Verify SupportsQuic. - net::SupportsQuic supports_quic2 = - http_server_props_manager_->GetSupportsQuic( - net::HostPortPair::FromString("www.google.com:80")); + SupportsQuic supports_quic2 = + http_server_props_manager_->GetSupportsQuic(google_server); EXPECT_TRUE(supports_quic2.used_quic); EXPECT_EQ("foo", supports_quic2.address); - supports_quic2 = http_server_props_manager_->GetSupportsQuic( - net::HostPortPair::FromString("mail.google.com:80")); + supports_quic2 = http_server_props_manager_->GetSupportsQuic(mail_server); EXPECT_FALSE(supports_quic2.used_quic); EXPECT_EQ("bar", supports_quic2.address); + + // Verify ServerNetworkStats. + const ServerNetworkStats* stats2 = + http_server_props_manager_->GetServerNetworkStats(google_server); + EXPECT_EQ(10, stats2->srtt.ToInternalValue()); + const ServerNetworkStats* stats3 = + http_server_props_manager_->GetServerNetworkStats(mail_server); + EXPECT_EQ(20, stats3->srtt.ToInternalValue()); } TEST_F(HttpServerPropertiesManagerTest, BadCachedHostPortPair) { @@ -266,6 +281,11 @@ TEST_F(HttpServerPropertiesManagerTest, BadCachedHostPortPair) { supports_quic->SetString("address", "foo"); server_pref_dict->SetWithoutPathExpansion("supports_quic", supports_quic); + // Set up ServerNetworkStats for www.google.com:65536. + base::DictionaryValue* stats = new base::DictionaryValue; + stats->SetInteger("srtt", 10); + server_pref_dict->SetWithoutPathExpansion("network_stats", stats); + // Set the server preference for www.google.com:65536. base::DictionaryValue* servers_dict = new base::DictionaryValue; servers_dict->SetWithoutPathExpansion("www.google.com:65536", @@ -285,13 +305,16 @@ TEST_F(HttpServerPropertiesManagerTest, BadCachedHostPortPair) { // Verify that nothing is set. EXPECT_FALSE(http_server_props_manager_->SupportsSpdy( - net::HostPortPair::FromString("www.google.com:65536"))); + HostPortPair::FromString("www.google.com:65536"))); EXPECT_FALSE(http_server_props_manager_->HasAlternateProtocol( - net::HostPortPair::FromString("www.google.com:65536"))); - net::SupportsQuic supports_quic2 = - http_server_props_manager_->GetSupportsQuic( - net::HostPortPair::FromString("www.google.com:65536")); + HostPortPair::FromString("www.google.com:65536"))); + SupportsQuic supports_quic2 = http_server_props_manager_->GetSupportsQuic( + HostPortPair::FromString("www.google.com:65536")); EXPECT_FALSE(supports_quic2.used_quic); + const ServerNetworkStats* stats1 = + http_server_props_manager_->GetServerNetworkStats( + HostPortPair::FromString("www.google.com:65536")); + EXPECT_EQ(NULL, stats1); } TEST_F(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) { @@ -329,7 +352,7 @@ TEST_F(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) { // Verify AlternateProtocol is not set. EXPECT_FALSE(http_server_props_manager_->HasAlternateProtocol( - net::HostPortPair::FromString("www.google.com:80"))); + HostPortPair::FromString("www.google.com:80"))); } TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) { @@ -339,7 +362,7 @@ TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) { // ScheduleUpdatePrefsOnNetworkThread. // Add mail.google.com:443 as a supporting spdy server. - net::HostPortPair spdy_server_mail("mail.google.com", 443); + HostPortPair spdy_server_mail("mail.google.com", 443); EXPECT_FALSE(http_server_props_manager_->SupportsSpdy(spdy_server_mail)); http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true); @@ -354,9 +377,9 @@ TEST_F(HttpServerPropertiesManagerTest, SetSpdySetting) { ExpectPrefsUpdate(); // Add SpdySetting for mail.google.com:443. - net::HostPortPair spdy_server_mail("mail.google.com", 443); - const net::SpdySettingsIds id1 = net::SETTINGS_UPLOAD_BANDWIDTH; - const net::SpdySettingsFlags flags1 = net::SETTINGS_FLAG_PLEASE_PERSIST; + HostPortPair spdy_server_mail("mail.google.com", 443); + const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; + const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST; const uint32 value1 = 31337; http_server_props_manager_->SetSpdySetting( spdy_server_mail, id1, flags1, value1); @@ -364,13 +387,13 @@ TEST_F(HttpServerPropertiesManagerTest, SetSpdySetting) { // Run the task. base::RunLoop().RunUntilIdle(); - const net::SettingsMap& settings_map1_ret = + const SettingsMap& settings_map1_ret = http_server_props_manager_->GetSpdySettings(spdy_server_mail); ASSERT_EQ(1U, settings_map1_ret.size()); - net::SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); + SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); EXPECT_TRUE(it1_ret != settings_map1_ret.end()); - net::SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; - EXPECT_EQ(net::SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); + SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; + EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); EXPECT_EQ(value1, flags_and_value1_ret.second); Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); @@ -380,9 +403,9 @@ TEST_F(HttpServerPropertiesManagerTest, ClearSpdySetting) { ExpectPrefsUpdateRepeatedly(); // Add SpdySetting for mail.google.com:443. - net::HostPortPair spdy_server_mail("mail.google.com", 443); - const net::SpdySettingsIds id1 = net::SETTINGS_UPLOAD_BANDWIDTH; - const net::SpdySettingsFlags flags1 = net::SETTINGS_FLAG_PLEASE_PERSIST; + HostPortPair spdy_server_mail("mail.google.com", 443); + const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; + const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST; const uint32 value1 = 31337; http_server_props_manager_->SetSpdySetting( spdy_server_mail, id1, flags1, value1); @@ -390,13 +413,13 @@ TEST_F(HttpServerPropertiesManagerTest, ClearSpdySetting) { // Run the task. base::RunLoop().RunUntilIdle(); - const net::SettingsMap& settings_map1_ret = + const SettingsMap& settings_map1_ret = http_server_props_manager_->GetSpdySettings(spdy_server_mail); ASSERT_EQ(1U, settings_map1_ret.size()); - net::SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); + SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); EXPECT_TRUE(it1_ret != settings_map1_ret.end()); - net::SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; - EXPECT_EQ(net::SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); + SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; + EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); EXPECT_EQ(value1, flags_and_value1_ret.second); // Clear SpdySetting for mail.google.com:443. @@ -407,7 +430,7 @@ TEST_F(HttpServerPropertiesManagerTest, ClearSpdySetting) { // Verify that there are no entries in the settings map for // mail.google.com:443. - const net::SettingsMap& settings_map2_ret = + const SettingsMap& settings_map2_ret = http_server_props_manager_->GetSpdySettings(spdy_server_mail); ASSERT_EQ(0U, settings_map2_ret.size()); @@ -418,9 +441,9 @@ TEST_F(HttpServerPropertiesManagerTest, ClearAllSpdySetting) { ExpectPrefsUpdateRepeatedly(); // Add SpdySetting for mail.google.com:443. - net::HostPortPair spdy_server_mail("mail.google.com", 443); - const net::SpdySettingsIds id1 = net::SETTINGS_UPLOAD_BANDWIDTH; - const net::SpdySettingsFlags flags1 = net::SETTINGS_FLAG_PLEASE_PERSIST; + HostPortPair spdy_server_mail("mail.google.com", 443); + const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; + const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST; const uint32 value1 = 31337; http_server_props_manager_->SetSpdySetting( spdy_server_mail, id1, flags1, value1); @@ -428,13 +451,13 @@ TEST_F(HttpServerPropertiesManagerTest, ClearAllSpdySetting) { // Run the task. base::RunLoop().RunUntilIdle(); - const net::SettingsMap& settings_map1_ret = + const SettingsMap& settings_map1_ret = http_server_props_manager_->GetSpdySettings(spdy_server_mail); ASSERT_EQ(1U, settings_map1_ret.size()); - net::SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); + SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); EXPECT_TRUE(it1_ret != settings_map1_ret.end()); - net::SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; - EXPECT_EQ(net::SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); + SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; + EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); EXPECT_EQ(value1, flags_and_value1_ret.second); // Clear All SpdySettings. @@ -444,7 +467,7 @@ TEST_F(HttpServerPropertiesManagerTest, ClearAllSpdySetting) { base::RunLoop().RunUntilIdle(); // Verify that there are no entries in the settings map. - const net::SpdySettingsMap& spdy_settings_map2_ret = + const SpdySettingsMap& spdy_settings_map2_ret = http_server_props_manager_->spdy_settings_map(); ASSERT_EQ(0U, spdy_settings_map2_ret.size()); @@ -454,11 +477,11 @@ TEST_F(HttpServerPropertiesManagerTest, ClearAllSpdySetting) { TEST_F(HttpServerPropertiesManagerTest, HasAlternateProtocol) { ExpectPrefsUpdate(); - net::HostPortPair spdy_server_mail("mail.google.com", 80); + HostPortPair spdy_server_mail("mail.google.com", 80); EXPECT_FALSE( http_server_props_manager_->HasAlternateProtocol(spdy_server_mail)); - http_server_props_manager_->SetAlternateProtocol( - spdy_server_mail, 443, net::NPN_SPDY_3, 1); + http_server_props_manager_->SetAlternateProtocol(spdy_server_mail, 443, + NPN_SPDY_3, 1); // Run the task. base::RunLoop().RunUntilIdle(); @@ -466,18 +489,18 @@ TEST_F(HttpServerPropertiesManagerTest, HasAlternateProtocol) { ASSERT_TRUE( http_server_props_manager_->HasAlternateProtocol(spdy_server_mail)); - net::AlternateProtocolInfo port_alternate_protocol = + AlternateProtocolInfo port_alternate_protocol = http_server_props_manager_->GetAlternateProtocol(spdy_server_mail); EXPECT_EQ(443, port_alternate_protocol.port); - EXPECT_EQ(net::NPN_SPDY_3, port_alternate_protocol.protocol); + EXPECT_EQ(NPN_SPDY_3, port_alternate_protocol.protocol); } TEST_F(HttpServerPropertiesManagerTest, SupportsQuic) { ExpectPrefsUpdate(); - net::HostPortPair quic_server_mail("mail.google.com", 80); - net::SupportsQuic supports_quic = http_server_props_manager_->GetSupportsQuic( - quic_server_mail); + HostPortPair quic_server_mail("mail.google.com", 80); + SupportsQuic supports_quic = + http_server_props_manager_->GetSupportsQuic(quic_server_mail); EXPECT_FALSE(supports_quic.used_quic); EXPECT_EQ("", supports_quic.address); http_server_props_manager_->SetSupportsQuic(quic_server_mail, true, "foo"); @@ -486,23 +509,46 @@ TEST_F(HttpServerPropertiesManagerTest, SupportsQuic) { base::RunLoop().RunUntilIdle(); Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); - net::SupportsQuic supports_quic1 = + SupportsQuic supports_quic1 = http_server_props_manager_->GetSupportsQuic(quic_server_mail); EXPECT_TRUE(supports_quic1.used_quic); EXPECT_EQ("foo", supports_quic1.address); } +TEST_F(HttpServerPropertiesManagerTest, ServerNetworkStats) { + ExpectPrefsUpdate(); + + HostPortPair mail_server("mail.google.com", 80); + const ServerNetworkStats* stats = + http_server_props_manager_->GetServerNetworkStats(mail_server); + EXPECT_EQ(NULL, stats); + ServerNetworkStats stats1; + stats1.srtt = base::TimeDelta::FromMicroseconds(10); + http_server_props_manager_->SetServerNetworkStats(mail_server, stats1); + + // Run the task. + base::RunLoop().RunUntilIdle(); + Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); + + const ServerNetworkStats* stats2 = + http_server_props_manager_->GetServerNetworkStats(mail_server); + EXPECT_EQ(10, stats2->srtt.ToInternalValue()); +} + TEST_F(HttpServerPropertiesManagerTest, Clear) { ExpectPrefsUpdate(); - net::HostPortPair spdy_server_mail("mail.google.com", 443); + HostPortPair spdy_server_mail("mail.google.com", 443); http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true); - http_server_props_manager_->SetAlternateProtocol( - spdy_server_mail, 443, net::NPN_SPDY_3, 1); + http_server_props_manager_->SetAlternateProtocol(spdy_server_mail, 443, + NPN_SPDY_3, 1); http_server_props_manager_->SetSupportsQuic(spdy_server_mail, true, "foo"); + ServerNetworkStats stats; + stats.srtt = base::TimeDelta::FromMicroseconds(10); + http_server_props_manager_->SetServerNetworkStats(spdy_server_mail, stats); - const net::SpdySettingsIds id1 = net::SETTINGS_UPLOAD_BANDWIDTH; - const net::SpdySettingsFlags flags1 = net::SETTINGS_FLAG_PLEASE_PERSIST; + const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; + const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST; const uint32 value1 = 31337; http_server_props_manager_->SetSpdySetting( spdy_server_mail, id1, flags1, value1); @@ -513,19 +559,22 @@ TEST_F(HttpServerPropertiesManagerTest, Clear) { EXPECT_TRUE(http_server_props_manager_->SupportsSpdy(spdy_server_mail)); EXPECT_TRUE( http_server_props_manager_->HasAlternateProtocol(spdy_server_mail)); - net::SupportsQuic supports_quic = http_server_props_manager_->GetSupportsQuic( - spdy_server_mail); + SupportsQuic supports_quic = + http_server_props_manager_->GetSupportsQuic(spdy_server_mail); EXPECT_TRUE(supports_quic.used_quic); EXPECT_EQ("foo", supports_quic.address); + const ServerNetworkStats* stats1 = + http_server_props_manager_->GetServerNetworkStats(spdy_server_mail); + EXPECT_EQ(10, stats1->srtt.ToInternalValue()); // Check SPDY settings values. - const net::SettingsMap& settings_map1_ret = + const SettingsMap& settings_map1_ret = http_server_props_manager_->GetSpdySettings(spdy_server_mail); ASSERT_EQ(1U, settings_map1_ret.size()); - net::SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); + SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); EXPECT_TRUE(it1_ret != settings_map1_ret.end()); - net::SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; - EXPECT_EQ(net::SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); + SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; + EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); EXPECT_EQ(value1, flags_and_value1_ret.second); Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); @@ -539,12 +588,15 @@ TEST_F(HttpServerPropertiesManagerTest, Clear) { EXPECT_FALSE(http_server_props_manager_->SupportsSpdy(spdy_server_mail)); EXPECT_FALSE( http_server_props_manager_->HasAlternateProtocol(spdy_server_mail)); - net::SupportsQuic supports_quic1 = + SupportsQuic supports_quic1 = http_server_props_manager_->GetSupportsQuic(spdy_server_mail); EXPECT_FALSE(supports_quic1.used_quic); EXPECT_EQ("", supports_quic1.address); + const ServerNetworkStats* stats2 = + http_server_props_manager_->GetServerNetworkStats(spdy_server_mail); + EXPECT_EQ(NULL, stats2); - const net::SettingsMap& settings_map2_ret = + const SettingsMap& settings_map2_ret = http_server_props_manager_->GetSpdySettings(spdy_server_mail); EXPECT_EQ(0U, settings_map2_ret.size()); diff --git a/net/quic/quic_stream_factory.cc b/net/quic/quic_stream_factory.cc index 7f137b7..0158bf9 100644 --- a/net/quic/quic_stream_factory.cc +++ b/net/quic/quic_stream_factory.cc @@ -1012,7 +1012,7 @@ int QuicStreamFactory::CreateSession( config.SetInitialStreamFlowControlWindowToSend(kInitialReceiveWindowSize); config.SetInitialSessionFlowControlWindowToSend(kInitialReceiveWindowSize); if (http_server_properties_) { - const HttpServerProperties::NetworkStats* stats = + const ServerNetworkStats* stats = http_server_properties_->GetServerNetworkStats( server_id.host_port_pair()); if (stats != nullptr) { @@ -1123,7 +1123,7 @@ void QuicStreamFactory::ProcessGoingAwaySession( const QuicConnectionStats& stats = session->connection()->GetStats(); if (session->IsCryptoHandshakeConfirmed()) { - HttpServerProperties::NetworkStats network_stats; + ServerNetworkStats network_stats; network_stats.srtt = base::TimeDelta::FromMicroseconds(stats.srtt_us); network_stats.bandwidth_estimate = stats.estimated_bandwidth; http_server_properties_->SetServerNetworkStats(server_id.host_port_pair(), -- cgit v1.1