Chromium Code Reviews| 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 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 412 if (initialization_state_ != INITIALIZED) | 412 if (initialization_state_ != INITIALIZED) |
| 413 return; | 413 return; |
| 414 | 414 |
| 415 StopPrefetching(response.navigation_id.main_frame_url); | 415 StopPrefetching(response.navigation_id.main_frame_url); |
| 416 } | 416 } |
| 417 | 417 |
| 418 void ResourcePrefetchPredictor::OnMainFrameRedirect( | 418 void ResourcePrefetchPredictor::OnMainFrameRedirect( |
| 419 const URLRequestSummary& response) { | 419 const URLRequestSummary& response) { |
| 420 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 420 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 421 | 421 |
| 422 // Stop any inflight prefetching. Remove the older navigation. | |
| 423 StopPrefetching(response.navigation_id.main_frame_url); | |
| 424 | |
| 425 std::unique_ptr<PageRequestSummary> summary; | 422 std::unique_ptr<PageRequestSummary> summary; |
| 426 NavigationMap::iterator nav_it = | 423 NavigationMap::iterator nav_it = |
| 427 inflight_navigations_.find(response.navigation_id); | 424 inflight_navigations_.find(response.navigation_id); |
| 428 if (nav_it != inflight_navigations_.end()) { | 425 if (nav_it != inflight_navigations_.end()) { |
| 429 summary.reset(nav_it->second.release()); | 426 summary.reset(nav_it->second.release()); |
| 430 inflight_navigations_.erase(nav_it); | 427 inflight_navigations_.erase(nav_it); |
| 431 } | 428 } |
| 432 | 429 |
| 433 // The redirect url may be empty if the URL was invalid. | 430 // The redirect url may be empty if the URL was invalid. |
| 434 if (response.redirect_url.is_empty()) | 431 if (response.redirect_url.is_empty()) |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 navigation_id, std::move(summary), | 483 navigation_id, std::move(summary), |
| 487 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup, | 484 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup, |
| 488 AsWeakPtr()))), | 485 AsWeakPtr()))), |
| 489 &history_lookup_consumer_); | 486 &history_lookup_consumer_); |
| 490 } | 487 } |
| 491 | 488 |
| 492 bool ResourcePrefetchPredictor::GetPrefetchData(const GURL& main_frame_url, | 489 bool ResourcePrefetchPredictor::GetPrefetchData(const GURL& main_frame_url, |
| 493 std::vector<GURL>* urls) { | 490 std::vector<GURL>* urls) { |
| 494 DCHECK(urls); | 491 DCHECK(urls); |
| 495 | 492 |
| 493 if (GetURLPrefetchData(main_frame_url.spec(), urls)) | |
| 494 return true; | |
| 495 | |
| 496 if (GetHostPrefetchData(main_frame_url.host(), urls)) | |
| 497 return true; | |
| 498 | |
| 499 std::string final_redirect; | |
| 500 if (GetFinalRedirect(main_frame_url.spec(), url_redirect_table_cache_.get(), | |
| 501 &final_redirect)) { | |
| 502 GURL redirect_url(final_redirect); | |
| 503 | |
| 504 if (GetURLPrefetchData(redirect_url.spec(), urls)) | |
| 505 return true; | |
| 506 | |
| 507 if (GetHostPrefetchData(redirect_url.host(), urls)) | |
| 508 return true; | |
| 509 } | |
| 510 | |
| 511 return GetFinalRedirect(main_frame_url.host(), | |
| 512 host_redirect_table_cache_.get(), &final_redirect) && | |
| 513 GetHostPrefetchData(final_redirect, urls); | |
| 514 } | |
| 515 | |
| 516 bool ResourcePrefetchPredictor::GetURLPrefetchData( | |
| 517 const std::string& main_frame_url, | |
| 518 std::vector<GURL>* urls) { | |
| 496 bool use_url_data = config_.IsPrefetchingEnabled(profile_) ? | 519 bool use_url_data = config_.IsPrefetchingEnabled(profile_) ? |
| 497 config_.IsURLPrefetchingEnabled(profile_) : | 520 config_.IsURLPrefetchingEnabled(profile_) : |
| 498 config_.IsURLLearningEnabled(); | 521 config_.IsURLLearningEnabled(); |
| 499 if (use_url_data) { | 522 if (use_url_data) { |
| 500 PrefetchDataMap::const_iterator iterator = | 523 PrefetchDataMap::const_iterator iterator = |
| 501 url_table_cache_->find(main_frame_url.spec()); | 524 url_table_cache_->find(main_frame_url); |
| 502 if (iterator != url_table_cache_->end()) | 525 if (iterator != url_table_cache_->end()) |
| 503 PopulatePrefetcherRequest(iterator->second, urls); | 526 PopulatePrefetcherRequest(iterator->second, urls); |
| 504 } | 527 } |
| 505 if (!urls->empty()) | 528 return !urls->empty(); |
| 506 return true; | 529 } |
| 507 | 530 |
| 531 bool ResourcePrefetchPredictor::GetHostPrefetchData( | |
| 532 const std::string& main_frame_host, | |
| 533 std::vector<GURL>* urls) { | |
| 508 bool use_host_data = config_.IsPrefetchingEnabled(profile_) ? | 534 bool use_host_data = config_.IsPrefetchingEnabled(profile_) ? |
| 509 config_.IsHostPrefetchingEnabled(profile_) : | 535 config_.IsHostPrefetchingEnabled(profile_) : |
| 510 config_.IsHostLearningEnabled(); | 536 config_.IsHostLearningEnabled(); |
| 511 if (use_host_data) { | 537 if (use_host_data) { |
| 512 PrefetchDataMap::const_iterator iterator = | 538 PrefetchDataMap::const_iterator iterator = |
| 513 host_table_cache_->find(main_frame_url.host()); | 539 host_table_cache_->find(main_frame_host); |
| 514 if (iterator != host_table_cache_->end()) | 540 if (iterator != host_table_cache_->end()) |
| 515 PopulatePrefetcherRequest(iterator->second, urls); | 541 PopulatePrefetcherRequest(iterator->second, urls); |
| 516 } | 542 } |
| 517 | |
| 518 return !urls->empty(); | 543 return !urls->empty(); |
| 519 } | 544 } |
| 520 | 545 |
| 521 void ResourcePrefetchPredictor::PopulatePrefetcherRequest( | 546 void ResourcePrefetchPredictor::PopulatePrefetcherRequest( |
| 522 const PrefetchData& data, | 547 const PrefetchData& data, |
| 523 std::vector<GURL>* urls) { | 548 std::vector<GURL>* urls) { |
| 524 for (const ResourceData& resource : data.resources()) { | 549 for (const ResourceData& resource : data.resources()) { |
| 525 float confidence = | 550 float confidence = |
| 526 static_cast<float>(resource.number_of_hits()) / | 551 static_cast<float>(resource.number_of_hits()) / |
| 527 (resource.number_of_hits() + resource.number_of_misses()); | 552 (resource.number_of_hits() + resource.number_of_misses()); |
| 528 if (confidence < config_.min_resource_confidence_to_trigger_prefetch || | 553 if (confidence < config_.min_resource_confidence_to_trigger_prefetch || |
| 529 resource.number_of_hits() < | 554 resource.number_of_hits() < |
| 530 config_.min_resource_hits_to_trigger_prefetch) { | 555 config_.min_resource_hits_to_trigger_prefetch) { |
| 531 continue; | 556 continue; |
| 532 } | 557 } |
| 533 | 558 |
| 534 urls->push_back(GURL(resource.resource_url())); | 559 urls->push_back(GURL(resource.resource_url())); |
| 535 } | 560 } |
| 536 } | 561 } |
| 537 | 562 |
| 563 bool ResourcePrefetchPredictor::GetFinalRedirect( | |
|
Benoit L
2016/10/07 09:52:18
The scoring code is duplicated, and it even seems
alexilin
2016/10/07 10:49:07
No, I'm not walking all the redirects here, where
alexilin
2016/10/07 13:09:29
Scan all the redirects here instead of sort while
Benoit L
2016/10/07 13:55:15
Sorry, wasn't clear enough. Thanks for reading bet
| |
| 564 const std::string& first_redirect, | |
| 565 RedirectDataMap* redirect_data_map, | |
| 566 std::string* final_redirect) { | |
| 567 DCHECK(final_redirect); | |
| 568 | |
| 569 RedirectDataMap::const_iterator iterator = | |
| 570 redirect_data_map->find(first_redirect); | |
| 571 if (iterator == redirect_data_map->end()) | |
| 572 return false; | |
| 573 | |
| 574 DCHECK(iterator->second.redirect_endpoints_size() > 0); | |
| 575 const RedirectStat& best_redirect = iterator->second.redirect_endpoints(0); | |
| 576 float confidence = | |
| 577 (best_redirect.number_of_hits() + 0.0) / | |
| 578 (best_redirect.number_of_hits() + best_redirect.number_of_misses()); | |
| 579 | |
| 580 const float kMinRedirectConfidenceToTriggerPrefetch = 0.7f; | |
| 581 const int kMinRedirectHitsToTriggerPrefetch = 2; | |
| 582 | |
| 583 if (confidence < kMinRedirectConfidenceToTriggerPrefetch || | |
| 584 best_redirect.number_of_hits() < kMinRedirectHitsToTriggerPrefetch) | |
| 585 return false; | |
| 586 | |
| 587 *final_redirect = best_redirect.url(); | |
| 588 return true; | |
| 589 } | |
| 590 | |
| 538 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url) { | 591 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url) { |
| 539 if (!prefetch_manager_.get()) // Prefetching not enabled. | 592 if (!prefetch_manager_.get()) // Prefetching not enabled. |
| 540 return; | 593 return; |
| 541 | 594 |
| 542 std::vector<GURL> subresource_urls; | 595 std::vector<GURL> subresource_urls; |
| 543 if (!GetPrefetchData(url, &subresource_urls)) { | 596 if (!GetPrefetchData(url, &subresource_urls)) { |
| 544 // No prefetching data at host or URL level. | 597 // No prefetching data at host or URL level. |
| 545 return; | 598 return; |
| 546 } | 599 } |
| 547 | 600 |
| (...skipping 520 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1068 // HistoryService is already loaded. Continue with Initialization. | 1121 // HistoryService is already loaded. Continue with Initialization. |
| 1069 OnHistoryAndCacheLoaded(); | 1122 OnHistoryAndCacheLoaded(); |
| 1070 return; | 1123 return; |
| 1071 } | 1124 } |
| 1072 DCHECK(!history_service_observer_.IsObserving(history_service)); | 1125 DCHECK(!history_service_observer_.IsObserving(history_service)); |
| 1073 history_service_observer_.Add(history_service); | 1126 history_service_observer_.Add(history_service); |
| 1074 return; | 1127 return; |
| 1075 } | 1128 } |
| 1076 | 1129 |
| 1077 } // namespace predictors | 1130 } // namespace predictors |
| OLD | NEW |