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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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; | |
Zhongyi Shi
2017/06/16 04:17:09
We used to decide the number of Alt Svc to persist
wangyix1
2017/06/16 18:41:15
Done.
| |
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) |
88 : HttpServerPropertiesManager(pref_delegate, | |
89 pref_task_runner, | |
90 network_task_runner, | |
91 nullptr) {} | |
92 | |
93 HttpServerPropertiesManager::HttpServerPropertiesManager( | |
94 PrefDelegate* pref_delegate, | |
95 scoped_refptr<base::SingleThreadTaskRunner> pref_task_runner, | |
96 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, | |
97 base::TickClock* clock) | |
79 : pref_task_runner_(std::move(pref_task_runner)), | 98 : pref_task_runner_(std::move(pref_task_runner)), |
80 pref_delegate_(pref_delegate), | 99 pref_delegate_(pref_delegate), |
81 setting_prefs_(false), | 100 setting_prefs_(false), |
101 clock_(clock ? clock : &default_clock_), | |
82 is_initialized_(false), | 102 is_initialized_(false), |
83 network_task_runner_(std::move(network_task_runner)) { | 103 network_task_runner_(std::move(network_task_runner)) { |
84 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); | 104 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); |
85 DCHECK(pref_delegate_); | 105 DCHECK(pref_delegate_); |
106 DCHECK(clock_); | |
86 pref_weak_ptr_factory_.reset( | 107 pref_weak_ptr_factory_.reset( |
87 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); | 108 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); |
88 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr(); | 109 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr(); |
89 pref_cache_update_timer_.reset(new base::OneShotTimer); | 110 pref_cache_update_timer_.reset(new base::OneShotTimer); |
90 pref_cache_update_timer_->SetTaskRunner(pref_task_runner_); | 111 pref_cache_update_timer_->SetTaskRunner(pref_task_runner_); |
91 pref_delegate_->StartListeningForUpdates( | 112 pref_delegate_->StartListeningForUpdates( |
92 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, | 113 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, |
93 base::Unretained(this))); | 114 base::Unretained(this))); |
94 } | 115 } |
95 | 116 |
96 HttpServerPropertiesManager::~HttpServerPropertiesManager() { | 117 HttpServerPropertiesManager::~HttpServerPropertiesManager() { |
97 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); | 118 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); |
98 network_weak_ptr_factory_.reset(); | 119 network_weak_ptr_factory_.reset(); |
99 } | 120 } |
100 | 121 |
101 void HttpServerPropertiesManager::InitializeOnNetworkSequence() { | 122 void HttpServerPropertiesManager::InitializeOnNetworkSequence() { |
102 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); | 123 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); |
103 | 124 |
104 network_weak_ptr_factory_.reset( | 125 network_weak_ptr_factory_.reset( |
105 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); | 126 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); |
106 http_server_properties_impl_.reset(new HttpServerPropertiesImpl()); | 127 http_server_properties_impl_.reset(new HttpServerPropertiesImpl(clock_)); |
107 | 128 |
108 network_prefs_update_timer_.reset(new base::OneShotTimer); | 129 network_prefs_update_timer_.reset(new base::OneShotTimer); |
109 network_prefs_update_timer_->SetTaskRunner(network_task_runner_); | 130 network_prefs_update_timer_->SetTaskRunner(network_task_runner_); |
110 // UpdateCacheFromPrefsOnPrefSequence() will post a task to network thread to | 131 // UpdateCacheFromPrefsOnPrefSequence() will post a task to network thread to |
111 // update server properties. SetInitialized() will be run after that task is | 132 // update server properties. SetInitialized() will be run after that task is |
112 // run as |network_task_runner_| is single threaded. | 133 // run as |network_task_runner_| is single threaded. |
113 pref_task_runner_->PostTaskAndReply( | 134 pref_task_runner_->PostTaskAndReply( |
114 FROM_HERE, | 135 FROM_HERE, |
115 base::Bind( | 136 base::Bind( |
116 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefSequence, | 137 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefSequence, |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
507 detected_corrupted_prefs = true; | 528 detected_corrupted_prefs = true; |
508 } | 529 } |
509 } | 530 } |
510 } | 531 } |
511 | 532 |
512 if (!AddToQuicServerInfoMap(http_server_properties_dict, | 533 if (!AddToQuicServerInfoMap(http_server_properties_dict, |
513 quic_server_info_map.get())) { | 534 quic_server_info_map.get())) { |
514 detected_corrupted_prefs = true; | 535 detected_corrupted_prefs = true; |
515 } | 536 } |
516 | 537 |
538 std::unique_ptr<RecentlyBrokenAlternativeServices> | |
539 recently_broken_alternative_services; | |
540 const base::ListValue* recently_broken_alt_svc_list = nullptr; | |
541 | |
542 std::unique_ptr<BrokenAlternativeServiceList> broken_alternative_service_list; | |
543 const base::ListValue* broken_alt_svc_list = nullptr; | |
544 | |
545 // Read recently broken alternative services if they exist. | |
546 if (http_server_properties_dict.GetListWithoutPathExpansion( | |
547 kRecentlyBrokenAlternativeServicesKey, | |
548 &recently_broken_alt_svc_list)) { | |
549 recently_broken_alternative_services.reset( | |
550 new RecentlyBrokenAlternativeServices( | |
551 RecentlyBrokenAlternativeServices::NO_AUTO_EVICT)); | |
552 | |
553 for (auto it = recently_broken_alt_svc_list->begin(); | |
554 it != recently_broken_alt_svc_list->end(); ++it) { | |
555 const base::DictionaryValue* entry_dict = nullptr; | |
556 if (!it->GetAsDictionary(&entry_dict) || | |
557 !AddRecentlyBrokenAlternativeServiceData( | |
558 entry_dict, recently_broken_alternative_services.get())) { | |
559 DVLOG(1) << "Malformed http_server_properties for recently broken " | |
560 << "alternative services list."; | |
561 detected_corrupted_prefs = true; | |
562 } | |
563 } | |
564 | |
565 // Read broken alternative services if they exist | |
566 if (http_server_properties_dict.GetListWithoutPathExpansion( | |
567 kBrokenAlternativeServicesKey, &broken_alt_svc_list)) { | |
568 broken_alternative_service_list.reset(new BrokenAlternativeServiceList()); | |
569 | |
570 for (auto it = broken_alt_svc_list->begin(); | |
571 it != broken_alt_svc_list->end(); ++it) { | |
572 const base::DictionaryValue* entry_dict = nullptr; | |
573 AlternativeService alternative_service; | |
574 base::TimeTicks expiration_time_ticks; | |
575 if (!it->GetAsDictionary(&entry_dict) || | |
576 !ExtractBrokenAlternativeServiceData( | |
577 entry_dict, &alternative_service, &expiration_time_ticks)) { | |
578 DVLOG(1) << "Malformed http_server_properties for broken alternative " | |
579 << "services list."; | |
580 detected_corrupted_prefs = true; | |
581 continue; | |
582 } | |
583 if (recently_broken_alternative_services->Peek(alternative_service) != | |
584 recently_broken_alternative_services->end()) { | |
585 broken_alternative_service_list->push_back( | |
586 std::make_pair(alternative_service, expiration_time_ticks)); | |
587 } else { | |
588 DVLOG(1) << "Broken alternative service does not have an entry in " | |
589 << "map of recently broken alternative services. " | |
590 << "Will not be added."; | |
591 } | |
592 } | |
593 } | |
594 } | |
595 | |
517 network_task_runner_->PostTask( | 596 network_task_runner_->PostTask( |
518 FROM_HERE, | 597 FROM_HERE, |
519 base::Bind( | 598 base::Bind( |
520 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkSequence, | 599 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkSequence, |
521 base::Unretained(this), base::Owned(spdy_servers.release()), | 600 base::Unretained(this), base::Owned(spdy_servers.release()), |
522 base::Owned(alternative_service_map.release()), base::Owned(addr), | 601 base::Owned(alternative_service_map.release()), base::Owned(addr), |
523 base::Owned(server_network_stats_map.release()), | 602 base::Owned(server_network_stats_map.release()), |
524 base::Owned(quic_server_info_map.release()), | 603 base::Owned(quic_server_info_map.release()), |
604 base::Passed(&broken_alternative_service_list), | |
605 base::Passed(&recently_broken_alternative_services), | |
525 detected_corrupted_prefs)); | 606 detected_corrupted_prefs)); |
526 } | 607 } |
527 | 608 |
609 bool HttpServerPropertiesManager::ExtractBrokenAlternativeServiceData( | |
610 const base::DictionaryValue* broken_alternative_service_dict, | |
611 AlternativeService* alternative_service, | |
612 base::TimeTicks* expiration_time_ticks) { | |
Zhongyi Shi
2017/06/16 04:17:09
Can we just change this method to take DictionaryV
wangyix1
2017/06/16 18:41:15
The reason for this can be seen in how this functi
| |
613 DCHECK(broken_alternative_service_dict); | |
614 | |
615 if (broken_alternative_service_dict->size() != 1u) | |
616 return false; | |
617 | |
618 base::DictionaryValue::Iterator it(*broken_alternative_service_dict); | |
619 | |
620 const std::string& alternative_service_string = it.key(); | |
621 if (!AlternativeService::FromString(alternative_service_string, | |
622 alternative_service)) | |
623 return false; | |
624 | |
625 int64_t expiration_int64; | |
626 std::string time_ticks_string; | |
627 if (!it.value().GetAsString(&time_ticks_string)) | |
628 return false; | |
629 if (!base::StringToInt64(time_ticks_string, &expiration_int64)) | |
630 return false; | |
631 time_t expiration_time_t = static_cast<time_t>(expiration_int64); | |
632 // Convert expiration from time_t to Time to TimeTicks | |
633 *expiration_time_ticks = | |
634 clock_->NowTicks() + | |
635 (base::Time::FromTimeT(expiration_time_t) - base::Time::Now()); | |
636 | |
637 return true; | |
638 } | |
639 | |
640 bool HttpServerPropertiesManager::AddRecentlyBrokenAlternativeServiceData( | |
641 const base::DictionaryValue* recently_broken_alternative_service_dict, | |
642 RecentlyBrokenAlternativeServices* recently_broken_alternative_services) { | |
643 DCHECK(recently_broken_alternative_service_dict); | |
644 | |
645 if (recently_broken_alternative_service_dict->size() != 1u) | |
646 return false; | |
647 | |
648 base::DictionaryValue::Iterator it(*recently_broken_alternative_service_dict); | |
649 | |
650 AlternativeService alt_service; | |
651 const std::string& alt_service_string = it.key(); | |
652 if (!AlternativeService::FromString(alt_service_string, &alt_service)) | |
653 return false; | |
654 | |
655 int broken_count; | |
656 if (!it.value().GetAsInteger(&broken_count)) | |
657 return false; | |
658 | |
659 recently_broken_alternative_services->Put(alt_service, broken_count); | |
660 return true; | |
661 } | |
662 | |
528 bool HttpServerPropertiesManager::AddServersData( | 663 bool HttpServerPropertiesManager::AddServersData( |
529 const base::DictionaryValue& servers_dict, | 664 const base::DictionaryValue& servers_dict, |
530 ServerList* spdy_servers, | 665 ServerList* spdy_servers, |
531 AlternativeServiceMap* alternative_service_map, | 666 AlternativeServiceMap* alternative_service_map, |
532 ServerNetworkStatsMap* network_stats_map, | 667 ServerNetworkStatsMap* network_stats_map, |
533 int version) { | 668 int version) { |
534 for (base::DictionaryValue::Iterator it(servers_dict); !it.IsAtEnd(); | 669 for (base::DictionaryValue::Iterator it(servers_dict); !it.IsAtEnd(); |
535 it.Advance()) { | 670 it.Advance()) { |
536 // Get server's scheme/host/pair. | 671 // Get server's scheme/host/pair. |
537 const std::string& server_str = it.key(); | 672 const std::string& server_str = it.key(); |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
773 } | 908 } |
774 return !detected_corrupted_prefs; | 909 return !detected_corrupted_prefs; |
775 } | 910 } |
776 | 911 |
777 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkSequence( | 912 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkSequence( |
778 ServerList* spdy_servers, | 913 ServerList* spdy_servers, |
779 AlternativeServiceMap* alternative_service_map, | 914 AlternativeServiceMap* alternative_service_map, |
780 IPAddress* last_quic_address, | 915 IPAddress* last_quic_address, |
781 ServerNetworkStatsMap* server_network_stats_map, | 916 ServerNetworkStatsMap* server_network_stats_map, |
782 QuicServerInfoMap* quic_server_info_map, | 917 QuicServerInfoMap* quic_server_info_map, |
918 std::unique_ptr<BrokenAlternativeServiceList> | |
919 broken_alternative_service_list, | |
920 std::unique_ptr<RecentlyBrokenAlternativeServices> | |
921 recently_broken_alternative_services, | |
783 bool detected_corrupted_prefs) { | 922 bool detected_corrupted_prefs) { |
784 // Preferences have the master data because admins might have pushed new | 923 // Preferences have the master data because admins might have pushed new |
785 // preferences. Update the cached data with new data from preferences. | 924 // preferences. Update the cached data with new data from preferences. |
786 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); | 925 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); |
787 | 926 |
788 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size()); | 927 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size()); |
789 http_server_properties_impl_->SetSpdyServers(spdy_servers, true); | 928 http_server_properties_impl_->SetSpdyServers(spdy_servers, true); |
790 | 929 |
791 // Update the cached data and use the new alternative service list from | 930 // Update the cached data and use the new alternative service list from |
792 // preferences. | 931 // preferences. |
793 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers", | 932 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers", |
794 alternative_service_map->size()); | 933 alternative_service_map->size()); |
795 http_server_properties_impl_->SetAlternativeServiceServers( | 934 http_server_properties_impl_->SetAlternativeServiceServers( |
796 alternative_service_map); | 935 alternative_service_map); |
797 | 936 |
798 http_server_properties_impl_->SetSupportsQuic(last_quic_address); | 937 http_server_properties_impl_->SetSupportsQuic(last_quic_address); |
799 | 938 |
800 http_server_properties_impl_->SetServerNetworkStats(server_network_stats_map); | 939 http_server_properties_impl_->SetServerNetworkStats(server_network_stats_map); |
801 | 940 |
802 UMA_HISTOGRAM_COUNTS_1000("Net.CountOfQuicServerInfos", | 941 UMA_HISTOGRAM_COUNTS_1000("Net.CountOfQuicServerInfos", |
803 quic_server_info_map->size()); | 942 quic_server_info_map->size()); |
804 | 943 |
805 http_server_properties_impl_->SetQuicServerInfoMap(quic_server_info_map); | 944 http_server_properties_impl_->SetQuicServerInfoMap(quic_server_info_map); |
806 | 945 |
946 if (recently_broken_alternative_services) { | |
947 DCHECK(broken_alternative_service_list); | |
948 http_server_properties_impl_->SetBrokenAndRecentlyBrokenAlternativeServices( | |
949 std::move(broken_alternative_service_list), | |
950 std::move(recently_broken_alternative_services)); | |
951 } | |
952 | |
807 // Update the prefs with what we have read (delete all corrupted prefs). | 953 // Update the prefs with what we have read (delete all corrupted prefs). |
808 if (detected_corrupted_prefs) | 954 if (detected_corrupted_prefs) |
809 ScheduleUpdatePrefsOnNetworkSequence(DETECTED_CORRUPTED_PREFS); | 955 ScheduleUpdatePrefsOnNetworkSequence(DETECTED_CORRUPTED_PREFS); |
810 } | 956 } |
811 | 957 |
812 // | 958 // |
813 // Update Preferences with data from the cached data. | 959 // Update Preferences with data from the cached data. |
814 // | 960 // |
815 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkSequence( | 961 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkSequence( |
816 Location location) { | 962 Location location) { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
862 continue; | 1008 continue; |
863 } | 1009 } |
864 AlternativeService alternative_service( | 1010 AlternativeService alternative_service( |
865 alternative_service_info.alternative_service()); | 1011 alternative_service_info.alternative_service()); |
866 if (!IsAlternateProtocolValid(alternative_service.protocol)) { | 1012 if (!IsAlternateProtocolValid(alternative_service.protocol)) { |
867 continue; | 1013 continue; |
868 } | 1014 } |
869 if (alternative_service.host.empty()) { | 1015 if (alternative_service.host.empty()) { |
870 alternative_service.host = server.host(); | 1016 alternative_service.host = server.host(); |
871 } | 1017 } |
872 if (IsAlternativeServiceBroken(alternative_service)) { | |
873 continue; | |
874 } | |
875 notbroken_alternative_service_info_vector.push_back( | 1018 notbroken_alternative_service_info_vector.push_back( |
876 alternative_service_info); | 1019 alternative_service_info); |
877 } | 1020 } |
878 if (notbroken_alternative_service_info_vector.empty()) { | 1021 if (notbroken_alternative_service_info_vector.empty()) { |
879 continue; | 1022 continue; |
880 } | 1023 } |
881 const std::string* canonical_suffix = | 1024 const std::string* canonical_suffix = |
882 http_server_properties_impl_->GetCanonicalSuffix(server.host()); | 1025 http_server_properties_impl_->GetCanonicalSuffix(server.host()); |
883 if (canonical_suffix != nullptr) { | 1026 if (canonical_suffix != nullptr) { |
884 if (persisted_map.find(*canonical_suffix) != persisted_map.end()) | 1027 if (persisted_map.find(*canonical_suffix) != persisted_map.end()) |
(...skipping 23 matching lines...) Expand all Loading... | |
908 http_server_properties_impl_->quic_server_info_map(); | 1051 http_server_properties_impl_->quic_server_info_map(); |
909 if (main_quic_server_info_map.size() > 0) { | 1052 if (main_quic_server_info_map.size() > 0) { |
910 quic_server_info_map = | 1053 quic_server_info_map = |
911 new QuicServerInfoMap(max_server_configs_stored_in_properties()); | 1054 new QuicServerInfoMap(max_server_configs_stored_in_properties()); |
912 for (const std::pair<const QuicServerId, std::string>& entry : | 1055 for (const std::pair<const QuicServerId, std::string>& entry : |
913 main_quic_server_info_map) { | 1056 main_quic_server_info_map) { |
914 quic_server_info_map->Put(entry.first, entry.second); | 1057 quic_server_info_map->Put(entry.first, entry.second); |
915 } | 1058 } |
916 } | 1059 } |
917 | 1060 |
1061 // Make copy of list of broken alternative services stored in | |
1062 // |http_server_properties_impl_| | |
1063 BrokenAlternativeServiceList* broken_alt_svc_list_copy = nullptr; | |
1064 const BrokenAlternativeServiceList& broken_alternative_service_list = | |
1065 http_server_properties_impl_->broken_alternative_service_list(); | |
1066 if (broken_alternative_service_list.size() > 0) { | |
1067 broken_alt_svc_list_copy = new BrokenAlternativeServiceList(); | |
1068 size_t count = 0; | |
1069 for (auto it = broken_alternative_service_list.begin(); | |
1070 it != broken_alternative_service_list.end() && | |
1071 count < kMaxBrokenAlternativeServicesToPersist; | |
1072 ++it) { | |
1073 broken_alt_svc_list_copy->push_back(*it); | |
1074 ++count; | |
1075 } | |
1076 } | |
1077 | |
1078 // Make copy of RecentlyBrokenAternativeServices stored in | |
1079 // |http_server_properties_impl_|. | |
1080 RecentlyBrokenAlternativeServices* recently_broken_alt_svc_copy = nullptr; | |
1081 const RecentlyBrokenAlternativeServices& recently_broken_alt_services = | |
1082 http_server_properties_impl_->recently_broken_alternative_services(); | |
1083 if (recently_broken_alt_services.size() > 0) { | |
1084 recently_broken_alt_svc_copy = new RecentlyBrokenAlternativeServices( | |
1085 kMaxRecentlyBrokenAlternativeServicesToPersist); | |
1086 size_t count = 0; | |
1087 for (auto it = recently_broken_alt_services.rbegin(); | |
1088 it != recently_broken_alt_services.rend() && | |
1089 count < kMaxRecentlyBrokenAlternativeServicesToPersist; | |
1090 ++it) { | |
1091 recently_broken_alt_svc_copy->Put(it->first, it->second); | |
1092 ++count; | |
1093 } | |
1094 } | |
1095 | |
918 IPAddress* last_quic_addr = new IPAddress; | 1096 IPAddress* last_quic_addr = new IPAddress; |
919 http_server_properties_impl_->GetSupportsQuic(last_quic_addr); | 1097 http_server_properties_impl_->GetSupportsQuic(last_quic_addr); |
920 // Update the preferences on the pref thread. | 1098 // Update the preferences on the pref thread. |
921 pref_task_runner_->PostTask( | 1099 pref_task_runner_->PostTask( |
922 FROM_HERE, | 1100 FROM_HERE, |
923 base::Bind( | 1101 base::Bind( |
924 &HttpServerPropertiesManager::UpdatePrefsOnPrefThread, pref_weak_ptr_, | 1102 &HttpServerPropertiesManager::UpdatePrefsOnPrefThread, pref_weak_ptr_, |
925 base::Owned(spdy_server_list), base::Owned(alternative_service_map), | 1103 base::Owned(spdy_server_list), base::Owned(alternative_service_map), |
926 base::Owned(last_quic_addr), base::Owned(server_network_stats_map), | 1104 base::Owned(last_quic_addr), base::Owned(server_network_stats_map), |
927 base::Owned(quic_server_info_map), completion)); | 1105 base::Owned(quic_server_info_map), |
1106 base::Owned(broken_alt_svc_list_copy), | |
1107 base::Owned(recently_broken_alt_svc_copy), completion)); | |
928 } | 1108 } |
929 | 1109 |
930 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, | 1110 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, |
931 // AlternativeServiceInfoVector, and SupportsQuic preferences for a server. This | 1111 // AlternativeServiceInfoVector, and SupportsQuic preferences for a server. This |
932 // is used only in UpdatePrefsOnPrefThread. | 1112 // is used only in UpdatePrefsOnPrefThread. |
933 struct ServerPref { | 1113 struct ServerPref { |
934 ServerPref() | 1114 ServerPref() |
935 : supports_spdy(false), | 1115 : supports_spdy(false), |
936 settings_map(nullptr), | 1116 settings_map(nullptr), |
937 alternative_service_info_vector(nullptr), | 1117 alternative_service_info_vector(nullptr), |
(...skipping 17 matching lines...) Expand all Loading... | |
955 const ServerNetworkStats* server_network_stats; | 1135 const ServerNetworkStats* server_network_stats; |
956 }; | 1136 }; |
957 | 1137 |
958 // All maps and lists are in MRU order. | 1138 // All maps and lists are in MRU order. |
959 void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( | 1139 void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( |
960 base::ListValue* spdy_server_list, | 1140 base::ListValue* spdy_server_list, |
961 AlternativeServiceMap* alternative_service_map, | 1141 AlternativeServiceMap* alternative_service_map, |
962 IPAddress* last_quic_address, | 1142 IPAddress* last_quic_address, |
963 ServerNetworkStatsMap* server_network_stats_map, | 1143 ServerNetworkStatsMap* server_network_stats_map, |
964 QuicServerInfoMap* quic_server_info_map, | 1144 QuicServerInfoMap* quic_server_info_map, |
1145 BrokenAlternativeServiceList* broken_alternative_service_list, | |
1146 RecentlyBrokenAlternativeServices* recently_broken_alternative_services, | |
965 const base::Closure& completion) { | 1147 const base::Closure& completion) { |
966 typedef base::MRUCache<url::SchemeHostPort, ServerPref> ServerPrefMap; | 1148 typedef base::MRUCache<url::SchemeHostPort, ServerPref> ServerPrefMap; |
967 ServerPrefMap server_pref_map(ServerPrefMap::NO_AUTO_EVICT); | 1149 ServerPrefMap server_pref_map(ServerPrefMap::NO_AUTO_EVICT); |
968 | 1150 |
969 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); | 1151 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); |
970 | 1152 |
971 // Add servers that support spdy to server_pref_map in the MRU order. | 1153 // 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) { | 1154 for (size_t index = spdy_server_list->GetSize(); index > 0; --index) { |
973 std::string server_str; | 1155 std::string server_str; |
974 if (spdy_server_list->GetString(index - 1, &server_str)) { | 1156 if (spdy_server_list->GetString(index - 1, &server_str)) { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1041 | 1223 |
1042 http_server_properties_dict.SetWithoutPathExpansion(kServersKey, | 1224 http_server_properties_dict.SetWithoutPathExpansion(kServersKey, |
1043 std::move(servers_list)); | 1225 std::move(servers_list)); |
1044 SetVersion(&http_server_properties_dict, kVersionNumber); | 1226 SetVersion(&http_server_properties_dict, kVersionNumber); |
1045 | 1227 |
1046 SaveSupportsQuicToPrefs(last_quic_address, &http_server_properties_dict); | 1228 SaveSupportsQuicToPrefs(last_quic_address, &http_server_properties_dict); |
1047 | 1229 |
1048 SaveQuicServerInfoMapToServerPrefs(quic_server_info_map, | 1230 SaveQuicServerInfoMapToServerPrefs(quic_server_info_map, |
1049 &http_server_properties_dict); | 1231 &http_server_properties_dict); |
1050 | 1232 |
1233 SaveBrokenAlternativeServiceListToPrefs(broken_alternative_service_list, | |
1234 &http_server_properties_dict); | |
1235 | |
1236 SaveRecentlyBrokenAlternativeServicesToPrefs( | |
1237 recently_broken_alternative_services, &http_server_properties_dict); | |
1238 | |
1051 setting_prefs_ = true; | 1239 setting_prefs_ = true; |
1052 pref_delegate_->SetServerProperties(http_server_properties_dict); | 1240 pref_delegate_->SetServerProperties(http_server_properties_dict); |
1053 setting_prefs_ = false; | 1241 setting_prefs_ = false; |
1054 | 1242 |
1055 // Note that |completion| will be fired after we have written everything to | 1243 // Note that |completion| will be fired after we have written everything to |
1056 // the Preferences, but likely before these changes are serialized to disk. | 1244 // the Preferences, but likely before these changes are serialized to disk. |
1057 // This is not a problem though, as JSONPrefStore guarantees that this will | 1245 // 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. | 1246 // happen, pretty soon, and even in the case we shut down immediately. |
1059 if (!completion.is_null()) | 1247 if (!completion.is_null()) |
1060 completion.Run(); | 1248 completion.Run(); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1137 auto quic_server_pref_dict = base::MakeUnique<base::DictionaryValue>(); | 1325 auto quic_server_pref_dict = base::MakeUnique<base::DictionaryValue>(); |
1138 quic_server_pref_dict->SetStringWithoutPathExpansion(kServerInfoKey, | 1326 quic_server_pref_dict->SetStringWithoutPathExpansion(kServerInfoKey, |
1139 entry.second); | 1327 entry.second); |
1140 quic_servers_dict->SetWithoutPathExpansion( | 1328 quic_servers_dict->SetWithoutPathExpansion( |
1141 server_id.ToString(), std::move(quic_server_pref_dict)); | 1329 server_id.ToString(), std::move(quic_server_pref_dict)); |
1142 } | 1330 } |
1143 http_server_properties_dict->SetWithoutPathExpansion( | 1331 http_server_properties_dict->SetWithoutPathExpansion( |
1144 kQuicServers, std::move(quic_servers_dict)); | 1332 kQuicServers, std::move(quic_servers_dict)); |
1145 } | 1333 } |
1146 | 1334 |
1335 void HttpServerPropertiesManager::SaveBrokenAlternativeServiceListToPrefs( | |
1336 const BrokenAlternativeServiceList* broken_alternative_service_list, | |
1337 base::DictionaryValue* http_server_properties_dict) { | |
1338 if (!broken_alternative_service_list) | |
1339 return; | |
1340 | |
1341 base::ListValue* broken_alt_services_list = new base::ListValue; | |
1342 for (const auto& entry : *broken_alternative_service_list) { | |
1343 const AlternativeService& alt_service = entry.first; | |
1344 const base::TimeTicks& expiration_time_ticks = entry.second; | |
1345 // Convert expiration from TimeTicks to Time to time_t | |
1346 time_t expiration_time_t = | |
1347 (base::Time::Now() + (expiration_time_ticks - clock_->NowTicks())) | |
1348 .ToTimeT(); | |
1349 int64_t expiration_int64 = static_cast<int64_t>(expiration_time_t); | |
1350 std::unique_ptr<base::DictionaryValue> broken_alt_service_entry_dict = | |
1351 base::MakeUnique<base::DictionaryValue>(); | |
1352 broken_alt_service_entry_dict->SetStringWithoutPathExpansion( | |
1353 alt_service.ToString(), base::Int64ToString(expiration_int64)); | |
1354 broken_alt_services_list->Append(std::move(broken_alt_service_entry_dict)); | |
1355 } | |
1356 http_server_properties_dict->SetWithoutPathExpansion( | |
1357 kBrokenAlternativeServicesKey, | |
1358 std::unique_ptr<base::Value>(broken_alt_services_list)); | |
1359 } | |
1360 | |
1361 void HttpServerPropertiesManager::SaveRecentlyBrokenAlternativeServicesToPrefs( | |
1362 const RecentlyBrokenAlternativeServices* | |
1363 recently_broken_alternative_services, | |
1364 base::DictionaryValue* http_server_properties_dict) { | |
1365 if (!recently_broken_alternative_services) | |
1366 return; | |
1367 | |
1368 // JSON list will have entries ordered from least recent to most recent. | |
1369 base::ListValue* recently_broken_alt_services_list = new base::ListValue; | |
1370 for (auto it = recently_broken_alternative_services->rbegin(); | |
1371 it != recently_broken_alternative_services->rend(); ++it) { | |
1372 const AlternativeService& alt_service = it->first; | |
1373 int broken_count = it->second; | |
1374 std::unique_ptr<base::DictionaryValue> broken_alt_service_entry_dict = | |
1375 base::MakeUnique<base::DictionaryValue>(); | |
1376 broken_alt_service_entry_dict->SetIntegerWithoutPathExpansion( | |
1377 alt_service.ToString(), broken_count); | |
1378 recently_broken_alt_services_list->Append( | |
1379 std::move(broken_alt_service_entry_dict)); | |
1380 } | |
1381 http_server_properties_dict->SetWithoutPathExpansion( | |
1382 kRecentlyBrokenAlternativeServicesKey, | |
1383 std::unique_ptr<base::Value>(recently_broken_alt_services_list)); | |
1384 } | |
1385 | |
1147 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { | 1386 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { |
1148 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); | 1387 DCHECK(pref_task_runner_->RunsTasksInCurrentSequence()); |
1149 if (!setting_prefs_) | 1388 if (!setting_prefs_) |
1150 ScheduleUpdateCacheOnPrefThread(); | 1389 ScheduleUpdateCacheOnPrefThread(); |
1151 } | 1390 } |
1152 | 1391 |
1153 void HttpServerPropertiesManager::SetInitialized() { | 1392 void HttpServerPropertiesManager::SetInitialized() { |
1154 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); | 1393 DCHECK(network_task_runner_->RunsTasksInCurrentSequence()); |
1155 is_initialized_ = true; | 1394 is_initialized_ = true; |
1156 } | 1395 } |
1157 | 1396 |
1158 } // namespace net | 1397 } // namespace net |
OLD | NEW |