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 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 // ResourcePrefetchPredictor. | 300 // ResourcePrefetchPredictor. |
| 301 | 301 |
| 302 ResourcePrefetchPredictor::ResourcePrefetchPredictor( | 302 ResourcePrefetchPredictor::ResourcePrefetchPredictor( |
| 303 const ResourcePrefetchPredictorConfig& config, | 303 const ResourcePrefetchPredictorConfig& config, |
| 304 Profile* profile) | 304 Profile* profile) |
| 305 : profile_(profile), | 305 : profile_(profile), |
| 306 config_(config), | 306 config_(config), |
| 307 initialization_state_(NOT_INITIALIZED), | 307 initialization_state_(NOT_INITIALIZED), |
| 308 tables_(PredictorDatabaseFactory::GetForProfile( | 308 tables_(PredictorDatabaseFactory::GetForProfile( |
| 309 profile)->resource_prefetch_tables()), | 309 profile)->resource_prefetch_tables()), |
| 310 results_map_deleter_(&results_map_) { | 310 results_map_deleter_(&results_map_), |
| 311 history_service_observer_(this) { | |
| 311 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 312 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 312 | 313 |
| 313 // Some form of learning has to be enabled. | 314 // Some form of learning has to be enabled. |
| 314 DCHECK(config_.IsLearningEnabled()); | 315 DCHECK(config_.IsLearningEnabled()); |
| 315 if (config_.IsURLPrefetchingEnabled(profile_)) | 316 if (config_.IsURLPrefetchingEnabled(profile_)) |
| 316 DCHECK(config_.IsURLLearningEnabled()); | 317 DCHECK(config_.IsURLLearningEnabled()); |
| 317 if (config_.IsHostPrefetchingEnabled(profile_)) | 318 if (config_.IsHostPrefetchingEnabled(profile_)) |
| 318 DCHECK(config_.IsHostLearningEnabled()); | 319 DCHECK(config_.IsHostLearningEnabled()); |
| 319 } | 320 } |
| 320 | 321 |
| 321 ResourcePrefetchPredictor::~ResourcePrefetchPredictor() { | 322 ResourcePrefetchPredictor::~ResourcePrefetchPredictor() { |
| 322 } | 323 } |
| 323 | 324 |
| 324 void ResourcePrefetchPredictor::RecordURLRequest( | 325 void ResourcePrefetchPredictor::RecordURLRequest( |
| 325 const URLRequestSummary& request) { | 326 const URLRequestSummary& request) { |
| 326 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 327 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 327 if (initialization_state_ != INITIALIZED) | 328 if (initialization_state_ != INITIALIZED) |
| 328 return; | 329 if (!CheckForHistoryService()) |
|
sdefresne
2014/10/30 17:37:04
Question: is it really required to insert all thos
nshaik
2014/10/30 21:48:36
I was trying to be extra careful which I think is
| |
| 330 return; | |
| 329 | 331 |
| 330 CHECK_EQ(request.resource_type, content::RESOURCE_TYPE_MAIN_FRAME); | 332 CHECK_EQ(request.resource_type, content::RESOURCE_TYPE_MAIN_FRAME); |
| 331 OnMainFrameRequest(request); | 333 OnMainFrameRequest(request); |
| 332 } | 334 } |
| 333 | 335 |
| 334 void ResourcePrefetchPredictor::RecordURLResponse( | 336 void ResourcePrefetchPredictor::RecordURLResponse( |
| 335 const URLRequestSummary& response) { | 337 const URLRequestSummary& response) { |
| 336 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 338 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 337 if (initialization_state_ != INITIALIZED) | 339 if (initialization_state_ != INITIALIZED) |
| 338 return; | 340 if (!CheckForHistoryService()) |
| 341 return; | |
| 339 | 342 |
| 340 if (response.resource_type == content::RESOURCE_TYPE_MAIN_FRAME) | 343 if (response.resource_type == content::RESOURCE_TYPE_MAIN_FRAME) |
| 341 OnMainFrameResponse(response); | 344 OnMainFrameResponse(response); |
| 342 else | 345 else |
| 343 OnSubresourceResponse(response); | 346 OnSubresourceResponse(response); |
| 344 } | 347 } |
| 345 | 348 |
| 346 void ResourcePrefetchPredictor::RecordURLRedirect( | 349 void ResourcePrefetchPredictor::RecordURLRedirect( |
| 347 const URLRequestSummary& response) { | 350 const URLRequestSummary& response) { |
| 348 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 351 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 349 if (initialization_state_ != INITIALIZED) | 352 if (initialization_state_ != INITIALIZED) |
| 350 return; | 353 if (!CheckForHistoryService()) |
| 354 return; | |
| 351 | 355 |
| 352 CHECK_EQ(response.resource_type, content::RESOURCE_TYPE_MAIN_FRAME); | 356 CHECK_EQ(response.resource_type, content::RESOURCE_TYPE_MAIN_FRAME); |
| 353 OnMainFrameRedirect(response); | 357 OnMainFrameRedirect(response); |
| 354 } | 358 } |
| 355 | 359 |
| 356 void ResourcePrefetchPredictor::RecordMainFrameLoadComplete( | 360 void ResourcePrefetchPredictor::RecordMainFrameLoadComplete( |
| 357 const NavigationID& navigation_id) { | 361 const NavigationID& navigation_id) { |
| 358 switch (initialization_state_) { | 362 switch (initialization_state_) { |
| 359 case NOT_INITIALIZED: | 363 case NOT_INITIALIZED: |
| 360 StartInitialization(); | 364 StartInitialization(); |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 391 } | 395 } |
| 392 } | 396 } |
| 393 | 397 |
| 394 void ResourcePrefetchPredictor::Observe( | 398 void ResourcePrefetchPredictor::Observe( |
| 395 int type, | 399 int type, |
| 396 const content::NotificationSource& source, | 400 const content::NotificationSource& source, |
| 397 const content::NotificationDetails& details) { | 401 const content::NotificationDetails& details) { |
| 398 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 402 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 399 | 403 |
| 400 switch (type) { | 404 switch (type) { |
| 401 case chrome::NOTIFICATION_HISTORY_LOADED: { | |
| 402 DCHECK_EQ(initialization_state_, INITIALIZING); | |
| 403 notification_registrar_.Remove(this, | |
| 404 chrome::NOTIFICATION_HISTORY_LOADED, | |
| 405 content::Source<Profile>(profile_)); | |
| 406 OnHistoryAndCacheLoaded(); | |
| 407 break; | |
| 408 } | |
| 409 | |
| 410 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: { | 405 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: { |
| 411 DCHECK_EQ(initialization_state_, INITIALIZED); | 406 DCHECK_EQ(initialization_state_, INITIALIZED); |
| 412 const content::Details<const history::URLsDeletedDetails> | 407 const content::Details<const history::URLsDeletedDetails> |
| 413 urls_deleted_details = | 408 urls_deleted_details = |
| 414 content::Details<const history::URLsDeletedDetails>(details); | 409 content::Details<const history::URLsDeletedDetails>(details); |
| 415 if (urls_deleted_details->all_history) { | 410 if (urls_deleted_details->all_history) { |
| 416 DeleteAllUrls(); | 411 DeleteAllUrls(); |
| 417 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ReportingEvent", | 412 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ReportingEvent", |
| 418 REPORTING_EVENT_ALL_HISTORY_CLEARED, | 413 REPORTING_EVENT_ALL_HISTORY_CLEARED, |
| 419 REPORTING_EVENT_COUNT); | 414 REPORTING_EVENT_COUNT); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 454 // New empty navigation entry. | 449 // New empty navigation entry. |
| 455 inflight_navigations_.insert(std::make_pair( | 450 inflight_navigations_.insert(std::make_pair( |
| 456 request.navigation_id, | 451 request.navigation_id, |
| 457 make_linked_ptr(new std::vector<URLRequestSummary>()))); | 452 make_linked_ptr(new std::vector<URLRequestSummary>()))); |
| 458 } | 453 } |
| 459 | 454 |
| 460 void ResourcePrefetchPredictor::OnMainFrameResponse( | 455 void ResourcePrefetchPredictor::OnMainFrameResponse( |
| 461 const URLRequestSummary& response) { | 456 const URLRequestSummary& response) { |
| 462 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 457 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 463 if (initialization_state_ != INITIALIZED) | 458 if (initialization_state_ != INITIALIZED) |
| 464 return; | 459 if (!CheckForHistoryService()) |
| 460 return; | |
| 465 | 461 |
| 466 RecordNavigationEvent(NAVIGATION_EVENT_RESPONSE_STARTED); | 462 RecordNavigationEvent(NAVIGATION_EVENT_RESPONSE_STARTED); |
| 467 | 463 |
| 468 StopPrefetching(response.navigation_id); | 464 StopPrefetching(response.navigation_id); |
| 469 } | 465 } |
| 470 | 466 |
| 471 void ResourcePrefetchPredictor::OnMainFrameRedirect( | 467 void ResourcePrefetchPredictor::OnMainFrameRedirect( |
| 472 const URLRequestSummary& response) { | 468 const URLRequestSummary& response) { |
| 473 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 469 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 474 | 470 |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 687 DCHECK(inflight_navigations_.empty()); | 683 DCHECK(inflight_navigations_.empty()); |
| 688 | 684 |
| 689 url_table_cache_.reset(url_data_map.release()); | 685 url_table_cache_.reset(url_data_map.release()); |
| 690 host_table_cache_.reset(host_data_map.release()); | 686 host_table_cache_.reset(host_data_map.release()); |
| 691 | 687 |
| 692 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableMainFrameUrlCount", | 688 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableMainFrameUrlCount", |
| 693 url_table_cache_->size()); | 689 url_table_cache_->size()); |
| 694 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableHostCount", | 690 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableHostCount", |
| 695 host_table_cache_->size()); | 691 host_table_cache_->size()); |
| 696 | 692 |
| 697 // Add notifications for history loading if it is not ready. | 693 CheckForHistoryService(); |
| 698 HistoryService* history_service = HistoryServiceFactory::GetForProfile( | |
| 699 profile_, Profile::EXPLICIT_ACCESS); | |
| 700 if (!history_service) { | |
| 701 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED, | |
| 702 content::Source<Profile>(profile_)); | |
| 703 } else { | |
| 704 OnHistoryAndCacheLoaded(); | |
| 705 } | |
| 706 } | 694 } |
| 707 | 695 |
| 708 void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() { | 696 void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() { |
| 709 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 697 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 710 DCHECK_EQ(initialization_state_, INITIALIZING); | 698 DCHECK_EQ(initialization_state_, INITIALIZING); |
| 711 | 699 |
| 712 notification_registrar_.Add(this, | 700 notification_registrar_.Add(this, |
| 713 chrome::NOTIFICATION_HISTORY_URLS_DELETED, | 701 chrome::NOTIFICATION_HISTORY_URLS_DELETED, |
| 714 content::Source<Profile>(profile_)); | 702 content::Source<Profile>(profile_)); |
| 715 | 703 |
| 716 // Initialize the prefetch manager only if prefetching is enabled. | 704 // Initialize the prefetch manager only if prefetching is enabled. |
| 717 if (config_.IsPrefetchingEnabled(profile_)) { | 705 if (config_.IsPrefetchingEnabled(profile_)) { |
| 718 prefetch_manager_ = new ResourcePrefetcherManager( | 706 prefetch_manager_ = new ResourcePrefetcherManager( |
| 719 this, config_, profile_->GetRequestContext()); | 707 this, config_, profile_->GetRequestContext()); |
| 720 } | 708 } |
| 721 | 709 |
| 710 history_service_observer_.RemoveAll(); | |
| 722 initialization_state_ = INITIALIZED; | 711 initialization_state_ = INITIALIZED; |
| 723 } | 712 } |
| 724 | 713 |
| 725 void ResourcePrefetchPredictor::CleanupAbandonedNavigations( | 714 void ResourcePrefetchPredictor::CleanupAbandonedNavigations( |
| 726 const NavigationID& navigation_id) { | 715 const NavigationID& navigation_id) { |
| 727 static const base::TimeDelta max_navigation_age = | 716 static const base::TimeDelta max_navigation_age = |
| 728 base::TimeDelta::FromSeconds(config_.max_navigation_lifetime_seconds); | 717 base::TimeDelta::FromSeconds(config_.max_navigation_lifetime_seconds); |
| 729 | 718 |
| 730 base::TimeTicks time_now = base::TimeTicks::Now(); | 719 base::TimeTicks time_now = base::TimeTicks::Now(); |
| 731 for (NavigationMap::iterator it = inflight_navigations_.begin(); | 720 for (NavigationMap::iterator it = inflight_navigations_.begin(); |
| (...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1214 if (total_resources_fetched_from_network > 0) { | 1203 if (total_resources_fetched_from_network > 0) { |
| 1215 RPP_PREDICTED_HISTOGRAM_PERCENTAGE( | 1204 RPP_PREDICTED_HISTOGRAM_PERCENTAGE( |
| 1216 "PrefetchFromNetworkPercentOfTotalFromNetwork", | 1205 "PrefetchFromNetworkPercentOfTotalFromNetwork", |
| 1217 prefetch_network * 100.0 / total_resources_fetched_from_network); | 1206 prefetch_network * 100.0 / total_resources_fetched_from_network); |
| 1218 } | 1207 } |
| 1219 | 1208 |
| 1220 #undef RPP_PREDICTED_HISTOGRAM_PERCENTAGE | 1209 #undef RPP_PREDICTED_HISTOGRAM_PERCENTAGE |
| 1221 #undef RPP_PREDICTED_HISTOGRAM_COUNTS | 1210 #undef RPP_PREDICTED_HISTOGRAM_COUNTS |
| 1222 } | 1211 } |
| 1223 | 1212 |
| 1213 bool ResourcePrefetchPredictor::CheckForHistoryService() { | |
| 1214 if (initialization_state_ == INITIALIZED) | |
| 1215 return true; | |
| 1216 | |
| 1217 // Register for HistoryServiceLoading if it is not ready. | |
| 1218 HistoryService* history_service = HistoryServiceFactory::GetForProfile( | |
| 1219 profile_, Profile::EXPLICIT_ACCESS); | |
| 1220 if (history_service && history_service->BackendLoaded()) { | |
| 1221 // HistoryService is already loaded. Continue with Initialization. | |
| 1222 OnHistoryAndCacheLoaded(); | |
| 1223 return true; | |
| 1224 } else if (history_service && | |
| 1225 !history_service_observer_.IsObserving(history_service)) { | |
| 1226 history_service_observer_.Add(history_service); | |
| 1227 } | |
| 1228 return false; | |
| 1229 } | |
| 1230 | |
| 1231 void ResourcePrefetchPredictor::OnHistoryServiceLoaded( | |
| 1232 HistoryService* history_service) { | |
| 1233 OnHistoryAndCacheLoaded(); | |
| 1234 history_service_observer_.Remove(history_service); | |
| 1235 } | |
| 1236 | |
| 1224 } // namespace predictors | 1237 } // namespace predictors |
| OLD | NEW |