Index: net/http/http_server_properties_manager.cc |
diff --git a/chrome/browser/net/http_server_properties_manager.cc b/net/http/http_server_properties_manager.cc |
similarity index 88% |
rename from chrome/browser/net/http_server_properties_manager.cc |
rename to net/http/http_server_properties_manager.cc |
index d99af2c82767c90d19089934cc737c739a68c1fc..8e310deaf7014bd792ab43ac3c708f417a0e4eb1 100644 |
--- a/chrome/browser/net/http_server_properties_manager.cc |
+++ b/net/http/http_server_properties_manager.cc |
@@ -2,26 +2,19 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "chrome/browser/net/http_server_properties_manager.h" |
+#include "net/http/http_server_properties_manager.h" |
#include "base/bind.h" |
#include "base/metrics/histogram.h" |
#include "base/prefs/pref_service.h" |
#include "base/rand_util.h" |
+#include "base/single_thread_task_runner.h" |
#include "base/stl_util.h" |
#include "base/strings/string_number_conversions.h" |
#include "base/strings/stringprintf.h" |
#include "base/values.h" |
-#include "chrome/browser/chrome_notification_types.h" |
-#include "chrome/common/pref_names.h" |
-#include "components/pref_registry/pref_registry_syncable.h" |
-#include "content/public/browser/browser_thread.h" |
-#include "content/public/browser/notification_details.h" |
-#include "content/public/browser/notification_source.h" |
-using content::BrowserThread; |
- |
-namespace chrome_browser_net { |
+namespace net { |
namespace { |
@@ -62,10 +55,16 @@ const int kMaxSupportsSpdyServerHostsToPersist = 300; |
// HttpServerPropertiesManager |
HttpServerPropertiesManager::HttpServerPropertiesManager( |
- PrefService* pref_service) |
- : pref_service_(pref_service), |
- setting_prefs_(false) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ PrefService* pref_service, |
+ const char* path, |
+ scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner, |
+ scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) |
+ : ui_task_runner_(ui_task_runner), |
+ pref_service_(pref_service), |
+ setting_prefs_(false), |
+ path_(path), |
+ io_task_runner_(io_task_runner) { |
+ DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
DCHECK(pref_service); |
ui_weak_ptr_factory_.reset( |
new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); |
@@ -74,18 +73,18 @@ HttpServerPropertiesManager::HttpServerPropertiesManager( |
new base::OneShotTimer<HttpServerPropertiesManager>); |
pref_change_registrar_.Init(pref_service_); |
pref_change_registrar_.Add( |
- prefs::kHttpServerProperties, |
+ path_, |
base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, |
base::Unretained(this))); |
} |
HttpServerPropertiesManager::~HttpServerPropertiesManager() { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
io_weak_ptr_factory_.reset(); |
} |
void HttpServerPropertiesManager::InitializeOnIOThread() { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
io_weak_ptr_factory_.reset( |
new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); |
http_server_properties_impl_.reset(new net::HttpServerPropertiesImpl()); |
@@ -93,15 +92,14 @@ void HttpServerPropertiesManager::InitializeOnIOThread() { |
io_prefs_update_timer_.reset( |
new base::OneShotTimer<HttpServerPropertiesManager>); |
- BrowserThread::PostTask( |
- BrowserThread::UI, |
+ ui_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI, |
ui_weak_ptr_)); |
} |
void HttpServerPropertiesManager::ShutdownOnUIThread() { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
// Cancel any pending updates, and stop listening for pref change updates. |
ui_cache_update_timer_->Stop(); |
ui_weak_ptr_factory_.reset(); |
@@ -109,14 +107,6 @@ void HttpServerPropertiesManager::ShutdownOnUIThread() { |
} |
// static |
-void HttpServerPropertiesManager::RegisterProfilePrefs( |
- user_prefs::PrefRegistrySyncable* prefs) { |
- prefs->RegisterDictionaryPref( |
- prefs::kHttpServerProperties, |
- user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
-} |
- |
-// static |
void HttpServerPropertiesManager::SetVersion( |
base::DictionaryValue* http_server_properties_dict, |
int version_number) { |
@@ -130,7 +120,7 @@ void HttpServerPropertiesManager::SetVersion( |
// This is required for conformance with the HttpServerProperties interface. |
base::WeakPtr<net::HttpServerProperties> |
HttpServerPropertiesManager::GetWeakPtr() { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
return io_weak_ptr_factory_->GetWeakPtr(); |
} |
@@ -139,7 +129,7 @@ void HttpServerPropertiesManager::Clear() { |
} |
void HttpServerPropertiesManager::Clear(const base::Closure& completion) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
http_server_properties_impl_->Clear(); |
UpdatePrefsFromCacheOnIO(completion); |
@@ -147,14 +137,14 @@ void HttpServerPropertiesManager::Clear(const base::Closure& completion) { |
bool HttpServerPropertiesManager::SupportsSpdy( |
const net::HostPortPair& server) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
return http_server_properties_impl_->SupportsSpdy(server); |
} |
void HttpServerPropertiesManager::SetSupportsSpdy( |
const net::HostPortPair& server, |
bool support_spdy) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
http_server_properties_impl_->SetSupportsSpdy(server, support_spdy); |
ScheduleUpdatePrefsOnIO(); |
@@ -162,14 +152,14 @@ void HttpServerPropertiesManager::SetSupportsSpdy( |
bool HttpServerPropertiesManager::HasAlternateProtocol( |
const net::HostPortPair& server) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
return http_server_properties_impl_->HasAlternateProtocol(server); |
} |
net::PortAlternateProtocolPair |
HttpServerPropertiesManager::GetAlternateProtocol( |
const net::HostPortPair& server) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
return http_server_properties_impl_->GetAlternateProtocol(server); |
} |
@@ -177,7 +167,7 @@ void HttpServerPropertiesManager::SetAlternateProtocol( |
const net::HostPortPair& server, |
uint16 alternate_port, |
net::AlternateProtocol alternate_protocol) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
http_server_properties_impl_->SetAlternateProtocol( |
server, alternate_port, alternate_protocol); |
ScheduleUpdatePrefsOnIO(); |
@@ -185,35 +175,35 @@ void HttpServerPropertiesManager::SetAlternateProtocol( |
void HttpServerPropertiesManager::SetBrokenAlternateProtocol( |
const net::HostPortPair& server) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
http_server_properties_impl_->SetBrokenAlternateProtocol(server); |
ScheduleUpdatePrefsOnIO(); |
} |
bool HttpServerPropertiesManager::WasAlternateProtocolRecentlyBroken( |
const net::HostPortPair& server) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
return http_server_properties_impl_->WasAlternateProtocolRecentlyBroken( |
server); |
} |
void HttpServerPropertiesManager::ConfirmAlternateProtocol( |
const net::HostPortPair& server) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
http_server_properties_impl_->ConfirmAlternateProtocol(server); |
ScheduleUpdatePrefsOnIO(); |
} |
void HttpServerPropertiesManager::ClearAlternateProtocol( |
const net::HostPortPair& server) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
http_server_properties_impl_->ClearAlternateProtocol(server); |
ScheduleUpdatePrefsOnIO(); |
} |
const net::AlternateProtocolMap& |
HttpServerPropertiesManager::alternate_protocol_map() const { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
return http_server_properties_impl_->alternate_protocol_map(); |
} |
@@ -230,7 +220,7 @@ HttpServerPropertiesManager::GetAlternateProtocolExperiment() const { |
const net::SettingsMap& |
HttpServerPropertiesManager::GetSpdySettings( |
const net::HostPortPair& host_port_pair) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
return http_server_properties_impl_->GetSpdySettings(host_port_pair); |
} |
@@ -239,7 +229,7 @@ bool HttpServerPropertiesManager::SetSpdySetting( |
net::SpdySettingsIds id, |
net::SpdySettingsFlags flags, |
uint32 value) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
bool persist = http_server_properties_impl_->SetSpdySetting( |
host_port_pair, id, flags, value); |
if (persist) |
@@ -249,34 +239,34 @@ bool HttpServerPropertiesManager::SetSpdySetting( |
void HttpServerPropertiesManager::ClearSpdySettings( |
const net::HostPortPair& host_port_pair) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
http_server_properties_impl_->ClearSpdySettings(host_port_pair); |
ScheduleUpdatePrefsOnIO(); |
} |
void HttpServerPropertiesManager::ClearAllSpdySettings() { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
http_server_properties_impl_->ClearAllSpdySettings(); |
ScheduleUpdatePrefsOnIO(); |
} |
const net::SpdySettingsMap& |
HttpServerPropertiesManager::spdy_settings_map() const { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
return http_server_properties_impl_->spdy_settings_map(); |
} |
void HttpServerPropertiesManager::SetServerNetworkStats( |
const net::HostPortPair& host_port_pair, |
NetworkStats stats) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
http_server_properties_impl_->SetServerNetworkStats(host_port_pair, stats); |
} |
const HttpServerPropertiesManager::NetworkStats* |
HttpServerPropertiesManager::GetServerNetworkStats( |
const net::HostPortPair& host_port_pair) const { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
return http_server_properties_impl_->GetServerNetworkStats(host_port_pair); |
} |
@@ -284,7 +274,7 @@ HttpServerPropertiesManager::GetServerNetworkStats( |
// Update the HttpServerPropertiesImpl's cache with data from preferences. |
// |
void HttpServerPropertiesManager::ScheduleUpdateCacheOnUI() { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
// Cancel pending updates, if any. |
ui_cache_update_timer_->Stop(); |
StartCacheUpdateTimerOnUI( |
@@ -293,7 +283,7 @@ void HttpServerPropertiesManager::ScheduleUpdateCacheOnUI() { |
void HttpServerPropertiesManager::StartCacheUpdateTimerOnUI( |
base::TimeDelta delay) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
ui_cache_update_timer_->Start( |
FROM_HERE, delay, this, |
&HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI); |
@@ -301,14 +291,14 @@ void HttpServerPropertiesManager::StartCacheUpdateTimerOnUI( |
void HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI() { |
// The preferences can only be read on the UI thread. |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
- if (!pref_service_->HasPrefPath(prefs::kHttpServerProperties)) |
+ if (!pref_service_->HasPrefPath(path_)) |
return; |
bool detected_corrupted_prefs = false; |
const base::DictionaryValue& http_server_properties_dict = |
- *pref_service_->GetDictionary(prefs::kHttpServerProperties); |
+ *pref_service_->GetDictionary(path_); |
int version = kMissingVersion; |
if (!http_server_properties_dict.GetIntegerWithoutPathExpansion( |
@@ -448,8 +438,7 @@ void HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI() { |
} while (false); |
} |
- BrowserThread::PostTask( |
- BrowserThread::IO, |
+ io_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&HttpServerPropertiesManager:: |
UpdateCacheFromPrefsOnIO, |
@@ -469,7 +458,7 @@ void HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO( |
bool detected_corrupted_prefs) { |
// Preferences have the master data because admins might have pushed new |
// preferences. Update the cached data with new data from preferences. |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size()); |
http_server_properties_impl_->InitializeSpdyServers(spdy_servers, true); |
@@ -498,7 +487,7 @@ void HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO( |
// Update Preferences with data from the cached data. |
// |
void HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO() { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
// Cancel pending updates, if any. |
io_prefs_update_timer_->Stop(); |
StartPrefsUpdateTimerOnIO( |
@@ -507,7 +496,7 @@ void HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO() { |
void HttpServerPropertiesManager::StartPrefsUpdateTimerOnIO( |
base::TimeDelta delay) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
// This is overridden in tests to post the task without the delay. |
io_prefs_update_timer_->Start( |
FROM_HERE, delay, this, |
@@ -521,7 +510,7 @@ void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO() { |
void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO( |
const base::Closure& completion) { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ DCHECK(io_task_runner_->BelongsToCurrentThread()); |
base::ListValue* spdy_server_list = new base::ListValue; |
http_server_properties_impl_->GetSpdyServerList( |
@@ -561,8 +550,7 @@ void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO( |
} |
// Update the preferences on the UI thread. |
- BrowserThread::PostTask( |
- BrowserThread::UI, |
+ ui_task_runner_->PostTask( |
FROM_HERE, |
base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnUI, |
ui_weak_ptr_, |
@@ -602,7 +590,7 @@ void HttpServerPropertiesManager::UpdatePrefsOnUI( |
typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; |
ServerPrefMap server_pref_map; |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
// Add servers that support spdy to server_pref_map. |
std::string s; |
@@ -655,7 +643,7 @@ void HttpServerPropertiesManager::UpdatePrefsOnUI( |
} |
} |
- // Persist the prefs::kHttpServerProperties. |
+ // Persist properties to the |path_|. |
base::DictionaryValue http_server_properties_dict; |
base::DictionaryValue* servers_dict = new base::DictionaryValue; |
for (ServerPrefMap::const_iterator map_it = |
@@ -705,8 +693,7 @@ void HttpServerPropertiesManager::UpdatePrefsOnUI( |
http_server_properties_dict.SetWithoutPathExpansion("servers", servers_dict); |
SetVersion(&http_server_properties_dict, kVersionNumber); |
setting_prefs_ = true; |
- pref_service_->Set(prefs::kHttpServerProperties, |
- http_server_properties_dict); |
+ pref_service_->Set(path_, http_server_properties_dict); |
setting_prefs_ = false; |
// Note that |completion| will be fired after we have written everything to |
@@ -718,7 +705,7 @@ void HttpServerPropertiesManager::UpdatePrefsOnUI( |
} |
void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { |
- DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
+ DCHECK(ui_task_runner_->BelongsToCurrentThread()); |
if (!setting_prefs_) |
ScheduleUpdateCacheOnUI(); |
} |