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 #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/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 ui_weak_ptr_factory_.reset(); | 84 ui_weak_ptr_factory_.reset(); |
85 pref_change_registrar_.RemoveAll(); | 85 pref_change_registrar_.RemoveAll(); |
86 } | 86 } |
87 | 87 |
88 // static | 88 // static |
89 void HttpServerPropertiesManager::RegisterPrefs(PrefService* prefs) { | 89 void HttpServerPropertiesManager::RegisterPrefs(PrefService* prefs) { |
90 prefs->RegisterDictionaryPref(prefs::kHttpServerProperties, | 90 prefs->RegisterDictionaryPref(prefs::kHttpServerProperties, |
91 PrefService::UNSYNCABLE_PREF); | 91 PrefService::UNSYNCABLE_PREF); |
92 } | 92 } |
93 | 93 |
94 // This is required for conformance with the HttpServerProperties interface. | |
94 void HttpServerPropertiesManager::Clear() { | 95 void HttpServerPropertiesManager::Clear() { |
96 Clear(base::Closure()); | |
97 } | |
98 | |
99 void HttpServerPropertiesManager::Clear(const base::Closure& completion) { | |
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
96 | 101 |
97 http_server_properties_impl_->Clear(); | 102 http_server_properties_impl_->Clear(); |
98 ScheduleUpdatePrefsOnIO(); | 103 UpdatePrefsFromCacheOnIO(completion); |
99 } | 104 } |
100 | 105 |
101 bool HttpServerPropertiesManager::SupportsSpdy( | 106 bool HttpServerPropertiesManager::SupportsSpdy( |
102 const net::HostPortPair& server) const { | 107 const net::HostPortPair& server) const { |
103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
104 return http_server_properties_impl_->SupportsSpdy(server); | 109 return http_server_properties_impl_->SupportsSpdy(server); |
105 } | 110 } |
106 | 111 |
107 void HttpServerPropertiesManager::SetSupportsSpdy( | 112 void HttpServerPropertiesManager::SetSupportsSpdy( |
108 const net::HostPortPair& server, | 113 const net::HostPortPair& server, |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
434 | 439 |
435 void HttpServerPropertiesManager::StartPrefsUpdateTimerOnIO( | 440 void HttpServerPropertiesManager::StartPrefsUpdateTimerOnIO( |
436 base::TimeDelta delay) { | 441 base::TimeDelta delay) { |
437 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 442 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
438 // This is overridden in tests to post the task without the delay. | 443 // This is overridden in tests to post the task without the delay. |
439 io_prefs_update_timer_->Start( | 444 io_prefs_update_timer_->Start( |
440 FROM_HERE, delay, this, | 445 FROM_HERE, delay, this, |
441 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO); | 446 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO); |
442 } | 447 } |
443 | 448 |
449 // This is required so we can set this as the callback for a timer. | |
444 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO() { | 450 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO() { |
451 UpdatePrefsFromCacheOnIO(base::Closure()); | |
452 } | |
453 | |
454 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO( | |
455 const base::Closure& completion) { | |
445 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 456 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
446 | 457 |
447 base::ListValue* spdy_server_list = new base::ListValue; | 458 base::ListValue* spdy_server_list = new base::ListValue; |
448 http_server_properties_impl_->GetSpdyServerList(spdy_server_list); | 459 http_server_properties_impl_->GetSpdyServerList(spdy_server_list); |
449 | 460 |
450 net::SpdySettingsMap* spdy_settings_map = new net::SpdySettingsMap; | 461 net::SpdySettingsMap* spdy_settings_map = new net::SpdySettingsMap; |
451 *spdy_settings_map = http_server_properties_impl_->spdy_settings_map(); | 462 *spdy_settings_map = http_server_properties_impl_->spdy_settings_map(); |
452 | 463 |
453 net::AlternateProtocolMap* alternate_protocol_map = | 464 net::AlternateProtocolMap* alternate_protocol_map = |
454 new net::AlternateProtocolMap; | 465 new net::AlternateProtocolMap; |
455 *alternate_protocol_map = | 466 *alternate_protocol_map = |
456 http_server_properties_impl_->alternate_protocol_map(); | 467 http_server_properties_impl_->alternate_protocol_map(); |
457 | 468 |
458 net::PipelineCapabilityMap* pipeline_capability_map = | 469 net::PipelineCapabilityMap* pipeline_capability_map = |
459 new net::PipelineCapabilityMap; | 470 new net::PipelineCapabilityMap; |
460 *pipeline_capability_map = | 471 *pipeline_capability_map = |
461 http_server_properties_impl_->GetPipelineCapabilityMap(); | 472 http_server_properties_impl_->GetPipelineCapabilityMap(); |
462 | 473 |
463 // Update the preferences on the UI thread. | 474 // Update the preferences on the UI thread. |
464 BrowserThread::PostTask( | 475 BrowserThread::PostTask( |
465 BrowserThread::UI, | 476 BrowserThread::UI, |
466 FROM_HERE, | 477 FROM_HERE, |
467 base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnUI, | 478 base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnUI, |
468 ui_weak_ptr_, | 479 ui_weak_ptr_, |
469 base::Owned(spdy_server_list), | 480 base::Owned(spdy_server_list), |
470 base::Owned(spdy_settings_map), | 481 base::Owned(spdy_settings_map), |
471 base::Owned(alternate_protocol_map), | 482 base::Owned(alternate_protocol_map), |
472 base::Owned(pipeline_capability_map))); | 483 base::Owned(pipeline_capability_map), |
484 completion)); | |
473 } | 485 } |
474 | 486 |
475 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, | 487 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, |
476 // PortAlternateProtocolPair, and |pipeline_capability| preferences for a | 488 // PortAlternateProtocolPair, and |pipeline_capability| preferences for a |
477 // server. This is used only in UpdatePrefsOnUI. | 489 // server. This is used only in UpdatePrefsOnUI. |
478 struct ServerPref { | 490 struct ServerPref { |
479 ServerPref() | 491 ServerPref() |
480 : supports_spdy(false), | 492 : supports_spdy(false), |
481 settings_map(NULL), | 493 settings_map(NULL), |
482 alternate_protocol(NULL), | 494 alternate_protocol(NULL), |
(...skipping 10 matching lines...) Expand all Loading... | |
493 bool supports_spdy; | 505 bool supports_spdy; |
494 const net::SettingsMap* settings_map; | 506 const net::SettingsMap* settings_map; |
495 const net::PortAlternateProtocolPair* alternate_protocol; | 507 const net::PortAlternateProtocolPair* alternate_protocol; |
496 net::HttpPipelinedHostCapability pipeline_capability; | 508 net::HttpPipelinedHostCapability pipeline_capability; |
497 }; | 509 }; |
498 | 510 |
499 void HttpServerPropertiesManager::UpdatePrefsOnUI( | 511 void HttpServerPropertiesManager::UpdatePrefsOnUI( |
500 base::ListValue* spdy_server_list, | 512 base::ListValue* spdy_server_list, |
501 net::SpdySettingsMap* spdy_settings_map, | 513 net::SpdySettingsMap* spdy_settings_map, |
502 net::AlternateProtocolMap* alternate_protocol_map, | 514 net::AlternateProtocolMap* alternate_protocol_map, |
503 net::PipelineCapabilityMap* pipeline_capability_map) { | 515 net::PipelineCapabilityMap* pipeline_capability_map, |
516 const base::Closure& completion) { | |
504 | 517 |
505 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; | 518 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; |
506 ServerPrefMap server_pref_map; | 519 ServerPrefMap server_pref_map; |
507 | 520 |
508 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 521 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
509 | 522 |
510 // Add servers that support spdy to server_pref_map. | 523 // Add servers that support spdy to server_pref_map. |
511 std::string s; | 524 std::string s; |
512 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); | 525 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); |
513 list_it != spdy_server_list->end(); ++list_it) { | 526 list_it != spdy_server_list->end(); ++list_it) { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
627 | 640 |
628 servers_dict->SetWithoutPathExpansion(server.ToString(), server_pref_dict); | 641 servers_dict->SetWithoutPathExpansion(server.ToString(), server_pref_dict); |
629 } | 642 } |
630 | 643 |
631 http_server_properties_dict.SetWithoutPathExpansion("servers", servers_dict); | 644 http_server_properties_dict.SetWithoutPathExpansion("servers", servers_dict); |
632 http_server_properties_dict.SetInteger("version", kVersionNumber); | 645 http_server_properties_dict.SetInteger("version", kVersionNumber); |
633 setting_prefs_ = true; | 646 setting_prefs_ = true; |
634 pref_service_->Set(prefs::kHttpServerProperties, | 647 pref_service_->Set(prefs::kHttpServerProperties, |
635 http_server_properties_dict); | 648 http_server_properties_dict); |
636 setting_prefs_ = false; | 649 setting_prefs_ = false; |
650 | |
651 // Note that |completion| will be fired after we have written everything to | |
652 // the Preferences, but likely before these changes are serialized to disk. | |
653 // This is not a problem though, as JSONPrefStore guarantees that this will | |
654 // happen soon, even if we shut down immediately. | |
655 if (!completion.is_null()) { | |
656 completion.Run(); | |
657 } | |
battre
2012/10/23 08:38:33
nit: no {}
engedy
2012/10/23 10:40:09
Done.
| |
637 } | 658 } |
638 | 659 |
639 void HttpServerPropertiesManager::Observe( | 660 void HttpServerPropertiesManager::Observe( |
640 int type, | 661 int type, |
641 const content::NotificationSource& source, | 662 const content::NotificationSource& source, |
642 const content::NotificationDetails& details) { | 663 const content::NotificationDetails& details) { |
643 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 664 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
644 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED); | 665 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED); |
645 PrefService* prefs = content::Source<PrefService>(source).ptr(); | 666 PrefService* prefs = content::Source<PrefService>(source).ptr(); |
646 DCHECK(prefs == pref_service_); | 667 DCHECK(prefs == pref_service_); |
647 std::string* pref_name = content::Details<std::string>(details).ptr(); | 668 std::string* pref_name = content::Details<std::string>(details).ptr(); |
648 if (*pref_name == prefs::kHttpServerProperties) { | 669 if (*pref_name == prefs::kHttpServerProperties) { |
649 if (!setting_prefs_) | 670 if (!setting_prefs_) |
650 ScheduleUpdateCacheOnUI(); | 671 ScheduleUpdateCacheOnUI(); |
651 } else { | 672 } else { |
652 NOTREACHED(); | 673 NOTREACHED(); |
653 } | 674 } |
654 } | 675 } |
655 | 676 |
656 } // namespace chrome_browser_net | 677 } // namespace chrome_browser_net |
OLD | NEW |