OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 #include "chrome/browser/net/http_server_properties_manager.h" |
| 5 |
| 6 #include "base/bind.h" |
| 7 #include "base/stl_util.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/prefs/pref_service.h" |
| 10 #include "chrome/common/chrome_notification_types.h" |
| 11 #include "chrome/common/pref_names.h" |
| 12 #include "content/browser/browser_thread.h" |
| 13 #include "content/common/notification_details.h" |
| 14 #include "content/common/notification_source.h" |
| 15 |
| 16 namespace chrome_browser_net { |
| 17 |
| 18 namespace { |
| 19 |
| 20 // Time to wait before starting an update the spdy_servers_table_ cache from |
| 21 // preferences. Scheduling another update during this period will reset the |
| 22 // timer. |
| 23 const int64 kUpdateCacheDelayMs = 1000; |
| 24 |
| 25 // Time to wait before starting an update the preferences from the |
| 26 // spdy_servers cache. Scheduling another update during this period will |
| 27 // reset the timer. |
| 28 const int64 kUpdatePrefsDelayMs = 5000; |
| 29 |
| 30 typedef std::vector<std::string> StringVector; |
| 31 |
| 32 // String is host/port pair of spdy server. |
| 33 StringVector* ListValueToStringVector(const base::ListValue* list) { |
| 34 StringVector* vector = new StringVector; |
| 35 |
| 36 if (!list) |
| 37 return vector; |
| 38 |
| 39 vector->reserve(list->GetSize()); |
| 40 std::string s; |
| 41 for (base::ListValue::const_iterator it = list->begin(); |
| 42 it != list->end(); ++it) { |
| 43 if ((*it)->GetAsString(&s)) |
| 44 vector->push_back(s); |
| 45 } |
| 46 |
| 47 return vector; |
| 48 } |
| 49 |
| 50 } // namespace |
| 51 |
| 52 //////////////////////////////////////////////////////////////////////////////// |
| 53 // HttpServerPropertiesManager |
| 54 |
| 55 HttpServerPropertiesManager::HttpServerPropertiesManager( |
| 56 PrefService* pref_service) |
| 57 : ALLOW_THIS_IN_INITIALIZER_LIST(ui_method_factory_(this)), |
| 58 pref_service_(pref_service) { |
| 59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 60 DCHECK(pref_service); |
| 61 ui_weak_ptr_factory_.reset( |
| 62 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); |
| 63 ui_weak_ptr_ = ui_weak_ptr_factory_->GetWeakPtr(); |
| 64 pref_change_registrar_.Init(pref_service_); |
| 65 pref_change_registrar_.Add(prefs::kSpdyServers, this); |
| 66 } |
| 67 |
| 68 HttpServerPropertiesManager::~HttpServerPropertiesManager() { |
| 69 } |
| 70 |
| 71 void HttpServerPropertiesManager::InitializeOnIOThread() { |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 73 http_server_properties_impl_.reset(new net::HttpServerPropertiesImpl()); |
| 74 |
| 75 io_method_factory_.reset( |
| 76 new ScopedRunnableMethodFactory<HttpServerPropertiesManager>(this)); |
| 77 |
| 78 BrowserThread::PostTask( |
| 79 BrowserThread::UI, |
| 80 FROM_HERE, |
| 81 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefs, |
| 82 ui_weak_ptr_)); |
| 83 } |
| 84 |
| 85 void HttpServerPropertiesManager::ShutdownOnUIThread() { |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 87 // Cancel any pending updates, and stop listening for pref change updates. |
| 88 ui_method_factory_.RevokeAll(); |
| 89 ui_weak_ptr_factory_.reset(); |
| 90 pref_change_registrar_.RemoveAll(); |
| 91 } |
| 92 |
| 93 bool HttpServerPropertiesManager::SupportsSpdy( |
| 94 const net::HostPortPair& server) const { |
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 96 return http_server_properties_impl_->SupportsSpdy(server); |
| 97 } |
| 98 |
| 99 void HttpServerPropertiesManager::SetSupportsSpdy( |
| 100 const net::HostPortPair& server, |
| 101 bool support_spdy) { |
| 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 103 |
| 104 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy); |
| 105 ScheduleUpdatePrefsOnIO(); |
| 106 } |
| 107 |
| 108 void HttpServerPropertiesManager::DeleteAll() { |
| 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 110 |
| 111 http_server_properties_impl_->DeleteAll(); |
| 112 ScheduleUpdatePrefsOnIO(); |
| 113 } |
| 114 |
| 115 // static |
| 116 void HttpServerPropertiesManager::RegisterPrefs(PrefService* prefs) { |
| 117 prefs->RegisterListPref(prefs::kSpdyServers, PrefService::UNSYNCABLE_PREF); |
| 118 } |
| 119 |
| 120 // |
| 121 // Update spdy_servers (the cached data) with data from preferences. |
| 122 // |
| 123 void HttpServerPropertiesManager::ScheduleUpdateCacheOnUI() { |
| 124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 125 // Cancel pending updates, if any. |
| 126 ui_method_factory_.RevokeAll(); |
| 127 PostUpdateTaskOnUI( |
| 128 ui_method_factory_.NewRunnableMethod( |
| 129 &HttpServerPropertiesManager::UpdateCacheFromPrefs)); |
| 130 } |
| 131 |
| 132 void HttpServerPropertiesManager::PostUpdateTaskOnUI(Task* task) { |
| 133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 134 // This is overridden in tests to post the task without the delay. |
| 135 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, kUpdateCacheDelayMs); |
| 136 } |
| 137 |
| 138 void HttpServerPropertiesManager::UpdateCacheFromPrefs() { |
| 139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 140 |
| 141 if (!pref_service_->HasPrefPath(prefs::kSpdyServers)) |
| 142 return; |
| 143 |
| 144 // The preferences can only be read on the UI thread. |
| 145 StringVector* spdy_servers = |
| 146 ListValueToStringVector(pref_service_->GetList(prefs::kSpdyServers)); |
| 147 |
| 148 // Go through the IO thread to grab a WeakPtr to |this|. This is safe from |
| 149 // here, since this task will always execute before a potential deletion of |
| 150 // ProfileIOData on IO. |
| 151 BrowserThread::PostTask( |
| 152 BrowserThread::IO, |
| 153 FROM_HERE, |
| 154 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO, |
| 155 base::Unretained(this), spdy_servers, true)); |
| 156 } |
| 157 |
| 158 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO( |
| 159 StringVector* spdy_servers, |
| 160 bool support_spdy) { |
| 161 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 162 // Preferences have the master data because admins might have pushed new |
| 163 // preferences. Clear the cached data and use the new spdy server list from |
| 164 // preferences. |
| 165 scoped_ptr<StringVector> scoped_spdy_servers(spdy_servers); |
| 166 http_server_properties_impl_->Initialize(spdy_servers, support_spdy); |
| 167 } |
| 168 |
| 169 // |
| 170 // Update Preferences with data from spdy_servers (the cached data). |
| 171 // |
| 172 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO() { |
| 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 174 // Cancel pending updates, if any. |
| 175 io_method_factory_->RevokeAll(); |
| 176 PostUpdateTaskOnIO( |
| 177 io_method_factory_->NewRunnableMethod( |
| 178 &HttpServerPropertiesManager::UpdatePrefsFromCache)); |
| 179 } |
| 180 |
| 181 void HttpServerPropertiesManager::PostUpdateTaskOnIO(Task* task) { |
| 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 183 // This is overridden in tests to post the task without the delay. |
| 184 MessageLoop::current()->PostDelayedTask(FROM_HERE, task, kUpdatePrefsDelayMs); |
| 185 } |
| 186 |
| 187 void HttpServerPropertiesManager::UpdatePrefsFromCache() { |
| 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 189 |
| 190 scoped_refptr<RefCountedListValue> spdy_server_list = |
| 191 new RefCountedListValue(); |
| 192 http_server_properties_impl_->GetSpdyServerList(&spdy_server_list->data); |
| 193 |
| 194 // Update the preferences on the UI thread. |
| 195 BrowserThread::PostTask( |
| 196 BrowserThread::UI, |
| 197 FROM_HERE, |
| 198 base::Bind(&HttpServerPropertiesManager::SetSpdyServersInPrefsOnUI, |
| 199 ui_weak_ptr_, spdy_server_list)); |
| 200 } |
| 201 |
| 202 void HttpServerPropertiesManager::SetSpdyServersInPrefsOnUI( |
| 203 scoped_refptr<RefCountedListValue> spdy_server_list) { |
| 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 205 pref_service_->Set(prefs::kSpdyServers, spdy_server_list->data); |
| 206 } |
| 207 |
| 208 void HttpServerPropertiesManager::Observe(int type, |
| 209 const NotificationSource& source, |
| 210 const NotificationDetails& details) { |
| 211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 212 DCHECK(type == chrome::NOTIFICATION_PREF_CHANGED); |
| 213 PrefService* prefs = Source<PrefService>(source).ptr(); |
| 214 DCHECK(prefs == pref_service_); |
| 215 std::string* pref_name = Details<std::string>(details).ptr(); |
| 216 DCHECK(*pref_name == prefs::kSpdyServers); |
| 217 ScheduleUpdateCacheOnUI(); |
| 218 } |
| 219 |
| 220 } // namespace chrome_browser_net |
OLD | NEW |