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

Side by Side Diff: chrome/browser/net/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: Fix renames in 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/net/http_server_properties_manager.h"
6
7 #include "base/bind.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/rand_util.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h"
14 #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
22 using content::BrowserThread;
23
24 namespace chrome_browser_net {
25
26 namespace {
27
28 // 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
30 // timer.
31 const int64 kUpdateCacheDelayMs = 1000;
32
33 // Time to wait before starting an update the preferences from the
34 // http_server_properties_impl_ cache. Scheduling another update during this
35 // period will reset the timer.
36 const int64 kUpdatePrefsDelayMs = 5000;
37
38 // "version" 0 indicates, http_server_properties doesn't have "version"
39 // property.
40 const int kMissingVersion = 0;
41
42 // The version number of persisted http_server_properties.
43 const int kVersionNumber = 3;
44
45 typedef std::vector<std::string> StringVector;
46
47 // Load either 200 or 1000 servers based on a coin flip.
48 const int k200AlternateProtocolHostsToLoad = 200;
49 const int k1000AlternateProtocolHostsToLoad = 1000;
50 // Persist 1000 MRU AlternateProtocolHostPortPairs.
51 const int kMaxAlternateProtocolHostsToPersist = 1000;
52
53 // Persist 200 MRU SpdySettingsHostPortPairs.
54 const int kMaxSpdySettingsHostsToPersist = 200;
55
56 // Persist 300 MRU SupportsSpdyServerHostPortPairs.
57 const int kMaxSupportsSpdyServerHostsToPersist = 300;
58
59 } // namespace
60
61 ////////////////////////////////////////////////////////////////////////////////
62 // HttpServerPropertiesManager
63
64 HttpServerPropertiesManager::HttpServerPropertiesManager(
65 PrefService* pref_service)
66 : pref_service_(pref_service),
67 setting_prefs_(false) {
68 DCHECK_CURRENTLY_ON(BrowserThread::UI);
69 DCHECK(pref_service);
70 ui_weak_ptr_factory_.reset(
71 new base::WeakPtrFactory<HttpServerPropertiesManager>(this));
72 ui_weak_ptr_ = ui_weak_ptr_factory_->GetWeakPtr();
73 ui_cache_update_timer_.reset(
74 new base::OneShotTimer<HttpServerPropertiesManager>);
75 pref_change_registrar_.Init(pref_service_);
76 pref_change_registrar_.Add(
77 prefs::kHttpServerProperties,
78 base::Bind(&HttpServerPropertiesManager::OnHttpServerPropertiesChanged,
79 base::Unretained(this)));
80 }
81
82 HttpServerPropertiesManager::~HttpServerPropertiesManager() {
83 DCHECK_CURRENTLY_ON(BrowserThread::IO);
84 io_weak_ptr_factory_.reset();
85 }
86
87 void HttpServerPropertiesManager::InitializeOnIOThread() {
88 DCHECK_CURRENTLY_ON(BrowserThread::IO);
89 io_weak_ptr_factory_.reset(
90 new base::WeakPtrFactory<HttpServerPropertiesManager>(this));
91 http_server_properties_impl_.reset(new net::HttpServerPropertiesImpl());
92
93 io_prefs_update_timer_.reset(
94 new base::OneShotTimer<HttpServerPropertiesManager>);
95
96 BrowserThread::PostTask(
97 BrowserThread::UI,
98 FROM_HERE,
99 base::Bind(&HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI,
100 ui_weak_ptr_));
101 }
102
103 void HttpServerPropertiesManager::ShutdownOnUIThread() {
104 DCHECK_CURRENTLY_ON(BrowserThread::UI);
105 // Cancel any pending updates, and stop listening for pref change updates.
106 ui_cache_update_timer_->Stop();
107 ui_weak_ptr_factory_.reset();
108 pref_change_registrar_.RemoveAll();
109 }
110
111 // 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(
121 base::DictionaryValue* http_server_properties_dict,
122 int version_number) {
123 if (version_number < 0)
124 version_number = kVersionNumber;
125 DCHECK_LE(version_number, kVersionNumber);
126 if (version_number <= kVersionNumber)
127 http_server_properties_dict->SetInteger("version", version_number);
128 }
129
130 // This is required for conformance with the HttpServerProperties interface.
131 base::WeakPtr<net::HttpServerProperties>
132 HttpServerPropertiesManager::GetWeakPtr() {
133 DCHECK_CURRENTLY_ON(BrowserThread::IO);
134 return io_weak_ptr_factory_->GetWeakPtr();
135 }
136
137 void HttpServerPropertiesManager::Clear() {
138 Clear(base::Closure());
139 }
140
141 void HttpServerPropertiesManager::Clear(const base::Closure& completion) {
142 DCHECK_CURRENTLY_ON(BrowserThread::IO);
143
144 http_server_properties_impl_->Clear();
145 UpdatePrefsFromCacheOnIO(completion);
146 }
147
148 bool HttpServerPropertiesManager::SupportsSpdy(
149 const net::HostPortPair& server) {
150 DCHECK_CURRENTLY_ON(BrowserThread::IO);
151 return http_server_properties_impl_->SupportsSpdy(server);
152 }
153
154 void HttpServerPropertiesManager::SetSupportsSpdy(
155 const net::HostPortPair& server,
156 bool support_spdy) {
157 DCHECK_CURRENTLY_ON(BrowserThread::IO);
158
159 http_server_properties_impl_->SetSupportsSpdy(server, support_spdy);
160 ScheduleUpdatePrefsOnIO();
161 }
162
163 bool HttpServerPropertiesManager::HasAlternateProtocol(
164 const net::HostPortPair& server) {
165 DCHECK_CURRENTLY_ON(BrowserThread::IO);
166 return http_server_properties_impl_->HasAlternateProtocol(server);
167 }
168
169 net::AlternateProtocolInfo
170 HttpServerPropertiesManager::GetAlternateProtocol(
171 const net::HostPortPair& server) {
172 DCHECK_CURRENTLY_ON(BrowserThread::IO);
173 return http_server_properties_impl_->GetAlternateProtocol(server);
174 }
175
176 void HttpServerPropertiesManager::SetAlternateProtocol(
177 const net::HostPortPair& server,
178 uint16 alternate_port,
179 net::AlternateProtocol alternate_protocol,
180 double alternate_probability) {
181 DCHECK_CURRENTLY_ON(BrowserThread::IO);
182 http_server_properties_impl_->SetAlternateProtocol(
183 server, alternate_port, alternate_protocol, alternate_probability);
184 ScheduleUpdatePrefsOnIO();
185 }
186
187 void HttpServerPropertiesManager::SetBrokenAlternateProtocol(
188 const net::HostPortPair& server) {
189 DCHECK_CURRENTLY_ON(BrowserThread::IO);
190 http_server_properties_impl_->SetBrokenAlternateProtocol(server);
191 ScheduleUpdatePrefsOnIO();
192 }
193
194 bool HttpServerPropertiesManager::WasAlternateProtocolRecentlyBroken(
195 const net::HostPortPair& server) {
196 DCHECK_CURRENTLY_ON(BrowserThread::IO);
197 return http_server_properties_impl_->WasAlternateProtocolRecentlyBroken(
198 server);
199 }
200
201 void HttpServerPropertiesManager::ConfirmAlternateProtocol(
202 const net::HostPortPair& server) {
203 DCHECK_CURRENTLY_ON(BrowserThread::IO);
204 http_server_properties_impl_->ConfirmAlternateProtocol(server);
205 ScheduleUpdatePrefsOnIO();
206 }
207
208 void HttpServerPropertiesManager::ClearAlternateProtocol(
209 const net::HostPortPair& server) {
210 DCHECK_CURRENTLY_ON(BrowserThread::IO);
211 http_server_properties_impl_->ClearAlternateProtocol(server);
212 ScheduleUpdatePrefsOnIO();
213 }
214
215 const net::AlternateProtocolMap&
216 HttpServerPropertiesManager::alternate_protocol_map() const {
217 DCHECK_CURRENTLY_ON(BrowserThread::IO);
218 return http_server_properties_impl_->alternate_protocol_map();
219 }
220
221 void HttpServerPropertiesManager::SetAlternateProtocolExperiment(
222 net::AlternateProtocolExperiment experiment) {
223 DCHECK_CURRENTLY_ON(BrowserThread::IO);
224 http_server_properties_impl_->SetAlternateProtocolExperiment(experiment);
225 }
226
227 void HttpServerPropertiesManager::SetAlternateProtocolProbabilityThreshold(
228 double threshold) {
229 DCHECK_CURRENTLY_ON(BrowserThread::IO);
230 http_server_properties_impl_->SetAlternateProtocolProbabilityThreshold(
231 threshold);
232 }
233
234 net::AlternateProtocolExperiment
235 HttpServerPropertiesManager::GetAlternateProtocolExperiment() const {
236 DCHECK_CURRENTLY_ON(BrowserThread::IO);
237 return http_server_properties_impl_->GetAlternateProtocolExperiment();
238 }
239
240 const net::SettingsMap&
241 HttpServerPropertiesManager::GetSpdySettings(
242 const net::HostPortPair& host_port_pair) {
243 DCHECK_CURRENTLY_ON(BrowserThread::IO);
244 return http_server_properties_impl_->GetSpdySettings(host_port_pair);
245 }
246
247 bool HttpServerPropertiesManager::SetSpdySetting(
248 const net::HostPortPair& host_port_pair,
249 net::SpdySettingsIds id,
250 net::SpdySettingsFlags flags,
251 uint32 value) {
252 DCHECK_CURRENTLY_ON(BrowserThread::IO);
253 bool persist = http_server_properties_impl_->SetSpdySetting(
254 host_port_pair, id, flags, value);
255 if (persist)
256 ScheduleUpdatePrefsOnIO();
257 return persist;
258 }
259
260 void HttpServerPropertiesManager::ClearSpdySettings(
261 const net::HostPortPair& host_port_pair) {
262 DCHECK_CURRENTLY_ON(BrowserThread::IO);
263 http_server_properties_impl_->ClearSpdySettings(host_port_pair);
264 ScheduleUpdatePrefsOnIO();
265 }
266
267 void HttpServerPropertiesManager::ClearAllSpdySettings() {
268 DCHECK_CURRENTLY_ON(BrowserThread::IO);
269 http_server_properties_impl_->ClearAllSpdySettings();
270 ScheduleUpdatePrefsOnIO();
271 }
272
273 const net::SpdySettingsMap&
274 HttpServerPropertiesManager::spdy_settings_map() const {
275 DCHECK_CURRENTLY_ON(BrowserThread::IO);
276 return http_server_properties_impl_->spdy_settings_map();
277 }
278
279 void HttpServerPropertiesManager::SetServerNetworkStats(
280 const net::HostPortPair& host_port_pair,
281 NetworkStats stats) {
282 DCHECK_CURRENTLY_ON(BrowserThread::IO);
283 http_server_properties_impl_->SetServerNetworkStats(host_port_pair, stats);
284 }
285
286 const HttpServerPropertiesManager::NetworkStats*
287 HttpServerPropertiesManager::GetServerNetworkStats(
288 const net::HostPortPair& host_port_pair) const {
289 DCHECK_CURRENTLY_ON(BrowserThread::IO);
290 return http_server_properties_impl_->GetServerNetworkStats(host_port_pair);
291 }
292
293 //
294 // Update the HttpServerPropertiesImpl's cache with data from preferences.
295 //
296 void HttpServerPropertiesManager::ScheduleUpdateCacheOnUI() {
297 DCHECK_CURRENTLY_ON(BrowserThread::UI);
298 // Cancel pending updates, if any.
299 ui_cache_update_timer_->Stop();
300 StartCacheUpdateTimerOnUI(
301 base::TimeDelta::FromMilliseconds(kUpdateCacheDelayMs));
302 }
303
304 void HttpServerPropertiesManager::StartCacheUpdateTimerOnUI(
305 base::TimeDelta delay) {
306 DCHECK_CURRENTLY_ON(BrowserThread::UI);
307 ui_cache_update_timer_->Start(
308 FROM_HERE, delay, this,
309 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI);
310 }
311
312 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnUI() {
313 // The preferences can only be read on the UI thread.
314 DCHECK_CURRENTLY_ON(BrowserThread::UI);
315
316 if (!pref_service_->HasPrefPath(prefs::kHttpServerProperties))
317 return;
318
319 bool detected_corrupted_prefs = false;
320 const base::DictionaryValue& http_server_properties_dict =
321 *pref_service_->GetDictionary(prefs::kHttpServerProperties);
322
323 int version = kMissingVersion;
324 if (!http_server_properties_dict.GetIntegerWithoutPathExpansion(
325 "version", &version)) {
326 DVLOG(1) << "Missing version. Clearing all properties.";
327 return;
328 }
329
330 // The properties for a given server is in
331 // http_server_properties_dict["servers"][server].
332 const base::DictionaryValue* servers_dict = NULL;
333 if (!http_server_properties_dict.GetDictionaryWithoutPathExpansion(
334 "servers", &servers_dict)) {
335 DVLOG(1) << "Malformed http_server_properties for servers.";
336 return;
337 }
338
339 // String is host/port pair of spdy server.
340 scoped_ptr<StringVector> spdy_servers(new StringVector);
341 scoped_ptr<net::SpdySettingsMap> spdy_settings_map(
342 new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist));
343 scoped_ptr<net::AlternateProtocolMap> alternate_protocol_map(
344 new net::AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist));
345 // TODO(rtenneti): Delete the following code after the experiment.
346 int alternate_protocols_to_load = k200AlternateProtocolHostsToLoad;
347 net::AlternateProtocolExperiment alternate_protocol_experiment =
348 net::ALTERNATE_PROTOCOL_NOT_PART_OF_EXPERIMENT;
349 if (version == kVersionNumber) {
350 if (base::RandInt(0, 99) == 0) {
351 alternate_protocol_experiment =
352 net::ALTERNATE_PROTOCOL_TRUNCATED_200_SERVERS;
353 } else {
354 alternate_protocols_to_load = k1000AlternateProtocolHostsToLoad;
355 alternate_protocol_experiment =
356 net::ALTERNATE_PROTOCOL_TRUNCATED_1000_SERVERS;
357 }
358 DVLOG(1) << "# of servers that support alternate_protocol: "
359 << alternate_protocols_to_load;
360 }
361
362 int count = 0;
363 for (base::DictionaryValue::Iterator it(*servers_dict); !it.IsAtEnd();
364 it.Advance()) {
365 // Get server's host/pair.
366 const std::string& server_str = it.key();
367 net::HostPortPair server = net::HostPortPair::FromString(server_str);
368 if (server.host().empty()) {
369 DVLOG(1) << "Malformed http_server_properties for server: " << server_str;
370 detected_corrupted_prefs = true;
371 continue;
372 }
373
374 const base::DictionaryValue* server_pref_dict = NULL;
375 if (!it.value().GetAsDictionary(&server_pref_dict)) {
376 DVLOG(1) << "Malformed http_server_properties server: " << server_str;
377 detected_corrupted_prefs = true;
378 continue;
379 }
380
381 // Get if server supports Spdy.
382 bool supports_spdy = false;
383 if ((server_pref_dict->GetBoolean(
384 "supports_spdy", &supports_spdy)) && supports_spdy) {
385 spdy_servers->push_back(server_str);
386 }
387
388 // Get SpdySettings.
389 DCHECK(spdy_settings_map->Peek(server) == spdy_settings_map->end());
390 const base::DictionaryValue* spdy_settings_dict = NULL;
391 if (server_pref_dict->GetDictionaryWithoutPathExpansion(
392 "settings", &spdy_settings_dict)) {
393 net::SettingsMap settings_map;
394 for (base::DictionaryValue::Iterator dict_it(*spdy_settings_dict);
395 !dict_it.IsAtEnd(); dict_it.Advance()) {
396 const std::string& id_str = dict_it.key();
397 int id = 0;
398 if (!base::StringToInt(id_str, &id)) {
399 DVLOG(1) << "Malformed id in SpdySettings for server: " <<
400 server_str;
401 NOTREACHED();
402 continue;
403 }
404 int value = 0;
405 if (!dict_it.value().GetAsInteger(&value)) {
406 DVLOG(1) << "Malformed value in SpdySettings for server: " <<
407 server_str;
408 NOTREACHED();
409 continue;
410 }
411 net::SettingsFlagsAndValue flags_and_value(
412 net::SETTINGS_FLAG_PERSISTED, value);
413 settings_map[static_cast<net::SpdySettingsIds>(id)] = flags_and_value;
414 }
415 spdy_settings_map->Put(server, settings_map);
416 }
417
418 // Get alternate_protocol server.
419 DCHECK(alternate_protocol_map->Peek(server) ==
420 alternate_protocol_map->end());
421 const base::DictionaryValue* port_alternate_protocol_dict = NULL;
422 if (!server_pref_dict->GetDictionaryWithoutPathExpansion(
423 "alternate_protocol", &port_alternate_protocol_dict)) {
424 continue;
425 }
426
427 if (count >= alternate_protocols_to_load)
428 continue;
429 do {
430 int port = 0;
431 if (!port_alternate_protocol_dict->GetIntegerWithoutPathExpansion(
432 "port", &port) || (port > (1 << 16))) {
433 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str;
434 detected_corrupted_prefs = true;
435 continue;
436 }
437 std::string protocol_str;
438 if (!port_alternate_protocol_dict->GetStringWithoutPathExpansion(
439 "protocol_str", &protocol_str)) {
440 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str;
441 detected_corrupted_prefs = true;
442 continue;
443 }
444 net::AlternateProtocol protocol =
445 net::AlternateProtocolFromString(protocol_str);
446 if (!net::IsAlternateProtocolValid(protocol)) {
447 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str;
448 detected_corrupted_prefs = true;
449 continue;
450 }
451
452 double probability = 1;
453 if (port_alternate_protocol_dict->HasKey("probability") &&
454 !port_alternate_protocol_dict->GetDoubleWithoutPathExpansion(
455 "probability", &probability)) {
456 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str;
457 detected_corrupted_prefs = true;
458 continue;
459 }
460
461 net::AlternateProtocolInfo port_alternate_protocol(port,
462 protocol,
463 probability);
464 alternate_protocol_map->Put(server, port_alternate_protocol);
465 ++count;
466 } while (false);
467 }
468
469 BrowserThread::PostTask(
470 BrowserThread::IO,
471 FROM_HERE,
472 base::Bind(&HttpServerPropertiesManager::
473 UpdateCacheFromPrefsOnIO,
474 base::Unretained(this),
475 base::Owned(spdy_servers.release()),
476 base::Owned(spdy_settings_map.release()),
477 base::Owned(alternate_protocol_map.release()),
478 alternate_protocol_experiment,
479 detected_corrupted_prefs));
480 }
481
482 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnIO(
483 StringVector* spdy_servers,
484 net::SpdySettingsMap* spdy_settings_map,
485 net::AlternateProtocolMap* alternate_protocol_map,
486 net::AlternateProtocolExperiment alternate_protocol_experiment,
487 bool detected_corrupted_prefs) {
488 // Preferences have the master data because admins might have pushed new
489 // preferences. Update the cached data with new data from preferences.
490 DCHECK_CURRENTLY_ON(BrowserThread::IO);
491
492 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size());
493 http_server_properties_impl_->InitializeSpdyServers(spdy_servers, true);
494
495 // Update the cached data and use the new spdy_settings from preferences.
496 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdySettings", spdy_settings_map->size());
497 http_server_properties_impl_->InitializeSpdySettingsServers(
498 spdy_settings_map);
499
500 // Update the cached data and use the new Alternate-Protocol server list from
501 // preferences.
502 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers",
503 alternate_protocol_map->size());
504 http_server_properties_impl_->InitializeAlternateProtocolServers(
505 alternate_protocol_map);
506 http_server_properties_impl_->SetAlternateProtocolExperiment(
507 alternate_protocol_experiment);
508
509 // Update the prefs with what we have read (delete all corrupted prefs).
510 if (detected_corrupted_prefs)
511 ScheduleUpdatePrefsOnIO();
512 }
513
514
515 //
516 // Update Preferences with data from the cached data.
517 //
518 void HttpServerPropertiesManager::ScheduleUpdatePrefsOnIO() {
519 DCHECK_CURRENTLY_ON(BrowserThread::IO);
520 // Cancel pending updates, if any.
521 io_prefs_update_timer_->Stop();
522 StartPrefsUpdateTimerOnIO(
523 base::TimeDelta::FromMilliseconds(kUpdatePrefsDelayMs));
524 }
525
526 void HttpServerPropertiesManager::StartPrefsUpdateTimerOnIO(
527 base::TimeDelta delay) {
528 DCHECK_CURRENTLY_ON(BrowserThread::IO);
529 // This is overridden in tests to post the task without the delay.
530 io_prefs_update_timer_->Start(
531 FROM_HERE, delay, this,
532 &HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO);
533 }
534
535 // This is required so we can set this as the callback for a timer.
536 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO() {
537 UpdatePrefsFromCacheOnIO(base::Closure());
538 }
539
540 void HttpServerPropertiesManager::UpdatePrefsFromCacheOnIO(
541 const base::Closure& completion) {
542 DCHECK_CURRENTLY_ON(BrowserThread::IO);
543
544 base::ListValue* spdy_server_list = new base::ListValue;
545 http_server_properties_impl_->GetSpdyServerList(
546 spdy_server_list, kMaxSupportsSpdyServerHostsToPersist);
547
548 net::SpdySettingsMap* spdy_settings_map =
549 new net::SpdySettingsMap(kMaxSpdySettingsHostsToPersist);
550 const net::SpdySettingsMap& main_map =
551 http_server_properties_impl_->spdy_settings_map();
552 int count = 0;
553 for (net::SpdySettingsMap::const_iterator it = main_map.begin();
554 it != main_map.end() && count < kMaxSpdySettingsHostsToPersist;
555 ++it, ++count) {
556 spdy_settings_map->Put(it->first, it->second);
557 }
558
559 net::AlternateProtocolMap* alternate_protocol_map =
560 new net::AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist);
561 const net::AlternateProtocolMap& map =
562 http_server_properties_impl_->alternate_protocol_map();
563 count = 0;
564 typedef std::map<std::string, bool> CanonicalHostPersistedMap;
565 CanonicalHostPersistedMap persisted_map;
566 for (net::AlternateProtocolMap::const_iterator it = map.begin();
567 it != map.end() && count < kMaxAlternateProtocolHostsToPersist;
568 ++it) {
569 const net::HostPortPair& server = it->first;
570 std::string canonical_suffix =
571 http_server_properties_impl_->GetCanonicalSuffix(server);
572 if (!canonical_suffix.empty()) {
573 if (persisted_map.find(canonical_suffix) != persisted_map.end())
574 continue;
575 persisted_map[canonical_suffix] = true;
576 }
577 alternate_protocol_map->Put(server, it->second);
578 ++count;
579 }
580
581 // Update the preferences on the UI thread.
582 BrowserThread::PostTask(
583 BrowserThread::UI,
584 FROM_HERE,
585 base::Bind(&HttpServerPropertiesManager::UpdatePrefsOnUI,
586 ui_weak_ptr_,
587 base::Owned(spdy_server_list),
588 base::Owned(spdy_settings_map),
589 base::Owned(alternate_protocol_map),
590 completion));
591 }
592
593 // A local or temporary data structure to hold |supports_spdy|, SpdySettings,
594 // and AlternateProtocolInfo preferences for a server. This is used only in
595 // UpdatePrefsOnUI.
596 struct ServerPref {
597 ServerPref()
598 : supports_spdy(false),
599 settings_map(NULL),
600 alternate_protocol(NULL) {
601 }
602 ServerPref(bool supports_spdy,
603 const net::SettingsMap* settings_map,
604 const net::AlternateProtocolInfo* alternate_protocol)
605 : supports_spdy(supports_spdy),
606 settings_map(settings_map),
607 alternate_protocol(alternate_protocol) {
608 }
609 bool supports_spdy;
610 const net::SettingsMap* settings_map;
611 const net::AlternateProtocolInfo* alternate_protocol;
612 };
613
614 void HttpServerPropertiesManager::UpdatePrefsOnUI(
615 base::ListValue* spdy_server_list,
616 net::SpdySettingsMap* spdy_settings_map,
617 net::AlternateProtocolMap* alternate_protocol_map,
618 const base::Closure& completion) {
619
620 typedef std::map<net::HostPortPair, ServerPref> ServerPrefMap;
621 ServerPrefMap server_pref_map;
622
623 DCHECK_CURRENTLY_ON(BrowserThread::UI);
624
625 // Add servers that support spdy to server_pref_map.
626 std::string s;
627 for (base::ListValue::const_iterator list_it = spdy_server_list->begin();
628 list_it != spdy_server_list->end(); ++list_it) {
629 if ((*list_it)->GetAsString(&s)) {
630 net::HostPortPair server = net::HostPortPair::FromString(s);
631
632 ServerPrefMap::iterator it = server_pref_map.find(server);
633 if (it == server_pref_map.end()) {
634 ServerPref server_pref(true, NULL, NULL);
635 server_pref_map[server] = server_pref;
636 } else {
637 it->second.supports_spdy = true;
638 }
639 }
640 }
641
642 // Add servers that have SpdySettings to server_pref_map.
643 for (net::SpdySettingsMap::iterator map_it = spdy_settings_map->begin();
644 map_it != spdy_settings_map->end(); ++map_it) {
645 const net::HostPortPair& server = map_it->first;
646
647 ServerPrefMap::iterator it = server_pref_map.find(server);
648 if (it == server_pref_map.end()) {
649 ServerPref server_pref(false, &map_it->second, NULL);
650 server_pref_map[server] = server_pref;
651 } else {
652 it->second.settings_map = &map_it->second;
653 }
654 }
655
656 // Add AlternateProtocol servers to server_pref_map.
657 for (net::AlternateProtocolMap::const_iterator map_it =
658 alternate_protocol_map->begin();
659 map_it != alternate_protocol_map->end(); ++map_it) {
660 const net::HostPortPair& server = map_it->first;
661 const net::AlternateProtocolInfo& port_alternate_protocol =
662 map_it->second;
663 if (!net::IsAlternateProtocolValid(port_alternate_protocol.protocol)) {
664 continue;
665 }
666
667 ServerPrefMap::iterator it = server_pref_map.find(server);
668 if (it == server_pref_map.end()) {
669 ServerPref server_pref(false, NULL, &map_it->second);
670 server_pref_map[server] = server_pref;
671 } else {
672 it->second.alternate_protocol = &map_it->second;
673 }
674 }
675
676 // Persist the prefs::kHttpServerProperties.
677 base::DictionaryValue http_server_properties_dict;
678 base::DictionaryValue* servers_dict = new base::DictionaryValue;
679 for (ServerPrefMap::const_iterator map_it =
680 server_pref_map.begin();
681 map_it != server_pref_map.end(); ++map_it) {
682 const net::HostPortPair& server = map_it->first;
683 const ServerPref& server_pref = map_it->second;
684
685 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
686
687 // Save supports_spdy.
688 if (server_pref.supports_spdy)
689 server_pref_dict->SetBoolean("supports_spdy", server_pref.supports_spdy);
690
691 // Save SPDY settings.
692 if (server_pref.settings_map) {
693 base::DictionaryValue* spdy_settings_dict = new base::DictionaryValue;
694 for (net::SettingsMap::const_iterator it =
695 server_pref.settings_map->begin();
696 it != server_pref.settings_map->end(); ++it) {
697 net::SpdySettingsIds id = it->first;
698 uint32 value = it->second.second;
699 std::string key = base::StringPrintf("%u", id);
700 spdy_settings_dict->SetInteger(key, value);
701 }
702 server_pref_dict->SetWithoutPathExpansion("settings", spdy_settings_dict);
703 }
704
705 // Save alternate_protocol.
706 if (server_pref.alternate_protocol) {
707 base::DictionaryValue* port_alternate_protocol_dict =
708 new base::DictionaryValue;
709 const net::AlternateProtocolInfo* port_alternate_protocol =
710 server_pref.alternate_protocol;
711 port_alternate_protocol_dict->SetInteger(
712 "port", port_alternate_protocol->port);
713 const char* protocol_str =
714 net::AlternateProtocolToString(port_alternate_protocol->protocol);
715 port_alternate_protocol_dict->SetString("protocol_str", protocol_str);
716 port_alternate_protocol_dict->SetDouble(
717 "probability", port_alternate_protocol->probability);
718 server_pref_dict->SetWithoutPathExpansion(
719 "alternate_protocol", port_alternate_protocol_dict);
720 }
721
722 servers_dict->SetWithoutPathExpansion(server.ToString(), server_pref_dict);
723 }
724
725 http_server_properties_dict.SetWithoutPathExpansion("servers", servers_dict);
726 SetVersion(&http_server_properties_dict, kVersionNumber);
727 setting_prefs_ = true;
728 pref_service_->Set(prefs::kHttpServerProperties,
729 http_server_properties_dict);
730 setting_prefs_ = false;
731
732 // Note that |completion| will be fired after we have written everything to
733 // the Preferences, but likely before these changes are serialized to disk.
734 // This is not a problem though, as JSONPrefStore guarantees that this will
735 // happen, pretty soon, and even in the case we shut down immediately.
736 if (!completion.is_null())
737 completion.Run();
738 }
739
740 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() {
741 DCHECK_CURRENTLY_ON(BrowserThread::UI);
742 if (!setting_prefs_)
743 ScheduleUpdateCacheOnUI();
744 }
745
746 } // namespace chrome_browser_net
OLDNEW
« no previous file with comments | « chrome/browser/net/http_server_properties_manager.h ('k') | chrome/browser/net/http_server_properties_manager_factory.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698