| 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 ScheduleUpdatePrefsOnNetworkThread(HTTP_11_REQUIRED); | 182 ScheduleUpdatePrefsOnNetworkThread(HTTP_11_REQUIRED); |
| 183 } | 183 } |
| 184 | 184 |
| 185 void HttpServerPropertiesManager::MaybeForceHTTP11(const HostPortPair& server, | 185 void HttpServerPropertiesManager::MaybeForceHTTP11(const HostPortPair& server, |
| 186 SSLConfig* ssl_config) { | 186 SSLConfig* ssl_config) { |
| 187 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 187 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 188 http_server_properties_impl_->MaybeForceHTTP11(server, ssl_config); | 188 http_server_properties_impl_->MaybeForceHTTP11(server, ssl_config); |
| 189 } | 189 } |
| 190 | 190 |
| 191 AlternativeServiceVector HttpServerPropertiesManager::GetAlternativeServices( | 191 AlternativeServiceVector HttpServerPropertiesManager::GetAlternativeServices( |
| 192 const HostPortPair& origin) { | 192 const url::SchemeHostPort& origin) { |
| 193 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 193 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 194 return http_server_properties_impl_->GetAlternativeServices(origin); | 194 return http_server_properties_impl_->GetAlternativeServices(origin); |
| 195 } | 195 } |
| 196 | 196 |
| 197 bool HttpServerPropertiesManager::SetAlternativeService( | 197 bool HttpServerPropertiesManager::SetAlternativeService( |
| 198 const HostPortPair& origin, | 198 const url::SchemeHostPort& origin, |
| 199 const AlternativeService& alternative_service, | 199 const AlternativeService& alternative_service, |
| 200 base::Time expiration) { | 200 base::Time expiration) { |
| 201 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 201 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 202 const bool changed = http_server_properties_impl_->SetAlternativeService( | 202 const bool changed = http_server_properties_impl_->SetAlternativeService( |
| 203 origin, alternative_service, expiration); | 203 origin, alternative_service, expiration); |
| 204 if (changed) { | 204 if (changed) { |
| 205 ScheduleUpdatePrefsOnNetworkThread(SET_ALTERNATIVE_SERVICES); | 205 ScheduleUpdatePrefsOnNetworkThread(SET_ALTERNATIVE_SERVICES); |
| 206 } | 206 } |
| 207 return changed; | 207 return changed; |
| 208 } | 208 } |
| 209 | 209 |
| 210 bool HttpServerPropertiesManager::SetAlternativeServices( | 210 bool HttpServerPropertiesManager::SetAlternativeServices( |
| 211 const HostPortPair& origin, | 211 const url::SchemeHostPort& origin, |
| 212 const AlternativeServiceInfoVector& alternative_service_info_vector) { | 212 const AlternativeServiceInfoVector& alternative_service_info_vector) { |
| 213 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 213 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 214 const bool changed = http_server_properties_impl_->SetAlternativeServices( | 214 const bool changed = http_server_properties_impl_->SetAlternativeServices( |
| 215 origin, alternative_service_info_vector); | 215 origin, alternative_service_info_vector); |
| 216 if (changed) { | 216 if (changed) { |
| 217 ScheduleUpdatePrefsOnNetworkThread(SET_ALTERNATIVE_SERVICES); | 217 ScheduleUpdatePrefsOnNetworkThread(SET_ALTERNATIVE_SERVICES); |
| 218 } | 218 } |
| 219 return changed; | 219 return changed; |
| 220 } | 220 } |
| 221 | 221 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 http_server_properties_impl_->ConfirmAlternativeService(alternative_service); | 257 http_server_properties_impl_->ConfirmAlternativeService(alternative_service); |
| 258 bool new_value = http_server_properties_impl_->IsAlternativeServiceBroken( | 258 bool new_value = http_server_properties_impl_->IsAlternativeServiceBroken( |
| 259 alternative_service); | 259 alternative_service); |
| 260 // For persisting, we only care about the value returned by | 260 // For persisting, we only care about the value returned by |
| 261 // IsAlternativeServiceBroken. If that value changes, then call persist. | 261 // IsAlternativeServiceBroken. If that value changes, then call persist. |
| 262 if (old_value != new_value) | 262 if (old_value != new_value) |
| 263 ScheduleUpdatePrefsOnNetworkThread(CONFIRM_ALTERNATIVE_SERVICE); | 263 ScheduleUpdatePrefsOnNetworkThread(CONFIRM_ALTERNATIVE_SERVICE); |
| 264 } | 264 } |
| 265 | 265 |
| 266 void HttpServerPropertiesManager::ClearAlternativeServices( | 266 void HttpServerPropertiesManager::ClearAlternativeServices( |
| 267 const HostPortPair& origin) { | 267 const url::SchemeHostPort& origin) { |
| 268 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 268 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 269 const AlternativeServiceMap& map = | 269 const AlternativeServiceMap& map = |
| 270 http_server_properties_impl_->alternative_service_map(); | 270 http_server_properties_impl_->alternative_service_map(); |
| 271 size_t old_size = map.size(); | 271 size_t old_size = map.size(); |
| 272 http_server_properties_impl_->ClearAlternativeServices(origin); | 272 http_server_properties_impl_->ClearAlternativeServices(origin); |
| 273 size_t new_size = map.size(); | 273 size_t new_size = map.size(); |
| 274 // Persist only if we have deleted an entry. | 274 // Persist only if we have deleted an entry. |
| 275 if (old_size != new_size) | 275 if (old_size != new_size) |
| 276 ScheduleUpdatePrefsOnNetworkThread(CLEAR_ALTERNATIVE_SERVICE); | 276 ScheduleUpdatePrefsOnNetworkThread(CLEAR_ALTERNATIVE_SERVICE); |
| 277 } | 277 } |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 ServerList* spdy_servers, | 546 ServerList* spdy_servers, |
| 547 SpdySettingsMap* spdy_settings_map, | 547 SpdySettingsMap* spdy_settings_map, |
| 548 AlternativeServiceMap* alternative_service_map, | 548 AlternativeServiceMap* alternative_service_map, |
| 549 ServerNetworkStatsMap* network_stats_map) { | 549 ServerNetworkStatsMap* network_stats_map) { |
| 550 for (base::DictionaryValue::Iterator it(servers_dict); !it.IsAtEnd(); | 550 for (base::DictionaryValue::Iterator it(servers_dict); !it.IsAtEnd(); |
| 551 it.Advance()) { | 551 it.Advance()) { |
| 552 // TODO(zhongyi): get server's scheme/host/port when servers_dict migrates | 552 // TODO(zhongyi): get server's scheme/host/port when servers_dict migrates |
| 553 // to SchemeHostPort. | 553 // to SchemeHostPort. |
| 554 // Get server's host/pair. | 554 // Get server's host/pair. |
| 555 const std::string& server_str = it.key(); | 555 const std::string& server_str = it.key(); |
| 556 HostPortPair server = HostPortPair::FromString(server_str); | |
| 557 // TODO(zhongyi): fill in the scheme field when servers_dict has scheme. | 556 // TODO(zhongyi): fill in the scheme field when servers_dict has scheme. |
| 558 std::string spdy_server_url = "https://" + server_str; | 557 std::string spdy_server_url = "https://" + server_str; |
| 559 url::SchemeHostPort spdy_server((GURL(spdy_server_url))); | 558 url::SchemeHostPort spdy_server((GURL(spdy_server_url))); |
| 560 if (server.host().empty()) { | 559 if (spdy_server.host().empty()) { |
| 561 DVLOG(1) << "Malformed http_server_properties for server: " << server_str; | 560 DVLOG(1) << "Malformed http_server_properties for server: " << server_str; |
| 562 return false; | 561 return false; |
| 563 } | 562 } |
| 564 | 563 |
| 565 const base::DictionaryValue* server_pref_dict = nullptr; | 564 const base::DictionaryValue* server_pref_dict = nullptr; |
| 566 if (!it.value().GetAsDictionary(&server_pref_dict)) { | 565 if (!it.value().GetAsDictionary(&server_pref_dict)) { |
| 567 DVLOG(1) << "Malformed http_server_properties server: " << server_str; | 566 DVLOG(1) << "Malformed http_server_properties server: " << server_str; |
| 568 return false; | 567 return false; |
| 569 } | 568 } |
| 570 | 569 |
| 571 // Get if server supports Spdy. | 570 // Get if server supports Spdy. |
| 572 bool supports_spdy = false; | 571 bool supports_spdy = false; |
| 573 if ((server_pref_dict->GetBoolean(kSupportsSpdyKey, &supports_spdy)) && | 572 if ((server_pref_dict->GetBoolean(kSupportsSpdyKey, &supports_spdy)) && |
| 574 supports_spdy) { | 573 supports_spdy) { |
| 575 spdy_servers->push_back(spdy_server.Serialize()); | 574 spdy_servers->push_back(spdy_server.Serialize()); |
| 576 } | 575 } |
| 577 | 576 |
| 578 AddToSpdySettingsMap(spdy_server, *server_pref_dict, spdy_settings_map); | 577 AddToSpdySettingsMap(spdy_server, *server_pref_dict, spdy_settings_map); |
| 579 if (!AddToAlternativeServiceMap(server, *server_pref_dict, | 578 if (!AddToAlternativeServiceMap(spdy_server, *server_pref_dict, |
| 580 alternative_service_map) || | 579 alternative_service_map) || |
| 581 !AddToNetworkStatsMap(spdy_server, *server_pref_dict, | 580 !AddToNetworkStatsMap(spdy_server, *server_pref_dict, |
| 582 network_stats_map)) { | 581 network_stats_map)) { |
| 583 return false; | 582 return false; |
| 584 } | 583 } |
| 585 } | 584 } |
| 586 return true; | 585 return true; |
| 587 } | 586 } |
| 588 | 587 |
| 589 void HttpServerPropertiesManager::AddToSpdySettingsMap( | 588 void HttpServerPropertiesManager::AddToSpdySettingsMap( |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 682 base::Time::FromInternalValue(expiration_int64); | 681 base::Time::FromInternalValue(expiration_int64); |
| 683 return true; | 682 return true; |
| 684 } | 683 } |
| 685 | 684 |
| 686 DVLOG(1) << "Malformed alternative service expiration for server: " | 685 DVLOG(1) << "Malformed alternative service expiration for server: " |
| 687 << server_str; | 686 << server_str; |
| 688 return false; | 687 return false; |
| 689 } | 688 } |
| 690 | 689 |
| 691 bool HttpServerPropertiesManager::AddToAlternativeServiceMap( | 690 bool HttpServerPropertiesManager::AddToAlternativeServiceMap( |
| 692 const HostPortPair& server, | 691 const url::SchemeHostPort& server, |
| 693 const base::DictionaryValue& server_pref_dict, | 692 const base::DictionaryValue& server_pref_dict, |
| 694 AlternativeServiceMap* alternative_service_map) { | 693 AlternativeServiceMap* alternative_service_map) { |
| 695 DCHECK(alternative_service_map->Peek(server) == | 694 DCHECK(alternative_service_map->Peek(server) == |
| 696 alternative_service_map->end()); | 695 alternative_service_map->end()); |
| 697 const base::ListValue* alternative_service_list; | 696 const base::ListValue* alternative_service_list; |
| 698 if (!server_pref_dict.GetListWithoutPathExpansion( | 697 if (!server_pref_dict.GetListWithoutPathExpansion( |
| 699 kAlternativeServiceKey, &alternative_service_list)) { | 698 kAlternativeServiceKey, &alternative_service_list)) { |
| 700 return true; | 699 return true; |
| 701 } | 700 } |
| 702 | 701 |
| 703 AlternativeServiceInfoVector alternative_service_info_vector; | 702 AlternativeServiceInfoVector alternative_service_info_vector; |
| 704 for (const base::Value* alternative_service_list_item : | 703 for (const base::Value* alternative_service_list_item : |
| 705 *alternative_service_list) { | 704 *alternative_service_list) { |
| 706 const base::DictionaryValue* alternative_service_dict; | 705 const base::DictionaryValue* alternative_service_dict; |
| 707 if (!alternative_service_list_item->GetAsDictionary( | 706 if (!alternative_service_list_item->GetAsDictionary( |
| 708 &alternative_service_dict)) | 707 &alternative_service_dict)) |
| 709 return false; | 708 return false; |
| 710 AlternativeServiceInfo alternative_service_info; | 709 AlternativeServiceInfo alternative_service_info; |
| 711 if (!ParseAlternativeServiceDict(*alternative_service_dict, | 710 if (!ParseAlternativeServiceDict(*alternative_service_dict, |
| 712 server.ToString(), | 711 server.Serialize(), |
| 713 &alternative_service_info)) { | 712 &alternative_service_info)) { |
| 714 return false; | 713 return false; |
| 715 } | 714 } |
| 716 if (base::Time::Now() < alternative_service_info.expiration) { | 715 if (base::Time::Now() < alternative_service_info.expiration) { |
| 717 alternative_service_info_vector.push_back(alternative_service_info); | 716 alternative_service_info_vector.push_back(alternative_service_info); |
| 718 } | 717 } |
| 719 } | 718 } |
| 720 | 719 |
| 721 if (alternative_service_info_vector.empty()) { | 720 if (alternative_service_info_vector.empty()) { |
| 722 return false; | 721 return false; |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 919 const AlternativeServiceMap& map = | 918 const AlternativeServiceMap& map = |
| 920 http_server_properties_impl_->alternative_service_map(); | 919 http_server_properties_impl_->alternative_service_map(); |
| 921 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers.Memory", | 920 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers.Memory", |
| 922 map.size()); | 921 map.size()); |
| 923 count = 0; | 922 count = 0; |
| 924 typedef std::map<std::string, bool> CanonicalHostPersistedMap; | 923 typedef std::map<std::string, bool> CanonicalHostPersistedMap; |
| 925 CanonicalHostPersistedMap persisted_map; | 924 CanonicalHostPersistedMap persisted_map; |
| 926 // Maintain MRU order. | 925 // Maintain MRU order. |
| 927 for (AlternativeServiceMap::const_reverse_iterator it = map.rbegin(); | 926 for (AlternativeServiceMap::const_reverse_iterator it = map.rbegin(); |
| 928 it != map.rend() && count < kMaxAlternateProtocolHostsToPersist; ++it) { | 927 it != map.rend() && count < kMaxAlternateProtocolHostsToPersist; ++it) { |
| 929 const HostPortPair& server = it->first; | 928 const url::SchemeHostPort& server = it->first; |
| 930 AlternativeServiceInfoVector notbroken_alternative_service_info_vector; | 929 AlternativeServiceInfoVector notbroken_alternative_service_info_vector; |
| 931 for (const AlternativeServiceInfo& alternative_service_info : it->second) { | 930 for (const AlternativeServiceInfo& alternative_service_info : it->second) { |
| 932 // Do not persist expired entries. | 931 // Do not persist expired entries. |
| 933 if (alternative_service_info.expiration < base::Time::Now()) { | 932 if (alternative_service_info.expiration < base::Time::Now()) { |
| 934 continue; | 933 continue; |
| 935 } | 934 } |
| 936 AlternativeService alternative_service( | 935 AlternativeService alternative_service( |
| 937 alternative_service_info.alternative_service); | 936 alternative_service_info.alternative_service); |
| 938 if (!IsAlternateProtocolValid(alternative_service.protocol)) { | 937 if (!IsAlternateProtocolValid(alternative_service.protocol)) { |
| 939 continue; | 938 continue; |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1071 server_pref_map.Put(server, server_pref); | 1070 server_pref_map.Put(server, server_pref); |
| 1072 } else { | 1071 } else { |
| 1073 it->second.settings_map = &map_it->second; | 1072 it->second.settings_map = &map_it->second; |
| 1074 } | 1073 } |
| 1075 } | 1074 } |
| 1076 | 1075 |
| 1077 // Add alternative services to server_pref_map in the MRU order. | 1076 // Add alternative services to server_pref_map in the MRU order. |
| 1078 for (AlternativeServiceMap::const_reverse_iterator map_it = | 1077 for (AlternativeServiceMap::const_reverse_iterator map_it = |
| 1079 alternative_service_map->rbegin(); | 1078 alternative_service_map->rbegin(); |
| 1080 map_it != alternative_service_map->rend(); ++map_it) { | 1079 map_it != alternative_service_map->rend(); ++map_it) { |
| 1081 const HostPortPair& server = map_it->first; | 1080 // TODO(zhongyi): migrate to SHP once server_pref_map is migrated. |
| 1081 const HostPortPair server(map_it->first.host(), map_it->first.port()); |
| 1082 ServerPrefMap::iterator it = server_pref_map.Get(server); | 1082 ServerPrefMap::iterator it = server_pref_map.Get(server); |
| 1083 if (it == server_pref_map.end()) { | 1083 if (it == server_pref_map.end()) { |
| 1084 ServerPref server_pref; | 1084 ServerPref server_pref; |
| 1085 server_pref.alternative_service_info_vector = &map_it->second; | 1085 server_pref.alternative_service_info_vector = &map_it->second; |
| 1086 server_pref_map.Put(server, server_pref); | 1086 server_pref_map.Put(server, server_pref); |
| 1087 } else { | 1087 } else { |
| 1088 it->second.alternative_service_info_vector = &map_it->second; | 1088 it->second.alternative_service_info_vector = &map_it->second; |
| 1089 } | 1089 } |
| 1090 } | 1090 } |
| 1091 | 1091 |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1252 quic_servers_dict); | 1252 quic_servers_dict); |
| 1253 } | 1253 } |
| 1254 | 1254 |
| 1255 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { | 1255 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { |
| 1256 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | 1256 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
| 1257 if (!setting_prefs_) | 1257 if (!setting_prefs_) |
| 1258 ScheduleUpdateCacheOnPrefThread(); | 1258 ScheduleUpdateCacheOnPrefThread(); |
| 1259 } | 1259 } |
| 1260 | 1260 |
| 1261 } // namespace net | 1261 } // namespace net |
| OLD | NEW |