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.
5 #include "components/omnibox/browser/answers_cache.h"
7 #include "base/i18n/case_conversion.h"
8 #include "base/strings/string_util.h"
10 AnswersQueryData::AnswersQueryData() {
12 AnswersQueryData::AnswersQueryData(const base::string16
& text
,
13 const base::string16
& type
)
14 : full_query_text(text
), query_type(type
) {
17 AnswersCache::AnswersCache(size_t max_entries
) : max_entries_(max_entries
) {
20 AnswersCache::~AnswersCache() {
23 AnswersQueryData
AnswersCache::GetTopAnswerEntry(const base::string16
& query
) {
24 base::string16 collapsed_query
= base::i18n::ToLower(
25 base::CollapseWhitespace(query
, false));
26 for (Cache::iterator it
= cache_
.begin(); it
!= cache_
.end(); ++it
) {
27 // If the query text starts with trimmed input, this is valid prefetch data.
28 if (base::StartsWith(base::i18n::ToLower(it
->full_query_text
),
29 collapsed_query
, base::CompareCase::SENSITIVE
)) {
30 // Move the touched item to the front of the list.
31 cache_
.splice(cache_
.begin(), cache_
, it
);
32 return cache_
.front();
35 return AnswersQueryData();
38 void AnswersCache::UpdateRecentAnswers(const base::string16
& full_query_text
,
39 const base::string16
& query_type
) {
40 // If this entry is already part of the cache, just update recency.
41 for (Cache::iterator it
= cache_
.begin(); it
!= cache_
.end(); ++it
) {
42 if (full_query_text
== it
->full_query_text
&&
43 query_type
== it
->query_type
) {
44 cache_
.splice(cache_
.begin(), cache_
, it
);
49 // Evict if cache size is exceeded.
50 if (cache_
.size() >= max_entries_
)
53 cache_
.push_front(AnswersQueryData(full_query_text
, query_type
));