Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2197)

Side by Side Diff: net/http/http_server_properties_manager.cc

Issue 2932953002: Persist broken and recently-broken alt-svcs to prefs in HttpServerPropertiesManager (Closed)
Patch Set: Added a DCHECK Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 42
43 // Persist 200 MRU AlternateProtocolHostPortPairs. 43 // Persist 200 MRU AlternateProtocolHostPortPairs.
44 const int kMaxAlternateProtocolHostsToPersist = 200; 44 const int kMaxAlternateProtocolHostsToPersist = 200;
45 45
46 // Persist 300 MRU SupportsSpdyServerHostPortPairs. 46 // Persist 300 MRU SupportsSpdyServerHostPortPairs.
47 const int kMaxSupportsSpdyServerHostsToPersist = 300; 47 const int kMaxSupportsSpdyServerHostsToPersist = 300;
48 48
49 // Persist 200 ServerNetworkStats. 49 // Persist 200 ServerNetworkStats.
50 const int kMaxServerNetworkStatsHostsToPersist = 200; 50 const int kMaxServerNetworkStatsHostsToPersist = 200;
51 51
52 // Persist 200 BrokenAlternativeServices
53 const int kMaxBrokenAlternativeServicesToPersist = 200;
54
55 // Persist 200 RecentlyBrokenAlternativeServices
56 const int kMaxRecentlyBrokenAlternativeServicesToPersist = 200;
57
52 const char kVersionKey[] = "version"; 58 const char kVersionKey[] = "version";
53 const char kServersKey[] = "servers"; 59 const char kServersKey[] = "servers";
54 const char kSupportsSpdyKey[] = "supports_spdy"; 60 const char kSupportsSpdyKey[] = "supports_spdy";
55 const char kSupportsQuicKey[] = "supports_quic"; 61 const char kSupportsQuicKey[] = "supports_quic";
56 const char kQuicServers[] = "quic_servers"; 62 const char kQuicServers[] = "quic_servers";
57 const char kServerInfoKey[] = "server_info"; 63 const char kServerInfoKey[] = "server_info";
58 const char kUsedQuicKey[] = "used_quic"; 64 const char kUsedQuicKey[] = "used_quic";
59 const char kAddressKey[] = "address"; 65 const char kAddressKey[] = "address";
60 const char kAlternativeServiceKey[] = "alternative_service"; 66 const char kAlternativeServiceKey[] = "alternative_service";
61 const char kProtocolKey[] = "protocol_str"; 67 const char kProtocolKey[] = "protocol_str";
62 const char kHostKey[] = "host"; 68 const char kHostKey[] = "host";
63 const char kPortKey[] = "port"; 69 const char kPortKey[] = "port";
64 const char kExpirationKey[] = "expiration"; 70 const char kExpirationKey[] = "expiration";
65 const char kNetworkStatsKey[] = "network_stats"; 71 const char kNetworkStatsKey[] = "network_stats";
66 const char kSrttKey[] = "srtt"; 72 const char kSrttKey[] = "srtt";
73 const char kBrokenAlternativeServicesKey[] = "broken_alternative_services";
74 const char kRecentlyBrokenAlternativeServicesKey[] =
75 "recently_broken_alternative_services";
67 76
68 } // namespace 77 } // namespace
69 78
70 //////////////////////////////////////////////////////////////////////////////// 79 ////////////////////////////////////////////////////////////////////////////////
71 // HttpServerPropertiesManager 80 // HttpServerPropertiesManager
72 81
73 HttpServerPropertiesManager::PrefDelegate::~PrefDelegate() {} 82 HttpServerPropertiesManager::PrefDelegate::~PrefDelegate() {}
74 83
75 HttpServerPropertiesManager::HttpServerPropertiesManager( 84 HttpServerPropertiesManager::HttpServerPropertiesManager(
76 PrefDelegate* pref_delegate, 85 PrefDelegate* pref_delegate,
77 scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner, 86 scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner,
78 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner) 87 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner)
79 : pref_task_runner_(std::move(pref_task_runner)), 88 : pref_task_runner_(std::move(pref_task_runner)),
80 pref_delegate_(pref_delegate), 89 pref_delegate_(pref_delegate),
81 setting_prefs_(false), 90 setting_prefs_(false),
91 clock_(&default_clock_),
82 is_initialized_(false), 92 is_initialized_(false),
83 network_task_runner_(std::move(network_task_runner)) { 93 network_task_runner_(std::move(network_task_runner)) {
84 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); 94 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence());
85 DCHECK(pref_delegate_); 95 DCHECK(pref_delegate_);
86 pref_weak_ptr_factory_.reset( 96 pref_weak_ptr_factory_.reset(
87 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); 97 new base::WeakPtrFactory<HttpServerPropertiesManager>(this));
88 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr(); 98 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr();
89 pref_cache_update_timer_.reset(new base::OneShotTimer); 99 pref_cache_update_timer_.reset(new base::OneShotTimer);
90 pref_cache_update_timer_->SetTaskRunner(pref_task_runner_); 100 pref_cache_update_timer_->SetTaskRunner(pref_task_runner_);
91 pref_delegate_->StartListeningForUpdates( 101 pref_delegate_->StartListeningForUpdates(
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 detected_corrupted_prefs = true; 517 detected_corrupted_prefs = true;
508 } 518 }
509 } 519 }
510 } 520 }
511 521
512 if (!AddToQuicServerInfoMap(http_server_properties_dict, 522 if (!AddToQuicServerInfoMap(http_server_properties_dict,
513 quic_server_info_map.get())) { 523 quic_server_info_map.get())) {
514 detected_corrupted_prefs = true; 524 detected_corrupted_prefs = true;
515 } 525 }
516 526
527 std::unique_ptr<RecentlyBrokenAlternativeServices>
528 recently_broken_alternative_services;
529 const base::ListValue* recently_broken_alt_svc_list = nullptr;
530
531 std::unique_ptr<BrokenAlternativeServiceList> broken_alternative_service_list;
532 const base::ListValue* broken_alt_svc_list = nullptr;
533
534 // Read recently broken alternative services if they exist.
535 if (http_server_properties_dict.GetListWithoutPathExpansion(
536 kRecentlyBrokenAlternativeServicesKey,
537 &recently_broken_alt_svc_list)) {
538 recently_broken_alternative_services.reset(
539 new RecentlyBrokenAlternativeServices(
540 RecentlyBrokenAlternativeServices::NO_AUTO_EVICT));
541
542 for (auto it = recently_broken_alt_svc_list->begin();
543 it != recently_broken_alt_svc_list->end(); ++it) {
544 const base::DictionaryValue* entry_dict = nullptr;
545 if (!it->GetAsDictionary(&entry_dict) ||
546 !AddRecentlyBrokenAlternativeServiceData(
547 entry_dict, recently_broken_alternative_services.get())) {
548 DVLOG(1) << "Malformed http_server_properties for recently broken "
549 << "alternative services list.";
550 detected_corrupted_prefs = true;
551 }
552 }
553
554 // Read broken alternative services if they exist
555 if (http_server_properties_dict.GetListWithoutPathExpansion(
556 kBrokenAlternativeServicesKey, &broken_alt_svc_list)) {
557 broken_alternative_service_list.reset(new BrokenAlternativeServiceList());
558
559 for (auto it = broken_alt_svc_list->begin();
560 it != broken_alt_svc_list->end(); ++it) {
561 const base::DictionaryValue* entry_dict = nullptr;
562 AlternativeService alternative_service;
563 base::TimeTicks expiration_time_ticks;
564 if (!it->GetAsDictionary(&entry_dict) ||
565 !ExtractBrokenAlternativeServiceData(
566 entry_dict, &alternative_service, &expiration_time_ticks)) {
567 DVLOG(1) << "Malformed http_server_properties for broken alternative "
568 << "services list.";
569 detected_corrupted_prefs = true;
570 continue;
571 }
572 if (recently_broken_alternative_services->Peek(alternative_service) !=
573 recently_broken_alternative_services->end()) {
574 broken_alternative_service_list->push_back(
575 std::make_pair(alternative_service, expiration_time_ticks));
576 } else {
577 DVLOG(1) << "Broken alternative service does not have an entry in "
578 << "map of recently broken alternative services. "
579 << "Will not be added.";
580 }
581 }
582 }
583 }
584
517 network_task_runner_->PostTask( 585 network_task_runner_->PostTask(
518 FROM_HERE, 586 FROM_HERE,
519 base::Bind( 587 base::Bind(
520 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkSequence, 588 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkSequence,
521 base::Unretained(this), base::Owned(spdy_servers.release()), 589 base::Unretained(this), base::Owned(spdy_servers.release()),
522 base::Owned(alternative_service_map.release()), base::Owned(addr), 590 base::Owned(alternative_service_map.release()), base::Owned(addr),
523 base::Owned(server_network_stats_map.release()), 591 base::Owned(server_network_stats_map.release()),
524 base::Owned(quic_server_info_map.release()), 592 base::Owned(quic_server_info_map.release()),
593 base::Passed(&broken_alternative_service_list),
594 base::Passed(&recently_broken_alternative_services),
525 detected_corrupted_prefs)); 595 detected_corrupted_prefs));
526 } 596 }
527 597
598 bool HttpServerPropertiesManager::ExtractBrokenAlternativeServiceData(
599 const base::DictionaryValue* broken_alternative_service_dict,
600 AlternativeService* alternative_service,
601 base::TimeTicks* expiration_time_ticks) {
602 DCHECK(broken_alternative_service_dict);
603
604 if (broken_alternative_service_dict->size() != 1u)
605 return false;
606
607 base::DictionaryValue::Iterator it(*broken_alternative_service_dict);
608
609 const std::string& alternative_service_string = it.key();
610 if (!AlternativeService::FromString(alternative_service_string,
611 alternative_service))
612 return false;
613
614 int64_t expiration_int64;
615 std::string time_ticks_string;
616 if (!it.value().GetAsString(&time_ticks_string))
617 return false;
618 if (!base::StringToInt64(time_ticks_string, &expiration_int64))
619 return false;
620 time_t expiration_time_t = static_cast<time_t>(expiration_int64);
621 // Convert expiration from time_t to Time to TimeTicks
622 *expiration_time_ticks =
623 clock_->NowTicks() +
624 (base::Time::FromTimeT(expiration_time_t) - base::Time::Now());
625
626 return true;
627 }
628
629 bool HttpServerPropertiesManager::AddRecentlyBrokenAlternativeServiceData(
630 const base::DictionaryValue* recently_broken_alternative_service_dict,
631 RecentlyBrokenAlternativeServices* recently_broken_alternative_services) {
632 DCHECK(recently_broken_alternative_service_dict);
633
634 if (recently_broken_alternative_service_dict->size() != 1u)
635 return false;
636
637 base::DictionaryValue::Iterator it(*recently_broken_alternative_service_dict);
638
639 AlternativeService alt_service;
640 const std::string& alt_service_string = it.key();
641 if (!AlternativeService::FromString(alt_service_string, &alt_service))
642 return false;
643
644 int broken_count;
645 if (!it.value().GetAsInteger(&broken_count))
646 return false;
647
648 recently_broken_alternative_services->Put(alt_service, broken_count);
649 return true;
650 }
651
528 bool HttpServerPropertiesManager::AddServersData( 652 bool HttpServerPropertiesManager::AddServersData(
529 const base::DictionaryValue& servers_dict, 653 const base::DictionaryValue& servers_dict,
530 ServerList* spdy_servers, 654 ServerList* spdy_servers,
531 AlternativeServiceMap* alternative_service_map, 655 AlternativeServiceMap* alternative_service_map,
532 ServerNetworkStatsMap* network_stats_map, 656 ServerNetworkStatsMap* network_stats_map,
533 int version) { 657 int version) {
534 for (base::DictionaryValue::Iterator it(servers_dict); !it.IsAtEnd(); 658 for (base::DictionaryValue::Iterator it(servers_dict); !it.IsAtEnd();
535 it.Advance()) { 659 it.Advance()) {
536 // Get server's scheme/host/pair. 660 // Get server's scheme/host/pair.
537 const std::string& server_str = it.key(); 661 const std::string& server_str = it.key();
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 } 897 }
774 return !detected_corrupted_prefs; 898 return !detected_corrupted_prefs;
775 } 899 }
776 900
777 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkSequence( 901 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkSequence(
778 ServerList* spdy_servers, 902 ServerList* spdy_servers,
779 AlternativeServiceMap* alternative_service_map, 903 AlternativeServiceMap* alternative_service_map,
780 IPAddress* last_quic_address, 904 IPAddress* last_quic_address,
781 ServerNetworkStatsMap* server_network_stats_map, 905 ServerNetworkStatsMap* server_network_stats_map,
782 QuicServerInfoMap* quic_server_info_map, 906 QuicServerInfoMap* quic_server_info_map,
907 std::unique_ptr<BrokenAlternativeServiceList>
908 broken_alternative_service_list,
909 std::unique_ptr<RecentlyBrokenAlternativeServices>
910 recently_broken_alternative_services,
783 bool detected_corrupted_prefs) { 911 bool detected_corrupted_prefs) {
784 // Preferences have the master data because admins might have pushed new 912 // Preferences have the master data because admins might have pushed new
785 // preferences. Update the cached data with new data from preferences. 913 // preferences. Update the cached data with new data from preferences.
786 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); 914 DCHECK(network_task_runner_->RunsTasksInCurrentSequence());
787 915
788 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size()); 916 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size());
789 http_server_properties_impl_->SetSpdyServers(spdy_servers, true); 917 http_server_properties_impl_->SetSpdyServers(spdy_servers, true);
790 918
791 // Update the cached data and use the new alternative service list from 919 // Update the cached data and use the new alternative service list from
792 // preferences. 920 // preferences.
793 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers", 921 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers",
794 alternative_service_map->size()); 922 alternative_service_map->size());
795 http_server_properties_impl_->SetAlternativeServiceServers( 923 http_server_properties_impl_->SetAlternativeServiceServers(
796 alternative_service_map); 924 alternative_service_map);
797 925
798 http_server_properties_impl_->SetSupportsQuic(last_quic_address); 926 http_server_properties_impl_->SetSupportsQuic(last_quic_address);
799 927
800 http_server_properties_impl_->SetServerNetworkStats(server_network_stats_map); 928 http_server_properties_impl_->SetServerNetworkStats(server_network_stats_map);
801 929
802 UMA_HISTOGRAM_COUNTS_1000("Net.CountOfQuicServerInfos", 930 UMA_HISTOGRAM_COUNTS_1000("Net.CountOfQuicServerInfos",
803 quic_server_info_map->size()); 931 quic_server_info_map->size());
804 932
805 http_server_properties_impl_->SetQuicServerInfoMap(quic_server_info_map); 933 http_server_properties_impl_->SetQuicServerInfoMap(quic_server_info_map);
806 934
935 if (recently_broken_alternative_services) {
936 DCHECK(broken_alternative_service_list);
937 http_server_properties_impl_->AddBrokenAndRecentlyBrokenAlternativeServices(
938 std::move(broken_alternative_service_list),
939 std::move(recently_broken_alternative_services));
940 }
941
807 // Update the prefs with what we have read (delete all corrupted prefs). 942 // Update the prefs with what we have read (delete all corrupted prefs).
808 if (detected_corrupted_prefs) 943 if (detected_corrupted_prefs)
809 ScheduleUpdatePrefsOnNetworkSequence(DETECTED_CORRUPTED_PREFS); 944 ScheduleUpdatePrefsOnNetworkSequence(DETECTED_CORRUPTED_PREFS);
810 } 945 }
811 946
812 // 947 //
813 // Update Preferences with data from the cached data. 948 // Update Preferences with data from the cached data.
814 // 949 //
815 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkSequence( 950 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkSequence(
816 Location location) { 951 Location location) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 continue; 997 continue;
863 } 998 }
864 AlternativeService alternative_service( 999 AlternativeService alternative_service(
865 alternative_service_info.alternative_service); 1000 alternative_service_info.alternative_service);
866 if (!IsAlternateProtocolValid(alternative_service.protocol)) { 1001 if (!IsAlternateProtocolValid(alternative_service.protocol)) {
867 continue; 1002 continue;
868 } 1003 }
869 if (alternative_service.host.empty()) { 1004 if (alternative_service.host.empty()) {
870 alternative_service.host = server.host(); 1005 alternative_service.host = server.host();
871 } 1006 }
872 if (IsAlternativeServiceBroken(alternative_service)) {
873 continue;
874 }
875 notbroken_alternative_service_info_vector.push_back( 1007 notbroken_alternative_service_info_vector.push_back(
876 alternative_service_info); 1008 alternative_service_info);
877 } 1009 }
878 if (notbroken_alternative_service_info_vector.empty()) { 1010 if (notbroken_alternative_service_info_vector.empty()) {
879 continue; 1011 continue;
880 } 1012 }
881 const std::string* canonical_suffix = 1013 const std::string* canonical_suffix =
882 http_server_properties_impl_->GetCanonicalSuffix(server.host()); 1014 http_server_properties_impl_->GetCanonicalSuffix(server.host());
883 if (canonical_suffix != nullptr) { 1015 if (canonical_suffix != nullptr) {
884 if (persisted_map.find(*canonical_suffix) != persisted_map.end()) 1016 if (persisted_map.find(*canonical_suffix) != persisted_map.end())
(...skipping 23 matching lines...) Expand all
908 http_server_properties_impl_->quic_server_info_map(); 1040 http_server_properties_impl_->quic_server_info_map();
909 if (main_quic_server_info_map.size() > 0) { 1041 if (main_quic_server_info_map.size() > 0) {
910 quic_server_info_map = 1042 quic_server_info_map =
911 new QuicServerInfoMap(max_server_configs_stored_in_properties()); 1043 new QuicServerInfoMap(max_server_configs_stored_in_properties());
912 for (const std::pair<const QuicServerId, std::string>& entry : 1044 for (const std::pair<const QuicServerId, std::string>& entry :
913 main_quic_server_info_map) { 1045 main_quic_server_info_map) {
914 quic_server_info_map->Put(entry.first, entry.second); 1046 quic_server_info_map->Put(entry.first, entry.second);
915 } 1047 }
916 } 1048 }
917 1049
1050 // Make copy of list of broken alternative services stored in
1051 // |http_server_properties_impl_|
1052 BrokenAlternativeServiceList* broken_alt_svc_list_copy = nullptr;
1053 const BrokenAlternativeServiceList& broken_alternative_service_list =
1054 http_server_properties_impl_->broken_alternative_service_list();
1055 if (broken_alternative_service_list.size() > 0) {
1056 broken_alt_svc_list_copy = new BrokenAlternativeServiceList();
1057 size_t count = 0;
1058 for (auto it = broken_alternative_service_list.begin();
1059 it != broken_alternative_service_list.end() &&
1060 count < kMaxBrokenAlternativeServicesToPersist;
1061 ++it) {
1062 broken_alt_svc_list_copy->push_back(*it);
1063 ++count;
1064 }
1065 }
1066
1067 // Make copy of RecentlyBrokenAternativeServices stored in
1068 // |http_server_properties_impl_|.
1069 RecentlyBrokenAlternativeServices* recently_broken_alt_svc_copy = nullptr;
1070 const RecentlyBrokenAlternativeServices& recently_broken_alt_services =
1071 http_server_properties_impl_->recently_broken_alternative_services();
1072 if (recently_broken_alt_services.size() > 0) {
1073 recently_broken_alt_svc_copy = new RecentlyBrokenAlternativeServices(
1074 kMaxRecentlyBrokenAlternativeServicesToPersist);
1075 size_t count = 0;
1076 for (auto it = recently_broken_alt_services.rbegin();
1077 it != recently_broken_alt_services.rend() &&
1078 count < kMaxRecentlyBrokenAlternativeServicesToPersist;
1079 ++it) {
1080 recently_broken_alt_svc_copy->Put(it->first, it->second);
1081 ++count;
1082 }
1083 }
1084
918 IPAddress* last_quic_addr = new IPAddress; 1085 IPAddress* last_quic_addr = new IPAddress;
919 http_server_properties_impl_->GetSupportsQuic(last_quic_addr); 1086 http_server_properties_impl_->GetSupportsQuic(last_quic_addr);
920 // Update the preferences on the pref thread. 1087 // Update the preferences on the pref thread.
921 pref_task_runner_->PostTask( 1088 pref_task_runner_->PostTask(
922 FROM_HERE, 1089 FROM_HERE,
923 base::Bind( 1090 base::Bind(
924 &HttpServerPropertiesManager::UpdatePrefsOnPrefThread, pref_weak_ptr_, 1091 &HttpServerPropertiesManager::UpdatePrefsOnPrefThread, pref_weak_ptr_,
925 base::Owned(spdy_server_list), base::Owned(alternative_service_map), 1092 base::Owned(spdy_server_list), base::Owned(alternative_service_map),
926 base::Owned(last_quic_addr), base::Owned(server_network_stats_map), 1093 base::Owned(last_quic_addr), base::Owned(server_network_stats_map),
927 base::Owned(quic_server_info_map), completion)); 1094 base::Owned(quic_server_info_map),
1095 base::Owned(broken_alt_svc_list_copy),
1096 base::Owned(recently_broken_alt_svc_copy), completion));
928 } 1097 }
929 1098
930 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, 1099 // A local or temporary data structure to hold |supports_spdy|, SpdySettings,
931 // AlternativeServiceInfoVector, and SupportsQuic preferences for a server. This 1100 // AlternativeServiceInfoVector, and SupportsQuic preferences for a server. This
932 // is used only in UpdatePrefsOnPrefThread. 1101 // is used only in UpdatePrefsOnPrefThread.
933 struct ServerPref { 1102 struct ServerPref {
934 ServerPref() 1103 ServerPref()
935 : supports_spdy(false), 1104 : supports_spdy(false),
936 settings_map(nullptr), 1105 settings_map(nullptr),
937 alternative_service_info_vector(nullptr), 1106 alternative_service_info_vector(nullptr),
(...skipping 17 matching lines...) Expand all
955 const ServerNetworkStats* server_network_stats; 1124 const ServerNetworkStats* server_network_stats;
956 }; 1125 };
957 1126
958 // All maps and lists are in MRU order. 1127 // All maps and lists are in MRU order.
959 void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( 1128 void HttpServerPropertiesManager::UpdatePrefsOnPrefThread(
960 base::ListValue* spdy_server_list, 1129 base::ListValue* spdy_server_list,
961 AlternativeServiceMap* alternative_service_map, 1130 AlternativeServiceMap* alternative_service_map,
962 IPAddress* last_quic_address, 1131 IPAddress* last_quic_address,
963 ServerNetworkStatsMap* server_network_stats_map, 1132 ServerNetworkStatsMap* server_network_stats_map,
964 QuicServerInfoMap* quic_server_info_map, 1133 QuicServerInfoMap* quic_server_info_map,
1134 BrokenAlternativeServiceList* broken_alternative_service_list,
1135 RecentlyBrokenAlternativeServices* recently_broken_alternative_services,
965 const base::Closure& completion) { 1136 const base::Closure& completion) {
966 typedef base::MRUCache<url::SchemeHostPort, ServerPref> ServerPrefMap; 1137 typedef base::MRUCache<url::SchemeHostPort, ServerPref> ServerPrefMap;
967 ServerPrefMap server_pref_map(ServerPrefMap::NO_AUTO_EVICT); 1138 ServerPrefMap server_pref_map(ServerPrefMap::NO_AUTO_EVICT);
968 1139
969 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); 1140 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence());
970 1141
971 // Add servers that support spdy to server_pref_map in the MRU order. 1142 // 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) { 1143 for (size_t index = spdy_server_list->GetSize(); index > 0; --index) {
973 std::string server_str; 1144 std::string server_str;
974 if (spdy_server_list->GetString(index - 1, &server_str)) { 1145 if (spdy_server_list->GetString(index - 1, &server_str)) {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
1041 1212
1042 http_server_properties_dict.SetWithoutPathExpansion(kServersKey, 1213 http_server_properties_dict.SetWithoutPathExpansion(kServersKey,
1043 std::move(servers_list)); 1214 std::move(servers_list));
1044 SetVersion(&http_server_properties_dict, kVersionNumber); 1215 SetVersion(&http_server_properties_dict, kVersionNumber);
1045 1216
1046 SaveSupportsQuicToPrefs(last_quic_address, &http_server_properties_dict); 1217 SaveSupportsQuicToPrefs(last_quic_address, &http_server_properties_dict);
1047 1218
1048 SaveQuicServerInfoMapToServerPrefs(quic_server_info_map, 1219 SaveQuicServerInfoMapToServerPrefs(quic_server_info_map,
1049 &http_server_properties_dict); 1220 &http_server_properties_dict);
1050 1221
1222 SaveBrokenAlternativeServiceListToPrefs(broken_alternative_service_list,
1223 &http_server_properties_dict);
1224
1225 SaveRecentlyBrokenAlternativeServicesToPrefs(
1226 recently_broken_alternative_services, &http_server_properties_dict);
1227
1051 setting_prefs_ = true; 1228 setting_prefs_ = true;
1052 pref_delegate_->SetServerProperties(http_server_properties_dict); 1229 pref_delegate_->SetServerProperties(http_server_properties_dict);
1053 setting_prefs_ = false; 1230 setting_prefs_ = false;
1054 1231
1055 // Note that |completion| will be fired after we have written everything to 1232 // Note that |completion| will be fired after we have written everything to
1056 // the Preferences, but likely before these changes are serialized to disk. 1233 // the Preferences, but likely before these changes are serialized to disk.
1057 // This is not a problem though, as JSONPrefStore guarantees that this will 1234 // 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. 1235 // happen, pretty soon, and even in the case we shut down immediately.
1059 if (!completion.is_null()) 1236 if (!completion.is_null())
1060 completion.Run(); 1237 completion.Run();
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1137 auto quic_server_pref_dict = base::MakeUnique<base::DictionaryValue>(); 1314 auto quic_server_pref_dict = base::MakeUnique<base::DictionaryValue>();
1138 quic_server_pref_dict->SetStringWithoutPathExpansion(kServerInfoKey, 1315 quic_server_pref_dict->SetStringWithoutPathExpansion(kServerInfoKey,
1139 entry.second); 1316 entry.second);
1140 quic_servers_dict->SetWithoutPathExpansion( 1317 quic_servers_dict->SetWithoutPathExpansion(
1141 server_id.ToString(), std::move(quic_server_pref_dict)); 1318 server_id.ToString(), std::move(quic_server_pref_dict));
1142 } 1319 }
1143 http_server_properties_dict->SetWithoutPathExpansion( 1320 http_server_properties_dict->SetWithoutPathExpansion(
1144 kQuicServers, std::move(quic_servers_dict)); 1321 kQuicServers, std::move(quic_servers_dict));
1145 } 1322 }
1146 1323
1324 void HttpServerPropertiesManager::SaveBrokenAlternativeServiceListToPrefs(
1325 const BrokenAlternativeServiceList* broken_alternative_service_list,
1326 base::DictionaryValue* http_server_properties_dict) {
1327 if (!broken_alternative_service_list)
1328 return;
1329
1330 base::ListValue* broken_alt_services_list = new base::ListValue;
1331 for (const auto& entry : *broken_alternative_service_list) {
1332 const AlternativeService& alt_service = entry.first;
1333 const base::TimeTicks& expiration_time_ticks = entry.second;
1334 // Convert expiration from TimeTicks to Time to time_t
1335 time_t expiration_time_t =
1336 (base::Time::Now() + (expiration_time_ticks - clock_->NowTicks()))
1337 .ToTimeT();
1338 int64_t expiration_int64 = static_cast<int64_t>(expiration_time_t);
1339 std::unique_ptr<base::DictionaryValue> broken_alt_service_entry_dict =
1340 base::MakeUnique<base::DictionaryValue>();
1341 broken_alt_service_entry_dict->SetStringWithoutPathExpansion(
1342 alt_service.ToString(), base::Int64ToString(expiration_int64));
1343 broken_alt_services_list->Append(std::move(broken_alt_service_entry_dict));
1344 }
1345 http_server_properties_dict->SetWithoutPathExpansion(
1346 kBrokenAlternativeServicesKey,
1347 std::unique_ptr<base::Value>(broken_alt_services_list));
1348 }
1349
1350 void HttpServerPropertiesManager::SaveRecentlyBrokenAlternativeServicesToPrefs(
1351 const RecentlyBrokenAlternativeServices*
1352 recently_broken_alternative_services,
1353 base::DictionaryValue* http_server_properties_dict) {
1354 if (!recently_broken_alternative_services)
1355 return;
1356
1357 // JSON list will have entries ordered from least recent to most recent.
1358 base::ListValue* recently_broken_alt_services_list = new base::ListValue;
1359 for (auto it = recently_broken_alternative_services->rbegin();
1360 it != recently_broken_alternative_services->rend(); ++it) {
1361 const AlternativeService& alt_service = it->first;
1362 int broken_count = it->second;
1363 std::unique_ptr<base::DictionaryValue> broken_alt_service_entry_dict =
1364 base::MakeUnique<base::DictionaryValue>();
1365 broken_alt_service_entry_dict->SetIntegerWithoutPathExpansion(
1366 alt_service.ToString(), broken_count);
1367 recently_broken_alt_services_list->Append(
1368 std::move(broken_alt_service_entry_dict));
1369 }
1370 http_server_properties_dict->SetWithoutPathExpansion(
1371 kRecentlyBrokenAlternativeServicesKey,
1372 std::unique_ptr<base::Value>(recently_broken_alt_services_list));
1373 }
1374
1147 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { 1375 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() {
1148 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); 1376 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence());
1149 if (!setting_prefs_) 1377 if (!setting_prefs_)
1150 ScheduleUpdateCacheOnPrefThread(); 1378 ScheduleUpdateCacheOnPrefThread();
1151 } 1379 }
1152 1380
1153 void HttpServerPropertiesManager::SetInitialized() { 1381 void HttpServerPropertiesManager::SetInitialized() {
1154 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); 1382 DCHECK(network_task_runner_->RunsTasksInCurrentSequence());
1155 is_initialized_ = true; 1383 is_initialized_ = true;
1156 } 1384 }
1157 1385
1386 void HttpServerPropertiesManager::SetTickClockForTesting(
1387 base::TickClock* clock) {
1388 DCHECK(clock);
1389 DCHECK(http_server_properties_impl_);
1390 clock_ = clock;
1391 http_server_properties_impl_->SetBrokenAlternativeServicesTickClockForTesting(
1392 clock);
1393 }
1394
1158 } // namespace net 1395 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698