| 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 |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/metrics/sparse_histogram.h" | 13 #include "base/metrics/sparse_histogram.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "chrome/browser/chrome_notification_types.h" | |
| 19 #include "chrome/browser/history/history_notifications.h" | |
| 20 #include "chrome/browser/history/history_service.h" | 18 #include "chrome/browser/history/history_service.h" |
| 21 #include "chrome/browser/history/history_service_factory.h" | 19 #include "chrome/browser/history/history_service_factory.h" |
| 22 #include "chrome/browser/predictors/predictor_database.h" | 20 #include "chrome/browser/predictors/predictor_database.h" |
| 23 #include "chrome/browser/predictors/predictor_database_factory.h" | 21 #include "chrome/browser/predictors/predictor_database_factory.h" |
| 24 #include "chrome/browser/predictors/resource_prefetcher_manager.h" | 22 #include "chrome/browser/predictors/resource_prefetcher_manager.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/common/chrome_switches.h" | 24 #include "chrome/common/chrome_switches.h" |
| 27 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
| 28 #include "components/history/core/browser/history_database.h" | 26 #include "components/history/core/browser/history_database.h" |
| 29 #include "components/history/core/browser/history_db_task.h" | 27 #include "components/history/core/browser/history_db_task.h" |
| 30 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/navigation_controller.h" | 29 #include "content/public/browser/navigation_controller.h" |
| 32 #include "content/public/browser/notification_service.h" | |
| 33 #include "content/public/browser/notification_source.h" | |
| 34 #include "content/public/browser/notification_types.h" | |
| 35 #include "content/public/browser/resource_request_info.h" | 30 #include "content/public/browser/resource_request_info.h" |
| 36 #include "content/public/browser/web_contents.h" | 31 #include "content/public/browser/web_contents.h" |
| 37 #include "net/base/mime_util.h" | 32 #include "net/base/mime_util.h" |
| 38 #include "net/base/network_change_notifier.h" | 33 #include "net/base/network_change_notifier.h" |
| 39 #include "net/http/http_response_headers.h" | 34 #include "net/http/http_response_headers.h" |
| 40 #include "net/url_request/url_request.h" | 35 #include "net/url_request/url_request.h" |
| 41 #include "net/url_request/url_request_context_getter.h" | 36 #include "net/url_request/url_request_context_getter.h" |
| 42 | 37 |
| 43 using content::BrowserThread; | 38 using content::BrowserThread; |
| 44 | 39 |
| (...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 425 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 431 | 426 |
| 432 Result* result = new Result(key_type, requests); | 427 Result* result = new Result(key_type, requests); |
| 433 // Add the results to the results map. | 428 // Add the results to the results map. |
| 434 if (!results_map_.insert(std::make_pair(navigation_id, result)).second) { | 429 if (!results_map_.insert(std::make_pair(navigation_id, result)).second) { |
| 435 DLOG(FATAL) << "Returning results for existing navigation."; | 430 DLOG(FATAL) << "Returning results for existing navigation."; |
| 436 delete result; | 431 delete result; |
| 437 } | 432 } |
| 438 } | 433 } |
| 439 | 434 |
| 440 void ResourcePrefetchPredictor::Observe( | |
| 441 int type, | |
| 442 const content::NotificationSource& source, | |
| 443 const content::NotificationDetails& details) { | |
| 444 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 445 | |
| 446 switch (type) { | |
| 447 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: { | |
| 448 DCHECK_EQ(initialization_state_, INITIALIZED); | |
| 449 const content::Details<const history::URLsDeletedDetails> | |
| 450 urls_deleted_details = | |
| 451 content::Details<const history::URLsDeletedDetails>(details); | |
| 452 if (urls_deleted_details->all_history) { | |
| 453 DeleteAllUrls(); | |
| 454 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ReportingEvent", | |
| 455 REPORTING_EVENT_ALL_HISTORY_CLEARED, | |
| 456 REPORTING_EVENT_COUNT); | |
| 457 } else { | |
| 458 DeleteUrls(urls_deleted_details->rows); | |
| 459 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ReportingEvent", | |
| 460 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED, | |
| 461 REPORTING_EVENT_COUNT); | |
| 462 } | |
| 463 break; | |
| 464 } | |
| 465 | |
| 466 default: | |
| 467 NOTREACHED() << "Unexpected notification observed."; | |
| 468 break; | |
| 469 } | |
| 470 } | |
| 471 | |
| 472 void ResourcePrefetchPredictor::Shutdown() { | 435 void ResourcePrefetchPredictor::Shutdown() { |
| 473 if (prefetch_manager_.get()) { | 436 if (prefetch_manager_.get()) { |
| 474 prefetch_manager_->ShutdownOnUIThread(); | 437 prefetch_manager_->ShutdownOnUIThread(); |
| 475 prefetch_manager_ = NULL; | 438 prefetch_manager_ = NULL; |
| 476 } | 439 } |
| 440 history_service_observer_.RemoveAll(); |
| 477 } | 441 } |
| 478 | 442 |
| 479 void ResourcePrefetchPredictor::OnMainFrameRequest( | 443 void ResourcePrefetchPredictor::OnMainFrameRequest( |
| 480 const URLRequestSummary& request) { | 444 const URLRequestSummary& request) { |
| 481 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 445 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 482 DCHECK_EQ(INITIALIZED, initialization_state_); | 446 DCHECK_EQ(INITIALIZED, initialization_state_); |
| 483 | 447 |
| 484 RecordNavigationEvent(NAVIGATION_EVENT_REQUEST_STARTED); | 448 RecordNavigationEvent(NAVIGATION_EVENT_REQUEST_STARTED); |
| 485 | 449 |
| 486 StartPrefetching(request.navigation_id); | 450 StartPrefetching(request.navigation_id); |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 702 BrowserThread::PostTask( | 666 BrowserThread::PostTask( |
| 703 BrowserThread::IO, FROM_HERE, | 667 BrowserThread::IO, FROM_HERE, |
| 704 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, | 668 base::Bind(&ResourcePrefetcherManager::MaybeRemovePrefetch, |
| 705 prefetch_manager_, | 669 prefetch_manager_, |
| 706 navigation_id)); | 670 navigation_id)); |
| 707 } | 671 } |
| 708 | 672 |
| 709 void ResourcePrefetchPredictor::StartInitialization() { | 673 void ResourcePrefetchPredictor::StartInitialization() { |
| 710 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 674 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 711 | 675 |
| 712 DCHECK_EQ(initialization_state_, NOT_INITIALIZED); | 676 DCHECK_EQ(NOT_INITIALIZED, initialization_state_); |
| 713 initialization_state_ = INITIALIZING; | 677 initialization_state_ = INITIALIZING; |
| 714 | 678 |
| 715 // Create local caches using the database as loaded. | 679 // Create local caches using the database as loaded. |
| 716 scoped_ptr<PrefetchDataMap> url_data_map(new PrefetchDataMap()); | 680 scoped_ptr<PrefetchDataMap> url_data_map(new PrefetchDataMap()); |
| 717 scoped_ptr<PrefetchDataMap> host_data_map(new PrefetchDataMap()); | 681 scoped_ptr<PrefetchDataMap> host_data_map(new PrefetchDataMap()); |
| 718 PrefetchDataMap* url_data_ptr = url_data_map.get(); | 682 PrefetchDataMap* url_data_ptr = url_data_map.get(); |
| 719 PrefetchDataMap* host_data_ptr = host_data_map.get(); | 683 PrefetchDataMap* host_data_ptr = host_data_map.get(); |
| 720 | 684 |
| 721 BrowserThread::PostTaskAndReply( | 685 BrowserThread::PostTaskAndReply( |
| 722 BrowserThread::DB, FROM_HERE, | 686 BrowserThread::DB, FROM_HERE, |
| 723 base::Bind(&ResourcePrefetchPredictorTables::GetAllData, | 687 base::Bind(&ResourcePrefetchPredictorTables::GetAllData, |
| 724 tables_, url_data_ptr, host_data_ptr), | 688 tables_, url_data_ptr, host_data_ptr), |
| 725 base::Bind(&ResourcePrefetchPredictor::CreateCaches, AsWeakPtr(), | 689 base::Bind(&ResourcePrefetchPredictor::CreateCaches, AsWeakPtr(), |
| 726 base::Passed(&url_data_map), base::Passed(&host_data_map))); | 690 base::Passed(&url_data_map), base::Passed(&host_data_map))); |
| 727 } | 691 } |
| 728 | 692 |
| 729 void ResourcePrefetchPredictor::CreateCaches( | 693 void ResourcePrefetchPredictor::CreateCaches( |
| 730 scoped_ptr<PrefetchDataMap> url_data_map, | 694 scoped_ptr<PrefetchDataMap> url_data_map, |
| 731 scoped_ptr<PrefetchDataMap> host_data_map) { | 695 scoped_ptr<PrefetchDataMap> host_data_map) { |
| 732 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 696 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 733 | 697 |
| 734 DCHECK_EQ(initialization_state_, INITIALIZING); | 698 DCHECK_EQ(INITIALIZING, initialization_state_); |
| 735 DCHECK(!url_table_cache_); | 699 DCHECK(!url_table_cache_); |
| 736 DCHECK(!host_table_cache_); | 700 DCHECK(!host_table_cache_); |
| 737 DCHECK(inflight_navigations_.empty()); | 701 DCHECK(inflight_navigations_.empty()); |
| 738 | 702 |
| 739 url_table_cache_.reset(url_data_map.release()); | 703 url_table_cache_.reset(url_data_map.release()); |
| 740 host_table_cache_.reset(host_data_map.release()); | 704 host_table_cache_.reset(host_data_map.release()); |
| 741 | 705 |
| 742 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableMainFrameUrlCount", | 706 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableMainFrameUrlCount", |
| 743 url_table_cache_->size()); | 707 url_table_cache_->size()); |
| 744 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableHostCount", | 708 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableHostCount", |
| 745 host_table_cache_->size()); | 709 host_table_cache_->size()); |
| 746 | 710 |
| 747 ConnectToHistoryService(); | 711 ConnectToHistoryService(); |
| 748 } | 712 } |
| 749 | 713 |
| 750 void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() { | 714 void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() { |
| 751 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 715 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 752 DCHECK_EQ(initialization_state_, INITIALIZING); | 716 DCHECK_EQ(INITIALIZING, initialization_state_); |
| 753 | |
| 754 notification_registrar_.Add(this, | |
| 755 chrome::NOTIFICATION_HISTORY_URLS_DELETED, | |
| 756 content::Source<Profile>(profile_)); | |
| 757 | 717 |
| 758 // Initialize the prefetch manager only if prefetching is enabled. | 718 // Initialize the prefetch manager only if prefetching is enabled. |
| 759 if (config_.IsPrefetchingEnabled(profile_)) { | 719 if (config_.IsPrefetchingEnabled(profile_)) { |
| 760 prefetch_manager_ = new ResourcePrefetcherManager( | 720 prefetch_manager_ = new ResourcePrefetcherManager( |
| 761 this, config_, profile_->GetRequestContext()); | 721 this, config_, profile_->GetRequestContext()); |
| 762 } | 722 } |
| 763 | |
| 764 history_service_observer_.RemoveAll(); | |
| 765 initialization_state_ = INITIALIZED; | 723 initialization_state_ = INITIALIZED; |
| 766 } | 724 } |
| 767 | 725 |
| 768 void ResourcePrefetchPredictor::CleanupAbandonedNavigations( | 726 void ResourcePrefetchPredictor::CleanupAbandonedNavigations( |
| 769 const NavigationID& navigation_id) { | 727 const NavigationID& navigation_id) { |
| 770 static const base::TimeDelta max_navigation_age = | 728 static const base::TimeDelta max_navigation_age = |
| 771 base::TimeDelta::FromSeconds(config_.max_navigation_lifetime_seconds); | 729 base::TimeDelta::FromSeconds(config_.max_navigation_lifetime_seconds); |
| 772 | 730 |
| 773 base::TimeTicks time_now = base::TimeTicks::Now(); | 731 base::TimeTicks time_now = base::TimeTicks::Now(); |
| 774 for (NavigationMap::iterator it = inflight_navigations_.begin(); | 732 for (NavigationMap::iterator it = inflight_navigations_.begin(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 800 | 758 |
| 801 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 759 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 802 base::Bind(&ResourcePrefetchPredictorTables::DeleteAllData, tables_)); | 760 base::Bind(&ResourcePrefetchPredictorTables::DeleteAllData, tables_)); |
| 803 } | 761 } |
| 804 | 762 |
| 805 void ResourcePrefetchPredictor::DeleteUrls(const history::URLRows& urls) { | 763 void ResourcePrefetchPredictor::DeleteUrls(const history::URLRows& urls) { |
| 806 // Check all the urls in the database and pick out the ones that are present | 764 // Check all the urls in the database and pick out the ones that are present |
| 807 // in the cache. | 765 // in the cache. |
| 808 std::vector<std::string> urls_to_delete, hosts_to_delete; | 766 std::vector<std::string> urls_to_delete, hosts_to_delete; |
| 809 | 767 |
| 810 for (history::URLRows::const_iterator it = urls.begin(); it != urls.end(); | 768 for (const auto& it : urls) { |
| 811 ++it) { | 769 const std::string& url_spec = it.url().spec(); |
| 812 const std::string url_spec = it->url().spec(); | |
| 813 if (url_table_cache_->find(url_spec) != url_table_cache_->end()) { | 770 if (url_table_cache_->find(url_spec) != url_table_cache_->end()) { |
| 814 urls_to_delete.push_back(url_spec); | 771 urls_to_delete.push_back(url_spec); |
| 815 url_table_cache_->erase(url_spec); | 772 url_table_cache_->erase(url_spec); |
| 816 } | 773 } |
| 817 | 774 |
| 818 const std::string host = it->url().host(); | 775 const std::string& host = it.url().host(); |
| 819 if (host_table_cache_->find(host) != host_table_cache_->end()) { | 776 if (host_table_cache_->find(host) != host_table_cache_->end()) { |
| 820 hosts_to_delete.push_back(host); | 777 hosts_to_delete.push_back(host); |
| 821 host_table_cache_->erase(host); | 778 host_table_cache_->erase(host); |
| 822 } | 779 } |
| 823 } | 780 } |
| 824 | 781 |
| 825 if (!urls_to_delete.empty() || !hosts_to_delete.empty()) { | 782 if (!urls_to_delete.empty() || !hosts_to_delete.empty()) { |
| 826 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, | 783 BrowserThread::PostTask(BrowserThread::DB, FROM_HERE, |
| 827 base::Bind(&ResourcePrefetchPredictorTables::DeleteData, | 784 base::Bind(&ResourcePrefetchPredictorTables::DeleteData, |
| 828 tables_, | 785 tables_, |
| (...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1332 RPP_PREDICTED_HISTOGRAM_PERCENTAGE( | 1289 RPP_PREDICTED_HISTOGRAM_PERCENTAGE( |
| 1333 "PrefetchFromNetworkPercentOfTotalFromNetwork", | 1290 "PrefetchFromNetworkPercentOfTotalFromNetwork", |
| 1334 prefetch_network * 100.0 / total_resources_fetched_from_network); | 1291 prefetch_network * 100.0 / total_resources_fetched_from_network); |
| 1335 } | 1292 } |
| 1336 | 1293 |
| 1337 #undef RPP_HISTOGRAM_MEDIUM_TIMES | 1294 #undef RPP_HISTOGRAM_MEDIUM_TIMES |
| 1338 #undef RPP_PREDICTED_HISTOGRAM_PERCENTAGE | 1295 #undef RPP_PREDICTED_HISTOGRAM_PERCENTAGE |
| 1339 #undef RPP_PREDICTED_HISTOGRAM_COUNTS | 1296 #undef RPP_PREDICTED_HISTOGRAM_COUNTS |
| 1340 } | 1297 } |
| 1341 | 1298 |
| 1299 void ResourcePrefetchPredictor::OnURLsDeleted( |
| 1300 HistoryService* history_service, |
| 1301 bool all_history, |
| 1302 bool expired, |
| 1303 const history::URLRows& deleted_rows, |
| 1304 const std::set<GURL>& favicon_urls) { |
| 1305 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1306 if (INITIALIZED != initialization_state_) |
| 1307 return; |
| 1308 |
| 1309 if (all_history) { |
| 1310 DeleteAllUrls(); |
| 1311 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ReportingEvent", |
| 1312 REPORTING_EVENT_ALL_HISTORY_CLEARED, |
| 1313 REPORTING_EVENT_COUNT); |
| 1314 } else { |
| 1315 DeleteUrls(deleted_rows); |
| 1316 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ReportingEvent", |
| 1317 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED, |
| 1318 REPORTING_EVENT_COUNT); |
| 1319 } |
| 1320 } |
| 1321 |
| 1342 void ResourcePrefetchPredictor::OnHistoryServiceLoaded( | 1322 void ResourcePrefetchPredictor::OnHistoryServiceLoaded( |
| 1343 HistoryService* history_service) { | 1323 HistoryService* history_service) { |
| 1344 OnHistoryAndCacheLoaded(); | 1324 OnHistoryAndCacheLoaded(); |
| 1345 history_service_observer_.Remove(history_service); | 1325 history_service_observer_.Remove(history_service); |
| 1346 } | 1326 } |
| 1347 | 1327 |
| 1348 void ResourcePrefetchPredictor::ConnectToHistoryService() { | 1328 void ResourcePrefetchPredictor::ConnectToHistoryService() { |
| 1349 // Register for HistoryServiceLoading if it is not ready. | 1329 // Register for HistoryServiceLoading if it is not ready. |
| 1350 HistoryService* history_service = HistoryServiceFactory::GetForProfile( | 1330 HistoryService* history_service = HistoryServiceFactory::GetForProfile( |
| 1351 profile_, ServiceAccessType::EXPLICIT_ACCESS); | 1331 profile_, ServiceAccessType::EXPLICIT_ACCESS); |
| 1352 if (!history_service) | 1332 if (!history_service) |
| 1353 return; | 1333 return; |
| 1354 if (history_service->BackendLoaded()) { | 1334 if (history_service->BackendLoaded()) { |
| 1355 // HistoryService is already loaded. Continue with Initialization. | 1335 // HistoryService is already loaded. Continue with Initialization. |
| 1356 OnHistoryAndCacheLoaded(); | 1336 OnHistoryAndCacheLoaded(); |
| 1357 return; | 1337 return; |
| 1358 } | 1338 } |
| 1359 DCHECK(!history_service_observer_.IsObserving(history_service)); | 1339 DCHECK(!history_service_observer_.IsObserving(history_service)); |
| 1360 history_service_observer_.Add(history_service); | 1340 history_service_observer_.Add(history_service); |
| 1361 return; | 1341 return; |
| 1362 } | 1342 } |
| 1363 | 1343 |
| 1364 } // namespace predictors | 1344 } // namespace predictors |
| OLD | NEW |