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 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 DCHECK(main_profile_predictor_); | 78 DCHECK(main_profile_predictor_); |
79 main_profile_predictor_->incognito_predictor_ = this; | 79 main_profile_predictor_->incognito_predictor_ = this; |
80 if (main_profile_predictor_->initialized_) | 80 if (main_profile_predictor_->initialized_) |
81 CopyFromMainProfile(); | 81 CopyFromMainProfile(); |
82 } else { | 82 } else { |
83 // Request the in-memory database from the history to force it to load so | 83 // Request the in-memory database from the history to force it to load so |
84 // it's available as soon as possible. | 84 // it's available as soon as possible. |
85 history::HistoryService* history_service = | 85 history::HistoryService* history_service = |
86 HistoryServiceFactory::GetForProfile( | 86 HistoryServiceFactory::GetForProfile( |
87 profile_, ServiceAccessType::EXPLICIT_ACCESS); | 87 profile_, ServiceAccessType::EXPLICIT_ACCESS); |
88 if (history_service) | 88 DCHECK(history_service); |
89 history_service->InMemoryDatabase(); | 89 history_service->InMemoryDatabase(); |
90 | 90 |
91 table_ = | 91 table_ = |
92 PredictorDatabaseFactory::GetForProfile(profile_)->autocomplete_table(); | 92 PredictorDatabaseFactory::GetForProfile(profile_)->autocomplete_table(); |
93 | 93 |
94 // Observe all main frame loads so we can wait for the first to complete | 94 // Observe all main frame loads so we can wait for the first to complete |
95 // before accessing DB and IO threads to build the local cache. | 95 // before accessing DB and IO threads to build the local cache. |
96 notification_registrar_.Add(this, | 96 notification_registrar_.Add(this, |
97 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, | 97 content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME, |
98 content::NotificationService::AllSources()); | 98 content::NotificationService::AllSources()); |
99 } | 99 } |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
328 std::vector<AutocompleteActionPredictorTable::Row>* rows = | 328 std::vector<AutocompleteActionPredictorTable::Row>* rows = |
329 new std::vector<AutocompleteActionPredictorTable::Row>(); | 329 new std::vector<AutocompleteActionPredictorTable::Row>(); |
330 content::BrowserThread::PostTaskAndReply( | 330 content::BrowserThread::PostTaskAndReply( |
331 content::BrowserThread::DB, FROM_HERE, | 331 content::BrowserThread::DB, FROM_HERE, |
332 base::Bind(&AutocompleteActionPredictorTable::GetAllRows, table_, rows), | 332 base::Bind(&AutocompleteActionPredictorTable::GetAllRows, table_, rows), |
333 base::Bind(&AutocompleteActionPredictor::CreateCaches, AsWeakPtr(), | 333 base::Bind(&AutocompleteActionPredictor::CreateCaches, AsWeakPtr(), |
334 base::Owned(rows))); | 334 base::Owned(rows))); |
335 } | 335 } |
336 | 336 |
337 void AutocompleteActionPredictor::DeleteAllRows() { | 337 void AutocompleteActionPredictor::DeleteAllRows() { |
338 if (!initialized_) | 338 if (!initialized_) |
pasko
2016/12/15 12:57:35
nit: DCHECK here as well?
dullweber
2016/12/15 14:52:18
Done.
| |
339 return; | 339 return; |
340 | 340 |
341 db_cache_.clear(); | 341 db_cache_.clear(); |
342 db_id_cache_.clear(); | 342 db_id_cache_.clear(); |
343 | 343 |
344 if (table_.get()) { | 344 if (table_.get()) { |
345 content::BrowserThread::PostTask( | 345 content::BrowserThread::PostTask( |
346 content::BrowserThread::DB, FROM_HERE, | 346 content::BrowserThread::DB, FROM_HERE, |
347 base::Bind(&AutocompleteActionPredictorTable::DeleteAllRows, table_)); | 347 base::Bind(&AutocompleteActionPredictorTable::DeleteAllRows, table_)); |
348 } | 348 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 const DBCacheKey key = { it->user_text, it->url }; | 435 const DBCacheKey key = { it->user_text, it->url }; |
436 const DBCacheValue value = { it->number_of_hits, it->number_of_misses }; | 436 const DBCacheValue value = { it->number_of_hits, it->number_of_misses }; |
437 db_cache_[key] = value; | 437 db_cache_[key] = value; |
438 db_id_cache_[key] = it->id; | 438 db_id_cache_[key] = it->id; |
439 } | 439 } |
440 | 440 |
441 // If the history service is ready, delete any old or invalid entries. | 441 // If the history service is ready, delete any old or invalid entries. |
442 history::HistoryService* history_service = | 442 history::HistoryService* history_service = |
443 HistoryServiceFactory::GetForProfile(profile_, | 443 HistoryServiceFactory::GetForProfile(profile_, |
444 ServiceAccessType::EXPLICIT_ACCESS); | 444 ServiceAccessType::EXPLICIT_ACCESS); |
445 if (!TryDeleteOldEntries(history_service)) { | 445 TryDeleteOldEntries(history_service); |
446 // Wait for the notification that the history service is ready and the URL | 446 DCHECK(history_service); |
447 // DB is loaded. | 447 history_service_observer_.Add(history_service); |
448 if (history_service) | |
449 history_service_observer_.Add(history_service); | |
450 } | |
451 } | 448 } |
452 | 449 |
453 bool AutocompleteActionPredictor::TryDeleteOldEntries( | 450 void AutocompleteActionPredictor::TryDeleteOldEntries( |
454 history::HistoryService* service) { | 451 history::HistoryService* service) { |
455 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 452 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
456 DCHECK(!profile_->IsOffTheRecord()); | 453 DCHECK(!profile_->IsOffTheRecord()); |
457 DCHECK(!initialized_); | 454 DCHECK(!initialized_); |
458 | 455 |
459 if (!service) | 456 if (!service) |
460 return false; | 457 return; |
461 | 458 |
462 history::URLDatabase* url_db = service->InMemoryDatabase(); | 459 history::URLDatabase* url_db = service->InMemoryDatabase(); |
463 if (!url_db) | 460 if (!url_db) |
464 return false; | 461 return; |
465 | 462 |
466 DeleteOldEntries(url_db); | 463 DeleteOldEntries(url_db); |
467 return true; | |
468 } | 464 } |
469 | 465 |
470 void AutocompleteActionPredictor::DeleteOldEntries( | 466 void AutocompleteActionPredictor::DeleteOldEntries( |
471 history::URLDatabase* url_db) { | 467 history::URLDatabase* url_db) { |
472 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 468 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
473 DCHECK(!profile_->IsOffTheRecord()); | 469 DCHECK(!profile_->IsOffTheRecord()); |
474 DCHECK(!initialized_); | 470 DCHECK(!initialized_); |
475 DCHECK(table_.get()); | 471 DCHECK(table_.get()); |
476 | 472 |
477 std::vector<AutocompleteActionPredictorTable::Row::Id> ids_to_delete; | 473 std::vector<AutocompleteActionPredictorTable::Row::Id> ids_to_delete; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
562 void AutocompleteActionPredictor::Shutdown() { | 558 void AutocompleteActionPredictor::Shutdown() { |
563 history_service_observer_.RemoveAll(); | 559 history_service_observer_.RemoveAll(); |
564 } | 560 } |
565 | 561 |
566 void AutocompleteActionPredictor::OnURLsDeleted( | 562 void AutocompleteActionPredictor::OnURLsDeleted( |
567 history::HistoryService* history_service, | 563 history::HistoryService* history_service, |
568 bool all_history, | 564 bool all_history, |
569 bool expired, | 565 bool expired, |
570 const history::URLRows& deleted_rows, | 566 const history::URLRows& deleted_rows, |
571 const std::set<GURL>& favicon_urls) { | 567 const std::set<GURL>& favicon_urls) { |
572 if (!initialized_) | 568 DCHECK(initialized_); |
573 return; | |
574 | 569 |
575 if (all_history) | 570 if (all_history) |
576 DeleteAllRows(); | 571 DeleteAllRows(); |
577 else | 572 else |
578 DeleteRowsWithURLs(deleted_rows); | 573 DeleteRowsWithURLs(deleted_rows); |
579 } | 574 } |
580 | 575 |
581 void AutocompleteActionPredictor::OnHistoryServiceLoaded( | 576 void AutocompleteActionPredictor::OnHistoryServiceLoaded( |
582 history::HistoryService* history_service) { | 577 history::HistoryService* history_service) { |
583 TryDeleteOldEntries(history_service); | 578 if (!initialized_) |
584 history_service_observer_.Remove(history_service); | 579 TryDeleteOldEntries(history_service); |
585 } | 580 } |
586 | 581 |
587 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch() { | 582 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch() { |
588 } | 583 } |
589 | 584 |
590 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch( | 585 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch( |
591 const TransitionalMatch& other) = default; | 586 const TransitionalMatch& other) = default; |
592 | 587 |
593 AutocompleteActionPredictor::TransitionalMatch::~TransitionalMatch() { | 588 AutocompleteActionPredictor::TransitionalMatch::~TransitionalMatch() { |
594 } | 589 } |
595 | 590 |
596 } // namespace predictors | 591 } // namespace predictors |
OLD | NEW |