| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/net/http_server_properties_manager.h" | 5 #include "chrome/browser/net/http_server_properties_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 | 58 |
| 59 } // namespace | 59 } // namespace |
| 60 | 60 |
| 61 //////////////////////////////////////////////////////////////////////////////// | 61 //////////////////////////////////////////////////////////////////////////////// |
| 62 // HttpServerPropertiesManager | 62 // HttpServerPropertiesManager |
| 63 | 63 |
| 64 HttpServerPropertiesManager::HttpServerPropertiesManager( | 64 HttpServerPropertiesManager::HttpServerPropertiesManager( |
| 65 PrefService* pref_service) | 65 PrefService* pref_service) |
| 66 : pref_service_(pref_service), | 66 : pref_service_(pref_service), |
| 67 setting_prefs_(false) { | 67 setting_prefs_(false) { |
| 68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 68 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 69 DCHECK(pref_service); | 69 DCHECK(pref_service); |
| 70 ui_weak_ptr_factory_.reset( | 70 ui_weak_ptr_factory_.reset( |
| 71 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); | 71 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); |
| 72 ui_weak_ptr_ = ui_weak_ptr_factory_->GetWeakPtr(); | 72 ui_weak_ptr_ = ui_weak_ptr_factory_->GetWeakPtr(); |
| 73 ui_cache_update_timer_.reset( | 73 ui_cache_update_timer_.reset( |
| 74 new base::OneShotTimer<HttpServerPropertiesManager>); | 74 new base::OneShotTimer<HttpServerPropertiesManager>); |
| 75 pref_change_registrar_.Init(pref_service_); | 75 pref_change_registrar_.Init(pref_service_); |
| 76 pref_change_registrar_.Add( | 76 pref_change_registrar_.Add( |
| 77 prefs::kHttpServerProperties, | 77 prefs::kHttpServerProperties, |
| 78 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, | 78 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, |
| 79 base::Unretained(this))); | 79 base::Unretained(this))); |
| 80 } | 80 } |
| 81 | 81 |
| 82 HttpServerPropertiesManager::~HttpServerPropertiesManager() { | 82 HttpServerPropertiesManager::~HttpServerPropertiesManager() { |
| 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 83 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 84 io_weak_ptr_factory_.reset(); | 84 io_weak_ptr_factory_.reset(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void HttpServerPropertiesManager::InitializeOnIOThread() { | 87 void HttpServerPropertiesManager::InitializeOnIOThread() { |
| 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 88 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 89 io_weak_ptr_factory_.reset( | 89 io_weak_ptr_factory_.reset( |
| 90 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); | 90 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); |
| 91 http_server_properties_impl_.reset(new net::HttpServerPropertiesImpl()); | 91 http_server_properties_impl_.reset(new net::HttpServerPropertiesImpl()); |
| 92 | 92 |
| 93 io_prefs_update_timer_.reset( | 93 io_prefs_update_timer_.reset( |
| 94 new base::OneShotTimer<HttpServerPropertiesManager>); | 94 new base::OneShotTimer<HttpServerPropertiesManager>); |
| 95 | 95 |
| 96 BrowserThread::PostTask( | 96 BrowserThread::PostTask( |
| 97 BrowserThread::UI, | 97 BrowserThread::UI, |
| 98 FROM_HERE, | 98 FROM_HERE, |
| 99 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI, | 99 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI, |
| 100 ui_weak_ptr_)); | 100 ui_weak_ptr_)); |
| 101 } | 101 } |
| 102 | 102 |
| 103 void HttpServerPropertiesManager::ShutdownOnUIThread() { | 103 void HttpServerPropertiesManager::ShutdownOnUIThread() { |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 104 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 105 // Cancel any pending updates, and stop listening for pref change updates. | 105 // Cancel any pending updates, and stop listening for pref change updates. |
| 106 ui_cache_update_timer_->Stop(); | 106 ui_cache_update_timer_->Stop(); |
| 107 ui_weak_ptr_factory_.reset(); | 107 ui_weak_ptr_factory_.reset(); |
| 108 pref_change_registrar_.RemoveAll(); | 108 pref_change_registrar_.RemoveAll(); |
| 109 } | 109 } |
| 110 | 110 |
| 111 // static | 111 // static |
| 112 void HttpServerPropertiesManager::RegisterProfilePrefs( | 112 void HttpServerPropertiesManager::RegisterProfilePrefs( |
| 113 user_prefs::PrefRegistrySyncable* prefs) { | 113 user_prefs::PrefRegistrySyncable* prefs) { |
| 114 prefs->RegisterDictionaryPref( | 114 prefs->RegisterDictionaryPref( |
| 115 prefs::kHttpServerProperties, | 115 prefs::kHttpServerProperties, |
| 116 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 116 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 117 } | 117 } |
| 118 | 118 |
| 119 // static | 119 // static |
| 120 void HttpServerPropertiesManager::SetVersion( | 120 void HttpServerPropertiesManager::SetVersion( |
| 121 base::DictionaryValue* http_server_properties_dict, | 121 base::DictionaryValue* http_server_properties_dict, |
| 122 int version_number) { | 122 int version_number) { |
| 123 if (version_number < 0) | 123 if (version_number < 0) |
| 124 version_number = kVersionNumber; | 124 version_number = kVersionNumber; |
| 125 DCHECK_LE(version_number, kVersionNumber); | 125 DCHECK_LE(version_number, kVersionNumber); |
| 126 if (version_number <= kVersionNumber) | 126 if (version_number <= kVersionNumber) |
| 127 http_server_properties_dict->SetInteger("version", version_number); | 127 http_server_properties_dict->SetInteger("version", version_number); |
| 128 } | 128 } |
| 129 | 129 |
| 130 // This is required for conformance with the HttpServerProperties interface. | 130 // This is required for conformance with the HttpServerProperties interface. |
| 131 base::WeakPtr<net::HttpServerProperties> | 131 base::WeakPtr<net::HttpServerProperties> |
| 132 HttpServerPropertiesManager::GetWeakPtr() { | 132 HttpServerPropertiesManager::GetWeakPtr() { |
| 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 133 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 134 return io_weak_ptr_factory_->GetWeakPtr(); | 134 return io_weak_ptr_factory_->GetWeakPtr(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void HttpServerPropertiesManager::Clear() { | 137 void HttpServerPropertiesManager::Clear() { |
| 138 Clear(base::Closure()); | 138 Clear(base::Closure()); |
| 139 } | 139 } |
| 140 | 140 |
| 141 void HttpServerPropertiesManager::Clear(const base::Closure& completion) { | 141 void HttpServerPropertiesManager::Clear(const base::Closure& completion) { |
| 142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 142 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 143 | 143 |
| 144 http_server_properties_impl_->Clear(); | 144 http_server_properties_impl_->Clear(); |
| 145 UpdatePrefsFromCacheOnIO(completion); | 145 UpdatePrefsFromCacheOnIO(completion); |
| 146 } | 146 } |
| 147 | 147 |
| 148 bool HttpServerPropertiesManager::SupportsSpdy( | 148 bool HttpServerPropertiesManager::SupportsSpdy( |
| 149 const net::HostPortPair& server) { | 149 const net::HostPortPair& server) { |
| 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 151 return http_server_properties_impl_->SupportsSpdy(server); | 151 return http_server_properties_impl_->SupportsSpdy(server); |
| 152 } | 152 } |
| 153 | 153 |
| 154 void HttpServerPropertiesManager::SetSupportsSpdy( | 154 void HttpServerPropertiesManager::SetSupportsSpdy( |
| 155 const net::HostPortPair& server, | 155 const net::HostPortPair& server, |
| 156 bool support_spdy) { | 156 bool support_spdy) { |
| 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 157 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 158 | 158 |
| 159 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy); | 159 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy); |
| 160 ScheduleUpdatePrefsOnIO(); | 160 ScheduleUpdatePrefsOnIO(); |
| 161 } | 161 } |
| 162 | 162 |
| 163 bool HttpServerPropertiesManager::HasAlternateProtocol( | 163 bool HttpServerPropertiesManager::HasAlternateProtocol( |
| 164 const net::HostPortPair& server) { | 164 const net::HostPortPair& server) { |
| 165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 165 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 166 return http_server_properties_impl_->HasAlternateProtocol(server); | 166 return http_server_properties_impl_->HasAlternateProtocol(server); |
| 167 } | 167 } |
| 168 | 168 |
| 169 net::PortAlternateProtocolPair | 169 net::AlternateProtocolInfo |
| 170 HttpServerPropertiesManager::GetAlternateProtocol( | 170 HttpServerPropertiesManager::GetAlternateProtocol( |
| 171 const net::HostPortPair& server) { | 171 const net::HostPortPair& server) { |
| 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 172 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 173 return http_server_properties_impl_->GetAlternateProtocol(server); | 173 return http_server_properties_impl_->GetAlternateProtocol(server); |
| 174 } | 174 } |
| 175 | 175 |
| 176 void HttpServerPropertiesManager::SetAlternateProtocol( | 176 void HttpServerPropertiesManager::SetAlternateProtocol( |
| 177 const net::HostPortPair& server, | 177 const net::HostPortPair& server, |
| 178 uint16 alternate_port, | 178 uint16 alternate_port, |
| 179 net::AlternateProtocol alternate_protocol) { | 179 net::AlternateProtocol alternate_protocol, |
| 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 180 double alternate_probability) { |
| 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 181 http_server_properties_impl_->SetAlternateProtocol( | 182 http_server_properties_impl_->SetAlternateProtocol( |
| 182 server, alternate_port, alternate_protocol); | 183 server, alternate_port, alternate_protocol, alternate_probability); |
| 183 ScheduleUpdatePrefsOnIO(); | 184 ScheduleUpdatePrefsOnIO(); |
| 184 } | 185 } |
| 185 | 186 |
| 186 void HttpServerPropertiesManager::SetBrokenAlternateProtocol( | 187 void HttpServerPropertiesManager::SetBrokenAlternateProtocol( |
| 187 const net::HostPortPair& server) { | 188 const net::HostPortPair& server) { |
| 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 189 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 189 http_server_properties_impl_->SetBrokenAlternateProtocol(server); | 190 http_server_properties_impl_->SetBrokenAlternateProtocol(server); |
| 190 ScheduleUpdatePrefsOnIO(); | 191 ScheduleUpdatePrefsOnIO(); |
| 191 } | 192 } |
| 192 | 193 |
| 193 bool HttpServerPropertiesManager::WasAlternateProtocolRecentlyBroken( | 194 bool HttpServerPropertiesManager::WasAlternateProtocolRecentlyBroken( |
| 194 const net::HostPortPair& server) { | 195 const net::HostPortPair& server) { |
| 195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 196 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 196 return http_server_properties_impl_->WasAlternateProtocolRecentlyBroken( | 197 return http_server_properties_impl_->WasAlternateProtocolRecentlyBroken( |
| 197 server); | 198 server); |
| 198 } | 199 } |
| 199 | 200 |
| 200 void HttpServerPropertiesManager::ConfirmAlternateProtocol( | 201 void HttpServerPropertiesManager::ConfirmAlternateProtocol( |
| 201 const net::HostPortPair& server) { | 202 const net::HostPortPair& server) { |
| 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 203 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 203 http_server_properties_impl_->ConfirmAlternateProtocol(server); | 204 http_server_properties_impl_->ConfirmAlternateProtocol(server); |
| 204 ScheduleUpdatePrefsOnIO(); | 205 ScheduleUpdatePrefsOnIO(); |
| 205 } | 206 } |
| 206 | 207 |
| 207 void HttpServerPropertiesManager::ClearAlternateProtocol( | 208 void HttpServerPropertiesManager::ClearAlternateProtocol( |
| 208 const net::HostPortPair& server) { | 209 const net::HostPortPair& server) { |
| 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 210 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 210 http_server_properties_impl_->ClearAlternateProtocol(server); | 211 http_server_properties_impl_->ClearAlternateProtocol(server); |
| 211 ScheduleUpdatePrefsOnIO(); | 212 ScheduleUpdatePrefsOnIO(); |
| 212 } | 213 } |
| 213 | 214 |
| 214 const net::AlternateProtocolMap& | 215 const net::AlternateProtocolMap& |
| 215 HttpServerPropertiesManager::alternate_protocol_map() const { | 216 HttpServerPropertiesManager::alternate_protocol_map() const { |
| 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 217 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 217 return http_server_properties_impl_->alternate_protocol_map(); | 218 return http_server_properties_impl_->alternate_protocol_map(); |
| 218 } | 219 } |
| 219 | 220 |
| 220 void HttpServerPropertiesManager::SetAlternateProtocolExperiment( | 221 void HttpServerPropertiesManager::SetAlternateProtocolExperiment( |
| 221 net::AlternateProtocolExperiment experiment) { | 222 net::AlternateProtocolExperiment experiment) { |
| 223 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 222 http_server_properties_impl_->SetAlternateProtocolExperiment(experiment); | 224 http_server_properties_impl_->SetAlternateProtocolExperiment(experiment); |
| 223 } | 225 } |
| 224 | 226 |
| 227 void HttpServerPropertiesManager::SetAlternateProtocolProbabilityThreshold( |
| 228 double threshold) { |
| 229 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 230 http_server_properties_impl_->SetAlternateProtocolProbabilityThreshold( |
| 231 threshold); |
| 232 } |
| 233 |
| 225 net::AlternateProtocolExperiment | 234 net::AlternateProtocolExperiment |
| 226 HttpServerPropertiesManager::GetAlternateProtocolExperiment() const { | 235 HttpServerPropertiesManager::GetAlternateProtocolExperiment() const { |
| 236 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 227 return http_server_properties_impl_->GetAlternateProtocolExperiment(); | 237 return http_server_properties_impl_->GetAlternateProtocolExperiment(); |
| 228 } | 238 } |
| 229 | 239 |
| 230 const net::SettingsMap& | 240 const net::SettingsMap& |
| 231 HttpServerPropertiesManager::GetSpdySettings( | 241 HttpServerPropertiesManager::GetSpdySettings( |
| 232 const net::HostPortPair& host_port_pair) { | 242 const net::HostPortPair& host_port_pair) { |
| 233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 243 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 234 return http_server_properties_impl_->GetSpdySettings(host_port_pair); | 244 return http_server_properties_impl_->GetSpdySettings(host_port_pair); |
| 235 } | 245 } |
| 236 | 246 |
| 237 bool HttpServerPropertiesManager::SetSpdySetting( | 247 bool HttpServerPropertiesManager::SetSpdySetting( |
| 238 const net::HostPortPair& host_port_pair, | 248 const net::HostPortPair& host_port_pair, |
| 239 net::SpdySettingsIds id, | 249 net::SpdySettingsIds id, |
| 240 net::SpdySettingsFlags flags, | 250 net::SpdySettingsFlags flags, |
| 241 uint32 value) { | 251 uint32 value) { |
| 242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 252 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 243 bool persist = http_server_properties_impl_->SetSpdySetting( | 253 bool persist = http_server_properties_impl_->SetSpdySetting( |
| 244 host_port_pair, id, flags, value); | 254 host_port_pair, id, flags, value); |
| 245 if (persist) | 255 if (persist) |
| 246 ScheduleUpdatePrefsOnIO(); | 256 ScheduleUpdatePrefsOnIO(); |
| 247 return persist; | 257 return persist; |
| 248 } | 258 } |
| 249 | 259 |
| 250 void HttpServerPropertiesManager::ClearSpdySettings( | 260 void HttpServerPropertiesManager::ClearSpdySettings( |
| 251 const net::HostPortPair& host_port_pair) { | 261 const net::HostPortPair& host_port_pair) { |
| 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 262 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 253 http_server_properties_impl_->ClearSpdySettings(host_port_pair); | 263 http_server_properties_impl_->ClearSpdySettings(host_port_pair); |
| 254 ScheduleUpdatePrefsOnIO(); | 264 ScheduleUpdatePrefsOnIO(); |
| 255 } | 265 } |
| 256 | 266 |
| 257 void HttpServerPropertiesManager::ClearAllSpdySettings() { | 267 void HttpServerPropertiesManager::ClearAllSpdySettings() { |
| 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 268 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 259 http_server_properties_impl_->ClearAllSpdySettings(); | 269 http_server_properties_impl_->ClearAllSpdySettings(); |
| 260 ScheduleUpdatePrefsOnIO(); | 270 ScheduleUpdatePrefsOnIO(); |
| 261 } | 271 } |
| 262 | 272 |
| 263 const net::SpdySettingsMap& | 273 const net::SpdySettingsMap& |
| 264 HttpServerPropertiesManager::spdy_settings_map() const { | 274 HttpServerPropertiesManager::spdy_settings_map() const { |
| 265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 275 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 266 return http_server_properties_impl_->spdy_settings_map(); | 276 return http_server_properties_impl_->spdy_settings_map(); |
| 267 } | 277 } |
| 268 | 278 |
| 269 void HttpServerPropertiesManager::SetServerNetworkStats( | 279 void HttpServerPropertiesManager::SetServerNetworkStats( |
| 270 const net::HostPortPair& host_port_pair, | 280 const net::HostPortPair& host_port_pair, |
| 271 NetworkStats stats) { | 281 NetworkStats stats) { |
| 272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 282 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 273 http_server_properties_impl_->SetServerNetworkStats(host_port_pair, stats); | 283 http_server_properties_impl_->SetServerNetworkStats(host_port_pair, stats); |
| 274 } | 284 } |
| 275 | 285 |
| 276 const HttpServerPropertiesManager::NetworkStats* | 286 const HttpServerPropertiesManager::NetworkStats* |
| 277 HttpServerPropertiesManager::GetServerNetworkStats( | 287 HttpServerPropertiesManager::GetServerNetworkStats( |
| 278 const net::HostPortPair& host_port_pair) const { | 288 const net::HostPortPair& host_port_pair) const { |
| 279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 289 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 280 return http_server_properties_impl_->GetServerNetworkStats(host_port_pair); | 290 return http_server_properties_impl_->GetServerNetworkStats(host_port_pair); |
| 281 } | 291 } |
| 282 | 292 |
| 283 // | 293 // |
| 284 // Update the HttpServerPropertiesImpl's cache with data from preferences. | 294 // Update the HttpServerPropertiesImpl's cache with data from preferences. |
| 285 // | 295 // |
| 286 void HttpServerPropertiesManager::ScheduleUpdateCacheOnUI() { | 296 void HttpServerPropertiesManager::ScheduleUpdateCacheOnUI() { |
| 287 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 297 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 288 // Cancel pending updates, if any. | 298 // Cancel pending updates, if any. |
| 289 ui_cache_update_timer_->Stop(); | 299 ui_cache_update_timer_->Stop(); |
| 290 StartCacheUpdateTimerOnUI( | 300 StartCacheUpdateTimerOnUI( |
| 291 base::TimeDelta::FromMilliseconds(kUpdateCacheDelayMs)); | 301 base::TimeDelta::FromMilliseconds(kUpdateCacheDelayMs)); |
| 292 } | 302 } |
| 293 | 303 |
| 294 void HttpServerPropertiesManager::StartCacheUpdateTimerOnUI( | 304 void HttpServerPropertiesManager::StartCacheUpdateTimerOnUI( |
| 295 base::TimeDelta delay) { | 305 base::TimeDelta delay) { |
| 296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 306 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 297 ui_cache_update_timer_->Start( | 307 ui_cache_update_timer_->Start( |
| 298 FROM_HERE, delay, this, | 308 FROM_HERE, delay, this, |
| 299 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI); | 309 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI); |
| 300 } | 310 } |
| 301 | 311 |
| 302 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI() { | 312 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI() { |
| 303 // The preferences can only be read on the UI thread. | 313 // The preferences can only be read on the UI thread. |
| 304 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 314 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 305 | 315 |
| 306 if (!pref_service_->HasPrefPath(prefs::kHttpServerProperties)) | 316 if (!pref_service_->HasPrefPath(prefs::kHttpServerProperties)) |
| 307 return; | 317 return; |
| 308 | 318 |
| 309 bool detected_corrupted_prefs = false; | 319 bool detected_corrupted_prefs = false; |
| 310 const base::DictionaryValue& http_server_properties_dict = | 320 const base::DictionaryValue& http_server_properties_dict = |
| 311 *pref_service_->GetDictionary(prefs::kHttpServerProperties); | 321 *pref_service_->GetDictionary(prefs::kHttpServerProperties); |
| 312 | 322 |
| 313 int version = kMissingVersion; | 323 int version = kMissingVersion; |
| 314 if (!http_server_properties_dict.GetIntegerWithoutPathExpansion( | 324 if (!http_server_properties_dict.GetIntegerWithoutPathExpansion( |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 continue; | 442 continue; |
| 433 } | 443 } |
| 434 net::AlternateProtocol protocol = | 444 net::AlternateProtocol protocol = |
| 435 net::AlternateProtocolFromString(protocol_str); | 445 net::AlternateProtocolFromString(protocol_str); |
| 436 if (!net::IsAlternateProtocolValid(protocol)) { | 446 if (!net::IsAlternateProtocolValid(protocol)) { |
| 437 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; | 447 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; |
| 438 detected_corrupted_prefs = true; | 448 detected_corrupted_prefs = true; |
| 439 continue; | 449 continue; |
| 440 } | 450 } |
| 441 | 451 |
| 442 net::PortAlternateProtocolPair port_alternate_protocol; | 452 double probability = 1; |
| 443 port_alternate_protocol.port = port; | 453 if (port_alternate_protocol_dict->HasKey("probability") && |
| 444 port_alternate_protocol.protocol = protocol; | 454 !port_alternate_protocol_dict->GetDoubleWithoutPathExpansion( |
| 455 "probability", &probability)) { |
| 456 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; |
| 457 detected_corrupted_prefs = true; |
| 458 continue; |
| 459 } |
| 445 | 460 |
| 461 net::AlternateProtocolInfo port_alternate_protocol(port, |
| 462 protocol, |
| 463 probability); |
| 446 alternate_protocol_map->Put(server, port_alternate_protocol); | 464 alternate_protocol_map->Put(server, port_alternate_protocol); |
| 447 ++count; | 465 ++count; |
| 448 } while (false); | 466 } while (false); |
| 449 } | 467 } |
| 450 | 468 |
| 451 BrowserThread::PostTask( | 469 BrowserThread::PostTask( |
| 452 BrowserThread::IO, | 470 BrowserThread::IO, |
| 453 FROM_HERE, | 471 FROM_HERE, |
| 454 base::Bind(&HttpServerPropertiesManager:: | 472 base::Bind(&HttpServerPropertiesManager:: |
| 455 UpdateCacheFromPrefsOnIO, | 473 UpdateCacheFromPrefsOnIO, |
| 456 base::Unretained(this), | 474 base::Unretained(this), |
| 457 base::Owned(spdy_servers.release()), | 475 base::Owned(spdy_servers.release()), |
| 458 base::Owned(spdy_settings_map.release()), | 476 base::Owned(spdy_settings_map.release()), |
| 459 base::Owned(alternate_protocol_map.release()), | 477 base::Owned(alternate_protocol_map.release()), |
| 460 alternate_protocol_experiment, | 478 alternate_protocol_experiment, |
| 461 detected_corrupted_prefs)); | 479 detected_corrupted_prefs)); |
| 462 } | 480 } |
| 463 | 481 |
| 464 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO( | 482 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO( |
| 465 StringVector* spdy_servers, | 483 StringVector* spdy_servers, |
| 466 net::SpdySettingsMap* spdy_settings_map, | 484 net::SpdySettingsMap* spdy_settings_map, |
| 467 net::AlternateProtocolMap* alternate_protocol_map, | 485 net::AlternateProtocolMap* alternate_protocol_map, |
| 468 net::AlternateProtocolExperiment alternate_protocol_experiment, | 486 net::AlternateProtocolExperiment alternate_protocol_experiment, |
| 469 bool detected_corrupted_prefs) { | 487 bool detected_corrupted_prefs) { |
| 470 // Preferences have the master data because admins might have pushed new | 488 // Preferences have the master data because admins might have pushed new |
| 471 // preferences. Update the cached data with new data from preferences. | 489 // preferences. Update the cached data with new data from preferences. |
| 472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 490 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 473 | 491 |
| 474 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size()); | 492 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size()); |
| 475 http_server_properties_impl_->InitializeSpdyServers(spdy_servers, true); | 493 http_server_properties_impl_->InitializeSpdyServers(spdy_servers, true); |
| 476 | 494 |
| 477 // Update the cached data and use the new spdy_settings from preferences. | 495 // Update the cached data and use the new spdy_settings from preferences. |
| 478 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdySettings", spdy_settings_map->size()); | 496 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdySettings", spdy_settings_map->size()); |
| 479 http_server_properties_impl_->InitializeSpdySettingsServers( | 497 http_server_properties_impl_->InitializeSpdySettingsServers( |
| 480 spdy_settings_map); | 498 spdy_settings_map); |
| 481 | 499 |
| 482 // Update the cached data and use the new Alternate-Protocol server list from | 500 // Update the cached data and use the new Alternate-Protocol server list from |
| 483 // preferences. | 501 // preferences. |
| 484 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers", | 502 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers", |
| 485 alternate_protocol_map->size()); | 503 alternate_protocol_map->size()); |
| 486 http_server_properties_impl_->InitializeAlternateProtocolServers( | 504 http_server_properties_impl_->InitializeAlternateProtocolServers( |
| 487 alternate_protocol_map); | 505 alternate_protocol_map); |
| 488 http_server_properties_impl_->SetAlternateProtocolExperiment( | 506 http_server_properties_impl_->SetAlternateProtocolExperiment( |
| 489 alternate_protocol_experiment); | 507 alternate_protocol_experiment); |
| 490 | 508 |
| 491 // Update the prefs with what we have read (delete all corrupted prefs). | 509 // Update the prefs with what we have read (delete all corrupted prefs). |
| 492 if (detected_corrupted_prefs) | 510 if (detected_corrupted_prefs) |
| 493 ScheduleUpdatePrefsOnIO(); | 511 ScheduleUpdatePrefsOnIO(); |
| 494 } | 512 } |
| 495 | 513 |
| 496 | 514 |
| 497 // | 515 // |
| 498 // Update Preferences with data from the cached data. | 516 // Update Preferences with data from the cached data. |
| 499 // | 517 // |
| 500 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO() { | 518 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO() { |
| 501 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 519 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 502 // Cancel pending updates, if any. | 520 // Cancel pending updates, if any. |
| 503 io_prefs_update_timer_->Stop(); | 521 io_prefs_update_timer_->Stop(); |
| 504 StartPrefsUpdateTimerOnIO( | 522 StartPrefsUpdateTimerOnIO( |
| 505 base::TimeDelta::FromMilliseconds(kUpdatePrefsDelayMs)); | 523 base::TimeDelta::FromMilliseconds(kUpdatePrefsDelayMs)); |
| 506 } | 524 } |
| 507 | 525 |
| 508 void HttpServerPropertiesManager::StartPrefsUpdateTimerOnIO( | 526 void HttpServerPropertiesManager::StartPrefsUpdateTimerOnIO( |
| 509 base::TimeDelta delay) { | 527 base::TimeDelta delay) { |
| 510 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 528 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 511 // This is overridden in tests to post the task without the delay. | 529 // This is overridden in tests to post the task without the delay. |
| 512 io_prefs_update_timer_->Start( | 530 io_prefs_update_timer_->Start( |
| 513 FROM_HERE, delay, this, | 531 FROM_HERE, delay, this, |
| 514 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO); | 532 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO); |
| 515 } | 533 } |
| 516 | 534 |
| 517 // This is required so we can set this as the callback for a timer. | 535 // This is required so we can set this as the callback for a timer. |
| 518 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO() { | 536 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO() { |
| 519 UpdatePrefsFromCacheOnIO(base::Closure()); | 537 UpdatePrefsFromCacheOnIO(base::Closure()); |
| 520 } | 538 } |
| 521 | 539 |
| 522 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO( | 540 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO( |
| 523 const base::Closure& completion) { | 541 const base::Closure& completion) { |
| 524 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 542 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 525 | 543 |
| 526 base::ListValue* spdy_server_list = new base::ListValue; | 544 base::ListValue* spdy_server_list = new base::ListValue; |
| 527 http_server_properties_impl_->GetSpdyServerList( | 545 http_server_properties_impl_->GetSpdyServerList( |
| 528 spdy_server_list, kMaxSupportsSpdyServerHostsToPersist); | 546 spdy_server_list, kMaxSupportsSpdyServerHostsToPersist); |
| 529 | 547 |
| 530 net::SpdySettingsMap* spdy_settings_map = | 548 net::SpdySettingsMap* spdy_settings_map = |
| 531 new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist); | 549 new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist); |
| 532 const net::SpdySettingsMap& main_map = | 550 const net::SpdySettingsMap& main_map = |
| 533 http_server_properties_impl_->spdy_settings_map(); | 551 http_server_properties_impl_->spdy_settings_map(); |
| 534 int count = 0; | 552 int count = 0; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 FROM_HERE, | 584 FROM_HERE, |
| 567 base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnUI, | 585 base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnUI, |
| 568 ui_weak_ptr_, | 586 ui_weak_ptr_, |
| 569 base::Owned(spdy_server_list), | 587 base::Owned(spdy_server_list), |
| 570 base::Owned(spdy_settings_map), | 588 base::Owned(spdy_settings_map), |
| 571 base::Owned(alternate_protocol_map), | 589 base::Owned(alternate_protocol_map), |
| 572 completion)); | 590 completion)); |
| 573 } | 591 } |
| 574 | 592 |
| 575 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, | 593 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, |
| 576 // and PortAlternateProtocolPair preferences for a server. This is used only in | 594 // and AlternateProtocolInfo preferences for a server. This is used only in |
| 577 // UpdatePrefsOnUI. | 595 // UpdatePrefsOnUI. |
| 578 struct ServerPref { | 596 struct ServerPref { |
| 579 ServerPref() | 597 ServerPref() |
| 580 : supports_spdy(false), | 598 : supports_spdy(false), |
| 581 settings_map(NULL), | 599 settings_map(NULL), |
| 582 alternate_protocol(NULL) { | 600 alternate_protocol(NULL) { |
| 583 } | 601 } |
| 584 ServerPref(bool supports_spdy, | 602 ServerPref(bool supports_spdy, |
| 585 const net::SettingsMap* settings_map, | 603 const net::SettingsMap* settings_map, |
| 586 const net::PortAlternateProtocolPair* alternate_protocol) | 604 const net::AlternateProtocolInfo* alternate_protocol) |
| 587 : supports_spdy(supports_spdy), | 605 : supports_spdy(supports_spdy), |
| 588 settings_map(settings_map), | 606 settings_map(settings_map), |
| 589 alternate_protocol(alternate_protocol) { | 607 alternate_protocol(alternate_protocol) { |
| 590 } | 608 } |
| 591 bool supports_spdy; | 609 bool supports_spdy; |
| 592 const net::SettingsMap* settings_map; | 610 const net::SettingsMap* settings_map; |
| 593 const net::PortAlternateProtocolPair* alternate_protocol; | 611 const net::AlternateProtocolInfo* alternate_protocol; |
| 594 }; | 612 }; |
| 595 | 613 |
| 596 void HttpServerPropertiesManager::UpdatePrefsOnUI( | 614 void HttpServerPropertiesManager::UpdatePrefsOnUI( |
| 597 base::ListValue* spdy_server_list, | 615 base::ListValue* spdy_server_list, |
| 598 net::SpdySettingsMap* spdy_settings_map, | 616 net::SpdySettingsMap* spdy_settings_map, |
| 599 net::AlternateProtocolMap* alternate_protocol_map, | 617 net::AlternateProtocolMap* alternate_protocol_map, |
| 600 const base::Closure& completion) { | 618 const base::Closure& completion) { |
| 601 | 619 |
| 602 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; | 620 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; |
| 603 ServerPrefMap server_pref_map; | 621 ServerPrefMap server_pref_map; |
| 604 | 622 |
| 605 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 623 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 606 | 624 |
| 607 // Add servers that support spdy to server_pref_map. | 625 // Add servers that support spdy to server_pref_map. |
| 608 std::string s; | 626 std::string s; |
| 609 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); | 627 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); |
| 610 list_it != spdy_server_list->end(); ++list_it) { | 628 list_it != spdy_server_list->end(); ++list_it) { |
| 611 if ((*list_it)->GetAsString(&s)) { | 629 if ((*list_it)->GetAsString(&s)) { |
| 612 net::HostPortPair server = net::HostPortPair::FromString(s); | 630 net::HostPortPair server = net::HostPortPair::FromString(s); |
| 613 | 631 |
| 614 ServerPrefMap::iterator it = server_pref_map.find(server); | 632 ServerPrefMap::iterator it = server_pref_map.find(server); |
| 615 if (it == server_pref_map.end()) { | 633 if (it == server_pref_map.end()) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 633 } else { | 651 } else { |
| 634 it->second.settings_map = &map_it->second; | 652 it->second.settings_map = &map_it->second; |
| 635 } | 653 } |
| 636 } | 654 } |
| 637 | 655 |
| 638 // Add AlternateProtocol servers to server_pref_map. | 656 // Add AlternateProtocol servers to server_pref_map. |
| 639 for (net::AlternateProtocolMap::const_iterator map_it = | 657 for (net::AlternateProtocolMap::const_iterator map_it = |
| 640 alternate_protocol_map->begin(); | 658 alternate_protocol_map->begin(); |
| 641 map_it != alternate_protocol_map->end(); ++map_it) { | 659 map_it != alternate_protocol_map->end(); ++map_it) { |
| 642 const net::HostPortPair& server = map_it->first; | 660 const net::HostPortPair& server = map_it->first; |
| 643 const net::PortAlternateProtocolPair& port_alternate_protocol = | 661 const net::AlternateProtocolInfo& port_alternate_protocol = |
| 644 map_it->second; | 662 map_it->second; |
| 645 if (!net::IsAlternateProtocolValid(port_alternate_protocol.protocol)) { | 663 if (!net::IsAlternateProtocolValid(port_alternate_protocol.protocol)) { |
| 646 continue; | 664 continue; |
| 647 } | 665 } |
| 648 | 666 |
| 649 ServerPrefMap::iterator it = server_pref_map.find(server); | 667 ServerPrefMap::iterator it = server_pref_map.find(server); |
| 650 if (it == server_pref_map.end()) { | 668 if (it == server_pref_map.end()) { |
| 651 ServerPref server_pref(false, NULL, &map_it->second); | 669 ServerPref server_pref(false, NULL, &map_it->second); |
| 652 server_pref_map[server] = server_pref; | 670 server_pref_map[server] = server_pref; |
| 653 } else { | 671 } else { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 681 std::string key = base::StringPrintf("%u", id); | 699 std::string key = base::StringPrintf("%u", id); |
| 682 spdy_settings_dict->SetInteger(key, value); | 700 spdy_settings_dict->SetInteger(key, value); |
| 683 } | 701 } |
| 684 server_pref_dict->SetWithoutPathExpansion("settings", spdy_settings_dict); | 702 server_pref_dict->SetWithoutPathExpansion("settings", spdy_settings_dict); |
| 685 } | 703 } |
| 686 | 704 |
| 687 // Save alternate_protocol. | 705 // Save alternate_protocol. |
| 688 if (server_pref.alternate_protocol) { | 706 if (server_pref.alternate_protocol) { |
| 689 base::DictionaryValue* port_alternate_protocol_dict = | 707 base::DictionaryValue* port_alternate_protocol_dict = |
| 690 new base::DictionaryValue; | 708 new base::DictionaryValue; |
| 691 const net::PortAlternateProtocolPair* port_alternate_protocol = | 709 const net::AlternateProtocolInfo* port_alternate_protocol = |
| 692 server_pref.alternate_protocol; | 710 server_pref.alternate_protocol; |
| 693 port_alternate_protocol_dict->SetInteger( | 711 port_alternate_protocol_dict->SetInteger( |
| 694 "port", port_alternate_protocol->port); | 712 "port", port_alternate_protocol->port); |
| 695 const char* protocol_str = | 713 const char* protocol_str = |
| 696 net::AlternateProtocolToString(port_alternate_protocol->protocol); | 714 net::AlternateProtocolToString(port_alternate_protocol->protocol); |
| 697 port_alternate_protocol_dict->SetString("protocol_str", protocol_str); | 715 port_alternate_protocol_dict->SetString("protocol_str", protocol_str); |
| 716 port_alternate_protocol_dict->SetDouble( |
| 717 "probability", port_alternate_protocol->probability); |
| 698 server_pref_dict->SetWithoutPathExpansion( | 718 server_pref_dict->SetWithoutPathExpansion( |
| 699 "alternate_protocol", port_alternate_protocol_dict); | 719 "alternate_protocol", port_alternate_protocol_dict); |
| 700 } | 720 } |
| 701 | 721 |
| 702 servers_dict->SetWithoutPathExpansion(server.ToString(), server_pref_dict); | 722 servers_dict->SetWithoutPathExpansion(server.ToString(), server_pref_dict); |
| 703 } | 723 } |
| 704 | 724 |
| 705 http_server_properties_dict.SetWithoutPathExpansion("servers", servers_dict); | 725 http_server_properties_dict.SetWithoutPathExpansion("servers", servers_dict); |
| 706 SetVersion(&http_server_properties_dict, kVersionNumber); | 726 SetVersion(&http_server_properties_dict, kVersionNumber); |
| 707 setting_prefs_ = true; | 727 setting_prefs_ = true; |
| 708 pref_service_->Set(prefs::kHttpServerProperties, | 728 pref_service_->Set(prefs::kHttpServerProperties, |
| 709 http_server_properties_dict); | 729 http_server_properties_dict); |
| 710 setting_prefs_ = false; | 730 setting_prefs_ = false; |
| 711 | 731 |
| 712 // Note that |completion| will be fired after we have written everything to | 732 // Note that |completion| will be fired after we have written everything to |
| 713 // the Preferences, but likely before these changes are serialized to disk. | 733 // the Preferences, but likely before these changes are serialized to disk. |
| 714 // This is not a problem though, as JSONPrefStore guarantees that this will | 734 // This is not a problem though, as JSONPrefStore guarantees that this will |
| 715 // happen, pretty soon, and even in the case we shut down immediately. | 735 // happen, pretty soon, and even in the case we shut down immediately. |
| 716 if (!completion.is_null()) | 736 if (!completion.is_null()) |
| 717 completion.Run(); | 737 completion.Run(); |
| 718 } | 738 } |
| 719 | 739 |
| 720 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { | 740 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { |
| 721 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 741 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 722 if (!setting_prefs_) | 742 if (!setting_prefs_) |
| 723 ScheduleUpdateCacheOnUI(); | 743 ScheduleUpdateCacheOnUI(); |
| 724 } | 744 } |
| 725 | 745 |
| 726 } // namespace chrome_browser_net | 746 } // namespace chrome_browser_net |
| OLD | NEW |