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

Unified 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: Address review comments Created 6 years, 2 months 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/predictors/resource_prefetch_predictor.cc
diff --git a/chrome/browser/predictors/resource_prefetch_predictor.cc b/chrome/browser/predictors/resource_prefetch_predictor.cc
index 878113d3faa552f34d35eb178acd072970c99778..ea1bd3e1732b0320b9e22f5e247d5911a081b585 100644
--- a/chrome/browser/predictors/resource_prefetch_predictor.cc
+++ b/chrome/browser/predictors/resource_prefetch_predictor.cc
@@ -307,7 +307,8 @@ ResourcePrefetchPredictor::ResourcePrefetchPredictor(
initialization_state_(NOT_INITIALIZED),
tables_(PredictorDatabaseFactory::GetForProfile(
profile)->resource_prefetch_tables()),
- results_map_deleter_(&results_map_) {
+ results_map_deleter_(&results_map_),
+ history_service_observer_(this) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Some form of learning has to be enabled.
@@ -325,7 +326,8 @@ void ResourcePrefetchPredictor::RecordURLRequest(
const URLRequestSummary& request) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (initialization_state_ != INITIALIZED)
- return;
+ 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
+ return;
CHECK_EQ(request.resource_type, content::RESOURCE_TYPE_MAIN_FRAME);
OnMainFrameRequest(request);
@@ -335,7 +337,8 @@ void ResourcePrefetchPredictor::RecordURLResponse(
const URLRequestSummary& response) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (initialization_state_ != INITIALIZED)
- return;
+ if (!CheckForHistoryService())
+ return;
if (response.resource_type == content::RESOURCE_TYPE_MAIN_FRAME)
OnMainFrameResponse(response);
@@ -347,7 +350,8 @@ void ResourcePrefetchPredictor::RecordURLRedirect(
const URLRequestSummary& response) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (initialization_state_ != INITIALIZED)
- return;
+ if (!CheckForHistoryService())
+ return;
CHECK_EQ(response.resource_type, content::RESOURCE_TYPE_MAIN_FRAME);
OnMainFrameRedirect(response);
@@ -398,15 +402,6 @@ void ResourcePrefetchPredictor::Observe(
DCHECK_CURRENTLY_ON(BrowserThread::UI);
switch (type) {
- case chrome::NOTIFICATION_HISTORY_LOADED: {
- DCHECK_EQ(initialization_state_, INITIALIZING);
- notification_registrar_.Remove(this,
- chrome::NOTIFICATION_HISTORY_LOADED,
- content::Source<Profile>(profile_));
- OnHistoryAndCacheLoaded();
- break;
- }
-
case chrome::NOTIFICATION_HISTORY_URLS_DELETED: {
DCHECK_EQ(initialization_state_, INITIALIZED);
const content::Details<const history::URLsDeletedDetails>
@@ -461,7 +456,8 @@ void ResourcePrefetchPredictor::OnMainFrameResponse(
const URLRequestSummary& response) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
if (initialization_state_ != INITIALIZED)
- return;
+ if (!CheckForHistoryService())
+ return;
RecordNavigationEvent(NAVIGATION_EVENT_RESPONSE_STARTED);
@@ -694,15 +690,7 @@ void ResourcePrefetchPredictor::CreateCaches(
UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.HostTableHostCount",
host_table_cache_->size());
- // Add notifications for history loading if it is not ready.
- HistoryService* history_service = HistoryServiceFactory::GetForProfile(
- profile_, Profile::EXPLICIT_ACCESS);
- if (!history_service) {
- notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED,
- content::Source<Profile>(profile_));
- } else {
- OnHistoryAndCacheLoaded();
- }
+ CheckForHistoryService();
}
void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() {
@@ -719,6 +707,7 @@ void ResourcePrefetchPredictor::OnHistoryAndCacheLoaded() {
this, config_, profile_->GetRequestContext());
}
+ history_service_observer_.RemoveAll();
initialization_state_ = INITIALIZED;
}
@@ -1221,4 +1210,28 @@ void ResourcePrefetchPredictor::ReportPredictedAccuracyStatsHelper(
#undef RPP_PREDICTED_HISTOGRAM_COUNTS
}
+bool ResourcePrefetchPredictor::CheckForHistoryService() {
+ if (initialization_state_ == INITIALIZED)
+ return true;
+
+ // Register for HistoryServiceLoading if it is not ready.
+ HistoryService* history_service = HistoryServiceFactory::GetForProfile(
+ profile_, Profile::EXPLICIT_ACCESS);
+ if (history_service && history_service->BackendLoaded()) {
+ // HistoryService is already loaded. Continue with Initialization.
+ OnHistoryAndCacheLoaded();
+ return true;
+ } else if (history_service &&
+ !history_service_observer_.IsObserving(history_service)) {
+ history_service_observer_.Add(history_service);
+ }
+ return false;
+}
+
+void ResourcePrefetchPredictor::OnHistoryServiceLoaded(
+ HistoryService* history_service) {
+ OnHistoryAndCacheLoaded();
+ history_service_observer_.Remove(history_service);
+}
+
} // namespace predictors

Powered by Google App Engine
This is Rietveld 408576698