 Chromium Code Reviews
 Chromium Code Reviews Issue 2755093002:
  predictors: Mark before_first_contentful_paint for resources fetched before fcp.  (Closed)
    
  
    Issue 2755093002:
  predictors: Mark before_first_contentful_paint for resources fetched before fcp.  (Closed) 
  | Index: chrome/browser/predictors/resource_prefetch_predictor.cc | 
| diff --git a/chrome/browser/predictors/resource_prefetch_predictor.cc b/chrome/browser/predictors/resource_prefetch_predictor.cc | 
| index 58d01b5df36a23eabb6ebf86be8c50d4a700047b..a101b6e38098d37c532bb0d76fc85054563e5375 100644 | 
| --- a/chrome/browser/predictors/resource_prefetch_predictor.cc | 
| +++ b/chrome/browser/predictors/resource_prefetch_predictor.cc | 
| @@ -469,6 +469,8 @@ ResourcePrefetchPredictor::OriginRequestSummary::~OriginRequestSummary() {} | 
| ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() | 
| : resource_type(content::RESOURCE_TYPE_LAST_TYPE), | 
| priority(net::IDLE), | 
| + response_time(base::TimeTicks()), | 
| + before_first_contentful_paint(false), | 
| was_cached(false), | 
| has_validators(false), | 
| always_revalidate(false), | 
| @@ -490,6 +492,7 @@ bool ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 
| if (!request_info) | 
| return false; | 
| + summary->response_time = base::TimeTicks::Now(); | 
| summary->resource_url = request.original_url(); | 
| summary->request_url = request.url(); | 
| content::ResourceType resource_type_from_request = | 
| @@ -517,7 +520,9 @@ bool ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 
| ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary( | 
| const GURL& i_main_frame_url) | 
| - : main_frame_url(i_main_frame_url), initial_url(i_main_frame_url) {} | 
| + : main_frame_url(i_main_frame_url), | 
| + initial_url(i_main_frame_url), | 
| + first_contentful_paint(base::TimeTicks::Max()) {} | 
| ResourcePrefetchPredictor::PageRequestSummary::PageRequestSummary( | 
| const PageRequestSummary& other) = default; | 
| @@ -645,6 +650,18 @@ void ResourcePrefetchPredictor::RecordMainFrameLoadComplete( | 
| } | 
| } | 
| +void ResourcePrefetchPredictor::RecordFirstContentfulPaint( | 
| + const NavigationID& navigation_id, | 
| + const base::TimeTicks& first_contentful_paint) { | 
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); | 
| + if (initialization_state_ != INITIALIZED) | 
| + return; | 
| + | 
| + NavigationMap::iterator nav_it = inflight_navigations_.find(navigation_id); | 
| + if (nav_it != inflight_navigations_.end()) | 
| + nav_it->second->first_contentful_paint = first_contentful_paint; | 
| +} | 
| + | 
| void ResourcePrefetchPredictor::StartPrefetching(const GURL& url, | 
| PrefetchOrigin origin) { | 
| TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StartPrefetching", "url", | 
| @@ -844,6 +861,12 @@ void ResourcePrefetchPredictor::OnNavigationComplete( | 
| std::unique_ptr<PageRequestSummary> summary = std::move(nav_it->second); | 
| inflight_navigations_.erase(nav_it); | 
| + // Set before_first_contentful paint for each resource. | 
| 
Benoit L
2017/04/25 15:30:36
If I understand the code correctly, this will slig
 
trevordixon
2017/04/25 20:23:55
OK, documented above where response_time is record
 | 
| + for (auto& request_summary : summary->subresource_requests) { | 
| + request_summary.before_first_contentful_paint = | 
| + request_summary.response_time < summary->first_contentful_paint; | 
| + } | 
| + | 
| const GURL& initial_url = summary->initial_url; | 
| ResourcePrefetchPredictor::Prediction prediction; | 
| bool has_data = GetPrefetchData(initial_url, &prediction); | 
| @@ -1343,6 +1366,8 @@ void ResourcePrefetchPredictor::LearnNavigation( | 
| resource_to_add->set_average_position(i + 1); | 
| resource_to_add->set_priority( | 
| static_cast<ResourceData::Priority>(summary.priority)); | 
| + resource_to_add->set_before_first_contentful_paint( | 
| + summary.before_first_contentful_paint); | 
| resource_to_add->set_has_validators(summary.has_validators); | 
| resource_to_add->set_always_revalidate(summary.always_revalidate); | 
| @@ -1392,6 +1417,8 @@ void ResourcePrefetchPredictor::LearnNavigation( | 
| old_resource->set_priority( | 
| static_cast<ResourceData::Priority>(new_summary.priority)); | 
| + old_resource->set_before_first_contentful_paint( | 
| + new_summary.before_first_contentful_paint); | 
| int position = new_index[resource_url] + 1; | 
| int total = | 
| @@ -1419,6 +1446,8 @@ void ResourcePrefetchPredictor::LearnNavigation( | 
| resource_to_add->set_average_position(i + 1); | 
| resource_to_add->set_priority( | 
| static_cast<ResourceData::Priority>(summary.priority)); | 
| + resource_to_add->set_before_first_contentful_paint( | 
| + summary.before_first_contentful_paint); | 
| resource_to_add->set_has_validators(new_resources[i].has_validators); | 
| resource_to_add->set_always_revalidate( | 
| new_resources[i].always_revalidate); |