-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
zero_suggest_cache_service.cc
119 lines (95 loc) · 3.64 KB
/
zero_suggest_cache_service.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/omnibox/browser/zero_suggest_cache_service.h"
#include "base/metrics/histogram_functions.h"
#include "base/trace_event/memory_usage_estimator.h"
#include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_provider_client.h"
#include "components/omnibox/browser/omnibox_prefs.h"
#include "components/omnibox/browser/search_suggestion_parser.h"
#include "components/prefs/pref_service.h"
using CacheEntry = ZeroSuggestCacheService::CacheEntry;
ZeroSuggestCacheService::ZeroSuggestCacheService(PrefService* prefs,
size_t cache_size)
: prefs_(prefs), cache_(cache_size) {
DCHECK_GT(cache_size, 0u);
if (prefs_) {
// Load cached ZPS response for NTP from prior session via prefs.
ntp_entry_.response_json =
omnibox::GetUserPreferenceForZeroSuggestCachedResponse(prefs_,
/*page_url=*/"");
}
}
ZeroSuggestCacheService::~ZeroSuggestCacheService() {
if (prefs_) {
// Dump cached ZPS response for NTP to prefs.
omnibox::SetUserPreferenceForZeroSuggestCachedResponse(
prefs_, /*page_url=*/"", /*response=*/ntp_entry_.response_json);
}
}
CacheEntry ZeroSuggestCacheService::ReadZeroSuggestResponse(
const std::string& page_url) const {
// Read cached ZPS response for NTP.
if (page_url.empty()) {
return ntp_entry_;
}
// Read cached ZPS response for SRP/Web.
const auto it = cache_.Get(page_url);
return it != cache_.end() ? it->second : CacheEntry();
}
void ZeroSuggestCacheService::StoreZeroSuggestResponse(
const std::string& page_url,
const CacheEntry& response) {
if (page_url.empty()) {
// Write ZPS response for NTP to cache.
ntp_entry_ = response;
} else {
// Write ZPS response for SRP/Web to cache.
cache_.Put(page_url, response);
}
base::UmaHistogramCounts1M(
"Omnibox.ZeroSuggestProvider.CacheMemoryUsage",
base::trace_event::EstimateMemoryUsage(cache_) +
base::trace_event::EstimateMemoryUsage(ntp_entry_));
for (auto& observer : observers_) {
observer.OnZeroSuggestResponseUpdated(page_url, response);
}
}
void ZeroSuggestCacheService::ClearCache() {
cache_.Clear();
}
bool ZeroSuggestCacheService::IsCacheEmpty() const {
return cache_.empty();
}
void ZeroSuggestCacheService::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void ZeroSuggestCacheService::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
CacheEntry::CacheEntry() = default;
CacheEntry::CacheEntry(const std::string& response_json)
: response_json(response_json) {}
CacheEntry::CacheEntry(const CacheEntry& entry) = default;
CacheEntry::~CacheEntry() = default;
SearchSuggestionParser::SuggestResults CacheEntry::GetSuggestResults(
const AutocompleteInput& input,
const AutocompleteProviderClient& client) const {
SearchSuggestionParser::Results results;
auto response_data =
SearchSuggestionParser::DeserializeJsonData(response_json);
if (!response_data) {
return results.suggest_results;
}
if (!SearchSuggestionParser::ParseSuggestResults(
*response_data, input, client.GetSchemeClassifier(),
/*default_result_relevance=*/100, /*is_keyword_result=*/false,
&results)) {
return results.suggest_results;
}
return results.suggest_results;
}
size_t CacheEntry::EstimateMemoryUsage() const {
return base::trace_event::EstimateMemoryUsage(response_json);
}