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 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 prefetch_manager_ = NULL; | 401 prefetch_manager_ = NULL; |
| 402 } | 402 } |
| 403 history_service_observer_.RemoveAll(); | 403 history_service_observer_.RemoveAll(); |
| 404 } | 404 } |
| 405 | 405 |
| 406 void ResourcePrefetchPredictor::OnMainFrameRequest( | 406 void ResourcePrefetchPredictor::OnMainFrameRequest( |
| 407 const URLRequestSummary& request) { | 407 const URLRequestSummary& request) { |
| 408 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 408 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 409 DCHECK_EQ(INITIALIZED, initialization_state_); | 409 DCHECK_EQ(INITIALIZED, initialization_state_); |
| 410 | 410 |
| 411 StartPrefetching(request.navigation_id); | 411 StartPrefetching(request.navigation_id.main_frame_url); |
| 412 | 412 |
| 413 // Cleanup older navigations. | 413 // Cleanup older navigations. |
| 414 CleanupAbandonedNavigations(request.navigation_id); | 414 CleanupAbandonedNavigations(request.navigation_id); |
| 415 | 415 |
| 416 // New empty navigation entry. | 416 // New empty navigation entry. |
| 417 inflight_navigations_.insert(std::make_pair( | 417 inflight_navigations_.insert(std::make_pair( |
| 418 request.navigation_id, | 418 request.navigation_id, |
| 419 make_linked_ptr(new std::vector<URLRequestSummary>()))); | 419 make_linked_ptr(new std::vector<URLRequestSummary>()))); |
| 420 } | 420 } |
| 421 | 421 |
| 422 void ResourcePrefetchPredictor::OnMainFrameResponse( | 422 void ResourcePrefetchPredictor::OnMainFrameResponse( |
| 423 const URLRequestSummary& response) { | 423 const URLRequestSummary& response) { |
| 424 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 424 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 425 if (initialization_state_ != INITIALIZED) | 425 if (initialization_state_ != INITIALIZED) |
| 426 return; | 426 return; |
| 427 | 427 |
| 428 StopPrefetching(response.navigation_id); | 428 StopPrefetching(response.navigation_id.main_frame_url); |
| 429 } | 429 } |
| 430 | 430 |
| 431 void ResourcePrefetchPredictor::OnMainFrameRedirect( | 431 void ResourcePrefetchPredictor::OnMainFrameRedirect( |
| 432 const URLRequestSummary& response) { | 432 const URLRequestSummary& response) { |
| 433 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 433 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 434 | 434 |
| 435 // TODO(shishir): There are significant gains to be had here if we can use the | 435 // TODO(shishir): There are significant gains to be had here if we can use the |
| 436 // start URL in a redirect chain as the key to start prefetching. We can save | 436 // start URL in a redirect chain as the key to start prefetching. We can save |
| 437 // of redirect times considerably assuming that the redirect chains do not | 437 // of redirect times considerably assuming that the redirect chains do not |
| 438 // change. | 438 // change. |
| 439 | 439 |
| 440 // Stop any inflight prefetching. Remove the older navigation. | 440 // Stop any inflight prefetching. Remove the older navigation. |
| 441 StopPrefetching(response.navigation_id); | 441 StopPrefetching(response.navigation_id.main_frame_url); |
| 442 inflight_navigations_.erase(response.navigation_id); | 442 inflight_navigations_.erase(response.navigation_id); |
| 443 | 443 |
| 444 // A redirect will not lead to another OnMainFrameRequest call, so record the | 444 // A redirect will not lead to another OnMainFrameRequest call, so record the |
| 445 // redirect url as a new navigation. | 445 // redirect url as a new navigation. |
| 446 | 446 |
| 447 // The redirect url may be empty if the URL was invalid. | 447 // The redirect url may be empty if the URL was invalid. |
| 448 if (response.redirect_url.is_empty()) | 448 if (response.redirect_url.is_empty()) |
| 449 return; | 449 return; |
| 450 | 450 |
| 451 NavigationID navigation_id(response.navigation_id); | 451 NavigationID navigation_id(response.navigation_id); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 489 ServiceAccessType::EXPLICIT_ACCESS); | 489 ServiceAccessType::EXPLICIT_ACCESS); |
| 490 DCHECK(history_service); | 490 DCHECK(history_service); |
| 491 history_service->ScheduleDBTask( | 491 history_service->ScheduleDBTask( |
| 492 std::unique_ptr<history::HistoryDBTask>(new GetUrlVisitCountTask( | 492 std::unique_ptr<history::HistoryDBTask>(new GetUrlVisitCountTask( |
| 493 navigation_id, requests, | 493 navigation_id, requests, |
| 494 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup, | 494 base::Bind(&ResourcePrefetchPredictor::OnVisitCountLookup, |
| 495 AsWeakPtr()))), | 495 AsWeakPtr()))), |
| 496 &history_lookup_consumer_); | 496 &history_lookup_consumer_); |
| 497 } | 497 } |
| 498 | 498 |
| 499 bool ResourcePrefetchPredictor::GetPrefetchData( | 499 bool ResourcePrefetchPredictor::GetPrefetchData(const GURL& url, |
|
gone
2016/09/28 18:18:15
Can you use something more descriptive than url he
Benoit L
2016/10/03 09:42:05
Done.
| |
| 500 const NavigationID& navigation_id, | 500 std::vector<GURL>* urls) { |
| 501 std::vector<GURL>* urls, | |
| 502 PrefetchKeyType* key_type) { | |
| 503 DCHECK(urls); | 501 DCHECK(urls); |
| 504 DCHECK(key_type); | |
| 505 | |
| 506 *key_type = PREFETCH_KEY_TYPE_URL; | |
| 507 const GURL& main_frame_url = navigation_id.main_frame_url; | |
| 508 | 502 |
| 509 bool use_url_data = config_.IsPrefetchingEnabled(profile_) ? | 503 bool use_url_data = config_.IsPrefetchingEnabled(profile_) ? |
| 510 config_.IsURLPrefetchingEnabled(profile_) : | 504 config_.IsURLPrefetchingEnabled(profile_) : |
| 511 config_.IsURLLearningEnabled(); | 505 config_.IsURLLearningEnabled(); |
| 512 if (use_url_data) { | 506 if (use_url_data) { |
| 513 PrefetchDataMap::const_iterator iterator = | 507 PrefetchDataMap::const_iterator iterator = |
| 514 url_table_cache_->find(main_frame_url.spec()); | 508 url_table_cache_->find(url.spec()); |
| 515 if (iterator != url_table_cache_->end()) | 509 if (iterator != url_table_cache_->end()) |
| 516 PopulatePrefetcherRequest(iterator->second, urls); | 510 PopulatePrefetcherRequest(iterator->second, urls); |
| 517 } | 511 } |
| 512 if (!urls->empty()) | |
| 513 return true; | |
| 518 | 514 |
| 519 bool use_host_data = config_.IsPrefetchingEnabled(profile_) ? | 515 bool use_host_data = config_.IsPrefetchingEnabled(profile_) ? |
| 520 config_.IsHostPrefetchingEnabled(profile_) : | 516 config_.IsHostPrefetchingEnabled(profile_) : |
| 521 config_.IsHostLearningEnabled(); | 517 config_.IsHostLearningEnabled(); |
| 522 if (urls->empty() && use_host_data) { | 518 if (use_host_data) { |
| 523 PrefetchDataMap::const_iterator iterator = | 519 PrefetchDataMap::const_iterator iterator = |
| 524 host_table_cache_->find(main_frame_url.host()); | 520 host_table_cache_->find(url.host()); |
| 525 if (iterator != host_table_cache_->end()) { | 521 if (iterator != host_table_cache_->end()) |
| 526 *key_type = PREFETCH_KEY_TYPE_HOST; | |
| 527 PopulatePrefetcherRequest(iterator->second, urls); | 522 PopulatePrefetcherRequest(iterator->second, urls); |
| 528 } | |
| 529 } | 523 } |
| 530 | 524 |
| 531 return !urls->empty(); | 525 return !urls->empty(); |
| 532 } | 526 } |
| 533 | 527 |
| 534 void ResourcePrefetchPredictor::PopulatePrefetcherRequest( | 528 void ResourcePrefetchPredictor::PopulatePrefetcherRequest( |
| 535 const PrefetchData& data, | 529 const PrefetchData& data, |
| 536 std::vector<GURL>* urls) { | 530 std::vector<GURL>* urls) { |
| 537 for (const ResourceData& resource : data.resources) { | 531 for (const ResourceData& resource : data.resources) { |
| 538 float confidence = | 532 float confidence = |
| 539 static_cast<float>(resource.number_of_hits()) / | 533 static_cast<float>(resource.number_of_hits()) / |
| 540 (resource.number_of_hits() + resource.number_of_misses()); | 534 (resource.number_of_hits() + resource.number_of_misses()); |
| 541 if (confidence < config_.min_resource_confidence_to_trigger_prefetch || | 535 if (confidence < config_.min_resource_confidence_to_trigger_prefetch || |
| 542 resource.number_of_hits() < | 536 resource.number_of_hits() < |
| 543 config_.min_resource_hits_to_trigger_prefetch) { | 537 config_.min_resource_hits_to_trigger_prefetch) { |
| 544 continue; | 538 continue; |
| 545 } | 539 } |
| 546 | 540 |
| 547 urls->push_back(GURL(resource.resource_url())); | 541 urls->push_back(GURL(resource.resource_url())); |
| 548 } | 542 } |
| 549 } | 543 } |
| 550 | 544 |
| 551 void ResourcePrefetchPredictor::StartPrefetching( | 545 void ResourcePrefetchPredictor::StartPrefetching(const GURL& url) { |
| 552 const NavigationID& navigation_id) { | |
| 553 if (!prefetch_manager_.get()) // Prefetching not enabled. | 546 if (!prefetch_manager_.get()) // Prefetching not enabled. |
| 554 return; | 547 return; |
| 555 | 548 |
| 556 // Prefer URL based data first. | 549 std::vector<GURL> subresource_urls; |
| 557 std::vector<GURL> urls; | 550 if (!GetPrefetchData(url, &subresource_urls)) { |
| 558 PrefetchKeyType key_type; | |
| 559 if (!GetPrefetchData(navigation_id, &urls, &key_type)) { | |
| 560 // No prefetching data at host or URL level. | 551 // No prefetching data at host or URL level. |
| 561 return; | 552 return; |
| 562 } | 553 } |
| 563 | 554 |
| 564 BrowserThread::PostTask( | 555 BrowserThread::PostTask( |
| 565 BrowserThread::IO, FROM_HERE, | 556 BrowserThread::IO, FROM_HERE, |
| 566 base::Bind(&ResourcePrefetcherManager::MaybeAddPrefetch, | 557 base::Bind(&ResourcePrefetcherManager::MaybeAddPrefetch, |
| 567 prefetch_manager_, navigation_id, urls)); | 558 prefetch_manager_, url, subresource_urls)); |
| 568 } | 559 } |
| 569 | 560 |
| 570 void ResourcePrefetchPredictor::StopPrefetching( | 561 void ResourcePrefetchPredictor::StopPrefetching(const GURL& url) { |
| 571 const NavigationID& navigation_id) { | |
| 572 if (!prefetch_manager_.get()) // Not enabled. | 562 if (!prefetch_manager_.get()) // Not enabled. |
| 573 return; | 563 return; |
| 574 | 564 |
| 575 BrowserThread::PostTask( | 565 BrowserThread::PostTask( |
| 576 BrowserThread::IO, FROM_HERE, | 566 BrowserThread::IO, FROM_HERE, |
| 577 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, | 567 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, |
| 578 prefetch_manager_, | 568 prefetch_manager_, url)); |
| 579 navigation_id)); | |
| 580 } | 569 } |
| 581 | 570 |
| 582 void ResourcePrefetchPredictor::StartInitialization() { | 571 void ResourcePrefetchPredictor::StartInitialization() { |
| 583 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 572 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 584 | 573 |
| 585 DCHECK_EQ(NOT_INITIALIZED, initialization_state_); | 574 DCHECK_EQ(NOT_INITIALIZED, initialization_state_); |
| 586 initialization_state_ = INITIALIZING; | 575 initialization_state_ = INITIALIZING; |
| 587 | 576 |
| 588 // Create local caches using the database as loaded. | 577 // Create local caches using the database as loaded. |
| 589 std::unique_ptr<PrefetchDataMap> url_data_map(new PrefetchDataMap()); | 578 std::unique_ptr<PrefetchDataMap> url_data_map(new PrefetchDataMap()); |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 939 // HistoryService is already loaded. Continue with Initialization. | 928 // HistoryService is already loaded. Continue with Initialization. |
| 940 OnHistoryAndCacheLoaded(); | 929 OnHistoryAndCacheLoaded(); |
| 941 return; | 930 return; |
| 942 } | 931 } |
| 943 DCHECK(!history_service_observer_.IsObserving(history_service)); | 932 DCHECK(!history_service_observer_.IsObserving(history_service)); |
| 944 history_service_observer_.Add(history_service); | 933 history_service_observer_.Add(history_service); |
| 945 return; | 934 return; |
| 946 } | 935 } |
| 947 | 936 |
| 948 } // namespace predictors | 937 } // namespace predictors |
| OLD | NEW |