blob: 78736426d28111554528a38b960d5cae875cd061 [file] [log] [blame]
[email protected]ebbac63e2014-08-22 01:43:061// 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
blundell2102f7c2015-07-09 10:00:535#include "components/omnibox/browser/answers_cache.h"
[email protected]ebbac63e2014-08-22 01:43:066
brettw94a2cc22015-07-01 19:26:547#include "base/i18n/case_conversion.h"
[email protected]ebbac63e2014-08-22 01:43:068#include "base/strings/string_util.h"
9
10AnswersQueryData::AnswersQueryData() {
11}
12AnswersQueryData::AnswersQueryData(const base::string16& text,
13 const base::string16& type)
14 : full_query_text(text), query_type(type) {
15}
16
17AnswersCache::AnswersCache(size_t max_entries) : max_entries_(max_entries) {
18}
19
20AnswersCache::~AnswersCache() {
21}
22
23AnswersQueryData AnswersCache::GetTopAnswerEntry(const base::string16& query) {
brettw94a2cc22015-07-01 19:26:5424 base::string16 collapsed_query = base::i18n::ToLower(
25 base::CollapseWhitespace(query, false));
jdoerrie2e6a651d2018-10-04 17:09:0826 for (auto it = cache_.begin(); it != cache_.end(); ++it) {
[email protected]ebbac63e2014-08-22 01:43:0627 // If the query text starts with trimmed input, this is valid prefetch data.
brettw94a2cc22015-07-01 19:26:5428 if (base::StartsWith(base::i18n::ToLower(it->full_query_text),
29 collapsed_query, base::CompareCase::SENSITIVE)) {
[email protected]ebbac63e2014-08-22 01:43:0630 // Move the touched item to the front of the list.
31 cache_.splice(cache_.begin(), cache_, it);
32 return cache_.front();
33 }
34 }
35 return AnswersQueryData();
36}
37
38void 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.
jdoerrie2e6a651d2018-10-04 17:09:0841 for (auto it = cache_.begin(); it != cache_.end(); ++it) {
[email protected]ebbac63e2014-08-22 01:43:0642 if (full_query_text == it->full_query_text &&
43 query_type == it->query_type) {
44 cache_.splice(cache_.begin(), cache_, it);
45 return;
46 }
47 }
48
49 // Evict if cache size is exceeded.
50 if (cache_.size() >= max_entries_)
51 cache_.pop_back();
52
53 cache_.push_front(AnswersQueryData(full_query_text, query_type));
54}