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