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 |
46 } // namespace | 49 } // namespace |
47 | 50 |
48 //////////////////////////////////////////////////////////////////////////////// | 51 //////////////////////////////////////////////////////////////////////////////// |
49 // HttpServerPropertiesManager | 52 // HttpServerPropertiesManager |
50 | 53 |
51 HttpServerPropertiesManager::HttpServerPropertiesManager( | 54 HttpServerPropertiesManager::HttpServerPropertiesManager( |
52 PrefService* pref_service) | 55 PrefService* pref_service) |
53 : pref_service_(pref_service), | 56 : pref_service_(pref_service), |
54 setting_prefs_(false) { | 57 setting_prefs_(false) { |
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 void HttpServerPropertiesManager::SetSupportsSpdy( | 144 void HttpServerPropertiesManager::SetSupportsSpdy( |
142 const net::HostPortPair& server, | 145 const net::HostPortPair& server, |
143 bool support_spdy) { | 146 bool support_spdy) { |
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 147 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
145 | 148 |
146 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy); | 149 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy); |
147 ScheduleUpdatePrefsOnIO(); | 150 ScheduleUpdatePrefsOnIO(); |
148 } | 151 } |
149 | 152 |
150 bool HttpServerPropertiesManager::HasAlternateProtocol( | 153 bool HttpServerPropertiesManager::HasAlternateProtocol( |
151 const net::HostPortPair& server) const { | 154 const net::HostPortPair& server) { |
152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
153 return http_server_properties_impl_->HasAlternateProtocol(server); | 156 return http_server_properties_impl_->HasAlternateProtocol(server); |
154 } | 157 } |
155 | 158 |
156 net::PortAlternateProtocolPair | 159 net::PortAlternateProtocolPair |
157 HttpServerPropertiesManager::GetAlternateProtocol( | 160 HttpServerPropertiesManager::GetAlternateProtocol( |
158 const net::HostPortPair& server) const { | 161 const net::HostPortPair& server) { |
159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
160 return http_server_properties_impl_->GetAlternateProtocol(server); | 163 return http_server_properties_impl_->GetAlternateProtocol(server); |
161 } | 164 } |
162 | 165 |
163 void HttpServerPropertiesManager::SetAlternateProtocol( | 166 void HttpServerPropertiesManager::SetAlternateProtocol( |
164 const net::HostPortPair& server, | 167 const net::HostPortPair& server, |
165 uint16 alternate_port, | 168 uint16 alternate_port, |
166 net::AlternateProtocol alternate_protocol) { | 169 net::AlternateProtocol alternate_protocol) { |
167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
168 http_server_properties_impl_->SetAlternateProtocol( | 171 http_server_properties_impl_->SetAlternateProtocol( |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 DVLOG(1) << "Size is too large. Clearing all properties."; | 323 DVLOG(1) << "Size is too large. Clearing all properties."; |
321 return; | 324 return; |
322 } | 325 } |
323 | 326 |
324 // String is host/port pair of spdy server. | 327 // String is host/port pair of spdy server. |
325 scoped_ptr<StringVector> spdy_servers(new StringVector); | 328 scoped_ptr<StringVector> spdy_servers(new StringVector); |
326 scoped_ptr<net::SpdySettingsMap> spdy_settings_map(new net::SpdySettingsMap); | 329 scoped_ptr<net::SpdySettingsMap> spdy_settings_map(new net::SpdySettingsMap); |
327 scoped_ptr<net::PipelineCapabilityMap> pipeline_capability_map( | 330 scoped_ptr<net::PipelineCapabilityMap> pipeline_capability_map( |
328 new net::PipelineCapabilityMap); | 331 new net::PipelineCapabilityMap); |
329 scoped_ptr<net::AlternateProtocolMap> alternate_protocol_map( | 332 scoped_ptr<net::AlternateProtocolMap> alternate_protocol_map( |
330 new net::AlternateProtocolMap); | 333 new net::AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist)); |
331 | 334 |
332 for (base::DictionaryValue::Iterator it(*servers_dict); !it.IsAtEnd(); | 335 for (base::DictionaryValue::Iterator it(*servers_dict); !it.IsAtEnd(); |
333 it.Advance()) { | 336 it.Advance()) { |
334 // Get server's host/pair. | 337 // Get server's host/pair. |
335 const std::string& server_str = it.key(); | 338 const std::string& server_str = it.key(); |
336 net::HostPortPair server = net::HostPortPair::FromString(server_str); | 339 net::HostPortPair server = net::HostPortPair::FromString(server_str); |
337 if (server.host().empty()) { | 340 if (server.host().empty()) { |
338 DVLOG(1) << "Malformed http_server_properties for server: " << server_str; | 341 DVLOG(1) << "Malformed http_server_properties for server: " << server_str; |
339 detected_corrupted_prefs = true; | 342 detected_corrupted_prefs = true; |
340 continue; | 343 continue; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 | 389 |
387 int pipeline_capability = net::PIPELINE_UNKNOWN; | 390 int pipeline_capability = net::PIPELINE_UNKNOWN; |
388 if ((server_pref_dict->GetInteger( | 391 if ((server_pref_dict->GetInteger( |
389 "pipeline_capability", &pipeline_capability)) && | 392 "pipeline_capability", &pipeline_capability)) && |
390 pipeline_capability != net::PIPELINE_UNKNOWN) { | 393 pipeline_capability != net::PIPELINE_UNKNOWN) { |
391 (*pipeline_capability_map)[server] = | 394 (*pipeline_capability_map)[server] = |
392 static_cast<net::HttpPipelinedHostCapability>(pipeline_capability); | 395 static_cast<net::HttpPipelinedHostCapability>(pipeline_capability); |
393 } | 396 } |
394 | 397 |
395 // Get alternate_protocol server. | 398 // Get alternate_protocol server. |
396 DCHECK(!ContainsKey(*alternate_protocol_map, server)); | 399 DCHECK(alternate_protocol_map->Peek(server) == |
| 400 alternate_protocol_map->end()); |
397 const base::DictionaryValue* port_alternate_protocol_dict = NULL; | 401 const base::DictionaryValue* port_alternate_protocol_dict = NULL; |
398 if (!server_pref_dict->GetDictionaryWithoutPathExpansion( | 402 if (!server_pref_dict->GetDictionaryWithoutPathExpansion( |
399 "alternate_protocol", &port_alternate_protocol_dict)) { | 403 "alternate_protocol", &port_alternate_protocol_dict)) { |
400 continue; | 404 continue; |
401 } | 405 } |
402 | 406 |
403 do { | 407 do { |
404 int port = 0; | 408 int port = 0; |
405 if (!port_alternate_protocol_dict->GetIntegerWithoutPathExpansion( | 409 if (!port_alternate_protocol_dict->GetIntegerWithoutPathExpansion( |
406 "port", &port) || (port > (1 << 16))) { | 410 "port", &port) || (port > (1 << 16))) { |
(...skipping 13 matching lines...) Expand all Loading... |
420 if (!net::IsAlternateProtocolValid(protocol)) { | 424 if (!net::IsAlternateProtocolValid(protocol)) { |
421 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; | 425 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; |
422 detected_corrupted_prefs = true; | 426 detected_corrupted_prefs = true; |
423 continue; | 427 continue; |
424 } | 428 } |
425 | 429 |
426 net::PortAlternateProtocolPair port_alternate_protocol; | 430 net::PortAlternateProtocolPair port_alternate_protocol; |
427 port_alternate_protocol.port = port; | 431 port_alternate_protocol.port = port; |
428 port_alternate_protocol.protocol = protocol; | 432 port_alternate_protocol.protocol = protocol; |
429 | 433 |
430 (*alternate_protocol_map)[server] = port_alternate_protocol; | 434 alternate_protocol_map->Put(server, port_alternate_protocol); |
431 } while (false); | 435 } while (false); |
432 } | 436 } |
433 | 437 |
434 BrowserThread::PostTask( | 438 BrowserThread::PostTask( |
435 BrowserThread::IO, | 439 BrowserThread::IO, |
436 FROM_HERE, | 440 FROM_HERE, |
437 base::Bind(&HttpServerPropertiesManager:: | 441 base::Bind(&HttpServerPropertiesManager:: |
438 UpdateCacheFromPrefsOnIO, | 442 UpdateCacheFromPrefsOnIO, |
439 base::Unretained(this), | 443 base::Unretained(this), |
440 base::Owned(spdy_servers.release()), | 444 base::Owned(spdy_servers.release()), |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 const base::Closure& completion) { | 513 const base::Closure& completion) { |
510 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 514 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
511 | 515 |
512 base::ListValue* spdy_server_list = new base::ListValue; | 516 base::ListValue* spdy_server_list = new base::ListValue; |
513 http_server_properties_impl_->GetSpdyServerList(spdy_server_list); | 517 http_server_properties_impl_->GetSpdyServerList(spdy_server_list); |
514 | 518 |
515 net::SpdySettingsMap* spdy_settings_map = new net::SpdySettingsMap; | 519 net::SpdySettingsMap* spdy_settings_map = new net::SpdySettingsMap; |
516 *spdy_settings_map = http_server_properties_impl_->spdy_settings_map(); | 520 *spdy_settings_map = http_server_properties_impl_->spdy_settings_map(); |
517 | 521 |
518 net::AlternateProtocolMap* alternate_protocol_map = | 522 net::AlternateProtocolMap* alternate_protocol_map = |
519 new net::AlternateProtocolMap; | 523 new net::AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist); |
520 *alternate_protocol_map = | 524 const net::AlternateProtocolMap& map = |
521 http_server_properties_impl_->alternate_protocol_map(); | 525 http_server_properties_impl_->alternate_protocol_map(); |
| 526 int count = 0; |
| 527 for (net::AlternateProtocolMap::const_iterator it = map.begin(); |
| 528 it != map.end() && count < kMaxAlternateProtocolHostsToPersist; |
| 529 ++it, ++count) { |
| 530 alternate_protocol_map->Put(it->first, it->second); |
| 531 } |
522 | 532 |
523 net::PipelineCapabilityMap* pipeline_capability_map = | 533 net::PipelineCapabilityMap* pipeline_capability_map = |
524 new net::PipelineCapabilityMap; | 534 new net::PipelineCapabilityMap; |
525 *pipeline_capability_map = | 535 *pipeline_capability_map = |
526 http_server_properties_impl_->GetPipelineCapabilityMap(); | 536 http_server_properties_impl_->GetPipelineCapabilityMap(); |
527 | 537 |
528 // Update the preferences on the UI thread. | 538 // Update the preferences on the UI thread. |
529 BrowserThread::PostTask( | 539 BrowserThread::PostTask( |
530 BrowserThread::UI, | 540 BrowserThread::UI, |
531 FROM_HERE, | 541 FROM_HERE, |
(...skipping 30 matching lines...) Expand all Loading... |
562 net::HttpPipelinedHostCapability pipeline_capability; | 572 net::HttpPipelinedHostCapability pipeline_capability; |
563 }; | 573 }; |
564 | 574 |
565 void HttpServerPropertiesManager::UpdatePrefsOnUI( | 575 void HttpServerPropertiesManager::UpdatePrefsOnUI( |
566 base::ListValue* spdy_server_list, | 576 base::ListValue* spdy_server_list, |
567 net::SpdySettingsMap* spdy_settings_map, | 577 net::SpdySettingsMap* spdy_settings_map, |
568 net::AlternateProtocolMap* alternate_protocol_map, | 578 net::AlternateProtocolMap* alternate_protocol_map, |
569 net::PipelineCapabilityMap* pipeline_capability_map, | 579 net::PipelineCapabilityMap* pipeline_capability_map, |
570 const base::Closure& completion) { | 580 const base::Closure& completion) { |
571 | 581 |
| 582 // TODO(rtenneti): Fix ServerPrefMap to preserve MRU order of |
| 583 // alternate_protocol_map and pipeline_capability_map. |
572 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; | 584 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; |
573 ServerPrefMap server_pref_map; | 585 ServerPrefMap server_pref_map; |
574 | 586 |
575 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 587 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
576 | 588 |
577 // Add servers that support spdy to server_pref_map. | 589 // Add servers that support spdy to server_pref_map. |
578 std::string s; | 590 std::string s; |
579 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); | 591 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); |
580 list_it != spdy_server_list->end(); ++list_it) { | 592 list_it != spdy_server_list->end(); ++list_it) { |
581 if ((*list_it)->GetAsString(&s)) { | 593 if ((*list_it)->GetAsString(&s)) { |
582 net::HostPortPair server = net::HostPortPair::FromString(s); | 594 net::HostPortPair server = net::HostPortPair::FromString(s); |
583 | 595 |
584 ServerPrefMap::iterator it = server_pref_map.find(server); | 596 ServerPrefMap::iterator it = server_pref_map.find(server); |
585 if (it == server_pref_map.end()) { | 597 if (it == server_pref_map.end()) { |
586 ServerPref server_pref(true, NULL, NULL); | 598 ServerPref server_pref(true, NULL, NULL); |
587 server_pref_map[server] = server_pref; | 599 server_pref_map[server] = server_pref; |
588 } else { | 600 } else { |
589 it->second.supports_spdy = true; | 601 it->second.supports_spdy = true; |
590 } | 602 } |
591 } | 603 } |
592 } | 604 } |
593 | 605 |
594 // Add servers that have SpdySettings to server_pref_map. | 606 // Add servers that have SpdySettings to server_pref_map. |
595 for (net::SpdySettingsMap::iterator map_it = | 607 for (net::SpdySettingsMap::iterator map_it = spdy_settings_map->begin(); |
596 spdy_settings_map->begin(); | |
597 map_it != spdy_settings_map->end(); ++map_it) { | 608 map_it != spdy_settings_map->end(); ++map_it) { |
598 const net::HostPortPair& server = map_it->first; | 609 const net::HostPortPair& server = map_it->first; |
599 | 610 |
600 ServerPrefMap::iterator it = server_pref_map.find(server); | 611 ServerPrefMap::iterator it = server_pref_map.find(server); |
601 if (it == server_pref_map.end()) { | 612 if (it == server_pref_map.end()) { |
602 ServerPref server_pref(false, &map_it->second, NULL); | 613 ServerPref server_pref(false, &map_it->second, NULL); |
603 server_pref_map[server] = server_pref; | 614 server_pref_map[server] = server_pref; |
604 } else { | 615 } else { |
605 it->second.settings_map = &map_it->second; | 616 it->second.settings_map = &map_it->second; |
606 } | 617 } |
607 } | 618 } |
608 | 619 |
609 // Add AlternateProtocol servers to server_pref_map. | 620 // Add AlternateProtocol servers to server_pref_map. |
610 for (net::AlternateProtocolMap::const_iterator map_it = | 621 for (net::AlternateProtocolMap::const_iterator map_it = |
611 alternate_protocol_map->begin(); | 622 alternate_protocol_map->begin(); |
612 map_it != alternate_protocol_map->end(); ++map_it) { | 623 map_it != alternate_protocol_map->end(); ++map_it) { |
613 const net::HostPortPair& server = map_it->first; | 624 const net::HostPortPair& server = map_it->first; |
614 const net::PortAlternateProtocolPair& port_alternate_protocol = | 625 const net::PortAlternateProtocolPair& port_alternate_protocol = |
615 map_it->second; | 626 map_it->second; |
616 if (!net::IsAlternateProtocolValid(port_alternate_protocol.protocol)) { | 627 if (!net::IsAlternateProtocolValid(port_alternate_protocol.protocol)) { |
617 continue; | 628 continue; |
618 } | 629 } |
619 | 630 |
620 ServerPrefMap::iterator it = server_pref_map.find(server); | 631 ServerPrefMap::iterator it = server_pref_map.find(server); |
621 if (it == server_pref_map.end()) { | 632 if (it == server_pref_map.end()) { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
709 completion.Run(); | 720 completion.Run(); |
710 } | 721 } |
711 | 722 |
712 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { | 723 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { |
713 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 724 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
714 if (!setting_prefs_) | 725 if (!setting_prefs_) |
715 ScheduleUpdateCacheOnUI(); | 726 ScheduleUpdateCacheOnUI(); |
716 } | 727 } |
717 | 728 |
718 } // namespace chrome_browser_net | 729 } // namespace chrome_browser_net |
OLD | NEW |