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