OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/net/http_server_properties_manager.h" | 4 #include "chrome/browser/net/http_server_properties_manager.h" |
5 | 5 |
6 #include "base/bind.h" | 6 #include "base/bind.h" |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/prefs/pref_service.h" | 9 #include "chrome/browser/prefs/pref_service.h" |
10 #include "chrome/common/chrome_notification_types.h" | 10 #include "chrome/common/chrome_notification_types.h" |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 http_server_properties_impl_->ClearSpdySettings(); | 164 http_server_properties_impl_->ClearSpdySettings(); |
165 ScheduleUpdatePrefsOnIO(); | 165 ScheduleUpdatePrefsOnIO(); |
166 } | 166 } |
167 | 167 |
168 const net::SpdySettingsMap& | 168 const net::SpdySettingsMap& |
169 HttpServerPropertiesManager::spdy_settings_map() const { | 169 HttpServerPropertiesManager::spdy_settings_map() const { |
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
171 return http_server_properties_impl_->spdy_settings_map(); | 171 return http_server_properties_impl_->spdy_settings_map(); |
172 } | 172 } |
173 | 173 |
174 net::HttpPipelinedHostCapability | |
175 HttpServerPropertiesManager::GetPipelineCapability( | |
176 const net::HostPortPair& origin) { | |
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
178 return http_server_properties_impl_->GetPipelineCapability(origin); | |
179 } | |
180 | |
181 void HttpServerPropertiesManager::SetPipelineCapability( | |
182 const net::HostPortPair& origin, | |
183 net::HttpPipelinedHostCapability capability) { | |
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
185 http_server_properties_impl_->SetPipelineCapability(origin, capability); | |
186 ScheduleUpdatePrefsOnIO(); | |
187 } | |
188 | |
189 void HttpServerPropertiesManager::ClearPipelineCapabilities() { | |
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
191 http_server_properties_impl_->ClearPipelineCapabilities(); | |
192 ScheduleUpdatePrefsOnIO(); | |
193 } | |
194 | |
195 net::PipelineCapabilityMap | |
196 HttpServerPropertiesManager::GetPipelineCapabilityMap() const { | |
197 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
198 return http_server_properties_impl_->GetPipelineCapabilityMap(); | |
199 } | |
200 | |
201 // | 174 // |
202 // Update the HttpServerPropertiesImpl's cache with data from preferences. | 175 // Update the HttpServerPropertiesImpl's cache with data from preferences. |
203 // | 176 // |
204 void HttpServerPropertiesManager::ScheduleUpdateCacheOnUI() { | 177 void HttpServerPropertiesManager::ScheduleUpdateCacheOnUI() { |
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
206 // Cancel pending updates, if any. | 179 // Cancel pending updates, if any. |
207 ui_cache_update_timer_->Stop(); | 180 ui_cache_update_timer_->Stop(); |
208 StartCacheUpdateTimerOnUI( | 181 StartCacheUpdateTimerOnUI( |
209 base::TimeDelta::FromMilliseconds(kUpdateCacheDelayMs)); | 182 base::TimeDelta::FromMilliseconds(kUpdateCacheDelayMs)); |
210 } | 183 } |
(...skipping 16 matching lines...) Expand all Loading... |
227 // String is host/port pair of spdy server. | 200 // String is host/port pair of spdy server. |
228 StringVector* spdy_servers = new StringVector; | 201 StringVector* spdy_servers = new StringVector; |
229 | 202 |
230 // Parse the preferences into a SpdySettingsMap. | 203 // Parse the preferences into a SpdySettingsMap. |
231 net::SpdySettingsMap* spdy_settings_map = new net::SpdySettingsMap; | 204 net::SpdySettingsMap* spdy_settings_map = new net::SpdySettingsMap; |
232 | 205 |
233 // Parse the preferences into a AlternateProtocolMap. | 206 // Parse the preferences into a AlternateProtocolMap. |
234 net::AlternateProtocolMap* alternate_protocol_map = | 207 net::AlternateProtocolMap* alternate_protocol_map = |
235 new net::AlternateProtocolMap; | 208 new net::AlternateProtocolMap; |
236 | 209 |
237 net::PipelineCapabilityMap* pipeline_capability_map = | |
238 new net::PipelineCapabilityMap; | |
239 | |
240 const base::DictionaryValue& http_server_properties_dict = | 210 const base::DictionaryValue& http_server_properties_dict = |
241 *pref_service_->GetDictionary(prefs::kHttpServerProperties); | 211 *pref_service_->GetDictionary(prefs::kHttpServerProperties); |
242 for (base::DictionaryValue::key_iterator it = | 212 for (base::DictionaryValue::key_iterator it = |
243 http_server_properties_dict.begin_keys(); | 213 http_server_properties_dict.begin_keys(); |
244 it != http_server_properties_dict.end_keys(); ++it) { | 214 it != http_server_properties_dict.end_keys(); ++it) { |
245 // Get server's host/pair. | 215 // Get server's host/pair. |
246 const std::string& server_str = *it; | 216 const std::string& server_str = *it; |
247 net::HostPortPair server = net::HostPortPair::FromString(server_str); | 217 net::HostPortPair server = net::HostPortPair::FromString(server_str); |
248 if (server.host().empty()) { | 218 if (server.host().empty()) { |
249 DVLOG(1) << "Malformed http_server_properties for server: " << server_str; | 219 DVLOG(1) << "Malformed http_server_properties for server: " << server_str; |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 spdy::SettingsFlagsAndId flags_and_id(0); | 274 spdy::SettingsFlagsAndId flags_and_id(0); |
305 flags_and_id.set_id(id); | 275 flags_and_id.set_id(id); |
306 flags_and_id.set_flags(spdy::SETTINGS_FLAG_PERSISTED); | 276 flags_and_id.set_flags(spdy::SETTINGS_FLAG_PERSISTED); |
307 | 277 |
308 spdy_settings.push_back(spdy::SpdySetting(flags_and_id, value)); | 278 spdy_settings.push_back(spdy::SpdySetting(flags_and_id, value)); |
309 } | 279 } |
310 | 280 |
311 (*spdy_settings_map)[server] = spdy_settings; | 281 (*spdy_settings_map)[server] = spdy_settings; |
312 } | 282 } |
313 | 283 |
314 int pipeline_capability = net::PIPELINE_UNKNOWN; | |
315 if ((server_pref_dict->GetInteger( | |
316 "pipeline_capability", &pipeline_capability)) && | |
317 pipeline_capability != net::PIPELINE_UNKNOWN) { | |
318 (*pipeline_capability_map)[server] = | |
319 static_cast<net::HttpPipelinedHostCapability>(pipeline_capability); | |
320 } | |
321 | |
322 // Get alternate_protocol server. | 284 // Get alternate_protocol server. |
323 DCHECK(!ContainsKey(*alternate_protocol_map, server)); | 285 DCHECK(!ContainsKey(*alternate_protocol_map, server)); |
324 base::DictionaryValue* port_alternate_protocol_dict = NULL; | 286 base::DictionaryValue* port_alternate_protocol_dict = NULL; |
325 if (!server_pref_dict->GetDictionaryWithoutPathExpansion( | 287 if (!server_pref_dict->GetDictionaryWithoutPathExpansion( |
326 "alternate_protocol", &port_alternate_protocol_dict)) { | 288 "alternate_protocol", &port_alternate_protocol_dict)) { |
327 continue; | 289 continue; |
328 } | 290 } |
329 | 291 |
330 do { | 292 do { |
331 int port = 0; | 293 int port = 0; |
(...skipping 22 matching lines...) Expand all Loading... |
354 } | 316 } |
355 | 317 |
356 BrowserThread::PostTask( | 318 BrowserThread::PostTask( |
357 BrowserThread::IO, | 319 BrowserThread::IO, |
358 FROM_HERE, | 320 FROM_HERE, |
359 base::Bind(&HttpServerPropertiesManager:: | 321 base::Bind(&HttpServerPropertiesManager:: |
360 UpdateCacheFromPrefsOnIO, | 322 UpdateCacheFromPrefsOnIO, |
361 base::Unretained(this), | 323 base::Unretained(this), |
362 base::Owned(spdy_servers), | 324 base::Owned(spdy_servers), |
363 base::Owned(spdy_settings_map), | 325 base::Owned(spdy_settings_map), |
364 base::Owned(alternate_protocol_map), | 326 base::Owned(alternate_protocol_map))); |
365 base::Owned(pipeline_capability_map))); | |
366 } | 327 } |
367 | 328 |
368 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO( | 329 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO( |
369 StringVector* spdy_servers, | 330 StringVector* spdy_servers, |
370 net::SpdySettingsMap* spdy_settings_map, | 331 net::SpdySettingsMap* spdy_settings_map, |
371 net::AlternateProtocolMap* alternate_protocol_map, | 332 net::AlternateProtocolMap* alternate_protocol_map) { |
372 net::PipelineCapabilityMap* pipeline_capability_map) { | |
373 // Preferences have the master data because admins might have pushed new | 333 // Preferences have the master data because admins might have pushed new |
374 // preferences. Update the cached data with new data from preferences. | 334 // preferences. Update the cached data with new data from preferences. |
375 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 335 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
376 | 336 |
377 http_server_properties_impl_->InitializeSpdyServers(spdy_servers, true); | 337 http_server_properties_impl_->InitializeSpdyServers(spdy_servers, true); |
378 | 338 |
379 // Clear the cached data and use the new spdy_settings from preferences. | 339 // Clear the cached data and use the new spdy_settings from preferences. |
380 http_server_properties_impl_->InitializeSpdySettingsServers( | 340 http_server_properties_impl_->InitializeSpdySettingsServers( |
381 spdy_settings_map); | 341 spdy_settings_map); |
382 | 342 |
383 // Clear the cached data and use the new Alternate-Protocol server list from | 343 // Clear the cached data and use the new Alternate-Protocol server list from |
384 // preferences. | 344 // preferences. |
385 http_server_properties_impl_->InitializeAlternateProtocolServers( | 345 http_server_properties_impl_->InitializeAlternateProtocolServers( |
386 alternate_protocol_map); | 346 alternate_protocol_map); |
387 | |
388 http_server_properties_impl_->InitializePipelineCapabilities( | |
389 pipeline_capability_map); | |
390 } | 347 } |
391 | 348 |
392 | 349 |
393 // | 350 // |
394 // Update Preferences with data from the cached data. | 351 // Update Preferences with data from the cached data. |
395 // | 352 // |
396 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO() { | 353 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO() { |
397 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
398 // Cancel pending updates, if any. | 355 // Cancel pending updates, if any. |
399 io_prefs_update_timer_->Stop(); | 356 io_prefs_update_timer_->Stop(); |
(...skipping 17 matching lines...) Expand all Loading... |
417 http_server_properties_impl_->GetSpdyServerList(spdy_server_list); | 374 http_server_properties_impl_->GetSpdyServerList(spdy_server_list); |
418 | 375 |
419 net::SpdySettingsMap* spdy_settings_map = new net::SpdySettingsMap; | 376 net::SpdySettingsMap* spdy_settings_map = new net::SpdySettingsMap; |
420 *spdy_settings_map = http_server_properties_impl_->spdy_settings_map(); | 377 *spdy_settings_map = http_server_properties_impl_->spdy_settings_map(); |
421 | 378 |
422 net::AlternateProtocolMap* alternate_protocol_map = | 379 net::AlternateProtocolMap* alternate_protocol_map = |
423 new net::AlternateProtocolMap; | 380 new net::AlternateProtocolMap; |
424 *alternate_protocol_map = | 381 *alternate_protocol_map = |
425 http_server_properties_impl_->alternate_protocol_map(); | 382 http_server_properties_impl_->alternate_protocol_map(); |
426 | 383 |
427 net::PipelineCapabilityMap* pipeline_capability_map = | |
428 new net::PipelineCapabilityMap; | |
429 *pipeline_capability_map = | |
430 http_server_properties_impl_->GetPipelineCapabilityMap(); | |
431 | |
432 // Update the preferences on the UI thread. | 384 // Update the preferences on the UI thread. |
433 BrowserThread::PostTask( | 385 BrowserThread::PostTask( |
434 BrowserThread::UI, | 386 BrowserThread::UI, |
435 FROM_HERE, | 387 FROM_HERE, |
436 base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnUI, | 388 base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnUI, |
437 ui_weak_ptr_, | 389 ui_weak_ptr_, |
438 base::Owned(spdy_server_list), | 390 base::Owned(spdy_server_list), |
439 base::Owned(spdy_settings_map), | 391 base::Owned(spdy_settings_map), |
440 base::Owned(alternate_protocol_map), | 392 base::Owned(alternate_protocol_map))); |
441 base::Owned(pipeline_capability_map))); | |
442 } | 393 } |
443 | 394 |
444 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, | 395 // A local or temporary data structure to hold supports_spdy, SpdySettings and |
445 // PortAlternateProtocolPair, and |pipeline_capability| preferences for a | 396 // PortAlternateProtocolPair preferences for a server. This is used only in |
446 // server. This is used only in UpdatePrefsOnUI. | 397 // UpdatePrefsOnUI. |
447 struct ServerPref { | 398 struct ServerPref { |
448 ServerPref() | 399 ServerPref() |
449 : supports_spdy(false), | 400 : supports_spdy(false), |
450 settings(NULL), | 401 settings(NULL), |
451 alternate_protocol(NULL), | 402 alternate_protocol(NULL) { |
452 pipeline_capability(net::PIPELINE_UNKNOWN) { | |
453 } | 403 } |
454 ServerPref(bool supports_spdy, | 404 ServerPref(bool supports_spdy, |
455 const spdy::SpdySettings* settings, | 405 const spdy::SpdySettings* settings, |
456 const net::PortAlternateProtocolPair* alternate_protocol) | 406 const net::PortAlternateProtocolPair* alternate_protocol) |
457 : supports_spdy(supports_spdy), | 407 : supports_spdy(supports_spdy), |
458 settings(settings), | 408 settings(settings), |
459 alternate_protocol(alternate_protocol), | 409 alternate_protocol(alternate_protocol) { |
460 pipeline_capability(net::PIPELINE_UNKNOWN) { | |
461 } | 410 } |
462 bool supports_spdy; | 411 bool supports_spdy; |
463 const spdy::SpdySettings* settings; | 412 const spdy::SpdySettings* settings; |
464 const net::PortAlternateProtocolPair* alternate_protocol; | 413 const net::PortAlternateProtocolPair* alternate_protocol; |
465 net::HttpPipelinedHostCapability pipeline_capability; | |
466 }; | 414 }; |
467 | 415 |
468 void HttpServerPropertiesManager::UpdatePrefsOnUI( | 416 void HttpServerPropertiesManager::UpdatePrefsOnUI( |
469 base::ListValue* spdy_server_list, | 417 base::ListValue* spdy_server_list, |
470 net::SpdySettingsMap* spdy_settings_map, | 418 net::SpdySettingsMap* spdy_settings_map, |
471 net::AlternateProtocolMap* alternate_protocol_map, | 419 net::AlternateProtocolMap* alternate_protocol_map) { |
472 net::PipelineCapabilityMap* pipeline_capability_map) { | |
473 | 420 |
474 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; | 421 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; |
475 ServerPrefMap server_pref_map; | 422 ServerPrefMap server_pref_map; |
476 | 423 |
477 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 424 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
478 | 425 |
479 // Add servers that support spdy to server_pref_map. | 426 // Add servers that support spdy to server_pref_map. |
480 std::string s; | 427 std::string s; |
481 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); | 428 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); |
482 list_it != spdy_server_list->end(); ++list_it) { | 429 list_it != spdy_server_list->end(); ++list_it) { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 | 469 |
523 ServerPrefMap::iterator it = server_pref_map.find(server); | 470 ServerPrefMap::iterator it = server_pref_map.find(server); |
524 if (it == server_pref_map.end()) { | 471 if (it == server_pref_map.end()) { |
525 ServerPref server_pref(false, NULL, &map_it->second); | 472 ServerPref server_pref(false, NULL, &map_it->second); |
526 server_pref_map[server] = server_pref; | 473 server_pref_map[server] = server_pref; |
527 } else { | 474 } else { |
528 it->second.alternate_protocol = &map_it->second; | 475 it->second.alternate_protocol = &map_it->second; |
529 } | 476 } |
530 } | 477 } |
531 | 478 |
532 for (net::PipelineCapabilityMap::const_iterator map_it = | |
533 pipeline_capability_map->begin(); | |
534 map_it != pipeline_capability_map->end(); ++map_it) { | |
535 const net::HostPortPair& server = map_it->first; | |
536 const net::HttpPipelinedHostCapability& pipeline_capability = | |
537 map_it->second; | |
538 | |
539 ServerPrefMap::iterator it = server_pref_map.find(server); | |
540 if (it == server_pref_map.end()) { | |
541 ServerPref server_pref; | |
542 server_pref.pipeline_capability = pipeline_capability; | |
543 server_pref_map[server] = server_pref; | |
544 } else { | |
545 it->second.pipeline_capability = pipeline_capability; | |
546 } | |
547 } | |
548 | |
549 // Persist the prefs::kHttpServerProperties. | 479 // Persist the prefs::kHttpServerProperties. |
550 base::DictionaryValue http_server_properties_dict; | 480 base::DictionaryValue http_server_properties_dict; |
551 for (ServerPrefMap::const_iterator map_it = | 481 for (ServerPrefMap::const_iterator map_it = |
552 server_pref_map.begin(); | 482 server_pref_map.begin(); |
553 map_it != server_pref_map.end(); ++map_it) { | 483 map_it != server_pref_map.end(); ++map_it) { |
554 const net::HostPortPair& server = map_it->first; | 484 const net::HostPortPair& server = map_it->first; |
555 const ServerPref& server_pref = map_it->second; | 485 const ServerPref& server_pref = map_it->second; |
556 | 486 |
557 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; | 487 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; |
558 | 488 |
(...skipping 22 matching lines...) Expand all Loading... |
581 new base::DictionaryValue; | 511 new base::DictionaryValue; |
582 const net::PortAlternateProtocolPair* port_alternate_protocol = | 512 const net::PortAlternateProtocolPair* port_alternate_protocol = |
583 server_pref.alternate_protocol; | 513 server_pref.alternate_protocol; |
584 port_alternate_protocol_dict->SetInteger( | 514 port_alternate_protocol_dict->SetInteger( |
585 "port", port_alternate_protocol->port); | 515 "port", port_alternate_protocol->port); |
586 port_alternate_protocol_dict->SetInteger( | 516 port_alternate_protocol_dict->SetInteger( |
587 "protocol", port_alternate_protocol->protocol); | 517 "protocol", port_alternate_protocol->protocol); |
588 server_pref_dict->SetWithoutPathExpansion( | 518 server_pref_dict->SetWithoutPathExpansion( |
589 "alternate_protocol", port_alternate_protocol_dict); | 519 "alternate_protocol", port_alternate_protocol_dict); |
590 } | 520 } |
591 | 521 http_server_properties_dict.SetWithoutPathExpansion( |
592 if (server_pref.pipeline_capability != net::PIPELINE_UNKNOWN) { | 522 server.ToString(), server_pref_dict); |
593 server_pref_dict->SetInteger("pipeline_capability", | |
594 server_pref.pipeline_capability); | |
595 } | |
596 | |
597 http_server_properties_dict.SetWithoutPathExpansion(server.ToString(), | |
598 server_pref_dict); | |
599 } | 523 } |
600 | 524 |
601 setting_prefs_ = true; | 525 setting_prefs_ = true; |
602 pref_service_->Set(prefs::kHttpServerProperties, | 526 pref_service_->Set(prefs::kHttpServerProperties, |
603 http_server_properties_dict); | 527 http_server_properties_dict); |
604 setting_prefs_ = false; | 528 setting_prefs_ = false; |
605 } | 529 } |
606 | 530 |
607 void HttpServerPropertiesManager::Observe( | 531 void HttpServerPropertiesManager::Observe( |
608 int type, | 532 int type, |
609 const content::NotificationSource& source, | 533 const content::NotificationSource& source, |
610 const content::NotificationDetails& details) { | 534 const content::NotificationDetails& details) { |
611 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 535 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
612 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED); | 536 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED); |
613 PrefService* prefs = content::Source<PrefService>(source).ptr(); | 537 PrefService* prefs = content::Source<PrefService>(source).ptr(); |
614 DCHECK(prefs == pref_service_); | 538 DCHECK(prefs == pref_service_); |
615 std::string* pref_name = content::Details<std::string>(details).ptr(); | 539 std::string* pref_name = content::Details<std::string>(details).ptr(); |
616 if (*pref_name == prefs::kHttpServerProperties) { | 540 if (*pref_name == prefs::kHttpServerProperties) { |
617 if (!setting_prefs_) | 541 if (!setting_prefs_) |
618 ScheduleUpdateCacheOnUI(); | 542 ScheduleUpdateCacheOnUI(); |
619 } else { | 543 } else { |
620 NOTREACHED(); | 544 NOTREACHED(); |
621 } | 545 } |
622 } | 546 } |
623 | 547 |
624 } // namespace chrome_browser_net | 548 } // namespace chrome_browser_net |
OLD | NEW |