OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/http/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/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 | 43 |
44 // Persist 200 MRU SpdySettingsHostPortPairs. | 44 // Persist 200 MRU SpdySettingsHostPortPairs. |
45 const int kMaxSpdySettingsHostsToPersist = 200; | 45 const int kMaxSpdySettingsHostsToPersist = 200; |
46 | 46 |
47 // Persist 300 MRU SupportsSpdyServerHostPortPairs. | 47 // Persist 300 MRU SupportsSpdyServerHostPortPairs. |
48 const int kMaxSupportsSpdyServerHostsToPersist = 300; | 48 const int kMaxSupportsSpdyServerHostsToPersist = 300; |
49 | 49 |
50 // Persist 200 ServerNetworkStats. | 50 // Persist 200 ServerNetworkStats. |
51 const int kMaxServerNetworkStatsHostsToPersist = 200; | 51 const int kMaxServerNetworkStatsHostsToPersist = 200; |
52 | 52 |
| 53 const char kVersionKey[] = "version"; |
| 54 const char kServersKey[] = "servers"; |
| 55 const char kSupportsSpdyKey[] = "supports_spdy"; |
| 56 const char kSettingsKey[] = "settings"; |
| 57 const char kSupportsQuicKey[] = "supports_quic"; |
| 58 const char kUsedQuicKey[] = "used_quic"; |
| 59 const char kAddressKey[] = "address"; |
| 60 const char kAlternateProtocolKey[] = "alternate_protocol"; |
| 61 const char kPortKey[] = "port"; |
| 62 const char kProtocolKey[] = "protocol_str"; |
| 63 const char kProbabilityKey[] = "probability"; |
| 64 const char kNetworkStatsKey[] = "network_stats"; |
| 65 const char kSrttKey[] = "srtt"; |
| 66 |
53 } // namespace | 67 } // namespace |
54 | 68 |
55 //////////////////////////////////////////////////////////////////////////////// | 69 //////////////////////////////////////////////////////////////////////////////// |
56 // HttpServerPropertiesManager | 70 // HttpServerPropertiesManager |
57 | 71 |
58 HttpServerPropertiesManager::HttpServerPropertiesManager( | 72 HttpServerPropertiesManager::HttpServerPropertiesManager( |
59 PrefService* pref_service, | 73 PrefService* pref_service, |
60 const char* pref_path, | 74 const char* pref_path, |
61 scoped_refptr<base::SequencedTaskRunner> network_task_runner) | 75 scoped_refptr<base::SequencedTaskRunner> network_task_runner) |
62 : pref_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 76 : pref_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 } | 120 } |
107 | 121 |
108 // static | 122 // static |
109 void HttpServerPropertiesManager::SetVersion( | 123 void HttpServerPropertiesManager::SetVersion( |
110 base::DictionaryValue* http_server_properties_dict, | 124 base::DictionaryValue* http_server_properties_dict, |
111 int version_number) { | 125 int version_number) { |
112 if (version_number < 0) | 126 if (version_number < 0) |
113 version_number = kVersionNumber; | 127 version_number = kVersionNumber; |
114 DCHECK_LE(version_number, kVersionNumber); | 128 DCHECK_LE(version_number, kVersionNumber); |
115 if (version_number <= kVersionNumber) | 129 if (version_number <= kVersionNumber) |
116 http_server_properties_dict->SetInteger("version", version_number); | 130 http_server_properties_dict->SetInteger(kVersionKey, version_number); |
117 } | 131 } |
118 | 132 |
119 // This is required for conformance with the HttpServerProperties interface. | 133 // This is required for conformance with the HttpServerProperties interface. |
120 base::WeakPtr<HttpServerProperties> HttpServerPropertiesManager::GetWeakPtr() { | 134 base::WeakPtr<HttpServerProperties> HttpServerPropertiesManager::GetWeakPtr() { |
121 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 135 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
122 return network_weak_ptr_factory_->GetWeakPtr(); | 136 return network_weak_ptr_factory_->GetWeakPtr(); |
123 } | 137 } |
124 | 138 |
125 void HttpServerPropertiesManager::Clear() { | 139 void HttpServerPropertiesManager::Clear() { |
126 Clear(base::Closure()); | 140 Clear(base::Closure()); |
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | 350 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
337 | 351 |
338 if (!pref_service_->HasPrefPath(path_)) | 352 if (!pref_service_->HasPrefPath(path_)) |
339 return; | 353 return; |
340 | 354 |
341 bool detected_corrupted_prefs = false; | 355 bool detected_corrupted_prefs = false; |
342 const base::DictionaryValue& http_server_properties_dict = | 356 const base::DictionaryValue& http_server_properties_dict = |
343 *pref_service_->GetDictionary(path_); | 357 *pref_service_->GetDictionary(path_); |
344 | 358 |
345 int version = kMissingVersion; | 359 int version = kMissingVersion; |
346 if (!http_server_properties_dict.GetIntegerWithoutPathExpansion("version", | 360 if (!http_server_properties_dict.GetIntegerWithoutPathExpansion(kVersionKey, |
347 &version)) { | 361 &version)) { |
348 DVLOG(1) << "Missing version. Clearing all properties."; | 362 DVLOG(1) << "Missing version. Clearing all properties."; |
349 return; | 363 return; |
350 } | 364 } |
351 | 365 |
352 // The properties for a given server is in | 366 // The properties for a given server is in |
353 // http_server_properties_dict["servers"][server]. | 367 // http_server_properties_dict["servers"][server]. |
354 const base::DictionaryValue* servers_dict = NULL; | 368 const base::DictionaryValue* servers_dict = NULL; |
355 if (!http_server_properties_dict.GetDictionaryWithoutPathExpansion( | 369 if (!http_server_properties_dict.GetDictionaryWithoutPathExpansion( |
356 "servers", &servers_dict)) { | 370 kServersKey, &servers_dict)) { |
357 DVLOG(1) << "Malformed http_server_properties for servers."; | 371 DVLOG(1) << "Malformed http_server_properties for servers."; |
358 return; | 372 return; |
359 } | 373 } |
360 | 374 |
361 // String is host/port pair of spdy server. | 375 // String is host/port pair of spdy server. |
362 scoped_ptr<StringVector> spdy_servers(new StringVector); | 376 scoped_ptr<StringVector> spdy_servers(new StringVector); |
363 scoped_ptr<SpdySettingsMap> spdy_settings_map( | 377 scoped_ptr<SpdySettingsMap> spdy_settings_map( |
364 new SpdySettingsMap(kMaxSpdySettingsHostsToPersist)); | 378 new SpdySettingsMap(kMaxSpdySettingsHostsToPersist)); |
365 scoped_ptr<AlternateProtocolMap> alternate_protocol_map( | 379 scoped_ptr<AlternateProtocolMap> alternate_protocol_map( |
366 new AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist)); | 380 new AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist)); |
(...skipping 14 matching lines...) Expand all Loading... |
381 | 395 |
382 const base::DictionaryValue* server_pref_dict = NULL; | 396 const base::DictionaryValue* server_pref_dict = NULL; |
383 if (!it.value().GetAsDictionary(&server_pref_dict)) { | 397 if (!it.value().GetAsDictionary(&server_pref_dict)) { |
384 DVLOG(1) << "Malformed http_server_properties server: " << server_str; | 398 DVLOG(1) << "Malformed http_server_properties server: " << server_str; |
385 detected_corrupted_prefs = true; | 399 detected_corrupted_prefs = true; |
386 continue; | 400 continue; |
387 } | 401 } |
388 | 402 |
389 // Get if server supports Spdy. | 403 // Get if server supports Spdy. |
390 bool supports_spdy = false; | 404 bool supports_spdy = false; |
391 if ((server_pref_dict->GetBoolean("supports_spdy", &supports_spdy)) && | 405 if ((server_pref_dict->GetBoolean(kSupportsSpdyKey, &supports_spdy)) && |
392 supports_spdy) { | 406 supports_spdy) { |
393 spdy_servers->push_back(server_str); | 407 spdy_servers->push_back(server_str); |
394 } | 408 } |
395 | 409 |
396 // Get SpdySettings. | 410 AddToSpdySettingsMap(server, *server_pref_dict, spdy_settings_map.get()); |
397 DCHECK(spdy_settings_map->Peek(server) == spdy_settings_map->end()); | 411 if (!AddToAlternateProtocolMap(server, *server_pref_dict, |
398 const base::DictionaryValue* spdy_settings_dict = NULL; | 412 alternate_protocol_map.get()) || |
399 if (server_pref_dict->GetDictionaryWithoutPathExpansion( | 413 !AddToSupportsQuicMap(server, *server_pref_dict, |
400 "settings", &spdy_settings_dict)) { | 414 supports_quic_map.get()) || |
401 SettingsMap settings_map; | 415 !AddToNetworkStatsMap(server, *server_pref_dict, |
402 for (base::DictionaryValue::Iterator dict_it(*spdy_settings_dict); | 416 server_network_stats_map.get())) { |
403 !dict_it.IsAtEnd(); | 417 detected_corrupted_prefs = true; |
404 dict_it.Advance()) { | |
405 const std::string& id_str = dict_it.key(); | |
406 int id = 0; | |
407 if (!base::StringToInt(id_str, &id)) { | |
408 DVLOG(1) << "Malformed id in SpdySettings for server: " << server_str; | |
409 NOTREACHED(); | |
410 continue; | |
411 } | |
412 int value = 0; | |
413 if (!dict_it.value().GetAsInteger(&value)) { | |
414 DVLOG(1) << "Malformed value in SpdySettings for server: " | |
415 << server_str; | |
416 NOTREACHED(); | |
417 continue; | |
418 } | |
419 SettingsFlagsAndValue flags_and_value(SETTINGS_FLAG_PERSISTED, value); | |
420 settings_map[static_cast<SpdySettingsIds>(id)] = flags_and_value; | |
421 } | |
422 spdy_settings_map->Put(server, settings_map); | |
423 } | |
424 | |
425 // Get alternate_protocol server. | |
426 DCHECK(alternate_protocol_map->Peek(server) == | |
427 alternate_protocol_map->end()); | |
428 const base::DictionaryValue* port_alternate_protocol_dict = NULL; | |
429 if (server_pref_dict->GetDictionaryWithoutPathExpansion( | |
430 "alternate_protocol", &port_alternate_protocol_dict)) { | |
431 int port = 0; | |
432 if (!port_alternate_protocol_dict->GetIntegerWithoutPathExpansion( | |
433 "port", &port) || | |
434 !IsPortValid(port)) { | |
435 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; | |
436 detected_corrupted_prefs = true; | |
437 continue; | |
438 } | |
439 std::string protocol_str; | |
440 if (!port_alternate_protocol_dict->GetStringWithoutPathExpansion( | |
441 "protocol_str", &protocol_str)) { | |
442 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; | |
443 detected_corrupted_prefs = true; | |
444 continue; | |
445 } | |
446 AlternateProtocol protocol = AlternateProtocolFromString(protocol_str); | |
447 if (!IsAlternateProtocolValid(protocol)) { | |
448 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; | |
449 detected_corrupted_prefs = true; | |
450 continue; | |
451 } | |
452 | |
453 double probability = 1; | |
454 if (port_alternate_protocol_dict->HasKey("probability") && | |
455 !port_alternate_protocol_dict->GetDoubleWithoutPathExpansion( | |
456 "probability", &probability)) { | |
457 DVLOG(1) << "Malformed Alternate-Protocol server: " << server_str; | |
458 detected_corrupted_prefs = true; | |
459 continue; | |
460 } | |
461 | |
462 AlternateProtocolInfo port_alternate_protocol(static_cast<uint16>(port), | |
463 protocol, probability); | |
464 alternate_protocol_map->Put(server, port_alternate_protocol); | |
465 } | |
466 | |
467 // Get SupportsQuic. | |
468 DCHECK(supports_quic_map->find(server) == supports_quic_map->end()); | |
469 const base::DictionaryValue* supports_quic_dict = NULL; | |
470 if (server_pref_dict->GetDictionaryWithoutPathExpansion( | |
471 "supports_quic", &supports_quic_dict)) { | |
472 bool used_quic = 0; | |
473 if (!supports_quic_dict->GetBooleanWithoutPathExpansion( | |
474 "used_quic", &used_quic)) { | |
475 DVLOG(1) << "Malformed SupportsQuic server: " << server_str; | |
476 detected_corrupted_prefs = true; | |
477 continue; | |
478 } | |
479 std::string address; | |
480 if (!supports_quic_dict->GetStringWithoutPathExpansion( | |
481 "address", &address)) { | |
482 DVLOG(1) << "Malformed SupportsQuic server: " << server_str; | |
483 detected_corrupted_prefs = true; | |
484 continue; | |
485 } | |
486 SupportsQuic supports_quic(used_quic, address); | |
487 supports_quic_map->insert(std::make_pair(server, supports_quic)); | |
488 } | |
489 | |
490 // Get ServerNetworkStats. | |
491 DCHECK(server_network_stats_map->Peek(server) == | |
492 server_network_stats_map->end()); | |
493 const base::DictionaryValue* server_network_stats_dict = NULL; | |
494 if (server_pref_dict->GetDictionaryWithoutPathExpansion( | |
495 "network_stats", &server_network_stats_dict)) { | |
496 int srtt; | |
497 if (!server_network_stats_dict->GetIntegerWithoutPathExpansion("srtt", | |
498 &srtt)) { | |
499 DVLOG(1) << "Malformed ServerNetworkStats for server: " << server_str; | |
500 detected_corrupted_prefs = true; | |
501 continue; | |
502 } | |
503 ServerNetworkStats server_network_stats; | |
504 server_network_stats.srtt = base::TimeDelta::FromInternalValue(srtt); | |
505 // TODO(rtenneti): When QUIC starts using bandwidth_estimate, then persist | |
506 // bandwidth_estimate. | |
507 server_network_stats_map->Put(server, server_network_stats); | |
508 } | 418 } |
509 } | 419 } |
510 | 420 |
511 network_task_runner_->PostTask( | 421 network_task_runner_->PostTask( |
512 FROM_HERE, | 422 FROM_HERE, |
513 base::Bind( | 423 base::Bind( |
514 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread, | 424 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread, |
515 base::Unretained(this), base::Owned(spdy_servers.release()), | 425 base::Unretained(this), base::Owned(spdy_servers.release()), |
516 base::Owned(spdy_settings_map.release()), | 426 base::Owned(spdy_settings_map.release()), |
517 base::Owned(alternate_protocol_map.release()), | 427 base::Owned(alternate_protocol_map.release()), |
518 base::Owned(supports_quic_map.release()), | 428 base::Owned(supports_quic_map.release()), |
519 base::Owned(server_network_stats_map.release()), | 429 base::Owned(server_network_stats_map.release()), |
520 detected_corrupted_prefs)); | 430 detected_corrupted_prefs)); |
521 } | 431 } |
522 | 432 |
| 433 void HttpServerPropertiesManager::AddToSpdySettingsMap( |
| 434 const HostPortPair& server, |
| 435 const base::DictionaryValue& server_pref_dict, |
| 436 SpdySettingsMap* spdy_settings_map) { |
| 437 // Get SpdySettings. |
| 438 DCHECK(spdy_settings_map->Peek(server) == spdy_settings_map->end()); |
| 439 const base::DictionaryValue* spdy_settings_dict = NULL; |
| 440 if (!server_pref_dict.GetDictionaryWithoutPathExpansion( |
| 441 kSettingsKey, &spdy_settings_dict)) { |
| 442 return; |
| 443 } |
| 444 SettingsMap settings_map; |
| 445 for (base::DictionaryValue::Iterator dict_it(*spdy_settings_dict); |
| 446 !dict_it.IsAtEnd(); dict_it.Advance()) { |
| 447 const std::string& id_str = dict_it.key(); |
| 448 int id = 0; |
| 449 if (!base::StringToInt(id_str, &id)) { |
| 450 DVLOG(1) << "Malformed id in SpdySettings for server: " |
| 451 << server.ToString(); |
| 452 NOTREACHED(); |
| 453 continue; |
| 454 } |
| 455 int value = 0; |
| 456 if (!dict_it.value().GetAsInteger(&value)) { |
| 457 DVLOG(1) << "Malformed value in SpdySettings for server: " |
| 458 << server.ToString(); |
| 459 NOTREACHED(); |
| 460 continue; |
| 461 } |
| 462 SettingsFlagsAndValue flags_and_value(SETTINGS_FLAG_PERSISTED, value); |
| 463 settings_map[static_cast<SpdySettingsIds>(id)] = flags_and_value; |
| 464 } |
| 465 spdy_settings_map->Put(server, settings_map); |
| 466 } |
| 467 |
| 468 bool HttpServerPropertiesManager::AddToAlternateProtocolMap( |
| 469 const HostPortPair& server, |
| 470 const base::DictionaryValue& server_pref_dict, |
| 471 AlternateProtocolMap* alternate_protocol_map) { |
| 472 // Get alternate_protocol server. |
| 473 DCHECK(alternate_protocol_map->Peek(server) == alternate_protocol_map->end()); |
| 474 const base::DictionaryValue* port_alternate_protocol_dict = NULL; |
| 475 if (!server_pref_dict.GetDictionaryWithoutPathExpansion( |
| 476 kAlternateProtocolKey, &port_alternate_protocol_dict)) { |
| 477 return true; |
| 478 } |
| 479 int port = 0; |
| 480 if (!port_alternate_protocol_dict->GetIntegerWithoutPathExpansion(kPortKey, |
| 481 &port) || |
| 482 !IsPortValid(port)) { |
| 483 DVLOG(1) << "Malformed Alternate-Protocol server: " << server.ToString(); |
| 484 return false; |
| 485 } |
| 486 std::string protocol_str; |
| 487 if (!port_alternate_protocol_dict->GetStringWithoutPathExpansion( |
| 488 kProtocolKey, &protocol_str)) { |
| 489 DVLOG(1) << "Malformed Alternate-Protocol server: " << server.ToString(); |
| 490 return false; |
| 491 } |
| 492 AlternateProtocol protocol = AlternateProtocolFromString(protocol_str); |
| 493 if (!IsAlternateProtocolValid(protocol)) { |
| 494 DVLOG(1) << "Malformed Alternate-Protocol server: " << server.ToString(); |
| 495 return false; |
| 496 } |
| 497 double probability = 1; |
| 498 if (port_alternate_protocol_dict->HasKey(kProbabilityKey) && |
| 499 !port_alternate_protocol_dict->GetDoubleWithoutPathExpansion( |
| 500 kProbabilityKey, &probability)) { |
| 501 DVLOG(1) << "Malformed Alternate-Protocol server: " << server.ToString(); |
| 502 return false; |
| 503 } |
| 504 |
| 505 AlternateProtocolInfo port_alternate_protocol(static_cast<uint16>(port), |
| 506 protocol, probability); |
| 507 alternate_protocol_map->Put(server, port_alternate_protocol); |
| 508 return true; |
| 509 } |
| 510 |
| 511 bool HttpServerPropertiesManager::AddToSupportsQuicMap( |
| 512 const HostPortPair& server, |
| 513 const base::DictionaryValue& server_pref_dict, |
| 514 SupportsQuicMap* supports_quic_map) { |
| 515 DCHECK(supports_quic_map->find(server) == supports_quic_map->end()); |
| 516 const base::DictionaryValue* supports_quic_dict = NULL; |
| 517 if (!server_pref_dict.GetDictionaryWithoutPathExpansion( |
| 518 kSupportsQuicKey, &supports_quic_dict)) { |
| 519 return true; |
| 520 } |
| 521 bool used_quic = 0; |
| 522 if (!supports_quic_dict->GetBooleanWithoutPathExpansion(kUsedQuicKey, |
| 523 &used_quic)) { |
| 524 DVLOG(1) << "Malformed SupportsQuic server: " << server.ToString(); |
| 525 return false; |
| 526 } |
| 527 std::string address; |
| 528 if (!supports_quic_dict->GetStringWithoutPathExpansion(kAddressKey, |
| 529 &address)) { |
| 530 DVLOG(1) << "Malformed SupportsQuic server: " << server.ToString(); |
| 531 return false; |
| 532 } |
| 533 SupportsQuic supports_quic(used_quic, address); |
| 534 supports_quic_map->insert(std::make_pair(server, supports_quic)); |
| 535 return true; |
| 536 } |
| 537 |
| 538 bool HttpServerPropertiesManager::AddToNetworkStatsMap( |
| 539 const HostPortPair& server, |
| 540 const base::DictionaryValue& server_pref_dict, |
| 541 ServerNetworkStatsMap* network_stats_map) { |
| 542 DCHECK(network_stats_map->Peek(server) == network_stats_map->end()); |
| 543 const base::DictionaryValue* server_network_stats_dict = NULL; |
| 544 if (!server_pref_dict.GetDictionaryWithoutPathExpansion( |
| 545 kNetworkStatsKey, &server_network_stats_dict)) { |
| 546 return true; |
| 547 } |
| 548 int srtt; |
| 549 if (!server_network_stats_dict->GetIntegerWithoutPathExpansion(kSrttKey, |
| 550 &srtt)) { |
| 551 DVLOG(1) << "Malformed ServerNetworkStats for server: " |
| 552 << server.ToString(); |
| 553 return false; |
| 554 } |
| 555 ServerNetworkStats server_network_stats; |
| 556 server_network_stats.srtt = base::TimeDelta::FromInternalValue(srtt); |
| 557 // TODO(rtenneti): When QUIC starts using bandwidth_estimate, then persist |
| 558 // bandwidth_estimate. |
| 559 network_stats_map->Put(server, server_network_stats); |
| 560 return true; |
| 561 } |
| 562 |
523 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread( | 563 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread( |
524 StringVector* spdy_servers, | 564 StringVector* spdy_servers, |
525 SpdySettingsMap* spdy_settings_map, | 565 SpdySettingsMap* spdy_settings_map, |
526 AlternateProtocolMap* alternate_protocol_map, | 566 AlternateProtocolMap* alternate_protocol_map, |
527 SupportsQuicMap* supports_quic_map, | 567 SupportsQuicMap* supports_quic_map, |
528 ServerNetworkStatsMap* server_network_stats_map, | 568 ServerNetworkStatsMap* server_network_stats_map, |
529 bool detected_corrupted_prefs) { | 569 bool detected_corrupted_prefs) { |
530 // Preferences have the master data because admins might have pushed new | 570 // Preferences have the master data because admins might have pushed new |
531 // preferences. Update the cached data with new data from preferences. | 571 // preferences. Update the cached data with new data from preferences. |
532 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); | 572 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 | 730 |
691 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | 731 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
692 | 732 |
693 // Add servers that support spdy to server_pref_map. | 733 // Add servers that support spdy to server_pref_map. |
694 std::string s; | 734 std::string s; |
695 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); | 735 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); |
696 list_it != spdy_server_list->end(); | 736 list_it != spdy_server_list->end(); |
697 ++list_it) { | 737 ++list_it) { |
698 if ((*list_it)->GetAsString(&s)) { | 738 if ((*list_it)->GetAsString(&s)) { |
699 HostPortPair server = HostPortPair::FromString(s); | 739 HostPortPair server = HostPortPair::FromString(s); |
700 | 740 server_pref_map[server].supports_spdy = true; |
701 ServerPrefMap::iterator it = server_pref_map.find(server); | |
702 if (it == server_pref_map.end()) { | |
703 ServerPref server_pref(true, NULL, NULL, NULL, NULL); | |
704 server_pref_map[server] = server_pref; | |
705 } else { | |
706 it->second.supports_spdy = true; | |
707 } | |
708 } | 741 } |
709 } | 742 } |
710 | 743 |
711 // Add servers that have SpdySettings to server_pref_map. | 744 // Add servers that have SpdySettings to server_pref_map. |
712 for (SpdySettingsMap::iterator map_it = spdy_settings_map->begin(); | 745 for (SpdySettingsMap::iterator map_it = spdy_settings_map->begin(); |
713 map_it != spdy_settings_map->end(); ++map_it) { | 746 map_it != spdy_settings_map->end(); ++map_it) { |
714 const HostPortPair& server = map_it->first; | 747 const HostPortPair& server = map_it->first; |
715 | 748 server_pref_map[server].settings_map = &map_it->second; |
716 ServerPrefMap::iterator it = server_pref_map.find(server); | |
717 if (it == server_pref_map.end()) { | |
718 ServerPref server_pref(false, &map_it->second, NULL, NULL, NULL); | |
719 server_pref_map[server] = server_pref; | |
720 } else { | |
721 it->second.settings_map = &map_it->second; | |
722 } | |
723 } | 749 } |
724 | 750 |
725 // Add AlternateProtocol servers to server_pref_map. | 751 // Add AlternateProtocol servers to server_pref_map. |
726 for (AlternateProtocolMap::const_iterator map_it = | 752 for (AlternateProtocolMap::const_iterator map_it = |
727 alternate_protocol_map->begin(); | 753 alternate_protocol_map->begin(); |
728 map_it != alternate_protocol_map->end(); ++map_it) { | 754 map_it != alternate_protocol_map->end(); ++map_it) { |
729 const HostPortPair& server = map_it->first; | 755 const HostPortPair& server = map_it->first; |
730 const AlternateProtocolInfo& port_alternate_protocol = map_it->second; | 756 const AlternateProtocolInfo& port_alternate_protocol = map_it->second; |
731 if (!IsAlternateProtocolValid(port_alternate_protocol.protocol)) { | 757 if (!IsAlternateProtocolValid(port_alternate_protocol.protocol)) { |
732 continue; | 758 continue; |
733 } | 759 } |
734 | 760 server_pref_map[server].alternate_protocol = &map_it->second; |
735 ServerPrefMap::iterator it = server_pref_map.find(server); | |
736 if (it == server_pref_map.end()) { | |
737 ServerPref server_pref(false, NULL, &map_it->second, NULL, NULL); | |
738 server_pref_map[server] = server_pref; | |
739 } else { | |
740 it->second.alternate_protocol = &map_it->second; | |
741 } | |
742 } | 761 } |
743 | 762 |
744 // Add SupportsQuic servers to server_pref_map. | 763 // Add SupportsQuic servers to server_pref_map. |
745 for (SupportsQuicMap::const_iterator map_it = supports_quic_map->begin(); | 764 for (SupportsQuicMap::const_iterator map_it = supports_quic_map->begin(); |
746 map_it != supports_quic_map->end(); ++map_it) { | 765 map_it != supports_quic_map->end(); ++map_it) { |
747 const HostPortPair& server = map_it->first; | 766 const HostPortPair& server = map_it->first; |
748 | 767 server_pref_map[server].supports_quic = &map_it->second; |
749 ServerPrefMap::iterator it = server_pref_map.find(server); | |
750 if (it == server_pref_map.end()) { | |
751 ServerPref server_pref(false, NULL, NULL, &map_it->second, NULL); | |
752 server_pref_map[server] = server_pref; | |
753 } else { | |
754 it->second.supports_quic = &map_it->second; | |
755 } | |
756 } | 768 } |
757 | 769 |
758 // Add ServerNetworkStats servers to server_pref_map. | 770 // Add ServerNetworkStats servers to server_pref_map. |
759 for (ServerNetworkStatsMap::const_iterator map_it = | 771 for (ServerNetworkStatsMap::const_iterator map_it = |
760 server_network_stats_map->begin(); | 772 server_network_stats_map->begin(); |
761 map_it != server_network_stats_map->end(); ++map_it) { | 773 map_it != server_network_stats_map->end(); ++map_it) { |
762 const HostPortPair& server = map_it->first; | 774 const HostPortPair& server = map_it->first; |
763 | 775 server_pref_map[server].server_network_stats = &map_it->second; |
764 ServerPrefMap::iterator it = server_pref_map.find(server); | |
765 if (it == server_pref_map.end()) { | |
766 ServerPref server_pref(false, NULL, NULL, NULL, &map_it->second); | |
767 server_pref_map[server] = server_pref; | |
768 } else { | |
769 it->second.server_network_stats = &map_it->second; | |
770 } | |
771 } | 776 } |
772 | 777 |
773 // Persist properties to the |path_|. | 778 // Persist properties to the |path_|. |
774 base::DictionaryValue http_server_properties_dict; | 779 base::DictionaryValue http_server_properties_dict; |
775 base::DictionaryValue* servers_dict = new base::DictionaryValue; | 780 base::DictionaryValue* servers_dict = new base::DictionaryValue; |
776 for (ServerPrefMap::const_iterator map_it = server_pref_map.begin(); | 781 for (ServerPrefMap::const_iterator map_it = server_pref_map.begin(); |
777 map_it != server_pref_map.end(); | 782 map_it != server_pref_map.end(); |
778 ++map_it) { | 783 ++map_it) { |
779 const HostPortPair& server = map_it->first; | 784 const HostPortPair& server = map_it->first; |
780 const ServerPref& server_pref = map_it->second; | 785 const ServerPref& server_pref = map_it->second; |
781 | 786 |
782 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; | 787 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; |
783 | 788 |
784 // Save supports_spdy. | 789 // Save supports_spdy. |
785 if (server_pref.supports_spdy) | 790 if (server_pref.supports_spdy) |
786 server_pref_dict->SetBoolean("supports_spdy", server_pref.supports_spdy); | 791 server_pref_dict->SetBoolean(kSupportsSpdyKey, server_pref.supports_spdy); |
787 | 792 SaveSpdySettingsToServerPrefs(server_pref.settings_map, server_pref_dict); |
788 // Save SPDY settings. | 793 SaveAlternateProtocolToServerPrefs(server_pref.alternate_protocol, |
789 if (server_pref.settings_map) { | 794 server_pref_dict); |
790 base::DictionaryValue* spdy_settings_dict = new base::DictionaryValue; | 795 SaveSupportsQuicToServerPrefs(server_pref.supports_quic, server_pref_dict); |
791 for (SettingsMap::const_iterator it = server_pref.settings_map->begin(); | 796 SaveNetworkStatsToServerPrefs(server_pref.server_network_stats, |
792 it != server_pref.settings_map->end(); ++it) { | 797 server_pref_dict); |
793 SpdySettingsIds id = it->first; | |
794 uint32 value = it->second.second; | |
795 std::string key = base::StringPrintf("%u", id); | |
796 spdy_settings_dict->SetInteger(key, value); | |
797 } | |
798 server_pref_dict->SetWithoutPathExpansion("settings", spdy_settings_dict); | |
799 } | |
800 | |
801 // Save alternate_protocol. | |
802 const AlternateProtocolInfo* port_alternate_protocol = | |
803 server_pref.alternate_protocol; | |
804 if (port_alternate_protocol && !port_alternate_protocol->is_broken) { | |
805 base::DictionaryValue* port_alternate_protocol_dict = | |
806 new base::DictionaryValue; | |
807 port_alternate_protocol_dict->SetInteger("port", | |
808 port_alternate_protocol->port); | |
809 const char* protocol_str = | |
810 AlternateProtocolToString(port_alternate_protocol->protocol); | |
811 port_alternate_protocol_dict->SetString("protocol_str", protocol_str); | |
812 port_alternate_protocol_dict->SetDouble( | |
813 "probability", port_alternate_protocol->probability); | |
814 server_pref_dict->SetWithoutPathExpansion( | |
815 "alternate_protocol", port_alternate_protocol_dict); | |
816 } | |
817 | |
818 // Save supports_quic. | |
819 if (server_pref.supports_quic) { | |
820 base::DictionaryValue* supports_quic_dict = new base::DictionaryValue; | |
821 const SupportsQuic* supports_quic = server_pref.supports_quic; | |
822 supports_quic_dict->SetBoolean("used_quic", supports_quic->used_quic); | |
823 supports_quic_dict->SetString("address", supports_quic->address); | |
824 server_pref_dict->SetWithoutPathExpansion( | |
825 "supports_quic", supports_quic_dict); | |
826 } | |
827 | |
828 // Save ServerNetworkStats. | |
829 if (server_pref.server_network_stats) { | |
830 base::DictionaryValue* server_network_stats_dict = | |
831 new base::DictionaryValue; | |
832 const ServerNetworkStats* server_network_stats = | |
833 server_pref.server_network_stats; | |
834 // Becasue JSON doesn't support int64, persist int64 as a string. | |
835 server_network_stats_dict->SetInteger( | |
836 "srtt", | |
837 static_cast<int>(server_network_stats->srtt.ToInternalValue())); | |
838 // TODO(rtenneti): When QUIC starts using bandwidth_estimate, then persist | |
839 // bandwidth_estimate. | |
840 server_pref_dict->SetWithoutPathExpansion("network_stats", | |
841 server_network_stats_dict); | |
842 } | |
843 | 798 |
844 servers_dict->SetWithoutPathExpansion(server.ToString(), server_pref_dict); | 799 servers_dict->SetWithoutPathExpansion(server.ToString(), server_pref_dict); |
845 } | 800 } |
846 | 801 |
847 http_server_properties_dict.SetWithoutPathExpansion("servers", servers_dict); | 802 http_server_properties_dict.SetWithoutPathExpansion(kServersKey, |
| 803 servers_dict); |
848 SetVersion(&http_server_properties_dict, kVersionNumber); | 804 SetVersion(&http_server_properties_dict, kVersionNumber); |
849 setting_prefs_ = true; | 805 setting_prefs_ = true; |
850 pref_service_->Set(path_, http_server_properties_dict); | 806 pref_service_->Set(path_, http_server_properties_dict); |
851 setting_prefs_ = false; | 807 setting_prefs_ = false; |
852 | 808 |
853 // Note that |completion| will be fired after we have written everything to | 809 // Note that |completion| will be fired after we have written everything to |
854 // the Preferences, but likely before these changes are serialized to disk. | 810 // the Preferences, but likely before these changes are serialized to disk. |
855 // This is not a problem though, as JSONPrefStore guarantees that this will | 811 // This is not a problem though, as JSONPrefStore guarantees that this will |
856 // happen, pretty soon, and even in the case we shut down immediately. | 812 // happen, pretty soon, and even in the case we shut down immediately. |
857 if (!completion.is_null()) | 813 if (!completion.is_null()) |
858 completion.Run(); | 814 completion.Run(); |
859 } | 815 } |
860 | 816 |
| 817 void HttpServerPropertiesManager::SaveSpdySettingsToServerPrefs( |
| 818 const SettingsMap* settings_map, |
| 819 base::DictionaryValue* server_pref_dict) { |
| 820 if (!settings_map) { |
| 821 return; |
| 822 } |
| 823 base::DictionaryValue* spdy_settings_dict = new base::DictionaryValue; |
| 824 for (SettingsMap::const_iterator it = settings_map->begin(); |
| 825 it != settings_map->end(); ++it) { |
| 826 SpdySettingsIds id = it->first; |
| 827 uint32 value = it->second.second; |
| 828 std::string key = base::StringPrintf("%u", id); |
| 829 spdy_settings_dict->SetInteger(key, value); |
| 830 } |
| 831 server_pref_dict->SetWithoutPathExpansion(kSettingsKey, spdy_settings_dict); |
| 832 } |
| 833 |
| 834 void HttpServerPropertiesManager::SaveAlternateProtocolToServerPrefs( |
| 835 const AlternateProtocolInfo* port_alternate_protocol, |
| 836 base::DictionaryValue* server_pref_dict) { |
| 837 if (!port_alternate_protocol || port_alternate_protocol->is_broken) |
| 838 return; |
| 839 |
| 840 base::DictionaryValue* port_alternate_protocol_dict = |
| 841 new base::DictionaryValue; |
| 842 port_alternate_protocol_dict->SetInteger(kPortKey, |
| 843 port_alternate_protocol->port); |
| 844 const char* protocol_str = |
| 845 AlternateProtocolToString(port_alternate_protocol->protocol); |
| 846 port_alternate_protocol_dict->SetString(kProtocolKey, protocol_str); |
| 847 port_alternate_protocol_dict->SetDouble(kProbabilityKey, |
| 848 port_alternate_protocol->probability); |
| 849 server_pref_dict->SetWithoutPathExpansion(kAlternateProtocolKey, |
| 850 port_alternate_protocol_dict); |
| 851 } |
| 852 |
| 853 void HttpServerPropertiesManager::SaveSupportsQuicToServerPrefs( |
| 854 const SupportsQuic* supports_quic, |
| 855 base::DictionaryValue* server_pref_dict) { |
| 856 // Save supports_quic. |
| 857 if (!supports_quic) |
| 858 return; |
| 859 |
| 860 base::DictionaryValue* supports_quic_dict = new base::DictionaryValue; |
| 861 supports_quic_dict->SetBoolean(kUsedQuicKey, supports_quic->used_quic); |
| 862 supports_quic_dict->SetString(kAddressKey, supports_quic->address); |
| 863 server_pref_dict->SetWithoutPathExpansion(kSupportsQuicKey, |
| 864 supports_quic_dict); |
| 865 } |
| 866 |
| 867 void HttpServerPropertiesManager::SaveNetworkStatsToServerPrefs( |
| 868 const ServerNetworkStats* server_network_stats, |
| 869 base::DictionaryValue* server_pref_dict) { |
| 870 if (!server_network_stats) |
| 871 return; |
| 872 |
| 873 base::DictionaryValue* server_network_stats_dict = new base::DictionaryValue; |
| 874 // Becasue JSON doesn't support int64, persist int64 as a string. |
| 875 server_network_stats_dict->SetInteger( |
| 876 kSrttKey, static_cast<int>(server_network_stats->srtt.ToInternalValue())); |
| 877 // TODO(rtenneti): When QUIC starts using bandwidth_estimate, then persist |
| 878 // bandwidth_estimate. |
| 879 server_pref_dict->SetWithoutPathExpansion(kNetworkStatsKey, |
| 880 server_network_stats_dict); |
| 881 } |
| 882 |
861 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { | 883 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { |
862 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); | 884 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); |
863 if (!setting_prefs_) | 885 if (!setting_prefs_) |
864 ScheduleUpdateCacheOnPrefThread(); | 886 ScheduleUpdateCacheOnPrefThread(); |
865 } | 887 } |
866 | 888 |
867 } // namespace net | 889 } // namespace net |
OLD | NEW |