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_database.h" | 18 #include "chrome/browser/history/history_database.h" |
20 #include "chrome/browser/history/history_db_task.h" | 19 #include "chrome/browser/history/history_db_task.h" |
21 #include "chrome/browser/history/history_notifications.h" | |
22 #include "chrome/browser/history/history_service.h" | 20 #include "chrome/browser/history/history_service.h" |
23 #include "chrome/browser/history/history_service_factory.h" | 21 #include "chrome/browser/history/history_service_factory.h" |
24 #include "chrome/browser/predictors/predictor_database.h" | 22 #include "chrome/browser/predictors/predictor_database.h" |
25 #include "chrome/browser/predictors/predictor_database_factory.h" | 23 #include "chrome/browser/predictors/predictor_database_factory.h" |
26 #include "chrome/browser/predictors/resource_prefetcher_manager.h" | 24 #include "chrome/browser/predictors/resource_prefetcher_manager.h" |
27 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
28 #include "chrome/common/chrome_switches.h" | 26 #include "chrome/common/chrome_switches.h" |
29 #include "chrome/common/url_constants.h" | 27 #include "chrome/common/url_constants.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 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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(initialization_state_, INITIALIZING); |
753 | 717 |
754 notification_registrar_.Add(this, | |
755 chrome::NOTIFICATION_HISTORY_URLS_DELETED, | |
756 content::Source<Profile>(profile_)); | |
757 | |
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(); |
sdefresne
2014/12/08 17:41:06
nit: const std::string& url_spec = it.url().spec()
nshaik
2014/12/08 21:29:53
Done.
| |
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(); |
sdefresne
2014/12/08 17:41:06
nit: const std::string& host = it.url().host();
nshaik
2014/12/08 21:29:53
Done.
| |
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 DCHECK_EQ(initialization_state_, INITIALIZED); | |
1307 if (all_history) { | |
1308 DeleteAllUrls(); | |
1309 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ReportingEvent", | |
1310 REPORTING_EVENT_ALL_HISTORY_CLEARED, | |
1311 REPORTING_EVENT_COUNT); | |
1312 } else { | |
1313 DeleteUrls(deleted_rows); | |
1314 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ReportingEvent", | |
1315 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED, | |
1316 REPORTING_EVENT_COUNT); | |
1317 } | |
1318 } | |
1319 | |
1342 void ResourcePrefetchPredictor::OnHistoryServiceLoaded( | 1320 void ResourcePrefetchPredictor::OnHistoryServiceLoaded( |
1343 HistoryService* history_service) { | 1321 HistoryService* history_service) { |
1344 OnHistoryAndCacheLoaded(); | 1322 OnHistoryAndCacheLoaded(); |
1345 history_service_observer_.Remove(history_service); | 1323 history_service_observer_.Remove(history_service); |
1346 } | 1324 } |
1347 | 1325 |
1348 void ResourcePrefetchPredictor::ConnectToHistoryService() { | 1326 void ResourcePrefetchPredictor::ConnectToHistoryService() { |
1349 // Register for HistoryServiceLoading if it is not ready. | 1327 // Register for HistoryServiceLoading if it is not ready. |
1350 HistoryService* history_service = | 1328 HistoryService* history_service = |
1351 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 1329 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
1352 if (!history_service) | 1330 if (!history_service) |
1353 return; | 1331 return; |
1354 if (history_service->BackendLoaded()) { | 1332 if (history_service->BackendLoaded()) { |
1355 // HistoryService is already loaded. Continue with Initialization. | 1333 // HistoryService is already loaded. Continue with Initialization. |
1356 OnHistoryAndCacheLoaded(); | 1334 OnHistoryAndCacheLoaded(); |
1357 return; | 1335 return; |
1358 } | 1336 } |
1359 DCHECK(!history_service_observer_.IsObserving(history_service)); | 1337 DCHECK(!history_service_observer_.IsObserving(history_service)); |
1360 history_service_observer_.Add(history_service); | 1338 history_service_observer_.Add(history_service); |
1361 return; | 1339 return; |
1362 } | 1340 } |
1363 | 1341 |
1364 } // namespace predictors | 1342 } // namespace predictors |
OLD | NEW |