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

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

Issue 10918007: Delay predictor DB access until after the first main frame load complete. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/autocomplete_action_predictor.h" 5 #include "chrome/browser/predictors/autocomplete_action_predictor.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include <vector> 9 #include <vector>
10 10
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 if (profile_->IsOffTheRecord()) { 87 if (profile_->IsOffTheRecord()) {
88 main_profile_predictor_ = AutocompleteActionPredictorFactory::GetForProfile( 88 main_profile_predictor_ = AutocompleteActionPredictorFactory::GetForProfile(
89 profile_->GetOriginalProfile()); 89 profile_->GetOriginalProfile());
90 DCHECK(main_profile_predictor_); 90 DCHECK(main_profile_predictor_);
91 main_profile_predictor_->incognito_predictor_ = this; 91 main_profile_predictor_->incognito_predictor_ = this;
92 if (main_profile_predictor_->initialized_) 92 if (main_profile_predictor_->initialized_)
93 CopyFromMainProfile(); 93 CopyFromMainProfile();
94 } else { 94 } else {
95 // Request the in-memory database from the history to force it to load so 95 // Request the in-memory database from the history to force it to load so
96 // it's available as soon as possible. 96 // it's available as soon as possible.
97 HistoryService* history_service = HistoryServiceFactory::GetForProfile( 97 HistoryService* history_service = HistoryServiceFactory::GetForProfile(
Shishir 2012/08/30 18:51:08 Does it make sense to move this to onload too?
dominich 2012/08/30 18:53:57 In most cases that I've seen, it's already loaded
98 profile_, Profile::EXPLICIT_ACCESS); 98 profile_, Profile::EXPLICIT_ACCESS);
99 if (history_service) 99 if (history_service)
100 history_service->InMemoryDatabase(); 100 history_service->InMemoryDatabase();
101 101
102 table_ = 102 table_ =
103 PredictorDatabaseFactory::GetForProfile(profile_)->autocomplete_table(); 103 PredictorDatabaseFactory::GetForProfile(profile_)->autocomplete_table();
104 104
105 // Create local caches using the database as loaded. We will garbage collect 105 // Observe all main frame loads so we can wait for the first to complete
106 // rows from the caches and the database once the history service is 106 // before accessing DB and IO threads to build the local cache.
107 // available. 107 notification_registrar_.Add(this,
108 std::vector<AutocompleteActionPredictorTable::Row>* rows = 108 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
109 new std::vector<AutocompleteActionPredictorTable::Row>(); 109 content::NotificationService::AllSources());
110 content::BrowserThread::PostTaskAndReply(content::BrowserThread::DB,
111 FROM_HERE,
112 base::Bind(&AutocompleteActionPredictorTable::GetAllRows, table_, rows),
113 base::Bind(&AutocompleteActionPredictor::CreateCaches, AsWeakPtr(),
114 base::Owned(rows)));
115 } 110 }
116 } 111 }
117 112
118 AutocompleteActionPredictor::~AutocompleteActionPredictor() { 113 AutocompleteActionPredictor::~AutocompleteActionPredictor() {
119 if (main_profile_predictor_) 114 if (main_profile_predictor_)
120 main_profile_predictor_->incognito_predictor_ = NULL; 115 main_profile_predictor_->incognito_predictor_ = NULL;
121 else if (incognito_predictor_) 116 else if (incognito_predictor_)
122 incognito_predictor_->main_profile_predictor_ = NULL; 117 incognito_predictor_->main_profile_predictor_ = NULL;
123 if (prerender_handle_.get()) 118 if (prerender_handle_.get())
124 prerender_handle_->OnCancel(); 119 prerender_handle_->OnCancel();
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 bool AutocompleteActionPredictor::IsPreconnectable( 221 bool AutocompleteActionPredictor::IsPreconnectable(
227 const AutocompleteMatch& match) { 222 const AutocompleteMatch& match) {
228 return IsAutocompleteMatchSearchType(match); 223 return IsAutocompleteMatchSearchType(match);
229 } 224 }
230 225
231 void AutocompleteActionPredictor::Observe( 226 void AutocompleteActionPredictor::Observe(
232 int type, 227 int type,
233 const content::NotificationSource& source, 228 const content::NotificationSource& source,
234 const content::NotificationDetails& details) { 229 const content::NotificationDetails& details) {
235 switch (type) { 230 switch (type) {
231 case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME:
232 CreateLocalCachesFromDatabase();
233 notification_registrar_.Remove(
234 this,
235 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
236 content::NotificationService::AllSources());
237 break;
238
236 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: { 239 case chrome::NOTIFICATION_HISTORY_URLS_DELETED: {
237 DCHECK(initialized_); 240 DCHECK(initialized_);
238 const content::Details<const history::URLsDeletedDetails> 241 const content::Details<const history::URLsDeletedDetails>
239 urls_deleted_details = 242 urls_deleted_details =
240 content::Details<const history::URLsDeletedDetails>(details); 243 content::Details<const history::URLsDeletedDetails>(details);
241 if (urls_deleted_details->all_history) 244 if (urls_deleted_details->all_history)
242 DeleteAllRows(); 245 DeleteAllRows();
243 else 246 else
244 DeleteRowsWithURLs(urls_deleted_details->rows); 247 DeleteRowsWithURLs(urls_deleted_details->rows);
245 break; 248 break;
(...skipping 19 matching lines...) Expand all
265 content::Source<Profile>(profile_)); 268 content::Source<Profile>(profile_));
266 break; 269 break;
267 } 270 }
268 271
269 default: 272 default:
270 NOTREACHED() << "Unexpected notification observed."; 273 NOTREACHED() << "Unexpected notification observed.";
271 break; 274 break;
272 } 275 }
273 } 276 }
274 277
278 void AutocompleteActionPredictor::CreateLocalCachesFromDatabase() {
279 // Create local caches using the database as loaded. We will garbage collect
280 // rows from the caches and the database once the history service is
281 // available.
282 std::vector<AutocompleteActionPredictorTable::Row>* rows =
283 new std::vector<AutocompleteActionPredictorTable::Row>();
284 content::BrowserThread::PostTaskAndReply(content::BrowserThread::DB,
285 FROM_HERE,
286 base::Bind(&AutocompleteActionPredictorTable::GetAllRows, table_, rows),
287 base::Bind(&AutocompleteActionPredictor::CreateCaches, AsWeakPtr(),
288 base::Owned(rows)));
289 }
290
275 void AutocompleteActionPredictor::DeleteAllRows() { 291 void AutocompleteActionPredictor::DeleteAllRows() {
276 if (!initialized_) 292 if (!initialized_)
277 return; 293 return;
278 294
279 db_cache_.clear(); 295 db_cache_.clear();
280 db_id_cache_.clear(); 296 db_id_cache_.clear();
281 297
282 if (table_.get()) { 298 if (table_.get()) {
283 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE, 299 content::BrowserThread::PostTask(content::BrowserThread::DB, FROM_HERE,
284 base::Bind(&AutocompleteActionPredictorTable::DeleteAllRows, 300 base::Bind(&AutocompleteActionPredictorTable::DeleteAllRows,
(...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 return number_of_hits / (number_of_hits + value.number_of_misses); 590 return number_of_hits / (number_of_hits + value.number_of_misses);
575 } 591 }
576 592
577 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch() { 593 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch() {
578 } 594 }
579 595
580 AutocompleteActionPredictor::TransitionalMatch::~TransitionalMatch() { 596 AutocompleteActionPredictor::TransitionalMatch::~TransitionalMatch() {
581 } 597 }
582 598
583 } // namespace predictors 599 } // namespace predictors
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698