Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(440)

Side by Side Diff: components/omnibox/browser/shortcuts_provider.cc

Issue 1877833002: Optimize shortcuts provider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added new files Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "components/omnibox/browser/shortcuts_provider.h" 5 #include "components/omnibox/browser/shortcuts_provider.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
(...skipping 10 matching lines...) Expand all
21 #include "base/time/time.h" 21 #include "base/time/time.h"
22 #include "base/trace_event/trace_event.h" 22 #include "base/trace_event/trace_event.h"
23 #include "components/history/core/browser/history_service.h" 23 #include "components/history/core/browser/history_service.h"
24 #include "components/metrics/proto/omnibox_input_type.pb.h" 24 #include "components/metrics/proto/omnibox_input_type.pb.h"
25 #include "components/omnibox/browser/autocomplete_i18n.h" 25 #include "components/omnibox/browser/autocomplete_i18n.h"
26 #include "components/omnibox/browser/autocomplete_input.h" 26 #include "components/omnibox/browser/autocomplete_input.h"
27 #include "components/omnibox/browser/autocomplete_match.h" 27 #include "components/omnibox/browser/autocomplete_match.h"
28 #include "components/omnibox/browser/autocomplete_provider_client.h" 28 #include "components/omnibox/browser/autocomplete_provider_client.h"
29 #include "components/omnibox/browser/autocomplete_result.h" 29 #include "components/omnibox/browser/autocomplete_result.h"
30 #include "components/omnibox/browser/history_provider.h" 30 #include "components/omnibox/browser/history_provider.h"
31 #include "components/omnibox/browser/match_compare.h"
31 #include "components/omnibox/browser/omnibox_field_trial.h" 32 #include "components/omnibox/browser/omnibox_field_trial.h"
33 #include "components/omnibox/browser/shortcut_match.h"
32 #include "components/omnibox/browser/url_prefix.h" 34 #include "components/omnibox/browser/url_prefix.h"
33 #include "components/prefs/pref_service.h" 35 #include "components/prefs/pref_service.h"
34 #include "components/url_formatter/url_fixer.h" 36 #include "components/url_formatter/url_fixer.h"
35 #include "url/third_party/mozilla/url_parse.h" 37 #include "url/third_party/mozilla/url_parse.h"
36 38
37 namespace { 39 namespace {
38 40
39 class DestinationURLEqualsURL { 41 class DestinationURLEqualsURL {
40 public: 42 public:
41 explicit DestinationURLEqualsURL(const GURL& url) : url_(url) {} 43 explicit DestinationURLEqualsURL(const GURL& url) : url_(url) {}
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 // completely match the search term. 133 // completely match the search term.
132 base::string16 term_string(base::i18n::ToLower(input.text())); 134 base::string16 term_string(base::i18n::ToLower(input.text()));
133 DCHECK(!term_string.empty()); 135 DCHECK(!term_string.empty());
134 136
135 int max_relevance; 137 int max_relevance;
136 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( 138 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance(
137 input.current_page_classification(), &max_relevance)) 139 input.current_page_classification(), &max_relevance))
138 max_relevance = kShortcutsProviderDefaultMaxRelevance; 140 max_relevance = kShortcutsProviderDefaultMaxRelevance;
139 TemplateURLService* template_url_service = client_->GetTemplateURLService(); 141 TemplateURLService* template_url_service = client_->GetTemplateURLService();
140 const base::string16 fixed_up_input(FixupUserInput(input).second); 142 const base::string16 fixed_up_input(FixupUserInput(input).second);
143
144 ShortcutMatches shortcut_matches;
141 for (ShortcutsBackend::ShortcutMap::const_iterator it = 145 for (ShortcutsBackend::ShortcutMap::const_iterator it =
142 FindFirstMatch(term_string, backend.get()); 146 FindFirstMatch(term_string, backend.get());
143 it != backend->shortcuts_map().end() && 147 it != backend->shortcuts_map().end() &&
144 base::StartsWith(it->first, term_string, 148 base::StartsWith(it->first, term_string,
145 base::CompareCase::SENSITIVE); 149 base::CompareCase::SENSITIVE);
146 ++it) { 150 ++it) {
147 // Don't return shortcuts with zero relevance. 151 // Don't return shortcuts with zero relevance.
148 int relevance = CalculateScore(term_string, it->second, max_relevance); 152 int relevance = CalculateScore(term_string, it->second, max_relevance);
149 if (relevance) { 153 if (relevance) {
150 matches_.push_back( 154 const ShortcutsDatabase::Shortcut& shortcut = it->second;
151 ShortcutToACMatch(it->second, relevance, input, fixed_up_input)); 155 GURL stripped_destination_url(AutocompleteMatch::GURLToStrippedGURL(
152 matches_.back().ComputeStrippedDestinationURL( 156 shortcut.match_core.destination_url,
153 input, client_->GetAcceptLanguages(), template_url_service); 157 input,
158 languages_,
159 template_url_service,
160 shortcut.match_core.keyword));
161 shortcut_matches.emplace_back(relevance, stripped_destination_url,
Peter Kasting 2016/04/12 00:55:09 See the recommendations on https://chromium-cpp.ap
Alexander Yashkin 2016/04/12 09:09:21 replaced with push_back().
162 &(it->second));
Peter Kasting 2016/04/12 00:55:08 Nit: All lines of args should be indented even
Alexander Yashkin 2016/04/12 09:09:20 Done.
154 } 163 }
155 } 164 }
156 // Remove duplicates. This is important because it's common to have multiple 165 // Remove duplicates. This is important because it's common to have multiple
157 // shortcuts pointing to the same URL, e.g., ma, mai, and mail all pointing 166 // shortcuts pointing to the same URL, e.g., ma, mai, and mail all pointing
158 // to mail.google.com, so typing "m" will return them all. If we then simply 167 // to mail.google.com, so typing "m" will return them all. If we then simply
159 // clamp to kMaxMatches and let the AutocompleteResult take care of 168 // clamp to kMaxMatches and let the AutocompleteResult take care of
160 // collapsing the duplicates, we'll effectively only be returning one match, 169 // collapsing the duplicates, we'll effectively only be returning one match,
161 // instead of several possibilities. 170 // instead of several possibilities.
162 // 171 //
163 // Note that while removing duplicates, we don't populate a match's 172 // Note that while removing duplicates, we don't populate a match's
164 // |duplicate_matches| field--duplicates don't need to be preserved in the 173 // |duplicate_matches| field--duplicates don't need to be preserved in the
165 // matches because they are only used for deletions, and this provider 174 // matches because they are only used for deletions, and this provider
166 // deletes matches based on the URL. 175 // deletes matches based on the URL.
167 AutocompleteResult::DedupMatchesByDestination( 176 ShortcutMatch::DedupShortcutMatchesByDestination(
168 input.current_page_classification(), false, &matches_); 177 input.current_page_classification(),
178 &shortcut_matches);
179
180 CompareWithDemoteByType<ShortcutMatch>
Peter Kasting 2016/04/12 00:55:09 Why did you templatize CompareWithDemoteByType and
Alexander Yashkin 2016/04/12 09:09:20 Fixed. I missed that CompareWithDemoteByType is u
181 comparing_object(input.current_page_classification());
182
169 // Find best matches. 183 // Find best matches.
170 std::partial_sort( 184 std::partial_sort(
171 matches_.begin(), 185 shortcut_matches.begin(),
172 matches_.begin() + 186 shortcut_matches.begin() +
173 std::min(AutocompleteProvider::kMaxMatches, matches_.size()), 187 std::min(AutocompleteProvider::kMaxMatches, shortcut_matches.size()),
174 matches_.end(), &AutocompleteMatch::MoreRelevant); 188 shortcut_matches.end(),
175 if (matches_.size() > AutocompleteProvider::kMaxMatches) { 189 comparing_object);
176 matches_.erase(matches_.begin() + AutocompleteProvider::kMaxMatches, 190 if (shortcut_matches.size() > AutocompleteProvider::kMaxMatches) {
177 matches_.end()); 191 shortcut_matches.erase(
192 shortcut_matches.begin() + AutocompleteProvider::kMaxMatches,
193 shortcut_matches.end());
178 } 194 }
179 // Guarantee that all scores are decreasing (but do not assign any scores 195 // Create and initialize autocomplete matches from shortcut matches.
180 // below 1). 196 // Also guarantee that all relevance scores are decreasing
181 for (ACMatches::iterator it = matches_.begin(); it != matches_.end(); ++it) { 197 // (but do not assign any scores below 1).
182 max_relevance = std::min(max_relevance, it->relevance); 198 WordMap terms_map(CreateWordMapForString(term_string));
183 it->relevance = max_relevance; 199 matches_.reserve(shortcut_matches.size());
200 for (ShortcutMatch& match : shortcut_matches) {
201 max_relevance = std::min(max_relevance, match.relevance);
202 match.relevance = max_relevance;
184 if (max_relevance > 1) 203 if (max_relevance > 1)
185 --max_relevance; 204 --max_relevance;
205 matches_.push_back(ShortcutMatchToACMatch(match, input, fixed_up_input,
206 term_string, terms_map));
Peter Kasting 2016/04/12 00:55:08 Nit: All lines of args should be indented even
Alexander Yashkin 2016/04/12 09:09:20 Done.
186 } 207 }
187 } 208 }
188 209
189 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( 210 AutocompleteMatch ShortcutsProvider::ShortcutMatchToACMatch(
190 const ShortcutsDatabase::Shortcut& shortcut, 211 const ShortcutMatch& shortcut_match,
191 int relevance,
192 const AutocompleteInput& input, 212 const AutocompleteInput& input,
193 const base::string16& fixed_up_input_text) { 213 const base::string16& fixed_up_input_text,
214 const base::string16 term_string,
215 const WordMap& terms_map) {
194 DCHECK(!input.text().empty()); 216 DCHECK(!input.text().empty());
217 const ShortcutsDatabase::Shortcut& shortcut = *shortcut_match.shortcut;
195 AutocompleteMatch match; 218 AutocompleteMatch match;
196 match.provider = this; 219 match.provider = this;
197 match.relevance = relevance; 220 match.relevance = shortcut_match.relevance;
198 match.deletable = true; 221 match.deletable = true;
199 match.fill_into_edit = shortcut.match_core.fill_into_edit; 222 match.fill_into_edit = shortcut.match_core.fill_into_edit;
200 match.destination_url = shortcut.match_core.destination_url; 223 match.destination_url = shortcut.match_core.destination_url;
201 DCHECK(match.destination_url.is_valid()); 224 DCHECK(match.destination_url.is_valid());
202 match.contents = shortcut.match_core.contents; 225 match.contents = shortcut.match_core.contents;
203 match.contents_class = AutocompleteMatch::ClassificationsFromString( 226 match.contents_class = AutocompleteMatch::ClassificationsFromString(
204 shortcut.match_core.contents_class); 227 shortcut.match_core.contents_class);
205 match.description = shortcut.match_core.description; 228 match.description = shortcut.match_core.description;
206 match.description_class = AutocompleteMatch::ClassificationsFromString( 229 match.description_class = AutocompleteMatch::ClassificationsFromString(
207 shortcut.match_core.description_class); 230 shortcut.match_core.description_class);
208 match.transition = ui::PageTransitionFromInt(shortcut.match_core.transition); 231 match.transition = ui::PageTransitionFromInt(shortcut.match_core.transition);
209 match.type = static_cast<AutocompleteMatch::Type>(shortcut.match_core.type); 232 match.type = static_cast<AutocompleteMatch::Type>(shortcut.match_core.type);
210 match.keyword = shortcut.match_core.keyword; 233 match.keyword = shortcut.match_core.keyword;
234 match.stripped_destination_url = shortcut_match.stripped_destination_url;
211 match.RecordAdditionalInfo("number of hits", shortcut.number_of_hits); 235 match.RecordAdditionalInfo("number of hits", shortcut.number_of_hits);
212 match.RecordAdditionalInfo("last access time", shortcut.last_access_time); 236 match.RecordAdditionalInfo("last access time", shortcut.last_access_time);
213 match.RecordAdditionalInfo("original input text", 237 match.RecordAdditionalInfo("original input text",
214 base::UTF16ToUTF8(shortcut.text)); 238 base::UTF16ToUTF8(shortcut.text));
215 239
216 // Set |inline_autocompletion| and |allowed_to_be_default_match| if possible. 240 // Set |inline_autocompletion| and |allowed_to_be_default_match| if possible.
217 // If the match is a search query this is easy: simply check whether the 241 // If the match is a search query this is easy: simply check whether the
218 // user text is a prefix of the query. If the match is a navigation, we 242 // user text is a prefix of the query. If the match is a navigation, we
219 // assume the fill_into_edit looks something like a URL, so we use 243 // assume the fill_into_edit looks something like a URL, so we use
220 // URLPrefix::GetInlineAutocompleteOffset() to try and strip off any prefixes 244 // URLPrefix::GetInlineAutocompleteOffset() to try and strip off any prefixes
(...skipping 19 matching lines...) Expand all
240 input.text(), fixed_up_input_text, true, match.fill_into_edit); 264 input.text(), fixed_up_input_text, true, match.fill_into_edit);
241 if (inline_autocomplete_offset != base::string16::npos) { 265 if (inline_autocomplete_offset != base::string16::npos) {
242 match.inline_autocompletion = 266 match.inline_autocompletion =
243 match.fill_into_edit.substr(inline_autocomplete_offset); 267 match.fill_into_edit.substr(inline_autocomplete_offset);
244 match.allowed_to_be_default_match = 268 match.allowed_to_be_default_match =
245 !HistoryProvider::PreventInlineAutocomplete(input) || 269 !HistoryProvider::PreventInlineAutocomplete(input) ||
246 match.inline_autocompletion.empty(); 270 match.inline_autocompletion.empty();
247 } 271 }
248 } 272 }
249 match.EnsureUWYTIsAllowedToBeDefault(input, 273 match.EnsureUWYTIsAllowedToBeDefault(input,
250 client_->GetAcceptLanguages(), 274 languages_,
251 client_->GetTemplateURLService()); 275 client_->GetTemplateURLService());
252 276
253 // Try to mark pieces of the contents and description as matches if they 277 // Try to mark pieces of the contents and description as matches if they
254 // appear in |input.text()|. 278 // appear in |input.text()|.
255 const base::string16 term_string = base::i18n::ToLower(input.text());
256 WordMap terms_map(CreateWordMapForString(term_string));
257 if (!terms_map.empty()) { 279 if (!terms_map.empty()) {
258 match.contents_class = ClassifyAllMatchesInString( 280 match.contents_class = ClassifyAllMatchesInString(
259 term_string, terms_map, match.contents, match.contents_class); 281 term_string, terms_map, match.contents, match.contents_class);
260 match.description_class = ClassifyAllMatchesInString( 282 match.description_class = ClassifyAllMatchesInString(
261 term_string, terms_map, match.description, match.description_class); 283 term_string, terms_map, match.description, match.description_class);
262 } 284 }
263 return match; 285 return match;
264 } 286 }
265 287
266 // static 288 // static
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 const double kMaxDecaySpeedDivisor = 5.0; 438 const double kMaxDecaySpeedDivisor = 5.0;
417 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; 439 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0;
418 double decay_divisor = std::min( 440 double decay_divisor = std::min(
419 kMaxDecaySpeedDivisor, 441 kMaxDecaySpeedDivisor,
420 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / 442 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) /
421 kNumUsesPerDecaySpeedDivisorIncrement); 443 kNumUsesPerDecaySpeedDivisorIncrement);
422 444
423 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + 445 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) +
424 0.5); 446 0.5);
425 } 447 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698