OLD | NEW |
---|---|
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 Loading... | |
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" |
32 #include "components/omnibox/browser/url_prefix.h" | 33 #include "components/omnibox/browser/url_prefix.h" |
33 #include "components/prefs/pref_service.h" | 34 #include "components/prefs/pref_service.h" |
34 #include "components/url_formatter/url_fixer.h" | 35 #include "components/url_formatter/url_fixer.h" |
35 #include "url/third_party/mozilla/url_parse.h" | 36 #include "url/third_party/mozilla/url_parse.h" |
36 | 37 |
37 namespace { | 38 namespace { |
38 | 39 |
39 class DestinationURLEqualsURL { | 40 class DestinationURLEqualsURL { |
40 public: | 41 public: |
41 explicit DestinationURLEqualsURL(const GURL& url) : url_(url) {} | 42 explicit DestinationURLEqualsURL(const GURL& url) : url_(url) {} |
42 bool operator()(const AutocompleteMatch& match) const { | 43 bool operator()(const AutocompleteMatch& match) const { |
43 return match.destination_url == url_; | 44 return match.destination_url == url_; |
44 } | 45 } |
45 | 46 |
46 private: | 47 private: |
47 const GURL url_; | 48 const GURL url_; |
48 }; | 49 }; |
49 | 50 |
51 // ShortcutMatch holds sufficient information about a single match from the | |
52 // shortcut database to allow for destination deduping and relevance sorting. | |
53 // After those stages the top matches are converted to the more heavyweight | |
54 // AutocompleteMatch struct. Avoiding constructing the larger struct for | |
55 // every such match can save significant time when there are many shortcut | |
56 // matches to process. | |
57 struct ShortcutMatch { | |
58 ShortcutMatch(int relevance, | |
59 const GURL& stripped_destination_url, | |
60 const ShortcutsDatabase::Shortcut* shortcut) | |
61 : relevance(relevance), | |
62 stripped_destination_url(stripped_destination_url), | |
Peter Kasting
2016/04/15 20:23:44
These lines are still not correctly indented. The
Alexander Yashkin
2016/04/16 05:18:17
Apllied git cl format and it fixed this problem..
| |
63 shortcut(shortcut), | |
64 contents(shortcut->match_core.contents), | |
65 type(static_cast<AutocompleteMatch::Type>(shortcut->match_core.type)) {} | |
66 | |
67 int relevance; | |
68 GURL stripped_destination_url; | |
69 const ShortcutsDatabase::Shortcut* shortcut; | |
70 base::string16 contents; | |
71 AutocompleteMatch::Type type; | |
72 }; | |
73 | |
74 // Sorts |matches| by destination, taking into account demotions based on | |
75 // |page_classification| when resolving ties about which of several | |
76 // duplicates to keep. The matches are also deduplicated. | |
77 void SortAndDedupMatches( | |
78 metrics::OmniboxEventProto::PageClassification page_classification, | |
79 std::vector<ShortcutMatch>* matches) { | |
80 // Sort matches such that duplicate matches are consecutive. | |
81 std::sort(matches->begin(), matches->end(), | |
82 DestinationSort<ShortcutMatch>(page_classification)); | |
83 | |
84 // Erase duplicate matches. Duplicate matches are those with | |
85 // stripped_destination_url fields equal and non empty. | |
86 matches->erase( | |
87 std::unique(matches->begin(), matches->end(), | |
88 [](const ShortcutMatch& elem1, const ShortcutMatch& elem2) { | |
89 return !elem1.stripped_destination_url.is_empty() && | |
90 (elem1.stripped_destination_url == | |
91 elem2.stripped_destination_url); | |
92 }), | |
93 matches->end()); | |
94 } | |
95 | |
50 } // namespace | 96 } // namespace |
51 | 97 |
52 const int ShortcutsProvider::kShortcutsProviderDefaultMaxRelevance = 1199; | 98 const int ShortcutsProvider::kShortcutsProviderDefaultMaxRelevance = 1199; |
53 | 99 |
54 ShortcutsProvider::ShortcutsProvider(AutocompleteProviderClient* client) | 100 ShortcutsProvider::ShortcutsProvider(AutocompleteProviderClient* client) |
55 : AutocompleteProvider(AutocompleteProvider::TYPE_SHORTCUTS), | 101 : AutocompleteProvider(AutocompleteProvider::TYPE_SHORTCUTS), |
56 client_(client), | 102 client_(client), |
57 languages_(client_->GetAcceptLanguages()), | 103 languages_(client_->GetAcceptLanguages()), |
58 initialized_(false) { | 104 initialized_(false) { |
59 scoped_refptr<ShortcutsBackend> backend = client_->GetShortcutsBackend(); | 105 scoped_refptr<ShortcutsBackend> backend = client_->GetShortcutsBackend(); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 // completely match the search term. | 177 // completely match the search term. |
132 base::string16 term_string(base::i18n::ToLower(input.text())); | 178 base::string16 term_string(base::i18n::ToLower(input.text())); |
133 DCHECK(!term_string.empty()); | 179 DCHECK(!term_string.empty()); |
134 | 180 |
135 int max_relevance; | 181 int max_relevance; |
136 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( | 182 if (!OmniboxFieldTrial::ShortcutsScoringMaxRelevance( |
137 input.current_page_classification(), &max_relevance)) | 183 input.current_page_classification(), &max_relevance)) |
138 max_relevance = kShortcutsProviderDefaultMaxRelevance; | 184 max_relevance = kShortcutsProviderDefaultMaxRelevance; |
139 TemplateURLService* template_url_service = client_->GetTemplateURLService(); | 185 TemplateURLService* template_url_service = client_->GetTemplateURLService(); |
140 const base::string16 fixed_up_input(FixupUserInput(input).second); | 186 const base::string16 fixed_up_input(FixupUserInput(input).second); |
187 | |
188 std::vector<ShortcutMatch> shortcut_matches; | |
141 for (ShortcutsBackend::ShortcutMap::const_iterator it = | 189 for (ShortcutsBackend::ShortcutMap::const_iterator it = |
142 FindFirstMatch(term_string, backend.get()); | 190 FindFirstMatch(term_string, backend.get()); |
143 it != backend->shortcuts_map().end() && | 191 it != backend->shortcuts_map().end() && |
144 base::StartsWith(it->first, term_string, | 192 base::StartsWith(it->first, term_string, |
145 base::CompareCase::SENSITIVE); | 193 base::CompareCase::SENSITIVE); |
146 ++it) { | 194 ++it) { |
147 // Don't return shortcuts with zero relevance. | 195 // Don't return shortcuts with zero relevance. |
148 int relevance = CalculateScore(term_string, it->second, max_relevance); | 196 int relevance = CalculateScore(term_string, it->second, max_relevance); |
149 if (relevance) { | 197 if (relevance) { |
150 matches_.push_back( | 198 const ShortcutsDatabase::Shortcut& shortcut = it->second; |
151 ShortcutToACMatch(it->second, relevance, input, fixed_up_input)); | 199 GURL stripped_destination_url(AutocompleteMatch::GURLToStrippedGURL( |
152 matches_.back().ComputeStrippedDestinationURL( | 200 shortcut.match_core.destination_url, |
153 input, client_->GetAcceptLanguages(), template_url_service); | 201 input, |
202 languages_, | |
203 template_url_service, | |
204 shortcut.match_core.keyword)); | |
205 shortcut_matches.push_back( | |
206 ShortcutMatch(relevance, stripped_destination_url, &it->second)); | |
154 } | 207 } |
155 } | 208 } |
156 // Remove duplicates. This is important because it's common to have multiple | 209 // 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 | 210 // 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 | 211 // 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 | 212 // clamp to kMaxMatches and let the SortAndDedupMatches take care of |
160 // collapsing the duplicates, we'll effectively only be returning one match, | 213 // collapsing the duplicates, we'll effectively only be returning one match, |
161 // instead of several possibilities. | 214 // instead of several possibilities. |
162 // | 215 // |
163 // Note that while removing duplicates, we don't populate a match's | 216 // 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 | 217 // |duplicate_matches| field--duplicates don't need to be preserved in the |
165 // matches because they are only used for deletions, and this provider | 218 // matches because they are only used for deletions, and this provider |
166 // deletes matches based on the URL. | 219 // deletes matches based on the URL. |
167 AutocompleteResult::DedupMatchesByDestination( | 220 SortAndDedupMatches(input.current_page_classification(), &shortcut_matches); |
168 input.current_page_classification(), false, &matches_); | 221 |
169 // Find best matches. | 222 // Find best matches. |
170 std::partial_sort( | 223 std::partial_sort( |
171 matches_.begin(), | 224 shortcut_matches.begin(), |
172 matches_.begin() + | 225 shortcut_matches.begin() + |
173 std::min(AutocompleteProvider::kMaxMatches, matches_.size()), | 226 std::min(AutocompleteProvider::kMaxMatches, shortcut_matches.size()), |
174 matches_.end(), &AutocompleteMatch::MoreRelevant); | 227 shortcut_matches.end(), |
175 if (matches_.size() > AutocompleteProvider::kMaxMatches) { | 228 [](const ShortcutMatch& elem1, const ShortcutMatch& elem2) { |
176 matches_.erase(matches_.begin() + AutocompleteProvider::kMaxMatches, | 229 // Ensure a stable sort by sorting equal-relevance matches |
177 matches_.end()); | 230 // alphabetically. |
231 return elem1.relevance == elem2.relevance ? | |
232 elem1.contents < elem2.contents : | |
233 elem1.relevance > elem2.relevance; | |
234 }); | |
235 if (shortcut_matches.size() > AutocompleteProvider::kMaxMatches) { | |
236 shortcut_matches.erase( | |
237 shortcut_matches.begin() + AutocompleteProvider::kMaxMatches, | |
238 shortcut_matches.end()); | |
178 } | 239 } |
179 // Guarantee that all scores are decreasing (but do not assign any scores | 240 // Create and initialize autocomplete matches from shortcut matches. |
180 // below 1). | 241 // Also guarantee that all relevance scores are decreasing (but do not assign |
181 for (ACMatches::iterator it = matches_.begin(); it != matches_.end(); ++it) { | 242 // any scores below 1). |
182 max_relevance = std::min(max_relevance, it->relevance); | 243 WordMap terms_map(CreateWordMapForString(term_string)); |
183 it->relevance = max_relevance; | 244 matches_.reserve(shortcut_matches.size()); |
245 for (ShortcutMatch& match : shortcut_matches) { | |
246 max_relevance = std::min(max_relevance, match.relevance); | |
247 matches_.push_back(ShortcutToACMatch(*match.shortcut, max_relevance, input, | |
248 fixed_up_input, term_string, terms_map)); | |
184 if (max_relevance > 1) | 249 if (max_relevance > 1) |
185 --max_relevance; | 250 --max_relevance; |
186 } | 251 } |
187 } | 252 } |
188 | 253 |
189 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( | 254 AutocompleteMatch ShortcutsProvider::ShortcutToACMatch( |
190 const ShortcutsDatabase::Shortcut& shortcut, | 255 const ShortcutsDatabase::Shortcut& shortcut, |
191 int relevance, | 256 int relevance, |
192 const AutocompleteInput& input, | 257 const AutocompleteInput& input, |
193 const base::string16& fixed_up_input_text) { | 258 const base::string16& fixed_up_input_text, |
259 const base::string16 term_string, | |
260 const WordMap& terms_map) { | |
194 DCHECK(!input.text().empty()); | 261 DCHECK(!input.text().empty()); |
195 AutocompleteMatch match; | 262 AutocompleteMatch match; |
196 match.provider = this; | 263 match.provider = this; |
197 match.relevance = relevance; | 264 match.relevance = relevance; |
198 match.deletable = true; | 265 match.deletable = true; |
199 match.fill_into_edit = shortcut.match_core.fill_into_edit; | 266 match.fill_into_edit = shortcut.match_core.fill_into_edit; |
200 match.destination_url = shortcut.match_core.destination_url; | 267 match.destination_url = shortcut.match_core.destination_url; |
201 DCHECK(match.destination_url.is_valid()); | 268 DCHECK(match.destination_url.is_valid()); |
202 match.contents = shortcut.match_core.contents; | 269 match.contents = shortcut.match_core.contents; |
203 match.contents_class = AutocompleteMatch::ClassificationsFromString( | 270 match.contents_class = AutocompleteMatch::ClassificationsFromString( |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 URLPrefix::GetInlineAutocompleteOffset( | 306 URLPrefix::GetInlineAutocompleteOffset( |
240 input.text(), fixed_up_input_text, true, match.fill_into_edit); | 307 input.text(), fixed_up_input_text, true, match.fill_into_edit); |
241 if (inline_autocomplete_offset != base::string16::npos) { | 308 if (inline_autocomplete_offset != base::string16::npos) { |
242 match.inline_autocompletion = | 309 match.inline_autocompletion = |
243 match.fill_into_edit.substr(inline_autocomplete_offset); | 310 match.fill_into_edit.substr(inline_autocomplete_offset); |
244 match.allowed_to_be_default_match = | 311 match.allowed_to_be_default_match = |
245 !HistoryProvider::PreventInlineAutocomplete(input) || | 312 !HistoryProvider::PreventInlineAutocomplete(input) || |
246 match.inline_autocompletion.empty(); | 313 match.inline_autocompletion.empty(); |
247 } | 314 } |
248 } | 315 } |
249 match.EnsureUWYTIsAllowedToBeDefault(input, | 316 match.EnsureUWYTIsAllowedToBeDefault(input, languages_, |
250 client_->GetAcceptLanguages(), | |
251 client_->GetTemplateURLService()); | 317 client_->GetTemplateURLService()); |
252 | 318 |
253 // Try to mark pieces of the contents and description as matches if they | 319 // Try to mark pieces of the contents and description as matches if they |
254 // appear in |input.text()|. | 320 // 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()) { | 321 if (!terms_map.empty()) { |
258 match.contents_class = ClassifyAllMatchesInString( | 322 match.contents_class = ClassifyAllMatchesInString( |
259 term_string, terms_map, match.contents, match.contents_class); | 323 term_string, terms_map, match.contents, match.contents_class); |
260 match.description_class = ClassifyAllMatchesInString( | 324 match.description_class = ClassifyAllMatchesInString( |
261 term_string, terms_map, match.description, match.description_class); | 325 term_string, terms_map, match.description, match.description_class); |
262 } | 326 } |
263 return match; | 327 return match; |
264 } | 328 } |
265 | 329 |
266 // static | 330 // static |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
416 const double kMaxDecaySpeedDivisor = 5.0; | 480 const double kMaxDecaySpeedDivisor = 5.0; |
417 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; | 481 const double kNumUsesPerDecaySpeedDivisorIncrement = 5.0; |
418 double decay_divisor = std::min( | 482 double decay_divisor = std::min( |
419 kMaxDecaySpeedDivisor, | 483 kMaxDecaySpeedDivisor, |
420 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / | 484 (shortcut.number_of_hits + kNumUsesPerDecaySpeedDivisorIncrement - 1) / |
421 kNumUsesPerDecaySpeedDivisorIncrement); | 485 kNumUsesPerDecaySpeedDivisorIncrement); |
422 | 486 |
423 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + | 487 return static_cast<int>((base_score / exp(decay_exponent / decay_divisor)) + |
424 0.5); | 488 0.5); |
425 } | 489 } |
OLD | NEW |