[email protected] | ebbac63e | 2014-08-22 01:43:06 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
blundell | 2102f7c | 2015-07-09 10:00:53 | [diff] [blame] | 5 | #include "components/omnibox/browser/answers_cache.h" |
[email protected] | ebbac63e | 2014-08-22 01:43:06 | [diff] [blame] | 6 | |
brettw | 94a2cc2 | 2015-07-01 19:26:54 | [diff] [blame] | 7 | #include "base/i18n/case_conversion.h" |
[email protected] | ebbac63e | 2014-08-22 01:43:06 | [diff] [blame] | 8 | #include "base/strings/string_util.h" |
| 9 | |
Ted Choc | 8e12e35 | 2018-11-27 03:27:52 | [diff] [blame] | 10 | AnswersQueryData::AnswersQueryData() : query_type(-1) {} |
| 11 | AnswersQueryData::AnswersQueryData(const base::string16& text, int type) |
| 12 | : full_query_text(text), query_type(type) {} |
[email protected] | ebbac63e | 2014-08-22 01:43:06 | [diff] [blame] | 13 | |
| 14 | AnswersCache::AnswersCache(size_t max_entries) : max_entries_(max_entries) { |
| 15 | } |
| 16 | |
| 17 | AnswersCache::~AnswersCache() { |
| 18 | } |
| 19 | |
| 20 | AnswersQueryData AnswersCache::GetTopAnswerEntry(const base::string16& query) { |
brettw | 94a2cc2 | 2015-07-01 19:26:54 | [diff] [blame] | 21 | base::string16 collapsed_query = base::i18n::ToLower( |
| 22 | base::CollapseWhitespace(query, false)); |
jdoerrie | 2e6a651d | 2018-10-04 17:09:08 | [diff] [blame] | 23 | for (auto it = cache_.begin(); it != cache_.end(); ++it) { |
[email protected] | ebbac63e | 2014-08-22 01:43:06 | [diff] [blame] | 24 | // If the query text starts with trimmed input, this is valid prefetch data. |
brettw | 94a2cc2 | 2015-07-01 19:26:54 | [diff] [blame] | 25 | if (base::StartsWith(base::i18n::ToLower(it->full_query_text), |
| 26 | collapsed_query, base::CompareCase::SENSITIVE)) { |
[email protected] | ebbac63e | 2014-08-22 01:43:06 | [diff] [blame] | 27 | // Move the touched item to the front of the list. |
| 28 | cache_.splice(cache_.begin(), cache_, it); |
| 29 | return cache_.front(); |
| 30 | } |
| 31 | } |
| 32 | return AnswersQueryData(); |
| 33 | } |
| 34 | |
| 35 | void AnswersCache::UpdateRecentAnswers(const base::string16& full_query_text, |
Ted Choc | 8e12e35 | 2018-11-27 03:27:52 | [diff] [blame] | 36 | int query_type) { |
[email protected] | ebbac63e | 2014-08-22 01:43:06 | [diff] [blame] | 37 | // If this entry is already part of the cache, just update recency. |
jdoerrie | 2e6a651d | 2018-10-04 17:09:08 | [diff] [blame] | 38 | for (auto it = cache_.begin(); it != cache_.end(); ++it) { |
[email protected] | ebbac63e | 2014-08-22 01:43:06 | [diff] [blame] | 39 | if (full_query_text == it->full_query_text && |
| 40 | query_type == it->query_type) { |
| 41 | cache_.splice(cache_.begin(), cache_, it); |
| 42 | return; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | // Evict if cache size is exceeded. |
| 47 | if (cache_.size() >= max_entries_) |
| 48 | cache_.pop_back(); |
| 49 | |
| 50 | cache_.push_front(AnswersQueryData(full_query_text, query_type)); |
| 51 | } |