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 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 void ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(bool state) { | 410 void ResourcePrefetchPredictor::SetAllowPortInUrlsForTesting(bool state) { |
411 g_allow_port_in_urls = state; | 411 g_allow_port_in_urls = state; |
412 } | 412 } |
413 | 413 |
414 //////////////////////////////////////////////////////////////////////////////// | 414 //////////////////////////////////////////////////////////////////////////////// |
415 // ResourcePrefetchPredictor nested types. | 415 // ResourcePrefetchPredictor nested types. |
416 | 416 |
417 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() | 417 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary() |
418 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), | 418 : resource_type(content::RESOURCE_TYPE_LAST_TYPE), |
419 priority(net::IDLE), | 419 priority(net::IDLE), |
420 creation_time(base::TimeTicks()), | |
421 before_first_contentful_paint(false), | |
420 was_cached(false), | 422 was_cached(false), |
421 has_validators(false), | 423 has_validators(false), |
422 always_revalidate(false) {} | 424 always_revalidate(false) {} |
423 | 425 |
424 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary( | 426 ResourcePrefetchPredictor::URLRequestSummary::URLRequestSummary( |
425 const URLRequestSummary& other) | 427 const URLRequestSummary& other) |
426 : navigation_id(other.navigation_id), | 428 : navigation_id(other.navigation_id), |
427 resource_url(other.resource_url), | 429 resource_url(other.resource_url), |
428 resource_type(other.resource_type), | 430 resource_type(other.resource_type), |
429 priority(other.priority), | 431 priority(other.priority), |
432 creation_time(other.creation_time), | |
433 before_first_contentful_paint(other.before_first_contentful_paint), | |
430 mime_type(other.mime_type), | 434 mime_type(other.mime_type), |
431 was_cached(other.was_cached), | 435 was_cached(other.was_cached), |
432 redirect_url(other.redirect_url), | 436 redirect_url(other.redirect_url), |
433 has_validators(other.has_validators), | 437 has_validators(other.has_validators), |
434 always_revalidate(other.always_revalidate) {} | 438 always_revalidate(other.always_revalidate) {} |
435 | 439 |
436 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() { | 440 ResourcePrefetchPredictor::URLRequestSummary::~URLRequestSummary() { |
437 } | 441 } |
438 | 442 |
439 // static | 443 // static |
440 bool ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( | 444 bool ResourcePrefetchPredictor::URLRequestSummary::SummarizeResponse( |
441 const net::URLRequest& request, | 445 const net::URLRequest& request, |
442 URLRequestSummary* summary) { | 446 URLRequestSummary* summary) { |
443 const content::ResourceRequestInfo* request_info = | 447 const content::ResourceRequestInfo* request_info = |
444 content::ResourceRequestInfo::ForRequest(&request); | 448 content::ResourceRequestInfo::ForRequest(&request); |
445 if (!request_info) | 449 if (!request_info) |
446 return false; | 450 return false; |
447 | 451 |
452 summary->creation_time = request.creation_time(); | |
448 summary->resource_url = request.original_url(); | 453 summary->resource_url = request.original_url(); |
449 content::ResourceType resource_type_from_request = | 454 content::ResourceType resource_type_from_request = |
450 request_info->GetResourceType(); | 455 request_info->GetResourceType(); |
451 summary->priority = request.priority(); | 456 summary->priority = request.priority(); |
452 request.GetMimeType(&summary->mime_type); | 457 request.GetMimeType(&summary->mime_type); |
453 summary->was_cached = request.was_cached(); | 458 summary->was_cached = request.was_cached(); |
454 summary->resource_type = | 459 summary->resource_type = |
455 GetResourceType(resource_type_from_request, summary->mime_type); | 460 GetResourceType(resource_type_from_request, summary->mime_type); |
456 | 461 |
457 scoped_refptr<net::HttpResponseHeaders> headers = | 462 scoped_refptr<net::HttpResponseHeaders> headers = |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
584 // corresponding to the navigation has not been created yet. | 589 // corresponding to the navigation has not been created yet. |
585 if (!navigation_id.main_frame_url.is_empty()) | 590 if (!navigation_id.main_frame_url.is_empty()) |
586 OnNavigationComplete(navigation_id); | 591 OnNavigationComplete(navigation_id); |
587 break; | 592 break; |
588 default: | 593 default: |
589 NOTREACHED() << "Unexpected initialization_state_: " | 594 NOTREACHED() << "Unexpected initialization_state_: " |
590 << initialization_state_; | 595 << initialization_state_; |
591 } | 596 } |
592 } | 597 } |
593 | 598 |
599 void ResourcePrefetchPredictor::RecordFirstContentfulPaint( | |
600 const NavigationID& navigation_id, | |
601 const base::TimeDelta& first_contentful_paint) { | |
602 NavigationMap::iterator nav_it = | |
603 inflight_navigations_.find(navigation_id); | |
604 if (nav_it == inflight_navigations_.end()) | |
605 return; | |
606 | |
607 base::TimeTicks fcp = nav_it->first.creation_time + first_contentful_paint; | |
608 for (auto& request_summary : nav_it->second->subresource_requests) { | |
609 request_summary.before_first_contentful_paint = | |
610 request_summary.creation_time < fcp; | |
611 } | |
612 } | |
613 | |
614 | |
594 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url, | 615 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url, |
595 PrefetchOrigin origin) { | 616 PrefetchOrigin origin) { |
596 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StartPrefetching", "url", | 617 TRACE_EVENT1("browser", "ResourcePrefetchPredictor::StartPrefetching", "url", |
597 url.spec()); | 618 url.spec()); |
598 // Save prefetch start time to report prefetching duration. | 619 // Save prefetch start time to report prefetching duration. |
599 if (inflight_prefetches_.find(url) == inflight_prefetches_.end() && | 620 if (inflight_prefetches_.find(url) == inflight_prefetches_.end() && |
600 IsUrlPrefetchable(url)) { | 621 IsUrlPrefetchable(url)) { |
601 inflight_prefetches_.insert(std::make_pair(url, base::TimeTicks::Now())); | 622 inflight_prefetches_.insert(std::make_pair(url, base::TimeTicks::Now())); |
602 } | 623 } |
603 | 624 |
(...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1172 data.set_primary_key(key); | 1193 data.set_primary_key(key); |
1173 data.set_last_visit_time(base::Time::Now().ToInternalValue()); | 1194 data.set_last_visit_time(base::Time::Now().ToInternalValue()); |
1174 size_t new_resources_size = new_resources.size(); | 1195 size_t new_resources_size = new_resources.size(); |
1175 std::set<GURL> resources_seen; | 1196 std::set<GURL> resources_seen; |
1176 for (size_t i = 0; i < new_resources_size; ++i) { | 1197 for (size_t i = 0; i < new_resources_size; ++i) { |
1177 const URLRequestSummary& summary = new_resources[i]; | 1198 const URLRequestSummary& summary = new_resources[i]; |
1178 if (resources_seen.find(summary.resource_url) != resources_seen.end()) | 1199 if (resources_seen.find(summary.resource_url) != resources_seen.end()) |
1179 continue; | 1200 continue; |
1180 | 1201 |
1181 ResourceData* resource_to_add = data.add_resources(); | 1202 ResourceData* resource_to_add = data.add_resources(); |
1203 resource_to_add->set_before_first_contentful_paint( | |
1204 summary.before_first_contentful_paint); | |
1182 resource_to_add->set_resource_url(summary.resource_url.spec()); | 1205 resource_to_add->set_resource_url(summary.resource_url.spec()); |
1183 resource_to_add->set_resource_type( | 1206 resource_to_add->set_resource_type( |
1184 static_cast<ResourceData::ResourceType>(summary.resource_type)); | 1207 static_cast<ResourceData::ResourceType>(summary.resource_type)); |
1185 resource_to_add->set_number_of_hits(1); | 1208 resource_to_add->set_number_of_hits(1); |
1186 resource_to_add->set_average_position(i + 1); | 1209 resource_to_add->set_average_position(i + 1); |
1187 resource_to_add->set_priority( | 1210 resource_to_add->set_priority( |
1188 static_cast<ResourceData::Priority>(summary.priority)); | 1211 static_cast<ResourceData::Priority>(summary.priority)); |
1189 resource_to_add->set_has_validators(summary.has_validators); | 1212 resource_to_add->set_has_validators(summary.has_validators); |
1190 resource_to_add->set_always_revalidate(summary.always_revalidate); | 1213 resource_to_add->set_always_revalidate(summary.always_revalidate); |
1191 | 1214 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1228 | 1251 |
1229 // Update the resource type since it could have changed. | 1252 // Update the resource type since it could have changed. |
1230 if (new_summary.resource_type != content::RESOURCE_TYPE_LAST_TYPE) { | 1253 if (new_summary.resource_type != content::RESOURCE_TYPE_LAST_TYPE) { |
1231 old_resource->set_resource_type( | 1254 old_resource->set_resource_type( |
1232 static_cast<ResourceData::ResourceType>( | 1255 static_cast<ResourceData::ResourceType>( |
1233 new_summary.resource_type)); | 1256 new_summary.resource_type)); |
1234 } | 1257 } |
1235 | 1258 |
1236 old_resource->set_priority( | 1259 old_resource->set_priority( |
1237 static_cast<ResourceData::Priority>(new_summary.priority)); | 1260 static_cast<ResourceData::Priority>(new_summary.priority)); |
1238 | 1261 |
alexilin
2017/03/27 15:31:59
Need to update before_first_contentful_paint prope
trevordixon
2017/03/28 11:10:41
Done.
| |
1239 int position = new_index[resource_url] + 1; | 1262 int position = new_index[resource_url] + 1; |
1240 int total = | 1263 int total = |
1241 old_resource->number_of_hits() + old_resource->number_of_misses(); | 1264 old_resource->number_of_hits() + old_resource->number_of_misses(); |
1242 old_resource->set_average_position( | 1265 old_resource->set_average_position( |
1243 ((old_resource->average_position() * total) + position) / | 1266 ((old_resource->average_position() * total) + position) / |
1244 (total + 1)); | 1267 (total + 1)); |
1245 old_resource->set_number_of_hits(old_resource->number_of_hits() + 1); | 1268 old_resource->set_number_of_hits(old_resource->number_of_hits() + 1); |
1246 old_resource->set_consecutive_misses(0); | 1269 old_resource->set_consecutive_misses(0); |
1247 } | 1270 } |
1248 } | 1271 } |
1249 | 1272 |
1250 // Add the new ones that we have not seen before. | 1273 // Add the new ones that we have not seen before. |
1251 for (int i = 0; i < new_resources_size; ++i) { | 1274 for (int i = 0; i < new_resources_size; ++i) { |
1252 const URLRequestSummary& summary = new_resources[i]; | 1275 const URLRequestSummary& summary = new_resources[i]; |
1253 if (old_index.find(summary.resource_url) != old_index.end()) | 1276 if (old_index.find(summary.resource_url) != old_index.end()) |
1254 continue; | 1277 continue; |
1255 | 1278 |
1256 // Only need to add new stuff. | 1279 // Only need to add new stuff. |
1257 ResourceData* resource_to_add = data.add_resources(); | 1280 ResourceData* resource_to_add = data.add_resources(); |
alexilin
2017/03/27 15:31:59
Need to set_before_first_contentful_paint() here t
trevordixon
2017/03/28 11:10:41
Done.
| |
1258 resource_to_add->set_resource_url(summary.resource_url.spec()); | 1281 resource_to_add->set_resource_url(summary.resource_url.spec()); |
1259 resource_to_add->set_resource_type( | 1282 resource_to_add->set_resource_type( |
1260 static_cast<ResourceData::ResourceType>(summary.resource_type)); | 1283 static_cast<ResourceData::ResourceType>(summary.resource_type)); |
1261 resource_to_add->set_number_of_hits(1); | 1284 resource_to_add->set_number_of_hits(1); |
1262 resource_to_add->set_average_position(i + 1); | 1285 resource_to_add->set_average_position(i + 1); |
1263 resource_to_add->set_priority( | 1286 resource_to_add->set_priority( |
1264 static_cast<ResourceData::Priority>(summary.priority)); | 1287 static_cast<ResourceData::Priority>(summary.priority)); |
1265 resource_to_add->set_has_validators(new_resources[i].has_validators); | 1288 resource_to_add->set_has_validators(new_resources[i].has_validators); |
1266 resource_to_add->set_always_revalidate( | 1289 resource_to_add->set_always_revalidate( |
1267 new_resources[i].always_revalidate); | 1290 new_resources[i].always_revalidate); |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1493 TestObserver::~TestObserver() { | 1516 TestObserver::~TestObserver() { |
1494 predictor_->SetObserverForTesting(nullptr); | 1517 predictor_->SetObserverForTesting(nullptr); |
1495 } | 1518 } |
1496 | 1519 |
1497 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) | 1520 TestObserver::TestObserver(ResourcePrefetchPredictor* predictor) |
1498 : predictor_(predictor) { | 1521 : predictor_(predictor) { |
1499 predictor_->SetObserverForTesting(this); | 1522 predictor_->SetObserverForTesting(this); |
1500 } | 1523 } |
1501 | 1524 |
1502 } // namespace predictors | 1525 } // namespace predictors |
OLD | NEW |