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/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
14 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "net/base/ip_address_number.h" | 16 #include "net/base/ip_address_number.h" |
17 #include "net/base/port_util.h" | 17 #include "net/base/port_util.h" |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 // Time to wait before starting an update the http_server_properties_impl_ cache | 23 // Time to wait before starting an update the http_server_properties_impl_ cache |
24 // from preferences. Scheduling another update during this period will reset the | 24 // from preferences. Scheduling another update during this period will reset the |
25 // timer. | 25 // timer. |
26 const int64 kUpdateCacheDelayMs = 1000; | 26 const int64_t kUpdateCacheDelayMs = 1000; |
27 | 27 |
28 // Time to wait before starting an update the preferences from the | 28 // Time to wait before starting an update the preferences from the |
29 // http_server_properties_impl_ cache. Scheduling another update during this | 29 // http_server_properties_impl_ cache. Scheduling another update during this |
30 // period will reset the timer. | 30 // period will reset the timer. |
31 const int64 kUpdatePrefsDelayMs = 60000; | 31 const int64_t kUpdatePrefsDelayMs = 60000; |
32 | 32 |
33 // "version" 0 indicates, http_server_properties doesn't have "version" | 33 // "version" 0 indicates, http_server_properties doesn't have "version" |
34 // property. | 34 // property. |
35 const int kMissingVersion = 0; | 35 const int kMissingVersion = 0; |
36 | 36 |
37 // The version number of persisted http_server_properties. | 37 // The version number of persisted http_server_properties. |
38 const int kVersionNumber = 3; | 38 const int kVersionNumber = 3; |
39 | 39 |
40 typedef std::vector<std::string> StringVector; | 40 typedef std::vector<std::string> StringVector; |
41 | 41 |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 const SettingsMap& HttpServerPropertiesManager::GetSpdySettings( | 303 const SettingsMap& HttpServerPropertiesManager::GetSpdySettings( |
304 const HostPortPair& host_port_pair) { | 304 const HostPortPair& host_port_pair) { |
305 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 305 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
306 return http_server_properties_impl_->GetSpdySettings(host_port_pair); | 306 return http_server_properties_impl_->GetSpdySettings(host_port_pair); |
307 } | 307 } |
308 | 308 |
309 bool HttpServerPropertiesManager::SetSpdySetting( | 309 bool HttpServerPropertiesManager::SetSpdySetting( |
310 const HostPortPair& host_port_pair, | 310 const HostPortPair& host_port_pair, |
311 SpdySettingsIds id, | 311 SpdySettingsIds id, |
312 SpdySettingsFlags flags, | 312 SpdySettingsFlags flags, |
313 uint32 value) { | 313 uint32_t value) { |
314 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 314 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
315 bool persist = http_server_properties_impl_->SetSpdySetting( | 315 bool persist = http_server_properties_impl_->SetSpdySetting( |
316 host_port_pair, id, flags, value); | 316 host_port_pair, id, flags, value); |
317 if (persist) | 317 if (persist) |
318 ScheduleUpdatePrefsOnNetworkThread(SET_SPDY_SETTING); | 318 ScheduleUpdatePrefsOnNetworkThread(SET_SPDY_SETTING); |
319 return persist; | 319 return persist; |
320 } | 320 } |
321 | 321 |
322 void HttpServerPropertiesManager::ClearSpdySettings( | 322 void HttpServerPropertiesManager::ClearSpdySettings( |
323 const HostPortPair& host_port_pair) { | 323 const HostPortPair& host_port_pair) { |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
587 } | 587 } |
588 | 588 |
589 // Port is mandatory. | 589 // Port is mandatory. |
590 int port = 0; | 590 int port = 0; |
591 if (!alternative_service_dict.GetInteger(kPortKey, &port) || | 591 if (!alternative_service_dict.GetInteger(kPortKey, &port) || |
592 !IsPortValid(port)) { | 592 !IsPortValid(port)) { |
593 DVLOG(1) << "Malformed alternative service port for server: " << server_str; | 593 DVLOG(1) << "Malformed alternative service port for server: " << server_str; |
594 return false; | 594 return false; |
595 } | 595 } |
596 alternative_service_info->alternative_service.port = | 596 alternative_service_info->alternative_service.port = |
597 static_cast<uint32>(port); | 597 static_cast<uint32_t>(port); |
598 | 598 |
599 // Probability is optional, defaults to 1.0. | 599 // Probability is optional, defaults to 1.0. |
600 alternative_service_info->probability = 1.0; | 600 alternative_service_info->probability = 1.0; |
601 if (alternative_service_dict.HasKey(kProbabilityKey) && | 601 if (alternative_service_dict.HasKey(kProbabilityKey) && |
602 !alternative_service_dict.GetDoubleWithoutPathExpansion( | 602 !alternative_service_dict.GetDoubleWithoutPathExpansion( |
603 kProbabilityKey, &(alternative_service_info->probability))) { | 603 kProbabilityKey, &(alternative_service_info->probability))) { |
604 DVLOG(1) << "Malformed alternative service probability for server: " | 604 DVLOG(1) << "Malformed alternative service probability for server: " |
605 << server_str; | 605 << server_str; |
606 return false; | 606 return false; |
607 } | 607 } |
608 | 608 |
609 // Expiration is optional, defaults to one day. | 609 // Expiration is optional, defaults to one day. |
610 base::Time expiration; | 610 base::Time expiration; |
611 if (!alternative_service_dict.HasKey(kExpirationKey)) { | 611 if (!alternative_service_dict.HasKey(kExpirationKey)) { |
612 alternative_service_info->expiration = | 612 alternative_service_info->expiration = |
613 base::Time::Now() + base::TimeDelta::FromDays(1); | 613 base::Time::Now() + base::TimeDelta::FromDays(1); |
614 return true; | 614 return true; |
615 } | 615 } |
616 | 616 |
617 std::string expiration_string; | 617 std::string expiration_string; |
618 if (alternative_service_dict.GetStringWithoutPathExpansion( | 618 if (alternative_service_dict.GetStringWithoutPathExpansion( |
619 kExpirationKey, &expiration_string)) { | 619 kExpirationKey, &expiration_string)) { |
620 int64 expiration_int64 = 0; | 620 int64_t expiration_int64 = 0; |
621 if (!base::StringToInt64(expiration_string, &expiration_int64)) { | 621 if (!base::StringToInt64(expiration_string, &expiration_int64)) { |
622 DVLOG(1) << "Malformed alternative service expiration for server: " | 622 DVLOG(1) << "Malformed alternative service expiration for server: " |
623 << server_str; | 623 << server_str; |
624 return false; | 624 return false; |
625 } | 625 } |
626 alternative_service_info->expiration = | 626 alternative_service_info->expiration = |
627 base::Time::FromInternalValue(expiration_int64); | 627 base::Time::FromInternalValue(expiration_int64); |
628 return true; | 628 return true; |
629 } | 629 } |
630 | 630 |
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1062 void HttpServerPropertiesManager::SaveSpdySettingsToServerPrefs( | 1062 void HttpServerPropertiesManager::SaveSpdySettingsToServerPrefs( |
1063 const SettingsMap* settings_map, | 1063 const SettingsMap* settings_map, |
1064 base::DictionaryValue* server_pref_dict) { | 1064 base::DictionaryValue* server_pref_dict) { |
1065 if (!settings_map) { | 1065 if (!settings_map) { |
1066 return; | 1066 return; |
1067 } | 1067 } |
1068 base::DictionaryValue* spdy_settings_dict = new base::DictionaryValue; | 1068 base::DictionaryValue* spdy_settings_dict = new base::DictionaryValue; |
1069 for (SettingsMap::const_iterator it = settings_map->begin(); | 1069 for (SettingsMap::const_iterator it = settings_map->begin(); |
1070 it != settings_map->end(); ++it) { | 1070 it != settings_map->end(); ++it) { |
1071 SpdySettingsIds id = it->first; | 1071 SpdySettingsIds id = it->first; |
1072 uint32 value = it->second.second; | 1072 uint32_t value = it->second.second; |
1073 std::string key = base::StringPrintf("%u", id); | 1073 std::string key = base::StringPrintf("%u", id); |
1074 spdy_settings_dict->SetInteger(key, value); | 1074 spdy_settings_dict->SetInteger(key, value); |
1075 } | 1075 } |
1076 server_pref_dict->SetWithoutPathExpansion(kSettingsKey, spdy_settings_dict); | 1076 server_pref_dict->SetWithoutPathExpansion(kSettingsKey, spdy_settings_dict); |
1077 } | 1077 } |
1078 | 1078 |
1079 void HttpServerPropertiesManager::SaveAlternativeServiceToServerPrefs( | 1079 void HttpServerPropertiesManager::SaveAlternativeServiceToServerPrefs( |
1080 const AlternativeServiceInfoVector* alternative_service_info_vector, | 1080 const AlternativeServiceInfoVector* alternative_service_info_vector, |
1081 base::DictionaryValue* server_pref_dict) { | 1081 base::DictionaryValue* server_pref_dict) { |
1082 if (!alternative_service_info_vector || | 1082 if (!alternative_service_info_vector || |
1083 alternative_service_info_vector->empty()) { | 1083 alternative_service_info_vector->empty()) { |
1084 return; | 1084 return; |
1085 } | 1085 } |
1086 scoped_ptr<base::ListValue> alternative_service_list(new base::ListValue); | 1086 scoped_ptr<base::ListValue> alternative_service_list(new base::ListValue); |
1087 for (const AlternativeServiceInfo& alternative_service_info : | 1087 for (const AlternativeServiceInfo& alternative_service_info : |
1088 *alternative_service_info_vector) { | 1088 *alternative_service_info_vector) { |
1089 const AlternativeService alternative_service = | 1089 const AlternativeService alternative_service = |
1090 alternative_service_info.alternative_service; | 1090 alternative_service_info.alternative_service; |
1091 DCHECK(IsAlternateProtocolValid(alternative_service.protocol)); | 1091 DCHECK(IsAlternateProtocolValid(alternative_service.protocol)); |
1092 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue; | 1092 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue; |
1093 alternative_service_dict->SetInteger(kPortKey, alternative_service.port); | 1093 alternative_service_dict->SetInteger(kPortKey, alternative_service.port); |
1094 if (!alternative_service.host.empty()) { | 1094 if (!alternative_service.host.empty()) { |
1095 alternative_service_dict->SetString(kHostKey, alternative_service.host); | 1095 alternative_service_dict->SetString(kHostKey, alternative_service.host); |
1096 } | 1096 } |
1097 alternative_service_dict->SetString( | 1097 alternative_service_dict->SetString( |
1098 kProtocolKey, AlternateProtocolToString(alternative_service.protocol)); | 1098 kProtocolKey, AlternateProtocolToString(alternative_service.protocol)); |
1099 alternative_service_dict->SetDouble(kProbabilityKey, | 1099 alternative_service_dict->SetDouble(kProbabilityKey, |
1100 alternative_service_info.probability); | 1100 alternative_service_info.probability); |
1101 // JSON cannot store int64, so expiration is converted to a string. | 1101 // JSON cannot store int64_t, so expiration is converted to a string. |
1102 alternative_service_dict->SetString( | 1102 alternative_service_dict->SetString( |
1103 kExpirationKey, | 1103 kExpirationKey, |
1104 base::Int64ToString( | 1104 base::Int64ToString( |
1105 alternative_service_info.expiration.ToInternalValue())); | 1105 alternative_service_info.expiration.ToInternalValue())); |
1106 alternative_service_list->Append(alternative_service_dict); | 1106 alternative_service_list->Append(alternative_service_dict); |
1107 } | 1107 } |
1108 if (alternative_service_list->GetSize() == 0) | 1108 if (alternative_service_list->GetSize() == 0) |
1109 return; | 1109 return; |
1110 server_pref_dict->SetWithoutPathExpansion(kAlternativeServiceKey, | 1110 server_pref_dict->SetWithoutPathExpansion(kAlternativeServiceKey, |
1111 alternative_service_list.release()); | 1111 alternative_service_list.release()); |
(...skipping 13 matching lines...) Expand all Loading... |
1125 supports_quic_dict); | 1125 supports_quic_dict); |
1126 } | 1126 } |
1127 | 1127 |
1128 void HttpServerPropertiesManager::SaveNetworkStatsToServerPrefs( | 1128 void HttpServerPropertiesManager::SaveNetworkStatsToServerPrefs( |
1129 const ServerNetworkStats* server_network_stats, | 1129 const ServerNetworkStats* server_network_stats, |
1130 base::DictionaryValue* server_pref_dict) { | 1130 base::DictionaryValue* server_pref_dict) { |
1131 if (!server_network_stats) | 1131 if (!server_network_stats) |
1132 return; | 1132 return; |
1133 | 1133 |
1134 base::DictionaryValue* server_network_stats_dict = new base::DictionaryValue; | 1134 base::DictionaryValue* server_network_stats_dict = new base::DictionaryValue; |
1135 // Becasue JSON doesn't support int64, persist int64 as a string. | 1135 // Becasue JSON doesn't support int64_t, persist int64_t as a string. |
1136 server_network_stats_dict->SetInteger( | 1136 server_network_stats_dict->SetInteger( |
1137 kSrttKey, static_cast<int>(server_network_stats->srtt.ToInternalValue())); | 1137 kSrttKey, static_cast<int>(server_network_stats->srtt.ToInternalValue())); |
1138 // TODO(rtenneti): When QUIC starts using bandwidth_estimate, then persist | 1138 // TODO(rtenneti): When QUIC starts using bandwidth_estimate, then persist |
1139 // bandwidth_estimate. | 1139 // bandwidth_estimate. |
1140 server_pref_dict->SetWithoutPathExpansion(kNetworkStatsKey, | 1140 server_pref_dict->SetWithoutPathExpansion(kNetworkStatsKey, |
1141 server_network_stats_dict); | 1141 server_network_stats_dict); |
1142 } | 1142 } |
1143 | 1143 |
1144 void HttpServerPropertiesManager::SaveQuicServerInfoMapToServerPrefs( | 1144 void HttpServerPropertiesManager::SaveQuicServerInfoMapToServerPrefs( |
1145 QuicServerInfoMap* quic_server_info_map, | 1145 QuicServerInfoMap* quic_server_info_map, |
(...skipping 15 matching lines...) Expand all Loading... |
1161 quic_servers_dict); | 1161 quic_servers_dict); |
1162 } | 1162 } |
1163 | 1163 |
1164 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { | 1164 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { |
1165 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | 1165 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
1166 if (!setting_prefs_) | 1166 if (!setting_prefs_) |
1167 ScheduleUpdateCacheOnPrefThread(); | 1167 ScheduleUpdateCacheOnPrefThread(); |
1168 } | 1168 } |
1169 | 1169 |
1170 } // namespace net | 1170 } // namespace net |
OLD | NEW |