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 "chrome/browser/predictors/resource_prefetch_predictor.h" | 5 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
462 first_occurrence(0) {} | 462 first_occurrence(0) {} |
463 | 463 |
464 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary( | 464 ResourcePrefetchPredictor::OriginRequestSummary::OriginRequestSummary( |
465 const OriginRequestSummary& other) = default; | 465 const OriginRequestSummary& other) = default; |
466 | 466 |
467 ResourcePrefetchPredictor::OriginRequestSummary::~OriginRequestSummary() {} | 467 ResourcePrefetchPredictor::OriginRequestSummary::~OriginRequestSummary() {} |
468 | 468 |
469 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() | 469 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() |
470 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), | 470 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), |
471 priority(net::IDLE), | 471 priority(net::IDLE), |
472 response_time(base::TimeTicks()), | |
473 before_first_contentful_paint(false), | |
472 was_cached(false), | 474 was_cached(false), |
473 has_validators(false), | 475 has_validators(false), |
474 always_revalidate(false), | 476 always_revalidate(false), |
475 is_no_store(false), | 477 is_no_store(false), |
476 network_accessed(false) {} | 478 network_accessed(false) {} |
477 | 479 |
478 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary( | 480 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary( |
479 const URLRequestSummary& other) = default; | 481 const URLRequestSummary& other) = default; |
480 | 482 |
481 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() { | 483 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() { |
482 } | 484 } |
483 | 485 |
484 // static | 486 // static |
485 bool ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 487 bool ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
486 const net::URLRequest& request, | 488 const net::URLRequest& request, |
487 URLRequestSummary* summary) { | 489 URLRequestSummary* summary) { |
488 const content::ResourceRequestInfo* request_info = | 490 const content::ResourceRequestInfo* request_info = |
489 content::ResourceRequestInfo::ForRequest(&request); | 491 content::ResourceRequestInfo::ForRequest(&request); |
490 if (!request_info) | 492 if (!request_info) |
491 return false; | 493 return false; |
492 | 494 |
495 summary->response_time = base::TimeTicks::Now(); | |
493 summary->resource_url = request.original_url(); | 496 summary->resource_url = request.original_url(); |
494 summary->request_url = request.url(); | 497 summary->request_url = request.url(); |
495 content::ResourceType resource_type_from_request = | 498 content::ResourceType resource_type_from_request = |
496 request_info->GetResourceType(); | 499 request_info->GetResourceType(); |
497 summary->priority = request.priority(); | 500 summary->priority = request.priority(); |
498 request.GetMimeType(&summary->mime_type); | 501 request.GetMimeType(&summary->mime_type); |
499 summary->was_cached = request.was_cached(); | 502 summary->was_cached = request.was_cached(); |
500 summary->resource_type = | 503 summary->resource_type = |
501 GetResourceType(resource_type_from_request, summary->mime_type); | 504 GetResourceType(resource_type_from_request, summary->mime_type); |
502 | 505 |
503 scoped_refptr<net::HttpResponseHeaders> headers = | 506 scoped_refptr<net::HttpResponseHeaders> headers = |
504 request.response_info().headers; | 507 request.response_info().headers; |
505 if (headers.get()) { | 508 if (headers.get()) { |
506 summary->has_validators = headers->HasValidators(); | 509 summary->has_validators = headers->HasValidators(); |
507 // RFC 2616, section 14.9. | 510 // RFC 2616, section 14.9. |
508 summary->always_revalidate = | 511 summary->always_revalidate = |
509 headers->HasHeaderValue("cache-control", "no-cache") || | 512 headers->HasHeaderValue("cache-control", "no-cache") || |
510 headers->HasHeaderValue("pragma", "no-cache") || | 513 headers->HasHeaderValue("pragma", "no-cache") || |
511 headers->HasHeaderValue("vary", "*"); | 514 headers->HasHeaderValue("vary", "*"); |
512 summary->is_no_store = IsNoStore(request); | 515 summary->is_no_store = IsNoStore(request); |
513 } | 516 } |
514 summary->network_accessed = request.response_info().network_accessed; | 517 summary->network_accessed = request.response_info().network_accessed; |
515 return true; | 518 return true; |
516 } | 519 } |
517 | 520 |
518 ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary( | 521 ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary( |
519 const GURL& i_main_frame_url) | 522 const GURL& i_main_frame_url) |
520 : main_frame_url(i_main_frame_url), initial_url(i_main_frame_url) {} | 523 : main_frame_url(i_main_frame_url), |
524 initial_url(i_main_frame_url), | |
525 first_contentful_paint(base::TimeTicks::Max()) {} | |
521 | 526 |
522 ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary( | 527 ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary( |
523 const PageRequestSummary& other) = default; | 528 const PageRequestSummary& other) = default; |
524 | 529 |
525 ResourcePrefetchPredictor::PageRequestSummary::~PageRequestSummary() {} | 530 ResourcePrefetchPredictor::PageRequestSummary::~PageRequestSummary() {} |
526 | 531 |
527 ResourcePrefetchPredictor::Prediction::Prediction() = default; | 532 ResourcePrefetchPredictor::Prediction::Prediction() = default; |
528 | 533 |
529 ResourcePrefetchPredictor::Prediction::Prediction( | 534 ResourcePrefetchPredictor::Prediction::Prediction( |
530 const ResourcePrefetchPredictor::Prediction& other) = default; | 535 const ResourcePrefetchPredictor::Prediction& other) = default; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
638 // corresponding to the navigation has not been created yet. | 643 // corresponding to the navigation has not been created yet. |
639 if (!navigation_id.main_frame_url.is_empty()) | 644 if (!navigation_id.main_frame_url.is_empty()) |
640 OnNavigationComplete(navigation_id); | 645 OnNavigationComplete(navigation_id); |
641 break; | 646 break; |
642 default: | 647 default: |
643 NOTREACHED() << "Unexpected initialization_state_: " | 648 NOTREACHED() << "Unexpected initialization_state_: " |
644 << initialization_state_; | 649 << initialization_state_; |
645 } | 650 } |
646 } | 651 } |
647 | 652 |
653 void ResourcePrefetchPredictor::RecordFirstContentfulPaint( | |
654 const NavigationID& navigation_id, | |
655 const base::TimeTicks& first_contentful_paint) { | |
alexilin
2017/04/21 13:49:05
Please add following lines to the beginning of the
trevordixon
2017/04/25 12:46:09
Done.
| |
656 NavigationMap::iterator nav_it = inflight_navigations_.find(navigation_id); | |
657 if (nav_it != inflight_navigations_.end()) { | |
alexilin
2017/04/21 13:49:05
very tiny nit:
You could omit curly braces.
trevordixon
2017/04/25 12:46:09
OK.
| |
658 nav_it->second->first_contentful_paint = first_contentful_paint; | |
659 } | |
660 } | |
661 | |
648 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url, | 662 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url, |
649 PrefetchOrigin origin) { | 663 PrefetchOrigin origin) { |
650 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StartPrefetching", "url", | 664 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StartPrefetching", "url", |
651 url.spec()); | 665 url.spec()); |
652 // Save prefetch start time to report prefetching duration. | 666 // Save prefetch start time to report prefetching duration. |
653 if (inflight_prefetches_.find(url) == inflight_prefetches_.end() && | 667 if (inflight_prefetches_.find(url) == inflight_prefetches_.end() && |
654 IsUrlPrefetchable(url)) { | 668 IsUrlPrefetchable(url)) { |
655 inflight_prefetches_.insert(std::make_pair(url, base::TimeTicks::Now())); | 669 inflight_prefetches_.insert(std::make_pair(url, base::TimeTicks::Now())); |
656 } | 670 } |
657 | 671 |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
837 | 851 |
838 NavigationMap::iterator nav_it = | 852 NavigationMap::iterator nav_it = |
839 inflight_navigations_.find(nav_id_without_timing_info); | 853 inflight_navigations_.find(nav_id_without_timing_info); |
840 if (nav_it == inflight_navigations_.end()) | 854 if (nav_it == inflight_navigations_.end()) |
841 return; | 855 return; |
842 | 856 |
843 // Remove the navigation from the inflight navigations. | 857 // Remove the navigation from the inflight navigations. |
844 std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second); | 858 std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second); |
845 inflight_navigations_.erase(nav_it); | 859 inflight_navigations_.erase(nav_it); |
846 | 860 |
861 // Set before_first_contentful paint for each resource. | |
862 for (auto& request_summary : summary->subresource_requests) { | |
863 request_summary.before_first_contentful_paint = | |
864 request_summary.response_time < summary->first_contentful_paint; | |
865 } | |
866 | |
847 const GURL& initial_url = summary->initial_url; | 867 const GURL& initial_url = summary->initial_url; |
848 ResourcePrefetchPredictor::Prediction prediction; | 868 ResourcePrefetchPredictor::Prediction prediction; |
849 bool has_data = GetPrefetchData(initial_url, &prediction); | 869 bool has_data = GetPrefetchData(initial_url, &prediction); |
850 if (has_data) | 870 if (has_data) |
851 ReportPredictionAccuracy(prediction, *summary); | 871 ReportPredictionAccuracy(prediction, *summary); |
852 | 872 |
853 auto it = prefetcher_stats_.find(initial_url); | 873 auto it = prefetcher_stats_.find(initial_url); |
854 if (it != prefetcher_stats_.end()) { | 874 if (it != prefetcher_stats_.end()) { |
855 const std::vector<URLRequestSummary>& summaries = | 875 const std::vector<URLRequestSummary>& summaries = |
856 summary->subresource_requests; | 876 summary->subresource_requests; |
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1412 | 1432 |
1413 // Only need to add new stuff. | 1433 // Only need to add new stuff. |
1414 ResourceData* resource_to_add = data.add_resources(); | 1434 ResourceData* resource_to_add = data.add_resources(); |
1415 resource_to_add->set_resource_url(summary.resource_url.spec()); | 1435 resource_to_add->set_resource_url(summary.resource_url.spec()); |
1416 resource_to_add->set_resource_type( | 1436 resource_to_add->set_resource_type( |
1417 static_cast<ResourceData::ResourceType>(summary.resource_type)); | 1437 static_cast<ResourceData::ResourceType>(summary.resource_type)); |
1418 resource_to_add->set_number_of_hits(1); | 1438 resource_to_add->set_number_of_hits(1); |
1419 resource_to_add->set_average_position(i + 1); | 1439 resource_to_add->set_average_position(i + 1); |
1420 resource_to_add->set_priority( | 1440 resource_to_add->set_priority( |
1421 static_cast<ResourceData::Priority>(summary.priority)); | 1441 static_cast<ResourceData::Priority>(summary.priority)); |
1442 resource_to_add->set_before_first_contentful_paint( | |
1443 summary.before_first_contentful_paint); | |
1422 resource_to_add->set_has_validators(new_resources[i].has_validators); | 1444 resource_to_add->set_has_validators(new_resources[i].has_validators); |
1423 resource_to_add->set_always_revalidate( | 1445 resource_to_add->set_always_revalidate( |
1424 new_resources[i].always_revalidate); | 1446 new_resources[i].always_revalidate); |
1425 | 1447 |
1426 // To ensure we dont add the same url twice. | 1448 // To ensure we dont add the same url twice. |
1427 old_index[summary.resource_url] = 0; | 1449 old_index[summary.resource_url] = 0; |
1428 } | 1450 } |
1429 } | 1451 } |
1430 | 1452 |
1431 PrefetchData& data = cache_entry->second; | 1453 PrefetchData& data = cache_entry->second; |
(...skipping 368 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1800 TestObserver::~TestObserver() { | 1822 TestObserver::~TestObserver() { |
1801 predictor_->SetObserverForTesting(nullptr); | 1823 predictor_->SetObserverForTesting(nullptr); |
1802 } | 1824 } |
1803 | 1825 |
1804 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1826 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
1805 : predictor_(predictor) { | 1827 : predictor_(predictor) { |
1806 predictor_->SetObserverForTesting(this); | 1828 predictor_->SetObserverForTesting(this); |
1807 } | 1829 } |
1808 | 1830 |
1809 } // namespace predictors | 1831 } // namespace predictors |
OLD | NEW |