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 317 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 DCHECK(initialized_); |
339 return; | |
340 | 339 |
341 db_cache_.clear(); | 340 db_cache_.clear(); |
342 db_id_cache_.clear(); | 341 db_id_cache_.clear(); |
343 | 342 |
344 if (table_.get()) { | 343 if (table_.get()) { |
345 content::BrowserThread::PostTask( | 344 content::BrowserThread::PostTask( |
346 content::BrowserThread::DB, FROM_HERE, | 345 content::BrowserThread::DB, FROM_HERE, |
347 base::Bind(&AutocompleteActionPredictorTable::DeleteAllRows, table_)); | 346 base::Bind(&AutocompleteActionPredictorTable::DeleteAllRows, table_)); |
348 } | 347 } |
349 | 348 |
350 UMA_HISTOGRAM_ENUMERATION("AutocompleteActionPredictor.DatabaseAction", | 349 UMA_HISTOGRAM_ENUMERATION("AutocompleteActionPredictor.DatabaseAction", |
351 DATABASE_ACTION_DELETE_ALL, DATABASE_ACTION_COUNT); | 350 DATABASE_ACTION_DELETE_ALL, DATABASE_ACTION_COUNT); |
352 } | 351 } |
353 | 352 |
354 void AutocompleteActionPredictor::DeleteRowsWithURLs( | 353 void AutocompleteActionPredictor::DeleteRowsWithURLs( |
355 const history::URLRows& rows) { | 354 const history::URLRows& rows) { |
356 if (!initialized_) | 355 DCHECK(initialized_); |
357 return; | |
358 | 356 |
359 std::vector<AutocompleteActionPredictorTable::Row::Id> id_list; | 357 std::vector<AutocompleteActionPredictorTable::Row::Id> id_list; |
360 | 358 |
361 for (DBCacheMap::iterator it = db_cache_.begin(); it != db_cache_.end();) { | 359 for (DBCacheMap::iterator it = db_cache_.begin(); it != db_cache_.end();) { |
362 if (std::find_if(rows.begin(), rows.end(), | 360 if (std::find_if(rows.begin(), rows.end(), |
363 history::URLRow::URLRowHasURL(it->first.url)) != | 361 history::URLRow::URLRowHasURL(it->first.url)) != |
364 rows.end()) { | 362 rows.end()) { |
365 const DBIdCacheMap::iterator id_it = db_id_cache_.find(it->first); | 363 const DBIdCacheMap::iterator id_it = db_id_cache_.find(it->first); |
366 DCHECK(id_it != db_id_cache_.end()); | 364 DCHECK(id_it != db_id_cache_.end()); |
367 id_list.push_back(id_it->second); | 365 id_list.push_back(id_it->second); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
435 const DBCacheKey key = { it->user_text, it->url }; | 433 const DBCacheKey key = { it->user_text, it->url }; |
436 const DBCacheValue value = { it->number_of_hits, it->number_of_misses }; | 434 const DBCacheValue value = { it->number_of_hits, it->number_of_misses }; |
437 db_cache_[key] = value; | 435 db_cache_[key] = value; |
438 db_id_cache_[key] = it->id; | 436 db_id_cache_[key] = it->id; |
439 } | 437 } |
440 | 438 |
441 // If the history service is ready, delete any old or invalid entries. | 439 // If the history service is ready, delete any old or invalid entries. |
442 history::HistoryService* history_service = | 440 history::HistoryService* history_service = |
443 HistoryServiceFactory::GetForProfile(profile_, | 441 HistoryServiceFactory::GetForProfile(profile_, |
444 ServiceAccessType::EXPLICIT_ACCESS); | 442 ServiceAccessType::EXPLICIT_ACCESS); |
445 if (!TryDeleteOldEntries(history_service)) { | 443 TryDeleteOldEntries(history_service); |
446 // Wait for the notification that the history service is ready and the URL | 444 if (history_service) |
pasko
2016/12/15 15:29:48
nit: extra check for the pointer in TryDeleteOldEn
| |
447 // DB is loaded. | 445 history_service_observer_.Add(history_service); |
448 if (history_service) | |
449 history_service_observer_.Add(history_service); | |
450 } | |
451 } | 446 } |
452 | 447 |
453 bool AutocompleteActionPredictor::TryDeleteOldEntries( | 448 void AutocompleteActionPredictor::TryDeleteOldEntries( |
454 history::HistoryService* service) { | 449 history::HistoryService* service) { |
455 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 450 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
456 DCHECK(!profile_->IsOffTheRecord()); | 451 DCHECK(!profile_->IsOffTheRecord()); |
457 DCHECK(!initialized_); | 452 DCHECK(!initialized_); |
458 | 453 |
459 if (!service) | 454 if (!service) |
460 return false; | 455 return; |
461 | 456 |
462 history::URLDatabase* url_db = service->InMemoryDatabase(); | 457 history::URLDatabase* url_db = service->InMemoryDatabase(); |
463 if (!url_db) | 458 if (!url_db) |
464 return false; | 459 return; |
465 | 460 |
466 DeleteOldEntries(url_db); | 461 DeleteOldEntries(url_db); |
467 return true; | |
468 } | 462 } |
469 | 463 |
470 void AutocompleteActionPredictor::DeleteOldEntries( | 464 void AutocompleteActionPredictor::DeleteOldEntries( |
471 history::URLDatabase* url_db) { | 465 history::URLDatabase* url_db) { |
472 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 466 CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
473 DCHECK(!profile_->IsOffTheRecord()); | 467 DCHECK(!profile_->IsOffTheRecord()); |
474 DCHECK(!initialized_); | 468 DCHECK(!initialized_); |
475 DCHECK(table_.get()); | 469 DCHECK(table_.get()); |
476 | 470 |
477 std::vector<AutocompleteActionPredictorTable::Row::Id> ids_to_delete; | 471 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() { | 556 void AutocompleteActionPredictor::Shutdown() { |
563 history_service_observer_.RemoveAll(); | 557 history_service_observer_.RemoveAll(); |
564 } | 558 } |
565 | 559 |
566 void AutocompleteActionPredictor::OnURLsDeleted( | 560 void AutocompleteActionPredictor::OnURLsDeleted( |
567 history::HistoryService* history_service, | 561 history::HistoryService* history_service, |
568 bool all_history, | 562 bool all_history, |
569 bool expired, | 563 bool expired, |
570 const history::URLRows& deleted_rows, | 564 const history::URLRows& deleted_rows, |
571 const std::set<GURL>& favicon_urls) { | 565 const std::set<GURL>& favicon_urls) { |
572 if (!initialized_) | 566 DCHECK(initialized_); |
573 return; | |
574 | 567 |
575 if (all_history) | 568 if (all_history) |
576 DeleteAllRows(); | 569 DeleteAllRows(); |
577 else | 570 else |
578 DeleteRowsWithURLs(deleted_rows); | 571 DeleteRowsWithURLs(deleted_rows); |
579 } | 572 } |
580 | 573 |
581 void AutocompleteActionPredictor::OnHistoryServiceLoaded( | 574 void AutocompleteActionPredictor::OnHistoryServiceLoaded( |
582 history::HistoryService* history_service) { | 575 history::HistoryService* history_service) { |
583 TryDeleteOldEntries(history_service); | 576 if (!initialized_) |
584 history_service_observer_.Remove(history_service); | 577 TryDeleteOldEntries(history_service); |
585 } | 578 } |
586 | 579 |
587 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch() { | 580 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch() { |
588 } | 581 } |
589 | 582 |
590 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch( | 583 AutocompleteActionPredictor::TransitionalMatch::TransitionalMatch( |
591 const TransitionalMatch& other) = default; | 584 const TransitionalMatch& other) = default; |
592 | 585 |
593 AutocompleteActionPredictor::TransitionalMatch::~TransitionalMatch() { | 586 AutocompleteActionPredictor::TransitionalMatch::~TransitionalMatch() { |
594 } | 587 } |
595 | 588 |
596 } // namespace predictors | 589 } // namespace predictors |
OLD | NEW |