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

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

Issue 1043973002: Introduce AlternativeServiceInfo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix unittest compilation problem. Created 5 years, 8 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 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 const int kMaxServerNetworkStatsHostsToPersist = 200; 51 const int kMaxServerNetworkStatsHostsToPersist = 200;
52 52
53 const char kVersionKey[] = "version"; 53 const char kVersionKey[] = "version";
54 const char kServersKey[] = "servers"; 54 const char kServersKey[] = "servers";
55 const char kSupportsSpdyKey[] = "supports_spdy"; 55 const char kSupportsSpdyKey[] = "supports_spdy";
56 const char kSettingsKey[] = "settings"; 56 const char kSettingsKey[] = "settings";
57 const char kSupportsQuicKey[] = "supports_quic"; 57 const char kSupportsQuicKey[] = "supports_quic";
58 const char kUsedQuicKey[] = "used_quic"; 58 const char kUsedQuicKey[] = "used_quic";
59 const char kAddressKey[] = "address"; 59 const char kAddressKey[] = "address";
60 const char kAlternateProtocolKey[] = "alternate_protocol"; 60 const char kAlternateProtocolKey[] = "alternate_protocol";
61 const char kAlternativeServiceKey[] = "alternative_service";
62 const char kProtocolKey[] = "protocol_str";
63 const char kHostKey[] = "host";
61 const char kPortKey[] = "port"; 64 const char kPortKey[] = "port";
62 const char kProtocolKey[] = "protocol_str";
63 const char kProbabilityKey[] = "probability"; 65 const char kProbabilityKey[] = "probability";
64 const char kNetworkStatsKey[] = "network_stats"; 66 const char kNetworkStatsKey[] = "network_stats";
65 const char kSrttKey[] = "srtt"; 67 const char kSrttKey[] = "srtt";
66 68
67 } // namespace 69 } // namespace
68 70
69 //////////////////////////////////////////////////////////////////////////////// 71 ////////////////////////////////////////////////////////////////////////////////
70 // HttpServerPropertiesManager 72 // HttpServerPropertiesManager
71 73
72 HttpServerPropertiesManager::HttpServerPropertiesManager( 74 HttpServerPropertiesManager::HttpServerPropertiesManager(
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 ScheduleUpdatePrefsOnNetworkThread(); 235 ScheduleUpdatePrefsOnNetworkThread();
234 } 236 }
235 237
236 void HttpServerPropertiesManager::ClearAlternativeService( 238 void HttpServerPropertiesManager::ClearAlternativeService(
237 const HostPortPair& origin) { 239 const HostPortPair& origin) {
238 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 240 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
239 http_server_properties_impl_->ClearAlternativeService(origin); 241 http_server_properties_impl_->ClearAlternativeService(origin);
240 ScheduleUpdatePrefsOnNetworkThread(); 242 ScheduleUpdatePrefsOnNetworkThread();
241 } 243 }
242 244
243 const AlternateProtocolMap& 245 const AlternativeServiceMap&
244 HttpServerPropertiesManager::alternate_protocol_map() const { 246 HttpServerPropertiesManager::alternative_service_map() const {
245 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 247 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
246 return http_server_properties_impl_->alternate_protocol_map(); 248 return http_server_properties_impl_->alternative_service_map();
247 } 249 }
248 250
249 void HttpServerPropertiesManager::SetAlternateProtocolProbabilityThreshold( 251 void HttpServerPropertiesManager::SetAlternateProtocolProbabilityThreshold(
250 double threshold) { 252 double threshold) {
251 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 253 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
252 http_server_properties_impl_->SetAlternateProtocolProbabilityThreshold( 254 http_server_properties_impl_->SetAlternateProtocolProbabilityThreshold(
253 threshold); 255 threshold);
254 } 256 }
255 257
256 const SettingsMap& HttpServerPropertiesManager::GetSpdySettings( 258 const SettingsMap& HttpServerPropertiesManager::GetSpdySettings(
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
373 return; 375 return;
374 } 376 }
375 377
376 IPAddressNumber* addr = new IPAddressNumber; 378 IPAddressNumber* addr = new IPAddressNumber;
377 ReadSupportsQuic(http_server_properties_dict, addr); 379 ReadSupportsQuic(http_server_properties_dict, addr);
378 380
379 // String is host/port pair of spdy server. 381 // String is host/port pair of spdy server.
380 scoped_ptr<StringVector> spdy_servers(new StringVector); 382 scoped_ptr<StringVector> spdy_servers(new StringVector);
381 scoped_ptr<SpdySettingsMap> spdy_settings_map( 383 scoped_ptr<SpdySettingsMap> spdy_settings_map(
382 new SpdySettingsMap(kMaxSpdySettingsHostsToPersist)); 384 new SpdySettingsMap(kMaxSpdySettingsHostsToPersist));
383 scoped_ptr<AlternateProtocolMap> alternate_protocol_map( 385 scoped_ptr<AlternativeServiceMap> alternative_service_map(
384 new AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist)); 386 new AlternativeServiceMap(kMaxAlternateProtocolHostsToPersist));
385 scoped_ptr<ServerNetworkStatsMap> server_network_stats_map( 387 scoped_ptr<ServerNetworkStatsMap> server_network_stats_map(
386 new ServerNetworkStatsMap(kMaxServerNetworkStatsHostsToPersist)); 388 new ServerNetworkStatsMap(kMaxServerNetworkStatsHostsToPersist));
387 389
388 for (base::DictionaryValue::Iterator it(*servers_dict); !it.IsAtEnd(); 390 for (base::DictionaryValue::Iterator it(*servers_dict); !it.IsAtEnd();
389 it.Advance()) { 391 it.Advance()) {
390 // Get server's host/pair. 392 // Get server's host/pair.
391 const std::string& server_str = it.key(); 393 const std::string& server_str = it.key();
392 HostPortPair server = HostPortPair::FromString(server_str); 394 HostPortPair server = HostPortPair::FromString(server_str);
393 if (server.host().empty()) { 395 if (server.host().empty()) {
394 DVLOG(1) << "Malformed http_server_properties for server: " << server_str; 396 DVLOG(1) << "Malformed http_server_properties for server: " << server_str;
395 detected_corrupted_prefs = true; 397 detected_corrupted_prefs = true;
396 continue; 398 continue;
397 } 399 }
398 400
399 const base::DictionaryValue* server_pref_dict = NULL; 401 const base::DictionaryValue* server_pref_dict = NULL;
400 if (!it.value().GetAsDictionary(&server_pref_dict)) { 402 if (!it.value().GetAsDictionary(&server_pref_dict)) {
401 DVLOG(1) << "Malformed http_server_properties server: " << server_str; 403 DVLOG(1) << "Malformed http_server_properties server: " << server_str;
402 detected_corrupted_prefs = true; 404 detected_corrupted_prefs = true;
403 continue; 405 continue;
404 } 406 }
405 407
406 // Get if server supports Spdy. 408 // Get if server supports Spdy.
407 bool supports_spdy = false; 409 bool supports_spdy = false;
408 if ((server_pref_dict->GetBoolean(kSupportsSpdyKey, &supports_spdy)) && 410 if ((server_pref_dict->GetBoolean(kSupportsSpdyKey, &supports_spdy)) &&
409 supports_spdy) { 411 supports_spdy) {
410 spdy_servers->push_back(server_str); 412 spdy_servers->push_back(server_str);
411 } 413 }
412 414
413 AddToSpdySettingsMap(server, *server_pref_dict, spdy_settings_map.get()); 415 AddToSpdySettingsMap(server, *server_pref_dict, spdy_settings_map.get());
414 if (!AddToAlternateProtocolMap(server, *server_pref_dict, 416 if (!AddToAlternativeServiceMap(server, *server_pref_dict,
415 alternate_protocol_map.get()) || 417 alternative_service_map.get()) ||
416 !AddToNetworkStatsMap(server, *server_pref_dict, 418 !AddToNetworkStatsMap(server, *server_pref_dict,
417 server_network_stats_map.get())) { 419 server_network_stats_map.get())) {
418 detected_corrupted_prefs = true; 420 detected_corrupted_prefs = true;
419 } 421 }
420 } 422 }
421 423
422 network_task_runner_->PostTask( 424 network_task_runner_->PostTask(
423 FROM_HERE, 425 FROM_HERE,
424 base::Bind( 426 base::Bind(
425 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread, 427 &HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread,
426 base::Unretained(this), base::Owned(spdy_servers.release()), 428 base::Unretained(this), base::Owned(spdy_servers.release()),
427 base::Owned(spdy_settings_map.release()), 429 base::Owned(spdy_settings_map.release()),
428 base::Owned(alternate_protocol_map.release()), base::Owned(addr), 430 base::Owned(alternative_service_map.release()), base::Owned(addr),
429 base::Owned(server_network_stats_map.release()), 431 base::Owned(server_network_stats_map.release()),
430 detected_corrupted_prefs)); 432 detected_corrupted_prefs));
431 } 433 }
432 434
433 void HttpServerPropertiesManager::AddToSpdySettingsMap( 435 void HttpServerPropertiesManager::AddToSpdySettingsMap(
434 const HostPortPair& server, 436 const HostPortPair& server,
435 const base::DictionaryValue& server_pref_dict, 437 const base::DictionaryValue& server_pref_dict,
436 SpdySettingsMap* spdy_settings_map) { 438 SpdySettingsMap* spdy_settings_map) {
437 // Get SpdySettings. 439 // Get SpdySettings.
438 DCHECK(spdy_settings_map->Peek(server) == spdy_settings_map->end()); 440 DCHECK(spdy_settings_map->Peek(server) == spdy_settings_map->end());
(...skipping 19 matching lines...) Expand all
458 << server.ToString(); 460 << server.ToString();
459 NOTREACHED(); 461 NOTREACHED();
460 continue; 462 continue;
461 } 463 }
462 SettingsFlagsAndValue flags_and_value(SETTINGS_FLAG_PERSISTED, value); 464 SettingsFlagsAndValue flags_and_value(SETTINGS_FLAG_PERSISTED, value);
463 settings_map[static_cast<SpdySettingsIds>(id)] = flags_and_value; 465 settings_map[static_cast<SpdySettingsIds>(id)] = flags_and_value;
464 } 466 }
465 spdy_settings_map->Put(server, settings_map); 467 spdy_settings_map->Put(server, settings_map);
466 } 468 }
467 469
468 AlternateProtocolInfo HttpServerPropertiesManager::ParseAlternateProtocolDict( 470 AlternativeServiceInfo HttpServerPropertiesManager::ParseAlternativeServiceDict(
469 const base::DictionaryValue& alternate_protocol_dict, 471 const base::DictionaryValue& alternative_service_dict,
470 const std::string& server_str) { 472 const std::string& server_str) {
471 AlternateProtocolInfo alternate_protocol; 473 // Protocol is mandatory.
472 int port = 0;
473 if (!alternate_protocol_dict.GetInteger(kPortKey, &port) ||
474 !IsPortValid(port)) {
475 DVLOG(1) << "Malformed alternative service port for server: " << server_str;
476 return alternate_protocol;
477 }
478 alternate_protocol.port = static_cast<uint16>(port);
479
480 double probability = 1.0;
481 if (alternate_protocol_dict.HasKey(kProbabilityKey) &&
482 !alternate_protocol_dict.GetDoubleWithoutPathExpansion(kProbabilityKey,
483 &probability)) {
484 DVLOG(1) << "Malformed alternative service probability for server: "
485 << server_str;
486 return alternate_protocol;
487 }
488 alternate_protocol.probability = probability;
489
490 std::string protocol_str; 474 std::string protocol_str;
491 if (!alternate_protocol_dict.GetStringWithoutPathExpansion(kProtocolKey, 475 if (!alternative_service_dict.GetStringWithoutPathExpansion(kProtocolKey,
492 &protocol_str)) { 476 &protocol_str)) {
493 DVLOG(1) << "Malformed alternative service protocol string for server: " 477 DVLOG(1) << "Malformed alternative service protocol string for server: "
494 << server_str; 478 << server_str;
495 return alternate_protocol; 479 return AlternativeServiceInfo();
496 } 480 }
497 AlternateProtocol protocol = AlternateProtocolFromString(protocol_str); 481 AlternateProtocol protocol = AlternateProtocolFromString(protocol_str);
498 if (!IsAlternateProtocolValid(protocol)) { 482 if (!IsAlternateProtocolValid(protocol)) {
499 DVLOG(1) << "Invalid alternative service protocol string for server: " 483 DVLOG(1) << "Invalid alternative service protocol string for server: "
500 << server_str; 484 << server_str;
501 return alternate_protocol; 485 return AlternativeServiceInfo();
502 } 486 }
503 alternate_protocol.protocol = protocol;
504 487
505 return alternate_protocol; 488 // Host is optional, defaults to "".
489 std::string host;
490 if (alternative_service_dict.HasKey(kHostKey) &&
491 !alternative_service_dict.GetStringWithoutPathExpansion(kHostKey,
492 &host)) {
493 DVLOG(1) << "Malformed alternative service host string for server: "
494 << server_str;
495 return AlternativeServiceInfo();
496 }
497
498 // Port is mandatory.
499 int port = 0;
500 if (!alternative_service_dict.GetInteger(kPortKey, &port) ||
501 !IsPortValid(port)) {
502 DVLOG(1) << "Malformed alternative service port for server: " << server_str;
503 return AlternativeServiceInfo();
504 }
505
506 // Probability is optional, defaults to 1.0.
507 double probability = 1.0;
508 if (alternative_service_dict.HasKey(kProbabilityKey) &&
509 !alternative_service_dict.GetDoubleWithoutPathExpansion(kProbabilityKey,
510 &probability)) {
511 DVLOG(1) << "Malformed alternative service probability for server: "
512 << server_str;
513 return AlternativeServiceInfo();
514 }
515
516 return AlternativeServiceInfo(protocol, host, static_cast<uint16>(port),
517 probability);
506 } 518 }
507 519
508 bool HttpServerPropertiesManager::AddToAlternateProtocolMap( 520 bool HttpServerPropertiesManager::AddToAlternativeServiceMap(
509 const HostPortPair& server, 521 const HostPortPair& server,
510 const base::DictionaryValue& server_pref_dict, 522 const base::DictionaryValue& server_pref_dict,
511 AlternateProtocolMap* alternate_protocol_map) { 523 AlternativeServiceMap* alternative_service_map) {
512 // Get alternate_protocol server. 524 DCHECK(alternative_service_map->Peek(server) ==
513 DCHECK(alternate_protocol_map->Peek(server) == alternate_protocol_map->end()); 525 alternative_service_map->end());
514 const base::DictionaryValue* alternate_protocol_dict = NULL; 526 // Get alternative_services...
515 if (!server_pref_dict.GetDictionaryWithoutPathExpansion( 527 const base::ListValue* alternative_service_list;
516 kAlternateProtocolKey, &alternate_protocol_dict)) { 528 const base::DictionaryValue* alternative_service_dict;
517 return true; 529 AlternativeServiceInfo alternative_service_info;
530 if (server_pref_dict.GetListWithoutPathExpansion(kAlternativeServiceKey,
531 &alternative_service_list)) {
532 if (alternative_service_list->empty()) {
533 return false;
534 }
535 // Get first element of the list.
536 // TODO(bnc): Once we store multiple AlternativeServiceInfo per server, read
537 // all of them.
538 if (!alternative_service_list->GetDictionary(0,
539 &alternative_service_dict)) {
540 return false;
541 }
542 alternative_service_info = ParseAlternativeServiceDict(
543 *alternative_service_dict, server.ToString());
544 } else {
545 // ...or alternate_protocol.
546 // TODO(bnc): Remove this in M46, we do not need preference migration for
547 // long.
548 if (server_pref_dict.GetDictionaryWithoutPathExpansion(
549 kAlternateProtocolKey, &alternative_service_dict)) {
550 alternative_service_info = ParseAlternativeServiceDict(
551 *alternative_service_dict, server.ToString());
552 } else {
553 return true;
554 }
518 } 555 }
519 AlternateProtocolInfo alternate_protocol = 556
520 ParseAlternateProtocolDict(*alternate_protocol_dict, server.ToString()); 557 if (alternative_service_info.alternative_service.protocol ==
521 if (alternate_protocol.protocol == UNINITIALIZED_ALTERNATE_PROTOCOL) 558 UNINITIALIZED_ALTERNATE_PROTOCOL) {
522 return false; 559 return false;
523 alternate_protocol_map->Put(server, alternate_protocol); 560 }
561 alternative_service_map->Put(server, alternative_service_info);
524 return true; 562 return true;
525 } 563 }
526 564
527 bool HttpServerPropertiesManager::ReadSupportsQuic( 565 bool HttpServerPropertiesManager::ReadSupportsQuic(
528 const base::DictionaryValue& http_server_properties_dict, 566 const base::DictionaryValue& http_server_properties_dict,
529 IPAddressNumber* last_quic_address) { 567 IPAddressNumber* last_quic_address) {
530 const base::DictionaryValue* supports_quic_dict = NULL; 568 const base::DictionaryValue* supports_quic_dict = NULL;
531 if (!http_server_properties_dict.GetDictionaryWithoutPathExpansion( 569 if (!http_server_properties_dict.GetDictionaryWithoutPathExpansion(
532 kSupportsQuicKey, &supports_quic_dict)) { 570 kSupportsQuicKey, &supports_quic_dict)) {
533 return true; 571 return true;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 server_network_stats.srtt = base::TimeDelta::FromInternalValue(srtt); 610 server_network_stats.srtt = base::TimeDelta::FromInternalValue(srtt);
573 // TODO(rtenneti): When QUIC starts using bandwidth_estimate, then persist 611 // TODO(rtenneti): When QUIC starts using bandwidth_estimate, then persist
574 // bandwidth_estimate. 612 // bandwidth_estimate.
575 network_stats_map->Put(server, server_network_stats); 613 network_stats_map->Put(server, server_network_stats);
576 return true; 614 return true;
577 } 615 }
578 616
579 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread( 617 void HttpServerPropertiesManager::UpdateCacheFromPrefsOnNetworkThread(
580 StringVector* spdy_servers, 618 StringVector* spdy_servers,
581 SpdySettingsMap* spdy_settings_map, 619 SpdySettingsMap* spdy_settings_map,
582 AlternateProtocolMap* alternate_protocol_map, 620 AlternativeServiceMap* alternative_service_map,
583 IPAddressNumber* last_quic_address, 621 IPAddressNumber* last_quic_address,
584 ServerNetworkStatsMap* server_network_stats_map, 622 ServerNetworkStatsMap* server_network_stats_map,
585 bool detected_corrupted_prefs) { 623 bool detected_corrupted_prefs) {
586 // Preferences have the master data because admins might have pushed new 624 // Preferences have the master data because admins might have pushed new
587 // preferences. Update the cached data with new data from preferences. 625 // preferences. Update the cached data with new data from preferences.
588 DCHECK(network_task_runner_->RunsTasksOnCurrentThread()); 626 DCHECK(network_task_runner_->RunsTasksOnCurrentThread());
589 627
590 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size()); 628 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdyServers", spdy_servers->size());
591 http_server_properties_impl_->InitializeSpdyServers(spdy_servers, true); 629 http_server_properties_impl_->InitializeSpdyServers(spdy_servers, true);
592 630
593 // Update the cached data and use the new spdy_settings from preferences. 631 // Update the cached data and use the new spdy_settings from preferences.
594 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdySettings", spdy_settings_map->size()); 632 UMA_HISTOGRAM_COUNTS("Net.CountOfSpdySettings", spdy_settings_map->size());
595 http_server_properties_impl_->InitializeSpdySettingsServers( 633 http_server_properties_impl_->InitializeSpdySettingsServers(
596 spdy_settings_map); 634 spdy_settings_map);
597 635
598 // Update the cached data and use the new Alternate-Protocol server list from 636 // Update the cached data and use the new Alternate-Protocol server list from
599 // preferences. 637 // preferences.
600 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers", 638 UMA_HISTOGRAM_COUNTS("Net.CountOfAlternateProtocolServers",
601 alternate_protocol_map->size()); 639 alternative_service_map->size());
602 http_server_properties_impl_->InitializeAlternateProtocolServers( 640 http_server_properties_impl_->InitializeAlternativeServiceServers(
603 alternate_protocol_map); 641 alternative_service_map);
604 642
605 http_server_properties_impl_->InitializeSupportsQuic(last_quic_address); 643 http_server_properties_impl_->InitializeSupportsQuic(last_quic_address);
606 644
607 http_server_properties_impl_->InitializeServerNetworkStats( 645 http_server_properties_impl_->InitializeServerNetworkStats(
608 server_network_stats_map); 646 server_network_stats_map);
609 647
610 // Update the prefs with what we have read (delete all corrupted prefs). 648 // Update the prefs with what we have read (delete all corrupted prefs).
611 if (detected_corrupted_prefs) 649 if (detected_corrupted_prefs)
612 ScheduleUpdatePrefsOnNetworkThread(); 650 ScheduleUpdatePrefsOnNetworkThread();
613 } 651 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 new SpdySettingsMap(kMaxSpdySettingsHostsToPersist); 689 new SpdySettingsMap(kMaxSpdySettingsHostsToPersist);
652 const SpdySettingsMap& main_map = 690 const SpdySettingsMap& main_map =
653 http_server_properties_impl_->spdy_settings_map(); 691 http_server_properties_impl_->spdy_settings_map();
654 int count = 0; 692 int count = 0;
655 for (SpdySettingsMap::const_iterator it = main_map.begin(); 693 for (SpdySettingsMap::const_iterator it = main_map.begin();
656 it != main_map.end() && count < kMaxSpdySettingsHostsToPersist; 694 it != main_map.end() && count < kMaxSpdySettingsHostsToPersist;
657 ++it, ++count) { 695 ++it, ++count) {
658 spdy_settings_map->Put(it->first, it->second); 696 spdy_settings_map->Put(it->first, it->second);
659 } 697 }
660 698
661 AlternateProtocolMap* alternate_protocol_map = 699 AlternativeServiceMap* alternative_service_map =
662 new AlternateProtocolMap(kMaxAlternateProtocolHostsToPersist); 700 new AlternativeServiceMap(kMaxAlternateProtocolHostsToPersist);
663 const AlternateProtocolMap& map = 701 const AlternativeServiceMap& map =
664 http_server_properties_impl_->alternate_protocol_map(); 702 http_server_properties_impl_->alternative_service_map();
665 count = 0; 703 count = 0;
666 typedef std::map<std::string, bool> CanonicalHostPersistedMap; 704 typedef std::map<std::string, bool> CanonicalHostPersistedMap;
667 CanonicalHostPersistedMap persisted_map; 705 CanonicalHostPersistedMap persisted_map;
668 for (AlternateProtocolMap::const_iterator it = map.begin(); 706 for (AlternativeServiceMap::const_iterator it = map.begin();
669 it != map.end() && count < kMaxAlternateProtocolHostsToPersist; ++it) { 707 it != map.end() && count < kMaxAlternateProtocolHostsToPersist; ++it) {
670 const AlternateProtocolInfo& alternate_protocol = it->second; 708 const AlternativeServiceInfo& alternative_service_info = it->second;
671 if (!IsAlternateProtocolValid(alternate_protocol.protocol)) { 709 if (!IsAlternateProtocolValid(
710 alternative_service_info.alternative_service.protocol)) {
672 continue; 711 continue;
673 } 712 }
674 const HostPortPair& server = it->first; 713 const HostPortPair& server = it->first;
675 if (IsAlternativeServiceBroken( 714 AlternativeService alternative_service(
676 AlternativeService(alternate_protocol.protocol, server.host(), 715 alternative_service_info.alternative_service);
677 alternate_protocol.port))) { 716 if (alternative_service.host.empty()) {
717 alternative_service.host = server.host();
718 }
719 if (IsAlternativeServiceBroken(alternative_service)) {
678 continue; 720 continue;
679 } 721 }
680 std::string canonical_suffix = 722 std::string canonical_suffix =
681 http_server_properties_impl_->GetCanonicalSuffix(server.host()); 723 http_server_properties_impl_->GetCanonicalSuffix(server.host());
682 if (!canonical_suffix.empty()) { 724 if (!canonical_suffix.empty()) {
683 if (persisted_map.find(canonical_suffix) != persisted_map.end()) 725 if (persisted_map.find(canonical_suffix) != persisted_map.end())
684 continue; 726 continue;
685 persisted_map[canonical_suffix] = true; 727 persisted_map[canonical_suffix] = true;
686 } 728 }
687 alternate_protocol_map->Put(server, alternate_protocol); 729 alternative_service_map->Put(server, alternative_service_info);
688 ++count; 730 ++count;
689 } 731 }
690 732
691 ServerNetworkStatsMap* server_network_stats_map = 733 ServerNetworkStatsMap* server_network_stats_map =
692 new ServerNetworkStatsMap(kMaxServerNetworkStatsHostsToPersist); 734 new ServerNetworkStatsMap(kMaxServerNetworkStatsHostsToPersist);
693 const ServerNetworkStatsMap& main_server_network_stats_map = 735 const ServerNetworkStatsMap& main_server_network_stats_map =
694 http_server_properties_impl_->server_network_stats_map(); 736 http_server_properties_impl_->server_network_stats_map();
695 for (ServerNetworkStatsMap::const_iterator it = 737 for (ServerNetworkStatsMap::const_iterator it =
696 main_server_network_stats_map.begin(); 738 main_server_network_stats_map.begin();
697 it != main_server_network_stats_map.end(); ++it) { 739 it != main_server_network_stats_map.end(); ++it) {
698 server_network_stats_map->Put(it->first, it->second); 740 server_network_stats_map->Put(it->first, it->second);
699 } 741 }
700 742
701 IPAddressNumber* last_quic_addr = new IPAddressNumber; 743 IPAddressNumber* last_quic_addr = new IPAddressNumber;
702 http_server_properties_impl_->GetSupportsQuic(last_quic_addr); 744 http_server_properties_impl_->GetSupportsQuic(last_quic_addr);
703 // Update the preferences on the pref thread. 745 // Update the preferences on the pref thread.
704 pref_task_runner_->PostTask( 746 pref_task_runner_->PostTask(
705 FROM_HERE, 747 FROM_HERE,
706 base::Bind( 748 base::Bind(
707 &HttpServerPropertiesManager::UpdatePrefsOnPrefThread, pref_weak_ptr_, 749 &HttpServerPropertiesManager::UpdatePrefsOnPrefThread, pref_weak_ptr_,
708 base::Owned(spdy_server_list), base::Owned(spdy_settings_map), 750 base::Owned(spdy_server_list), base::Owned(spdy_settings_map),
709 base::Owned(alternate_protocol_map), base::Owned(last_quic_addr), 751 base::Owned(alternative_service_map), base::Owned(last_quic_addr),
710 base::Owned(server_network_stats_map), completion)); 752 base::Owned(server_network_stats_map), completion));
711 } 753 }
712 754
713 // A local or temporary data structure to hold |supports_spdy|, SpdySettings, 755 // A local or temporary data structure to hold |supports_spdy|, SpdySettings,
714 // AlternateProtocolInfo and SupportsQuic preferences for a server. This is used 756 // AlternativeServiceInfo and SupportsQuic preferences for a server. This is
715 // only in UpdatePrefsOnPrefThread. 757 // used only in UpdatePrefsOnPrefThread.
716 struct ServerPref { 758 struct ServerPref {
717 ServerPref() 759 ServerPref()
718 : supports_spdy(false), 760 : supports_spdy(false),
719 settings_map(NULL), 761 settings_map(NULL),
720 alternate_protocol(NULL), 762 alternative_service(NULL),
721 supports_quic(NULL), 763 supports_quic(NULL),
722 server_network_stats(NULL) {} 764 server_network_stats(NULL) {}
723 ServerPref(bool supports_spdy, 765 ServerPref(bool supports_spdy,
724 const SettingsMap* settings_map, 766 const SettingsMap* settings_map,
725 const AlternateProtocolInfo* alternate_protocol, 767 const AlternativeServiceInfo* alternative_service,
726 const SupportsQuic* supports_quic, 768 const SupportsQuic* supports_quic,
727 const ServerNetworkStats* server_network_stats) 769 const ServerNetworkStats* server_network_stats)
728 : supports_spdy(supports_spdy), 770 : supports_spdy(supports_spdy),
729 settings_map(settings_map), 771 settings_map(settings_map),
730 alternate_protocol(alternate_protocol), 772 alternative_service(alternative_service),
731 supports_quic(supports_quic), 773 supports_quic(supports_quic),
732 server_network_stats(server_network_stats) {} 774 server_network_stats(server_network_stats) {}
733 bool supports_spdy; 775 bool supports_spdy;
734 const SettingsMap* settings_map; 776 const SettingsMap* settings_map;
735 const AlternateProtocolInfo* alternate_protocol; 777 const AlternativeServiceInfo* alternative_service;
736 const SupportsQuic* supports_quic; 778 const SupportsQuic* supports_quic;
737 const ServerNetworkStats* server_network_stats; 779 const ServerNetworkStats* server_network_stats;
738 }; 780 };
739 781
740 void HttpServerPropertiesManager::UpdatePrefsOnPrefThread( 782 void HttpServerPropertiesManager::UpdatePrefsOnPrefThread(
741 base::ListValue* spdy_server_list, 783 base::ListValue* spdy_server_list,
742 SpdySettingsMap* spdy_settings_map, 784 SpdySettingsMap* spdy_settings_map,
743 AlternateProtocolMap* alternate_protocol_map, 785 AlternativeServiceMap* alternative_service_map,
744 IPAddressNumber* last_quic_address, 786 IPAddressNumber* last_quic_address,
745 ServerNetworkStatsMap* server_network_stats_map, 787 ServerNetworkStatsMap* server_network_stats_map,
746 const base::Closure& completion) { 788 const base::Closure& completion) {
747 typedef std::map<HostPortPair, ServerPref> ServerPrefMap; 789 typedef std::map<HostPortPair, ServerPref> ServerPrefMap;
748 ServerPrefMap server_pref_map; 790 ServerPrefMap server_pref_map;
749 791
750 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); 792 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread());
751 793
752 // Add servers that support spdy to server_pref_map. 794 // Add servers that support spdy to server_pref_map.
753 std::string s; 795 std::string s;
754 for (base::ListValue::const_iterator list_it = spdy_server_list->begin(); 796 for (base::ListValue::const_iterator list_it = spdy_server_list->begin();
755 list_it != spdy_server_list->end(); 797 list_it != spdy_server_list->end();
756 ++list_it) { 798 ++list_it) {
757 if ((*list_it)->GetAsString(&s)) { 799 if ((*list_it)->GetAsString(&s)) {
758 HostPortPair server = HostPortPair::FromString(s); 800 HostPortPair server = HostPortPair::FromString(s);
759 server_pref_map[server].supports_spdy = true; 801 server_pref_map[server].supports_spdy = true;
760 } 802 }
761 } 803 }
762 804
763 // Add servers that have SpdySettings to server_pref_map. 805 // Add servers that have SpdySettings to server_pref_map.
764 for (SpdySettingsMap::iterator map_it = spdy_settings_map->begin(); 806 for (SpdySettingsMap::iterator map_it = spdy_settings_map->begin();
765 map_it != spdy_settings_map->end(); ++map_it) { 807 map_it != spdy_settings_map->end(); ++map_it) {
766 const HostPortPair& server = map_it->first; 808 const HostPortPair& server = map_it->first;
767 server_pref_map[server].settings_map = &map_it->second; 809 server_pref_map[server].settings_map = &map_it->second;
768 } 810 }
769 811
770 // Add AlternateProtocol servers to server_pref_map. 812 // Add AlternateProtocol servers to server_pref_map.
771 for (AlternateProtocolMap::const_iterator map_it = 813 for (AlternativeServiceMap::const_iterator map_it =
772 alternate_protocol_map->begin(); 814 alternative_service_map->begin();
773 map_it != alternate_protocol_map->end(); ++map_it) { 815 map_it != alternative_service_map->end(); ++map_it) {
774 server_pref_map[map_it->first].alternate_protocol = &map_it->second; 816 server_pref_map[map_it->first].alternative_service = &map_it->second;
775 } 817 }
776 818
777 // Add ServerNetworkStats servers to server_pref_map. 819 // Add ServerNetworkStats servers to server_pref_map.
778 for (ServerNetworkStatsMap::const_iterator map_it = 820 for (ServerNetworkStatsMap::const_iterator map_it =
779 server_network_stats_map->begin(); 821 server_network_stats_map->begin();
780 map_it != server_network_stats_map->end(); ++map_it) { 822 map_it != server_network_stats_map->end(); ++map_it) {
781 const HostPortPair& server = map_it->first; 823 const HostPortPair& server = map_it->first;
782 server_pref_map[server].server_network_stats = &map_it->second; 824 server_pref_map[server].server_network_stats = &map_it->second;
783 } 825 }
784 826
785 // Persist properties to the |path_|. 827 // Persist properties to the |path_|.
786 base::DictionaryValue http_server_properties_dict; 828 base::DictionaryValue http_server_properties_dict;
787 base::DictionaryValue* servers_dict = new base::DictionaryValue; 829 base::DictionaryValue* servers_dict = new base::DictionaryValue;
788 for (ServerPrefMap::const_iterator map_it = server_pref_map.begin(); 830 for (ServerPrefMap::const_iterator map_it = server_pref_map.begin();
789 map_it != server_pref_map.end(); 831 map_it != server_pref_map.end();
790 ++map_it) { 832 ++map_it) {
791 const HostPortPair& server = map_it->first; 833 const HostPortPair& server = map_it->first;
792 const ServerPref& server_pref = map_it->second; 834 const ServerPref& server_pref = map_it->second;
793 835
794 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; 836 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
795 837
796 // Save supports_spdy. 838 // Save supports_spdy.
797 if (server_pref.supports_spdy) 839 if (server_pref.supports_spdy)
798 server_pref_dict->SetBoolean(kSupportsSpdyKey, server_pref.supports_spdy); 840 server_pref_dict->SetBoolean(kSupportsSpdyKey, server_pref.supports_spdy);
799 SaveSpdySettingsToServerPrefs(server_pref.settings_map, server_pref_dict); 841 SaveSpdySettingsToServerPrefs(server_pref.settings_map, server_pref_dict);
800 SaveAlternateProtocolToServerPrefs(server_pref.alternate_protocol, 842 SaveAlternativeServiceToServerPrefs(server_pref.alternative_service,
801 server_pref_dict); 843 server_pref_dict);
802 SaveNetworkStatsToServerPrefs(server_pref.server_network_stats, 844 SaveNetworkStatsToServerPrefs(server_pref.server_network_stats,
803 server_pref_dict); 845 server_pref_dict);
804 846
805 servers_dict->SetWithoutPathExpansion(server.ToString(), server_pref_dict); 847 servers_dict->SetWithoutPathExpansion(server.ToString(), server_pref_dict);
806 } 848 }
807 849
808 http_server_properties_dict.SetWithoutPathExpansion(kServersKey, 850 http_server_properties_dict.SetWithoutPathExpansion(kServersKey,
809 servers_dict); 851 servers_dict);
810 SetVersion(&http_server_properties_dict, kVersionNumber); 852 SetVersion(&http_server_properties_dict, kVersionNumber);
811 853
(...skipping 21 matching lines...) Expand all
833 for (SettingsMap::const_iterator it = settings_map->begin(); 875 for (SettingsMap::const_iterator it = settings_map->begin();
834 it != settings_map->end(); ++it) { 876 it != settings_map->end(); ++it) {
835 SpdySettingsIds id = it->first; 877 SpdySettingsIds id = it->first;
836 uint32 value = it->second.second; 878 uint32 value = it->second.second;
837 std::string key = base::StringPrintf("%u", id); 879 std::string key = base::StringPrintf("%u", id);
838 spdy_settings_dict->SetInteger(key, value); 880 spdy_settings_dict->SetInteger(key, value);
839 } 881 }
840 server_pref_dict->SetWithoutPathExpansion(kSettingsKey, spdy_settings_dict); 882 server_pref_dict->SetWithoutPathExpansion(kSettingsKey, spdy_settings_dict);
841 } 883 }
842 884
843 void HttpServerPropertiesManager::SaveAlternateProtocolToServerPrefs( 885 void HttpServerPropertiesManager::SaveAlternativeServiceToServerPrefs(
844 const AlternateProtocolInfo* port_alternate_protocol, 886 const AlternativeServiceInfo* alternative_service_info,
845 base::DictionaryValue* server_pref_dict) { 887 base::DictionaryValue* server_pref_dict) {
846 if (!port_alternate_protocol) 888 if (!alternative_service_info)
847 return; 889 return;
848 890
849 base::DictionaryValue* port_alternate_protocol_dict = 891 const AlternativeService& alternative_service =
892 alternative_service_info->alternative_service;
893 base::DictionaryValue* alternative_service_info_dict =
850 new base::DictionaryValue; 894 new base::DictionaryValue;
851 port_alternate_protocol_dict->SetInteger(kPortKey, 895 alternative_service_info_dict->SetString(
852 port_alternate_protocol->port); 896 kProtocolKey, AlternateProtocolToString(alternative_service.protocol));
853 const char* protocol_str = 897 if (!alternative_service.host.empty()) {
854 AlternateProtocolToString(port_alternate_protocol->protocol); 898 alternative_service_info_dict->SetString(kHostKey,
855 port_alternate_protocol_dict->SetString(kProtocolKey, protocol_str); 899 alternative_service.host);
856 port_alternate_protocol_dict->SetDouble(kProbabilityKey, 900 }
857 port_alternate_protocol->probability); 901 alternative_service_info_dict->SetInteger(kPortKey, alternative_service.port);
858 server_pref_dict->SetWithoutPathExpansion(kAlternateProtocolKey, 902 alternative_service_info_dict->SetDouble(
859 port_alternate_protocol_dict); 903 kProbabilityKey, alternative_service_info->probability);
904
905 // Create a single element list here.
906 // TODO(bnc): Once we store multiple AlternativeServiceInfo per server, save
907 // all of them.
908 base::ListValue* alternative_service_list = new base::ListValue();
909 alternative_service_list->Append(alternative_service_info_dict);
910 server_pref_dict->SetWithoutPathExpansion(kAlternativeServiceKey,
911 alternative_service_list);
860 } 912 }
861 913
862 void HttpServerPropertiesManager::SaveSupportsQuicToPrefs( 914 void HttpServerPropertiesManager::SaveSupportsQuicToPrefs(
863 const IPAddressNumber* last_quic_address, 915 const IPAddressNumber* last_quic_address,
864 base::DictionaryValue* http_server_properties_dict) { 916 base::DictionaryValue* http_server_properties_dict) {
865 if (!last_quic_address || last_quic_address->empty()) 917 if (!last_quic_address || last_quic_address->empty())
866 return; 918 return;
867 919
868 base::DictionaryValue* supports_quic_dict = new base::DictionaryValue; 920 base::DictionaryValue* supports_quic_dict = new base::DictionaryValue;
869 supports_quic_dict->SetBoolean(kUsedQuicKey, true); 921 supports_quic_dict->SetBoolean(kUsedQuicKey, true);
(...skipping 19 matching lines...) Expand all
889 server_network_stats_dict); 941 server_network_stats_dict);
890 } 942 }
891 943
892 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() { 944 void HttpServerPropertiesManager::OnHttpServerPropertiesChanged() {
893 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread()); 945 DCHECK(pref_task_runner_->RunsTasksOnCurrentThread());
894 if (!setting_prefs_) 946 if (!setting_prefs_)
895 ScheduleUpdateCacheOnPrefThread(); 947 ScheduleUpdateCacheOnPrefThread();
896 } 948 }
897 949
898 } // namespace net 950 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698