OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_manager.h" | 5 #include "net/http/http_server_properties_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 // ... | 482 // ... |
483 // ], ... | 483 // ], ... |
484 // }, | 484 // }, |
485 if (!http_server_properties_dict.GetListWithoutPathExpansion( | 485 if (!http_server_properties_dict.GetListWithoutPathExpansion( |
486 kServersKey, &servers_list)) { | 486 kServersKey, &servers_list)) { |
487 DVLOG(1) << "Malformed http_server_properties for servers list."; | 487 DVLOG(1) << "Malformed http_server_properties for servers list."; |
488 return; | 488 return; |
489 } | 489 } |
490 } | 490 } |
491 | 491 |
492 IPAddress* addr = new IPAddress; | 492 std::unique_ptr<IPAddress> addr = base::MakeUnique<IPAddress>(); |
493 ReadSupportsQuic(http_server_properties_dict, addr); | 493 ReadSupportsQuic(http_server_properties_dict, addr.get()); |
494 | 494 |
495 // String is "scheme://host:port" tuple of spdy server. | 495 // String is "scheme://host:port" tuple of spdy server. |
496 std::unique_ptr<ServerList> spdy_servers(new ServerList); | 496 std::unique_ptr<SpdyServersMap> spdy_servers_map( |
| 497 new SpdyServersMap(SpdyServersMap::NO_AUTO_EVICT)); |
497 std::unique_ptr<AlternativeServiceMap> alternative_service_map( | 498 std::unique_ptr<AlternativeServiceMap> alternative_service_map( |
498 new AlternativeServiceMap(kMaxAlternateProtocolHostsToPersist)); | 499 new AlternativeServiceMap(kMaxAlternateProtocolHostsToPersist)); |
499 std::unique_ptr<ServerNetworkStatsMap> server_network_stats_map( | 500 std::unique_ptr<ServerNetworkStatsMap> server_network_stats_map( |
500 new ServerNetworkStatsMap(kMaxServerNetworkStatsHostsToPersist)); | 501 new ServerNetworkStatsMap(kMaxServerNetworkStatsHostsToPersist)); |
501 std::unique_ptr<QuicServerInfoMap> quic_server_info_map( | 502 std::unique_ptr<QuicServerInfoMap> quic_server_info_map( |
502 new QuicServerInfoMap(QuicServerInfoMap::NO_AUTO_EVICT)); | 503 new QuicServerInfoMap(QuicServerInfoMap::NO_AUTO_EVICT)); |
503 | 504 |
504 if (version < 4) { | 505 if (version < 4) { |
505 if (!AddServersData(*servers_dict, spdy_servers.get(), | 506 if (!AddServersData(*servers_dict, spdy_servers_map.get(), |
506 alternative_service_map.get(), | 507 alternative_service_map.get(), |
507 server_network_stats_map.get(), version)) { | 508 server_network_stats_map.get(), version)) { |
508 detected_corrupted_prefs = true; | 509 detected_corrupted_prefs = true; |
509 } | 510 } |
510 } else { | 511 } else { |
511 for (base::ListValue::const_iterator it = servers_list->begin(); | 512 // Iterate servers list in reverse MRU order so that entries are inserted |
512 it != servers_list->end(); ++it) { | 513 // into |spdy_servers_map|, |alternative_service_map|, and |
| 514 // |server_network_stats_map| from oldest to newest. |
| 515 for (base::ListValue::const_iterator it = servers_list->end(); |
| 516 it != servers_list->begin();) { |
| 517 --it; |
513 if (!it->GetAsDictionary(&servers_dict)) { | 518 if (!it->GetAsDictionary(&servers_dict)) { |
514 DVLOG(1) << "Malformed http_server_properties for servers dictionary."; | 519 DVLOG(1) << "Malformed http_server_properties for servers dictionary."; |
515 detected_corrupted_prefs = true; | 520 detected_corrupted_prefs = true; |
516 continue; | 521 continue; |
517 } | 522 } |
518 if (!AddServersData(*servers_dict, spdy_servers.get(), | 523 if (!AddServersData(*servers_dict, spdy_servers_map.get(), |
519 alternative_service_map.get(), | 524 alternative_service_map.get(), |
520 server_network_stats_map.get(), version)) { | 525 server_network_stats_map.get(), version)) { |
521 detected_corrupted_prefs = true; | 526 detected_corrupted_prefs = true; |
522 } | 527 } |
523 } | 528 } |
524 } | 529 } |
525 | 530 |
526 if (!AddToQuicServerInfoMap(http_server_properties_dict, | 531 if (!AddToQuicServerInfoMap(http_server_properties_dict, |
527 quic_server_info_map.get())) { | 532 quic_server_info_map.get())) { |
528 detected_corrupted_prefs = true; | 533 detected_corrupted_prefs = true; |
529 } | 534 } |
530 | 535 |
531 network_task_runner_->PostTask( | 536 network_task_runner_->PostTask( |
532 FROM_HERE, | 537 FROM_HERE, |
533 base::Bind( | 538 base::Bind( |
534 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkSequence, | 539 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkSequence, |
535 base::Unretained(this), base::Owned(spdy_servers.release()), | 540 base::Unretained(this), base::Passed(&spdy_servers_map), |
536 base::Owned(alternative_service_map.release()), base::Owned(addr), | 541 base::Passed(&alternative_service_map), base::Passed(&addr), |
537 base::Owned(server_network_stats_map.release()), | 542 base::Passed(&server_network_stats_map), |
538 base::Owned(quic_server_info_map.release()), | 543 base::Passed(&quic_server_info_map), detected_corrupted_prefs)); |
539 detected_corrupted_prefs)); | |
540 } | 544 } |
541 | 545 |
542 bool HttpServerPropertiesManager::AddServersData( | 546 bool HttpServerPropertiesManager::AddServersData( |
543 const base::DictionaryValue& servers_dict, | 547 const base::DictionaryValue& servers_dict, |
544 ServerList* spdy_servers, | 548 SpdyServersMap* spdy_servers_map, |
545 AlternativeServiceMap* alternative_service_map, | 549 AlternativeServiceMap* alternative_service_map, |
546 ServerNetworkStatsMap* network_stats_map, | 550 ServerNetworkStatsMap* network_stats_map, |
547 int version) { | 551 int version) { |
548 for (base::DictionaryValue::Iterator it(servers_dict); !it.IsAtEnd(); | 552 for (base::DictionaryValue::Iterator it(servers_dict); !it.IsAtEnd(); |
549 it.Advance()) { | 553 it.Advance()) { |
550 // Get server's scheme/host/pair. | 554 // Get server's scheme/host/pair. |
551 const std::string& server_str = it.key(); | 555 const std::string& server_str = it.key(); |
552 std::string spdy_server_url = server_str; | 556 std::string spdy_server_url = server_str; |
553 if (version < 5) { | 557 if (version < 5) { |
554 // For old version disk data, always use HTTPS as the scheme. | 558 // For old version disk data, always use HTTPS as the scheme. |
555 spdy_server_url.insert(0, "https://"); | 559 spdy_server_url.insert(0, "https://"); |
556 } | 560 } |
557 url::SchemeHostPort spdy_server((GURL(spdy_server_url))); | 561 url::SchemeHostPort spdy_server((GURL(spdy_server_url))); |
558 if (spdy_server.host().empty()) { | 562 if (spdy_server.host().empty()) { |
559 DVLOG(1) << "Malformed http_server_properties for server: " << server_str; | 563 DVLOG(1) << "Malformed http_server_properties for server: " << server_str; |
560 return false; | 564 return false; |
561 } | 565 } |
562 | 566 |
563 const base::DictionaryValue* server_pref_dict = nullptr; | 567 const base::DictionaryValue* server_pref_dict = nullptr; |
564 if (!it.value().GetAsDictionary(&server_pref_dict)) { | 568 if (!it.value().GetAsDictionary(&server_pref_dict)) { |
565 DVLOG(1) << "Malformed http_server_properties server: " << server_str; | 569 DVLOG(1) << "Malformed http_server_properties server: " << server_str; |
566 return false; | 570 return false; |
567 } | 571 } |
568 | 572 |
569 // Get if server supports Spdy. | 573 // Get if server supports Spdy. |
570 bool supports_spdy = false; | 574 bool supports_spdy = false; |
571 if ((server_pref_dict->GetBoolean(kSupportsSpdyKey, &supports_spdy)) && | 575 if (server_pref_dict->GetBoolean(kSupportsSpdyKey, &supports_spdy) && |
572 supports_spdy) { | 576 supports_spdy) { |
573 spdy_servers->push_back(spdy_server.Serialize()); | 577 spdy_servers_map->Put(spdy_server.Serialize(), supports_spdy); |
574 } | 578 } |
575 | 579 |
576 if (!AddToAlternativeServiceMap(spdy_server, *server_pref_dict, | 580 if (!AddToAlternativeServiceMap(spdy_server, *server_pref_dict, |
577 alternative_service_map) || | 581 alternative_service_map) || |
578 !AddToNetworkStatsMap(spdy_server, *server_pref_dict, | 582 !AddToNetworkStatsMap(spdy_server, *server_pref_dict, |
579 network_stats_map)) { | 583 network_stats_map)) { |
580 return false; | 584 return false; |
581 } | 585 } |
582 } | 586 } |
583 return true; | 587 return true; |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
782 << quic_server_id_str; | 786 << quic_server_id_str; |
783 detected_corrupted_prefs = true; | 787 detected_corrupted_prefs = true; |
784 continue; | 788 continue; |
785 } | 789 } |
786 quic_server_info_map->Put(quic_server_id, quic_server_info); | 790 quic_server_info_map->Put(quic_server_id, quic_server_info); |
787 } | 791 } |
788 return !detected_corrupted_prefs; | 792 return !detected_corrupted_prefs; |
789 } | 793 } |
790 | 794 |
791 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkSequence( | 795 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkSequence( |
792 ServerList* spdy_servers, | 796 std::unique_ptr<SpdyServersMap> spdy_servers_map, |
793 AlternativeServiceMap* alternative_service_map, | 797 std::unique_ptr<AlternativeServiceMap> alternative_service_map, |
794 IPAddress* last_quic_address, | 798 std::unique_ptr<IPAddress> last_quic_address, |
795 ServerNetworkStatsMap* server_network_stats_map, | 799 std::unique_ptr<ServerNetworkStatsMap> server_network_stats_map, |
796 QuicServerInfoMap* quic_server_info_map, | 800 std::unique_ptr<QuicServerInfoMap> quic_server_info_map, |
797 bool detected_corrupted_prefs) { | 801 bool detected_corrupted_prefs) { |
798 // Preferences have the master data because admins might have pushed new | 802 // Preferences have the master data because admins might have pushed new |
799 // preferences. Update the cached data with new data from preferences. | 803 // preferences. Update the cached data with new data from preferences. |
800 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); | 804 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); |
801 | 805 |
802 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size()); | 806 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers_map->size()); |
803 http_server_properties_impl_->SetSpdyServers(spdy_servers, true); | 807 http_server_properties_impl_->SetSpdyServers(std::move(spdy_servers_map)); |
804 | 808 |
805 // Update the cached data and use the new alternative service list from | 809 // Update the cached data and use the new alternative service list from |
806 // preferences. | 810 // preferences. |
807 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers", | 811 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers", |
808 alternative_service_map->size()); | 812 alternative_service_map->size()); |
809 http_server_properties_impl_->SetAlternativeServiceServers( | 813 http_server_properties_impl_->SetAlternativeServiceServers( |
810 alternative_service_map); | 814 std::move(alternative_service_map)); |
811 | 815 |
812 http_server_properties_impl_->SetSupportsQuic(last_quic_address); | 816 http_server_properties_impl_->SetSupportsQuic(*last_quic_address); |
813 | 817 |
814 http_server_properties_impl_->SetServerNetworkStats(server_network_stats_map); | 818 http_server_properties_impl_->SetServerNetworkStats( |
| 819 std::move(server_network_stats_map)); |
815 | 820 |
816 UMA_HISTOGRAM_COUNTS_1000("Net.CountOfQuicServerInfos", | 821 UMA_HISTOGRAM_COUNTS_1000("Net.CountOfQuicServerInfos", |
817 quic_server_info_map->size()); | 822 quic_server_info_map->size()); |
818 | 823 |
819 http_server_properties_impl_->SetQuicServerInfoMap(quic_server_info_map); | 824 http_server_properties_impl_->SetQuicServerInfoMap( |
| 825 std::move(quic_server_info_map)); |
820 | 826 |
821 // Update the prefs with what we have read (delete all corrupted prefs). | 827 // Update the prefs with what we have read (delete all corrupted prefs). |
822 if (detected_corrupted_prefs) | 828 if (detected_corrupted_prefs) |
823 ScheduleUpdatePrefsOnNetworkSequence(DETECTED_CORRUPTED_PREFS); | 829 ScheduleUpdatePrefsOnNetworkSequence(DETECTED_CORRUPTED_PREFS); |
824 } | 830 } |
825 | 831 |
826 // | 832 // |
827 // Update Preferences with data from the cached data. | 833 // Update Preferences with data from the cached data. |
828 // | 834 // |
829 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkSequence( | 835 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkSequence( |
(...skipping 15 matching lines...) Expand all Loading... |
845 // This is required so we can set this as the callback for a timer. | 851 // This is required so we can set this as the callback for a timer. |
846 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkSequence() { | 852 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkSequence() { |
847 UpdatePrefsFromCacheOnNetworkSequence(base::Closure()); | 853 UpdatePrefsFromCacheOnNetworkSequence(base::Closure()); |
848 } | 854 } |
849 | 855 |
850 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkSequence( | 856 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkSequence( |
851 const base::Closure& completion) { | 857 const base::Closure& completion) { |
852 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); | 858 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); |
853 | 859 |
854 // It is in MRU order. | 860 // It is in MRU order. |
855 base::ListValue* spdy_server_list = new base::ListValue; | 861 std::unique_ptr<std::vector<std::string>> spdy_servers = |
| 862 base::MakeUnique<std::vector<std::string>>(); |
856 http_server_properties_impl_->GetSpdyServerList( | 863 http_server_properties_impl_->GetSpdyServerList( |
857 spdy_server_list, kMaxSupportsSpdyServerHostsToPersist); | 864 spdy_servers.get(), kMaxSupportsSpdyServerHostsToPersist); |
858 | 865 |
859 AlternativeServiceMap* alternative_service_map = | 866 std::unique_ptr<AlternativeServiceMap> alternative_service_map = |
860 new AlternativeServiceMap(kMaxAlternateProtocolHostsToPersist); | 867 base::MakeUnique<AlternativeServiceMap>( |
| 868 kMaxAlternateProtocolHostsToPersist); |
861 const AlternativeServiceMap& map = | 869 const AlternativeServiceMap& map = |
862 http_server_properties_impl_->alternative_service_map(); | 870 http_server_properties_impl_->alternative_service_map(); |
863 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers.Memory", | 871 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers.Memory", |
864 map.size()); | 872 map.size()); |
865 int count = 0; | 873 int count = 0; |
866 typedef std::map<std::string, bool> CanonicalHostPersistedMap; | 874 typedef std::map<std::string, bool> CanonicalHostPersistedMap; |
867 CanonicalHostPersistedMap persisted_map; | 875 CanonicalHostPersistedMap persisted_map; |
868 // Maintain MRU order. | 876 // Maintain MRU order. |
869 for (AlternativeServiceMap::const_reverse_iterator it = map.rbegin(); | 877 for (AlternativeServiceMap::const_reverse_iterator it = map.rbegin(); |
870 it != map.rend() && count < kMaxAlternateProtocolHostsToPersist; ++it) { | 878 it != map.rend() && count < kMaxAlternateProtocolHostsToPersist; ++it) { |
(...skipping 26 matching lines...) Expand all Loading... |
897 if (canonical_suffix != nullptr) { | 905 if (canonical_suffix != nullptr) { |
898 if (persisted_map.find(*canonical_suffix) != persisted_map.end()) | 906 if (persisted_map.find(*canonical_suffix) != persisted_map.end()) |
899 continue; | 907 continue; |
900 persisted_map[*canonical_suffix] = true; | 908 persisted_map[*canonical_suffix] = true; |
901 } | 909 } |
902 alternative_service_map->Put(server, | 910 alternative_service_map->Put(server, |
903 notbroken_alternative_service_info_vector); | 911 notbroken_alternative_service_info_vector); |
904 ++count; | 912 ++count; |
905 } | 913 } |
906 | 914 |
907 ServerNetworkStatsMap* server_network_stats_map = | 915 std::unique_ptr<ServerNetworkStatsMap> server_network_stats_map = |
908 new ServerNetworkStatsMap(kMaxServerNetworkStatsHostsToPersist); | 916 base::MakeUnique<ServerNetworkStatsMap>( |
| 917 kMaxServerNetworkStatsHostsToPersist); |
909 const ServerNetworkStatsMap& network_stats_map = | 918 const ServerNetworkStatsMap& network_stats_map = |
910 http_server_properties_impl_->server_network_stats_map(); | 919 http_server_properties_impl_->server_network_stats_map(); |
911 count = 0; | 920 count = 0; |
912 for (ServerNetworkStatsMap::const_reverse_iterator | 921 for (ServerNetworkStatsMap::const_reverse_iterator |
913 it = network_stats_map.rbegin(); | 922 it = network_stats_map.rbegin(); |
914 it != network_stats_map.rend() && | 923 it != network_stats_map.rend() && |
915 count < kMaxServerNetworkStatsHostsToPersist; | 924 count < kMaxServerNetworkStatsHostsToPersist; |
916 ++it, ++count) { | 925 ++it, ++count) { |
917 server_network_stats_map->Put(it->first, it->second); | 926 server_network_stats_map->Put(it->first, it->second); |
918 } | 927 } |
919 | 928 |
920 QuicServerInfoMap* quic_server_info_map = nullptr; | 929 std::unique_ptr<QuicServerInfoMap> quic_server_info_map; |
921 const QuicServerInfoMap& main_quic_server_info_map = | 930 const QuicServerInfoMap& main_quic_server_info_map = |
922 http_server_properties_impl_->quic_server_info_map(); | 931 http_server_properties_impl_->quic_server_info_map(); |
923 if (main_quic_server_info_map.size() > 0) { | 932 if (main_quic_server_info_map.size() > 0) { |
924 quic_server_info_map = | 933 quic_server_info_map = base::MakeUnique<QuicServerInfoMap>( |
925 new QuicServerInfoMap(max_server_configs_stored_in_properties()); | 934 max_server_configs_stored_in_properties()); |
926 for (const std::pair<const QuicServerId, std::string>& entry : | 935 for (QuicServerInfoMap::const_reverse_iterator it = |
927 main_quic_server_info_map) { | 936 main_quic_server_info_map.rbegin(); |
928 quic_server_info_map->Put(entry.first, entry.second); | 937 it != main_quic_server_info_map.rend(); ++it) { |
| 938 quic_server_info_map->Put(it->first, it->second); |
929 } | 939 } |
930 } | 940 } |
931 | 941 |
932 IPAddress* last_quic_addr = new IPAddress; | 942 std::unique_ptr<IPAddress> last_quic_addr = base::MakeUnique<IPAddress>(); |
933 http_server_properties_impl_->GetSupportsQuic(last_quic_addr); | 943 http_server_properties_impl_->GetSupportsQuic(last_quic_addr.get()); |
934 // Update the preferences on the pref thread. | 944 // Update the preferences on the pref thread. |
935 pref_task_runner_->PostTask( | 945 pref_task_runner_->PostTask( |
936 FROM_HERE, | 946 FROM_HERE, |
937 base::Bind( | 947 base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnPrefThread, |
938 &HttpServerPropertiesManager::UpdatePrefsOnPrefThread, pref_weak_ptr_, | 948 pref_weak_ptr_, base::Passed(&spdy_servers), |
939 base::Owned(spdy_server_list), base::Owned(alternative_service_map), | 949 base::Passed(&alternative_service_map), |
940 base::Owned(last_quic_addr), base::Owned(server_network_stats_map), | 950 base::Passed(&last_quic_addr), |
941 base::Owned(quic_server_info_map), completion)); | 951 base::Passed(&server_network_stats_map), |
| 952 base::Passed(&quic_server_info_map), completion)); |
942 } | 953 } |
943 | 954 |
944 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, | 955 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, |
945 // AlternativeServiceInfoVector, and SupportsQuic preferences for a server. This | 956 // AlternativeServiceInfoVector, and SupportsQuic preferences for a server. This |
946 // is used only in UpdatePrefsOnPrefThread. | 957 // is used only in UpdatePrefsOnPrefThread. |
947 struct ServerPref { | 958 struct ServerPref { |
948 ServerPref() | 959 ServerPref() |
949 : supports_spdy(false), | 960 : supports_spdy(false), |
950 settings_map(nullptr), | 961 settings_map(nullptr), |
951 alternative_service_info_vector(nullptr), | 962 alternative_service_info_vector(nullptr), |
(...skipping 12 matching lines...) Expand all Loading... |
964 server_network_stats(server_network_stats) {} | 975 server_network_stats(server_network_stats) {} |
965 bool supports_spdy; | 976 bool supports_spdy; |
966 const SettingsMap* settings_map; | 977 const SettingsMap* settings_map; |
967 const AlternativeServiceInfoVector* alternative_service_info_vector; | 978 const AlternativeServiceInfoVector* alternative_service_info_vector; |
968 const SupportsQuic* supports_quic; | 979 const SupportsQuic* supports_quic; |
969 const ServerNetworkStats* server_network_stats; | 980 const ServerNetworkStats* server_network_stats; |
970 }; | 981 }; |
971 | 982 |
972 // All maps and lists are in MRU order. | 983 // All maps and lists are in MRU order. |
973 void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( | 984 void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( |
974 base::ListValue* spdy_server_list, | 985 std::unique_ptr<std::vector<std::string>> spdy_servers, |
975 AlternativeServiceMap* alternative_service_map, | 986 std::unique_ptr<AlternativeServiceMap> alternative_service_map, |
976 IPAddress* last_quic_address, | 987 std::unique_ptr<IPAddress> last_quic_address, |
977 ServerNetworkStatsMap* server_network_stats_map, | 988 std::unique_ptr<ServerNetworkStatsMap> server_network_stats_map, |
978 QuicServerInfoMap* quic_server_info_map, | 989 std::unique_ptr<QuicServerInfoMap> quic_server_info_map, |
979 const base::Closure& completion) { | 990 const base::Closure& completion) { |
980 typedef base::MRUCache<url::SchemeHostPort, ServerPref> ServerPrefMap; | 991 typedef base::MRUCache<url::SchemeHostPort, ServerPref> ServerPrefMap; |
981 ServerPrefMap server_pref_map(ServerPrefMap::NO_AUTO_EVICT); | 992 ServerPrefMap server_pref_map(ServerPrefMap::NO_AUTO_EVICT); |
982 | 993 |
983 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); | 994 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); |
984 | 995 |
985 // Add servers that support spdy to server_pref_map in the MRU order. | 996 // Add servers that support spdy to server_pref_map in the MRU order. |
986 for (size_t index = spdy_server_list->GetSize(); index > 0; --index) { | 997 DCHECK(spdy_servers); |
987 std::string server_str; | 998 for (size_t i = spdy_servers->size(); i > 0; --i) { |
988 if (spdy_server_list->GetString(index - 1, &server_str)) { | 999 url::SchemeHostPort server((GURL(spdy_servers->at(i - 1)))); |
989 url::SchemeHostPort server((GURL(server_str))); | 1000 ServerPrefMap::iterator it = server_pref_map.Get(server); |
990 ServerPrefMap::iterator it = server_pref_map.Get(server); | 1001 if (it == server_pref_map.end()) { |
991 if (it == server_pref_map.end()) { | 1002 ServerPref server_pref; |
992 ServerPref server_pref; | 1003 server_pref.supports_spdy = true; |
993 server_pref.supports_spdy = true; | 1004 server_pref_map.Put(server, server_pref); |
994 server_pref_map.Put(server, server_pref); | 1005 } else { |
995 } else { | 1006 it->second.supports_spdy = true; |
996 it->second.supports_spdy = true; | |
997 } | |
998 } | 1007 } |
999 } | 1008 } |
1000 | 1009 |
1001 // Add alternative services to server_pref_map in the MRU order. | 1010 // Add alternative services to server_pref_map in the MRU order. |
| 1011 DCHECK(alternative_service_map); |
1002 for (AlternativeServiceMap::const_reverse_iterator map_it = | 1012 for (AlternativeServiceMap::const_reverse_iterator map_it = |
1003 alternative_service_map->rbegin(); | 1013 alternative_service_map->rbegin(); |
1004 map_it != alternative_service_map->rend(); ++map_it) { | 1014 map_it != alternative_service_map->rend(); ++map_it) { |
1005 const url::SchemeHostPort server = map_it->first; | 1015 const url::SchemeHostPort server = map_it->first; |
1006 ServerPrefMap::iterator it = server_pref_map.Get(server); | 1016 ServerPrefMap::iterator it = server_pref_map.Get(server); |
1007 if (it == server_pref_map.end()) { | 1017 if (it == server_pref_map.end()) { |
1008 ServerPref server_pref; | 1018 ServerPref server_pref; |
1009 server_pref.alternative_service_info_vector = &map_it->second; | 1019 server_pref.alternative_service_info_vector = &map_it->second; |
1010 server_pref_map.Put(server, server_pref); | 1020 server_pref_map.Put(server, server_pref); |
1011 } else { | 1021 } else { |
1012 it->second.alternative_service_info_vector = &map_it->second; | 1022 it->second.alternative_service_info_vector = &map_it->second; |
1013 } | 1023 } |
1014 } | 1024 } |
1015 | 1025 |
1016 // Add ServerNetworkStats servers to server_pref_map in the MRU order. | 1026 // Add ServerNetworkStats servers to server_pref_map in the MRU order. |
| 1027 DCHECK(server_network_stats_map); |
1017 for (ServerNetworkStatsMap::const_reverse_iterator map_it = | 1028 for (ServerNetworkStatsMap::const_reverse_iterator map_it = |
1018 server_network_stats_map->rbegin(); | 1029 server_network_stats_map->rbegin(); |
1019 map_it != server_network_stats_map->rend(); ++map_it) { | 1030 map_it != server_network_stats_map->rend(); ++map_it) { |
1020 const url::SchemeHostPort server = map_it->first; | 1031 const url::SchemeHostPort server = map_it->first; |
1021 ServerPrefMap::iterator it = server_pref_map.Get(server); | 1032 ServerPrefMap::iterator it = server_pref_map.Get(server); |
1022 if (it == server_pref_map.end()) { | 1033 if (it == server_pref_map.end()) { |
1023 ServerPref server_pref; | 1034 ServerPref server_pref; |
1024 server_pref.server_network_stats = &map_it->second; | 1035 server_pref.server_network_stats = &map_it->second; |
1025 server_pref_map.Put(server, server_pref); | 1036 server_pref_map.Put(server, server_pref); |
1026 } else { | 1037 } else { |
1027 it->second.server_network_stats = &map_it->second; | 1038 it->second.server_network_stats = &map_it->second; |
1028 } | 1039 } |
1029 } | 1040 } |
1030 | 1041 |
1031 // Persist properties to the prefs in the MRU order. | 1042 // Persist properties to the prefs in the MRU order. |
1032 base::DictionaryValue http_server_properties_dict; | 1043 base::DictionaryValue http_server_properties_dict; |
1033 auto servers_list = base::MakeUnique<base::ListValue>(); | 1044 auto servers_list = base::MakeUnique<base::ListValue>(); |
1034 for (ServerPrefMap::const_reverse_iterator map_it = server_pref_map.rbegin(); | 1045 for (ServerPrefMap::const_reverse_iterator map_it = server_pref_map.rbegin(); |
1035 map_it != server_pref_map.rend(); ++map_it) { | 1046 map_it != server_pref_map.rend(); ++map_it) { |
1036 const url::SchemeHostPort server = map_it->first; | 1047 const url::SchemeHostPort server = map_it->first; |
1037 const ServerPref& server_pref = map_it->second; | 1048 const ServerPref& server_pref = map_it->second; |
1038 | 1049 |
1039 auto servers_dict = base::MakeUnique<base::DictionaryValue>(); | 1050 auto servers_dict = base::MakeUnique<base::DictionaryValue>(); |
1040 auto server_pref_dict = base::MakeUnique<base::DictionaryValue>(); | 1051 auto server_pref_dict = base::MakeUnique<base::DictionaryValue>(); |
1041 | 1052 |
1042 // Save supports_spdy. | 1053 // Save supports_spdy. |
1043 if (server_pref.supports_spdy) | 1054 if (server_pref.supports_spdy) { |
1044 server_pref_dict->SetBoolean(kSupportsSpdyKey, server_pref.supports_spdy); | 1055 server_pref_dict->SetBoolean(kSupportsSpdyKey, server_pref.supports_spdy); |
1045 SaveAlternativeServiceToServerPrefs( | 1056 } |
1046 server_pref.alternative_service_info_vector, server_pref_dict.get()); | 1057 if (server_pref.alternative_service_info_vector) { |
1047 SaveNetworkStatsToServerPrefs(server_pref.server_network_stats, | 1058 SaveAlternativeServiceToServerPrefs( |
1048 server_pref_dict.get()); | 1059 *server_pref.alternative_service_info_vector, server_pref_dict.get()); |
| 1060 } |
| 1061 if (server_pref.server_network_stats) { |
| 1062 SaveNetworkStatsToServerPrefs(*server_pref.server_network_stats, |
| 1063 server_pref_dict.get()); |
| 1064 } |
1049 | 1065 |
1050 servers_dict->SetWithoutPathExpansion(server.Serialize(), | 1066 servers_dict->SetWithoutPathExpansion(server.Serialize(), |
1051 std::move(server_pref_dict)); | 1067 std::move(server_pref_dict)); |
1052 bool value = servers_list->AppendIfNotPresent(std::move(servers_dict)); | 1068 bool value = servers_list->AppendIfNotPresent(std::move(servers_dict)); |
1053 DCHECK(value); // Should never happen. | 1069 DCHECK(value); // Should never happen. |
1054 } | 1070 } |
1055 | 1071 |
1056 http_server_properties_dict.SetWithoutPathExpansion(kServersKey, | 1072 http_server_properties_dict.SetWithoutPathExpansion(kServersKey, |
1057 std::move(servers_list)); | 1073 std::move(servers_list)); |
1058 SetVersion(&http_server_properties_dict, kVersionNumber); | 1074 SetVersion(&http_server_properties_dict, kVersionNumber); |
1059 | 1075 |
1060 SaveSupportsQuicToPrefs(last_quic_address, &http_server_properties_dict); | 1076 DCHECK(last_quic_address); |
| 1077 SaveSupportsQuicToPrefs(*last_quic_address, &http_server_properties_dict); |
1061 | 1078 |
1062 SaveQuicServerInfoMapToServerPrefs(quic_server_info_map, | 1079 if (quic_server_info_map) { |
1063 &http_server_properties_dict); | 1080 SaveQuicServerInfoMapToServerPrefs(*quic_server_info_map, |
| 1081 &http_server_properties_dict); |
| 1082 } |
1064 | 1083 |
1065 setting_prefs_ = true; | 1084 setting_prefs_ = true; |
1066 pref_delegate_->SetServerProperties(http_server_properties_dict); | 1085 pref_delegate_->SetServerProperties(http_server_properties_dict); |
1067 setting_prefs_ = false; | 1086 setting_prefs_ = false; |
1068 | 1087 |
1069 net_log_.AddEvent(NetLogEventType::HTTP_SERVER_PROPERTIES_UPDATE_PREFS, | 1088 net_log_.AddEvent(NetLogEventType::HTTP_SERVER_PROPERTIES_UPDATE_PREFS, |
1070 base::Bind(&NetLogCallback, http_server_properties_dict)); | 1089 base::Bind(&NetLogCallback, http_server_properties_dict)); |
1071 // Note that |completion| will be fired after we have written everything to | 1090 // Note that |completion| will be fired after we have written everything to |
1072 // the Preferences, but likely before these changes are serialized to disk. | 1091 // the Preferences, but likely before these changes are serialized to disk. |
1073 // This is not a problem though, as JSONPrefStore guarantees that this will | 1092 // This is not a problem though, as JSONPrefStore guarantees that this will |
1074 // happen, pretty soon, and even in the case we shut down immediately. | 1093 // happen, pretty soon, and even in the case we shut down immediately. |
1075 if (!completion.is_null()) | 1094 if (!completion.is_null()) |
1076 completion.Run(); | 1095 completion.Run(); |
1077 } | 1096 } |
1078 | 1097 |
1079 void HttpServerPropertiesManager::SaveAlternativeServiceToServerPrefs( | 1098 void HttpServerPropertiesManager::SaveAlternativeServiceToServerPrefs( |
1080 const AlternativeServiceInfoVector* alternative_service_info_vector, | 1099 const AlternativeServiceInfoVector& alternative_service_info_vector, |
1081 base::DictionaryValue* server_pref_dict) { | 1100 base::DictionaryValue* server_pref_dict) { |
1082 if (!alternative_service_info_vector || | 1101 if (alternative_service_info_vector.empty()) { |
1083 alternative_service_info_vector->empty()) { | |
1084 return; | 1102 return; |
1085 } | 1103 } |
1086 std::unique_ptr<base::ListValue> alternative_service_list( | 1104 std::unique_ptr<base::ListValue> alternative_service_list( |
1087 new base::ListValue); | 1105 new base::ListValue); |
1088 for (const AlternativeServiceInfo& alternative_service_info : | 1106 for (const AlternativeServiceInfo& alternative_service_info : |
1089 *alternative_service_info_vector) { | 1107 alternative_service_info_vector) { |
1090 const AlternativeService alternative_service = | 1108 const AlternativeService alternative_service = |
1091 alternative_service_info.alternative_service(); | 1109 alternative_service_info.alternative_service(); |
1092 DCHECK(IsAlternateProtocolValid(alternative_service.protocol)); | 1110 DCHECK(IsAlternateProtocolValid(alternative_service.protocol)); |
1093 std::unique_ptr<base::DictionaryValue> alternative_service_dict( | 1111 std::unique_ptr<base::DictionaryValue> alternative_service_dict( |
1094 new base::DictionaryValue); | 1112 new base::DictionaryValue); |
1095 alternative_service_dict->SetInteger(kPortKey, alternative_service.port); | 1113 alternative_service_dict->SetInteger(kPortKey, alternative_service.port); |
1096 if (!alternative_service.host.empty()) { | 1114 if (!alternative_service.host.empty()) { |
1097 alternative_service_dict->SetString(kHostKey, alternative_service.host); | 1115 alternative_service_dict->SetString(kHostKey, alternative_service.host); |
1098 } | 1116 } |
1099 alternative_service_dict->SetString( | 1117 alternative_service_dict->SetString( |
1100 kProtocolKey, NextProtoToString(alternative_service.protocol)); | 1118 kProtocolKey, NextProtoToString(alternative_service.protocol)); |
1101 // JSON cannot store int64_t, so expiration is converted to a string. | 1119 // JSON cannot store int64_t, so expiration is converted to a string. |
1102 alternative_service_dict->SetString( | 1120 alternative_service_dict->SetString( |
1103 kExpirationKey, | 1121 kExpirationKey, |
1104 base::Int64ToString( | 1122 base::Int64ToString( |
1105 alternative_service_info.expiration().ToInternalValue())); | 1123 alternative_service_info.expiration().ToInternalValue())); |
1106 alternative_service_list->Append(std::move(alternative_service_dict)); | 1124 alternative_service_list->Append(std::move(alternative_service_dict)); |
1107 } | 1125 } |
1108 if (alternative_service_list->GetSize() == 0) | 1126 if (alternative_service_list->GetSize() == 0) |
1109 return; | 1127 return; |
1110 server_pref_dict->SetWithoutPathExpansion( | 1128 server_pref_dict->SetWithoutPathExpansion( |
1111 kAlternativeServiceKey, std::move(alternative_service_list)); | 1129 kAlternativeServiceKey, std::move(alternative_service_list)); |
1112 } | 1130 } |
1113 | 1131 |
1114 void HttpServerPropertiesManager::SaveSupportsQuicToPrefs( | 1132 void HttpServerPropertiesManager::SaveSupportsQuicToPrefs( |
1115 const IPAddress* last_quic_address, | 1133 const IPAddress& last_quic_address, |
1116 base::DictionaryValue* http_server_properties_dict) { | 1134 base::DictionaryValue* http_server_properties_dict) { |
1117 if (!last_quic_address || !last_quic_address->IsValid()) | 1135 if (!last_quic_address.IsValid()) |
1118 return; | 1136 return; |
1119 | 1137 |
1120 auto supports_quic_dict = base::MakeUnique<base::DictionaryValue>(); | 1138 auto supports_quic_dict = base::MakeUnique<base::DictionaryValue>(); |
1121 supports_quic_dict->SetBoolean(kUsedQuicKey, true); | 1139 supports_quic_dict->SetBoolean(kUsedQuicKey, true); |
1122 supports_quic_dict->SetString(kAddressKey, last_quic_address->ToString()); | 1140 supports_quic_dict->SetString(kAddressKey, last_quic_address.ToString()); |
1123 http_server_properties_dict->SetWithoutPathExpansion( | 1141 http_server_properties_dict->SetWithoutPathExpansion( |
1124 kSupportsQuicKey, std::move(supports_quic_dict)); | 1142 kSupportsQuicKey, std::move(supports_quic_dict)); |
1125 } | 1143 } |
1126 | 1144 |
1127 void HttpServerPropertiesManager::SaveNetworkStatsToServerPrefs( | 1145 void HttpServerPropertiesManager::SaveNetworkStatsToServerPrefs( |
1128 const ServerNetworkStats* server_network_stats, | 1146 const ServerNetworkStats& server_network_stats, |
1129 base::DictionaryValue* server_pref_dict) { | 1147 base::DictionaryValue* server_pref_dict) { |
1130 if (!server_network_stats) | |
1131 return; | |
1132 | |
1133 auto server_network_stats_dict = base::MakeUnique<base::DictionaryValue>(); | 1148 auto server_network_stats_dict = base::MakeUnique<base::DictionaryValue>(); |
1134 // Becasue JSON doesn't support int64_t, persist int64_t as a string. | 1149 // Becasue JSON doesn't support int64_t, persist int64_t as a string. |
1135 server_network_stats_dict->SetInteger( | 1150 server_network_stats_dict->SetInteger( |
1136 kSrttKey, static_cast<int>(server_network_stats->srtt.ToInternalValue())); | 1151 kSrttKey, static_cast<int>(server_network_stats.srtt.ToInternalValue())); |
1137 // TODO(rtenneti): When QUIC starts using bandwidth_estimate, then persist | 1152 // TODO(rtenneti): When QUIC starts using bandwidth_estimate, then persist |
1138 // bandwidth_estimate. | 1153 // bandwidth_estimate. |
1139 server_pref_dict->SetWithoutPathExpansion( | 1154 server_pref_dict->SetWithoutPathExpansion( |
1140 kNetworkStatsKey, std::move(server_network_stats_dict)); | 1155 kNetworkStatsKey, std::move(server_network_stats_dict)); |
1141 } | 1156 } |
1142 | 1157 |
1143 void HttpServerPropertiesManager::SaveQuicServerInfoMapToServerPrefs( | 1158 void HttpServerPropertiesManager::SaveQuicServerInfoMapToServerPrefs( |
1144 QuicServerInfoMap* quic_server_info_map, | 1159 const QuicServerInfoMap& quic_server_info_map, |
1145 base::DictionaryValue* http_server_properties_dict) { | 1160 base::DictionaryValue* http_server_properties_dict) { |
1146 if (!quic_server_info_map) | |
1147 return; | |
1148 | |
1149 auto quic_servers_dict = base::MakeUnique<base::DictionaryValue>(); | 1161 auto quic_servers_dict = base::MakeUnique<base::DictionaryValue>(); |
1150 for (const std::pair<QuicServerId, std::string>& entry : | 1162 for (QuicServerInfoMap::const_reverse_iterator it = |
1151 *quic_server_info_map) { | 1163 quic_server_info_map.rbegin(); |
1152 const QuicServerId& server_id = entry.first; | 1164 it != quic_server_info_map.rend(); ++it) { |
| 1165 const QuicServerId& server_id = it->first; |
1153 auto quic_server_pref_dict = base::MakeUnique<base::DictionaryValue>(); | 1166 auto quic_server_pref_dict = base::MakeUnique<base::DictionaryValue>(); |
1154 quic_server_pref_dict->SetStringWithoutPathExpansion(kServerInfoKey, | 1167 quic_server_pref_dict->SetStringWithoutPathExpansion(kServerInfoKey, |
1155 entry.second); | 1168 it->second); |
1156 quic_servers_dict->SetWithoutPathExpansion( | 1169 quic_servers_dict->SetWithoutPathExpansion( |
1157 server_id.ToString(), std::move(quic_server_pref_dict)); | 1170 server_id.ToString(), std::move(quic_server_pref_dict)); |
1158 } | 1171 } |
1159 http_server_properties_dict->SetWithoutPathExpansion( | 1172 http_server_properties_dict->SetWithoutPathExpansion( |
1160 kQuicServers, std::move(quic_servers_dict)); | 1173 kQuicServers, std::move(quic_servers_dict)); |
1161 } | 1174 } |
1162 | 1175 |
1163 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { | 1176 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { |
1164 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); | 1177 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); |
1165 if (!setting_prefs_) | 1178 if (!setting_prefs_) |
1166 ScheduleUpdateCacheOnPrefThread(); | 1179 ScheduleUpdateCacheOnPrefThread(); |
1167 } | 1180 } |
1168 | 1181 |
1169 void HttpServerPropertiesManager::SetInitialized() { | 1182 void HttpServerPropertiesManager::SetInitialized() { |
1170 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); | 1183 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); |
1171 is_initialized_ = true; | 1184 is_initialized_ = true; |
1172 net_log_.EndEvent(NetLogEventType::HTTP_SERVER_PROPERTIES_INITIALIZATION); | 1185 net_log_.EndEvent(NetLogEventType::HTTP_SERVER_PROPERTIES_INITIALIZATION); |
1173 } | 1186 } |
1174 | 1187 |
1175 } // namespace net | 1188 } // namespace net |
OLD | NEW |