| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http/http_server_properties_impl.h" | 5 #include "net/http/http_server_properties_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "net/http/http_pipelined_host_capability.h" | 14 #include "net/http/http_pipelined_host_capability.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // TODO(simonjam): Run experiments with different values of this to see what | 20 // TODO(simonjam): Run experiments with different values of this to see what |
| 21 // value is good at avoiding evictions without eating too much memory. Until | 21 // value is good at avoiding evictions without eating too much memory. Until |
| 22 // then, this is just a bad guess. | 22 // then, this is just a bad guess. |
| 23 const int kDefaultNumHostsToRemember = 200; | 23 const int kDefaultNumHostsToRemember = 200; |
| 24 | 24 |
| 25 const uint64 kBrokenAlternateProtocolDelaySecs = 300; | 25 const uint64 kBrokenAlternateProtocolDelaySecs = 300; |
| 26 | 26 |
| 27 } // namespace | 27 } // namespace |
| 28 | 28 |
| 29 HttpServerPropertiesImpl::HttpServerPropertiesImpl() | 29 HttpServerPropertiesImpl::HttpServerPropertiesImpl() |
| 30 : alternate_protocol_map_(AlternateProtocolMap::NO_AUTO_EVICT), | 30 : spdy_servers_map_(SpdyServerHostPortMap::NO_AUTO_EVICT), |
| 31 alternate_protocol_map_(AlternateProtocolMap::NO_AUTO_EVICT), |
| 31 spdy_settings_map_(SpdySettingsMap::NO_AUTO_EVICT), | 32 spdy_settings_map_(SpdySettingsMap::NO_AUTO_EVICT), |
| 32 pipeline_capability_map_( | 33 pipeline_capability_map_( |
| 33 new CachedPipelineCapabilityMap(kDefaultNumHostsToRemember)), | 34 new CachedPipelineCapabilityMap(kDefaultNumHostsToRemember)), |
| 34 weak_ptr_factory_(this) { | 35 weak_ptr_factory_(this) { |
| 35 canoncial_suffixes_.push_back(".c.youtube.com"); | 36 canoncial_suffixes_.push_back(".c.youtube.com"); |
| 36 canoncial_suffixes_.push_back(".googlevideo.com"); | 37 canoncial_suffixes_.push_back(".googlevideo.com"); |
| 37 } | 38 } |
| 38 | 39 |
| 39 HttpServerPropertiesImpl::~HttpServerPropertiesImpl() { | 40 HttpServerPropertiesImpl::~HttpServerPropertiesImpl() { |
| 40 } | 41 } |
| 41 | 42 |
| 42 void HttpServerPropertiesImpl::InitializeSpdyServers( | 43 void HttpServerPropertiesImpl::InitializeSpdyServers( |
| 43 std::vector<std::string>* spdy_servers, | 44 std::vector<std::string>* spdy_servers, |
| 44 bool support_spdy) { | 45 bool support_spdy) { |
| 45 DCHECK(CalledOnValidThread()); | 46 DCHECK(CalledOnValidThread()); |
| 46 spdy_servers_table_.clear(); | |
| 47 if (!spdy_servers) | 47 if (!spdy_servers) |
| 48 return; | 48 return; |
| 49 for (std::vector<std::string>::iterator it = spdy_servers->begin(); | 49 // Add the entries from persisted data. |
| 50 it != spdy_servers->end(); ++it) { | 50 for (std::vector<std::string>::reverse_iterator it = spdy_servers->rbegin(); |
| 51 spdy_servers_table_[*it] = support_spdy; | 51 it != spdy_servers->rend(); ++it) { |
| 52 spdy_servers_map_.Put(*it, support_spdy); |
| 52 } | 53 } |
| 53 } | 54 } |
| 54 | 55 |
| 55 void HttpServerPropertiesImpl::InitializeAlternateProtocolServers( | 56 void HttpServerPropertiesImpl::InitializeAlternateProtocolServers( |
| 56 AlternateProtocolMap* alternate_protocol_map) { | 57 AlternateProtocolMap* alternate_protocol_map) { |
| 57 // Keep all the ALTERNATE_PROTOCOL_BROKEN ones since those don't | 58 // Keep all the ALTERNATE_PROTOCOL_BROKEN ones since those don't |
| 58 // get persisted. | 59 // get persisted. |
| 59 for (AlternateProtocolMap::iterator it = alternate_protocol_map_.begin(); | 60 for (AlternateProtocolMap::iterator it = alternate_protocol_map_.begin(); |
| 60 it != alternate_protocol_map_.end();) { | 61 it != alternate_protocol_map_.end();) { |
| 61 AlternateProtocolMap::iterator old_it = it; | 62 AlternateProtocolMap::iterator old_it = it; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 pipeline_capability_map_->Put(it->first, it->second); | 116 pipeline_capability_map_->Put(it->first, it->second); |
| 116 } | 117 } |
| 117 } | 118 } |
| 118 | 119 |
| 119 void HttpServerPropertiesImpl::SetNumPipelinedHostsToRemember(int max_size) { | 120 void HttpServerPropertiesImpl::SetNumPipelinedHostsToRemember(int max_size) { |
| 120 DCHECK(pipeline_capability_map_->empty()); | 121 DCHECK(pipeline_capability_map_->empty()); |
| 121 pipeline_capability_map_.reset(new CachedPipelineCapabilityMap(max_size)); | 122 pipeline_capability_map_.reset(new CachedPipelineCapabilityMap(max_size)); |
| 122 } | 123 } |
| 123 | 124 |
| 124 void HttpServerPropertiesImpl::GetSpdyServerList( | 125 void HttpServerPropertiesImpl::GetSpdyServerList( |
| 125 base::ListValue* spdy_server_list) const { | 126 base::ListValue* spdy_server_list, |
| 127 size_t max_size) const { |
| 126 DCHECK(CalledOnValidThread()); | 128 DCHECK(CalledOnValidThread()); |
| 127 DCHECK(spdy_server_list); | 129 DCHECK(spdy_server_list); |
| 128 spdy_server_list->Clear(); | 130 spdy_server_list->Clear(); |
| 131 size_t count = 0; |
| 129 // Get the list of servers (host/port) that support SPDY. | 132 // Get the list of servers (host/port) that support SPDY. |
| 130 for (SpdyServerHostPortTable::const_iterator it = spdy_servers_table_.begin(); | 133 for (SpdyServerHostPortMap::const_iterator it = spdy_servers_map_.begin(); |
| 131 it != spdy_servers_table_.end(); ++it) { | 134 it != spdy_servers_map_.end() && count < max_size; ++it) { |
| 132 const std::string spdy_server_host_port = it->first; | 135 const std::string spdy_server_host_port = it->first; |
| 133 if (it->second) | 136 if (it->second) { |
| 134 spdy_server_list->Append(new base::StringValue(spdy_server_host_port)); | 137 spdy_server_list->Append(new base::StringValue(spdy_server_host_port)); |
| 138 ++count; |
| 139 } |
| 135 } | 140 } |
| 136 } | 141 } |
| 137 | 142 |
| 138 // static | 143 // static |
| 139 std::string HttpServerPropertiesImpl::GetFlattenedSpdyServer( | 144 std::string HttpServerPropertiesImpl::GetFlattenedSpdyServer( |
| 140 const net::HostPortPair& host_port_pair) { | 145 const net::HostPortPair& host_port_pair) { |
| 141 std::string spdy_server; | 146 std::string spdy_server; |
| 142 spdy_server.append(host_port_pair.host()); | 147 spdy_server.append(host_port_pair.host()); |
| 143 spdy_server.append(":"); | 148 spdy_server.append(":"); |
| 144 base::StringAppendF(&spdy_server, "%d", host_port_pair.port()); | 149 base::StringAppendF(&spdy_server, "%d", host_port_pair.port()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 161 delete g_forced_alternate_protocol; | 166 delete g_forced_alternate_protocol; |
| 162 g_forced_alternate_protocol = NULL; | 167 g_forced_alternate_protocol = NULL; |
| 163 } | 168 } |
| 164 | 169 |
| 165 base::WeakPtr<HttpServerProperties> HttpServerPropertiesImpl::GetWeakPtr() { | 170 base::WeakPtr<HttpServerProperties> HttpServerPropertiesImpl::GetWeakPtr() { |
| 166 return weak_ptr_factory_.GetWeakPtr(); | 171 return weak_ptr_factory_.GetWeakPtr(); |
| 167 } | 172 } |
| 168 | 173 |
| 169 void HttpServerPropertiesImpl::Clear() { | 174 void HttpServerPropertiesImpl::Clear() { |
| 170 DCHECK(CalledOnValidThread()); | 175 DCHECK(CalledOnValidThread()); |
| 171 spdy_servers_table_.clear(); | 176 spdy_servers_map_.Clear(); |
| 172 alternate_protocol_map_.Clear(); | 177 alternate_protocol_map_.Clear(); |
| 173 spdy_settings_map_.Clear(); | 178 spdy_settings_map_.Clear(); |
| 174 pipeline_capability_map_->Clear(); | 179 pipeline_capability_map_->Clear(); |
| 175 } | 180 } |
| 176 | 181 |
| 177 bool HttpServerPropertiesImpl::SupportsSpdy( | 182 bool HttpServerPropertiesImpl::SupportsSpdy( |
| 178 const net::HostPortPair& host_port_pair) const { | 183 const net::HostPortPair& host_port_pair) { |
| 179 DCHECK(CalledOnValidThread()); | 184 DCHECK(CalledOnValidThread()); |
| 180 if (host_port_pair.host().empty()) | 185 if (host_port_pair.host().empty()) |
| 181 return false; | 186 return false; |
| 182 std::string spdy_server = GetFlattenedSpdyServer(host_port_pair); | 187 std::string spdy_server = GetFlattenedSpdyServer(host_port_pair); |
| 183 | 188 |
| 184 SpdyServerHostPortTable::const_iterator spdy_host_port = | 189 SpdyServerHostPortMap::iterator spdy_host_port = |
| 185 spdy_servers_table_.find(spdy_server); | 190 spdy_servers_map_.Get(spdy_server); |
| 186 if (spdy_host_port != spdy_servers_table_.end()) | 191 if (spdy_host_port != spdy_servers_map_.end()) |
| 187 return spdy_host_port->second; | 192 return spdy_host_port->second; |
| 188 return false; | 193 return false; |
| 189 } | 194 } |
| 190 | 195 |
| 191 void HttpServerPropertiesImpl::SetSupportsSpdy( | 196 void HttpServerPropertiesImpl::SetSupportsSpdy( |
| 192 const net::HostPortPair& host_port_pair, | 197 const net::HostPortPair& host_port_pair, |
| 193 bool support_spdy) { | 198 bool support_spdy) { |
| 194 DCHECK(CalledOnValidThread()); | 199 DCHECK(CalledOnValidThread()); |
| 195 if (host_port_pair.host().empty()) | 200 if (host_port_pair.host().empty()) |
| 196 return; | 201 return; |
| 197 std::string spdy_server = GetFlattenedSpdyServer(host_port_pair); | 202 std::string spdy_server = GetFlattenedSpdyServer(host_port_pair); |
| 198 | 203 |
| 199 SpdyServerHostPortTable::iterator spdy_host_port = | 204 SpdyServerHostPortMap::iterator spdy_host_port = |
| 200 spdy_servers_table_.find(spdy_server); | 205 spdy_servers_map_.Get(spdy_server); |
| 201 if ((spdy_host_port != spdy_servers_table_.end()) && | 206 if ((spdy_host_port != spdy_servers_map_.end()) && |
| 202 (spdy_host_port->second == support_spdy)) { | 207 (spdy_host_port->second == support_spdy)) { |
| 203 return; | 208 return; |
| 204 } | 209 } |
| 205 // Cache the data. | 210 // Cache the data. |
| 206 spdy_servers_table_[spdy_server] = support_spdy; | 211 spdy_servers_map_.Put(spdy_server, support_spdy); |
| 207 } | 212 } |
| 208 | 213 |
| 209 bool HttpServerPropertiesImpl::HasAlternateProtocol( | 214 bool HttpServerPropertiesImpl::HasAlternateProtocol( |
| 210 const HostPortPair& server) { | 215 const HostPortPair& server) { |
| 211 if (alternate_protocol_map_.Get(server) != alternate_protocol_map_.end() || | 216 if (alternate_protocol_map_.Get(server) != alternate_protocol_map_.end() || |
| 212 g_forced_alternate_protocol) | 217 g_forced_alternate_protocol) |
| 213 return true; | 218 return true; |
| 214 | 219 |
| 215 return GetCanonicalHost(server) != canonical_host_to_origin_map_.end(); | 220 return GetCanonicalHost(server) != canonical_host_to_origin_map_.end(); |
| 216 } | 221 } |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 base::TimeDelta delay = when > now ? when - now : base::TimeDelta(); | 479 base::TimeDelta delay = when > now ? when - now : base::TimeDelta(); |
| 475 base::MessageLoop::current()->PostDelayedTask( | 480 base::MessageLoop::current()->PostDelayedTask( |
| 476 FROM_HERE, | 481 FROM_HERE, |
| 477 base::Bind( | 482 base::Bind( |
| 478 &HttpServerPropertiesImpl::ExpireBrokenAlternateProtocolMappings, | 483 &HttpServerPropertiesImpl::ExpireBrokenAlternateProtocolMappings, |
| 479 weak_ptr_factory_.GetWeakPtr()), | 484 weak_ptr_factory_.GetWeakPtr()), |
| 480 delay); | 485 delay); |
| 481 } | 486 } |
| 482 | 487 |
| 483 } // namespace net | 488 } // namespace net |
| OLD | NEW |