OLD | NEW |
---|---|
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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 } // namespace | 62 } // namespace |
63 | 63 |
64 namespace predictors { | 64 namespace predictors { |
65 | 65 |
66 const int AutocompleteActionPredictor::kMaximumDaysToKeepEntry = 14; | 66 const int AutocompleteActionPredictor::kMaximumDaysToKeepEntry = 14; |
67 | 67 |
68 AutocompleteActionPredictor::AutocompleteActionPredictor(Profile* profile) | 68 AutocompleteActionPredictor::AutocompleteActionPredictor(Profile* profile) |
69 : profile_(profile), | 69 : profile_(profile), |
70 main_profile_predictor_(NULL), | 70 main_profile_predictor_(NULL), |
71 incognito_predictor_(NULL), | 71 incognito_predictor_(NULL), |
72 initialized_(false) { | 72 initialized_(false), |
73 history_service_observer_(this) { | |
73 if (profile_->IsOffTheRecord()) { | 74 if (profile_->IsOffTheRecord()) { |
74 main_profile_predictor_ = AutocompleteActionPredictorFactory::GetForProfile( | 75 main_profile_predictor_ = AutocompleteActionPredictorFactory::GetForProfile( |
75 profile_->GetOriginalProfile()); | 76 profile_->GetOriginalProfile()); |
76 DCHECK(main_profile_predictor_); | 77 DCHECK(main_profile_predictor_); |
77 main_profile_predictor_->incognito_predictor_ = this; | 78 main_profile_predictor_->incognito_predictor_ = this; |
78 if (main_profile_predictor_->initialized_) | 79 if (main_profile_predictor_->initialized_) |
79 CopyFromMainProfile(); | 80 CopyFromMainProfile(); |
80 } else { | 81 } else { |
81 // Request the in-memory database from the history to force it to load so | 82 // Request the in-memory database from the history to force it to load so |
82 // it's available as soon as possible. | 83 // it's available as soon as possible. |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 | 251 |
251 case chrome::NOTIFICATION_OMNIBOX_OPENED_URL: { | 252 case chrome::NOTIFICATION_OMNIBOX_OPENED_URL: { |
252 DCHECK(initialized_); | 253 DCHECK(initialized_); |
253 | 254 |
254 // TODO(dominich): This doesn't need to be synchronous. Investigate | 255 // TODO(dominich): This doesn't need to be synchronous. Investigate |
255 // posting it as a task to be run later. | 256 // posting it as a task to be run later. |
256 OnOmniboxOpenedUrl(*content::Details<OmniboxLog>(details).ptr()); | 257 OnOmniboxOpenedUrl(*content::Details<OmniboxLog>(details).ptr()); |
257 break; | 258 break; |
258 } | 259 } |
259 | 260 |
260 case chrome::NOTIFICATION_HISTORY_LOADED: { | |
261 TryDeleteOldEntries(content::Details<HistoryService>(details).ptr()); | |
262 | |
263 notification_registrar_.Remove(this, | |
264 chrome::NOTIFICATION_HISTORY_LOADED, | |
265 content::Source<Profile>(profile_)); | |
266 break; | |
267 } | |
268 | |
269 default: | 261 default: |
270 NOTREACHED() << "Unexpected notification observed."; | 262 NOTREACHED() << "Unexpected notification observed."; |
271 break; | 263 break; |
272 } | 264 } |
273 } | 265 } |
274 | 266 |
275 void AutocompleteActionPredictor::CreateLocalCachesFromDatabase() { | 267 void AutocompleteActionPredictor::CreateLocalCachesFromDatabase() { |
276 // Create local caches using the database as loaded. We will garbage collect | 268 // Create local caches using the database as loaded. We will garbage collect |
277 // rows from the caches and the database once the history service is | 269 // rows from the caches and the database once the history service is |
278 // available. | 270 // available. |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
468 DCHECK(db_id_cache_.empty()); | 460 DCHECK(db_id_cache_.empty()); |
469 | 461 |
470 for (std::vector<AutocompleteActionPredictorTable::Row>::const_iterator it = | 462 for (std::vector<AutocompleteActionPredictorTable::Row>::const_iterator it = |
471 rows->begin(); it != rows->end(); ++it) { | 463 rows->begin(); it != rows->end(); ++it) { |
472 const DBCacheKey key = { it->user_text, it->url }; | 464 const DBCacheKey key = { it->user_text, it->url }; |
473 const DBCacheValue value = { it->number_of_hits, it->number_of_misses }; | 465 const DBCacheValue value = { it->number_of_hits, it->number_of_misses }; |
474 db_cache_[key] = value; | 466 db_cache_[key] = value; |
475 db_id_cache_[key] = it->id; | 467 db_id_cache_[key] = it->id; |
476 } | 468 } |
477 | 469 |
478 // If the history service is ready, delete any old or invalid entries. | |
sdefresne
2014/10/30 17:37:04
Please restore comment.
nshaik
2014/10/30 21:48:35
Done.
| |
479 HistoryService* history_service = | 470 HistoryService* history_service = |
480 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 471 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); |
481 if (!TryDeleteOldEntries(history_service)) { | 472 TryDeleteOldEntries(history_service); |
sdefresne
2014/10/30 17:37:03
Could you keep the old structure and comment and j
nshaik
2014/10/30 21:48:35
Done.
| |
482 // Wait for the notification that the history service is ready and the URL | |
483 // DB is loaded. | |
484 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_LOADED, | |
485 content::Source<Profile>(profile_)); | |
486 } | |
487 } | 473 } |
488 | 474 |
489 bool AutocompleteActionPredictor::TryDeleteOldEntries(HistoryService* service) { | 475 bool AutocompleteActionPredictor::TryDeleteOldEntries(HistoryService* service) { |
490 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 476 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
491 DCHECK(!profile_->IsOffTheRecord()); | 477 DCHECK(!profile_->IsOffTheRecord()); |
492 DCHECK(!initialized_); | 478 DCHECK(!initialized_); |
493 | 479 |
494 if (!service) | 480 if (!service) |
495 return false; | 481 return false; |
496 | 482 |
497 history::URLDatabase* url_db = service->InMemoryDatabase(); | 483 history::URLDatabase* url_db = service->InMemoryDatabase(); |
498 if (!url_db) | 484 if (!url_db) { |
485 history_service_observer_.Add(service); | |
sdefresne
2014/10/30 17:37:03
See previous comment.
nshaik
2014/10/30 21:48:35
Done.
| |
499 return false; | 486 return false; |
487 } | |
500 | 488 |
501 DeleteOldEntries(url_db); | 489 DeleteOldEntries(url_db); |
502 return true; | 490 return true; |
503 } | 491 } |
504 | 492 |
505 void AutocompleteActionPredictor::DeleteOldEntries( | 493 void AutocompleteActionPredictor::DeleteOldEntries( |
506 history::URLDatabase* url_db) { | 494 history::URLDatabase* url_db) { |
507 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 495 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
508 DCHECK(!profile_->IsOffTheRecord()); | 496 DCHECK(!profile_->IsOffTheRecord()); |
509 DCHECK(!initialized_); | 497 DCHECK(!initialized_); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
602 const double number_of_hits = static_cast<double>(value.number_of_hits); | 590 const double number_of_hits = static_cast<double>(value.number_of_hits); |
603 return number_of_hits / (number_of_hits + value.number_of_misses); | 591 return number_of_hits / (number_of_hits + value.number_of_misses); |
604 } | 592 } |
605 | 593 |
606 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch() { | 594 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch() { |
607 } | 595 } |
608 | 596 |
609 AutocompleteActionPredictor::TransitionalMatch::~TransitionalMatch() { | 597 AutocompleteActionPredictor::TransitionalMatch::~TransitionalMatch() { |
610 } | 598 } |
611 | 599 |
600 void AutocompleteActionPredictor::Shutdown() { | |
601 history_service_observer_.RemoveAll(); | |
602 } | |
603 | |
604 void AutocompleteActionPredictor::OnHistoryServiceLoaded( | |
605 HistoryService* history_service) { | |
606 TryDeleteOldEntries(history_service); | |
607 history_service_observer_.Remove(history_service); | |
608 } | |
609 | |
612 } // namespace predictors | 610 } // namespace predictors |
OLD | NEW |