Chromium Code Reviews| 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 "net/http/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" |
| 11 #include "base/single_thread_task_runner.h" | |
| 11 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/thread_task_runner_handle.h" | |
| 14 #include "base/values.h" | 16 #include "base/values.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | |
| 16 #include "chrome/common/pref_names.h" | |
| 17 #include "components/pref_registry/pref_registry_syncable.h" | |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 #include "content/public/browser/notification_details.h" | |
| 20 #include "content/public/browser/notification_source.h" | |
| 21 | 17 |
| 22 using content::BrowserThread; | 18 namespace net { |
| 23 | |
| 24 namespace chrome_browser_net { | |
| 25 | 19 |
| 26 namespace { | 20 namespace { |
| 27 | 21 |
| 28 // Time to wait before starting an update the http_server_properties_impl_ cache | 22 // Time to wait before starting an update the http_server_properties_impl_ cache |
| 29 // from preferences. Scheduling another update during this period will reset the | 23 // from preferences. Scheduling another update during this period will reset the |
| 30 // timer. | 24 // timer. |
| 31 const int64 kUpdateCacheDelayMs = 1000; | 25 const int64 kUpdateCacheDelayMs = 1000; |
| 32 | 26 |
| 33 // Time to wait before starting an update the preferences from the | 27 // Time to wait before starting an update the preferences from the |
| 34 // http_server_properties_impl_ cache. Scheduling another update during this | 28 // http_server_properties_impl_ cache. Scheduling another update during this |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 55 | 49 |
| 56 // Persist 300 MRU SupportsSpdyServerHostPortPairs. | 50 // Persist 300 MRU SupportsSpdyServerHostPortPairs. |
| 57 const int kMaxSupportsSpdyServerHostsToPersist = 300; | 51 const int kMaxSupportsSpdyServerHostsToPersist = 300; |
| 58 | 52 |
| 59 } // namespace | 53 } // namespace |
| 60 | 54 |
| 61 //////////////////////////////////////////////////////////////////////////////// | 55 //////////////////////////////////////////////////////////////////////////////// |
| 62 // HttpServerPropertiesManager | 56 // HttpServerPropertiesManager |
| 63 | 57 |
| 64 HttpServerPropertiesManager::HttpServerPropertiesManager( | 58 HttpServerPropertiesManager::HttpServerPropertiesManager( |
| 65 PrefService* pref_service) | 59 PrefService* pref_service, |
| 66 : pref_service_(pref_service), | 60 const char* pref_path, |
| 67 setting_prefs_(false) { | 61 scoped_refptr<base::SequencedTaskRunner> network_task_runner) |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 62 : pref_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 63 pref_service_(pref_service), | |
| 64 setting_prefs_(false), | |
| 65 path_(pref_path), | |
| 66 network_task_runner_(network_task_runner) { | |
| 69 DCHECK(pref_service); | 67 DCHECK(pref_service); |
| 70 ui_weak_ptr_factory_.reset( | 68 pref_weak_ptr_factory_.reset( |
| 71 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); | 69 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); |
| 72 ui_weak_ptr_ = ui_weak_ptr_factory_->GetWeakPtr(); | 70 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr(); |
| 73 ui_cache_update_timer_.reset( | 71 pref_cache_update_timer_.reset( |
| 74 new base::OneShotTimer<HttpServerPropertiesManager>); | 72 new base::OneShotTimer<HttpServerPropertiesManager>); |
| 75 pref_change_registrar_.Init(pref_service_); | 73 pref_change_registrar_.Init(pref_service_); |
| 76 pref_change_registrar_.Add( | 74 pref_change_registrar_.Add( |
| 77 prefs::kHttpServerProperties, | 75 path_, |
| 78 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, | 76 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, |
| 79 base::Unretained(this))); | 77 base::Unretained(this))); |
| 80 } | 78 } |
| 81 | 79 |
| 82 HttpServerPropertiesManager::~HttpServerPropertiesManager() { | 80 HttpServerPropertiesManager::~HttpServerPropertiesManager() { |
| 83 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 81 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 84 io_weak_ptr_factory_.reset(); | 82 network_weak_ptr_factory_.reset(); |
| 85 } | 83 } |
| 86 | 84 |
| 87 void HttpServerPropertiesManager::InitializeOnIOThread() { | 85 void HttpServerPropertiesManager::InitializeOnNetworkThread() { |
| 88 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 86 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 89 io_weak_ptr_factory_.reset( | 87 network_weak_ptr_factory_.reset( |
| 90 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); | 88 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); |
| 91 http_server_properties_impl_.reset(new net::HttpServerPropertiesImpl()); | 89 http_server_properties_impl_.reset(new net::HttpServerPropertiesImpl()); |
| 92 | 90 |
| 93 io_prefs_update_timer_.reset( | 91 network_prefs_update_timer_.reset( |
| 94 new base::OneShotTimer<HttpServerPropertiesManager>); | 92 new base::OneShotTimer<HttpServerPropertiesManager>); |
| 95 | 93 |
| 96 BrowserThread::PostTask( | 94 pref_task_runner_->PostTask( |
| 97 BrowserThread::UI, | |
| 98 FROM_HERE, | 95 FROM_HERE, |
| 99 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI, | 96 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread, |
| 100 ui_weak_ptr_)); | 97 pref_weak_ptr_)); |
| 101 } | 98 } |
| 102 | 99 |
| 103 void HttpServerPropertiesManager::ShutdownOnUIThread() { | 100 void HttpServerPropertiesManager::ShutdownOnPrefThread() { |
| 104 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 101 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
| 105 // Cancel any pending updates, and stop listening for pref change updates. | 102 // Cancel any pending updates, and stop listening for pref change updates. |
| 106 ui_cache_update_timer_->Stop(); | 103 pref_cache_update_timer_->Stop(); |
| 107 ui_weak_ptr_factory_.reset(); | 104 pref_weak_ptr_factory_.reset(); |
| 108 pref_change_registrar_.RemoveAll(); | 105 pref_change_registrar_.RemoveAll(); |
| 109 } | 106 } |
| 110 | 107 |
| 111 // static | 108 // static |
| 112 void HttpServerPropertiesManager::RegisterProfilePrefs( | |
| 113 user_prefs::PrefRegistrySyncable* prefs) { | |
| 114 prefs->RegisterDictionaryPref( | |
| 115 prefs::kHttpServerProperties, | |
| 116 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
| 117 } | |
| 118 | |
| 119 // static | |
| 120 void HttpServerPropertiesManager::SetVersion( | 109 void HttpServerPropertiesManager::SetVersion( |
| 121 base::DictionaryValue* http_server_properties_dict, | 110 base::DictionaryValue* http_server_properties_dict, |
| 122 int version_number) { | 111 int version_number) { |
| 123 if (version_number < 0) | 112 if (version_number < 0) |
| 124 version_number = kVersionNumber; | 113 version_number = kVersionNumber; |
| 125 DCHECK_LE(version_number, kVersionNumber); | 114 DCHECK_LE(version_number, kVersionNumber); |
| 126 if (version_number <= kVersionNumber) | 115 if (version_number <= kVersionNumber) |
| 127 http_server_properties_dict->SetInteger("version", version_number); | 116 http_server_properties_dict->SetInteger("version", version_number); |
| 128 } | 117 } |
| 129 | 118 |
| 130 // This is required for conformance with the HttpServerProperties interface. | 119 // This is required for conformance with the HttpServerProperties interface. |
| 131 base::WeakPtr<net::HttpServerProperties> | 120 base::WeakPtr<net::HttpServerProperties> |
| 132 HttpServerPropertiesManager::GetWeakPtr() { | 121 HttpServerPropertiesManager::GetWeakPtr() { |
| 133 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 122 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 134 return io_weak_ptr_factory_->GetWeakPtr(); | 123 return network_weak_ptr_factory_->GetWeakPtr(); |
| 135 } | 124 } |
| 136 | 125 |
| 137 void HttpServerPropertiesManager::Clear() { | 126 void HttpServerPropertiesManager::Clear() { |
| 138 Clear(base::Closure()); | 127 Clear(base::Closure()); |
| 139 } | 128 } |
| 140 | 129 |
| 141 void HttpServerPropertiesManager::Clear(const base::Closure& completion) { | 130 void HttpServerPropertiesManager::Clear(const base::Closure& completion) { |
| 142 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 131 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 143 | 132 |
| 144 http_server_properties_impl_->Clear(); | 133 http_server_properties_impl_->Clear(); |
| 145 UpdatePrefsFromCacheOnIO(completion); | 134 UpdatePrefsFromCacheOnNetworkThread(completion); |
| 146 } | 135 } |
| 147 | 136 |
| 148 bool HttpServerPropertiesManager::SupportsSpdy( | 137 bool HttpServerPropertiesManager::SupportsSpdy( |
| 149 const net::HostPortPair& server) { | 138 const net::HostPortPair& server) { |
| 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 139 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 151 return http_server_properties_impl_->SupportsSpdy(server); | 140 return http_server_properties_impl_->SupportsSpdy(server); |
| 152 } | 141 } |
| 153 | 142 |
| 154 void HttpServerPropertiesManager::SetSupportsSpdy( | 143 void HttpServerPropertiesManager::SetSupportsSpdy( |
| 155 const net::HostPortPair& server, | 144 const net::HostPortPair& server, |
| 156 bool support_spdy) { | 145 bool support_spdy) { |
| 157 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 146 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 158 | 147 |
| 159 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy); | 148 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy); |
| 160 ScheduleUpdatePrefsOnIO(); | 149 ScheduleUpdatePrefsOnNetworkThread(); |
| 161 } | 150 } |
| 162 | 151 |
| 163 bool HttpServerPropertiesManager::HasAlternateProtocol( | 152 bool HttpServerPropertiesManager::HasAlternateProtocol( |
| 164 const net::HostPortPair& server) { | 153 const net::HostPortPair& server) { |
| 165 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 154 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 166 return http_server_properties_impl_->HasAlternateProtocol(server); | 155 return http_server_properties_impl_->HasAlternateProtocol(server); |
| 167 } | 156 } |
| 168 | 157 |
| 169 net::AlternateProtocolInfo | 158 net::AlternateProtocolInfo |
| 170 HttpServerPropertiesManager::GetAlternateProtocol( | 159 HttpServerPropertiesManager::GetAlternateProtocol( |
| 171 const net::HostPortPair& server) { | 160 const net::HostPortPair& server) { |
| 172 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 161 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 173 return http_server_properties_impl_->GetAlternateProtocol(server); | 162 return http_server_properties_impl_->GetAlternateProtocol(server); |
| 174 } | 163 } |
| 175 | 164 |
| 176 void HttpServerPropertiesManager::SetAlternateProtocol( | 165 void HttpServerPropertiesManager::SetAlternateProtocol( |
| 177 const net::HostPortPair& server, | 166 const net::HostPortPair& server, |
| 178 uint16 alternate_port, | 167 uint16 alternate_port, |
| 179 net::AlternateProtocol alternate_protocol, | 168 AlternateProtocol alternate_protocol, |
| 180 double alternate_probability) { | 169 double alternate_probability) { |
| 181 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 170 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 182 http_server_properties_impl_->SetAlternateProtocol( | 171 http_server_properties_impl_->SetAlternateProtocol( |
| 183 server, alternate_port, alternate_protocol, alternate_probability); | 172 server, alternate_port, alternate_protocol, alternate_probability); |
| 184 ScheduleUpdatePrefsOnIO(); | 173 ScheduleUpdatePrefsOnNetworkThread(); |
| 185 } | 174 } |
| 186 | 175 |
| 187 void HttpServerPropertiesManager::SetBrokenAlternateProtocol( | 176 void HttpServerPropertiesManager::SetBrokenAlternateProtocol( |
| 188 const net::HostPortPair& server) { | 177 const net::HostPortPair& server) { |
| 189 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 178 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 190 http_server_properties_impl_->SetBrokenAlternateProtocol(server); | 179 http_server_properties_impl_->SetBrokenAlternateProtocol(server); |
| 191 ScheduleUpdatePrefsOnIO(); | 180 ScheduleUpdatePrefsOnNetworkThread(); |
| 192 } | 181 } |
| 193 | 182 |
| 194 bool HttpServerPropertiesManager::WasAlternateProtocolRecentlyBroken( | 183 bool HttpServerPropertiesManager::WasAlternateProtocolRecentlyBroken( |
| 195 const net::HostPortPair& server) { | 184 const net::HostPortPair& server) { |
| 196 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 185 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 197 return http_server_properties_impl_->WasAlternateProtocolRecentlyBroken( | 186 return http_server_properties_impl_->WasAlternateProtocolRecentlyBroken( |
| 198 server); | 187 server); |
| 199 } | 188 } |
| 200 | 189 |
| 201 void HttpServerPropertiesManager::ConfirmAlternateProtocol( | 190 void HttpServerPropertiesManager::ConfirmAlternateProtocol( |
| 202 const net::HostPortPair& server) { | 191 const net::HostPortPair& server) { |
| 203 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 192 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 204 http_server_properties_impl_->ConfirmAlternateProtocol(server); | 193 http_server_properties_impl_->ConfirmAlternateProtocol(server); |
| 205 ScheduleUpdatePrefsOnIO(); | 194 ScheduleUpdatePrefsOnNetworkThread(); |
| 206 } | 195 } |
| 207 | 196 |
| 208 void HttpServerPropertiesManager::ClearAlternateProtocol( | 197 void HttpServerPropertiesManager::ClearAlternateProtocol( |
| 209 const net::HostPortPair& server) { | 198 const net::HostPortPair& server) { |
| 210 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 199 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 211 http_server_properties_impl_->ClearAlternateProtocol(server); | 200 http_server_properties_impl_->ClearAlternateProtocol(server); |
| 212 ScheduleUpdatePrefsOnIO(); | 201 ScheduleUpdatePrefsOnNetworkThread(); |
| 213 } | 202 } |
| 214 | 203 |
| 215 const net::AlternateProtocolMap& | 204 const net::AlternateProtocolMap& |
| 216 HttpServerPropertiesManager::alternate_protocol_map() const { | 205 HttpServerPropertiesManager::alternate_protocol_map() const { |
| 217 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 206 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 218 return http_server_properties_impl_->alternate_protocol_map(); | 207 return http_server_properties_impl_->alternate_protocol_map(); |
| 219 } | 208 } |
| 220 | 209 |
| 221 void HttpServerPropertiesManager::SetAlternateProtocolExperiment( | 210 void HttpServerPropertiesManager::SetAlternateProtocolExperiment( |
| 222 net::AlternateProtocolExperiment experiment) { | 211 AlternateProtocolExperiment experiment) { |
| 223 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 212 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 224 http_server_properties_impl_->SetAlternateProtocolExperiment(experiment); | 213 http_server_properties_impl_->SetAlternateProtocolExperiment(experiment); |
| 225 } | 214 } |
| 226 | 215 |
| 227 void HttpServerPropertiesManager::SetAlternateProtocolProbabilityThreshold( | 216 void HttpServerPropertiesManager::SetAlternateProtocolProbabilityThreshold( |
| 228 double threshold) { | 217 double threshold) { |
| 229 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 218 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 230 http_server_properties_impl_->SetAlternateProtocolProbabilityThreshold( | 219 http_server_properties_impl_->SetAlternateProtocolProbabilityThreshold( |
| 231 threshold); | 220 threshold); |
| 232 } | 221 } |
| 233 | 222 |
| 234 net::AlternateProtocolExperiment | 223 AlternateProtocolExperiment |
| 235 HttpServerPropertiesManager::GetAlternateProtocolExperiment() const { | 224 HttpServerPropertiesManager::GetAlternateProtocolExperiment() const { |
| 236 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 225 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 237 return http_server_properties_impl_->GetAlternateProtocolExperiment(); | 226 return http_server_properties_impl_->GetAlternateProtocolExperiment(); |
| 238 } | 227 } |
| 239 | 228 |
| 240 const net::SettingsMap& | 229 const SettingsMap& HttpServerPropertiesManager::GetSpdySettings( |
| 241 HttpServerPropertiesManager::GetSpdySettings( | 230 const HostPortPair& host_port_pair) { |
| 242 const net::HostPortPair& host_port_pair) { | 231 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 243 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 244 return http_server_properties_impl_->GetSpdySettings(host_port_pair); | 232 return http_server_properties_impl_->GetSpdySettings(host_port_pair); |
| 245 } | 233 } |
| 246 | 234 |
| 247 bool HttpServerPropertiesManager::SetSpdySetting( | 235 bool HttpServerPropertiesManager::SetSpdySetting( |
| 248 const net::HostPortPair& host_port_pair, | 236 const HostPortPair& host_port_pair, |
| 249 net::SpdySettingsIds id, | 237 SpdySettingsIds id, |
| 250 net::SpdySettingsFlags flags, | 238 SpdySettingsFlags flags, |
| 251 uint32 value) { | 239 uint32 value) { |
| 252 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 240 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 253 bool persist = http_server_properties_impl_->SetSpdySetting( | 241 bool persist = http_server_properties_impl_->SetSpdySetting( |
| 254 host_port_pair, id, flags, value); | 242 host_port_pair, id, flags, value); |
| 255 if (persist) | 243 if (persist) |
| 256 ScheduleUpdatePrefsOnIO(); | 244 ScheduleUpdatePrefsOnNetworkThread(); |
| 257 return persist; | 245 return persist; |
| 258 } | 246 } |
| 259 | 247 |
| 260 void HttpServerPropertiesManager::ClearSpdySettings( | 248 void HttpServerPropertiesManager::ClearSpdySettings( |
| 261 const net::HostPortPair& host_port_pair) { | 249 const HostPortPair& host_port_pair) { |
| 262 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 250 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 263 http_server_properties_impl_->ClearSpdySettings(host_port_pair); | 251 http_server_properties_impl_->ClearSpdySettings(host_port_pair); |
| 264 ScheduleUpdatePrefsOnIO(); | 252 ScheduleUpdatePrefsOnNetworkThread(); |
| 265 } | 253 } |
| 266 | 254 |
| 267 void HttpServerPropertiesManager::ClearAllSpdySettings() { | 255 void HttpServerPropertiesManager::ClearAllSpdySettings() { |
| 268 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 256 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 269 http_server_properties_impl_->ClearAllSpdySettings(); | 257 http_server_properties_impl_->ClearAllSpdySettings(); |
| 270 ScheduleUpdatePrefsOnIO(); | 258 ScheduleUpdatePrefsOnNetworkThread(); |
| 271 } | 259 } |
| 272 | 260 |
| 273 const net::SpdySettingsMap& | 261 const SpdySettingsMap& HttpServerPropertiesManager::spdy_settings_map() |
| 274 HttpServerPropertiesManager::spdy_settings_map() const { | 262 const { |
| 275 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 263 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 276 return http_server_properties_impl_->spdy_settings_map(); | 264 return http_server_properties_impl_->spdy_settings_map(); |
| 277 } | 265 } |
| 278 | 266 |
| 279 void HttpServerPropertiesManager::SetServerNetworkStats( | 267 void HttpServerPropertiesManager::SetServerNetworkStats( |
| 280 const net::HostPortPair& host_port_pair, | 268 const net::HostPortPair& host_port_pair, |
| 281 NetworkStats stats) { | 269 NetworkStats stats) { |
| 282 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 270 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 283 http_server_properties_impl_->SetServerNetworkStats(host_port_pair, stats); | 271 http_server_properties_impl_->SetServerNetworkStats(host_port_pair, stats); |
| 284 } | 272 } |
| 285 | 273 |
| 286 const HttpServerPropertiesManager::NetworkStats* | 274 const HttpServerPropertiesManager::NetworkStats* |
| 287 HttpServerPropertiesManager::GetServerNetworkStats( | 275 HttpServerPropertiesManager::GetServerNetworkStats( |
| 288 const net::HostPortPair& host_port_pair) const { | 276 const net::HostPortPair& host_port_pair) const { |
| 289 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 277 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 290 return http_server_properties_impl_->GetServerNetworkStats(host_port_pair); | 278 return http_server_properties_impl_->GetServerNetworkStats(host_port_pair); |
| 291 } | 279 } |
| 292 | 280 |
| 293 // | 281 // |
| 294 // Update the HttpServerPropertiesImpl's cache with data from preferences. | 282 // Update the HttpServerPropertiesImpl's cache with data from preferences. |
| 295 // | 283 // |
| 296 void HttpServerPropertiesManager::ScheduleUpdateCacheOnUI() { | 284 void HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread() { |
| 297 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 285 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
| 298 // Cancel pending updates, if any. | 286 // Cancel pending updates, if any. |
| 299 ui_cache_update_timer_->Stop(); | 287 pref_cache_update_timer_->Stop(); |
| 300 StartCacheUpdateTimerOnUI( | 288 StartCacheUpdateTimerOnPrefThread( |
| 301 base::TimeDelta::FromMilliseconds(kUpdateCacheDelayMs)); | 289 base::TimeDelta::FromMilliseconds(kUpdateCacheDelayMs)); |
| 302 } | 290 } |
| 303 | 291 |
| 304 void HttpServerPropertiesManager::StartCacheUpdateTimerOnUI( | 292 void HttpServerPropertiesManager::StartCacheUpdateTimerOnPrefThread( |
| 305 base::TimeDelta delay) { | 293 base::TimeDelta delay) { |
| 306 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 294 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
| 307 ui_cache_update_timer_->Start( | 295 pref_cache_update_timer_->Start( |
| 308 FROM_HERE, delay, this, | 296 FROM_HERE, |
| 309 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI); | 297 delay, |
| 298 this, | |
| 299 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread); | |
| 310 } | 300 } |
| 311 | 301 |
| 312 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI() { | 302 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread() { |
| 313 // The preferences can only be read on the UI thread. | 303 // The preferences can only be read on the UI thread. |
|
Ryan Sleevi
2014/07/09 19:29:58
s/UI/pref/
mef
2014/07/10 08:50:14
Done.
| |
| 314 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 304 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
| 315 | 305 |
| 316 if (!pref_service_->HasPrefPath(prefs::kHttpServerProperties)) | 306 if (!pref_service_->HasPrefPath(path_)) |
| 317 return; | 307 return; |
| 318 | 308 |
| 319 bool detected_corrupted_prefs = false; | 309 bool detected_corrupted_prefs = false; |
| 320 const base::DictionaryValue& http_server_properties_dict = | 310 const base::DictionaryValue& http_server_properties_dict = |
| 321 *pref_service_->GetDictionary(prefs::kHttpServerProperties); | 311 *pref_service_->GetDictionary(path_); |
| 322 | 312 |
| 323 int version = kMissingVersion; | 313 int version = kMissingVersion; |
| 324 if (!http_server_properties_dict.GetIntegerWithoutPathExpansion( | 314 if (!http_server_properties_dict.GetIntegerWithoutPathExpansion("version", |
| 325 "version", &version)) { | 315 &version)) { |
| 326 DVLOG(1) << "Missing version. Clearing all properties."; | 316 DVLOG(1) << "Missing version. Clearing all properties."; |
| 327 return; | 317 return; |
| 328 } | 318 } |
| 329 | 319 |
| 330 // The properties for a given server is in | 320 // The properties for a given server is in |
| 331 // http_server_properties_dict["servers"][server]. | 321 // http_server_properties_dict["servers"][server]. |
| 332 const base::DictionaryValue* servers_dict = NULL; | 322 const base::DictionaryValue* servers_dict = NULL; |
| 333 if (!http_server_properties_dict.GetDictionaryWithoutPathExpansion( | 323 if (!http_server_properties_dict.GetDictionaryWithoutPathExpansion( |
| 334 "servers", &servers_dict)) { | 324 "servers", &servers_dict)) { |
| 335 DVLOG(1) << "Malformed http_server_properties for servers."; | 325 DVLOG(1) << "Malformed http_server_properties for servers."; |
| 336 return; | 326 return; |
| 337 } | 327 } |
| 338 | 328 |
| 339 // String is host/port pair of spdy server. | 329 // String is host/port pair of spdy server. |
| 340 scoped_ptr<StringVector> spdy_servers(new StringVector); | 330 scoped_ptr<StringVector> spdy_servers(new StringVector); |
| 341 scoped_ptr<net::SpdySettingsMap> spdy_settings_map( | 331 scoped_ptr<net::SpdySettingsMap> spdy_settings_map( |
| 342 new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist)); | 332 new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist)); |
| 343 scoped_ptr<net::AlternateProtocolMap> alternate_protocol_map( | 333 scoped_ptr<net::AlternateProtocolMap> alternate_protocol_map( |
| 344 new net::AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist)); | 334 new net::AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist)); |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 373 | 363 |
| 374 const base::DictionaryValue* server_pref_dict = NULL; | 364 const base::DictionaryValue* server_pref_dict = NULL; |
| 375 if (!it.value().GetAsDictionary(&server_pref_dict)) { | 365 if (!it.value().GetAsDictionary(&server_pref_dict)) { |
| 376 DVLOG(1) << "Malformed http_server_properties server: " << server_str; | 366 DVLOG(1) << "Malformed http_server_properties server: " << server_str; |
| 377 detected_corrupted_prefs = true; | 367 detected_corrupted_prefs = true; |
| 378 continue; | 368 continue; |
| 379 } | 369 } |
| 380 | 370 |
| 381 // Get if server supports Spdy. | 371 // Get if server supports Spdy. |
| 382 bool supports_spdy = false; | 372 bool supports_spdy = false; |
| 383 if ((server_pref_dict->GetBoolean( | 373 if ((server_pref_dict->GetBoolean("supports_spdy", &supports_spdy)) && |
| 384 "supports_spdy", &supports_spdy)) && supports_spdy) { | 374 supports_spdy) { |
| 385 spdy_servers->push_back(server_str); | 375 spdy_servers->push_back(server_str); |
| 386 } | 376 } |
| 387 | 377 |
| 388 // Get SpdySettings. | 378 // Get SpdySettings. |
| 389 DCHECK(spdy_settings_map->Peek(server) == spdy_settings_map->end()); | 379 DCHECK(spdy_settings_map->Peek(server) == spdy_settings_map->end()); |
| 390 const base::DictionaryValue* spdy_settings_dict = NULL; | 380 const base::DictionaryValue* spdy_settings_dict = NULL; |
| 391 if (server_pref_dict->GetDictionaryWithoutPathExpansion( | 381 if (server_pref_dict->GetDictionaryWithoutPathExpansion( |
| 392 "settings", &spdy_settings_dict)) { | 382 "settings", &spdy_settings_dict)) { |
| 393 net::SettingsMap settings_map; | 383 net::SettingsMap settings_map; |
| 394 for (base::DictionaryValue::Iterator dict_it(*spdy_settings_dict); | 384 for (base::DictionaryValue::Iterator dict_it(*spdy_settings_dict); |
| 395 !dict_it.IsAtEnd(); dict_it.Advance()) { | 385 !dict_it.IsAtEnd(); |
| 386 dict_it.Advance()) { | |
| 396 const std::string& id_str = dict_it.key(); | 387 const std::string& id_str = dict_it.key(); |
| 397 int id = 0; | 388 int id = 0; |
| 398 if (!base::StringToInt(id_str, &id)) { | 389 if (!base::StringToInt(id_str, &id)) { |
| 399 DVLOG(1) << "Malformed id in SpdySettings for server: " << | 390 DVLOG(1) << "Malformed id in SpdySettings for server: " << server_str; |
| 400 server_str; | |
| 401 NOTREACHED(); | 391 NOTREACHED(); |
| 402 continue; | 392 continue; |
| 403 } | 393 } |
| 404 int value = 0; | 394 int value = 0; |
| 405 if (!dict_it.value().GetAsInteger(&value)) { | 395 if (!dict_it.value().GetAsInteger(&value)) { |
| 406 DVLOG(1) << "Malformed value in SpdySettings for server: " << | 396 DVLOG(1) << "Malformed value in SpdySettings for server: " |
| 407 server_str; | 397 << server_str; |
| 408 NOTREACHED(); | 398 NOTREACHED(); |
| 409 continue; | 399 continue; |
| 410 } | 400 } |
| 411 net::SettingsFlagsAndValue flags_and_value( | 401 net::SettingsFlagsAndValue flags_and_value(net::SETTINGS_FLAG_PERSISTED, |
| 412 net::SETTINGS_FLAG_PERSISTED, value); | 402 value); |
| 413 settings_map[static_cast<net::SpdySettingsIds>(id)] = flags_and_value; | 403 settings_map[static_cast<net::SpdySettingsIds>(id)] = flags_and_value; |
| 414 } | 404 } |
| 415 spdy_settings_map->Put(server, settings_map); | 405 spdy_settings_map->Put(server, settings_map); |
| 416 } | 406 } |
| 417 | 407 |
| 418 // Get alternate_protocol server. | 408 // Get alternate_protocol server. |
| 419 DCHECK(alternate_protocol_map->Peek(server) == | 409 DCHECK(alternate_protocol_map->Peek(server) == |
| 420 alternate_protocol_map->end()); | 410 alternate_protocol_map->end()); |
| 421 const base::DictionaryValue* port_alternate_protocol_dict = NULL; | 411 const base::DictionaryValue* port_alternate_protocol_dict = NULL; |
| 422 if (!server_pref_dict->GetDictionaryWithoutPathExpansion( | 412 if (!server_pref_dict->GetDictionaryWithoutPathExpansion( |
| 423 "alternate_protocol", &port_alternate_protocol_dict)) { | 413 "alternate_protocol", &port_alternate_protocol_dict)) { |
| 424 continue; | 414 continue; |
| 425 } | 415 } |
| 426 | 416 |
| 427 if (count >= alternate_protocols_to_load) | 417 if (count >= alternate_protocols_to_load) |
| 428 continue; | 418 continue; |
| 429 do { | 419 do { |
| 430 int port = 0; | 420 int port = 0; |
| 431 if (!port_alternate_protocol_dict->GetIntegerWithoutPathExpansion( | 421 if (!port_alternate_protocol_dict->GetIntegerWithoutPathExpansion( |
| 432 "port", &port) || (port > (1 << 16))) { | 422 "port", &port) || |
| 423 (port > (1 << 16))) { | |
| 433 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; | 424 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; |
| 434 detected_corrupted_prefs = true; | 425 detected_corrupted_prefs = true; |
| 435 continue; | 426 continue; |
| 436 } | 427 } |
| 437 std::string protocol_str; | 428 std::string protocol_str; |
| 438 if (!port_alternate_protocol_dict->GetStringWithoutPathExpansion( | 429 if (!port_alternate_protocol_dict->GetStringWithoutPathExpansion( |
| 439 "protocol_str", &protocol_str)) { | 430 "protocol_str", &protocol_str)) { |
| 440 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; | 431 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; |
| 441 detected_corrupted_prefs = true; | 432 detected_corrupted_prefs = true; |
| 442 continue; | 433 continue; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 459 } | 450 } |
| 460 | 451 |
| 461 net::AlternateProtocolInfo port_alternate_protocol(port, | 452 net::AlternateProtocolInfo port_alternate_protocol(port, |
| 462 protocol, | 453 protocol, |
| 463 probability); | 454 probability); |
| 464 alternate_protocol_map->Put(server, port_alternate_protocol); | 455 alternate_protocol_map->Put(server, port_alternate_protocol); |
| 465 ++count; | 456 ++count; |
| 466 } while (false); | 457 } while (false); |
| 467 } | 458 } |
| 468 | 459 |
| 469 BrowserThread::PostTask( | 460 network_task_runner_->PostTask( |
| 470 BrowserThread::IO, | |
| 471 FROM_HERE, | 461 FROM_HERE, |
| 472 base::Bind(&HttpServerPropertiesManager:: | 462 base::Bind( |
| 473 UpdateCacheFromPrefsOnIO, | 463 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread, |
| 474 base::Unretained(this), | 464 base::Unretained(this), |
| 475 base::Owned(spdy_servers.release()), | 465 base::Owned(spdy_servers.release()), |
| 476 base::Owned(spdy_settings_map.release()), | 466 base::Owned(spdy_settings_map.release()), |
| 477 base::Owned(alternate_protocol_map.release()), | 467 base::Owned(alternate_protocol_map.release()), |
| 478 alternate_protocol_experiment, | 468 alternate_protocol_experiment, |
| 479 detected_corrupted_prefs)); | 469 detected_corrupted_prefs)); |
| 480 } | 470 } |
| 481 | 471 |
| 482 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO( | 472 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread( |
| 483 StringVector* spdy_servers, | 473 StringVector* spdy_servers, |
| 484 net::SpdySettingsMap* spdy_settings_map, | 474 net::SpdySettingsMap* spdy_settings_map, |
| 485 net::AlternateProtocolMap* alternate_protocol_map, | 475 net::AlternateProtocolMap* alternate_protocol_map, |
| 486 net::AlternateProtocolExperiment alternate_protocol_experiment, | 476 net::AlternateProtocolExperiment alternate_protocol_experiment, |
| 487 bool detected_corrupted_prefs) { | 477 bool detected_corrupted_prefs) { |
| 488 // Preferences have the master data because admins might have pushed new | 478 // Preferences have the master data because admins might have pushed new |
| 489 // preferences. Update the cached data with new data from preferences. | 479 // preferences. Update the cached data with new data from preferences. |
| 490 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 480 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 491 | 481 |
| 492 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size()); | 482 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size()); |
| 493 http_server_properties_impl_->InitializeSpdyServers(spdy_servers, true); | 483 http_server_properties_impl_->InitializeSpdyServers(spdy_servers, true); |
| 494 | 484 |
| 495 // Update the cached data and use the new spdy_settings from preferences. | 485 // Update the cached data and use the new spdy_settings from preferences. |
| 496 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdySettings", spdy_settings_map->size()); | 486 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdySettings", spdy_settings_map->size()); |
| 497 http_server_properties_impl_->InitializeSpdySettingsServers( | 487 http_server_properties_impl_->InitializeSpdySettingsServers( |
| 498 spdy_settings_map); | 488 spdy_settings_map); |
| 499 | 489 |
| 500 // Update the cached data and use the new Alternate-Protocol server list from | 490 // Update the cached data and use the new Alternate-Protocol server list from |
| 501 // preferences. | 491 // preferences. |
| 502 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers", | 492 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers", |
| 503 alternate_protocol_map->size()); | 493 alternate_protocol_map->size()); |
| 504 http_server_properties_impl_->InitializeAlternateProtocolServers( | 494 http_server_properties_impl_->InitializeAlternateProtocolServers( |
| 505 alternate_protocol_map); | 495 alternate_protocol_map); |
| 506 http_server_properties_impl_->SetAlternateProtocolExperiment( | 496 http_server_properties_impl_->SetAlternateProtocolExperiment( |
| 507 alternate_protocol_experiment); | 497 alternate_protocol_experiment); |
| 508 | 498 |
| 509 // Update the prefs with what we have read (delete all corrupted prefs). | 499 // Update the prefs with what we have read (delete all corrupted prefs). |
| 510 if (detected_corrupted_prefs) | 500 if (detected_corrupted_prefs) |
| 511 ScheduleUpdatePrefsOnIO(); | 501 ScheduleUpdatePrefsOnNetworkThread(); |
| 512 } | 502 } |
| 513 | 503 |
| 514 | |
| 515 // | 504 // |
| 516 // Update Preferences with data from the cached data. | 505 // Update Preferences with data from the cached data. |
| 517 // | 506 // |
| 518 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO() { | 507 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread() { |
| 519 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 508 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 520 // Cancel pending updates, if any. | 509 // Cancel pending updates, if any. |
| 521 io_prefs_update_timer_->Stop(); | 510 network_prefs_update_timer_->Stop(); |
| 522 StartPrefsUpdateTimerOnIO( | 511 StartPrefsUpdateTimerOnNetworkThread( |
| 523 base::TimeDelta::FromMilliseconds(kUpdatePrefsDelayMs)); | 512 base::TimeDelta::FromMilliseconds(kUpdatePrefsDelayMs)); |
| 524 } | 513 } |
| 525 | 514 |
| 526 void HttpServerPropertiesManager::StartPrefsUpdateTimerOnIO( | 515 void HttpServerPropertiesManager::StartPrefsUpdateTimerOnNetworkThread( |
| 527 base::TimeDelta delay) { | 516 base::TimeDelta delay) { |
| 528 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 517 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 529 // This is overridden in tests to post the task without the delay. | 518 // This is overridden in tests to post the task without the delay. |
| 530 io_prefs_update_timer_->Start( | 519 network_prefs_update_timer_->Start( |
| 531 FROM_HERE, delay, this, | 520 FROM_HERE, |
| 532 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO); | 521 delay, |
| 522 this, | |
| 523 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread); | |
| 533 } | 524 } |
| 534 | 525 |
| 535 // This is required so we can set this as the callback for a timer. | 526 // This is required so we can set this as the callback for a timer. |
| 536 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO() { | 527 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread() { |
| 537 UpdatePrefsFromCacheOnIO(base::Closure()); | 528 UpdatePrefsFromCacheOnNetworkThread(base::Closure()); |
| 538 } | 529 } |
| 539 | 530 |
| 540 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO( | 531 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread( |
| 541 const base::Closure& completion) { | 532 const base::Closure& completion) { |
| 542 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 533 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
| 543 | 534 |
| 544 base::ListValue* spdy_server_list = new base::ListValue; | 535 base::ListValue* spdy_server_list = new base::ListValue; |
| 545 http_server_properties_impl_->GetSpdyServerList( | 536 http_server_properties_impl_->GetSpdyServerList( |
| 546 spdy_server_list, kMaxSupportsSpdyServerHostsToPersist); | 537 spdy_server_list, kMaxSupportsSpdyServerHostsToPersist); |
| 547 | 538 |
| 548 net::SpdySettingsMap* spdy_settings_map = | 539 net::SpdySettingsMap* spdy_settings_map = |
| 549 new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist); | 540 new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist); |
| 550 const net::SpdySettingsMap& main_map = | 541 const net::SpdySettingsMap& main_map = |
| 551 http_server_properties_impl_->spdy_settings_map(); | 542 http_server_properties_impl_->spdy_settings_map(); |
| 552 int count = 0; | 543 int count = 0; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 571 http_server_properties_impl_->GetCanonicalSuffix(server); | 562 http_server_properties_impl_->GetCanonicalSuffix(server); |
| 572 if (!canonical_suffix.empty()) { | 563 if (!canonical_suffix.empty()) { |
| 573 if (persisted_map.find(canonical_suffix) != persisted_map.end()) | 564 if (persisted_map.find(canonical_suffix) != persisted_map.end()) |
| 574 continue; | 565 continue; |
| 575 persisted_map[canonical_suffix] = true; | 566 persisted_map[canonical_suffix] = true; |
| 576 } | 567 } |
| 577 alternate_protocol_map->Put(server, it->second); | 568 alternate_protocol_map->Put(server, it->second); |
| 578 ++count; | 569 ++count; |
| 579 } | 570 } |
| 580 | 571 |
| 581 // Update the preferences on the UI thread. | 572 // Update the preferences on the UI thread. |
|
Ryan Sleevi
2014/07/09 19:29:57
s/UI/pref/
mef
2014/07/10 08:50:14
Done.
| |
| 582 BrowserThread::PostTask( | 573 pref_task_runner_->PostTask( |
| 583 BrowserThread::UI, | |
| 584 FROM_HERE, | 574 FROM_HERE, |
| 585 base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnUI, | 575 base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnPrefThread, |
| 586 ui_weak_ptr_, | 576 pref_weak_ptr_, |
| 587 base::Owned(spdy_server_list), | 577 base::Owned(spdy_server_list), |
| 588 base::Owned(spdy_settings_map), | 578 base::Owned(spdy_settings_map), |
| 589 base::Owned(alternate_protocol_map), | 579 base::Owned(alternate_protocol_map), |
| 590 completion)); | 580 completion)); |
| 591 } | 581 } |
| 592 | 582 |
| 593 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, | 583 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, |
| 594 // and AlternateProtocolInfo preferences for a server. This is used only in | 584 // and AlternateProtocolInfo preferences for a server. This is used only in |
| 595 // UpdatePrefsOnUI. | 585 // UpdatePrefsOnUI. |
|
Ryan Sleevi
2014/07/09 19:29:57
s/UpdatePrefsOnUI/UpdatePrefsOnPrefThread/
mef
2014/07/10 08:50:14
Done.
| |
| 596 struct ServerPref { | 586 struct ServerPref { |
| 597 ServerPref() | 587 ServerPref() |
| 598 : supports_spdy(false), | 588 : supports_spdy(false), settings_map(NULL), alternate_protocol(NULL) {} |
| 599 settings_map(NULL), | |
| 600 alternate_protocol(NULL) { | |
| 601 } | |
| 602 ServerPref(bool supports_spdy, | 589 ServerPref(bool supports_spdy, |
| 603 const net::SettingsMap* settings_map, | 590 const net::SettingsMap* settings_map, |
| 604 const net::AlternateProtocolInfo* alternate_protocol) | 591 const net::AlternateProtocolInfo* alternate_protocol) |
| 605 : supports_spdy(supports_spdy), | 592 : supports_spdy(supports_spdy), |
| 606 settings_map(settings_map), | 593 settings_map(settings_map), |
| 607 alternate_protocol(alternate_protocol) { | 594 alternate_protocol(alternate_protocol) {} |
| 608 } | |
| 609 bool supports_spdy; | 595 bool supports_spdy; |
| 610 const net::SettingsMap* settings_map; | 596 const net::SettingsMap* settings_map; |
| 611 const net::AlternateProtocolInfo* alternate_protocol; | 597 const net::AlternateProtocolInfo* alternate_protocol; |
| 612 }; | 598 }; |
| 613 | 599 |
| 614 void HttpServerPropertiesManager::UpdatePrefsOnUI( | 600 void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( |
| 615 base::ListValue* spdy_server_list, | 601 base::ListValue* spdy_server_list, |
| 616 net::SpdySettingsMap* spdy_settings_map, | 602 net::SpdySettingsMap* spdy_settings_map, |
| 617 net::AlternateProtocolMap* alternate_protocol_map, | 603 net::AlternateProtocolMap* alternate_protocol_map, |
| 618 const base::Closure& completion) { | 604 const base::Closure& completion) { |
| 619 | |
| 620 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; | 605 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; |
| 621 ServerPrefMap server_pref_map; | 606 ServerPrefMap server_pref_map; |
| 622 | 607 |
| 623 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 608 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
| 624 | 609 |
| 625 // Add servers that support spdy to server_pref_map. | 610 // Add servers that support spdy to server_pref_map. |
| 626 std::string s; | 611 std::string s; |
| 627 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); | 612 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); |
| 628 list_it != spdy_server_list->end(); ++list_it) { | 613 list_it != spdy_server_list->end(); |
| 614 ++list_it) { | |
| 629 if ((*list_it)->GetAsString(&s)) { | 615 if ((*list_it)->GetAsString(&s)) { |
| 630 net::HostPortPair server = net::HostPortPair::FromString(s); | 616 net::HostPortPair server = net::HostPortPair::FromString(s); |
| 631 | 617 |
| 632 ServerPrefMap::iterator it = server_pref_map.find(server); | 618 ServerPrefMap::iterator it = server_pref_map.find(server); |
| 633 if (it == server_pref_map.end()) { | 619 if (it == server_pref_map.end()) { |
| 634 ServerPref server_pref(true, NULL, NULL); | 620 ServerPref server_pref(true, NULL, NULL); |
| 635 server_pref_map[server] = server_pref; | 621 server_pref_map[server] = server_pref; |
| 636 } else { | 622 } else { |
| 637 it->second.supports_spdy = true; | 623 it->second.supports_spdy = true; |
| 638 } | 624 } |
| 639 } | 625 } |
| 640 } | 626 } |
| 641 | 627 |
| 642 // Add servers that have SpdySettings to server_pref_map. | 628 // Add servers that have SpdySettings to server_pref_map. |
| 643 for (net::SpdySettingsMap::iterator map_it = spdy_settings_map->begin(); | 629 for (net::SpdySettingsMap::iterator map_it = spdy_settings_map->begin(); |
| 644 map_it != spdy_settings_map->end(); ++map_it) { | 630 map_it != spdy_settings_map->end(); |
| 631 ++map_it) { | |
| 645 const net::HostPortPair& server = map_it->first; | 632 const net::HostPortPair& server = map_it->first; |
| 646 | 633 |
| 647 ServerPrefMap::iterator it = server_pref_map.find(server); | 634 ServerPrefMap::iterator it = server_pref_map.find(server); |
| 648 if (it == server_pref_map.end()) { | 635 if (it == server_pref_map.end()) { |
| 649 ServerPref server_pref(false, &map_it->second, NULL); | 636 ServerPref server_pref(false, &map_it->second, NULL); |
| 650 server_pref_map[server] = server_pref; | 637 server_pref_map[server] = server_pref; |
| 651 } else { | 638 } else { |
| 652 it->second.settings_map = &map_it->second; | 639 it->second.settings_map = &map_it->second; |
| 653 } | 640 } |
| 654 } | 641 } |
| 655 | 642 |
| 656 // Add AlternateProtocol servers to server_pref_map. | 643 // Add AlternateProtocol servers to server_pref_map. |
| 657 for (net::AlternateProtocolMap::const_iterator map_it = | 644 for (net::AlternateProtocolMap::const_iterator map_it = |
| 658 alternate_protocol_map->begin(); | 645 alternate_protocol_map->begin(); |
| 659 map_it != alternate_protocol_map->end(); ++map_it) { | 646 map_it != alternate_protocol_map->end(); |
| 647 ++map_it) { | |
| 660 const net::HostPortPair& server = map_it->first; | 648 const net::HostPortPair& server = map_it->first; |
| 661 const net::AlternateProtocolInfo& port_alternate_protocol = | 649 const net::AlternateProtocolInfo& port_alternate_protocol = |
| 662 map_it->second; | 650 map_it->second; |
| 663 if (!net::IsAlternateProtocolValid(port_alternate_protocol.protocol)) { | 651 if (!net::IsAlternateProtocolValid(port_alternate_protocol.protocol)) { |
| 664 continue; | 652 continue; |
| 665 } | 653 } |
| 666 | 654 |
| 667 ServerPrefMap::iterator it = server_pref_map.find(server); | 655 ServerPrefMap::iterator it = server_pref_map.find(server); |
| 668 if (it == server_pref_map.end()) { | 656 if (it == server_pref_map.end()) { |
| 669 ServerPref server_pref(false, NULL, &map_it->second); | 657 ServerPref server_pref(false, NULL, &map_it->second); |
| 670 server_pref_map[server] = server_pref; | 658 server_pref_map[server] = server_pref; |
| 671 } else { | 659 } else { |
| 672 it->second.alternate_protocol = &map_it->second; | 660 it->second.alternate_protocol = &map_it->second; |
| 673 } | 661 } |
| 674 } | 662 } |
| 675 | 663 |
| 676 // Persist the prefs::kHttpServerProperties. | 664 // Persist properties to the |path_|. |
| 677 base::DictionaryValue http_server_properties_dict; | 665 base::DictionaryValue http_server_properties_dict; |
| 678 base::DictionaryValue* servers_dict = new base::DictionaryValue; | 666 base::DictionaryValue* servers_dict = new base::DictionaryValue; |
| 679 for (ServerPrefMap::const_iterator map_it = | 667 for (ServerPrefMap::const_iterator map_it = server_pref_map.begin(); |
| 680 server_pref_map.begin(); | 668 map_it != server_pref_map.end(); |
| 681 map_it != server_pref_map.end(); ++map_it) { | 669 ++map_it) { |
| 682 const net::HostPortPair& server = map_it->first; | 670 const net::HostPortPair& server = map_it->first; |
| 683 const ServerPref& server_pref = map_it->second; | 671 const ServerPref& server_pref = map_it->second; |
| 684 | 672 |
| 685 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; | 673 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; |
| 686 | 674 |
| 687 // Save supports_spdy. | 675 // Save supports_spdy. |
| 688 if (server_pref.supports_spdy) | 676 if (server_pref.supports_spdy) |
| 689 server_pref_dict->SetBoolean("supports_spdy", server_pref.supports_spdy); | 677 server_pref_dict->SetBoolean("supports_spdy", server_pref.supports_spdy); |
| 690 | 678 |
| 691 // Save SPDY settings. | 679 // Save SPDY settings. |
| 692 if (server_pref.settings_map) { | 680 if (server_pref.settings_map) { |
| 693 base::DictionaryValue* spdy_settings_dict = new base::DictionaryValue; | 681 base::DictionaryValue* spdy_settings_dict = new base::DictionaryValue; |
| 694 for (net::SettingsMap::const_iterator it = | 682 for (net::SettingsMap::const_iterator it = |
| 695 server_pref.settings_map->begin(); | 683 server_pref.settings_map->begin(); |
| 696 it != server_pref.settings_map->end(); ++it) { | 684 it != server_pref.settings_map->end(); |
| 685 ++it) { | |
| 697 net::SpdySettingsIds id = it->first; | 686 net::SpdySettingsIds id = it->first; |
| 698 uint32 value = it->second.second; | 687 uint32 value = it->second.second; |
| 699 std::string key = base::StringPrintf("%u", id); | 688 std::string key = base::StringPrintf("%u", id); |
| 700 spdy_settings_dict->SetInteger(key, value); | 689 spdy_settings_dict->SetInteger(key, value); |
| 701 } | 690 } |
| 702 server_pref_dict->SetWithoutPathExpansion("settings", spdy_settings_dict); | 691 server_pref_dict->SetWithoutPathExpansion("settings", spdy_settings_dict); |
| 703 } | 692 } |
| 704 | 693 |
| 705 // Save alternate_protocol. | 694 // Save alternate_protocol. |
| 706 if (server_pref.alternate_protocol) { | 695 if (server_pref.alternate_protocol) { |
| 707 base::DictionaryValue* port_alternate_protocol_dict = | 696 base::DictionaryValue* port_alternate_protocol_dict = |
| 708 new base::DictionaryValue; | 697 new base::DictionaryValue; |
| 709 const net::AlternateProtocolInfo* port_alternate_protocol = | 698 const net::AlternateProtocolInfo* port_alternate_protocol = |
| 710 server_pref.alternate_protocol; | 699 server_pref.alternate_protocol; |
| 711 port_alternate_protocol_dict->SetInteger( | 700 port_alternate_protocol_dict->SetInteger("port", |
| 712 "port", port_alternate_protocol->port); | 701 port_alternate_protocol->port); |
| 713 const char* protocol_str = | 702 const char* protocol_str = |
| 714 net::AlternateProtocolToString(port_alternate_protocol->protocol); | 703 net::AlternateProtocolToString(port_alternate_protocol->protocol); |
| 715 port_alternate_protocol_dict->SetString("protocol_str", protocol_str); | 704 port_alternate_protocol_dict->SetString("protocol_str", protocol_str); |
| 716 port_alternate_protocol_dict->SetDouble( | 705 port_alternate_protocol_dict->SetDouble( |
| 717 "probability", port_alternate_protocol->probability); | 706 "probability", port_alternate_protocol->probability); |
| 718 server_pref_dict->SetWithoutPathExpansion( | 707 server_pref_dict->SetWithoutPathExpansion( |
| 719 "alternate_protocol", port_alternate_protocol_dict); | 708 "alternate_protocol", port_alternate_protocol_dict); |
| 720 } | 709 } |
| 721 | 710 |
| 722 servers_dict->SetWithoutPathExpansion(server.ToString(), server_pref_dict); | 711 servers_dict->SetWithoutPathExpansion(server.ToString(), server_pref_dict); |
| 723 } | 712 } |
| 724 | 713 |
| 725 http_server_properties_dict.SetWithoutPathExpansion("servers", servers_dict); | 714 http_server_properties_dict.SetWithoutPathExpansion("servers", servers_dict); |
| 726 SetVersion(&http_server_properties_dict, kVersionNumber); | 715 SetVersion(&http_server_properties_dict, kVersionNumber); |
| 727 setting_prefs_ = true; | 716 setting_prefs_ = true; |
| 728 pref_service_->Set(prefs::kHttpServerProperties, | 717 pref_service_->Set(path_, http_server_properties_dict); |
| 729 http_server_properties_dict); | |
| 730 setting_prefs_ = false; | 718 setting_prefs_ = false; |
| 731 | 719 |
| 732 // Note that |completion| will be fired after we have written everything to | 720 // Note that |completion| will be fired after we have written everything to |
| 733 // the Preferences, but likely before these changes are serialized to disk. | 721 // the Preferences, but likely before these changes are serialized to disk. |
| 734 // This is not a problem though, as JSONPrefStore guarantees that this will | 722 // This is not a problem though, as JSONPrefStore guarantees that this will |
| 735 // happen, pretty soon, and even in the case we shut down immediately. | 723 // happen, pretty soon, and even in the case we shut down immediately. |
| 736 if (!completion.is_null()) | 724 if (!completion.is_null()) |
| 737 completion.Run(); | 725 completion.Run(); |
| 738 } | 726 } |
| 739 | 727 |
| 740 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { | 728 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { |
| 741 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 729 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
| 742 if (!setting_prefs_) | 730 if (!setting_prefs_) |
| 743 ScheduleUpdateCacheOnUI(); | 731 ScheduleUpdateCacheOnPrefThread(); |
| 744 } | 732 } |
| 745 | 733 |
| 746 } // namespace chrome_browser_net | 734 } // namespace net |
| OLD | NEW |