| 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 "chrome/browser/net/http_server_properties_manager.h" | 5 #include "chrome/browser/net/http_server_properties_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 // "version" 0 indicates, http_server_properties doesn't have "version" | 37 // "version" 0 indicates, http_server_properties doesn't have "version" |
| 38 // property. | 38 // property. |
| 39 const int kMissingVersion = 0; | 39 const int kMissingVersion = 0; |
| 40 | 40 |
| 41 // The version number of persisted http_server_properties. | 41 // The version number of persisted http_server_properties. |
| 42 const int kVersionNumber = 2; | 42 const int kVersionNumber = 2; |
| 43 | 43 |
| 44 typedef std::vector<std::string> StringVector; | 44 typedef std::vector<std::string> StringVector; |
| 45 | 45 |
| 46 // Persist 200 MRU AlternateProtocolHostPortPairs. | |
| 47 const int kMaxAlternateProtocolHostsToPersist = 200; | |
| 48 | |
| 49 } // namespace | 46 } // namespace |
| 50 | 47 |
| 51 //////////////////////////////////////////////////////////////////////////////// | 48 //////////////////////////////////////////////////////////////////////////////// |
| 52 // HttpServerPropertiesManager | 49 // HttpServerPropertiesManager |
| 53 | 50 |
| 54 HttpServerPropertiesManager::HttpServerPropertiesManager( | 51 HttpServerPropertiesManager::HttpServerPropertiesManager( |
| 55 PrefService* pref_service) | 52 PrefService* pref_service) |
| 56 : pref_service_(pref_service), | 53 : pref_service_(pref_service), |
| 57 setting_prefs_(false) { | 54 setting_prefs_(false) { |
| 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 void HttpServerPropertiesManager::SetSupportsSpdy( | 141 void HttpServerPropertiesManager::SetSupportsSpdy( |
| 145 const net::HostPortPair& server, | 142 const net::HostPortPair& server, |
| 146 bool support_spdy) { | 143 bool support_spdy) { |
| 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 148 | 145 |
| 149 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy); | 146 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy); |
| 150 ScheduleUpdatePrefsOnIO(); | 147 ScheduleUpdatePrefsOnIO(); |
| 151 } | 148 } |
| 152 | 149 |
| 153 bool HttpServerPropertiesManager::HasAlternateProtocol( | 150 bool HttpServerPropertiesManager::HasAlternateProtocol( |
| 154 const net::HostPortPair& server) { | 151 const net::HostPortPair& server) const { |
| 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 156 return http_server_properties_impl_->HasAlternateProtocol(server); | 153 return http_server_properties_impl_->HasAlternateProtocol(server); |
| 157 } | 154 } |
| 158 | 155 |
| 159 net::PortAlternateProtocolPair | 156 net::PortAlternateProtocolPair |
| 160 HttpServerPropertiesManager::GetAlternateProtocol( | 157 HttpServerPropertiesManager::GetAlternateProtocol( |
| 161 const net::HostPortPair& server) { | 158 const net::HostPortPair& server) const { |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 163 return http_server_properties_impl_->GetAlternateProtocol(server); | 160 return http_server_properties_impl_->GetAlternateProtocol(server); |
| 164 } | 161 } |
| 165 | 162 |
| 166 void HttpServerPropertiesManager::SetAlternateProtocol( | 163 void HttpServerPropertiesManager::SetAlternateProtocol( |
| 167 const net::HostPortPair& server, | 164 const net::HostPortPair& server, |
| 168 uint16 alternate_port, | 165 uint16 alternate_port, |
| 169 net::AlternateProtocol alternate_protocol) { | 166 net::AlternateProtocol alternate_protocol) { |
| 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 171 http_server_properties_impl_->SetAlternateProtocol( | 168 http_server_properties_impl_->SetAlternateProtocol( |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 DVLOG(1) << "Size is too large. Clearing all properties."; | 313 DVLOG(1) << "Size is too large. Clearing all properties."; |
| 317 return; | 314 return; |
| 318 } | 315 } |
| 319 | 316 |
| 320 // String is host/port pair of spdy server. | 317 // String is host/port pair of spdy server. |
| 321 scoped_ptr<StringVector> spdy_servers(new StringVector); | 318 scoped_ptr<StringVector> spdy_servers(new StringVector); |
| 322 scoped_ptr<net::SpdySettingsMap> spdy_settings_map(new net::SpdySettingsMap); | 319 scoped_ptr<net::SpdySettingsMap> spdy_settings_map(new net::SpdySettingsMap); |
| 323 scoped_ptr<net::PipelineCapabilityMap> pipeline_capability_map( | 320 scoped_ptr<net::PipelineCapabilityMap> pipeline_capability_map( |
| 324 new net::PipelineCapabilityMap); | 321 new net::PipelineCapabilityMap); |
| 325 scoped_ptr<net::AlternateProtocolMap> alternate_protocol_map( | 322 scoped_ptr<net::AlternateProtocolMap> alternate_protocol_map( |
| 326 new net::AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist)); | 323 new net::AlternateProtocolMap); |
| 327 | 324 |
| 328 for (base::DictionaryValue::Iterator it(*servers_dict); !it.IsAtEnd(); | 325 for (base::DictionaryValue::Iterator it(*servers_dict); !it.IsAtEnd(); |
| 329 it.Advance()) { | 326 it.Advance()) { |
| 330 // Get server's host/pair. | 327 // Get server's host/pair. |
| 331 const std::string& server_str = it.key(); | 328 const std::string& server_str = it.key(); |
| 332 net::HostPortPair server = net::HostPortPair::FromString(server_str); | 329 net::HostPortPair server = net::HostPortPair::FromString(server_str); |
| 333 if (server.host().empty()) { | 330 if (server.host().empty()) { |
| 334 DVLOG(1) << "Malformed http_server_properties for server: " << server_str; | 331 DVLOG(1) << "Malformed http_server_properties for server: " << server_str; |
| 335 detected_corrupted_prefs = true; | 332 detected_corrupted_prefs = true; |
| 336 continue; | 333 continue; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 | 379 |
| 383 int pipeline_capability = net::PIPELINE_UNKNOWN; | 380 int pipeline_capability = net::PIPELINE_UNKNOWN; |
| 384 if ((server_pref_dict->GetInteger( | 381 if ((server_pref_dict->GetInteger( |
| 385 "pipeline_capability", &pipeline_capability)) && | 382 "pipeline_capability", &pipeline_capability)) && |
| 386 pipeline_capability != net::PIPELINE_UNKNOWN) { | 383 pipeline_capability != net::PIPELINE_UNKNOWN) { |
| 387 (*pipeline_capability_map)[server] = | 384 (*pipeline_capability_map)[server] = |
| 388 static_cast<net::HttpPipelinedHostCapability>(pipeline_capability); | 385 static_cast<net::HttpPipelinedHostCapability>(pipeline_capability); |
| 389 } | 386 } |
| 390 | 387 |
| 391 // Get alternate_protocol server. | 388 // Get alternate_protocol server. |
| 392 DCHECK(alternate_protocol_map->Peek(server) == | 389 DCHECK(!ContainsKey(*alternate_protocol_map, server)); |
| 393 alternate_protocol_map->end()); | |
| 394 const base::DictionaryValue* port_alternate_protocol_dict = NULL; | 390 const base::DictionaryValue* port_alternate_protocol_dict = NULL; |
| 395 if (!server_pref_dict->GetDictionaryWithoutPathExpansion( | 391 if (!server_pref_dict->GetDictionaryWithoutPathExpansion( |
| 396 "alternate_protocol", &port_alternate_protocol_dict)) { | 392 "alternate_protocol", &port_alternate_protocol_dict)) { |
| 397 continue; | 393 continue; |
| 398 } | 394 } |
| 399 | 395 |
| 400 do { | 396 do { |
| 401 int port = 0; | 397 int port = 0; |
| 402 if (!port_alternate_protocol_dict->GetIntegerWithoutPathExpansion( | 398 if (!port_alternate_protocol_dict->GetIntegerWithoutPathExpansion( |
| 403 "port", &port) || (port > (1 << 16))) { | 399 "port", &port) || (port > (1 << 16))) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 417 if (!net::IsAlternateProtocolValid(protocol)) { | 413 if (!net::IsAlternateProtocolValid(protocol)) { |
| 418 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; | 414 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; |
| 419 detected_corrupted_prefs = true; | 415 detected_corrupted_prefs = true; |
| 420 continue; | 416 continue; |
| 421 } | 417 } |
| 422 | 418 |
| 423 net::PortAlternateProtocolPair port_alternate_protocol; | 419 net::PortAlternateProtocolPair port_alternate_protocol; |
| 424 port_alternate_protocol.port = port; | 420 port_alternate_protocol.port = port; |
| 425 port_alternate_protocol.protocol = protocol; | 421 port_alternate_protocol.protocol = protocol; |
| 426 | 422 |
| 427 alternate_protocol_map->Put(server, port_alternate_protocol); | 423 (*alternate_protocol_map)[server] = port_alternate_protocol; |
| 428 } while (false); | 424 } while (false); |
| 429 } | 425 } |
| 430 | 426 |
| 431 BrowserThread::PostTask( | 427 BrowserThread::PostTask( |
| 432 BrowserThread::IO, | 428 BrowserThread::IO, |
| 433 FROM_HERE, | 429 FROM_HERE, |
| 434 base::Bind(&HttpServerPropertiesManager:: | 430 base::Bind(&HttpServerPropertiesManager:: |
| 435 UpdateCacheFromPrefsOnIO, | 431 UpdateCacheFromPrefsOnIO, |
| 436 base::Unretained(this), | 432 base::Unretained(this), |
| 437 base::Owned(spdy_servers.release()), | 433 base::Owned(spdy_servers.release()), |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 506 const base::Closure& completion) { | 502 const base::Closure& completion) { |
| 507 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 503 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 508 | 504 |
| 509 base::ListValue* spdy_server_list = new base::ListValue; | 505 base::ListValue* spdy_server_list = new base::ListValue; |
| 510 http_server_properties_impl_->GetSpdyServerList(spdy_server_list); | 506 http_server_properties_impl_->GetSpdyServerList(spdy_server_list); |
| 511 | 507 |
| 512 net::SpdySettingsMap* spdy_settings_map = new net::SpdySettingsMap; | 508 net::SpdySettingsMap* spdy_settings_map = new net::SpdySettingsMap; |
| 513 *spdy_settings_map = http_server_properties_impl_->spdy_settings_map(); | 509 *spdy_settings_map = http_server_properties_impl_->spdy_settings_map(); |
| 514 | 510 |
| 515 net::AlternateProtocolMap* alternate_protocol_map = | 511 net::AlternateProtocolMap* alternate_protocol_map = |
| 516 new net::AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist); | 512 new net::AlternateProtocolMap; |
| 517 const net::AlternateProtocolMap& map = | 513 *alternate_protocol_map = |
| 518 http_server_properties_impl_->alternate_protocol_map(); | 514 http_server_properties_impl_->alternate_protocol_map(); |
| 519 int count = 0; | |
| 520 for (net::AlternateProtocolMap::const_iterator it = map.begin(); | |
| 521 it != map.end() && count < kMaxAlternateProtocolHostsToPersist; | |
| 522 ++it, ++count) { | |
| 523 alternate_protocol_map->Put(it->first, it->second); | |
| 524 } | |
| 525 | 515 |
| 526 net::PipelineCapabilityMap* pipeline_capability_map = | 516 net::PipelineCapabilityMap* pipeline_capability_map = |
| 527 new net::PipelineCapabilityMap; | 517 new net::PipelineCapabilityMap; |
| 528 *pipeline_capability_map = | 518 *pipeline_capability_map = |
| 529 http_server_properties_impl_->GetPipelineCapabilityMap(); | 519 http_server_properties_impl_->GetPipelineCapabilityMap(); |
| 530 | 520 |
| 531 // Update the preferences on the UI thread. | 521 // Update the preferences on the UI thread. |
| 532 BrowserThread::PostTask( | 522 BrowserThread::PostTask( |
| 533 BrowserThread::UI, | 523 BrowserThread::UI, |
| 534 FROM_HERE, | 524 FROM_HERE, |
| (...skipping 30 matching lines...) Expand all Loading... |
| 565 net::HttpPipelinedHostCapability pipeline_capability; | 555 net::HttpPipelinedHostCapability pipeline_capability; |
| 566 }; | 556 }; |
| 567 | 557 |
| 568 void HttpServerPropertiesManager::UpdatePrefsOnUI( | 558 void HttpServerPropertiesManager::UpdatePrefsOnUI( |
| 569 base::ListValue* spdy_server_list, | 559 base::ListValue* spdy_server_list, |
| 570 net::SpdySettingsMap* spdy_settings_map, | 560 net::SpdySettingsMap* spdy_settings_map, |
| 571 net::AlternateProtocolMap* alternate_protocol_map, | 561 net::AlternateProtocolMap* alternate_protocol_map, |
| 572 net::PipelineCapabilityMap* pipeline_capability_map, | 562 net::PipelineCapabilityMap* pipeline_capability_map, |
| 573 const base::Closure& completion) { | 563 const base::Closure& completion) { |
| 574 | 564 |
| 575 // TODO(rtenneti): Fix ServerPrefMap to preserve MRU order of | |
| 576 // alternate_protocol_map and pipeline_capability_map. | |
| 577 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; | 565 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; |
| 578 ServerPrefMap server_pref_map; | 566 ServerPrefMap server_pref_map; |
| 579 | 567 |
| 580 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 568 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 581 | 569 |
| 582 // Add servers that support spdy to server_pref_map. | 570 // Add servers that support spdy to server_pref_map. |
| 583 std::string s; | 571 std::string s; |
| 584 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); | 572 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); |
| 585 list_it != spdy_server_list->end(); ++list_it) { | 573 list_it != spdy_server_list->end(); ++list_it) { |
| 586 if ((*list_it)->GetAsString(&s)) { | 574 if ((*list_it)->GetAsString(&s)) { |
| 587 net::HostPortPair server = net::HostPortPair::FromString(s); | 575 net::HostPortPair server = net::HostPortPair::FromString(s); |
| 588 | 576 |
| 589 ServerPrefMap::iterator it = server_pref_map.find(server); | 577 ServerPrefMap::iterator it = server_pref_map.find(server); |
| 590 if (it == server_pref_map.end()) { | 578 if (it == server_pref_map.end()) { |
| 591 ServerPref server_pref(true, NULL, NULL); | 579 ServerPref server_pref(true, NULL, NULL); |
| 592 server_pref_map[server] = server_pref; | 580 server_pref_map[server] = server_pref; |
| 593 } else { | 581 } else { |
| 594 it->second.supports_spdy = true; | 582 it->second.supports_spdy = true; |
| 595 } | 583 } |
| 596 } | 584 } |
| 597 } | 585 } |
| 598 | 586 |
| 599 // Add servers that have SpdySettings to server_pref_map. | 587 // Add servers that have SpdySettings to server_pref_map. |
| 600 for (net::SpdySettingsMap::iterator map_it = spdy_settings_map->begin(); | 588 for (net::SpdySettingsMap::iterator map_it = |
| 589 spdy_settings_map->begin(); |
| 601 map_it != spdy_settings_map->end(); ++map_it) { | 590 map_it != spdy_settings_map->end(); ++map_it) { |
| 602 const net::HostPortPair& server = map_it->first; | 591 const net::HostPortPair& server = map_it->first; |
| 603 | 592 |
| 604 ServerPrefMap::iterator it = server_pref_map.find(server); | 593 ServerPrefMap::iterator it = server_pref_map.find(server); |
| 605 if (it == server_pref_map.end()) { | 594 if (it == server_pref_map.end()) { |
| 606 ServerPref server_pref(false, &map_it->second, NULL); | 595 ServerPref server_pref(false, &map_it->second, NULL); |
| 607 server_pref_map[server] = server_pref; | 596 server_pref_map[server] = server_pref; |
| 608 } else { | 597 } else { |
| 609 it->second.settings_map = &map_it->second; | 598 it->second.settings_map = &map_it->second; |
| 610 } | 599 } |
| 611 } | 600 } |
| 612 | 601 |
| 613 // Add AlternateProtocol servers to server_pref_map. | 602 // Add AlternateProtocol servers to server_pref_map. |
| 614 for (net::AlternateProtocolMap::const_iterator map_it = | 603 for (net::AlternateProtocolMap::const_iterator map_it = |
| 615 alternate_protocol_map->begin(); | 604 alternate_protocol_map->begin(); |
| 616 map_it != alternate_protocol_map->end(); ++map_it) { | 605 map_it != alternate_protocol_map->end(); ++map_it) { |
| 617 const net::HostPortPair& server = map_it->first; | 606 const net::HostPortPair& server = map_it->first; |
| 618 const net::PortAlternateProtocolPair& port_alternate_protocol = | 607 const net::PortAlternateProtocolPair& port_alternate_protocol = |
| 619 map_it->second; | 608 map_it->second; |
| 620 if (!net::IsAlternateProtocolValid(port_alternate_protocol.protocol)) { | 609 if (!net::IsAlternateProtocolValid(port_alternate_protocol.protocol)) { |
| 621 continue; | 610 continue; |
| 622 } | 611 } |
| 623 | 612 |
| 624 ServerPrefMap::iterator it = server_pref_map.find(server); | 613 ServerPrefMap::iterator it = server_pref_map.find(server); |
| 625 if (it == server_pref_map.end()) { | 614 if (it == server_pref_map.end()) { |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 713 completion.Run(); | 702 completion.Run(); |
| 714 } | 703 } |
| 715 | 704 |
| 716 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { | 705 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { |
| 717 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 706 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 718 if (!setting_prefs_) | 707 if (!setting_prefs_) |
| 719 ScheduleUpdateCacheOnUI(); | 708 ScheduleUpdateCacheOnUI(); |
| 720 } | 709 } |
| 721 | 710 |
| 722 } // namespace chrome_browser_net | 711 } // namespace chrome_browser_net |
| OLD | NEW |