Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(82)

Side by Side Diff: net/http/http_server_properties_manager.cc

Issue 378823002: Move http_server_properties_manager from chrome/browser/net to net/http. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/message_loop/message_loop.h"
8 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
10 #include "base/rand_util.h" 11 #include "base/rand_util.h"
12 #include "base/single_thread_task_runner.h"
11 #include "base/stl_util.h" 13 #include "base/stl_util.h"
12 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
14 #include "base/values.h" 16 #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 17
22 using content::BrowserThread; 18 namespace net {
23
24 namespace chrome_browser_net {
25 19
26 namespace { 20 namespace {
27 21
28 // Time to wait before starting an update the http_server_properties_impl_ cache 22 // 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 23 // from preferences. Scheduling another update during this period will reset the
30 // timer. 24 // timer.
31 const int64 kUpdateCacheDelayMs = 1000; 25 const int64 kUpdateCacheDelayMs = 1000;
32 26
33 // Time to wait before starting an update the preferences from the 27 // Time to wait before starting an update the preferences from the
34 // http_server_properties_impl_ cache. Scheduling another update during this 28 // http_server_properties_impl_ cache. Scheduling another update during this
(...skipping 20 matching lines...) Expand all
55 49
56 // Persist 300 MRU SupportsSpdyServerHostPortPairs. 50 // Persist 300 MRU SupportsSpdyServerHostPortPairs.
57 const int kMaxSupportsSpdyServerHostsToPersist = 300; 51 const int kMaxSupportsSpdyServerHostsToPersist = 300;
58 52
59 } // namespace 53 } // namespace
60 54
61 //////////////////////////////////////////////////////////////////////////////// 55 ////////////////////////////////////////////////////////////////////////////////
62 // HttpServerPropertiesManager 56 // HttpServerPropertiesManager
63 57
64 HttpServerPropertiesManager::HttpServerPropertiesManager( 58 HttpServerPropertiesManager::HttpServerPropertiesManager(
65 PrefService* pref_service) 59 PrefService* pref_service,
66 : pref_service_(pref_service), 60 const char* pref_path,
67 setting_prefs_(false) { 61 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
68 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 62 : pref_task_runner_(base::MessageLoop::current()->message_loop_proxy()),
Ryan Sleevi 2014/07/08 18:19:26 use ThreadTaskRunnerHandle::Get() (see Chromium-de
mef 2014/07/09 12:12:04 Done.
63 pref_service_(pref_service),
64 setting_prefs_(false),
65 path_(pref_path),
66 io_task_runner_(io_task_runner) {
69 DCHECK(pref_service); 67 DCHECK(pref_service);
70 ui_weak_ptr_factory_.reset( 68 pref_weak_ptr_factory_.reset(
71 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); 69 new base::WeakPtrFactory<HttpServerPropertiesManager>(this));
72 ui_weak_ptr_ = ui_weak_ptr_factory_->GetWeakPtr(); 70 pref_weak_ptr_ = pref_weak_ptr_factory_->GetWeakPtr();
73 ui_cache_update_timer_.reset( 71 pref_cache_update_timer_.reset(
74 new base::OneShotTimer<HttpServerPropertiesManager>); 72 new base::OneShotTimer<HttpServerPropertiesManager>);
75 pref_change_registrar_.Init(pref_service_); 73 pref_change_registrar_.Init(pref_service_);
76 pref_change_registrar_.Add( 74 pref_change_registrar_.Add(
77 prefs::kHttpServerProperties, 75 path_,
78 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged, 76 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged,
79 base::Unretained(this))); 77 base::Unretained(this)));
80 } 78 }
81 79
82 HttpServerPropertiesManager::~HttpServerPropertiesManager() { 80 HttpServerPropertiesManager::~HttpServerPropertiesManager() {
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 81 DCHECK(io_task_runner_->BelongsToCurrentThread());
84 io_weak_ptr_factory_.reset(); 82 io_weak_ptr_factory_.reset();
85 } 83 }
86 84
87 void HttpServerPropertiesManager::InitializeOnIOThread() { 85 void HttpServerPropertiesManager::InitializeOnIOThread() {
88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 86 DCHECK(io_task_runner_->BelongsToCurrentThread());
89 io_weak_ptr_factory_.reset( 87 io_weak_ptr_factory_.reset(
90 new base::WeakPtrFactory<HttpServerPropertiesManager>(this)); 88 new base::WeakPtrFactory<HttpServerPropertiesManager>(this));
91 http_server_properties_impl_.reset(new net::HttpServerPropertiesImpl()); 89 http_server_properties_impl_.reset(new net::HttpServerPropertiesImpl());
92 90
93 io_prefs_update_timer_.reset( 91 io_prefs_update_timer_.reset(
94 new base::OneShotTimer<HttpServerPropertiesManager>); 92 new base::OneShotTimer<HttpServerPropertiesManager>);
95 93
96 BrowserThread::PostTask( 94 pref_task_runner_->PostTask(
97 BrowserThread::UI,
98 FROM_HERE, 95 FROM_HERE,
99 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI, 96 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnPref,
100 ui_weak_ptr_)); 97 pref_weak_ptr_));
101 } 98 }
102 99
103 void HttpServerPropertiesManager::ShutdownOnUIThread() { 100 void HttpServerPropertiesManager::ShutdownOnPrefThread() {
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 101 DCHECK(pref_task_runner_->BelongsToCurrentThread());
105 // Cancel any pending updates, and stop listening for pref change updates. 102 // Cancel any pending updates, and stop listening for pref change updates.
106 ui_cache_update_timer_->Stop(); 103 pref_cache_update_timer_->Stop();
107 ui_weak_ptr_factory_.reset(); 104 pref_weak_ptr_factory_.reset();
108 pref_change_registrar_.RemoveAll(); 105 pref_change_registrar_.RemoveAll();
109 } 106 }
110 107
111 // static 108 // 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( 109 void HttpServerPropertiesManager::SetVersion(
121 base::DictionaryValue* http_server_properties_dict, 110 base::DictionaryValue* http_server_properties_dict,
122 int version_number) { 111 int version_number) {
123 if (version_number < 0) 112 if (version_number < 0)
124 version_number = kVersionNumber; 113 version_number = kVersionNumber;
125 DCHECK_LE(version_number, kVersionNumber); 114 DCHECK_LE(version_number, kVersionNumber);
126 if (version_number <= kVersionNumber) 115 if (version_number <= kVersionNumber)
127 http_server_properties_dict->SetInteger("version", version_number); 116 http_server_properties_dict->SetInteger("version", version_number);
128 } 117 }
129 118
130 // This is required for conformance with the HttpServerProperties interface. 119 // This is required for conformance with the HttpServerProperties interface.
131 base::WeakPtr<net::HttpServerProperties> 120 base::WeakPtr<net::HttpServerProperties>
132 HttpServerPropertiesManager::GetWeakPtr() { 121 HttpServerPropertiesManager::GetWeakPtr() {
133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 122 DCHECK(io_task_runner_->BelongsToCurrentThread());
134 return io_weak_ptr_factory_->GetWeakPtr(); 123 return io_weak_ptr_factory_->GetWeakPtr();
135 } 124 }
136 125
137 void HttpServerPropertiesManager::Clear() { 126 void HttpServerPropertiesManager::Clear() {
138 Clear(base::Closure()); 127 Clear(base::Closure());
139 } 128 }
140 129
141 void HttpServerPropertiesManager::Clear(const base::Closure& completion) { 130 void HttpServerPropertiesManager::Clear(const base::Closure& completion) {
142 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 131 DCHECK(io_task_runner_->BelongsToCurrentThread());
143 132
144 http_server_properties_impl_->Clear(); 133 http_server_properties_impl_->Clear();
145 UpdatePrefsFromCacheOnIO(completion); 134 UpdatePrefsFromCacheOnIO(completion);
146 } 135 }
147 136
148 bool HttpServerPropertiesManager::SupportsSpdy( 137 bool HttpServerPropertiesManager::SupportsSpdy(
149 const net::HostPortPair& server) { 138 const net::HostPortPair& server) {
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 139 DCHECK(io_task_runner_->BelongsToCurrentThread());
151 return http_server_properties_impl_->SupportsSpdy(server); 140 return http_server_properties_impl_->SupportsSpdy(server);
152 } 141 }
153 142
154 void HttpServerPropertiesManager::SetSupportsSpdy( 143 void HttpServerPropertiesManager::SetSupportsSpdy(
155 const net::HostPortPair& server, 144 const net::HostPortPair& server,
156 bool support_spdy) { 145 bool support_spdy) {
157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 146 DCHECK(io_task_runner_->BelongsToCurrentThread());
158 147
159 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy); 148 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy);
160 ScheduleUpdatePrefsOnIO(); 149 ScheduleUpdatePrefsOnIO();
161 } 150 }
162 151
163 bool HttpServerPropertiesManager::HasAlternateProtocol( 152 bool HttpServerPropertiesManager::HasAlternateProtocol(
164 const net::HostPortPair& server) { 153 const net::HostPortPair& server) {
165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 154 DCHECK(io_task_runner_->BelongsToCurrentThread());
166 return http_server_properties_impl_->HasAlternateProtocol(server); 155 return http_server_properties_impl_->HasAlternateProtocol(server);
167 } 156 }
168 157
169 net::PortAlternateProtocolPair 158 net::PortAlternateProtocolPair
170 HttpServerPropertiesManager::GetAlternateProtocol( 159 HttpServerPropertiesManager::GetAlternateProtocol(
171 const net::HostPortPair& server) { 160 const net::HostPortPair& server) {
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 161 DCHECK(io_task_runner_->BelongsToCurrentThread());
Ryan Sleevi 2014/07/08 18:19:26 All of the BelongsTo will be replaced with "RunsTa
mef 2014/07/09 12:12:04 Done.
173 return http_server_properties_impl_->GetAlternateProtocol(server); 162 return http_server_properties_impl_->GetAlternateProtocol(server);
174 } 163 }
175 164
176 void HttpServerPropertiesManager::SetAlternateProtocol( 165 void HttpServerPropertiesManager::SetAlternateProtocol(
177 const net::HostPortPair& server, 166 const net::HostPortPair& server,
178 uint16 alternate_port, 167 uint16 alternate_port,
179 net::AlternateProtocol alternate_protocol) { 168 net::AlternateProtocol alternate_protocol) {
180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 169 DCHECK(io_task_runner_->BelongsToCurrentThread());
181 http_server_properties_impl_->SetAlternateProtocol( 170 http_server_properties_impl_->SetAlternateProtocol(
182 server, alternate_port, alternate_protocol); 171 server, alternate_port, alternate_protocol);
183 ScheduleUpdatePrefsOnIO(); 172 ScheduleUpdatePrefsOnIO();
184 } 173 }
185 174
186 void HttpServerPropertiesManager::SetBrokenAlternateProtocol( 175 void HttpServerPropertiesManager::SetBrokenAlternateProtocol(
187 const net::HostPortPair& server) { 176 const net::HostPortPair& server) {
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 177 DCHECK(io_task_runner_->BelongsToCurrentThread());
189 http_server_properties_impl_->SetBrokenAlternateProtocol(server); 178 http_server_properties_impl_->SetBrokenAlternateProtocol(server);
190 ScheduleUpdatePrefsOnIO(); 179 ScheduleUpdatePrefsOnIO();
191 } 180 }
192 181
193 bool HttpServerPropertiesManager::WasAlternateProtocolRecentlyBroken( 182 bool HttpServerPropertiesManager::WasAlternateProtocolRecentlyBroken(
194 const net::HostPortPair& server) { 183 const net::HostPortPair& server) {
195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 184 DCHECK(io_task_runner_->BelongsToCurrentThread());
196 return http_server_properties_impl_->WasAlternateProtocolRecentlyBroken( 185 return http_server_properties_impl_->WasAlternateProtocolRecentlyBroken(
197 server); 186 server);
198 } 187 }
199 188
200 void HttpServerPropertiesManager::ConfirmAlternateProtocol( 189 void HttpServerPropertiesManager::ConfirmAlternateProtocol(
201 const net::HostPortPair& server) { 190 const net::HostPortPair& server) {
202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 191 DCHECK(io_task_runner_->BelongsToCurrentThread());
203 http_server_properties_impl_->ConfirmAlternateProtocol(server); 192 http_server_properties_impl_->ConfirmAlternateProtocol(server);
204 ScheduleUpdatePrefsOnIO(); 193 ScheduleUpdatePrefsOnIO();
205 } 194 }
206 195
207 void HttpServerPropertiesManager::ClearAlternateProtocol( 196 void HttpServerPropertiesManager::ClearAlternateProtocol(
208 const net::HostPortPair& server) { 197 const net::HostPortPair& server) {
209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 198 DCHECK(io_task_runner_->BelongsToCurrentThread());
210 http_server_properties_impl_->ClearAlternateProtocol(server); 199 http_server_properties_impl_->ClearAlternateProtocol(server);
211 ScheduleUpdatePrefsOnIO(); 200 ScheduleUpdatePrefsOnIO();
212 } 201 }
213 202
214 const net::AlternateProtocolMap& 203 const net::AlternateProtocolMap&
215 HttpServerPropertiesManager::alternate_protocol_map() const { 204 HttpServerPropertiesManager::alternate_protocol_map() const {
216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 205 DCHECK(io_task_runner_->BelongsToCurrentThread());
217 return http_server_properties_impl_->alternate_protocol_map(); 206 return http_server_properties_impl_->alternate_protocol_map();
218 } 207 }
219 208
220 void HttpServerPropertiesManager::SetAlternateProtocolExperiment( 209 void HttpServerPropertiesManager::SetAlternateProtocolExperiment(
221 net::AlternateProtocolExperiment experiment) { 210 net::AlternateProtocolExperiment experiment) {
222 http_server_properties_impl_->SetAlternateProtocolExperiment(experiment); 211 http_server_properties_impl_->SetAlternateProtocolExperiment(experiment);
223 } 212 }
224 213
225 net::AlternateProtocolExperiment 214 net::AlternateProtocolExperiment
226 HttpServerPropertiesManager::GetAlternateProtocolExperiment() const { 215 HttpServerPropertiesManager::GetAlternateProtocolExperiment() const {
227 return http_server_properties_impl_->GetAlternateProtocolExperiment(); 216 return http_server_properties_impl_->GetAlternateProtocolExperiment();
228 } 217 }
229 218
230 const net::SettingsMap& 219 const net::SettingsMap& HttpServerPropertiesManager::GetSpdySettings(
231 HttpServerPropertiesManager::GetSpdySettings(
232 const net::HostPortPair& host_port_pair) { 220 const net::HostPortPair& host_port_pair) {
233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 221 DCHECK(io_task_runner_->BelongsToCurrentThread());
234 return http_server_properties_impl_->GetSpdySettings(host_port_pair); 222 return http_server_properties_impl_->GetSpdySettings(host_port_pair);
235 } 223 }
236 224
237 bool HttpServerPropertiesManager::SetSpdySetting( 225 bool HttpServerPropertiesManager::SetSpdySetting(
238 const net::HostPortPair& host_port_pair, 226 const net::HostPortPair& host_port_pair,
239 net::SpdySettingsIds id, 227 net::SpdySettingsIds id,
240 net::SpdySettingsFlags flags, 228 net::SpdySettingsFlags flags,
241 uint32 value) { 229 uint32 value) {
242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 230 DCHECK(io_task_runner_->BelongsToCurrentThread());
243 bool persist = http_server_properties_impl_->SetSpdySetting( 231 bool persist = http_server_properties_impl_->SetSpdySetting(
244 host_port_pair, id, flags, value); 232 host_port_pair, id, flags, value);
245 if (persist) 233 if (persist)
246 ScheduleUpdatePrefsOnIO(); 234 ScheduleUpdatePrefsOnIO();
247 return persist; 235 return persist;
248 } 236 }
249 237
250 void HttpServerPropertiesManager::ClearSpdySettings( 238 void HttpServerPropertiesManager::ClearSpdySettings(
251 const net::HostPortPair& host_port_pair) { 239 const net::HostPortPair& host_port_pair) {
252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 240 DCHECK(io_task_runner_->BelongsToCurrentThread());
253 http_server_properties_impl_->ClearSpdySettings(host_port_pair); 241 http_server_properties_impl_->ClearSpdySettings(host_port_pair);
254 ScheduleUpdatePrefsOnIO(); 242 ScheduleUpdatePrefsOnIO();
255 } 243 }
256 244
257 void HttpServerPropertiesManager::ClearAllSpdySettings() { 245 void HttpServerPropertiesManager::ClearAllSpdySettings() {
258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 246 DCHECK(io_task_runner_->BelongsToCurrentThread());
259 http_server_properties_impl_->ClearAllSpdySettings(); 247 http_server_properties_impl_->ClearAllSpdySettings();
260 ScheduleUpdatePrefsOnIO(); 248 ScheduleUpdatePrefsOnIO();
261 } 249 }
262 250
263 const net::SpdySettingsMap& 251 const net::SpdySettingsMap& HttpServerPropertiesManager::spdy_settings_map()
264 HttpServerPropertiesManager::spdy_settings_map() const { 252 const {
265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 253 DCHECK(io_task_runner_->BelongsToCurrentThread());
266 return http_server_properties_impl_->spdy_settings_map(); 254 return http_server_properties_impl_->spdy_settings_map();
267 } 255 }
268 256
269 void HttpServerPropertiesManager::SetServerNetworkStats( 257 void HttpServerPropertiesManager::SetServerNetworkStats(
270 const net::HostPortPair& host_port_pair, 258 const net::HostPortPair& host_port_pair,
271 NetworkStats stats) { 259 NetworkStats stats) {
272 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 260 DCHECK(io_task_runner_->BelongsToCurrentThread());
273 http_server_properties_impl_->SetServerNetworkStats(host_port_pair, stats); 261 http_server_properties_impl_->SetServerNetworkStats(host_port_pair, stats);
274 } 262 }
275 263
276 const HttpServerPropertiesManager::NetworkStats* 264 const HttpServerPropertiesManager::NetworkStats*
277 HttpServerPropertiesManager::GetServerNetworkStats( 265 HttpServerPropertiesManager::GetServerNetworkStats(
278 const net::HostPortPair& host_port_pair) const { 266 const net::HostPortPair& host_port_pair) const {
279 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 267 DCHECK(io_task_runner_->BelongsToCurrentThread());
280 return http_server_properties_impl_->GetServerNetworkStats(host_port_pair); 268 return http_server_properties_impl_->GetServerNetworkStats(host_port_pair);
281 } 269 }
282 270
283 // 271 //
284 // Update the HttpServerPropertiesImpl's cache with data from preferences. 272 // Update the HttpServerPropertiesImpl's cache with data from preferences.
285 // 273 //
286 void HttpServerPropertiesManager::ScheduleUpdateCacheOnUI() { 274 void HttpServerPropertiesManager::ScheduleUpdateCacheOnPref() {
287 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 275 DCHECK(pref_task_runner_->BelongsToCurrentThread());
288 // Cancel pending updates, if any. 276 // Cancel pending updates, if any.
289 ui_cache_update_timer_->Stop(); 277 pref_cache_update_timer_->Stop();
290 StartCacheUpdateTimerOnUI( 278 StartCacheUpdateTimerOnPref(
291 base::TimeDelta::FromMilliseconds(kUpdateCacheDelayMs)); 279 base::TimeDelta::FromMilliseconds(kUpdateCacheDelayMs));
292 } 280 }
293 281
294 void HttpServerPropertiesManager::StartCacheUpdateTimerOnUI( 282 void HttpServerPropertiesManager::StartCacheUpdateTimerOnPref(
295 base::TimeDelta delay) { 283 base::TimeDelta delay) {
296 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 284 DCHECK(pref_task_runner_->BelongsToCurrentThread());
297 ui_cache_update_timer_->Start( 285 pref_cache_update_timer_->Start(
298 FROM_HERE, delay, this, 286 FROM_HERE,
299 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI); 287 delay,
288 this,
289 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnPref);
300 } 290 }
301 291
302 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI() { 292 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnPref() {
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(pref_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("version",
315 "version", &version)) { 305 &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].
322 const base::DictionaryValue* servers_dict = NULL; 312 const base::DictionaryValue* servers_dict = NULL;
323 if (!http_server_properties_dict.GetDictionaryWithoutPathExpansion( 313 if (!http_server_properties_dict.GetDictionaryWithoutPathExpansion(
324 "servers", &servers_dict)) { 314 "servers", &servers_dict)) {
325 DVLOG(1) << "Malformed http_server_properties for servers."; 315 DVLOG(1) << "Malformed http_server_properties for servers.";
326 return; 316 return;
327 } 317 }
328 318
329 // String is host/port pair of spdy server. 319 // String is host/port pair of spdy server.
330 scoped_ptr<StringVector> spdy_servers(new StringVector); 320 scoped_ptr<StringVector> spdy_servers(new StringVector);
331 scoped_ptr<net::SpdySettingsMap> spdy_settings_map( 321 scoped_ptr<net::SpdySettingsMap> spdy_settings_map(
332 new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist)); 322 new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist));
333 scoped_ptr<net::AlternateProtocolMap> alternate_protocol_map( 323 scoped_ptr<net::AlternateProtocolMap> alternate_protocol_map(
334 new net::AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist)); 324 new net::AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist));
(...skipping 28 matching lines...) Expand all
363 353
364 const base::DictionaryValue* server_pref_dict = NULL; 354 const base::DictionaryValue* server_pref_dict = NULL;
365 if (!it.value().GetAsDictionary(&server_pref_dict)) { 355 if (!it.value().GetAsDictionary(&server_pref_dict)) {
366 DVLOG(1) << "Malformed http_server_properties server: " << server_str; 356 DVLOG(1) << "Malformed http_server_properties server: " << server_str;
367 detected_corrupted_prefs = true; 357 detected_corrupted_prefs = true;
368 continue; 358 continue;
369 } 359 }
370 360
371 // Get if server supports Spdy. 361 // Get if server supports Spdy.
372 bool supports_spdy = false; 362 bool supports_spdy = false;
373 if ((server_pref_dict->GetBoolean( 363 if ((server_pref_dict->GetBoolean("supports_spdy", &supports_spdy)) &&
374 "supports_spdy", &supports_spdy)) && supports_spdy) { 364 supports_spdy) {
375 spdy_servers->push_back(server_str); 365 spdy_servers->push_back(server_str);
376 } 366 }
377 367
378 // Get SpdySettings. 368 // Get SpdySettings.
379 DCHECK(spdy_settings_map->Peek(server) == spdy_settings_map->end()); 369 DCHECK(spdy_settings_map->Peek(server) == spdy_settings_map->end());
380 const base::DictionaryValue* spdy_settings_dict = NULL; 370 const base::DictionaryValue* spdy_settings_dict = NULL;
381 if (server_pref_dict->GetDictionaryWithoutPathExpansion( 371 if (server_pref_dict->GetDictionaryWithoutPathExpansion(
382 "settings", &spdy_settings_dict)) { 372 "settings", &spdy_settings_dict)) {
383 net::SettingsMap settings_map; 373 net::SettingsMap settings_map;
384 for (base::DictionaryValue::Iterator dict_it(*spdy_settings_dict); 374 for (base::DictionaryValue::Iterator dict_it(*spdy_settings_dict);
385 !dict_it.IsAtEnd(); dict_it.Advance()) { 375 !dict_it.IsAtEnd();
376 dict_it.Advance()) {
386 const std::string& id_str = dict_it.key(); 377 const std::string& id_str = dict_it.key();
387 int id = 0; 378 int id = 0;
388 if (!base::StringToInt(id_str, &id)) { 379 if (!base::StringToInt(id_str, &id)) {
389 DVLOG(1) << "Malformed id in SpdySettings for server: " << 380 DVLOG(1) << "Malformed id in SpdySettings for server: " << server_str;
390 server_str;
391 NOTREACHED(); 381 NOTREACHED();
392 continue; 382 continue;
393 } 383 }
394 int value = 0; 384 int value = 0;
395 if (!dict_it.value().GetAsInteger(&value)) { 385 if (!dict_it.value().GetAsInteger(&value)) {
396 DVLOG(1) << "Malformed value in SpdySettings for server: " << 386 DVLOG(1) << "Malformed value in SpdySettings for server: "
397 server_str; 387 << server_str;
398 NOTREACHED(); 388 NOTREACHED();
399 continue; 389 continue;
400 } 390 }
401 net::SettingsFlagsAndValue flags_and_value( 391 net::SettingsFlagsAndValue flags_and_value(net::SETTINGS_FLAG_PERSISTED,
402 net::SETTINGS_FLAG_PERSISTED, value); 392 value);
403 settings_map[static_cast<net::SpdySettingsIds>(id)] = flags_and_value; 393 settings_map[static_cast<net::SpdySettingsIds>(id)] = flags_and_value;
404 } 394 }
405 spdy_settings_map->Put(server, settings_map); 395 spdy_settings_map->Put(server, settings_map);
406 } 396 }
407 397
408 // Get alternate_protocol server. 398 // Get alternate_protocol server.
409 DCHECK(alternate_protocol_map->Peek(server) == 399 DCHECK(alternate_protocol_map->Peek(server) ==
410 alternate_protocol_map->end()); 400 alternate_protocol_map->end());
411 const base::DictionaryValue* port_alternate_protocol_dict = NULL; 401 const base::DictionaryValue* port_alternate_protocol_dict = NULL;
412 if (!server_pref_dict->GetDictionaryWithoutPathExpansion( 402 if (!server_pref_dict->GetDictionaryWithoutPathExpansion(
413 "alternate_protocol", &port_alternate_protocol_dict)) { 403 "alternate_protocol", &port_alternate_protocol_dict)) {
414 continue; 404 continue;
415 } 405 }
416 406
417 if (count >= alternate_protocols_to_load) 407 if (count >= alternate_protocols_to_load)
418 continue; 408 continue;
419 do { 409 do {
420 int port = 0; 410 int port = 0;
421 if (!port_alternate_protocol_dict->GetIntegerWithoutPathExpansion( 411 if (!port_alternate_protocol_dict->GetIntegerWithoutPathExpansion(
422 "port", &port) || (port > (1 << 16))) { 412 "port", &port) ||
413 (port > (1 << 16))) {
423 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; 414 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str;
424 detected_corrupted_prefs = true; 415 detected_corrupted_prefs = true;
425 continue; 416 continue;
426 } 417 }
427 std::string protocol_str; 418 std::string protocol_str;
428 if (!port_alternate_protocol_dict->GetStringWithoutPathExpansion( 419 if (!port_alternate_protocol_dict->GetStringWithoutPathExpansion(
429 "protocol_str", &protocol_str)) { 420 "protocol_str", &protocol_str)) {
430 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; 421 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str;
431 detected_corrupted_prefs = true; 422 detected_corrupted_prefs = true;
432 continue; 423 continue;
433 } 424 }
434 net::AlternateProtocol protocol = 425 net::AlternateProtocol protocol =
435 net::AlternateProtocolFromString(protocol_str); 426 net::AlternateProtocolFromString(protocol_str);
436 if (!net::IsAlternateProtocolValid(protocol)) { 427 if (!net::IsAlternateProtocolValid(protocol)) {
437 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; 428 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str;
438 detected_corrupted_prefs = true; 429 detected_corrupted_prefs = true;
439 continue; 430 continue;
440 } 431 }
441 432
442 net::PortAlternateProtocolPair port_alternate_protocol; 433 net::PortAlternateProtocolPair port_alternate_protocol;
443 port_alternate_protocol.port = port; 434 port_alternate_protocol.port = port;
444 port_alternate_protocol.protocol = protocol; 435 port_alternate_protocol.protocol = protocol;
445 436
446 alternate_protocol_map->Put(server, port_alternate_protocol); 437 alternate_protocol_map->Put(server, port_alternate_protocol);
447 ++count; 438 ++count;
448 } while (false); 439 } while (false);
449 } 440 }
450 441
451 BrowserThread::PostTask( 442 io_task_runner_->PostTask(
452 BrowserThread::IO,
453 FROM_HERE, 443 FROM_HERE,
454 base::Bind(&HttpServerPropertiesManager:: 444 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO,
455 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
497 // 485 //
498 // Update Preferences with data from the cached data. 486 // Update Preferences with data from the cached data.
499 // 487 //
500 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO() { 488 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO() {
501 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 489 DCHECK(io_task_runner_->BelongsToCurrentThread());
502 // Cancel pending updates, if any. 490 // Cancel pending updates, if any.
503 io_prefs_update_timer_->Stop(); 491 io_prefs_update_timer_->Stop();
504 StartPrefsUpdateTimerOnIO( 492 StartPrefsUpdateTimerOnIO(
505 base::TimeDelta::FromMilliseconds(kUpdatePrefsDelayMs)); 493 base::TimeDelta::FromMilliseconds(kUpdatePrefsDelayMs));
506 } 494 }
507 495
508 void HttpServerPropertiesManager::StartPrefsUpdateTimerOnIO( 496 void HttpServerPropertiesManager::StartPrefsUpdateTimerOnIO(
509 base::TimeDelta delay) { 497 base::TimeDelta delay) {
510 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 498 DCHECK(io_task_runner_->BelongsToCurrentThread());
511 // This is overridden in tests to post the task without the delay. 499 // This is overridden in tests to post the task without the delay.
512 io_prefs_update_timer_->Start( 500 io_prefs_update_timer_->Start(
513 FROM_HERE, delay, this, 501 FROM_HERE,
502 delay,
503 this,
514 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO); 504 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO);
515 } 505 }
516 506
517 // This is required so we can set this as the callback for a timer. 507 // This is required so we can set this as the callback for a timer.
518 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO() { 508 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO() {
519 UpdatePrefsFromCacheOnIO(base::Closure()); 509 UpdatePrefsFromCacheOnIO(base::Closure());
520 } 510 }
521 511
522 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO( 512 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO(
523 const base::Closure& completion) { 513 const base::Closure& completion) {
524 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 514 DCHECK(io_task_runner_->BelongsToCurrentThread());
525 515
526 base::ListValue* spdy_server_list = new base::ListValue; 516 base::ListValue* spdy_server_list = new base::ListValue;
527 http_server_properties_impl_->GetSpdyServerList( 517 http_server_properties_impl_->GetSpdyServerList(
528 spdy_server_list, kMaxSupportsSpdyServerHostsToPersist); 518 spdy_server_list, kMaxSupportsSpdyServerHostsToPersist);
529 519
530 net::SpdySettingsMap* spdy_settings_map = 520 net::SpdySettingsMap* spdy_settings_map =
531 new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist); 521 new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist);
532 const net::SpdySettingsMap& main_map = 522 const net::SpdySettingsMap& main_map =
533 http_server_properties_impl_->spdy_settings_map(); 523 http_server_properties_impl_->spdy_settings_map();
534 int count = 0; 524 int count = 0;
(...skipping 19 matching lines...) Expand all
554 if (!canonical_suffix.empty()) { 544 if (!canonical_suffix.empty()) {
555 if (persisted_map.find(canonical_suffix) != persisted_map.end()) 545 if (persisted_map.find(canonical_suffix) != persisted_map.end())
556 continue; 546 continue;
557 persisted_map[canonical_suffix] = true; 547 persisted_map[canonical_suffix] = true;
558 } 548 }
559 alternate_protocol_map->Put(server, it->second); 549 alternate_protocol_map->Put(server, it->second);
560 ++count; 550 ++count;
561 } 551 }
562 552
563 // Update the preferences on the UI thread. 553 // Update the preferences on the UI thread.
564 BrowserThread::PostTask( 554 pref_task_runner_->PostTask(
565 BrowserThread::UI,
566 FROM_HERE, 555 FROM_HERE,
567 base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnUI, 556 base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnPref,
568 ui_weak_ptr_, 557 pref_weak_ptr_,
569 base::Owned(spdy_server_list), 558 base::Owned(spdy_server_list),
570 base::Owned(spdy_settings_map), 559 base::Owned(spdy_settings_map),
571 base::Owned(alternate_protocol_map), 560 base::Owned(alternate_protocol_map),
572 completion)); 561 completion));
573 } 562 }
574 563
575 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, 564 // A local or temporary data structure to hold |supports_spdy|, SpdySettings,
576 // and PortAlternateProtocolPair preferences for a server. This is used only in 565 // and PortAlternateProtocolPair preferences for a server. This is used only in
577 // UpdatePrefsOnUI. 566 // UpdatePrefsOnUI.
578 struct ServerPref { 567 struct ServerPref {
579 ServerPref() 568 ServerPref()
580 : supports_spdy(false), 569 : supports_spdy(false), settings_map(NULL), alternate_protocol(NULL) {}
581 settings_map(NULL),
582 alternate_protocol(NULL) {
583 }
584 ServerPref(bool supports_spdy, 570 ServerPref(bool supports_spdy,
585 const net::SettingsMap* settings_map, 571 const net::SettingsMap* settings_map,
586 const net::PortAlternateProtocolPair* alternate_protocol) 572 const net::PortAlternateProtocolPair* alternate_protocol)
587 : supports_spdy(supports_spdy), 573 : supports_spdy(supports_spdy),
588 settings_map(settings_map), 574 settings_map(settings_map),
589 alternate_protocol(alternate_protocol) { 575 alternate_protocol(alternate_protocol) {}
590 }
591 bool supports_spdy; 576 bool supports_spdy;
592 const net::SettingsMap* settings_map; 577 const net::SettingsMap* settings_map;
593 const net::PortAlternateProtocolPair* alternate_protocol; 578 const net::PortAlternateProtocolPair* alternate_protocol;
594 }; 579 };
595 580
596 void HttpServerPropertiesManager::UpdatePrefsOnUI( 581 void HttpServerPropertiesManager::UpdatePrefsOnPref(
597 base::ListValue* spdy_server_list, 582 base::ListValue* spdy_server_list,
598 net::SpdySettingsMap* spdy_settings_map, 583 net::SpdySettingsMap* spdy_settings_map,
599 net::AlternateProtocolMap* alternate_protocol_map, 584 net::AlternateProtocolMap* alternate_protocol_map,
600 const base::Closure& completion) { 585 const base::Closure& completion) {
601
602 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap; 586 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap;
603 ServerPrefMap server_pref_map; 587 ServerPrefMap server_pref_map;
604 588
605 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 589 DCHECK(pref_task_runner_->BelongsToCurrentThread());
606 590
607 // Add servers that support spdy to server_pref_map. 591 // Add servers that support spdy to server_pref_map.
608 std::string s; 592 std::string s;
609 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); 593 for (base::ListValue::const_iterator list_it = spdy_server_list->begin();
610 list_it != spdy_server_list->end(); ++list_it) { 594 list_it != spdy_server_list->end();
595 ++list_it) {
611 if ((*list_it)->GetAsString(&s)) { 596 if ((*list_it)->GetAsString(&s)) {
612 net::HostPortPair server = net::HostPortPair::FromString(s); 597 net::HostPortPair server = net::HostPortPair::FromString(s);
613 598
614 ServerPrefMap::iterator it = server_pref_map.find(server); 599 ServerPrefMap::iterator it = server_pref_map.find(server);
615 if (it == server_pref_map.end()) { 600 if (it == server_pref_map.end()) {
616 ServerPref server_pref(true, NULL, NULL); 601 ServerPref server_pref(true, NULL, NULL);
617 server_pref_map[server] = server_pref; 602 server_pref_map[server] = server_pref;
618 } else { 603 } else {
619 it->second.supports_spdy = true; 604 it->second.supports_spdy = true;
620 } 605 }
621 } 606 }
622 } 607 }
623 608
624 // Add servers that have SpdySettings to server_pref_map. 609 // Add servers that have SpdySettings to server_pref_map.
625 for (net::SpdySettingsMap::iterator map_it = spdy_settings_map->begin(); 610 for (net::SpdySettingsMap::iterator map_it = spdy_settings_map->begin();
626 map_it != spdy_settings_map->end(); ++map_it) { 611 map_it != spdy_settings_map->end();
612 ++map_it) {
627 const net::HostPortPair& server = map_it->first; 613 const net::HostPortPair& server = map_it->first;
628 614
629 ServerPrefMap::iterator it = server_pref_map.find(server); 615 ServerPrefMap::iterator it = server_pref_map.find(server);
630 if (it == server_pref_map.end()) { 616 if (it == server_pref_map.end()) {
631 ServerPref server_pref(false, &map_it->second, NULL); 617 ServerPref server_pref(false, &map_it->second, NULL);
632 server_pref_map[server] = server_pref; 618 server_pref_map[server] = server_pref;
633 } else { 619 } else {
634 it->second.settings_map = &map_it->second; 620 it->second.settings_map = &map_it->second;
635 } 621 }
636 } 622 }
637 623
638 // Add AlternateProtocol servers to server_pref_map. 624 // Add AlternateProtocol servers to server_pref_map.
639 for (net::AlternateProtocolMap::const_iterator map_it = 625 for (net::AlternateProtocolMap::const_iterator map_it =
640 alternate_protocol_map->begin(); 626 alternate_protocol_map->begin();
641 map_it != alternate_protocol_map->end(); ++map_it) { 627 map_it != alternate_protocol_map->end();
628 ++map_it) {
642 const net::HostPortPair& server = map_it->first; 629 const net::HostPortPair& server = map_it->first;
643 const net::PortAlternateProtocolPair& port_alternate_protocol = 630 const net::PortAlternateProtocolPair& port_alternate_protocol =
644 map_it->second; 631 map_it->second;
645 if (!net::IsAlternateProtocolValid(port_alternate_protocol.protocol)) { 632 if (!net::IsAlternateProtocolValid(port_alternate_protocol.protocol)) {
646 continue; 633 continue;
647 } 634 }
648 635
649 ServerPrefMap::iterator it = server_pref_map.find(server); 636 ServerPrefMap::iterator it = server_pref_map.find(server);
650 if (it == server_pref_map.end()) { 637 if (it == server_pref_map.end()) {
651 ServerPref server_pref(false, NULL, &map_it->second); 638 ServerPref server_pref(false, NULL, &map_it->second);
652 server_pref_map[server] = server_pref; 639 server_pref_map[server] = server_pref;
653 } else { 640 } else {
654 it->second.alternate_protocol = &map_it->second; 641 it->second.alternate_protocol = &map_it->second;
655 } 642 }
656 } 643 }
657 644
658 // Persist the prefs::kHttpServerProperties. 645 // Persist properties to the |path_|.
659 base::DictionaryValue http_server_properties_dict; 646 base::DictionaryValue http_server_properties_dict;
660 base::DictionaryValue* servers_dict = new base::DictionaryValue; 647 base::DictionaryValue* servers_dict = new base::DictionaryValue;
661 for (ServerPrefMap::const_iterator map_it = 648 for (ServerPrefMap::const_iterator map_it = server_pref_map.begin();
662 server_pref_map.begin(); 649 map_it != server_pref_map.end();
663 map_it != server_pref_map.end(); ++map_it) { 650 ++map_it) {
664 const net::HostPortPair& server = map_it->first; 651 const net::HostPortPair& server = map_it->first;
665 const ServerPref& server_pref = map_it->second; 652 const ServerPref& server_pref = map_it->second;
666 653
667 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; 654 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
668 655
669 // Save supports_spdy. 656 // Save supports_spdy.
670 if (server_pref.supports_spdy) 657 if (server_pref.supports_spdy)
671 server_pref_dict->SetBoolean("supports_spdy", server_pref.supports_spdy); 658 server_pref_dict->SetBoolean("supports_spdy", server_pref.supports_spdy);
672 659
673 // Save SPDY settings. 660 // Save SPDY settings.
674 if (server_pref.settings_map) { 661 if (server_pref.settings_map) {
675 base::DictionaryValue* spdy_settings_dict = new base::DictionaryValue; 662 base::DictionaryValue* spdy_settings_dict = new base::DictionaryValue;
676 for (net::SettingsMap::const_iterator it = 663 for (net::SettingsMap::const_iterator it =
677 server_pref.settings_map->begin(); 664 server_pref.settings_map->begin();
678 it != server_pref.settings_map->end(); ++it) { 665 it != server_pref.settings_map->end();
666 ++it) {
679 net::SpdySettingsIds id = it->first; 667 net::SpdySettingsIds id = it->first;
680 uint32 value = it->second.second; 668 uint32 value = it->second.second;
681 std::string key = base::StringPrintf("%u", id); 669 std::string key = base::StringPrintf("%u", id);
682 spdy_settings_dict->SetInteger(key, value); 670 spdy_settings_dict->SetInteger(key, value);
683 } 671 }
684 server_pref_dict->SetWithoutPathExpansion("settings", spdy_settings_dict); 672 server_pref_dict->SetWithoutPathExpansion("settings", spdy_settings_dict);
685 } 673 }
686 674
687 // Save alternate_protocol. 675 // Save alternate_protocol.
688 if (server_pref.alternate_protocol) { 676 if (server_pref.alternate_protocol) {
689 base::DictionaryValue* port_alternate_protocol_dict = 677 base::DictionaryValue* port_alternate_protocol_dict =
690 new base::DictionaryValue; 678 new base::DictionaryValue;
691 const net::PortAlternateProtocolPair* port_alternate_protocol = 679 const net::PortAlternateProtocolPair* port_alternate_protocol =
692 server_pref.alternate_protocol; 680 server_pref.alternate_protocol;
693 port_alternate_protocol_dict->SetInteger( 681 port_alternate_protocol_dict->SetInteger("port",
694 "port", port_alternate_protocol->port); 682 port_alternate_protocol->port);
695 const char* protocol_str = 683 const char* protocol_str =
696 net::AlternateProtocolToString(port_alternate_protocol->protocol); 684 net::AlternateProtocolToString(port_alternate_protocol->protocol);
697 port_alternate_protocol_dict->SetString("protocol_str", protocol_str); 685 port_alternate_protocol_dict->SetString("protocol_str", protocol_str);
698 server_pref_dict->SetWithoutPathExpansion( 686 server_pref_dict->SetWithoutPathExpansion("alternate_protocol",
699 "alternate_protocol", port_alternate_protocol_dict); 687 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(pref_task_runner_->BelongsToCurrentThread());
722 if (!setting_prefs_) 709 if (!setting_prefs_)
723 ScheduleUpdateCacheOnUI(); 710 ScheduleUpdateCacheOnPref();
724 } 711 }
725 712
726 } // namespace chrome_browser_net 713 } // namespace chrome_browser_net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698