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" | 18 #include "chrome/browser/chrome_notification_types.h" |
sdefresne
2014/12/04 17:21:13
This #include is no longer useful, remove.
nshaik
2014/12/07 09:34:50
Done.
| |
19 #include "chrome/browser/history/history_database.h" | 19 #include "chrome/browser/history/history_database.h" |
20 #include "chrome/browser/history/history_db_task.h" | 20 #include "chrome/browser/history/history_db_task.h" |
21 #include "chrome/browser/history/history_notifications.h" | 21 #include "chrome/browser/history/history_notifications.h" |
22 #include "chrome/browser/history/history_service.h" | 22 #include "chrome/browser/history/history_service.h" |
23 #include "chrome/browser/history/history_service_factory.h" | 23 #include "chrome/browser/history/history_service_factory.h" |
24 #include "chrome/browser/predictors/predictor_database.h" | 24 #include "chrome/browser/predictors/predictor_database.h" |
25 #include "chrome/browser/predictors/predictor_database_factory.h" | 25 #include "chrome/browser/predictors/predictor_database_factory.h" |
26 #include "chrome/browser/predictors/resource_prefetcher_manager.h" | 26 #include "chrome/browser/predictors/resource_prefetcher_manager.h" |
27 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
28 #include "chrome/common/chrome_switches.h" | 28 #include "chrome/common/chrome_switches.h" |
29 #include "chrome/common/url_constants.h" | 29 #include "chrome/common/url_constants.h" |
30 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
31 #include "content/public/browser/navigation_controller.h" | 31 #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" | 32 #include "content/public/browser/resource_request_info.h" |
36 #include "content/public/browser/web_contents.h" | 33 #include "content/public/browser/web_contents.h" |
37 #include "net/base/mime_util.h" | 34 #include "net/base/mime_util.h" |
38 #include "net/base/network_change_notifier.h" | 35 #include "net/base/network_change_notifier.h" |
39 #include "net/http/http_response_headers.h" | 36 #include "net/http/http_response_headers.h" |
40 #include "net/url_request/url_request.h" | 37 #include "net/url_request/url_request.h" |
41 #include "net/url_request/url_request_context_getter.h" | 38 #include "net/url_request/url_request_context_getter.h" |
42 | 39 |
43 using content::BrowserThread; | 40 using content::BrowserThread; |
44 | 41 |
(...skipping 385 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
430 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 427 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
431 | 428 |
432 Result* result = new Result(key_type, requests); | 429 Result* result = new Result(key_type, requests); |
433 // Add the results to the results map. | 430 // Add the results to the results map. |
434 if (!results_map_.insert(std::make_pair(navigation_id, result)).second) { | 431 if (!results_map_.insert(std::make_pair(navigation_id, result)).second) { |
435 DLOG(FATAL) << "Returning results for existing navigation."; | 432 DLOG(FATAL) << "Returning results for existing navigation."; |
436 delete result; | 433 delete result; |
437 } | 434 } |
438 } | 435 } |
439 | 436 |
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() { | 437 void ResourcePrefetchPredictor::Shutdown() { |
473 if (prefetch_manager_.get()) { | 438 if (prefetch_manager_.get()) { |
474 prefetch_manager_->ShutdownOnUIThread(); | 439 prefetch_manager_->ShutdownOnUIThread(); |
475 prefetch_manager_ = NULL; | 440 prefetch_manager_ = NULL; |
476 } | 441 } |
442 history_service_observer_.RemoveAll(); | |
477 } | 443 } |
478 | 444 |
479 void ResourcePrefetchPredictor::OnMainFrameRequest( | 445 void ResourcePrefetchPredictor::OnMainFrameRequest( |
480 const URLRequestSummary& request) { | 446 const URLRequestSummary& request) { |
481 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 447 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
482 DCHECK_EQ(INITIALIZED, initialization_state_); | 448 DCHECK_EQ(INITIALIZED, initialization_state_); |
483 | 449 |
484 RecordNavigationEvent(NAVIGATION_EVENT_REQUEST_STARTED); | 450 RecordNavigationEvent(NAVIGATION_EVENT_REQUEST_STARTED); |
485 | 451 |
486 StartPrefetching(request.navigation_id); | 452 StartPrefetching(request.navigation_id); |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
744 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableHostCount", | 710 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableHostCount", |
745 host_table_cache_->size()); | 711 host_table_cache_->size()); |
746 | 712 |
747 ConnectToHistoryService(); | 713 ConnectToHistoryService(); |
748 } | 714 } |
749 | 715 |
750 void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() { | 716 void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() { |
751 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 717 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
752 DCHECK_EQ(initialization_state_, INITIALIZING); | 718 DCHECK_EQ(initialization_state_, INITIALIZING); |
753 | 719 |
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. | 720 // Initialize the prefetch manager only if prefetching is enabled. |
759 if (config_.IsPrefetchingEnabled(profile_)) { | 721 if (config_.IsPrefetchingEnabled(profile_)) { |
760 prefetch_manager_ = new ResourcePrefetcherManager( | 722 prefetch_manager_ = new ResourcePrefetcherManager( |
761 this, config_, profile_->GetRequestContext()); | 723 this, config_, profile_->GetRequestContext()); |
762 } | 724 } |
763 | |
764 history_service_observer_.RemoveAll(); | |
765 initialization_state_ = INITIALIZED; | 725 initialization_state_ = INITIALIZED; |
766 } | 726 } |
767 | 727 |
768 void ResourcePrefetchPredictor::CleanupAbandonedNavigations( | 728 void ResourcePrefetchPredictor::CleanupAbandonedNavigations( |
769 const NavigationID& navigation_id) { | 729 const NavigationID& navigation_id) { |
770 static const base::TimeDelta max_navigation_age = | 730 static const base::TimeDelta max_navigation_age = |
771 base::TimeDelta::FromSeconds(config_.max_navigation_lifetime_seconds); | 731 base::TimeDelta::FromSeconds(config_.max_navigation_lifetime_seconds); |
772 | 732 |
773 base::TimeTicks time_now = base::TimeTicks::Now(); | 733 base::TimeTicks time_now = base::TimeTicks::Now(); |
774 for (NavigationMap::iterator it = inflight_navigations_.begin(); | 734 for (NavigationMap::iterator it = inflight_navigations_.begin(); |
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1332 RPP_PREDICTED_HISTOGRAM_PERCENTAGE( | 1292 RPP_PREDICTED_HISTOGRAM_PERCENTAGE( |
1333 "PrefetchFromNetworkPercentOfTotalFromNetwork", | 1293 "PrefetchFromNetworkPercentOfTotalFromNetwork", |
1334 prefetch_network * 100.0 / total_resources_fetched_from_network); | 1294 prefetch_network * 100.0 / total_resources_fetched_from_network); |
1335 } | 1295 } |
1336 | 1296 |
1337 #undef RPP_HISTOGRAM_MEDIUM_TIMES | 1297 #undef RPP_HISTOGRAM_MEDIUM_TIMES |
1338 #undef RPP_PREDICTED_HISTOGRAM_PERCENTAGE | 1298 #undef RPP_PREDICTED_HISTOGRAM_PERCENTAGE |
1339 #undef RPP_PREDICTED_HISTOGRAM_COUNTS | 1299 #undef RPP_PREDICTED_HISTOGRAM_COUNTS |
1340 } | 1300 } |
1341 | 1301 |
1302 void ResourcePrefetchPredictor::OnURLsDeleted( | |
1303 HistoryService* history_service, | |
1304 const history::URLsDeletedDetails& deleted_details) { | |
1305 DCHECK_EQ(initialization_state_, INITIALIZED); | |
1306 if (deleted_details.all_history) { | |
1307 DeleteAllUrls(); | |
1308 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ReportingEvent", | |
1309 REPORTING_EVENT_ALL_HISTORY_CLEARED, | |
1310 REPORTING_EVENT_COUNT); | |
1311 } else { | |
1312 DeleteUrls(deleted_details.rows); | |
1313 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ReportingEvent", | |
1314 REPORTING_EVENT_PARTIAL_HISTORY_CLEARED, | |
1315 REPORTING_EVENT_COUNT); | |
1316 } | |
1317 } | |
1318 | |
1342 void ResourcePrefetchPredictor::OnHistoryServiceLoaded( | 1319 void ResourcePrefetchPredictor::OnHistoryServiceLoaded( |
1343 HistoryService* history_service) { | 1320 HistoryService* history_service) { |
1344 OnHistoryAndCacheLoaded(); | 1321 OnHistoryAndCacheLoaded(); |
1345 history_service_observer_.Remove(history_service); | 1322 history_service_observer_.Remove(history_service); |
1346 } | 1323 } |
1347 | 1324 |
1348 void ResourcePrefetchPredictor::ConnectToHistoryService() { | 1325 void ResourcePrefetchPredictor::ConnectToHistoryService() { |
1349 // Register for HistoryServiceLoading if it is not ready. | 1326 // Register for HistoryServiceLoading if it is not ready. |
1350 HistoryService* history_service = | 1327 HistoryService* history_service = |
1351 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 1328 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
1352 if (!history_service) | 1329 if (!history_service) |
1353 return; | 1330 return; |
1354 if (history_service->BackendLoaded()) { | 1331 if (history_service->BackendLoaded()) { |
1355 // HistoryService is already loaded. Continue with Initialization. | 1332 // HistoryService is already loaded. Continue with Initialization. |
1356 OnHistoryAndCacheLoaded(); | 1333 OnHistoryAndCacheLoaded(); |
1357 return; | 1334 return; |
1358 } | 1335 } |
1359 DCHECK(!history_service_observer_.IsObserving(history_service)); | 1336 DCHECK(!history_service_observer_.IsObserving(history_service)); |
1360 history_service_observer_.Add(history_service); | 1337 history_service_observer_.Add(history_service); |
1361 return; | 1338 return; |
1362 } | 1339 } |
1363 | 1340 |
1364 } // namespace predictors | 1341 } // namespace predictors |
OLD | NEW |