Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor.cc

Issue 573553004: Eliminate NOTIFICATION_HISTORY_LOADED notification (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move HistoryServiceObserver functions to private Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 343
344 //////////////////////////////////////////////////////////////////////////////// 344 ////////////////////////////////////////////////////////////////////////////////
345 // ResourcePrefetchPredictor. 345 // ResourcePrefetchPredictor.
346 346
347 ResourcePrefetchPredictor::ResourcePrefetchPredictor( 347 ResourcePrefetchPredictor::ResourcePrefetchPredictor(
348 const ResourcePrefetchPredictorConfig& config, 348 const ResourcePrefetchPredictorConfig& config,
349 Profile* profile) 349 Profile* profile)
350 : profile_(profile), 350 : profile_(profile),
351 config_(config), 351 config_(config),
352 initialization_state_(NOT_INITIALIZED), 352 initialization_state_(NOT_INITIALIZED),
353 tables_(PredictorDatabaseFactory::GetForProfile( 353 tables_(PredictorDatabaseFactory::GetForProfile(profile)
354 profile)->resource_prefetch_tables()), 354 ->resource_prefetch_tables()),
355 results_map_deleter_(&results_map_) { 355 results_map_deleter_(&results_map_),
356 history_service_observer_(this) {
356 DCHECK_CURRENTLY_ON(BrowserThread::UI); 357 DCHECK_CURRENTLY_ON(BrowserThread::UI);
357 358
358 // Some form of learning has to be enabled. 359 // Some form of learning has to be enabled.
359 DCHECK(config_.IsLearningEnabled()); 360 DCHECK(config_.IsLearningEnabled());
360 if (config_.IsURLPrefetchingEnabled(profile_)) 361 if (config_.IsURLPrefetchingEnabled(profile_))
361 DCHECK(config_.IsURLLearningEnabled()); 362 DCHECK(config_.IsURLLearningEnabled());
362 if (config_.IsHostPrefetchingEnabled(profile_)) 363 if (config_.IsHostPrefetchingEnabled(profile_))
363 DCHECK(config_.IsHostLearningEnabled()); 364 DCHECK(config_.IsHostLearningEnabled());
364 } 365 }
365 366
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 } 437 }
437 } 438 }
438 439
439 void ResourcePrefetchPredictor::Observe( 440 void ResourcePrefetchPredictor::Observe(
440 int type, 441 int type,
441 const content::NotificationSource& source, 442 const content::NotificationSource& source,
442 const content::NotificationDetails& details) { 443 const content::NotificationDetails& details) {
443 DCHECK_CURRENTLY_ON(BrowserThread::UI); 444 DCHECK_CURRENTLY_ON(BrowserThread::UI);
444 445
445 switch (type) { 446 switch (type) {
446 case chrome::NOTIFICATION_HISTORY_LOADED: {
447 DCHECK_EQ(initialization_state_, INITIALIZING);
448 notification_registrar_.Remove(this,
449 chrome::NOTIFICATION_HISTORY_LOADED,
450 content::Source<Profile>(profile_));
451 OnHistoryAndCacheLoaded();
452 break;
453 }
454
455 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: { 447 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: {
456 DCHECK_EQ(initialization_state_, INITIALIZED); 448 DCHECK_EQ(initialization_state_, INITIALIZED);
457 const content::Details<const history::URLsDeletedDetails> 449 const content::Details<const history::URLsDeletedDetails>
458 urls_deleted_details = 450 urls_deleted_details =
459 content::Details<const history::URLsDeletedDetails>(details); 451 content::Details<const history::URLsDeletedDetails>(details);
460 if (urls_deleted_details->all_history) { 452 if (urls_deleted_details->all_history) {
461 DeleteAllUrls(); 453 DeleteAllUrls();
462 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ReportingEvent", 454 UMA_HISTOGRAM_ENUMERATION("ResourcePrefetchPredictor.ReportingEvent",
463 REPORTING_EVENT_ALL_HISTORY_CLEARED, 455 REPORTING_EVENT_ALL_HISTORY_CLEARED,
464 REPORTING_EVENT_COUNT); 456 REPORTING_EVENT_COUNT);
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
745 DCHECK(inflight_navigations_.empty()); 737 DCHECK(inflight_navigations_.empty());
746 738
747 url_table_cache_.reset(url_data_map.release()); 739 url_table_cache_.reset(url_data_map.release());
748 host_table_cache_.reset(host_data_map.release()); 740 host_table_cache_.reset(host_data_map.release());
749 741
750 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableMainFrameUrlCount", 742 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableMainFrameUrlCount",
751 url_table_cache_->size()); 743 url_table_cache_->size());
752 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableHostCount", 744 UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableHostCount",
753 host_table_cache_->size()); 745 host_table_cache_->size());
754 746
755 // Add notifications for history loading if it is not ready. 747 ConnectToHistoryService();
756 HistoryService* history_service = HistoryServiceFactory::GetForProfile(
757 profile_, Profile::EXPLICIT_ACCESS);
758 if (!history_service) {
759 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED,
760 content::Source<Profile>(profile_));
761 } else {
762 OnHistoryAndCacheLoaded();
763 }
764 } 748 }
765 749
766 void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() { 750 void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() {
767 DCHECK_CURRENTLY_ON(BrowserThread::UI); 751 DCHECK_CURRENTLY_ON(BrowserThread::UI);
768 DCHECK_EQ(initialization_state_, INITIALIZING); 752 DCHECK_EQ(initialization_state_, INITIALIZING);
769 753
770 notification_registrar_.Add(this, 754 notification_registrar_.Add(this,
771 chrome::NOTIFICATION_HISTORY_URLS_DELETED, 755 chrome::NOTIFICATION_HISTORY_URLS_DELETED,
772 content::Source<Profile>(profile_)); 756 content::Source<Profile>(profile_));
773 757
774 // Initialize the prefetch manager only if prefetching is enabled. 758 // Initialize the prefetch manager only if prefetching is enabled.
775 if (config_.IsPrefetchingEnabled(profile_)) { 759 if (config_.IsPrefetchingEnabled(profile_)) {
776 prefetch_manager_ = new ResourcePrefetcherManager( 760 prefetch_manager_ = new ResourcePrefetcherManager(
777 this, config_, profile_->GetRequestContext()); 761 this, config_, profile_->GetRequestContext());
778 } 762 }
779 763
764 history_service_observer_.RemoveAll();
780 initialization_state_ = INITIALIZED; 765 initialization_state_ = INITIALIZED;
781 } 766 }
782 767
783 void ResourcePrefetchPredictor::CleanupAbandonedNavigations( 768 void ResourcePrefetchPredictor::CleanupAbandonedNavigations(
784 const NavigationID& navigation_id) { 769 const NavigationID& navigation_id) {
785 static const base::TimeDelta max_navigation_age = 770 static const base::TimeDelta max_navigation_age =
786 base::TimeDelta::FromSeconds(config_.max_navigation_lifetime_seconds); 771 base::TimeDelta::FromSeconds(config_.max_navigation_lifetime_seconds);
787 772
788 base::TimeTicks time_now = base::TimeTicks::Now(); 773 base::TimeTicks time_now = base::TimeTicks::Now();
789 for (NavigationMap::iterator it = inflight_navigations_.begin(); 774 for (NavigationMap::iterator it = inflight_navigations_.begin();
(...skipping 557 matching lines...) Expand 10 before | Expand all | Expand 10 after
1347 RPP_PREDICTED_HISTOGRAM_PERCENTAGE( 1332 RPP_PREDICTED_HISTOGRAM_PERCENTAGE(
1348 "PrefetchFromNetworkPercentOfTotalFromNetwork", 1333 "PrefetchFromNetworkPercentOfTotalFromNetwork",
1349 prefetch_network * 100.0 / total_resources_fetched_from_network); 1334 prefetch_network * 100.0 / total_resources_fetched_from_network);
1350 } 1335 }
1351 1336
1352 #undef RPP_HISTOGRAM_MEDIUM_TIMES 1337 #undef RPP_HISTOGRAM_MEDIUM_TIMES
1353 #undef RPP_PREDICTED_HISTOGRAM_PERCENTAGE 1338 #undef RPP_PREDICTED_HISTOGRAM_PERCENTAGE
1354 #undef RPP_PREDICTED_HISTOGRAM_COUNTS 1339 #undef RPP_PREDICTED_HISTOGRAM_COUNTS
1355 } 1340 }
1356 1341
1342 void ResourcePrefetchPredictor::OnHistoryServiceLoaded(
1343 HistoryService* history_service) {
1344 OnHistoryAndCacheLoaded();
1345 history_service_observer_.Remove(history_service);
1346 }
1347
1348 void ResourcePrefetchPredictor::ConnectToHistoryService() {
1349 // Register for HistoryServiceLoading if it is not ready.
1350 HistoryService* history_service =
1351 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
1352 if (!history_service)
1353 return;
1354 if (history_service->BackendLoaded()) {
1355 // HistoryService is already loaded. Continue with Initialization.
1356 OnHistoryAndCacheLoaded();
1357 return;
1358 }
1359 DCHECK(!history_service_observer_.IsObserving(history_service));
1360 history_service_observer_.Add(history_service);
1361 return;
1362 }
1363
1357 } // namespace predictors 1364 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698